abuild.in 46.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
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
108

cleanup() {
	set_xterm_title ""
109
	if [ -z "$install_after" ] && [ -n "$uninstall_after" ]; then
110
		msg "Uninstalling dependencies..."
111
		$SUDO_APK del --quiet $apk_opt_wait $uninstall_after
112
	fi
113
114
115
	if [ -n "$CLEANUP_FILES" ]; then
		rm -f $CLEANUP_FILES
	fi
Linux User's avatar
Linux User committed
116
117
}

Natanael Copa's avatar
Natanael Copa committed
118
die() {
Linux User's avatar
Linux User committed
119
	error "$@"
Linux User's avatar
Linux User committed
120
	cleanup
Natanael Copa's avatar
Natanael Copa committed
121
122
123
	exit 1
}

124
125
126
127
spell_error() {
	die "APKBUILD contains '$1'. It should be '$2'"
}

128
# check if apkbuild is basicly sane
129
default_sanitycheck() {
130
	local i= j= suggestion=
131
	msg "Checking sanity of $APKBUILD..."
132
133
134
	[ -z "$pkgname" ] && die "Missing pkgname in APKBUILD"
	[ -z "${pkgname##* *}" ] && die "pkgname contains spaces"
	[ -z "$pkgver" ] && die "Missing pkgver in APKBUILD"
135
	if [ "$pkgver" != "volatile" ] && [ -z "$nodeps" ]; then
136
		$APK version --check --quiet "$pkgver" ||\
Natanael Copa's avatar
Natanael Copa committed
137
138
			die "$pkgver is not a valid version"
	fi
139
	[ -z "$pkgrel" ] && die "Missing pkgrel in APKBUILD"
140
141
142
	[ -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
143
144
145
	if [ $(echo "$pkgdesc" | wc -c) -gt 128 ]; then
		die "pkgdesc is too long"
	fi
146

Natanael Copa's avatar
Natanael Copa committed
147
148
	if [ -n "$replaces_priority" ] \
		&& ! echo $replaces_priority | egrep -q '^[0-9]+$'; then
149
150
		die "replaces_priority must be a number"
	fi
151
152
153
154
155
156
157
	# 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

158
	# check if CARCH, CBUILD, CHOST and CTARGET is set
159
160
161
162
	[ -z "$CHOST" ] && die "Please set CHOST in /etc/abuild.conf"
	[ -z "$CBUILD" ] && CBUILD="$CHOST"
	[ -z "$CTARGET" ] && CTARGET="$CHOST"

163
	if [ -z "$CARCH" ]; then
164
165
166
167
168
		case "$CHOST" in
		arm*-*-*-*)	CARCH="armel" ;;
		i[0-9]86-*-*-*)	CARCH="x86" ;;
		x86_64-*-*-*)	CARCH="x86_64" ;;
		*)		die "Please fix CHOST, or set CARCH in abuild.conf"
169
170
		esac
	fi
171
172
173
174
175
176
177
178
179
180
181
182
183
184

	# check CLIBC
	if [ -z "$CLIBC" ]; then
		if [ -z "$ALPINE_LIBC" ]; then
			case "$CHOST" in
			*-*-*-uclibc*)	CLIBC="uclibc" ;;
			*-*-*-musl*)	CLIBC="musl" ;;
			*-*-*-gnu*)	CLIBC="eglibc" ;;
			*)		CLIBC="uclibc" ;;
			esac
		else
			CLIBC="$ALPINE_LIBC"
		fi
	fi
185

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

205
206
207
	for i in $triggers; do
		local f=${i%=*}
		local p=${f%.trigger}
208
		[ "$f" = "$i" ] && die "$f: triggers must contain '='"
209
210
211
212
		[ "$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
213

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

231
232
233
234
235
236
237
238
	# 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"
239
		done
240
	done
241

242
	# common spelling errors
243
244
245
246
247
	[ -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
248
249

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

	makedepends_has 'g++' && warning "g++ should not be in makedepends"
252
253
254
	return 0
}

255
256
257
258
sanitycheck() {
	default_sanitycheck
}

259
260
sumcheck() {
	local algo="$1" sums="$2"
261
	local dummy f endreturnval originalparams origin file
262
263
264
265
266
267
268
269

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

293
294
295
296
297
298
299
300
301
# for compatibility
md5check() {
	warning "'md5check' is deprecated. Use 'verify' instead"
	sumcheck md5 "$md5sums"
}

# verify checksums
verify() {
	local verified=false algo=
302
	for algo in sha512 sha256 sha1 md5; do
303
304
305
306
307
308
309
		local sums=
		eval sums=\"\$${algo}sums\"
		if [ -z "$sums" ] || [ -z "$source" ]; then
			continue
		fi
		sumcheck "$algo" "$sums" || return 1
		verified=true
310
		break
311
312
313
314
315
316
317
	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
318
319
# verify upstream sources
sourcecheck() {
320
321
322
323
324
325
326
327
	local uri
	for uri in $source; do
		is_remote $uri || continue
		case "$uri" in
		saveas-*://*)
			uri=${uri#saveas-}
			uri=${uri%/*}
			;;
328
329
330
		*::*)
			uri=${uri##*::}
			;;
331
332
		esac
		wget -q -s "$uri" || return 1
Natanael Copa's avatar
Natanael Copa committed
333
	done
334
	return 0
Natanael Copa's avatar
Natanael Copa committed
335
}
336

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

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

362
363
364
365
366
	# 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)}";;
367
368
369
370
371

		*::*)
			d=${uri%%::*}
			uri=${uri#$d::}
			;;
Natanael Copa's avatar
Natanael Copa committed
372
	esac
373
374

	case "$uri" in
375
		https://*) opts="-k";;
376
	esac
377

Natanael Copa's avatar
Natanael Copa committed
378
	mkdir -p "$SRCDEST"
379
380
381

	CLEANUP_FILES="$CLEANUP_FILES $lockfile"
	(
382
383
	flock -n -x 9 || msg "Waiting for ${lockfile##*/}..."
	flock -x 9
384
385
386

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

387
388
	if [ -f "$SRCDEST/$d.part" ]; then
		msg "Partial download found. Trying to resume"
389
		opts="$opts -C -"
Natanael Copa's avatar
Natanael Copa committed
390
	fi
Linux User's avatar
Linux User committed
391
	msg "Fetching $uri"
392
393

	# fallback to wget if curl is missing. useful for bootstrapping
394
	local fetcher=
395
396
	if ! [ -x "$(which curl)" ]; then
		fetcher=wget_fallback
397
398
	else
		fetcher=curl
399
		opts="$opts -L -f -k"
400
401
402
	fi

	$fetcher $opts -o "$SRCDEST/$d.part" "$uri" \
403
		&& mv "$SRCDEST/$d.part" "$SRCDEST/$d"
404

405
	) 9>$lockfile
406
407
408
409

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

412
is_remote() {
413
	case "${1#*::}" in
414
		http://*|ftp://*|https://*|saveas-*://*)
415
416
417
418
419
			return 0;;
	esac
	return 1
}

420
421
422
423
424
425
426
427
428
filename_from_uri() {
	local uri="$1"
	local filename="${uri##*/}"  # $(basename $uri)
	case "$uri" in
	*::*) filename=${uri%%::*};;
	esac
	echo "$filename"
}

429
430
431
432
# try download from file from mirror first
uri_fetch_mirror() {
	local uri="$1"
	if [ -n "$DISTFILES_MIRROR" ]; then
433
		if is_remote "$DISTFILES_MIRROR"; then
434
435
			uri_fetch "$DISTFILES_MIRROR"/$(filename_from_uri $uri)\
				&& return 0
436
		else
437
438
			cp "$DISTFILES_MIRROR"/$(filename_from_uri $uri) \
				"$SRCDEST" && return 0
439
		fi
440
441
442
443
	fi
	uri_fetch "$uri"
}

444
default_fetch() {
Natanael Copa's avatar
Natanael Copa committed
445
446
	local s
	mkdir -p "$srcdir"
447
448
	for s in $source; do
		if is_remote "$s"; then
449
			uri_fetch_mirror "$s" || return 1
450
			ln -sf "$SRCDEST/$(filename_from_uri $s)" "$srcdir"/
451
		else
452
453
			ln -sf "$startdir/$s" "$srcdir/"
		fi
Natanael Copa's avatar
Natanael Copa committed
454
455
456
	done
}

457
458
459
460
fetch() {
	default_fetch
}

Natanael Copa's avatar
Natanael Copa committed
461
462
463
464
465
466
467
468
469
470
471
472
# 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
473
	done
Natanael Copa's avatar
Natanael Copa committed
474
475
}

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

510
511
512
513
unpack() {
	default_unpack
}

514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
default_post_unpack() {
	local i
	options_has "!configsub" && return 0
	for i in "$srcdir"/*/configure; do
		[ -x "$i" ] || continue
		find "${i%/configure}" -name config.sub -o -name config.guess \
			| while read f; do
				msg "Replacing ${f##*/}"
				cp "$datadir"/${f##*/} "$f" || return 1
			done
		break
	done
}

post_unpack() {
	default_post_unpack
}

Natanael Copa's avatar
Natanael Copa committed
532
533
# cleanup source and package dir
clean() {
534
	msg "Cleaning temporary build dirs..."
Natanael Copa's avatar
Natanael Copa committed
535
	rm -rf "$srcdir"
536
	rm -rf "$pkgbasedir"
Natanael Copa's avatar
Natanael Copa committed
537
538
539
540
541
542
}

# cleanup fetched sources
cleancache() {
	local s
	for s in $source; do
543
		if is_remote "$s"; then
544
545
546
			s=$(filename_from_uri $s)
			msg "Cleaning downloaded $s ..."
			rm -f "$SRCDEST/$s"
547
		fi
Natanael Copa's avatar
Natanael Copa committed
548
549
550
	done
}

551
552
553
554
555
556
557
558
559
560
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
561
562
cleanpkg() {
	local i
563
	getpkgver || return 1
564
	msg "Cleaning built packages..."
565
	for i in $(listpkgnames); do
566
567
		local p="${i%:*}-$pkgver-r$pkgrel"
		rm -f "$PKGDEST/$p.apk" "$PKGDEST/$p.src.tar.gz" \
568
			"$abuildrepo"/$p.apk "$abuildrepo"/*/$p.apk
Natanael Copa's avatar
Natanael Copa committed
569
	done
570
	# remove given packages from index
571
	update_abuildrepo_index
Natanael Copa's avatar
Natanael Copa committed
572
573
}

574
575
576
# clean all packages except current
cleanoldpkg() {
	local i j
577
	getpkgver || return 1
578
	msg "Cleaning all packages except $pkgver-r$pkgrel..."
579
	for i in $(listpkgnames); do
580
581
582
583
584
		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##*/}
585
586
587
588
		done
	done
	return 0
}
589
590
591

mkusers() {
	local i
592
593
594
595
596
597
	for i in $pkggroups; do
		if ! getent group $i >/dev/null; then
			msg "Creating group $i"
			$ADDGROUP -S $i || return 1
		fi
	done
598
599
	for i in $pkgusers; do
		if ! getent passwd $i >/dev/null; then
600
			local gopt=
601
			msg "Creating user $i"
602
603
604
			if getent group $i >/dev/null; then
				gopt="-G $i"
			fi
605
			$ADDUSER -S -D -H $gopt $i || return 1
606
607
608
609
610
		fi
	done
}


Natanael Copa's avatar
Natanael Copa committed
611
612
runpart() {
	local part=$1
613
	[ -n "$DEBUG" ] && msg "$part"
Natanael Copa's avatar
Natanael Copa committed
614
615
616
617
	$part || die "$part failed"
}

# override those in your build script
618
619
620
621
622
623
624
625
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
626
627
628
629
prepare() {
	:
}

Natanael Copa's avatar
Natanael Copa committed
630
build() {
631
	:
Natanael Copa's avatar
Natanael Copa committed
632
633
}

634
635
636
# generate a simple tar.gz package of pkgdir
targz() {
	cd "$pkgdir" || return 1
Natanael Copa's avatar
Natanael Copa committed
637
	mkdir -p "$PKGDEST"
638
639
640
	tar -czf "$PKGDEST"/$pkgname-$pkgver-r$pkgrel.tar.gz *
}

Natanael Copa's avatar
Natanael Copa committed
641
642
643
644
645
646
647
648
649
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
}

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

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

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

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

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

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
}

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

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

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

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

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

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

830
831
832
833
834
835
836
837
838
839
	local last_commit="$(git_last_commit)$(git_dirty)"
	if [ -n "$last_commit" ]; then
		echo "commit = $last_commit" >> "$pkginfo"
	fi

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

840
	if [ -n "$replaces_priority" ]; then
Natanael Copa's avatar
Natanael Copa committed
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
1036
		paths="$paths:$(echo "$ldpath" | sed "s|\(^\|:\)|\1$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=
1056
1057
1058

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

1062
1063
1064
	if options_has "ldpath-recursive"; then
		opt="--recursive"
	fi
1065
1066
	msg "Scanning shared objects"
	# lets tell all the .so files this package provides in .provides-so
1067
	scanelf --nobanner --soname $opt "$@" | while read etype soname file; do
1068
1069
		# if soname field is missing, soname will be the filepath
		sover=0
1070
1071
		if [ -z "$file" ]; then
			file="$soname"
1072
			soname=${soname##*/}
1073
1074
1075
1076
		fi

		# we only want shared libs
		case $soname in