]>
Commit | Line | Data |
---|---|---|
ce96f77c | 1 | #!/bin/sh |
e2ea04e5 | 2 | set -eu |
ce96f77c | 3 | |
e2ea04e5 | 4 | PROGRAM=${0##*/} |
ce96f77c | 5 | |
e2ea04e5 ER |
6 | # defaults |
7 | : ${PACKAGE_NAME=''} | |
3ac42f8d | 8 | : ${NETWORKING=false} |
5d14e3f9 | 9 | : ${TRACING=false} |
60584f5d ER |
10 | : ${WITH=} |
11 | : ${WITHOUT=} | |
ad5ed75b | 12 | : ${KEEP_CONTAINER=false} |
2da31caa | 13 | : ${TMPFS="4G"} |
ce96f77c | 14 | |
ce96f77c | 15 | dir=$(pwd) |
47ec7a26 | 16 | image=registry.gitlab.com/pld-linux/cleanbuild |
ce96f77c | 17 | topdir=$dir/rpm |
8f52cf50 | 18 | home=/home/builder |
73d3710b | 19 | |
e2ea04e5 ER |
20 | die() { |
21 | echo >&2 "$0: $*" | |
22 | exit 1 | |
23 | } | |
24 | ||
2da31caa ER |
25 | is_no() { |
26 | # Test syntax | |
27 | if [ $# = 0 ]; then | |
28 | return 2 | |
29 | fi | |
30 | ||
31 | case "$1" in | |
32 | no|No|NO|false|False|FALSE|off|Off|OFF|N|n|0) | |
33 | # true returns zero | |
34 | return 0 | |
35 | ;; | |
36 | *) | |
37 | # false returns one | |
38 | return 1 | |
39 | ;; | |
40 | esac | |
41 | } | |
42 | ||
43 | tmpfs() { | |
8e5e409e | 44 | if is_no "${TMPFS:-true}" || [ "$TMPFS" = "0" ]; then |
2da31caa ER |
45 | return |
46 | fi | |
47 | ||
48 | echo "--tmpfs $home/rpm/BUILD:rw,exec,nosuid,size=$TMPFS" | |
49 | } | |
50 | ||
e2ea04e5 ER |
51 | create_container() { |
52 | # cleanup first | |
53 | docker kill $name >/dev/null 2>&1 || : | |
54 | docker rm $name >/dev/null 2>&1 || : | |
ce96f77c | 55 | |
e2ea04e5 | 56 | install -d $topdir/logs |
ce96f77c | 57 | |
e2ea04e5 ER |
58 | # start the container |
59 | docker run --name=$name -d \ | |
60 | -w $home \ | |
61 | -v $topdir:$home/rpm \ | |
62 | -v $dir:$home/cleanbuild \ | |
63 | -v $dir/cache/poldek:/var/cache/poldek \ | |
64 | -v $dir/cache/ccache/$PACKAGE_NAME:$home/.ccache \ | |
3bf22c6f | 65 | --label=cleanbuild=$name \ |
2da31caa | 66 | $(tmpfs) \ |
60584f5d | 67 | $image >/dev/null |
ce96f77c | 68 | |
e2ea04e5 ER |
69 | # set the homedir |
70 | docker exec --user=root -w / $name usermod -d $home builder | |
621943d5 | 71 | |
e2ea04e5 ER |
72 | # these paths need to be accessible for builder |
73 | docker exec --user=root -w / $name sh -c "cd $home && chown builder:builder rpm rpm/logs rpm/BUILD .ccache" | |
74 | ||
75 | if [ ! -d $topdir/rpm-build-tools ]; then | |
76 | docker exec $name builder --init-rpm-dir | |
ce96f77c | 77 | fi |
e2ea04e5 | 78 | } |
ce96f77c | 79 | |
e2ea04e5 ER |
80 | package_prepare() { |
81 | # fetch sources and install deps | |
82 | if [ -d $topdir/packages/$PACKAGE_NAME ]; then | |
83 | # chown, as it might be different owner (root) modified outside container | |
84 | docker exec --user=root -w / $name chown -R builder:builder $home/rpm/packages/$PACKAGE_NAME | |
85 | fi | |
86 | docker exec $name builder -g $PACKAGE_NAME | |
87 | ||
88 | # prevent network access like pld builders do | |
3ac42f8d | 89 | $NETWORKING || docker exec --user=root -w / $name setfacl -m u:builder:--- /etc/resolv.conf |
e2ea04e5 ER |
90 | |
91 | git_tag=$(GIT_DIR=$topdir/packages/$PACKAGE_NAME/.git git describe --tags --always) | |
92 | buildlog=rpm/logs/${git_tag#auto/*/}.log | |
93 | } | |
94 | ||
95 | package_build() { | |
60584f5d ER |
96 | # create default args for builder |
97 | set -- -nn ${WITH:+--with "${WITH# }"} ${WITHOUT:+--without "${WITHOUT# }"} "$PACKAGE_NAME" | |
98 | ||
e2ea04e5 ER |
99 | while true; do |
100 | # install deps | |
60584f5d | 101 | docker exec $name builder -g -R "$@" |
e2ea04e5 | 102 | # remove .la dependencies |
60ca295b | 103 | docker exec --user=root -w / $name $home/cleanbuild/bin/cleanup-la |
e2ea04e5 | 104 | # reset findunusedbr state after deps install |
60ca295b | 105 | docker exec --user=root -w / $name $home/cleanbuild/bin/findunusedbr -c / $home/rpm/packages/$PACKAGE_NAME/$PACKAGE_NAME.spec |
e2ea04e5 ER |
106 | |
107 | # actual build | |
60ca295b | 108 | docker exec $name cleanbuild/bin/teeboth $buildlog builder -bb --define '__spec_clean_body %{nil}' "$@" && rc=$? || rc=$? |
e2ea04e5 ER |
109 | |
110 | findbr=$PACKAGE_NAME.findbr.log | |
111 | builddir=$(docker exec $name sh -c 'test ! -d rpm/BUILD/* || echo rpm/BUILD/*') | |
112 | if [ -z "$builddir" ]; then | |
113 | echo >&2 "No build dir. Build failed?" | |
114 | exit 6 | |
115 | fi | |
116 | # need root to run poldek | |
60ca295b | 117 | docker exec --user=root -w / $name sh -c "cd $home && cleanbuild/bin/findbr $builddir $buildlog" > $findbr |
e2ea04e5 ER |
118 | |
119 | installed_something=false | |
120 | while read pkg msg; do | |
03501aec | 121 | bin/addbr rpm/packages/$PACKAGE_NAME/$PACKAGE_NAME.spec "$pkg" "$msg" || continue |
e2ea04e5 ER |
122 | installed_something=true |
123 | done < $findbr | |
124 | rm -f $findbr | |
125 | ||
126 | # go for another try | |
127 | $installed_something && continue | |
128 | ||
60ca295b | 129 | docker exec --user=root -w / $name $home/cleanbuild/bin/findunusedbr -c / $home/rpm/packages/$PACKAGE_NAME/$PACKAGE_NAME.spec |
e2ea04e5 | 130 | |
ad5ed75b | 131 | if [ $rc -eq 0 ] && ! $KEEP_CONTAINER; then |
e2ea04e5 ER |
132 | # finished ok, cleanup |
133 | docker kill $name >/dev/null && docker rm $name >/dev/null || : | |
134 | fi | |
135 | ||
136 | # propagate error | |
137 | exit $rc | |
138 | done | |
139 | } | |
140 | ||
141 | parse_options() { | |
142 | local t | |
60584f5d | 143 | t=$(getopt -o 'x' --long 'network,no-tmpfs,notmpfs,tmpfs:,keep-container,with:,without:' -n "$PROGRAM" -- "$@") |
e2ea04e5 ER |
144 | [ $? != 0 ] && exit $? |
145 | eval set -- "$t" | |
146 | ||
147 | while :; do | |
148 | case "$1" in | |
5d14e3f9 ER |
149 | -x) |
150 | TRACING=true | |
151 | ;; | |
3ac42f8d ER |
152 | --network) |
153 | NETWORKING=true | |
154 | ;; | |
8447721f ER |
155 | --no-tmpfs|--notmpfs) |
156 | TMPFS=false | |
157 | ;; | |
2da31caa ER |
158 | --tmpfs) |
159 | shift | |
160 | TMPFS="$1" | |
161 | ;; | |
ad5ed75b ER |
162 | --keep-container) |
163 | KEEP_CONTAINER=true | |
164 | ;; | |
60584f5d ER |
165 | --with) |
166 | shift | |
167 | WITH="$WITH $1" | |
168 | ;; | |
169 | --without) | |
170 | shift | |
171 | WITHOUT="$WITHOUT $1" | |
172 | ;; | |
e2ea04e5 ER |
173 | --) |
174 | shift | |
175 | break | |
176 | ;; | |
177 | *) | |
178 | die "Internal error: [$1] not recognized!" | |
179 | ;; | |
180 | esac | |
181 | shift | |
182 | done | |
183 | ||
184 | test "$#" -eq 1 || die "package not specified or excess arguments" | |
185 | PACKAGE_NAME="${1%.spec}" | |
186 | } | |
187 | ||
188 | main() { | |
189 | parse_options "$@" | |
190 | ||
5d14e3f9 | 191 | $TRACING && set -x |
e2ea04e5 | 192 | local name="cleanbuild-$PACKAGE_NAME" |
e2ea04e5 ER |
193 | create_container |
194 | package_prepare | |
195 | package_build | |
196 | } | |
197 | ||
198 | main "$@" |