abuild.in 48.2 KB
Newer Older
Natanael Copa's avatar
Natanael Copa committed
1 2
#!/bin/sh

3
# abuild - build apk packages (light version of makepkg)
Natanael Copa's avatar
Natanael Copa committed
4 5 6 7 8
# Copyright (c) 2008 Natanael Copa <natanael.copa@gmail.com>
#
# Distributed under GPL-2
#

Natanael Copa's avatar
Natanael Copa committed
9 10
abuild_ver=@VERSION@
sysconfdir=@sysconfdir@
11
abuildrepo_base=@abuildrepo@
Natanael Copa's avatar
Natanael Copa committed
12
datadir=@datadir@
Natanael Copa's avatar
Natanael Copa committed
13

14
abuild_path=$(readlink -f $0)
Natanael Copa's avatar
Natanael Copa committed
15

16 17 18 19 20 21
if ! [ -f "$datadir/functions.sh" ]; then
	echo "$datadir/functions.sh: not found" >&2
	exit 1
fi
. "$datadir/functions.sh"

Natanael Copa's avatar
Natanael Copa committed
22
# defaults
23
BUILD_BASE="build-base"
24

25 26 27 28 29
: ${FAKEROOT:="fakeroot"}
: ${SUDO_APK:="abuild-apk"}
: ${APK:="apk"}
: ${ADDUSER:="abuild-adduser"}
: ${ADDGROUP:="abuild-addgroup"}
30

31
apk_opt_wait="--wait 30"
32

33 34 35 36 37
# run optional log command for remote logging
logcmd() {
	${ABUILD_LOG_CMD:-true} "$@"
	return 0
}
Natanael Copa's avatar
Natanael Copa committed
38

39
# we override the default msg, warning and error as we want the pkgname
Natanael Copa's avatar
Natanael Copa committed
40
msg() {
41
	[ -n "$quiet" ] && return 0
Natanael Copa's avatar
Natanael Copa committed
42 43 44
	local prompt="$GREEN>>>${NORMAL}"
	local fake="${FAKEROOTKEY:+${BLUE}*${NORMAL}}"
	local name="${STRONG}${subpkgname:-$pkgname}${NORMAL}"
45 46 47
	printf "${prompt} ${name}${fake}: %s\n" "$1" >&2
}

Natanael Copa's avatar
Natanael Copa committed
48 49 50 51
warning() {
	local prompt="${YELLOW}>>> WARNING:${NORMAL}"
	local fake="${FAKEROOTKEY:+${BLUE}*${NORMAL}}"
	local name="${STRONG}${subpkgname:-$pkgname}${NORMAL}"
52 53 54
	printf "${prompt} ${name}${fake}: %s\n" "$1" >&2
}

Natanael Copa's avatar
Natanael Copa committed
55 56 57 58
error() {
	local prompt="${RED}>>> ERROR:${NORMAL}"
	local fake="${FAKEROOTKEY:+${BLUE}*${NORMAL}}"
	local name="${STRONG}${subpkgname:-$pkgname}${NORMAL}"
59
	printf "${prompt} ${name}${fake}: %s\n" "$1" >&2
60
	logcmd "ERROR: $pkgname: $1"
61 62
}

63 64 65 66
cross_compiling() {
	test "$CBUILD" != "$CHOST" -a -n "$CBUILDROOT"
}

Linux User's avatar
Linux User committed
67
cleanup() {
68
	local i=
Linux User's avatar
Linux User committed
69
	set_xterm_title ""
70 71 72
	if [ -n "$CLEANUP_FILES" ]; then
		rm -f $CLEANUP_FILES
	fi
73 74 75 76 77 78 79
	for i; do
		case $i in
		pkgdir) msg "Cleaning up pkgdir"; rm -rf "$pkgbasedir";;
		srcdir) msg "Cleaning up srcdir"; rm -rf "$srcdir";;
		deps)
			if [ -z "$install_after" ] && [ -n "$uninstall_after" ]; then
				msg "Uninstalling dependencies..."
80
				$SUDO_APK del --quiet $apk_opt_wait $uninstall_after
81
				if cross_compiling; then
82 83 84 85
					$SUDO_APK del --root "$CBUILDROOT" \
						--quiet $apk_opt_wait \
						$uninstall_after
				fi
86 87 88 89
			fi
			;;
		esac
	done
Linux User's avatar
Linux User committed
90 91
}

Natanael Copa's avatar
Natanael Copa committed
92
die() {
Linux User's avatar
Linux User committed
93
	error "$@"
94
	cleanup $ERROR_CLEANUP
Natanael Copa's avatar
Natanael Copa committed
95 96 97
	exit 1
}

98 99 100 101
spell_error() {
	die "APKBUILD contains '$1'. It should be '$2'"
}

102
# check if apkbuild is basicly sane
103
default_sanitycheck() {
104
	local i= j= suggestion=
105
	msg "Checking sanity of $APKBUILD..."
106 107 108
	[ -z "$pkgname" ] && die "Missing pkgname in APKBUILD"
	[ -z "${pkgname##* *}" ] && die "pkgname contains spaces"
	[ -z "$pkgver" ] && die "Missing pkgver in APKBUILD"
109
	if [ "$pkgver" != "volatile" ] && [ -z "$nodeps" ]; then
110
		$APK version --check --quiet "$pkgver" ||\
111 112
			die "$pkgver is not a valid version"
	fi
113
	[ -z "$pkgrel" ] && die "Missing pkgrel in APKBUILD"
114 115 116
	[ -z "$pkgdesc" ] && die "Missing pkgdesc in APKBUILD"
	[ -z "$url" ] && die "Missing url in APKBUILD"
	[ -z "$license" ] && die "Missing license in APKBULID"
117 118 119
	if [ $(echo "$pkgdesc" | wc -c) -gt 128 ]; then
		die "pkgdesc is too long"
	fi
120
	is_function package || warning "Missing package() function in APKBUILD"
121

122 123
	if [ -n "$replaces_priority" ] \
		&& ! echo $replaces_priority | egrep -q '^[0-9]+$'; then
124 125
		die "replaces_priority must be a number"
	fi
126 127 128 129 130 131 132
	# check so no package names starts with -
	for i in $pkgname $subpackages; do
		case $i in
		-*) die "${i%:*} is not a valid package name";;
		esac
	done

133
	# check if CARCH, CBUILD, CHOST and CTARGET is set
134 135
	[ -z "$CHOST" ] && die "Please set CHOST in /etc/abuild.conf"

136
	if [ -z "$CARCH" ]; then
Natanael Copa's avatar
Natanael Copa committed
137
		die "Please fix CHOST, or set CARCH in abuild.conf"
138
	fi
139

Natanael Copa's avatar
Natanael Copa committed
140
	for i in $install; do
141
		local n=${i%.*}
142
		local suff=${i##*.}
143 144 145 146 147
		case "$suff" in
		pre-install|post-install|pre-upgrade|post-upgrade|pre-deinstall|post-deinstall);;
		*) die "$i: unknown install script suffix"
		esac
		if ! subpackages_has "$n" && [ "$n" != "$pkgname" ]; then
148
			die "$i: install script does not match pkgname or any subpackage"
149
		fi
150
		[ -e "$startdir/$i" ] || die "install script $i is missing"
151 152 153 154 155 156
		for j in chown chmod chgrp; do
			if grep -q $j "$startdir"/$i; then
				warning "$i: found $j"
				warning2 "Permissions should be fixed in APKBUILD package()"
			fi
		done
Natanael Copa's avatar
Natanael Copa committed
157
	done
158

159 160 161
	for i in $triggers; do
		local f=${i%=*}
		local p=${f%.trigger}
162
		[ "$f" = "$i" ] && die "$f: triggers must contain '='"
163 164 165 166
		[ "$p" = "$f" ] && die "$f: triggers scripts must have .trigger suffix"
		if ! subpackages_has "$p" && [ "$p" != "$pkgname" ]; then
			die "$p: trigger script does not match pkgname or any subpackage"
		fi
Natanael Copa's avatar
Natanael Copa committed
167

168 169
		[ -e "$startdir"/$f ] || die "trigger script $f is missing"
	done
170 171
	if [ -n "$source" ]; then
		for i in $source; do
Natanael Copa's avatar
Natanael Copa committed
172 173 174 175
			if install_has "$i"; then
				warning "You should not have \$install in source"
				continue
			fi
176
			case "$i" in
177
				*::*) i=${i%%::*};;
178
				https://*) makedepends_has wget && warning "wget no longer need to be in makedepends when source has https://" ;;
179
			esac
180 181
			list_has ${i##*/} $md5sums $sha256sums $sha512sums \
				|| die "${i##*/} is missing in checksums"
182 183
		done
	fi
Natanael Copa's avatar
Natanael Copa committed
184

185 186 187 188 189 190 191 192
	# verify that things listed in checksum also is listed in source
	local algo=
	for algo in md5 sha256 sha512; do
		eval set -- \$${algo}sums
		while [ $# -gt 1 ]; do
			local file="$2"
			shift 2
			source_has $file || die "$file exists in ${algo}sums but is missing in source"
193
		done
194
	done
195

196
	# common spelling errors
197 198 199 200 201
	[ -n "$depend" ] && spell_error depend depends
	[ -n "$makedepend" ] && spell_error makedepend makedepends
	[ -n "$pkguser" ] && spell_error pkguser pkgusers
	[ -n "$pkggroup" ] && spell_error pkggroup pkggroups
	[ -n "$subpackage" ] && spell_error subpackage subpackages
Natanael Copa's avatar
Natanael Copa committed
202 203

	grep '^# Maintainer:' $APKBUILD >/dev/null || warning "No maintainer"
204 205

	makedepends_has 'g++' && warning "g++ should not be in makedepends"
206 207 208
	return 0
}

209 210 211 212
sanitycheck() {
	default_sanitycheck
}

213 214
sumcheck() {
	local algo="$1" sums="$2"
215
	local dummy f endreturnval originalparams origin file
216 217 218 219 220 221 222 223

	# get number of checksums
	set -- $sums
	local numsums=$(( $# / 2 ))

	set -- $source
	if [ $# -ne $numsums ]; then
		die "Number of ${algo}sums($numsums) does not correspond to number of sources($#)"
224
	fi
225
	fetch || return 1
226
	msg "Checking ${algo}sums..."
Natanael Copa's avatar
Natanael Copa committed
227
	cd "$srcdir" || return 1
228 229
	IFS=$'\n'
	endreturnval=0
230
	for src in $sums; do
231
		origin=$1; shift
232
		echo "$src" | ${algo}sum -c
233
		if [ $? -ne 0 ]; then
234
			endreturnval=1
235
			is_remote $origin || continue
236
			echo "Because the remote file above failed the ${algo}sum check it will be deleted."
237 238 239 240 241 242 243 244
			echo "Rebuilding will cause it to re-download which in some cases may fix the problem."
			file=`echo "$src" | sed 's/.*[ \t\n]\(.*\)/\1/'`
			echo "Deleting: $file"
			rm $file
		fi
	done
	unset IFS
	return $endreturnval
Natanael Copa's avatar
Natanael Copa committed
245 246
}

247 248 249 250 251 252 253 254 255
# for compatibility
md5check() {
	warning "'md5check' is deprecated. Use 'verify' instead"
	sumcheck md5 "$md5sums"
}

# verify checksums
verify() {
	local verified=false algo=
256
	for algo in sha512 sha256 sha1 md5; do
257 258 259 260 261 262 263
		local sums=
		eval sums=\"\$${algo}sums\"
		if [ -z "$sums" ] || [ -z "$source" ]; then
			continue
		fi
		sumcheck "$algo" "$sums" || return 1
		verified=true
264
		break
265 266 267 268 269 270 271
	done
	if [ -n "$source" ] && ! $verified; then
		die "Use 'abuild checksum' to generate/update the checksum(s)"
	fi
	return 0
}

272 273
# verify upstream sources
sourcecheck() {
274 275 276 277 278 279 280 281
	local uri
	for uri in $source; do
		is_remote $uri || continue
		case "$uri" in
		saveas-*://*)
			uri=${uri#saveas-}
			uri=${uri%/*}
			;;
282 283 284
		*::*)
			uri=${uri##*::}
			;;
285 286
		esac
		wget -q -s "$uri" || return 1
287
	done
288
	return 0
289
}
290

291 292 293
# convert curl options to wget options and call wget instead of curl
wget_fallback() {
	local wget_opts= outfile= opt=
294
	while getopts "C:Lko:s" opt; do
295
		case $opt in
296
		'L') ;; # --location. wget does this by default
297
		'f') ;; # --fail. wget does this by default
298 299 300 301 302 303 304 305 306 307
		'C') wget_opts="$wget_opts -c";; # --continue-at
		's') wget_opts="$wget_opts -q";; # --silent
		'o') wget_opts="$wget_opts -O $OPTARG";; # --output
		'k') wget_opts="$wget_opts --no-check-certificate";; #gnu wget
		esac
	done
	shift $(( $OPTIND - 1 ))
	wget $wget_opts "$1"
}

Natanael Copa's avatar
Natanael Copa committed
308 309
uri_fetch() {
	local uri="$1"
310
	local d="${uri##*/}"	# $(basename $uri)
311
	local opts
312
	[ -n "$quiet" ] && opts="-s"
313 314

	local lockfile="$SRCDEST/$d".lock
315

316 317 318 319 320
	# fix saveas-*://* URIs
	case "$uri" in
		# remove 'saveas-' from beginning and
		# '/filename' from end of URI
		saveas-*://*) uri="${uri:7:$(expr ${#uri} - 7 - ${#d} - 1)}";;
321 322 323 324 325

		*::*)
			d=${uri%%::*}
			uri=${uri#$d::}
			;;
326
	esac
327 328

	case "$uri" in
329
		https://*) opts="-k";;
330
	esac
331

Natanael Copa's avatar
Natanael Copa committed
332
	mkdir -p "$SRCDEST"
333 334 335

	CLEANUP_FILES="$CLEANUP_FILES $lockfile"
	(
336 337
	flock -n -x 9 || msg "Waiting for ${lockfile##*/}..."
	flock -x 9
338 339 340

	[ -f "$SRCDEST/$d" ] && exit 0 # use exit since its a subshell

341 342
	if [ -f "$SRCDEST/$d.part" ]; then
		msg "Partial download found. Trying to resume"
343
		opts="$opts -C -"
Natanael Copa's avatar
Natanael Copa committed
344
	fi
Linux User's avatar
Linux User committed
345
	msg "Fetching $uri"
346 347

	# fallback to wget if curl is missing. useful for bootstrapping
348
	local fetcher=
349 350
	if ! [ -x "$(which curl)" ]; then
		fetcher=wget_fallback
351 352
	else
		fetcher=curl
353
		opts="$opts -L -f -k"
354 355 356
	fi

	$fetcher $opts -o "$SRCDEST/$d.part" "$uri" \
357
		&& mv "$SRCDEST/$d.part" "$SRCDEST/$d"
358

359
	) 9>$lockfile
360 361 362 363

	local rc=$?
	rm -f "$lockfile"
	return $rc
Natanael Copa's avatar
Natanael Copa committed
364 365
}

366
is_remote() {
367
	case "${1#*::}" in
368
		http://*|ftp://*|https://*|saveas-*://*)
369 370 371 372 373
			return 0;;
	esac
	return 1
}

374 375 376 377 378 379 380 381 382
filename_from_uri() {
	local uri="$1"
	local filename="${uri##*/}"  # $(basename $uri)
	case "$uri" in
	*::*) filename=${uri%%::*};;
	esac
	echo "$filename"
}

383 384 385 386
# try download from file from mirror first
uri_fetch_mirror() {
	local uri="$1"
	if [ -n "$DISTFILES_MIRROR" ]; then
387
		if is_remote "$DISTFILES_MIRROR"; then
388 389
			uri_fetch "$DISTFILES_MIRROR"/$(filename_from_uri $uri)\
				&& return 0
390
		else
391 392
			cp "$DISTFILES_MIRROR"/$(filename_from_uri $uri) \
				"$SRCDEST" && return 0
393
		fi
394 395 396 397
	fi
	uri_fetch "$uri"
}

398
default_fetch() {
Natanael Copa's avatar
Natanael Copa committed
399 400
	local s
	mkdir -p "$srcdir"
401 402
	for s in $source; do
		if is_remote "$s"; then
403
			uri_fetch_mirror "$s" || return 1
404
			ln -sf "$SRCDEST/$(filename_from_uri $s)" "$srcdir"/
405
		else
406 407
			ln -sf "$startdir/$s" "$srcdir/"
		fi
Natanael Copa's avatar
Natanael Copa committed
408 409 410
	done
}

411 412 413 414
fetch() {
	default_fetch
}

Natanael Copa's avatar
Natanael Copa committed
415 416 417 418 419 420 421 422 423 424 425 426
# verify that all init.d scripts are openrc runscripts
initdcheck() {
	local i
	for i in $source; do
		case $i in
		*.initd)
			head -n 1 "$srcdir"/$i | grep -q '/sbin/runscript' \
				&& continue
			error "$i is not an openrc #!/sbin/runscript"
			return 1
			;;
		esac
427
	done
Natanael Copa's avatar
Natanael Copa committed
428 429
}

Natanael Copa's avatar
Natanael Copa committed
430
# unpack the sources
431
default_unpack() {
Natanael Copa's avatar
Natanael Copa committed
432
	local u
433
	if [ -z "$force" ]; then
434
		verify || return 1
Natanael Copa's avatar
Natanael Copa committed
435
		initdcheck || return 1
436
	fi
Natanael Copa's avatar
Natanael Copa committed
437 438
	mkdir -p "$srcdir"
	for u in $source; do
439
		local s="$SRCDEST/$(filename_from_uri $u)"
Natanael Copa's avatar
Natanael Copa committed
440
		case "$s" in
441 442 443
			*.tar)
				msg "Unpacking $s..."
				tar -C "$srcdir" -xf "$s" || return 1;;
Natanael Copa's avatar
Natanael Copa committed
444
			*.tar.gz|*.tgz)
445
				msg "Unpacking $s..."
Natanael Copa's avatar
Natanael Copa committed
446 447
				tar -C "$srcdir" -zxf "$s" || return 1;;
			*.tar.bz2)
448
				msg "Unpacking $s..."
Natanael Copa's avatar
Natanael Copa committed
449
				tar -C "$srcdir" -jxf "$s" || return 1;;
450 451 452 453
			*.tar.lzma)
				msg "Unpacking $s..."
				unlzma -c "$s" | tar -C "$srcdir" -x  \
					|| return 1;;
454 455 456
			*.tar.xz)
				msg "Unpacking $s..."
				unxz -c "$s" | tar -C "$srcdir" -x || return 1;;
Natanael Copa's avatar
Natanael Copa committed
457 458 459
			*.zip)
				msg "Unpacking $s..."
				unzip "$s" -d "$srcdir" || return 1;;
Natanael Copa's avatar
Natanael Copa committed
460 461 462 463
		esac
	done
}

464 465 466 467
unpack() {
	default_unpack
}

Natanael Copa's avatar
Natanael Copa committed
468 469
# cleanup source and package dir
clean() {
470
	msg "Cleaning temporary build dirs..."
Natanael Copa's avatar
Natanael Copa committed
471
	rm -rf "$srcdir"
472
	rm -rf "$pkgbasedir"
Natanael Copa's avatar
Natanael Copa committed
473 474 475 476 477 478
}

# cleanup fetched sources
cleancache() {
	local s
	for s in $source; do
479
		if is_remote "$s"; then
480 481 482
			s=$(filename_from_uri $s)
			msg "Cleaning downloaded $s ..."
			rm -f "$SRCDEST/$s"
483
		fi
Natanael Copa's avatar
Natanael Copa committed
484 485 486
	done
}

487 488 489 490 491 492 493 494 495 496
listpkgnames() {
	local i
	for i in $pkgname $subpackages; do
		echo ${i%:*}
	done
	for i in $linguas; do
		echo $pkgname-lang-$i
	done
}

Natanael Copa's avatar
Natanael Copa committed
497 498
cleanpkg() {
	local i
499
	getpkgver || return 1
500
	msg "Cleaning built packages..."
501
	for i in $(listpkgnames); do
502 503
		local p="${i%:*}-$pkgver-r$pkgrel"
		rm -f "$PKGDEST/$p.apk" "$PKGDEST/$p.src.tar.gz" \
504
			"$abuildrepo"/$p.apk "$abuildrepo"/*/$p.apk
Natanael Copa's avatar
Natanael Copa committed
505
	done
506
	# remove given packages from index
507
	update_abuildrepo_index
Natanael Copa's avatar
Natanael Copa committed
508 509
}

510 511 512
# clean all packages except current
cleanoldpkg() {
	local i j
513
	getpkgver || return 1
514
	msg "Cleaning all packages except $pkgver-r$pkgrel..."
515
	for i in $(listpkgnames); do
516 517 518 519 520
		local pn=${i%:*}
		for j in "$PKGDEST"/$pn-[0-9]*.apk ; do
			[ "$j" = "$PKGDEST/$pn-$pkgver-r$pkgrel.apk" ] \
				&& continue
			rm -f "$j" "$abuildrepo"/*/${j##*/}
521 522 523 524
		done
	done
	return 0
}
525 526 527

mkusers() {
	local i
528 529 530 531 532 533
	for i in $pkggroups; do
		if ! getent group $i >/dev/null; then
			msg "Creating group $i"
			$ADDGROUP -S $i || return 1
		fi
	done
534 535
	for i in $pkgusers; do
		if ! getent passwd $i >/dev/null; then
536
			local gopt=
537
			msg "Creating user $i"
538 539 540
			if getent group $i >/dev/null; then
				gopt="-G $i"
			fi
541
			$ADDUSER -S -D -H $gopt $i || return 1
542 543 544 545
		fi
	done
}

Natanael Copa's avatar
Natanael Copa committed
546 547
# helper to update config.sub to a recent version
update_config_sub() {
548
	local changed=false
Natanael Copa's avatar
Natanael Copa committed
549
	find . -name config.sub | while read f; do
550 551
		if ! ./$f armv6-alpine-linux-muslgnueabihf 2>/dev/null; then
			msg "Updating $f"
552 553 554
			cp "$datadir"/${f##*/} "$f" || return 1
			changed=true
		else
555
			msg "No update needed for $f"
556 557 558
		fi
		# pipe subshell will return status of last command
		$changed
Natanael Copa's avatar
Natanael Copa committed
559
	done
560
	return $?
Natanael Copa's avatar
Natanael Copa committed
561
}
562

Natanael Copa's avatar
Natanael Copa committed
563 564
runpart() {
	local part=$1
565
	[ -n "$DEBUG" ] && msg "$part"
Natanael Copa's avatar
Natanael Copa committed
566 567 568 569
	$part || die "$part failed"
}

# override those in your build script
570 571 572 573 574 575 576 577
getpkgver() {
	# this func is supposed to be overridden by volatile packages
	if [ "$pkgver" = "volatile" ]; then
		error "Please provide a getpkgver() function in your APKBUILD"
		return 1
	fi
}

Natanael Copa's avatar
Natanael Copa committed
578 579 580 581
prepare() {
	:
}

Natanael Copa's avatar
Natanael Copa committed
582
build() {
583
	:
Natanael Copa's avatar
Natanael Copa committed
584 585
}

586 587 588
# generate a simple tar.gz package of pkgdir
targz() {
	cd "$pkgdir" || return 1
Natanael Copa's avatar
Natanael Copa committed
589
	mkdir -p "$PKGDEST"
590 591 592
	tar -czf "$PKGDEST"/$pkgname-$pkgver-r$pkgrel.tar.gz *
}

593 594 595 596 597 598 599 600 601
get_split_func() {
	# get the 'func' from "sub-pkg:func"
	local func=${1##*:}

	# get 'func' from "sub-pkg-func" if there was no :func
	[ "$func" = "$1" ] && func=${func##*-}
	echo $func
}

602 603 604
postcheck() {
	local dir="$1" name="$2" i=
	msg "Running postcheck for $name"
605
	# checking for FHS compat
Natanael Copa's avatar
Natanael Copa committed
606 607 608 609 610 611 612
	if ! options_has "!fhs"; then
		for i in "$dir"/srv/* "$dir"/usr/local/* "$dir"/opt/*; do
			if [ -e "$i" ]; then
				error "Packages must not put anything under /srv, /usr/local or /opt"
				return 1
			fi
		done
613 614 615 616
		if [ -d "$dir"/usr/var ]; then
			error "Found /usr/var, localstatedir is most likely wrong"
			return 1
		fi
Natanael Copa's avatar
Natanael Copa committed
617
	fi
618 619 620 621 622 623 624
	# look for *.la files
	i=$(find "$dir" -name '*.la' | sed "s|^$dir|\t|")
	if [ -n "$i" ] && ! options_has "libtool"; then
		error "Libtool archives (*.la) files found and \$options has no 'libtool' flag:"
		echo "$i"
		return 1
	fi
625 626 627 628 629 630
	# look for /usr/lib/charset.alias
	if [ -e "$dir"/usr/lib/charset.alias ] \
			&& ! options_has "charset.alias"; then
		error "Found /usr/lib/charset.alias"
		return 1
	fi
631 632 633 634 635 636
	# check directory permissions
	i=$(find "$dir" -type d -perm -777 | sed "s|^$dir|\t|")
	if [ -n "$i" ]; then
		warning "World writeable directories found:"
		echo "$i"
	fi
637
	# check so we dont have any suid root binaries that are not
638 639 640 641 642 643 644 645
	i=$(find "$dir" -type f -perm +6000 \
		| xargs scanelf --nobanner --etype ET_EXEC \
		| sed "s|ET_EXEC $dir|\t|")
	if [ -n "$i" ]; then
		error "Found non-PIE files that has SUID:"
		echo "$i"
		return 1
	fi
646 647 648 649 650 651 652 653 654
	# test for textrels
	if ! options_has "textrels"; then
		local res="$(scanelf --recursive --textrel --quiet "$dir")"
		if [ -n "$res" ]; then
			error "Found textrels:"
			echo "$res"
			return 1
		fi
	fi
655 656 657
	return 0
}

658
prepare_subpackages() {
Natanael Copa's avatar
Natanael Copa committed
659 660
	local i
	cd "$startdir"
661
	for i in $subpackages; do
662 663
		local func=$(get_split_func $i)
		# call abuild recursively, setting subpkg{dir,name}
664
		msg "Running split function $func..."
665
		local dir="$pkgbasedir/${i%:*}" name="${i%:*}"
666 667 668
		( subpkgdir="$dir" subpkgname="$name" \
			$0 $func prepare_package \
			&& postcheck "$dir" "$name" ) || return 1
Natanael Copa's avatar
Natanael Copa committed
669
	done
670
	postcheck "$pkgdir" "$pkgname" || return 1
671 672
	# post check for /usr/share/locale
	if [ -d "$pkgdir"/usr/share/locale ]; then
673
		warning "Found /usr/share/locale"
674
		warning2 "Maybe add \$pkgname-lang to subpackages?"
675
	fi
Natanael Copa's avatar
Natanael Copa committed
676 677
}

678
default_lang() {
679
	pkgdesc="Languages for package $pkgname"
680 681 682 683 684 685 686 687 688 689 690 691 692
	install_if="$pkgname=$pkgver-r$pkgrel lang"
	arch="noarch"
	local dir
	for dir in ${langdir:-/usr/share/locale}; do
		mkdir -p "$subpkgdir"/${dir%/*}
		mv "$pkgdir"/"$dir" "$subpkgdir"/"$dir" || return 1
	done
}

lang() {
	default_lang
}

693
default_lang_subpkg() {
694 695 696 697
	if [ -z "$lang" ]; then
		error "lang is not set"
		return 1
	fi
698
	pkgdesc="$pkgname language pack for $lang"
699 700
	arch="noarch"
	install_if="$pkgname=$pkgver-r$pkgrel lang-$lang"
701 702 703 704 705
	local dir
	for dir in ${langdir:-/usr/share/locale}; do
		mkdir -p "$subpkgdir"/$dir
		mv "$pkgdir"/$dir/$lang* \
		"$subpkgdir"/$dir/ \
706
		|| return 1
707 708 709 710 711
	done
}

lang_subpkg() {
	default_lang_subpkg
712 713 714 715 716 717 718 719 720 721 722
}

prepare_language_packs() {
	for lang in $linguas; do
		lang="$lang" \
		subpkgname="$pkgname-lang-$lang" \
		subpkgdir="$pkgbasedir"/$subpkgname \
			$0 lang_subpkg prepare_package || return 1
	done
}

723 724 725 726 727 728 729 730 731 732 733 734
# echo '-dirty' if git is not clean
git_dirty() {
	if [ $(git status -s "$startdir" | wc -l) -ne 0 ]; then
		echo "-dirty"
	fi
}

# echo last commit hash id
git_last_commit() {
	git log --format=oneline -n 1 "$startdir" | awk '{print $1}'
}

735 736 737 738 739 740
get_maintainer() {
	if [ -z "$maintainer" ]; then
		maintainer=$(awk -F': ' '/\# *Maintainer/ {print $2}' "$APKBUILD")
	fi
}

741
prepare_metafiles() {
742
	getpkgver || return 1
743 744
	local name=${subpkgname:-$pkgname}
	[ -z "${name##* *}" ] && die "package name contains spaces"
745
	local dir=${subpkgdir:-$pkgdir}
746
	local pkg="$name-$pkgver-r$pkgrel.apk"
747
	local pkginfo="$controldir"/.PKGINFO
748
	local sub
749

Natanael Copa's avatar
Natanael Copa committed
750 751
	[ ! -d "$dir" ] && die "Missing $dir"
	cd "$dir"
752
	mkdir -p "$controldir"
Natanael Copa's avatar
Natanael Copa committed
753 754
	local builddate=$(date -u "+%s")
	local size=$(du -sk | awk '{print $1 * 1024}')
Natanael Copa's avatar
Natanael Copa committed
755
	local parch="$CARCH"
756 757 758 759 760

	# we need to wait with setting noarch til our build infra can handle it
#	if [ "$arch" = "noarch" ]; then
#		parch="noarch"
#	fi
Natanael Copa's avatar
Natanael Copa committed
761

762
	echo "# Generated by $(basename $0) $abuild_ver" >"$pkginfo"
Natanael Copa's avatar
Natanael Copa committed
763
	if [ -n "$FAKEROOTKEY" ]; then
764
		echo "# using $($FAKEROOT -v)" >> "$pkginfo"
Natanael Copa's avatar
Natanael Copa committed
765
	fi
766 767
	echo "# $(date -u)" >> "$pkginfo"
	cat >> "$pkginfo" <<EOF
768
pkgname = $name
769
pkgver = $pkgver-r$pkgrel
Natanael Copa's avatar
Natanael Copa committed
770 771 772 773 774
pkgdesc = $pkgdesc
url = $url
builddate = $builddate
packager = ${PACKAGER:-"Unknown"}
size = $size
Natanael Copa's avatar
Natanael Copa committed
775
arch = $parch
776
origin = $pkgname
Natanael Copa's avatar
Natanael Copa committed
777
EOF
778 779
	local i deps
	deps="$depends"
780
	if [ "$pkgname" != "busybox" ] && ! depends_has busbox; then
781 782
		for i in $install $triggers; do
			local s=${i%=*}
783
			[ "$name" != "${s%.*}" ] && continue
784
			if head -n 1 "$startdir/$s" | grep '^#!/bin/sh' >/dev/null ; then
785 786 787 788 789
				msg "Script found. busybox added as a dependency for $pkg"
				deps="$deps busybox"
				break
			fi
		done
790
	fi
791

792 793 794
	# store last_commit in global var so we only call git once
	if [ -z "$last_commit" ]; then
		last_commit="$(git_last_commit)$(git_dirty)"
795
	fi
796
	echo "commit = $last_commit" >> "$pkginfo"
797 798 799 800 801 802

	get_maintainer
	if [ -n "$maintainer" ]; then
		echo "maintainer = $maintainer" >> "$pkginfo"
	fi

803
	if [ -n "$replaces_priority" ]; then
804
		echo "replaces_priority = $replaces_priority" >> "$pkginfo"
805 806
	fi

Natanael Copa's avatar
Natanael Copa committed
807
	for i in $license; do
808
		echo "license = $i" >> "$pkginfo"
Natanael Copa's avatar
Natanael Copa committed
809 810
	done
	for i in $replaces; do
811
		echo "replaces = $i" >> "$pkginfo"
Natanael Copa's avatar
Natanael Copa committed
812
	done
813
	for i in $deps; do
814
		echo "depend = $i" >> "$pkginfo"
Natanael Copa's avatar
Natanael Copa committed
815 816
	done
	for i in $conflicts; do
817
		echo "conflict = $i" >> "$pkginfo"
Natanael Copa's avatar
Natanael Copa committed
818 819
	done
	for i in $provides; do
820
		echo "provides = $i" >> "$pkginfo"
Natanael Copa's avatar
Natanael Copa committed
821
	done
822 823 824 825 826 827
	for i in $triggers; do
		local f=${i%=*}
		local dirs=${i#*=}
		[ "${f%.trigger}" != "$name" ] && continue
		echo "triggers = ${dirs//:/ }" >> "$pkginfo"
	done
828 829 830
	if [ -n "$install_if" ]; then
		echo "install_if = $(echo $install_if)" >> "$pkginfo"
	fi
Natanael Copa's avatar
Natanael Copa committed
831 832

	local metafiles=".PKGINFO"
833 834 835
	for i in $install $triggers; do
		local f=${i%=*}
		local n=${f%.*}
836
		if [ "$n" != "$name" ]; then
837 838
			continue
		fi
839
		script=${f#$name}
840
		msg "Adding $script"
841
		cp "$startdir/$f" "$controldir/$script" || return 1
842
		chmod +x "$controldir/$script"
843 844
		metafiles="$metafiles $script"
	done
845
	echo $metafiles | tr ' ' '\n' > "$controldir"/.metafiles
846
}
847

848 849
prepare_tracedeps() {
	local dir=${subpkgdir:-$pkgdir}
850
	local etype= soname= file= sover=
851
	[ "$arch" = "noarch" ] && return 0
852
	options_has "!tracedeps" && return 0
853
	# lets tell all the places we should look for .so files - all rpaths
854 855
	scanelf --quiet --recursive --rpath "$dir" \
		| sed -e 's/[[:space:]].*//' -e 's/:/\n/' | sort -u \
856
		>"$controldir"/.rpaths
857 858 859 860 861 862 863
	if grep -q -x '/usr/lib' "$controldir"/.rpaths; then
		warning "Redundat /usr/lib in rpath found"
	fi
	if grep '^/home/' "$controldir"/.rpaths; then
		error "Has /home/... in rpath"
		return 1
	fi
Natanael Copa's avatar
Natanael Copa committed
864 865
}

866 867 868 869 870 871 872 873 874 875 876 877
# check if dir has arch specific binaries
dir_has_arch_binaries() {
	local dir="$1"
	# if scanelf returns something, then we have binaries
	[ -n "$(scanelf -R "$dir" | head -n 1)" ] && return 0

	# look for static *.a
	[ -n "$(find "$dir" -type f -name '*.a' | head -n 1)" ] && return 0

	return 1
}

878 879 880 881 882
# returns true if this is the -dev package
is_dev_pkg() {
	test "${subpkgname%-dev}" != "$subpkgname"
}

883 884 885 886 887 888 889
# check that noarch is set if needed
archcheck() {
	options_has "!archcheck" && return 0
	if dir_has_arch_binaries "${subpkgdir:-$pkgdir}"; then
		[ "$arch" != "noarch" ] && return 0
		error "Arch specific binaries found so arch must not be set to \"noarch\""
		return 1
890 891 892
	elif [ "$arch" != "noarch" ] && ! is_dev_pkg; then
		# we dont want -dev package go to noarch
		warning "No arch specific binaries found so arch should probably be set to \"noarch\""
893 894 895 896
	fi
	return 0
}

897
prepare_package() {
898
	msg "Preparing ${subpkgname:+sub}package ${subpkgname:-$pkgname}..."
899
	stripbin
900
	prepare_metafiles && prepare_tracedeps || return 1
901
	archcheck
902 903 904 905 906 907 908 909
}

pkginfo_val() {
	local key="$1"
	local file="$2"
	awk -F ' = ' "\$1 == \"$key\" {print \$2}" "$file"
}

910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
# find real path to so files
real_so_path() {
	local so="$1"
	shift
	while [ $# -gt 0 ]; do
		[ -e "$1"/$so ] && realpath "$1/$so" && return 0
		shift
	done
	error "$so: path not found"
	return 1
}

# search rpaths and /usr/lib /lib for given so files
find_so_files() {
	local rpaths=$(cat "$1")
	shift
	while [ $# -gt 0 ]; do
		real_so_path "$1" /usr/lib /lib $rpaths || return 1
		shift
	done
	return 0
}

933 934 935 936
subpkg_provides() {
	grep -q -w "^$1" "$pkgbasedir"/.control.*/.provides-so 2>/dev/null
}

937 938 939
trace_apk_deps() {
	local name="$1"
	local dir="$2"
940
	local i= found= autodeps= deppkgs= missing=
941
	msg "Tracing dependencies..."
942 943 944
	# add pkgconfig if usr/lib/pkgconfig is found
	if [ -d "$pkgbasedir"/$name/usr/lib/pkgconfig ] \
			&& ! grep -q '^depend = pkgconfig' "$dir"/.PKGINFO; then
945
		msg "  added pkgconfig (found /usr/lib/pkgconfig)"
946 947 948 949
		autodeps="$autodeps pkgconfig"
	fi

	# special case for libpthread: we need depend on libgcc
950
	if [ -f "$dir"/.needs-so ] && grep -q -w '^libpthread.so.*' "$dir"/.needs-so \
951 952
			&& ! grep -q -w "^depend = libgcc" "$dir"/.PKGINFO; then
		autodeps="$autodeps libgcc"
953
		msg "  added libgcc (due to libpthread)"
954
	fi
955
	[ -f "$dir"/.needs-so ] && for i in $(cat "$dir"/.needs-so); do
956 957 958
		# first check if its provided by same apkbuild
		grep -q -w "^$i" "$dir"/.provides-so 2>/dev/null && continue

959
		if subpkg_provides "$i" || cross_compiling \
960
				|| $APK info --quiet --installed "so:$i"; then
961
			autodeps="$autodeps so:$i"
962
		else
963
			missing="$missing $i"
964
		fi
965 966 967
	done

	# find all packages that holds the so files
968
	if [ -f "$dir"/.rpaths ]; then
969 970
		local so_files=$(find_so_files "$dir"/.rpaths $missing) \
			|| return 1
971
		deppkgs=$($APK info --quiet --who-owns $so_files) || return 1
972 973
	fi

974
	for found in $deppkgs; do
975
		if grep -w "^depend = ${found}$" "$dir"/.PKGINFO >/dev/null ; then
976 977 978
			warning "You can remove '$found' from depends"
			continue
		fi
979
		autodeps="$autodeps $found"
980
	done
981

982
	echo "# automatically detected:" >> "$dir"/.PKGINFO
983
	if [ -f "$dir"/.provides-so ]; then
984
		sed 's/^\(.*\) \([0-9].*\)/provides = so:\1=\2/' "$dir"/.provides-so \
985
			>> "$dir"/.PKGINFO
986 987
	fi
	[ -z "$autodeps" ] && return 0
988
	for i in $autodeps; do
989 990
		echo "depend = $i"
	done | sort -u >> "$dir"/.PKGINFO
991 992
	# display all depends
	sed -n '/^depend =/s/depend = /\t/p' "$dir"/.PKGINFO >&2
993 994
}

995
find_scanelf_paths() {
996
	local datadir="$1"
Natanael Copa's avatar
Natanael Copa committed
997
	local paths="$datadir/lib:$datadir/usr/lib" i= rpaths=
998
	if [ -n "$ldpath" ]; then
Natanael Copa's avatar
Natanael Copa committed
999
		paths="$paths:$(echo "${datadir}${ldpath}" | sed "s|:|:$datadir|g")"
1000 1001
	fi
	# search in all rpaths
Natanael Copa's avatar
Natanael Copa committed
1002 1003
	for rpaths in "$pkgbasedir"/.control.*/.rpaths; do
		[ -f "$rpaths" ] || continue
1004
		while read i; do
1005 1006 1007 1008
			local dir="${datadir}${i}"
			IFS=:
			if [ -d "$dir" ] && ! list_has "$dir" $paths; then
				paths="$paths:${dir}"
1009
			fi
1010
			unset IFS
Natanael Copa's avatar
Natanael Copa committed
1011
		done < "$rpaths"
1012 1013 1014 1015 1016 1017
	done
	echo "$paths"
}

scan_shared_objects() {
	local name="$1" controldir="$2" datadir="$3"
1018
	local opt= i=
1019 1020 1021

	# allow spaces in paths
	IFS=:
1022
	set -- $(find_scanelf_paths "$datadir")
1023 1024
	unset IFS

1025 1026 1027 1028 1029 1030 1031 1032
	# sanity check, verify that each path is prefixed with datadir
	for i; do
		if [ "${i#$datadir}" = "$i" ]; then
			error "Internal error in scanelf paths"
			return 1
		fi
	done

1033 1034 1035
	if options_has "ldpath-recursive"; then
		opt="--recursive"
	fi
1036 1037
	msg "Scanning shared objects"
	# lets tell all the .so files this package provides in .provides-so
1038
	scanelf --nobanner --soname $opt "$@" | while read etype soname file; do
1039 1040
		# if soname field is missing, soname will be the filepath
		sover=0
1041 1042
		if [ -z "$file" ]; then
			file="$soname"
1043
			soname=${soname##*/}
1044 1045 1046 1047
		fi

		# we only want shared libs
		case $soname in
1048
		*.so|*.so.[0-9]*|*.c32);;
1049 1050 1051 1052 1053 1054 1055 1056 1057
		*) continue;;
		esac

		case "$file" in
		*.so.[0-9]*) sover=${file##*.so.};;
		*.so)
			# filter out sonames with version when file does not
			# have version
			case "$soname" in
1058 1059 1060 1061
			*.so.[0-9]*)
				if options_has "sover-namecheck"; then
					continue
				fi
1062 1063 1064
			esac
			;;
		esac
Natanael Copa's avatar
Natanael Copa committed
1065
		list_has "$soname" $somask && continue
1066
		echo "$soname $sover"
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
	# use awk to filter out dupes that has sover = 0
	done | awk '{ if (so[$1] == 0) so[$1] = $2; }
		END { for (i in so) print(i " " so[i]); }' \
		| sort -u > "$controldir"/.provides-so

	# verify that we dont have any duplicates
	local dupes="$(cut -d' ' -f1 "$controldir"/.provides-so | uniq -d)"
	if [ -n "$dupes" ]; then
		die  "provides multiple versions of same shared object: $dupes"
	fi
1077 1078 1079 1080 1081

	# now find the so dependencies
	scanelf --nobanner --recursive --needed "$datadir"  | tr ' ' ':' \
		| awk -F ":" '$1 == "ET_DYN" || $1 == "ET_EXEC" {print $2}'  \
		| sed 's:,:\n:g' | sort -u \
Natanael Copa's avatar
Natanael Copa committed
1082
	| while read soname; do
1083
		# only add files that are not self provided
Natanael Copa's avatar
Natanael Copa committed
1084 1085 1086
		grep -q -w "^$soname" "$controldir"/.provides-so \
			|| list_has "$soname" $somask \
			|| echo $soname
1087 1088 1089
	done > "$controldir"/.needs-so
}

1090 1091 1092 1093 1094 1095 1096 1097
# read size in bytes from stdin and show as human readable
human_size() {
	awk '{  split("B KB MB GB TB PB", type)
		for(i=5; y < 1; i--)
			y = $1 / (2**(10*i))
		printf("%.1f %s\n", y, type[i+2]) }'
}

1098
create_apks() {
1099
	local file= dir= name= ver= apk= datadir= size=
1100
	getpkgver || return 1
Natanael Copa's avatar
Natanael Copa committed
1101
	mkdir -p "$PKGDEST"