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
}
Natanael Copa's avatar
Natanael Copa committed
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
				$SUDO_APK del --quiet $apk_opt_wait $uninstall_after
121 122 123 124 125
				if [ "$CBUILD" != "$CHOST" -a -n "$CBUILDROOT" ]; then
					$SUDO_APK del --root "$CBUILDROOT" \
						--quiet $apk_opt_wait \
						$uninstall_after
				fi
126 127 128 129
			fi
			;;
		esac
	done
Linux User's avatar
Linux User committed
130 131
}

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

138 139 140 141
spell_error() {
	die "APKBUILD contains '$1'. It should be '$2'"
}

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

Natanael Copa's avatar
Natanael Copa committed
162 163
	if [ -n "$replaces_priority" ] \
		&& ! echo $replaces_priority | egrep -q '^[0-9]+$'; then
164 165
		die "replaces_priority must be a number"
	fi
166 167 168 169 170 171 172
	# 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

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

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

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

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

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

225 226 227 228 229 230 231 232
	# 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"
233
		done
234
	done
235

236
	# common spelling errors
237 238 239 240 241
	[ -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
242 243

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

	makedepends_has 'g++' && warning "g++ should not be in makedepends"
246 247 248
	return 0
}

249 250 251 252
sanitycheck() {
	default_sanitycheck
}

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

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

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

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

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

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

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

356 357 358 359 360
	# 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)}";;
361 362 363 364 365

		*::*)
			d=${uri%%::*}
			uri=${uri#$d::}
			;;
Natanael Copa's avatar
Natanael Copa committed
366
	esac
367 368

	case "$uri" in
369
		https://*) opts="-k";;
370
	esac
371

Natanael Copa's avatar
Natanael Copa committed
372
	mkdir -p "$SRCDEST"
373 374 375

	CLEANUP_FILES="$CLEANUP_FILES $lockfile"
	(
376 377
	flock -n -x 9 || msg "Waiting for ${lockfile##*/}..."
	flock -x 9
378 379 380

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

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

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

	$fetcher $opts -o "$SRCDEST/$d.part" "$uri" \
397
		&& mv "$SRCDEST/$d.part" "$SRCDEST/$d"
398

399
	) 9>$lockfile
400 401 402 403

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

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

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

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

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

451 452 453 454
fetch() {
	default_fetch
}

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

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

504 505 506 507
unpack() {
	default_unpack
}

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

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

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

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

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

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

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

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

Natanael Copa's avatar
Natanael Copa committed
622
build() {
623
	:
Natanael Copa's avatar
Natanael Copa committed
624 625
}

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

Natanael Copa's avatar
Natanael Copa committed
633 634 635 636 637 638 639 640 641
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
}

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

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

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

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

lang_subpkg() {
	default_lang_subpkg
752 753 754 755 756 757 758 759 760 761 762
}

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
}

763 764 765 766 767 768 769 770 771 772 773 774
# 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}'
}

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

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

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

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

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

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

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

843
	if [ -n "$replaces_priority" ]; then
Natanael Copa's avatar
Natanael Copa committed
844
		echo "replaces_priority = $replaces_priority" >> "$pkginfo"
845 846
	fi

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

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

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

906 907 908 909 910 911 912 913 914 915 916 917
# 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
}

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

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

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

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

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

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

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

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

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

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

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

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

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

scan_shared_objects() {
	local name="$1" controldir="$2" datadir="$3"
1058
	local opt= i=
1059 1060 1061

	# allow spaces in paths
	IFS=:
1062
	set -- $(find_scanelf_paths "$datadir")
1063 1064
	unset IFS

1065 1066 1067 1068 1069 1070 1071 1072
	# 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

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

		# we only want shared libs
		case $soname in
1088
		*.so|*.so.[0-9]*|*.c32);;
1089 1090 1091 1092 1093 1094 1095 1096 1097
		*) 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
1098 1099 1100 1101
			*.so.[0-9]*)
				if options_has "sover-namecheck"; then
					continue
				fi
1102 1103 1104
			esac
			;;
		esac