summaryrefslogtreecommitdiff
path: root/src/chroot-tools/libremakepkg
blob: 2901b0d6a0b80cf78babbb3773a5318c000c3dd6 (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
#!/bin/bash -euE
# libremakepkg

# Copyright 2010 - 2011 Nicolás Reynolds
# Copyright 2011 Joshua Ismael Haase Hernández
# Copyright 2012 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/>.

. /usr/share/libretools/conf.sh
load_conf_libretools_chroot

. "$(which libremessages)"

shopt -s nullglob

# This file (libremakepkg) is GPLv3+, but I would like to use some code
# modified from devtools' "makechrootpkg", which is GPLv2.
. "$(dirname "$0")/libremakepkg.gpl2"
# This gives us the functions:
#  - chroot_init
#  - chroot_exec
#  - chroot_copy_in
#  - chroot_copy_out
#  - add_to_local_repo

# Boring functions #############################################################

##
# copy logs if they exist
##
copy_logs() {
	for l in "$copydir"/build/*.log; do
		chown "$LIBREUSER" "$l"
		mv "$l" .
	done
}

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

extract() {
	local user=$LIBREUSER
	$INCHROOT || user=nobody

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

	chroot_exec "${clean} && sudo -u ${user} ${MAKEPKG} ${makepkg_args} -o"
}

build() {
	local user=$LIBREUSER
	$INCHROOT || user=nobody

	chroot_exec -N "sudo -u ${user} ${MAKEPKG} ${makepkg_args} -e"
}

# 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, this will set PKGDEST and'
	echo "SRCDEST in the chroot's \`/etc/makepkg.conf', as well as making"
	echo "whataver alterations to the chroot \`librechroot' makes."
	echo ''
	echo 'Options:'
	echo "  -n <CHROOT>   Use this chroot instead of \`$CHROOT'"
	echo "  -l <COPY>     Use this chroot copy instead \`$CHROOTCOPY'"
	echo "  -m <MAKEPKG>  Use the command MAKEPKG instead of \'makepkg'"
	echo '  -h            Show this message'
}

main() {
	# Parse command line ###################################################

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

	makepkg_args='-s --noconfirm -L '
	MAKEPKG=makepkg

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

	while getopts 'n:l:m:Rh' arg ; do
		case "${arg}" in
			n) CHROOT=$OPTARG;;
			l) CHROOTCOPY=$OPTARG;;
			m) MAKEPKG=$OPTARG;;
			h) usage; exit 0;;
			*) usage; exit 1;;
		esac
	done
	shift $(($OPTIND - 1))
	# Pass all arguments after -- right to makepkg
	makepkg_args+=" $*"

	if $INCHROOT; then
		copydir=''
	else
		copydir="${CHROOTDIR}/${CHROOT}/${CHROOTCOPY}"
	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 unknown error has occurred. Exiting..."' ERR

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

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

	lock_open_write 9 "$copydir" \
		"Waiting for existing lock on \`$copydir' to be released"

	# 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

	check_pkgbuild
	$INCHROOT || chroot_copy_in
	extract
	check_src
	build
	check_pkg

	add_to_local_repo
	$INCHROOT || chroot_copy_out
}

main "$@"