]> git.pld-linux.org Git - projects/cleanbuild.git/blob - cleanbuild
- inverted REBUILDDB condition (not it is called NOREBUILDDB)
[projects/cleanbuild.git] / cleanbuild
1 #!/usr/bin/sudo /bin/sh
2
3 RPMS_FROM="$HOME/rpm/cleanRPMS.repo"
4 DEST="th"
5 SRC="-n th-x86_64-test"
6 SUFFIX=""
7 CACHEDIR="$PWD/poldekcache"
8 RPMMACROS=""
9 BUILDERRC=""
10 IGNORE=""
11 ignore() { IGNORE="$IGNORE $*"; }
12 NODEBUG=true
13 CLEANAFTER=false
14 FORCE_UMOUNT=false
15 NOREBUILDDB=true
16 # Whatever you set here as value, consider that it may not be shorter than
17 # /usr/lib/debug (or /usr/src/debug) for debuginfo to work.
18 # You get "Only dest dir longer than base dir not supported" error otherwise.
19 BUILDDIR=/usr/src/BUILD
20
21 [ -r .cleanbuildrc ] && . ./.cleanbuildrc
22
23 [ -z "$USER" ] && echo "USER not defined" && exit 1
24 [ "$USER" == "root" ] && echo "USER must not be root" && exit 1
25
26 export LC_ALL=C
27 unset LANGUAGE
28 unset LANG
29
30 usage() {
31         [ $# -gt 0 ] && echo "$*"
32         echo "Usage:"
33         echo "  ./cleanbuild [cleanbuild options] specname [builder options]"
34         echo "  ./build [cleanbuild options] specname [builder options]"
35         echo "  ./clean [cleanbuild options]"
36         echo "  ./create [cleanbuild options]"
37         echo "  ./install [cleanbuild options] packages"
38         echo ""
39         echo "cleanbuild options:"
40         echo " -32, -64, -th-i486  - select architecture"
41         echo " --cleanafter | -ca  - clean after build"
42         echo " --forceumount | -fu - force umount tmpfs"
43         echo " --debug             - enable debug"
44         echo " -a, -b, -c, -d, -e  - select alternative chroot directory"
45         exit 1
46 }
47
48 FETCH=false
49 CLEAN=false
50 CREATE=false
51 BUILD=false
52 INSTALL=false
53
54 case "$0" in
55         *clean)
56                 CLEAN=exit_after
57                 ;;
58         *cleanbuild)
59                 FETCH=true
60                 CLEAN=true
61                 CREATE=true
62                 BUILD=true
63                 ;;
64         *build)
65                 CREATE=true
66                 BUILD=true
67                 ;;
68         *create)
69                 CLEAN=true
70                 CREATE=exit_after
71                 ;;
72         *install)
73                 CREATE=true
74                 INSTALL=exit_after
75                 ;;
76         *)
77                 usage
78                 ;;
79 esac
80
81 while [ $# -gt 0 ]; do
82         OPT="${1}"
83         case "$OPT" in
84                 -32)    OPT="-th-i686" ;;
85                 -64)    OPT="-th-x86_64" ;;
86                 -th-32) OPT="-th-i686" ;;
87                 -th-64) OPT="-th-x86_64" ;;
88                 -ti-32) OPT="-ti-i686" ;;
89                 -ti-64) OPT="-ti-x86_64" ;;
90                 -ac)    OPT="-ac-amd64" ;;
91                 -ac-32) OPT="-ac-i586" ;;
92                 -ac-64) OPT="-ac-amd64" ;;
93         esac
94
95         V="${OPT#-}"
96         case "$OPT" in
97                 -th-i[46]86 | -th-x86_64)
98                         DEST="$V"
99                         SRC="-n $V-ready"
100                         ;;
101                 -th-i[46]86-test | -th-x86_64-test)
102                         DEST="$V"
103                         SRC="-n $V"
104                         ;;
105                 -ti-i[56]86 | -ti-x86_64)
106                         DEST="$V"
107                         SRC="-n $V-ready"
108                         ;;
109                 -ti-i[56]86-test | -ti-x86_64-test)
110                         DEST="$V"
111                         SRC="-n $V"
112                         ;;
113                 -ac-amd64 | -ac-i[356]86 | -ac-athlon)
114                         DEST="$V"
115                         SRC="-n $V-ready"
116                         ;;
117                 --cleanafter | -ca)
118                         CLEANAFTER=true
119                         ;;
120                 --debug)
121                         NODEBUG=false
122                         ;;
123                 --forceumount | -fu)
124                         FORCE_UMOUNT=true
125                         ;;
126                 -[a-e])
127                         SUFFIX="$OPT"
128                         ;;
129                 -~*)
130                         SUFFIX="$V"
131                         ;;
132                 *)
133                         break
134                         ;;
135         esac
136         shift
137 done
138
139 if $BUILD; then
140         [ $# -ne 0 ] || usage
141         build_pkg="${1}"
142         shift
143
144         builder_options="$*"
145 fi
146
147 $NODEBUG || set -x
148
149 CHNAME="chroot-$DEST$SUFFIX"
150 CHDIR="$PWD/$CHNAME"
151 CHHOME="/home/users/$USER"
152
153 warn()
154 {
155         echo -n -e "\033[31;1m" >&2
156         echo -n "$*" >&2
157         echo -e "\033[0m" >&2
158 }
159
160 die()
161 {
162         code=$1
163         shift
164         warn "$*"
165         exit $code
166 }
167
168 info()
169 {
170         echo -n -e "\033[32m"
171         echo -n "$*"
172         echo -e "\033[0m"
173 }
174
175 title()
176 {
177         [ -t 1 ] || return 0
178         local msg="$CHNAME: $build_pkg: $*"
179         case "$TERM" in
180                 cygwin|xterm*)
181                         echo -ne "\033]1;$msg\007\033]2;$msg\007" >&2
182                         ;;
183                 screen*)
184                         echo -ne "\033]0;$msg\007" >&2
185                         ;;
186         esac
187         return 0
188 }
189
190 exit_after()
191 {
192         return 0;
193 }
194
195 check_running()
196 {
197         [ -r "$CHDIR/.pid" ] || return
198         PID=$(< "$CHDIR/.pid")
199         if [ -d /proc/$PID ]; then
200                 die 10 "Another process ($PID) already running in $CHNAME"
201         fi
202 }
203
204 for D in installed buildlogs $CACHEDIR; do
205         if [ ! -d "$D" ]; then
206                 info "mkdir $D"
207                 su $USER -c "mkdir -p $D" || die 13 "Cannot create work directories"
208         fi
209 done
210
211 ignore \
212         vserver-packages \
213         upstart\* \
214         xorg-driver-video-fglrx\* xorg-driver-video-nvidia\* xorg-xserver-xgl-libGL \
215         xorg-data-xbitmaps \
216         compat-gcc\* \
217         libpng1\* \
218         freetype1-devel-* \
219         anacron fcron hc-cron \
220         masqmail msmtp-sendmail omta postfix sendmail ssmtp nail-mail nullmailer \
221         ghostscript-esp \
222         \*-multilib-\* \
223         gnome-speech-driver-festival gnome-speech-driver-speech-dispatcher
224
225
226 rebuilddb()
227 {
228         $NOREBUILDDB || rpm --root=$CHDIR --rebuilddb
229 }
230
231 poldek()
232 {
233         $NODEBUG || set -x
234         rebuilddb
235         /usr/bin/poldek $SRC -s "$RPMS_FROM" -r "$CHDIR" "--cachedir=$CACHEDIR" --conf=$PWD/poldekconf/poldek.conf "$@"
236 }
237
238
239 build_umount()
240 {
241         for DIR in $CHHOME/rpm $CHHOME dev proc sys; do
242                 [ -d $CHDIR/$DIR ] && umount $CHDIR/$DIR
243         done
244 }
245
246 build_remove_root()
247 {
248         $NODEBUG || set -x
249         if $FORCE_UMOUNT; then
250                 # safety checks.
251                 [ "$CHDIR" ] || exit 1
252                 [ -d "$CHDIR" ] || exit 1
253                 rm -rf $CHDIR/*
254                 umount -l $CHDIR
255         else
256                 umount $CHDIR
257         fi
258         rmdir $CHDIR
259 }
260
261 clean()
262 {
263         info "Cleaning $CHNAME"
264         title "cleaning chroot"
265         build_umount
266         build_remove_root
267 }
268
269 build_prepare_root()
270 {
271         title "preparing chroot"
272         set -e
273         $NODEBUG || set -x
274         mkdir $CHDIR
275         mount -t tmpfs -o size=8G,relatime /dev/null $CHDIR
276         echo $$ > $CHDIR/.pid
277
278         rpm --root=$CHDIR --initdb 
279         poldek --up || :
280         poldek -O "ignore=$IGNORE" -u rpm-build pwdutils coreutils
281
282         for DIR in dev proc sys; do
283                 # We need to create these directories manually, because they are marked
284                 # as netsharedpath in cleanbuild poldek.conf
285                 mkdir $CHDIR/$DIR
286                 mount -o bind /$DIR $CHDIR/$DIR
287         done
288
289         chroot $CHDIR useradd -m $USER -u$(id $USER -u)
290
291         # replicate files which already belong to $USER
292         # so they will have correct owner and permissions
293         cp -a $CHDIR/$CHHOME/{tmp,rpm}
294         cp -a $CHDIR/$CHHOME/tmp $CHDIR$BUILDDIR
295
296         cp -a $CHDIR/$CHHOME/{.bashrc,.rpmmacros}
297         cat <<-EOM > $CHDIR/$CHHOME/.rpmmacros
298         %_builddir              $BUILDDIR
299         %buildroot              %{_builddir}/%{name}-%{version}-root-%(id -u -n)
300         %_rpmdirname    cleanRPMS
301         %_rpmdir                %{expand:%%global _rpmdir %([ -d %{_topdir}/../%{_rpmdirname} ] && (cd %{_topdir}/../%{_rpmdirname}; pwd) || echo %{_topdir}/%{_rpmdirname})}%_rpmdir
302         %distribution   CleanPLD
303         %_binary_payload        w1.gzdio
304 EOM
305         [ -z "$RPMMACROS" ] || echo "$RPMMACROS" >> $CHDIR/$CHHOME/.rpmmacros
306
307         cp -a $CHDIR/$CHHOME/{.bashrc,.builderrc}
308         cat <<-'EORC' > $CHDIR/$CHHOME/.builderrc
309         TITLECHANGE=no
310 EORC
311         [ -z "$BUILDERRC" ] || echo "$BUILDERRC" >> $CHDIR/$CHHOME/.builderrc
312
313         set +e
314 }
315
316 build_mount_home()
317 {
318         $NODEBUG || set -x
319         mount -o bind $HOME/rpm $CHDIR/$CHHOME/rpm
320 }
321
322
323 print_installed()
324 {
325         echo=$1
326         if [ -r installed/$build_pkg ]; then
327                 $echo "$(cat installed/$build_pkg | awk '{print $1}' | sort -u \
328                         | awk '{br=br ", " $1} END{gsub(/^, /, "- BR: ", br ); print br}')"
329                 cat installed/$build_pkg
330         fi
331 }
332
333 addlist()
334 {
335         LIST="$1"
336
337         print_installed info
338
339         return
340         echo "*** $build_pkg $(date --rfc-3339=seconds) ***" >> $LIST
341         print_installed echo >> $LIST
342 }
343
344 builddie()
345 {
346         LIST="$1"; shift
347         CODE="$1"; shift
348         MSG="$*"
349
350         rm -f $CHDIR/.pid
351
352         $CLEANAFTER && clean
353         title "failed !"
354
355         addlist "ERROR_$LIST"
356         die $CODE "$MSG"
357 }
358
359 LAST_INSTALL=""
360 poldek_install()
361 {
362         I="$1";
363         # Nothing to install
364         [ -n "$I" ] || return 1
365         # Installing same packets second time
366         [ "$LAST_INSTALL" != "$I" ] || return 1
367         LAST_INSTALL="$I"
368
369         info "Installing" $I
370         poldek -O "ignore=$IGNORE" -u $I | tee $$.poldek_install
371         ret=
372         if grep -q "Preparing...                ##################################################" $$.poldek_install \
373                         && ! grep -q "file .* from install of .* conflicts with file from package" $$.poldek_install
374                 then
375                 info "Poldek:" $I "installed"
376                 ret=0
377         elif grep -q "Nothing to do" $$.poldek_install; then
378                 warn "Poldek:" $I "installed already"
379                 ret=1
380         fi
381         rm $$.poldek_install
382         [ -n "$ret" ] && return $ret
383
384         # try harder
385         poldek -u $I && return 0
386         poldek -u $I && return 0
387         warn "Poldek:" "Could not install" $I
388         return 1
389 }
390
391 maybe_call()
392 {
393         local cond="$1"; shift
394         local func="$1"; shift
395
396         [ $cond == "false" ] && return
397         $func "$@"
398         [ $cond == "exit_after" ] && exit
399 }
400
401 fetch()
402 {
403         info "Fetching $build_pkg"
404         title "fetch"
405         $NODEBUG || set -x
406         su $USER -c "$HOME/rpm/packages/builder -g $build_pkg $builder_options" \
407                 || die 11 "Fetch failed"
408 }
409
410 create()
411 {
412         $NODEBUG || set -x
413         su $USER -c "poldek -s $RPMS_FROM --mkidx"
414
415         if [ ! -d $CHDIR ]; then
416                 info "Preparing $CHNAME"
417                 build_prepare_root
418                 build_mount_home
419         fi
420 }
421
422
423 maybe_call $FETCH fetch
424
425 check_running
426
427 maybe_call $CLEAN clean
428
429 maybe_call $CREATE create
430
431 echo $$ > $CHDIR/.pid
432
433 maybe_call $INSTALL poldek_install "$*"
434
435 $BUILD || exit
436
437 if [ -p /tmp/fixfreq ]; then
438         echo $$ > /tmp/fixfreq
439 fi
440
441 while true; do
442         info "Building $build_pkg in $CHNAME"
443         rebuilddb
444         buildlog="buildlogs/$build_pkg"
445         if [ -r $buildlog ]; then
446                 i=1
447                 while [ -r $buildlog.$i ]; do
448                         i=$((i+1))
449                 done
450                 info "moving $buildlog to $buildlog.$i"
451                 mv $buildlog $buildlog.$i
452         fi
453         ./findunusedbr -c $CHDIR $HOME/rpm/packages/$build_pkg/$build_pkg.spec
454         title "building"
455         { chroot $CHDIR su $USER -c "$CHHOME/rpm/packages/builder -nn -bb $build_pkg $builder_options" < /dev/null 2>&1; echo $? > ecode; } | tee $buildlog
456
457         ECODE=$(< ecode)
458         rm -f ecode
459
460         if grep -q "error: Failed build dependencies:" $buildlog; then
461                 SEARCH=$(cat $buildlog | awk '/^Error:/ { p = 0 }; { if ( p ) { f="p"; if ( $1 ~ /^\// ) f="f"; printf "search -%c %s; ", f, $1; } }; /error: Failed build dependencies:/ { p = 1 }')
462                 INSTALL=$(poldek -O "ignore=$IGNORE" --shcmd="$SEARCH" | awk '{ if ( p ) { print; p = 0; } } / package\(s\) found:$/ { p = 1 }' | sed 's/^\(.*\)-.*-.*$/\1/' | sort -u)
463
464                 if poldek_install "$INSTALL"; then
465                         info "Deps installed"
466                         continue
467                 else
468                         addlist ERROR_BRINSTALL
469                         die 4 "Cannot install BRs"
470                 fi
471         fi
472
473         ./findbr $CHDIR/BUILD $buildlog > $$.installed
474         installed_something=false
475         while read pkg msg; do
476                 if poldek_install $pkg; then
477                         info "findbr:" $pkg "installed"
478                         echo "$pkg $msg" >> installed/$build_pkg
479                         ./addbr $build_pkg "$pkg" "$msg"
480                         installed_something=true
481                 else
482                         warn "findbr:" $pkg "not installed"
483                 fi
484         done < $$.installed
485         rm -f $$.installed
486         $installed_something && continue
487
488         if [ $ECODE -eq 0 ]; then
489                 $CLEANAFTER && clean
490                 addlist BUILT_OK
491                 ./findunusedbr $CHDIR $HOME/rpm/packages/$build_pkg/$build_pkg.spec
492                 info "$build_pkg built OK !"
493                 title "OK !"
494                 exit 0
495         else
496                 builddie UNKNOWN 1 "Got error but dunno what to do !"
497         fi
498 done
499
500
501 # vim: ts=4 sw=4 filetype=sh
This page took 0.088306 seconds and 4 git commands to generate.