blob: d0f97473405a36be3c712ade2520aca210be9f99 (
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
|
#!/bin/sh
set -eu
PROGRAM=${0##*/}
# defaults
: ${PACKAGE_NAME=''}
: ${NETWORKING=false}
: ${TRACING=false}
: ${WITH=}
: ${WITHOUT=}
: ${KEEP_CONTAINER=false}
: ${TMPFS="4G"}
dir=$(pwd)
image=registry.gitlab.com/pld-linux/cleanbuild
topdir=$dir/rpm
home=/home/builder
die() {
echo >&2 "$0: $*"
exit 1
}
is_no() {
# Test syntax
if [ $# = 0 ]; then
return 2
fi
case "$1" in
no|No|NO|false|False|FALSE|off|Off|OFF|N|n|0)
# true returns zero
return 0
;;
*)
# false returns one
return 1
;;
esac
}
tmpfs() {
if is_no "${TMPFS:-true}" || [ "$TMPFS" = "0" ]; then
return
fi
echo "--tmpfs $home/rpm/BUILD:rw,exec,nosuid,size=$TMPFS"
}
create_container() {
# cleanup first
docker kill $name >/dev/null 2>&1 || :
docker rm $name >/dev/null 2>&1 || :
install -d $topdir/logs
# start the container
TMPFS_SIZE=$TMPFS \
PACKAGE_NAME=$PACKAGE_NAME \
docker-compose run --rm -d \
--name=$name \
--label=cleanbuild=$PACKAGE_NAME \
cleanbuild
docker exec --user=root -w / $name usermod -d $home builder
# these paths need to be accessible for builder
docker exec --user=root -w / $name sh -c "cd $home && chown builder:builder rpm rpm/logs rpm/BUILD .ccache"
if [ ! -d $topdir/rpm-build-tools ]; then
docker exec $name builder --init-rpm-dir
fi
}
package_prepare() {
# fetch sources and install deps
if [ -d $topdir/packages/$PACKAGE_NAME ]; then
# chown, as it might be different owner (root) modified outside container
docker exec --user=root -w / $name chown -R builder:builder $home/rpm/packages/$PACKAGE_NAME
fi
docker exec $name builder -g $PACKAGE_NAME
# prevent network access like pld builders do
$NETWORKING || docker exec --user=root -w / $name setfacl -m u:builder:--- /etc/resolv.conf
git_tag=$(GIT_DIR=$topdir/packages/$PACKAGE_NAME/.git git describe --tags --always)
buildlog=rpm/logs/${git_tag#auto/*/}.log
}
package_build() {
# create default args for builder
set -- -nn ${WITH:+--with "${WITH# }"} ${WITHOUT:+--without "${WITHOUT# }"} "$PACKAGE_NAME"
while true; do
# install deps
docker exec $name builder -g -R "$@"
# remove .la dependencies
docker exec --user=root -w / $name $home/cleanbuild/bin/cleanup-la
# reset findunusedbr state after deps install
docker exec --user=root -w / $name $home/cleanbuild/bin/findunusedbr -c / $home/rpm/packages/$PACKAGE_NAME/$PACKAGE_NAME.spec
# actual build
docker exec $name cleanbuild/bin/teeboth $buildlog builder -bb --define '__spec_clean_body %{nil}' "$@" && rc=$? || rc=$?
findbr=$PACKAGE_NAME.findbr.log
builddir=$(docker exec $name sh -c 'test ! -d rpm/BUILD/* || echo rpm/BUILD/*')
if [ -z "$builddir" ]; then
echo >&2 "No build dir. Build failed?"
exit 6
fi
# need root to run poldek
docker exec --user=root -w / $name sh -c "cd $home && cleanbuild/bin/findbr $builddir $buildlog" > $findbr
installed_something=false
while read pkg msg; do
bin/addbr rpm/packages/$PACKAGE_NAME/$PACKAGE_NAME.spec "$pkg" "$msg" || continue
installed_something=true
done < $findbr
rm -f $findbr
# go for another try
$installed_something && continue
docker exec --user=root -w / $name $home/cleanbuild/bin/findunusedbr -c / $home/rpm/packages/$PACKAGE_NAME/$PACKAGE_NAME.spec
if [ $rc -eq 0 ] && ! $KEEP_CONTAINER; then
# finished ok, cleanup
docker kill $name >/dev/null && docker rm $name >/dev/null || :
fi
# propagate error
exit $rc
done
}
parse_options() {
local t
t=$(getopt -o 'x' --long 'network,no-tmpfs,notmpfs,tmpfs:,keep-container,with:,without:' -n "$PROGRAM" -- "$@")
[ $? != 0 ] && exit $?
eval set -- "$t"
while :; do
case "$1" in
-x)
TRACING=true
;;
--network)
NETWORKING=true
;;
--no-tmpfs|--notmpfs)
TMPFS=false
;;
--tmpfs)
shift
TMPFS="$1"
;;
--keep-container)
KEEP_CONTAINER=true
;;
--with)
shift
WITH="$WITH $1"
;;
--without)
shift
WITHOUT="$WITHOUT $1"
;;
--)
shift
break
;;
*)
die "Internal error: [$1] not recognized!"
;;
esac
shift
done
test "$#" -eq 1 || die "package not specified or excess arguments"
PACKAGE_NAME="${1%.spec}"
}
main() {
parse_options "$@"
$TRACING && set -x
local name="cleanbuild-$PACKAGE_NAME"
create_container
package_prepare
package_build
}
main "$@"
|