-
Notifications
You must be signed in to change notification settings - Fork 0
/
gabs-build
executable file
·307 lines (258 loc) · 9.33 KB
/
gabs-build
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
#!/bin/zsh -x
# Find and load script config
local SCRIPTS_DIR="${0:A:h}"
[[ ! -d "${SCRIPTS_DIR}" ]] && exit 1
source "${SCRIPTS_DIR}/gabs-build-common" || exit $?
local SHOULD_RESUME=false
local SHOULD_RESUME_AFTER=false
local RESUME_PKG=
local BUILTIN_PACKAGES=()
local PACKAGES=()
local FIRST_PARAMS=()
local EVERY_PARAMS=()
local ONE_ONLY=false
local BACKUP_DIR=
local CHROOT_NAME=
local LOOP_DEVICE_SIZE=
local UNMOUNT_LOOP_DEVICE=true
local EPHEMERAL_DIR=
local BUILD_CONFIG_SCRIPT=
local DONT_UPDATE_PACKAGES=false
while [[ $# -gt 0 ]]; do
case "${1}" in
-r)
if [[ "${SHOULD_RESUME}" != "false" ]]; then
>&2 echo -r conflicts with either other -r or -rp
exit 1
fi
SHOULD_RESUME=true
;;
-rp)
if [[ "${SHOULD_RESUME}" != "false" ]]; then
>&2 echo -rp conflicts with either other -rp or -r
exit 1
fi
SHOULD_RESUME=true
RESUME_PKG="${2}"
shift
;;
-ra) SHOULD_RESUME_AFTER=true;;
-p) PACKAGES+=("${2}"); shift;;
-fp) FIRST_PARAMS+=("${2}"); shift;;
-ep) EVERY_PARAMS+=("${2}"); shift;;
-o) ONE_ONLY=true;;
-bk) BACKUP_DIR="${2}"; shift;;
-cn) CHROOT_NAME="${2}"; shift;;
-lds) LOOP_DEVICE_SIZE="${2}"; shift;;
-kld) UNMOUNT_LOOP_DEVICE=false;;
-ed) EPHEMERAL_DIR="${2}"; shift;;
-bcs) BUILD_CONFIG_SCRIPT="${2}"; shift;;
-dup) DONT_UPDATE_PACKAGES=true;;
--help)
>&2 echo "Please refer to the man page."
exit
;;
*) # unknown option
>&2 echo "Unkown option \"${1}\""
exit 1
;;
esac
shift
done
builtin_config_script_try_dirs
BUILTINS_DIR=
error_blank_nondir_param -f BUILD_CONFIG_SCRIPT
source "${BUILD_CONFIG_SCRIPT}" || exit $?
[[ ! -d "${BUILTINS_DIR}" ]] && exit 1
error_blank_param -d EPHEMERAL_DIR
if [[ "${USER}" != "root" || "${SUDO_USER}" == "" ]]; then
>&2 echo "gabs-build was not started via sudo."
exit 1
fi
if [[ "${#EVERY_PARAMS[@]}" == "0" && "${BACKUP_DIR}" == "" ]]; then
EVERY_PARAMS=("-w")
print_fallback_param "EVERY_PARAMS" "${EVERY_PARAMS[@]}"
fi
if [[ "${CHROOT_NAME}" == "" ]]; then
CHROOT_NAME="chroot"
print_fallback_param "CHROOT_NAME" "${CHROOT_NAME}"
fi
if [[ "${#PACKAGES[@]}" == "0" ]]; then
PACKAGES=("${BUILTIN_PACKAGES[@]}")
print_fallback_param "PACKAGES" "${PACKAGES[@]}"
fi
if [[ "${SHOULD_RESUME}" == "true" && "${RESUME_PKG}" == "" ]]; then
RESUME_PKG="$(cat "${EPHEMERAL_DIR}/last_pkg.${CHROOT_NAME}")"
if [[ "${RESUME_PKG}" == "" ]]; then
RESUME_PKG="${PACKAGES[1]}"
fi
print_fallback_param "RESUME_PKG" "${RESUME_PKG}"
fi
for PKG_DIR in ${PACKAGES}; do
local EVAL_PKG_DIR="$(eval "echo \"${PKG_DIR}\"")"
if [[ ! -d "${EVAL_PKG_DIR}" ]]; then
>&2 echo "Package \"${PKG_DIR}\" (\"${EVAL_PKG_DIR}\") is missing"
>&2 echo "Have you called \`git submodule update --init --recursive\` yet?"
exit 1
fi
done
mkdir -p "${EPHEMERAL_DIR}/custom.${CHROOT_NAME}"
# If line starts with BUILDENV and this option is "sign" add a "!".
awk '{
if (/^ *BUILDENV *= *\( */) {
split($0, a, "(");
for (i in a) {
if (i != 1) printf "("
if (i == 2) {
split(a[i], b, ")");
for (j in b) if (j == 1) {
split(b[j], c, " ")
for(k in c) {
if (k != 1) printf " "
if(c[k] == "sign") printf "!"
printf c[k]
}
} else printf ")" b[j]
} else printf a[i]
}
printf "\n"
} else print
}' /etc/makepkg.conf > "${EPHEMERAL_DIR}/custom.${CHROOT_NAME}/makepkg.conf"
local USE_DISTCC
local USE_CCACHE
if_buildenv_uses DISTCC
if_buildenv_uses CCACHE
local CHROOT_DIR="${EPHEMERAL_DIR}/chroots/${CHROOT_NAME}/chroot"
local CHROOT_MOUNT="${EPHEMERAL_DIR}/chroots/${CHROOT_NAME}"
local CHROOT_FILE="${EPHEMERAL_DIR}/chroots/${CHROOT_NAME}.img"
local PKGBUILD_CLONE_DIR="${EPHEMERAL_DIR}/pkgbuilds.${CHROOT_NAME}"
local REMAKE_CHROOT=false
[[ "${SHOULD_RESUME}" == "false" || ! -f "${CHROOT_FILE}" ]] \
&& REMAKE_CHROOT=true
if [[ "${REMAKE_CHROOT}" == "true" ]]; then
mkdir -p "${PKGBUILD_CLONE_DIR}"
unsetopt nomatch
for PKGBUILD_CLONE in "${PKGBUILD_CLONE_DIR}"/*; do
rm "${PKGBUILD_CLONE}/data/packages"
done
setopt nomatch
mkdir -p "${EPHEMERAL_DIR}/alt_pkgs.${CHROOT_NAME}"
mkdir -p "${EPHEMERAL_DIR}/chroots"
rm "${EPHEMERAL_DIR}/last_pkg.${CHROOT_NAME}"
mkdir -p "${EPHEMERAL_DIR}/pkgs.old.${CHROOT_NAME}"
mkdir -p "${EPHEMERAL_DIR}/pkgs.${CHROOT_NAME}"
mv "${EPHEMERAL_DIR}/pkgs.${CHROOT_NAME}/"* \
"${EPHEMERAL_DIR}/pkgs.old.${CHROOT_NAME}"
fi
"${SCRIPTS_DIR}/gabs-build-chroot" \
-cm "${CHROOT_MOUNT}" \
-cf "${CHROOT_FILE}" \
-c
local BUILD_CHROOT_PARAMS=()
[[ "${REMAKE_CHROOT}" == true ]] && BUILD_CHROOT_PARAMS+="-rm"
[[ "${LOOP_DEVICE_SIZE}" != "" ]] && BUILD_CHROOT_PARAMS+=("-lds" "${LOOP_DEVICE_SIZE}")
local LOOP_DEVICE="$("${SCRIPTS_DIR}/gabs-build-chroot" \
-cm "${CHROOT_MOUNT}" \
-cf "${CHROOT_FILE}" \
"${BUILD_CHROOT_PARAMS[@]}" \
-s | tail -n1 || exit $?)"
trap cleanup_loopdevice2 EXIT
if [[ "${REMAKE_CHROOT}" == "true" ]]; then
mkdir -p "${CHROOT_DIR}"
local INSTALL_PACKAGES=(base-devel gcc-multilib zsh neovim git)
[[ "${USE_CCACHE}" == "true" ]] && INSTALL_PACKAGES+=(ccache)
[[ "${USE_DISTCC}" == "true" ]] && INSTALL_PACKAGES+=(distcc python procps-ng)
/usr/bin/mkarchroot \
-C /etc/pacman.conf \
-M "${EPHEMERAL_DIR}/custom.${CHROOT_NAME}/makepkg.conf" \
-c /var/cache/pacman/pkg \
"${CHROOT_DIR}/root" \
"${INSTALL_PACKAGES[@]}" || exit $?
nspawn "${CHROOT_DIR}/root" /bin/bash -xc 'yes | pacman -Syu' || exit $?
mkdir -p "${CHROOT_DIR}/root/alt_pkgs.${CHROOT_NAME}/"
cp "${EPHEMERAL_DIR}/alt_pkgs.${CHROOT_NAME}/"*.pkg* "${CHROOT_DIR}/root/pkgs/"
if [[ "$(ls "${CHROOT_DIR}/root/pkgs/")" != "" ]]; then
nspawn "${CHROOT_DIR}/root" /bin/bash -xc "yes | pacman -U /pkgs/*" || exit $?
fi
else
clone_file {"${EPHEMERAL_DIR}/custom.${CHROOT_NAME}",/etc}/makepkg.conf root:root 644
if [[ "${DONT_UPDATE_PACKAGES}" == false ]]; then
for CHROOT_DIR2 in "${CHROOT_DIR}"/*; do
[[ ! -d "${CHROOT_DIR2}" ]] && continue
nspawn "${CHROOT_DIR2}" /bin/bash -xc 'yes | pacman -Syu' || exit $?
done
fi
fi
if [[ "${USE_DISTCC}" == "true" ]]; then
clone_file /etc/distcc/hosts{,} root:root 644
clone_file /etc/conf.d/distccd{,} root:root 644
FIRST_PARAMS+=(-dist)
EVERY_PARAMS+=(-dist)
fi
for CHROOT_DIR2 in "${CHROOT_DIR}"/*; do
[[ ! -d "${CHROOT_DIR2}" ]] && continue
if [[ "${USE_DISTCC}" == "true" ]]; then
ln -s /usr/bin/distcc "${CHROOT_DIR2}/usr/lib/distcc/bin/clang++"
ln -s /usr/bin/distcc "${CHROOT_DIR2}/usr/lib/distcc/bin/clang"
ln -s /usr/bin/distcc "${CHROOT_DIR2}/usr/lib/distcc/clang++"
ln -s /usr/bin/distcc "${CHROOT_DIR2}/usr/lib/distcc/clang"
else
rm "${CHROOT_DIR2}/usr/lib/distcc/bin/clang++"
rm "${CHROOT_DIR2}/usr/lib/distcc/bin/clang"
rm "${CHROOT_DIR2}/usr/lib/distcc/clang++"
rm "${CHROOT_DIR2}/usr/lib/distcc/clang"
fi
done
[[ "${BACKUP_DIR}" != "" ]] && mkdir -p "${BACKUP_DIR}"
for PKG_DIR in ${PACKAGES}; do
local EVAL_PKG_DIR="$(eval "echo \"${PKG_DIR}\"")"
if [[ "${SHOULD_RESUME}" != "false" ]]; then
if [[ "${EVAL_PKG_DIR}" != "$(eval "echo \"${RESUME_PKG}\"")" ]]; then
>&2 echo "Skipping \"${PKG_DIR}\" (\"${EVAL_PKG_DIR}\")"
continue
fi
SHOULD_RESUME=false
if [[ "${SHOULD_RESUME_AFTER}" == "true" ]]; then
continue
fi
fi
>&2 echo "Doing \"${PKG_DIR}\" (\"${EVAL_PKG_DIR}\")"
>&2 echo "${PKG_DIR}" > "${EPHEMERAL_DIR}/last_pkg.${CHROOT_NAME}"
local PARAMS=(${EVERY_PARAMS[@]})
local PKGNAME="$(basename "${EVAL_PKG_DIR}").$(md5sum <(echo "${PKG_DIR}") | cut -d" " -f1)"
if [[ "${#FIRST_PARAMS[@]}" != "0" ]]; then
PARAMS=("${FIRST_PARAMS[@]}")
FIRST_PARAMS=()
fi
PARAMS+=("-bk" "${BACKUP_DIR}/${PKGNAME}")
local BUILD_SCRIPT="${EVAL_PKG_DIR}/gabs-build"
if [[ ! -f "${BUILD_SCRIPT}" ]]; then
BUILD_SCRIPT="${SCRIPTS_DIR}/gabs-build-handler"
fi
mkdir -p "${PKGBUILD_CLONE_DIR}/${PKGNAME}"
rsync -chavPASWX --stats --inplace --compress-level=0 \
--chown="${SUDO_USER}:${SUDO_USER}" \
"${EVAL_PKG_DIR}/" "${PKGBUILD_CLONE_DIR}/${PKGNAME}" || exit $?
pushd "${PKGBUILD_CLONE_DIR}/${PKGNAME}"
>&2 echo "Delegating \"${PKG_DIR}\" (\"${EVAL_PKG_DIR}\") @ \"${PKGBUILD_CLONE_DIR}/${PKGNAME}\" to \"${BUILD_SCRIPT}\""
"${BUILD_SCRIPT}" "${PARAMS[@]}" \
-c "${CHROOT_DIR}" \
-cn "${CHROOT_NAME}" \
-ed "${EPHEMERAL_DIR}" \
-rpd "${EVAL_PKG_DIR}" \
-bcs "${BUILD_CONFIG_SCRIPT}" || {
local ERROR=$?
>&2 echo "Failed to build \"${PKG_DIR}\" (\"${EVAL_PKG_DIR}\")"
UNMOUNT_LOOP_DEVICE=false
exit ${ERROR}
}
>&2 echo "Done \"${PKG_DIR}\" (\"${EVAL_PKG_DIR}\")"
popd
if [[ "${ONE_ONLY}" == "true" ]]; then
exit
fi
done
if [[ "${SHOULD_RESUME}" != "false" ]]; then
>&2 echo "Could not find package to resume at."
exit 1
fi