summarylogtreecommitdiffstats
path: root/PKGBUILD
blob: ec0d0d02be1a48d1b4284d8dbb9b30cc1dcd3005 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Maintainer: Johnothan King <johnothanking dot protonmail at com>
# Contributor: Adrien Wu <adrien.sf.wu dot gmail at com>
# Contributor: Head_on_a_Stick <matthew.t.hoare dot gmail at com>

_pkgname='ksh93'
pkgname="${_pkgname}-git"
pkgver=r1850.c692ee1a
pkgrel=1
pkgdesc="KornShell 93u+m, fork based on ksh 93u+"
arch=('x86_64' 'i686' 'pentium4' 'powerpc64le' 'powerpc64' 'powerpc' 'riscv64' 'arm' 'armv6h' 'armv7h' 'aarch64')
url='https://github.com/ksh93/ksh/'
license=('EPL')
depends=('glibc')
makedepends=('git')
conflicts=('ksh' 'ksh93')
provides=('ksh' 'ksh93')
install='ksh93.install'
source=("${_pkgname}::git+https://github.com/ksh93/ksh#branch=dev"
	'sample.kshrc')
sha512sums=('SKIP'
	'd201874dc09457457a544c8d5d4ddd193ee3384a3af0716daa786fd7dc5aa0660765a42361353e9e828c6b8414af463bc14f3c7398333bb6c6cc19549b7855ac')

pkgver() {
	cd "${_pkgname}"
	printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
	cd "${srcdir}/${_pkgname}"
	# Build with SHOPT_ALL_LIBCMD (aka enable all ksh builtins) if ${_all_libcmd} is enabled.
	if [[ ${_all_libcmd} == 1 || ${_all_libcmd} == yes || ${_all_libcmd} == true ]]; then
		sed -i 's/ALL_LIBCMD=0/ALL_LIBCMD=1/g' src/cmd/ksh93/SHOPT.sh
	fi
	# 'bin/package install' requires that the file(1) command be functional.
	# The package() function in all PKGBUILD scripts is run in a fakeroot
	# environment, which by default has seccomp restrictions that stop
	# file from operating. To work around this, file is passed the --no-sandbox
	# flag via an alias inserted into the bin/package script.
	sed -i '1s/^/alias file="file --no-sandbox" /' bin/package
}

build() {
	cd "${srcdir}/${_pkgname}"
	# Get rid of any leftover build files (in case makepkg.conf changed).
	rm -rf ./arch
	if ! [[ ${_ksh_pgo} == 1 || ${_ksh_pgo} == yes || ${_ksh_pgo} == true ]] || [[ $CC == clang ]]; then
		export CCFLAGS=${CFLAGS}  # bin/package uses CCFLAGS rather than CFLAGS.
		./bin/package make        # Build ksh (no -j flag because that's still experimental).
	else
		# Optional and experimental: Compile ksh with profile guided optimization (making
		# use of the regression tests) if ${_ksh_pgo} is enabled.
		# Only GCC has been successfully tested; Clang is completely unsupported.
		local save_ccflags=${CFLAGS}
		local save_ldflags=${LDFLAGS}
		# Obtain the number of CPU cores.
		local -i cores=$(bin/package host cpu)
		# Create a temporary directory to use for PGO
		local tmpdir="${PWD}/pgotmp-$SRANDOM"
		mkdir "$tmpdir"
		# Build with profiling flags set (-fno-unroll-loops increases overall
		# performance slightly according to my results from shbench).
		local generation_flags="-fprofile-dir=\"${tmpdir}\" -fprofile-generate=\"${tmpdir}\" -fno-unroll-loops"
		local use_flags="-fprofile-dir=\"${tmpdir}\" -fprofile-use=\"${tmpdir}\" -fprofile-correction -fno-unroll-loops -Wno-error=coverage-mismatch"
		export CCFLAGS="${save_ccflags} ${generation_flags}"
		export LDFLAGS="${save_ldflags} ${generation_flags}"
		bin/package make -j${cores}
		# Run the regression tests to profile ksh
		local -i status=0
		./arch/*/bin/ksh bin/shtests -u || status=$?
		# For any curious script readers, the only reason
		# some test failures are tolerated is because ksh's
		# test suite suffers from intermittent test failures
		# (see: https://github.com/ksh93/ksh/issues/344).
		# Regardless, only a few expected possible test failures
		# should be tolerated. Abort if there are too many (or
		# if ksh instantly exited with SIGSEGV/SIGABRT etc.).
		if ((status > 9)); then
			echo "Too many test failures; aborting build..."
			false
		fi
		# Second build after obtaining profiling data
		export CCFLAGS="${save_ccflags} ${use_flags}"
		export LDFLAGS="${save_ldflags} ${use_flags}"
		rm -rf ./arch
		./bin/package make -j${cores}
		# Cleanup PGO artefacts
		rm -r "${tmpdir}"
	fi
}

package() {
	# Folder creation in ${pkgdir}
	cd "${srcdir}"
	install -dm0755 "${pkgdir}/etc/skel"
	install -dm0755 "${pkgdir}/usr/share/ksh"
	install -dm0755 "${pkgdir}/usr/share/doc/ksh"
	install -dm0755 "${pkgdir}/usr/share/licenses/ksh"

	# Install the example kshrc provided with the PKGBUILD
	install -Dm0644 sample.kshrc "${pkgdir}/etc/skel/.kshrc"

	# Use the bin/package script to install the core components of ksh + man pages
	cd "${srcdir}/$_pkgname"
	./bin/package install "${pkgdir}/usr"

	# Install license files and additional documentation
	install -Dm0644 LICENSE.md "${pkgdir}/usr/share/licenses/ksh/LICENSE.md"
	install -Dm0644 COPYRIGHT "${pkgdir}/usr/share/licenses/ksh/COPYRIGHT"
	local _doc
	for _doc in 'builtins.mm' 'COMPATIBILITY' 'DESIGN' 'OBSOLETE' 'PROMO.mm' 'README' 'README-AUDIT.md' \
			'RELEASE' 'RELEASE88' 'RELEASE93' 'sh.memo' 'TYPES'; do
		install -Dm0644 "src/cmd/${_pkgname}/${_doc}" "${pkgdir}/usr/share/doc/ksh/${_doc}"
	done

	# Create relevant relative symlinks for alternate ksh names
	local _kshname
	for _kshname in 'ksh93' 'rksh' 'rksh93'; do
		ln -sr "${pkgdir}/usr/bin/ksh" "${pkgdir}/usr/bin/${_kshname}"
		ln -sr "${pkgdir}/usr/share/man/man1/ksh.1" "${pkgdir}/usr/share/man/man1/${_kshname}.1"
	done

	# Move ksh-specific /usr/share/fun to /usr/share/ksh/functions
	mv "${pkgdir}/usr/share/fun" "${pkgdir}/usr/share/ksh/functions"
}