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