-
Notifications
You must be signed in to change notification settings - Fork 0
/
gabs-build-handler
executable file
·245 lines (210 loc) · 7.74 KB
/
gabs-build-handler
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
#!/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 STARTING_PARAMS=("$@")
local MAKEPKG_PARAMS=("-s")
local MAKECHROOTPKG_PARAMS=()
local CHROOT_DIR=
local BACKUP_DIR=
local WIPE_SRC=wipe
local RESET_REL=
local DONT_BUMP_REL=false
local NO_BUILD=false
local CHROOT_NAME=
local DISTC_SUPPORT=false
local ONLY_REPACKAGE=false
local EPHEMERAL_DIR=
local BUILD_CONFIG_SCRIPT=
local REAL_PACKAGE_DIR=
local PREVIOUS_REPAIR=()
proc_nw() {
[[ "${WIPE_SRC}" == "no_wipe" ]] && return
WIPE_SRC=no_wipe
MAKEPKG_PARAMS+=("--noextract" "--noprepare" "--skipinteg")
}
while [[ ${#} -gt 0 ]]; do
case "${1}" in
-m) MAKEPKG_PARAMS+=("${2}"); shift;;
-mc) MAKECHROOTPKG_PARAMS+=("${2}"); shift;;
-w)
if [[ "${WIPE_SRC}" != "wipe" ]]; then
>&2 echo -w conflicts other parameter.
exit 1
fi
WIPE_SRC=wipe_all
MAKEPKG_PARAMS+=("-C")
;;
-nw)
if [[ "${WIPE_SRC}" != "wipe" ]]; then
>&2 echo -nw conflicts other parameter.
exit 1
fi
proc_nw
;;
-r)
if [[ "${ONLY_REPACKAGE}" != "false" || "${WIPE_SRC}" != "wipe" && "${BACKUP_DIR}" == "" ]]; then
>&2 echo -r conflicts other parameter.
exit 1
fi
ONLY_REPACKAGE=true
MAKEPKG_PARAMS+=("--repackage")
proc_nw
;;
-bk)
if [[ "${BACKUP_DIR}" != "" ]]; then
>&2 echo BACKUP_DIR dir already passed.
exit 1
fi
BACKUP_DIR="${2}"
shift
;;
-c) CHROOT_DIR="${2}"; MAKECHROOTPKG_PARAMS+=("-r" "${2}"); shift;;
-dbr) DONT_BUMP_REL=true;;
-nb) NO_BUILD=true;;
-rr) RESET_REL="${2}"; shift;;
-cn) CHROOT_NAME="${2}"; shift;;
-dist) DISTC_SUPPORT=true;;
-ed) EPHEMERAL_DIR="${2}"; shift;;
-bcs) BUILD_CONFIG_SCRIPT="${2}"; shift;;
-rpd) REAL_PACKAGE_DIR="${2}"; shift;;
-pr) PREVIOUS_REPAIR+=("${2}"); shift;;
--);;
--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_nondir_param -d REAL_PACKAGE_DIR
error_blank_nondir_param -d EPHEMERAL_DIR
error_blank_nondir_param -d CHROOT_DIR
error_blank_param CHROOT_NAME
if [[ "${WIPE_SRC}" != "no_wipe" && "${DONT_BUMP_REL}" == "false" || "${RESET_REL}" != "" ]]; then
local PKGREL="$(cat PKGBUILD | grep "^pkgrel=[0-9]*$" | cut -d"=" -f2)"
if [[ "$(echo "${PKGREL}" | wc -l)" != "1" ]]; then
>&2 echo Invalid number of pkgrel found, exiting
exit 1
fi
if [[ "${RESET_REL}" != "" ]]; then
sed -i "s/^pkgrel=${PKGREL}$/pkgrel=${RESET_REL}/g" PKGBUILD
exit
fi
sed -i "s/^pkgrel=${PKGREL}$/pkgrel=$(( ${PKGREL} + 1 ))/g" PKGBUILD
fi
local HOMEDIR="$(getent passwd "${SUDO_USER}" | cut -d: -f6)"
[[ -d "${HOMEDIR}/.ccache" ]] && MAKECHROOTPKG_PARAMS+=("-d" "${HOMEDIR}/.ccache:/ccache")
# Remove old packages
rm *.pkg*
rm -rf "${CHROOT_DIR}/root/pkgs/"
local MAKECHROOTPKG_BIN="${EPHEMERAL_DIR}/custom.${CHROOT_NAME}/makechrootpkg"
mkdir -p data
cp "/bin/makechrootpkg" "${MAKECHROOTPKG_BIN}"
chmod +x "${MAKECHROOTPKG_BIN}"
patch -p1 -i"${BUILTINS_DIR}/makepkg_patches/makechrootpkg-use_custom.patch" \
-d"${EPHEMERAL_DIR}/custom.${CHROOT_NAME}" || exit $?
patch -p1 -i"${BUILTINS_DIR}/makepkg_patches/makechrootpkg-force_color.patch" \
-d"${EPHEMERAL_DIR}/custom.${CHROOT_NAME}" || exit $?
#patch -p1 -i"${BUILTINS_DIR}/makepkg_patches/makechrootpkg-add_x.patch" \
# -d"${EPHEMERAL_DIR}/custom.${CHROOT_NAME}"
if [[ "${BACKUP_DIR}" != "" ]]; then
MAKECHROOTPKG_PARAMS+=("-d" "${BACKUP_DIR}:/build")
if [[ "${WIPE_SRC}" == "wipe" || "${WIPE_SRC}" == "wipe_all" ]]; then
rm -rf "${BACKUP_DIR}"
fi
mkdir -p "${BACKUP_DIR}"
chown "${SUDO_USER}:${SUDO_USER}" "${BACKUP_DIR}"
patch -p1 -i"${BUILTINS_DIR}/makepkg_patches/makechrootpkg-no_wipe_build.patch" \
-d"${EPHEMERAL_DIR}/custom.${CHROOT_NAME}" || exit $?
if array_contains_element "-c" "${MAKEPKG_PARAMS[@]}"; then
>&2 echo "-c will wipe backup dir after creation."
exit 1
fi
fi
for CHROOT_DIR2 in "${CHROOT_DIR}"/*; do
[[ ! -d "${CHROOT_DIR2}" ]] && continue
# We got to make a copy of makepkg... make it use pump
rm "${CHROOT_DIR2}/bin/makepkg.gabs-inner"
cp "${CHROOT_DIR2}/bin/makepkg"{.backup,}
cp "${CHROOT_DIR2}/bin/makepkg"{,.backup}
patch -p1 -i"${BUILTINS_DIR}/makepkg_patches/makepkg-force_color.patch" \
-d"${CHROOT_DIR2}/bin" || exit $?
patch -p1 -i"${BUILTINS_DIR}/makepkg_patches/makepkg-no_package.patch" \
-d"${CHROOT_DIR2}/bin" || exit $?
patch -p1 -i"${BUILTINS_DIR}/makepkg_patches/makepkg-buildenv_during_prepare.patch" \
-d"${CHROOT_DIR2}/bin" || exit $?
if [[ "${DISTC_SUPPORT}" == "true" ]]; then
mv "${CHROOT_DIR2}/bin/makepkg"{,.gabs-inner}
printf '#!/bin/zsh -x\npump /bin/makepkg.gabs-inner "$@"\n' \
> "${CHROOT_DIR2}/bin/makepkg"
chmod +x "${CHROOT_DIR2}/bin/makepkg"
fi
done
rm *.pkg.(tar|tar.gz|tar.xz|tar.bz2)
md5sum_files() {
md5sum "${@}" | cut -d" " -f1 | sort -u | wc -l
}
# Then actually build it
if [[ "${NO_BUILD}" != "true" ]]; then
if ! "${MAKECHROOTPKG_BIN}" "${MAKECHROOTPKG_PARAMS[@]}" \
-- CCACHE_DIR=/ccache "${MAKEPKG_PARAMS[@]}" \
2> >(tee data/makechrootpkgerrors 1>&2); then
local LINE_COUNT="$(( \
$(md5sum_files \
=(cat "data/makechrootpkgerrors" | tail -n2) \
"${BUILTINS_DIR}/makepkg_errors/updpkgsums") \
+ $(md5sum_files \
=(cat "data/makechrootpkgerrors" | tail -n2 | sed "s/([a-z0-9]*)/(HASH)/") \
"${BUILTINS_DIR}/makepkg_errors/updpkgsums2") \
))"
if [[ "${LINE_COUNT}" != "4" ]] \
&& ! array_contains_element "updpkgsums" "${PREVIOUS_REPAIR[@]}"; then
sudo -u "${SUDO_USER}" updpkgsums || exit $?
cp PKGBUILD "${REAL_PACKAGE_DIR}/PKGBUILD"
exec "${SCRIPTS_DIR}/gabs-build-handler" "${STARTING_PARAMS[@]}" \
-pr updpkgsums
else
exit $?
fi
fi
fi
[[ "${NO_BUILD}" == "true" ]] && exit
if array_contains_element "--noarchive" "${MAKEPKG_PARAMS[@]}" \
|| array_contains_element "--nopackage" "${MAKEPKG_PARAMS[@]}"; then
exit
fi
if [[ "$(ls *.pkg.(tar|tar.gz|tar.xz|tar.bz2) | wc -l)" == "0" ]]; then
>&2 echo No packages generated.
exit 1
fi
# Install the pkg everywhere.
for CHROOT_DIR2 in "${CHROOT_DIR}"/*; do
[[ ! -d "${CHROOT_DIR2}" ]] && continue
mkdir -p "${CHROOT_DIR2}/pkgs/"
rm "${CHROOT_DIR2}/pkgs/"*
cp *.pkg.(tar|tar.gz|tar.xz|tar.bz2) "${CHROOT_DIR2}/pkgs/"
nspawn "${CHROOT_DIR2}" /bin/bash -xc "yes | pacman -U /pkgs/* || exit \$?" || exit $?
rm "${CHROOT_DIR2}/pkgs/"*
done
# Move the old packages out
local DATA_DIR="$(pwd)/data"
if [[ -f "data/packages" && "$(cat "data/packages" | wc -l)" != "0" ]]; then
pushd "${EPHEMERAL_DIR}/pkgs.${CHROOT_NAME}"
cat "${DATA_DIR}/packages" | xargs -I% bash -c "mv \"%\" \"${EPHEMERAL_DIR}/pkgs.old.${CHROOT_NAME}\""
popd
fi
# Update the package list.
ls *.pkg.(tar|tar.gz|tar.xz|tar.bz2) > "data/packages"
# Add it with the rest of the packages
cat "data/packages" | xargs -I% bash -c "mv \"%\" \"${EPHEMERAL_DIR}/pkgs.${CHROOT_NAME}\""