abuild.in 49.3 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
program=${0##*/}
15
abuild_path=$(readlink -f $0)
Natanael Copa's avatar
Natanael Copa committed
16 17

# defaults
18
BUILD_BASE="build-base"
19
FAKEROOT=${FAKEROOT:-"fakeroot"}
20

21 22
: ${SUDO_APK:=abuild-apk}
: ${APK:=apk}
23 24 25
: ${ADDUSER:=abuild-adduser}
: ${ADDGROUP:=abuild-addgroup}

26
apk_opt_wait="--wait 30"
27

Natanael Copa's avatar
Natanael Copa committed
28
# read config
Natanael Copa's avatar
Natanael Copa committed
29
ABUILD_CONF=${ABUILD_CONF:-"$sysconfdir/abuild.conf"}
Natanael Copa's avatar
Natanael Copa committed
30 31
[ -f "$ABUILD_CONF" ] && . "$ABUILD_CONF"

32
default_colors() {
Natanael Copa's avatar
Natanael Copa committed
33 34 35 36 37 38
	NORMAL="\033[1;0m"
	STRONG="\033[1;1m"
	RED="\033[1;31m"
	GREEN="\033[1;32m"
	YELLOW="\033[1;33m"
	BLUE="\033[1;34m"
39 40 41 42 43 44 45 46 47 48 49 50 51 52
}

monochrome() {
	NORMAL=""
	STRONG=""
	RED=""
	GREEN=""
	YELLOW=""
	BLUE=""
}

#colors
if [ -n "$USE_COLORS" ]; then
    default_colors
53 54 55 56 57 58 59
fi

# run optional log command for remote logging
logcmd() {
	${ABUILD_LOG_CMD:-true} "$@"
	return 0
}
Natanael Copa's avatar
Natanael Copa committed
60 61 62

# functions
msg() {
63
	[ -n "$quiet" ] && return 0
Natanael Copa's avatar
Natanael Copa committed
64 65 66
	local prompt="$GREEN>>>${NORMAL}"
	local fake="${FAKEROOTKEY:+${BLUE}*${NORMAL}}"
	local name="${STRONG}${subpkgname:-$pkgname}${NORMAL}"
67 68 69 70 71 72 73
	printf "${prompt} ${name}${fake}: %s\n" "$1" >&2
}

msg2() {
	[ -n "$quiet" ] && return 0
	#      ">>> %s"
	printf "    %s\n" "$1" >&2
Natanael Copa's avatar
Natanael Copa committed
74 75 76 77 78 79
}

warning() {
	local prompt="${YELLOW}>>> WARNING:${NORMAL}"
	local fake="${FAKEROOTKEY:+${BLUE}*${NORMAL}}"
	local name="${STRONG}${subpkgname:-$pkgname}${NORMAL}"
80 81 82 83
	printf "${prompt} ${name}${fake}: %s\n" "$1" >&2
}

warning2() {
84
	#      ">>> WARNING:
85
	printf "             %s\n" "$1" >&2
Natanael Copa's avatar
Natanael Copa committed
86 87 88 89 90 91
}

error() {
	local prompt="${RED}>>> ERROR:${NORMAL}"
	local fake="${FAKEROOTKEY:+${BLUE}*${NORMAL}}"
	local name="${STRONG}${subpkgname:-$pkgname}${NORMAL}"
92
	printf "${prompt} ${name}${fake}: %s\n" "$1" >&2
93
	logcmd "ERROR: $pkgname: $1"
94 95 96
}

error2() {
97
	#      ">>> ERROR:
98
	printf "           %s\n" "$1" >&2
Natanael Copa's avatar
Natanael Copa committed
99
}
100

Linux User's avatar
Linux User committed
101
set_xterm_title() {
102
	if [ "$TERM" = xterm ] && [ -n "$USE_COLORS" ]; then
103
		 printf "\033]0;$1\007" >&2
Linux User's avatar
Linux User committed
104
	fi
105
}
Linux User's avatar
Linux User committed
106 107

cleanup() {
108
	local i=
Linux User's avatar
Linux User committed
109
	set_xterm_title ""
110 111 112
	if [ -n "$CLEANUP_FILES" ]; then
		rm -f $CLEANUP_FILES
	fi
113 114 115 116 117 118 119
	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..."
120 121 122
				$SUDO_APK del --quiet $apk_opt_wait $uninstall_after
				[ "$CBUILD" != "$CHOST" -a -n "$CBUILDROOT" ] && \
					$SUDO_APK del --root "$CBUILDROOT" --quiet $apk_opt_wait $uninstall_after
123 124 125 126
			fi
			;;
		esac
	done
Linux User's avatar
Linux User committed
127 128
}

Natanael Copa's avatar
Natanael Copa committed
129
die() {
Linux User's avatar
Linux User committed
130
	error "$@"
131
	cleanup $ERROR_CLEANUP
Natanael Copa's avatar
Natanael Copa committed
132 133 134
	exit 1
}

135 136 137 138
spell_error() {
	die "APKBUILD contains '$1'. It should be '$2'"
}

139
# check if apkbuild is basicly sane
140
default_sanitycheck() {
141
	local i= j= suggestion=
142
	msg "Checking sanity of $APKBUILD..."
143 144 145
	[ -z "$pkgname" ] && die "Missing pkgname in APKBUILD"
	[ -z "${pkgname##* *}" ] && die "pkgname contains spaces"
	[ -z "$pkgver" ] && die "Missing pkgver in APKBUILD"
146
	if [ "$pkgver" != "volatile" ] && [ -z "$nodeps" ]; then
147
		$APK version --check --quiet "$pkgver" ||\
148 149
			die "$pkgver is not a valid version"
	fi
150
	[ -z "$pkgrel" ] && die "Missing pkgrel in APKBUILD"
151 152 153
	[ -z "$pkgdesc" ] && die "Missing pkgdesc in APKBUILD"
	[ -z "$url" ] && die "Missing url in APKBUILD"
	[ -z "$license" ] && die "Missing license in APKBULID"
154 155 156
	if [ $(echo "$pkgdesc" | wc -c) -gt 128 ]; then
		die "pkgdesc is too long"
	fi
157
	is_function package || warning "Missing package() function in APKBUILD"
158

159 160
	if [ -n "$replaces_priority" ] \
		&& ! echo $replaces_priority | egrep -q '^[0-9]+$'; then
161 162
		die "replaces_priority must be a number"
	fi
163 164 165 166 167 168 169
	# 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

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

173
	if [ -z "$CARCH" ]; then
Natanael Copa's avatar
Natanael Copa committed
174
		die "Please fix CHOST, or set CARCH in abuild.conf"
175
	fi
176

Natanael Copa's avatar
Natanael Copa committed
177
	for i in $install; do
178
		local n=${i%.*}
179
		local suff=${i##*.}
180 181 182 183 184
		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
185
			die "$i: install script does not match pkgname or any subpackage"
186
		fi
187
		[ -e "$startdir/$i" ] || die "install script $i is missing"
188 189 190 191 192 193
		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
194
	done
195

196 197 198
	for i in $triggers; do
		local f=${i%=*}
		local p=${f%.trigger}
199
		[ "$f" = "$i" ] && die "$f: triggers must contain '='"
200 201 202 203
		[ "$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
204

205 206
		[ -e "$startdir"/$f ] || die "trigger script $f is missing"
	done
207 208
	if [ -n "$source" ]; then
		for i in $source; do
Natanael Copa's avatar
Natanael Copa committed
209 210 211 212
			if install_has "$i"; then
				warning "You should not have \$install in source"
				continue
			fi
213
			case "$i" in
214
				*::*) i=${i%%::*};;
215
				https://*) makedepends_has wget && warning "wget no longer need to be in makedepends when source has https://" ;;
216
			esac
217 218
			list_has ${i##*/} $md5sums $sha256sums $sha512sums \
				|| die "${i##*/} is missing in checksums"
219 220
		done
	fi
Natanael Copa's avatar
Natanael Copa committed
221

222 223 224 225 226 227 228 229
	# 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"
230
		done
231
	done
232

233
	# common spelling errors
234 235 236 237 238
	[ -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
239 240

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

	makedepends_has 'g++' && warning "g++ should not be in makedepends"
243 244 245
	return 0
}

246 247 248 249
sanitycheck() {
	default_sanitycheck
}

250 251
sumcheck() {
	local algo="$1" sums="$2"
252
	local dummy f endreturnval originalparams origin file
253 254 255 256 257 258 259 260

	# 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($#)"
261
	fi
262
	fetch || return 1
263
	msg "Checking ${algo}sums..."
Natanael Copa's avatar
Natanael Copa committed
264
	cd "$srcdir" || return 1
265 266
	IFS=$'\n'
	endreturnval=0
267
	for src in $sums; do
268
		origin=$1; shift
269
		echo "$src" | ${algo}sum -c
270
		if [ $? -ne 0 ]; then
271
			endreturnval=1
272
			is_remote $origin || continue
273
			echo "Because the remote file above failed the ${algo}sum check it will be deleted."
274 275 276 277 278 279 280 281
			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
282 283
}

284 285 286 287 288 289 290 291 292
# for compatibility
md5check() {
	warning "'md5check' is deprecated. Use 'verify' instead"
	sumcheck md5 "$md5sums"
}

# verify checksums
verify() {
	local verified=false algo=
293
	for algo in sha512 sha256 sha1 md5; do
294 295 296 297 298 299 300
		local sums=
		eval sums=\"\$${algo}sums\"
		if [ -z "$sums" ] || [ -z "$source" ]; then
			continue
		fi
		sumcheck "$algo" "$sums" || return 1
		verified=true
301
		break
302 303 304 305 306 307 308
	done
	if [ -n "$source" ] && ! $verified; then
		die "Use 'abuild checksum' to generate/update the checksum(s)"
	fi
	return 0
}

309 310
# verify upstream sources
sourcecheck() {
311 312 313 314 315 316 317 318
	local uri
	for uri in $source; do
		is_remote $uri || continue
		case "$uri" in
		saveas-*://*)
			uri=${uri#saveas-}
			uri=${uri%/*}
			;;
319 320 321
		*::*)
			uri=${uri##*::}
			;;
322 323
		esac
		wget -q -s "$uri" || return 1
324
	done
325
	return 0
326
}
327

328 329 330
# convert curl options to wget options and call wget instead of curl
wget_fallback() {
	local wget_opts= outfile= opt=
331
	while getopts "C:Lko:s" opt; do
332
		case $opt in
333
		'L') ;; # --location. wget does this by default
334
		'f') ;; # --fail. wget does this by default
335 336 337 338 339 340 341 342 343 344
		'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
345 346
uri_fetch() {
	local uri="$1"
347
	local d="${uri##*/}"	# $(basename $uri)
348
	local opts
349
	[ -n "$quiet" ] && opts="-s"
350 351

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

353 354 355 356 357
	# 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)}";;
358 359 360 361 362

		*::*)
			d=${uri%%::*}
			uri=${uri#$d::}
			;;
363
	esac
364 365

	case "$uri" in
366
		https://*) opts="-k";;
367
	esac
368

Natanael Copa's avatar
Natanael Copa committed
369
	mkdir -p "$SRCDEST"
370 371 372

	CLEANUP_FILES="$CLEANUP_FILES $lockfile"
	(
373 374
	flock -n -x 9 || msg "Waiting for ${lockfile##*/}..."
	flock -x 9
375 376 377

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

378 379
	if [ -f "$SRCDEST/$d.part" ]; then
		msg "Partial download found. Trying to resume"
380
		opts="$opts -C -"
Natanael Copa's avatar
Natanael Copa committed
381
	fi
Linux User's avatar
Linux User committed
382
	msg "Fetching $uri"
383 384

	# fallback to wget if curl is missing. useful for bootstrapping
385
	local fetcher=
386 387
	if ! [ -x "$(which curl)" ]; then
		fetcher=wget_fallback
388 389
	else
		fetcher=curl
390
		opts="$opts -L -f -k"
391 392 393
	fi

	$fetcher $opts -o "$SRCDEST/$d.part" "$uri" \
394
		&& mv "$SRCDEST/$d.part" "$SRCDEST/$d"
395

396
	) 9>$lockfile
397 398 399 400

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

403
is_remote() {
404
	case "${1#*::}" in
405
		http://*|ftp://*|https://*|saveas-*://*)
406 407 408 409 410
			return 0;;
	esac
	return 1
}

411 412 413 414 415 416 417 418 419
filename_from_uri() {
	local uri="$1"
	local filename="${uri##*/}"  # $(basename $uri)
	case "$uri" in
	*::*) filename=${uri%%::*};;
	esac
	echo "$filename"
}

420 421 422 423
# try download from file from mirror first
uri_fetch_mirror() {
	local uri="$1"
	if [ -n "$DISTFILES_MIRROR" ]; then
424
		if is_remote "$DISTFILES_MIRROR"; then
425 426
			uri_fetch "$DISTFILES_MIRROR"/$(filename_from_uri $uri)\
				&& return 0
427
		else
428 429
			cp "$DISTFILES_MIRROR"/$(filename_from_uri $uri) \
				"$SRCDEST" && return 0
430
		fi
431 432 433 434
	fi
	uri_fetch "$uri"
}

435
default_fetch() {
Natanael Copa's avatar
Natanael Copa committed
436 437
	local s
	mkdir -p "$srcdir"
438 439
	for s in $source; do
		if is_remote "$s"; then
440
			uri_fetch_mirror "$s" || return 1
441
			ln -sf "$SRCDEST/$(filename_from_uri $s)" "$srcdir"/
442
		else
443 444
			ln -sf "$startdir/$s" "$srcdir/"
		fi
Natanael Copa's avatar
Natanael Copa committed
445 446 447
	done
}

448 449 450 451
fetch() {
	default_fetch
}

Natanael Copa's avatar
Natanael Copa committed
452 453 454 455 456 457 458 459 460 461 462 463
# 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
464
	done
Natanael Copa's avatar
Natanael Copa committed
465 466
}

Natanael Copa's avatar
Natanael Copa committed
467
# unpack the sources
468
default_unpack() {
Natanael Copa's avatar
Natanael Copa committed
469
	local u
470
	if [ -z "$force" ]; then
471
		verify || return 1
Natanael Copa's avatar
Natanael Copa committed
472
		initdcheck || return 1
473
	fi
Natanael Copa's avatar
Natanael Copa committed
474 475
	mkdir -p "$srcdir"
	for u in $source; do
476
		local s="$SRCDEST/$(filename_from_uri $u)"
Natanael Copa's avatar
Natanael Copa committed
477
		case "$s" in
478 479 480
			*.tar)
				msg "Unpacking $s..."
				tar -C "$srcdir" -xf "$s" || return 1;;
Natanael Copa's avatar
Natanael Copa committed
481
			*.tar.gz|*.tgz)
482
				msg "Unpacking $s..."
Natanael Copa's avatar
Natanael Copa committed
483 484
				tar -C "$srcdir" -zxf "$s" || return 1;;
			*.tar.bz2)
485
				msg "Unpacking $s..."
Natanael Copa's avatar
Natanael Copa committed
486
				tar -C "$srcdir" -jxf "$s" || return 1;;
487 488 489 490
			*.tar.lzma)
				msg "Unpacking $s..."
				unlzma -c "$s" | tar -C "$srcdir" -x  \
					|| return 1;;
491 492 493
			*.tar.xz)
				msg "Unpacking $s..."
				unxz -c "$s" | tar -C "$srcdir" -x || return 1;;
Natanael Copa's avatar
Natanael Copa committed
494 495 496
			*.zip)
				msg "Unpacking $s..."
				unzip "$s" -d "$srcdir" || return 1;;
Natanael Copa's avatar
Natanael Copa committed
497 498 499 500
		esac
	done
}

501 502 503 504
unpack() {
	default_unpack
}

Natanael Copa's avatar
Natanael Copa committed
505 506
# cleanup source and package dir
clean() {
507
	msg "Cleaning temporary build dirs..."
Natanael Copa's avatar
Natanael Copa committed
508
	rm -rf "$srcdir"
509
	rm -rf "$pkgbasedir"
Natanael Copa's avatar
Natanael Copa committed
510 511 512 513 514 515
}

# cleanup fetched sources
cleancache() {
	local s
	for s in $source; do
516
		if is_remote "$s"; then
517 518 519
			s=$(filename_from_uri $s)
			msg "Cleaning downloaded $s ..."
			rm -f "$SRCDEST/$s"
520
		fi
Natanael Copa's avatar
Natanael Copa committed
521 522 523
	done
}

524 525 526 527 528 529 530 531 532 533
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
534 535
cleanpkg() {
	local i
536
	getpkgver || return 1
537
	msg "Cleaning built packages..."
538
	for i in $(listpkgnames); do
539 540
		local p="${i%:*}-$pkgver-r$pkgrel"
		rm -f "$PKGDEST/$p.apk" "$PKGDEST/$p.src.tar.gz" \
541
			"$abuildrepo"/$p.apk "$abuildrepo"/*/$p.apk
Natanael Copa's avatar
Natanael Copa committed
542
	done
543
	# remove given packages from index
544
	update_abuildrepo_index
Natanael Copa's avatar
Natanael Copa committed
545 546
}

547 548 549
# clean all packages except current
cleanoldpkg() {
	local i j
550
	getpkgver || return 1
551
	msg "Cleaning all packages except $pkgver-r$pkgrel..."
552
	for i in $(listpkgnames); do
553 554 555 556 557
		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##*/}
558 559 560 561
		done
	done
	return 0
}
562 563 564

mkusers() {
	local i
565 566 567 568 569 570
	for i in $pkggroups; do
		if ! getent group $i >/dev/null; then
			msg "Creating group $i"
			$ADDGROUP -S $i || return 1
		fi
	done
571 572
	for i in $pkgusers; do
		if ! getent passwd $i >/dev/null; then
573
			local gopt=
574
			msg "Creating user $i"
575 576 577
			if getent group $i >/dev/null; then
				gopt="-G $i"
			fi
578
			$ADDUSER -S -D -H $gopt $i || return 1
579 580 581 582
		fi
	done
}

Natanael Copa's avatar
Natanael Copa committed
583 584
# helper to update config.sub to a recent version
update_config_sub() {
585
	local changed=false
Natanael Copa's avatar
Natanael Copa committed
586
	find . -name config.sub | while read f; do
587 588
		if ! ./$f armv6-alpine-linux-muslgnueabihf 2>/dev/null; then
			msg "Updating $f"
589 590 591
			cp "$datadir"/${f##*/} "$f" || return 1
			changed=true
		else
592
			msg "Not new enough $f"
593 594 595
		fi
		# pipe subshell will return status of last command
		$changed
Natanael Copa's avatar
Natanael Copa committed
596
	done
597
	return $?
Natanael Copa's avatar
Natanael Copa committed
598
}
599

Natanael Copa's avatar
Natanael Copa committed
600 601
runpart() {
	local part=$1
602
	[ -n "$DEBUG" ] && msg "$part"
Natanael Copa's avatar
Natanael Copa committed
603 604 605 606
	$part || die "$part failed"
}

# override those in your build script
607 608 609 610 611 612 613 614
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
615 616 617 618
prepare() {
	:
}

Natanael Copa's avatar
Natanael Copa committed
619
build() {
620
	:
Natanael Copa's avatar
Natanael Copa committed
621 622
}

623 624 625
# generate a simple tar.gz package of pkgdir
targz() {
	cd "$pkgdir" || return 1
Natanael Copa's avatar
Natanael Copa committed
626
	mkdir -p "$PKGDEST"
627 628 629
	tar -czf "$PKGDEST"/$pkgname-$pkgver-r$pkgrel.tar.gz *
}

630 631 632 633 634 635 636 637 638
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
}

639 640 641
postcheck() {
	local dir="$1" name="$2" i=
	msg "Running postcheck for $name"
642
	# checking for FHS compat
Natanael Copa's avatar
Natanael Copa committed
643 644 645 646 647 648 649
	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
650 651 652 653
		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
654
	fi
655 656 657 658 659 660 661
	# 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
662 663 664 665 666 667
	# 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
668 669 670 671 672 673
	# 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
674
	# check so we dont have any suid root binaries that are not
675 676 677 678 679 680 681 682
	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
683 684 685 686 687 688 689 690 691
	# 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
692 693 694
	return 0
}

695
prepare_subpackages() {
Natanael Copa's avatar
Natanael Copa committed
696 697
	local i
	cd "$startdir"
698
	for i in $subpackages; do
699 700
		local func=$(get_split_func $i)
		# call abuild recursively, setting subpkg{dir,name}
701
		msg "Running split function $func..."
702
		local dir="$pkgbasedir/${i%:*}" name="${i%:*}"
703 704 705
		( subpkgdir="$dir" subpkgname="$name" \
			$0 $func prepare_package \
			&& postcheck "$dir" "$name" ) || return 1
Natanael Copa's avatar
Natanael Copa committed
706
	done
707
	postcheck "$pkgdir" "$pkgname" || return 1
708 709
	# post check for /usr/share/locale
	if [ -d "$pkgdir"/usr/share/locale ]; then
710
		warning "Found /usr/share/locale"
711
		warning2 "Maybe add \$pkgname-lang to subpackages?"
712
	fi
Natanael Copa's avatar
Natanael Copa committed
713 714
}

715
default_lang() {
716
	pkgdesc="Languages for package $pkgname"
717 718 719 720 721 722 723 724 725 726 727 728 729
	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
}

730
default_lang_subpkg() {
731 732 733 734
	if [ -z "$lang" ]; then
		error "lang is not set"
		return 1
	fi
735
	pkgdesc="$pkgname language pack for $lang"
736 737
	arch="noarch"
	install_if="$pkgname=$pkgver-r$pkgrel lang-$lang"
738 739 740 741 742
	local dir
	for dir in ${langdir:-/usr/share/locale}; do
		mkdir -p "$subpkgdir"/$dir
		mv "$pkgdir"/$dir/$lang* \
		"$subpkgdir"/$dir/ \
743
		|| return 1
744 745 746 747 748
	done
}

lang_subpkg() {
	default_lang_subpkg
749 750 751 752 753 754 755 756 757 758 759
}

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
}

760 761 762 763 764 765 766 767 768 769 770 771
# 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}'
}

772 773 774 775 776 777
get_maintainer() {
	if [ -z "$maintainer" ]; then
		maintainer=$(awk -F': ' '/\# *Maintainer/ {print $2}' "$APKBUILD")
	fi
}

778
prepare_metafiles() {
779
	getpkgver || return 1
780 781
	local name=${subpkgname:-$pkgname}
	[ -z "${name##* *}" ] && die "package name contains spaces"
782
	local dir=${subpkgdir:-$pkgdir}
783
	local pkg="$name-$pkgver-r$pkgrel.apk"
784
	local pkginfo="$controldir"/.PKGINFO
785
	local sub
786

Natanael Copa's avatar
Natanael Copa committed
787 788
	[ ! -d "$dir" ] && die "Missing $dir"
	cd "$dir"
789
	mkdir -p "$controldir"
Natanael Copa's avatar
Natanael Copa committed
790 791
	local builddate=$(date -u "+%s")
	local size=$(du -sk | awk '{print $1 * 1024}')
Natanael Copa's avatar
Natanael Copa committed
792
	local parch="$CARCH"
793 794 795 796 797

	# 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
798

799
	echo "# Generated by $(basename $0) $abuild_ver" >"$pkginfo"
Natanael Copa's avatar
Natanael Copa committed
800
	if [ -n "$FAKEROOTKEY" ]; then
801
		echo "# using $($FAKEROOT -v)" >> "$pkginfo"
Natanael Copa's avatar
Natanael Copa committed
802
	fi
803 804
	echo "# $(date -u)" >> "$pkginfo"
	cat >> "$pkginfo" <<EOF
805
pkgname = $name
806
pkgver = $pkgver-r$pkgrel
Natanael Copa's avatar
Natanael Copa committed
807 808 809 810 811
pkgdesc = $pkgdesc
url = $url
builddate = $builddate
packager = ${PACKAGER:-"Unknown"}
size = $size
Natanael Copa's avatar
Natanael Copa committed
812
arch = $parch
813
origin = $pkgname
Natanael Copa's avatar
Natanael Copa committed
814
EOF
815 816
	local i deps
	deps="$depends"
817
	if [ "$pkgname" != "busybox" ] && ! depends_has busbox; then
818 819
		for i in $install $triggers; do
			local s=${i%=*}
820
			[ "$name" != "${s%.*}" ] && continue
821
			if head -n 1 "$startdir/$s" | grep '^#!/bin/sh' >/dev/null ; then
822 823 824 825 826
				msg "Script found. busybox added as a dependency for $pkg"
				deps="$deps busybox"
				break
			fi
		done
827
	fi
828

829 830 831
	# store last_commit in global var so we only call git once
	if [ -z "$last_commit" ]; then
		last_commit="$(git_last_commit)$(git_dirty)"
832
	fi
833
	echo "commit = $last_commit" >> "$pkginfo"
834 835 836 837 838 839

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

840
	if [ -n "$replaces_priority" ]; then
841
		echo "replaces_priority = $replaces_priority" >> "$pkginfo"
842 843
	fi

Natanael Copa's avatar
Natanael Copa committed
844
	for i in $license; do
845
		echo "license = $i" >> "$pkginfo"
Natanael Copa's avatar
Natanael Copa committed
846 847
	done
	for i in $replaces; do
848
		echo "replaces = $i" >> "$pkginfo"
Natanael Copa's avatar
Natanael Copa committed
849
	done
850
	for i in $deps; do
851
		echo "depend = $i" >> "$pkginfo"
Natanael Copa's avatar
Natanael Copa committed
852 853
	done
	for i in $conflicts; do
854
		echo "conflict = $i" >> "$pkginfo"
Natanael Copa's avatar
Natanael Copa committed
855 856
	done
	for i in $provides; do
857
		echo "provides = $i" >> "$pkginfo"
Natanael Copa's avatar
Natanael Copa committed
858
	done
859 860 861 862 863 864
	for i in $triggers; do
		local f=${i%=*}
		local dirs=${i#*=}
		[ "${f%.trigger}" != "$name" ] && continue
		echo "triggers = ${dirs//:/ }" >> "$pkginfo"
	done
865 866 867
	if [ -n "$install_if" ]; then
		echo "install_if = $(echo $install_if)" >> "$pkginfo"
	fi
Natanael Copa's avatar
Natanael Copa committed
868 869

	local metafiles=".PKGINFO"
870 871 872
	for i in $install $triggers; do
		local f=${i%=*}
		local n=${f%.*}
873
		if [ "$n" != "$name" ]; then
874 875
			continue
		fi
876
		script=${f#$name}
877
		msg "Adding $script"
878
		cp "$startdir/$f" "$controldir/$script" || return 1
879
		chmod +x "$controldir/$script"
880 881
		metafiles="$metafiles $script"
	done
882
	echo $metafiles | tr ' ' '\n' > "$controldir"/.metafiles
883
}
884

885 886
prepare_tracedeps() {
	local dir=${subpkgdir:-$pkgdir}
887
	local etype= soname= file= sover=
888
	[ "$arch" = "noarch" ] && return 0
889
	options_has "!tracedeps" && return 0
890
	# lets tell all the places we should look for .so files - all rpaths
891 892
	scanelf --quiet --recursive --rpath "$dir" \
		| sed -e 's/[[:space:]].*//' -e 's/:/\n/' | sort -u \
893
		>"$controldir"/.rpaths
894 895 896 897 898 899 900
	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
901 902
}

903 904 905 906 907 908 909 910 911 912 913 914
# 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
}

915 916 917 918 919
# returns true if this is the -dev package
is_dev_pkg() {
	test "${subpkgname%-dev}" != "$subpkgname"
}

920 921 922 923 924 925 926
# 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
927 928 929
	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\""
930 931 932 933
	fi
	return 0
}

934
prepare_package() {
935
	msg "Preparing ${subpkgname:+sub}package ${subpkgname:-$pkgname}..."
936
	stripbin
937
	prepare_metafiles && prepare_tracedeps || return 1
938
	archcheck
939 940 941 942 943 944 945 946
}

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

947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
# 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
}

970 971 972 973
subpkg_provides() {
	grep -q -w "^$1" "$pkgbasedir"/.control.*/.provides-so 2>/dev/null
}

974 975 976
trace_apk_deps() {
	local name="$1"
	local dir="$2"
977
	local i= found= autodeps= deppkgs= missing=
978
	msg "Tracing dependencies..."
979 980 981
	# add pkgconfig if usr/lib/pkgconfig is found
	if [ -d "$pkgbasedir"/$name/usr/lib/pkgconfig ] \
			&& ! grep -q '^depend = pkgconfig' "$dir"/.PKGINFO; then
982
		msg "  added pkgconfig (found /usr/lib/pkgconfig)"
983 984 985 986
		autodeps="$autodeps pkgconfig"
	fi

	# special case for libpthread: we need depend on libgcc
987
	if [ -f "$dir"/.needs-so ] && grep -q -w '^libpthread.so.*' "$dir"/.needs-so \
988 989
			&& ! grep -q -w "^depend = libgcc" "$dir"/.PKGINFO; then
		autodeps="$autodeps libgcc"
990
		msg "  added libgcc (due to libpthread)"
991
	fi
992
	[ -f "$dir"/.needs-so ] && for i in $(cat "$dir"/.needs-so); do
993 994 995
		# first check if its provided by same apkbuild
		grep -q -w "^$i" "$dir"/.provides-so 2>/dev/null && continue

996 997
		if subpkg_provides "$i" || [ "$CBUILD" != "$CHOST" ] \
				|| $APK info --quiet --installed "so:$i"; then
998
			autodeps="$autodeps so:$i"
999
		else
1000
			missing="$missing $i"
1001
		fi
1002 1003 1004
	done

	# find all packages that holds the so files
1005
	if [ -f "$dir"/.rpaths ]; then
1006 1007
		local so_files=$(find_so_files "$dir"/.rpaths $missing) \
			|| return 1
1008
		deppkgs=$($APK info --quiet --who-owns $so_files) || return 1
1009 1010
	fi

1011
	for found in $deppkgs; do
1012
		if grep -w "^depend = ${found}$" "$dir"/.PKGINFO >/dev/null ; then
1013 1014 1015
			warning "You can remove '$found' from depends"
			continue
		fi
1016
		autodeps="$autodeps $found"
1017
	done
1018

1019
	echo "# automatically detected:" >> "$dir"/.PKGINFO
1020
	if [ -f "$dir"/.provides-so ]; then
1021
		sed 's/^\(.*\) \([0-9].*\)/provides = so:\1=\2/' "$dir"/.provides-so \
1022
			>> "$dir"/.PKGINFO
1023 1024
	fi
	[ -z "$autodeps" ] && return 0
1025
	for i in $autodeps; do
1026 1027
		echo "depend = $i"
	done | sort -u >> "$dir"/.PKGINFO
1028 1029
	# display all depends
	sed -n '/^depend =/s/depend = /\t/p' "$dir"/.PKGINFO >&2
1030 1031
}

1032
find_scanelf_paths() {
1033
	local datadir="$1"
Natanael Copa's avatar
Natanael Copa committed
1034
	local paths="$datadir/lib:$datadir/usr/lib" i= rpaths=
1035
	if [ -n "$ldpath" ]; then
Natanael Copa's avatar
Natanael Copa committed
1036
		paths="$paths:$(echo "${datadir}${ldpath}" | sed "s|:|:$datadir|g")"
1037 1038
	fi
	# search in all rpaths
Natanael Copa's avatar
Natanael Copa committed
1039 1040
	for rpaths in "$pkgbasedir"/.control.*/.rpaths; do
		[ -f "$rpaths" ] || continue
1041
		while read i; do
1042 1043 1044 1045
			local dir="${datadir}${i}"
			IFS=:
			if [ -d "$dir" ] && ! list_has "$dir" $paths; then
				paths="$paths:${dir}"
1046
			fi
1047
			unset IFS
Natanael Copa's avatar
Natanael Copa committed
1048
		done < "$rpaths"
1049 1050 1051 1052 1053 1054
	done
	echo "$paths"
}

scan_shared_objects() {
	local name="$1" controldir="$2" datadir="$3"
1055
	local opt= i=
1056 1057 1058

	# allow spaces in paths
	IFS=:
1059
	set -- $(find_scanelf_paths "$datadir")
1060 1061
	unset IFS

1062 1063 1064 1065 1066 1067 1068 1069
	# 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

1070 1071 1072
	if options_has "ldpath-recursive"; then
		opt="--recursive"
	fi
1073 1074
	msg "Scanning shared objects"
	# lets tell all the .so files this package provides in .provides-so
1075
	scanelf --nobanner --soname $opt "$@" | while read etype soname file; do
1076 1077
		# if soname field is missing, soname will be the filepath
		sover=0
1078 1079
		if [ -z "$file" ]; then
			file="$soname"
1080
			soname=${soname##*/}
1081 1082 1083 1084
		fi

		# we only want shared libs
		case $soname in
1085
		*.so|*.so.[0-9]*|*.c32);;
1086 1087 1088 1089 1090 1091 1092 1093 1094
		*) 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
1095 1096 1097 1098
			*.so.[0-9]*)
				if options_has "sover-namecheck"; then
					continue
				fi
1099 1100 1101
			esac
			;;
		esac
Natanael Copa's avatar
Natanael Copa committed
1102
		list_has "$soname" $somask && continue