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