summaryrefslogtreecommitdiff
path: root/src/chroot-tools/libremakepkg
blob: 3fa2f3d6277facd0c77dad4a36896cdec32feade (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
#!/bin/bash
# libremakepkg
# analogous to devtools' archbuild

# Copyright 2010 - 2011 Nicolás Reynolds
# Copyright 2011 Joshua Ismael Haase Hernández
# Copyright 2012 Luke Shumaker

# ---------- GNU General Public License 3 ----------

# 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/>.

. /etc/libretools.conf

##
# copy logs if they exist
##
copy_log() {
	find "${copydir}/build/" -maxdepth 1 -name "*\.log" -exec cp {} ./ \;
}

##
# End inmediately but print a useful message
##
trap_exit() {
	copy_log
	error "$@"
	exit 1
}

usage() {
    echo 'cd to a dir containing a PKGBUILD and run:'
    echo '$0 [options] [-- makechrootpkg args [-- makepkg args]]'
    echo 'This script will build your package in a chroot.'
    echo ''
    echo 'OPTIONS:'
    echo ' -h              Show this message'
    echo ''
    echo ' -c              Clean the chroot before building'
    echo ' -u              Update the chroot before building'
    echo ' -N              Do not check freedom issues (for fullpkg)'
    echo ''
    echo " -d <chrootdir>  Use this dir instead of \`$CHROOTDIR'"
    echo " -n <chroot>     Use this chroot instead of \`$CHROOT'"
    echo ' -l <copy>       Use this chroot copy instead of basing it'
    echo '                 on the username'
}

main() {
	# The logic for setting CHROOTCOPY is mirred from makechrootpkg
	CHROOTCOPY=$USER
	[[ -n $SUDO_USER ]] && CHROOTCOPY=$SUDO_USER
	[[ -z $CHROOTCOPY || $CHROOTCOPY = root ]] && CHROOTCOPY=copy

	CLEANFIRST=false
	UPDATEFIRST=false
	CHECKNONFREE=true

	makechrootpkg_args=()

	while getopts 'hcuNd:n:l:' arg ; do
		case "${arg}" in
			c) CLEANFIRST=true;;
			u) UPDATEFIRST=true;;
			N) CHECKNONFREE=false;;

			d) CHROOTDIR=$OPTARG;;
			n) CHROOT=$OPTARG;;
			l) CHROOTCOPY=$OPTARG;;

			h) usage; exit 0;;
			*) usage; exit 1;;
		esac
	done
	shift $(($OPTIND - 1))
	# Pass all arguments after -- right to makechrootpkg
	makechrootpkg_args+=("$@")

	# not local
	rootdir="${CHROOTDIR}/${CHROOT}/root"
	copydir="${CHROOTDIR}/${CHROOT}/${CHROOTCOPY}"

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

	if [[ ! -e PKGBUILD ]]; then
		error "This isn't a build directory"
		exit 1
	fi

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

	# Trap signals from makepkg
	set -E
	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 unknown error has occurred. Exiting..."' ERR

	if $CHECKNONFREE; then
		msg "Checking PKGBUILD for non-free issues"
		if ! pkgbuild-check-nonfree; 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
	fi

	if $CLEANFIRST; then
		librechroot -c -d "$CHROOTDIR" -l "$CHROOTCOPY" "$CHROOT"
	fi

	if $UPDATEFIRST; then
		librechroot -u -d "$CHROOTDIR" -l "$CHROOTCOPY" "$CHROOT"
	fi

	unset CLEANFIRST UPDATEFIRST librechroot_args

	makechrootpkg "${makechrootpkg_args[@]}" -d -r "$CHROOTDIR/$CHROOT" -l "$CHROOTCOPY"
	ev=$? # exit value
	copy_log
	exit $ev
}

main "$@"