summaryrefslogtreecommitdiff
path: root/src/chroot-tools/libremakepkg
blob: c4a2cc88d46199fe8a2fcc0323650bfe5b8be50b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/bin/bash -euE
# libremakepkg

# Copyright 2010 - 2011 Nicolás Reynolds
# Copyright 2011 Joshua Ismael Haase Hernández
# Copyright 2012-2013 Luke Shumaker
#
# This file is part of Parabola.
#
# Parabola is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Parabola is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Parabola. If not, see <http://www.gnu.org/licenses/>.

. $(librelib conf.sh)
load_files chroot

. libremessages
makechrootpkg=$(librelib makechrootpkg)

shopt -s nullglob
umask 0022

# Boring/mundane functions #####################################################

# End inmediately but print a useful message
trap_exit() {
	error "$*"
	$INCHROOT || chroot_copy_out "$copydir" "$LIBREUSER"
	exit 1
}

# Usage run [-N] $copydir "$cmd"
# Runs cmd properly, whether in a chroot already or not.
#
# Note that $cmd is a quoted string, not a list of arguments.
# $copydir=/ if INCHROOT=true
#
# Environment
# - $INCHROOT is set
run() (
	local HASNET=true
	[[ $1 == -N ]] && { HASNET=false; shift; }
	local copydir=$1; shift
	local cmd="$*"

	if $HASNET; then
		trap "rm -f -- '$copydir/chrootexec'" EXIT
	else
		distcc_start "$copydir"
		trap "rm -f -- '$copydir/chrootexec'; distcc_stop '$copydir'" EXIT
	fi

	cat >"$copydir/chrootexec" <<EOF
#!/bin/bash
. /etc/profile
${INCHROOT} || export HOME=/build
${INCHROOT} || cd /build

${cmd}
EOF
	chmod 755 "$copydir/chrootexec"

	local flags=''
	if $INCHROOT; then
		$HASNET || flags='-n'
		unshare $flags -- /chrootexec
	else
		$HASNET || flags='-N'
		librechroot $flags -l "$copydir" run /chrootexec
	fi
)

distcc_start() {
	local copydir=$1
	# Because /{,usr/}{,s}bin are all symlinked together for
	# fileystem 2013.05-2 and up, I can take shortcuts when checking for
	# existance of programs.
	if [[ -f "$copydir/bin/socat" && -f "$copydir/bin/distcc" ]]; then
		local home=$LIBREHOME
		$INCHROOT || home="$copydir/build"

		cp -a "$(which distcc-tool)" "$copydir/distcc-tool"

		mkdir -p "$home/.ssh"

		printf '%s\n' \
			'/distcc-tool idaemon "$DISTCC_HOSTS" &' \
			'DISTCC_HOSTS="$(/distcc-tool rewrite "$DISTCC_HOSTS")"' \
			> "$home/.makepkg.conf"

		printf '%s\n' \
			'Host *' \
			'    ProxyCommand /distcc-tool client %h %p' \
			> "$home/.ssh/config"

		distcc-tool odaemon "$copydir" &
		echo $! > "$copydir/distcc-tool.pid"
	fi
}

distcc_stop() {
	local copydir=$1
	local home=$LIBREHOME
	$INCHROOT || home="$copydir/build"
	if [[ -f "$copydir/distcc-tool.pid" ]]; then

		odaemon=$(cat "$copydir/distcc-tool.pid")
		kill -- $odaemon

		rm -f -- \
			"$copydir/distcc-tool" \
			"$home/.makepkg.conf" \
			"$home/.ssh/config" \
			"$copydir/distcc-tool.pid"
	fi
}

# Usage: add_to_local_repo $copydir $pkgfiles...
add_to_local_repo() (
	set +euE
	. $makechrootpkg
	chroot_add_to_local_repo "$@"
)

# Usage: chroot_copy_in $copydir
# Environment:
#  - In the dirctory of a PKGBUILD
#  - $SRCDEST is set
chroot_copy_in() (
	set +euE
	. $makechrootpkg
	chroot_copy_in "$@"
)

# Usage: chroot_copy_out $copydir $owner
# Environment:
#  - $SRCDEST is set
#  - $PKGDEST is set
chroot_copy_out() (
	set +euE
	. $makechrootpkg
	chroot_copy_out_pkgs "$@"
	chroot_copy_out_logs "$@"
	chroot_copy_out_srcs "$@"
)

# Usage: chroot_let_nobody_use_pacman $copydir
chroot_let_nobody_use_pacman() (
	set +euE
	. $makechrootpkg
	chroot_let_nobody_use_pacman "$@"
)

# Usage: chroot_init $copydir $repack
# Environment
# - $LIBREHOME is set
chroot_init() {
	local copydir=$1
	local repack=$2

	librechroot -l "$copydir" make # make sure the chroot exists
	mkdir -p "$copydir"/{build,pkgdest,srcdest}

	# Remove anything in there UNLESS -R (repack) was passed
	$repack || rm -rf "$copydir"/build/*

	if [[ -r "$LIBREHOME/.gnupg/pubring.gpg" ]]; then
		install -D "$LIBREHOME/.gnupg/pubring.gpg" "$copydir/build/.gnupg/pubring.gpg"
	fi
	rm -f "$copydir/build/.makepkg.conf"

	MAKEPKG_CONF="$copydir/etc/makepkg.conf" set_conf_makepkg PKGDEST /pkgdest
	MAKEPKG_CONF="$copydir/etc/makepkg.conf" set_conf_makepkg SRCDEST /srcdest
	local PACKAGER="$(get_conf_makepkg PACKAGER '')"
	if [[ -n $PACKAGER ]]; then
		MAKEPKG_CONF="$copydir/etc/makepkg.conf" set_conf_makepkg PACKAGER "$PACKAGER"
	fi
	unset PACKAGER

	if ! grep -q '^\[repo\]' "$copydir/etc/pacman.conf"; then
		cat >> "$copydir/etc/pacman.conf" <<EOF
[repo]
SigLevel = Optional TrustAll
Server = file:///repo
EOF
	fi

	chroot_let_nobody_use_pacman "$copydir"
}

# Core functions ###############################################################

# Usage: extract
# Extracts the sources (`makepkg -o`)
# Environment:
# - $INCHROOT is set
# - $copydir is set
# - $LIBREUSER is set
extract() (
	local user=$LIBREUSER
	$INCHROOT || user=nobody

	local clean
	if $INCHROOT; then
		clean=chcleanup
	else
		trap "rm -f '$copydir/clean'" EXIT
		cp -a "$(which chcleanup)" "${copydir}/clean"
		clean=/clean
	fi

	run "$copydir" "${clean} && sudo -u ${user} -- makepkg ${makepkg_args} -o"
)

# Usage: build
# Builds the package (`makepkg -e`)
# Environment:
# - $INCHROOT is set
# - $copydir is set
# - $LIBREUSER is set
build() {
	local user=$LIBREUSER
	$INCHROOT || user=nobody

	if $NONET; then
		run -N "$copydir" "sudo -u ${user} -- makepkg ${makepkg_args} -e"
	else
		run "$copydir" "sudo -u ${user} -- makepkg ${makepkg_args} -e"
	fi
}

# Functions that check for issues with the build ###############################

check_pkgbuild() {
	msg "Checking PKGBUILD for issues"
	# TODO
	if ! pkgbuild-check-nonfree -f; then
		if [[ $? -eq 15 ]]; then
			# other errors mean fail, not nonfree
			error "PKGBUILD contains non-free issues"
			exit 15
		else
			warning "PKGBUILD couldn't be check aganist non-free issues"
		fi
	fi
}

check_src() {
	msg "Checking src directory for issues"
	# TODO
}

check_pkg() {
	msg "Checking final package for issues"
	# TODO
}


# The main program #############################################################

cmd=${0##*/}
usage() {
	echo "Usage: $cmd [options] [-- makepkg args]"
	echo 'This program will build your package.'
	echo ''
	echo 'If run from outside of a chroot, command will make the following'
	echo 'configuration changes in the chroot:'
	echo '  - whatever changes `librechroot` makes.'
	echo '  - set `PKGDEST` and `SRCDEST` in `/etc/makepkg.conf`'
	echo '  - set `PACKAGER` in `/etc/makepkg.conf` to reflect the value'
	echo '    outside of the chroot.'
	echo '  - (maybe) delete `/build/.makepkg.conf`'
	echo '  - (maybe) delete `/build/.ssh/config`'
	echo 'If run from inside of a chroot, this command will:'
	echo '  - (maybe) delete `~/.makepkg.conf`'
	echo '  - (maybe) delete `~/.ssh/config`'
	echo "The above 'maybe's happen as part of the workarounds to make"
	echo 'distcc work in a network-less environment.  They will happen if'
	echo 'both `socat` and `distcc` are installed in the chroot.'
	echo ''
	echo "The \`-n' and \`-l' options behave identically to librechroot,"
	echo 'see the documentation there.'
	echo ''
	echo 'Options:'
	echo '  -n <CHROOT>   Name of the chroot to use'
	echo '  -l <COPY>     Name of, or absolute path to, the copy to use'
	echo "  -N            Don't disable networking during build() or package()"
	echo "                PLEASE don't use this unless you have a special"
	echo "                reason, its use is a violation of Parabola policy."
	echo '  -R            Repackage contents of the package without rebuilding'
	echo '  -h            Show this message'
}

# Globals: $CHROOTDIR, $CHROOT, $COPY and $copydir
# Globals: $makepkg_args, $INCHROOT
main() {
	# Parse command line ###################################################

	COPY=$LIBREUSER
	[[ $COPY != root ]] || COPY=copy

	makepkg_args='-s --noconfirm -L '
	local repack=false

	INCHROOT=false
	if [[ -f /.arch-chroot ]]; then
		INCHROOT=true
	fi

	NONET=true
	while getopts 'n:l:NRh' arg ; do
		case "${arg}" in
			n) CHROOT=$OPTARG;;
			l) COPY=$OPTARG;;
			N) NONET=false;;
			R) repack=true; makepkg_args+=" -R";;
			h) usage; return 0;;
			*) usage; return 1;;
		esac
	done
	shift $(($OPTIND - 1))
	# Pass all arguments after -- right to makepkg
	makepkg_args+=" $*"

	if $INCHROOT; then
		copydir='/'
	else
		check_vars chroot CHROOTDIR CHROOT
		if [[ ${COPY:0:1} = / ]]; then
			copydir=$COPY
		else
			copydir="${CHROOTDIR}/${CHROOT}/${COPY}"
		fi
	fi

	# Init #################################################################

	if (( EUID )); then
		error "This script must be run as root"
		exit 1
	fi

	if [[ ! -f PKGBUILD ]]; then
		# This is the message used by makepkg
		error "PKGBUILD does not exist."
		exit 1
	fi

	# Trap signals from makepkg
	trap 'trap_exit "(libremakepkg): TERM signal caught. Exiting..."' TERM HUP QUIT
	trap 'trap_exit "(libremakepkg): Aborted by user! Exiting..."' INT
	trap 'trap_exit "(libremakepkg): An error has occurred. Exiting..."' ERR

	SRCDEST="$(get_conf_makepkg SRCDEST .)"
	PKGDEST="$(get_conf_makepkg PKGDEST .)"

	# OK, we're starting now ###############################################

	$INCHROOT || lock_open_write 9 "$copydir" \
		"Waiting for existing lock on chroot copy to be released: [$COPY]"

	# Set target CARCH as it might be used within the PKGBUILD to select
	# correct sources
	MAKEPKG_CONF=$copydir/etc/makepkg.conf
	export CARCH="$(get_conf_makepkg CARCH)"
	unset MAKEPKG_CONF

	$INCHROOT || chroot_init "$copydir" "$repack"

	check_pkgbuild
	$INCHROOT || chroot_copy_in "$copydir"
	$repack || extract
	check_src
	build
	check_pkg

	add_to_local_repo "$copydir" "$copydir"/pkgdest/*.pkg.tar*
	$INCHROOT || chroot_copy_out "$copydir" "$LIBREUSER"
}

main "$@"