cookutils view cook @ rev 11

More log and all the same
author Christophe Lincoln <pankso@slitaz.org>
date Wed May 04 23:23:51 2011 +0200 (2011-05-04)
parents b543222cb3e0
children fa9a36a8c1f4
line source
1 #!/bin/sh
2 #
3 # Cook - A tool to cook and generate SliTaz packages. Read the README
4 # before adding or modifing any code in cook!
5 #
6 # Copyright (C) SliTaz GNU/Linux - GNU gpl v3
7 # Author: Christophe Lincoln <pankso@slitaz.org>
8 #
10 [ -f "/etc/slitaz/cook.conf" ] && . /etc/slitaz/cook.conf
11 [ -f "cook.conf" ] && . ./cook.conf
13 # Share activity and broken packages with the Cooker.
14 activity="$CACHE/activity"
15 broken="$CACHE/broken"
17 #
18 # Functions
19 #
21 usage() {
22 cat << EOT
24 $(echo -e "\033[1m$(gettext "Usage:")\033[0m") cook [package|command|list] [--option]
26 $(echo -e "\033[1m$(gettext "Commands:")\033[0m")
27 usage|help $(gettext "Display this short usage.")
28 list-wok $(gettext "List packages in the wok.")
29 setup $(gettext "Setup your build environment.")
30 test $(gettext "Test environment and cook a package.")
31 new $(gettext "Create a new package with receipt".)
32 list $(gettext "Cook a list of packages.")
33 clean-wok $(gettext "Clean-up all packages files.")
34 clean-src $(gettext "Clean-up all packages source.")
35 pkglist $(gettext "Create all packages.* lists.")
37 $(echo -e "\033[1m$(gettext "Options:")\033[0m")
38 --clean|-c Cook : $(gettext "clean the package in the wok.")
39 --install|-i Cook : $(gettext "and install the package.")
40 --wok|-w Setup: $(gettext "create also a wok from Hg repo.")
42 EOT
43 exit 0
44 }
46 # Be sure we root.
47 check_root() {
48 [ $(id -u) != 0 ] && gettext -e "\nYou must be root to cook.\n\n" && exit 0
49 }
51 separator() {
52 echo "================================================================================"
53 }
55 status() {
56 echo -en "\\033[70G[ "
57 if [ $? = 0 ]; then
58 echo -en "\\033[1;32mOK"
59 else
60 echo -en "\\033[1;31mFailed"
61 fi
62 echo -e "\\033[0;39m ]"
63 }
65 # Log activities.
66 log() {
67 grep ^[a-zA-Z0-9] | \
68 sed s"#^[A-Z]\([^']*\)#$(date '+%Y-%m-%d %H:%M') : \0#" >> $activity
69 }
71 # We dont want those escape in web interface.
72 clean_log() {
73 sed -i -e s'|\[70G\[ \[1;32m| |' \
74 -e s'|\[0;39m \]||' $LOGS/$pkg.log
75 }
77 # Be sure package exist in wok.
78 check_pkg_in_wok() {
79 if [ ! -d "$WOK/$pkg" ]; then
80 gettext -e "\nUnable to find package in the wok:"
81 echo -e " $pkg\n" && exit 1
82 fi
83 }
85 if_empty_value() {
86 if [ -z "$value" ]; then
87 gettext "QA: empty variable:"; echo -e " ${var}=\"\"\n"
88 exit 1
89 fi
90 }
92 # QA: check a receip consistency befor building.
93 receipt_quality() {
94 gettext -e "QA: checking package receipt...\n"
95 unset online
96 if ifconfig | grep -q -A 1 "^[a-z]*[0-9]" | fgrep 'addr:'; then
97 online="online"
98 fi
99 for var in PACKAGE VERSION CATEGORY SHORT_DESC MAINTAINER WEB_SITE
100 do
101 unset value
102 value=$(grep ^$var= receipt | cut -d \" -f 2)
103 case "$var" in
104 PACKAGE|VERSION|SHORT_DESC)
105 if_empty_value ;;
106 CATEGORY)
107 [ -z "$value" ] && value="empty"
108 valid="base-system x-window utilities network graphics \
109 multimedia office development system-tools security games \
110 misc meta non-free"
111 if ! echo "$valid" | grep -q -w "$value"; then
112 gettext "QA: unknow category:"; echo -e " $value\n"
113 exit 1
114 fi ;;
115 WEB_SITE)
116 # We dont check WGET_URL since if dl is needed it will fail.
117 # Break also if we not online. Here error is not fatal.
118 if_empty_value
119 [ -z "$online" ] || break
120 if ! busybox wget -s $value 2>/dev/null; then
121 gettext "QA: Unable to reach:"; echo -e " $value\n"
122 fi ;;
123 esac
124 done
125 }
127 # Executed before sourcing a receipt.
128 unset_receipt() {
129 unset DEPENDS BUILD_DEPENDS WANTED EXTRAVERSION WGET_URL PROVIDE TARBALL
130 }
132 # Path's used in receipt and by cook itself.
133 set_paths() {
134 pkgdir=$WOK/$PACKAGE
135 src=$pkgdir/source/$PACKAGE-$VERSION
136 pack=$pkgdir/taz/$PACKAGE-${VERSION}${EXTRAVERSION}
137 fs=$pack/fs
138 stuff=$pkgdir/stuff
139 install=$pkgdir/install
140 if [ "$WANTED" ]; then
141 src=$WOK/$WANTED/source/$WANTED-$VERSION
142 install=$WOK/$WANTED/install
143 fi
144 # Old way compatibility.
145 _pkg=$install
146 }
148 # Get package source.
149 get_source() {
150 case "$WGET_URL" in
151 http://*|ftp://*)
152 # Busybox Wget is better!
153 busybox wget -c -P $SRC $WGET_URL ;;
154 hg*|mercurial*)
155 # We are in cache so clone here and create a tarball
156 pwd=$(pwd)
157 if $(echo "$WGET_URL" | fgrep -q hg); then
158 url=${WGET_URL#hg|}
159 else
160 url=${WGET_URL#mercurial|}
161 fi
162 pkgsrc=${SOURCE:-$PACKAGE}-$VERSION
163 tarball=$pkgsrc.tar.bz2
164 gettext "Getting source from Hg: "; echo $url
165 gettext "Cloning to: "; echo "$pwd/$pkgsrc"
166 hg clone $url $pkgsrc || exit 1
167 gettext "Creating tarball: "; echo "$tarball"
168 tar cjf $tarball $pkgsrc || exit 1
169 mv $tarball $SRC && rm -rf $pkgsrc ;;
170 git*)
171 echo "TODO: git implementation in cook" && exit 1 ;;
172 svn*)
173 echo "TODO: svn implementation in cook" && exit 1 ;;
174 *)
175 gettext -e "\nERROR: Unable to handle:"; echo -e " $WGET_URL \n" | \
176 tee -a $LOGS/$PACKAGE.log
177 exit 1 ;;
178 esac
179 }
181 # Extract source package.
182 extract_source() {
183 gettext "Extracting:"; echo " $TARBALL"
184 case "$TARBALL" in
185 *.tar.gz|*.tgz) tar xzf $SRC/$TARBALL ;;
186 *.tar.bz2) tar xjf $SRC/$TARBALL ;;
187 *.tar.lzma) tar xaf $SRC/$TARBALL ;;
188 *.zip) unzip $SRC/$TARBALL ;;
189 esac
190 }
192 # Display cooked package summary.
193 summary() {
194 cd $WOK/$pkg
195 [ -d install ] && prod=$(du -sh install | awk '{print $1}' 2>/dev/null)
196 fs=$(du -sh taz/* | awk '{print $1}')
197 size=$(du -sh $PKGS/$PACKAGE-${VERSION}${EXTRAVERSION}.* | awk '{print $1}')
198 files=$(cat taz/$PACKAGE-*/files.list | wc -l)
199 gettext "Summary for:"; echo " $PACKAGE $VERSION"
200 separator
201 [ "$prod" ] && echo "Produce : $prod"
202 cat << EOT
203 Packed : $fs
204 Compressed : $size
205 Cook time : ${time}s
206 Files : $files
207 $(separator)
209 EOT
210 }
212 # Copy all generic files (locale, pixmaps, .desktop). We use standard paths,
213 # so some packages need to copy these files with the receipt and genpkg_rules.
214 copy_generic_files()
215 {
216 # $LOCALE is set in cook.conf
217 if [ "$LOCALE" ]; then
218 if [ -d "$_pkg/usr/share/locale" ]; then
219 mkdir -p $fs/usr/share/locale
220 for i in $LOCALE
221 do
222 if [ -d "$_pkg/usr/share/locale/$i" ]; then
223 cp -a $_pkg/usr/share/locale/$i $fs/usr/share/locale
224 fi
225 done
226 fi
227 fi
229 # Generic pixmaps copy can be disabled with GENERIC_PIXMAPS="no"
230 if [ "$GENERIC_PIXMAPS" != "no" ]; then
231 if [ -d "$_pkg/usr/share/pixmaps" ]; then
232 mkdir -p $fs/usr/share/pixmaps
233 cp -a $_pkg/usr/share/pixmaps/$PACKAGE.png \
234 $fs/usr/share/pixmaps 2>/dev/null
235 cp -a $_pkg/usr/share/pixmaps/$PACKAGE.xpm \
236 $fs/usr/share/pixmaps 2>/dev/null
237 fi
239 # Custom or homemade PNG pixmap can be in stuff.
240 if [ -f "$stuff/$PACKAGE.png" ]; then
241 mkdir -p $fs/usr/share/pixmaps
242 cp -a $stuff/$PACKAGE.png $fs/usr/share/pixmaps
243 fi
244 fi
246 # Desktop entry (.desktop).
247 if [ -d "$_pkg/usr/share/applications" ]; then
248 cp -a $_pkg/usr/share/applications $fs/usr/share
249 fi
251 # Homemade desktop file(s) can be in stuff.
252 if [ -d "$stuff/applications" ]; then
253 mkdir -p $fs/usr/share
254 cp -a $stuff/applications $fs/usr/share
255 fi
256 if [ -f "$stuff/$PACKAGE.desktop" ]; then
257 mkdir -p $fs/usr/share/applications
258 cp -a $stuff/$PACKAGE.desktop $fs/usr/share/applications
259 fi
260 }
262 # Find and strip : --strip-all (-s) or --strip-debug on static libs.
263 strip_package()
264 {
265 gettext "Executing strip on all files"
266 for dir in $fs/bin $fs/sbin $fs/usr/bin $fs/usr/sbin $fs/usr/games
267 do
268 if [ -d "$dir" ]; then
269 find $dir -type f -exec strip -s '{}' 2>/dev/null \;
270 fi
271 done
272 find $fs -name "*.so*" -exec strip -s '{}' 2>/dev/null \;
273 find $fs -name "*.a" -exec strip --strip-debug '{}' 2>/dev/null \;
274 status
275 }
277 # Remove installed deps.
278 remove_deps() {
279 # Now remove installed build deps.
280 diff="$CACHE/installed.diff"
281 deps=$(cat $diff | grep ^+[a-zA-Z0-9] | sed s/^+//)
282 nb=$(cat $diff | grep ^+[a-zA-Z0-9] | wc -l)
283 if [ -s "$CACHE/installed.diff" ]; then
284 gettext "Build dependencies to remove:"; echo " $nb"
285 gettext "Removing:"
286 for dep in $deps
287 do
288 echo -n " $dep"
289 yes | tazpkg remove $dep >/dev/null
290 done
291 echo ""
292 mv -f $CACHE/installed.diff $CACHE/installed.last.diff
293 fi
294 }
296 # The main cook function.
297 cookit() {
298 echo "Cook: $PACKAGE $VERSION"
299 separator
300 set_paths
301 [ "$QA" ] && receipt_quality
302 rm -rf install taz source $CACHE/error
304 # Disable -pipe if less than 512Mb free RAM.
305 free=$(free | fgrep '/+ buffers' | tr -s ' ' | cut -f 4 -d ' ')
306 if [ "$free" -lt 524288 ] && [ "$CFLAGS" != "${CFLAGS/-pipe}" ]; then
307 gettext -e "Disabling -pipe compile flag: $free RAM\n"
308 CFLAGS="${CFLAGS/-pipe}" && CFLAGS=$(echo "$CFLAGS" | tr -s ' ')
309 CXXFLAGS="${CXXFLAGS/-pipe}" && CXXFLAGS=$(echo "$CXXFLAGS" | tr -s ' ')
310 fi
311 unset free
313 # Export flags and path to be used by make
314 DESTDIR=$WOK/$PACKAGE/install
315 export DESTDIR MAKEFLAGS CFLAGS CXXFLAGS BUILD_HOST CONFIG_SITE
316 local LC_ALL=POSIX LANG=POSIX
318 # Check for build dep.
319 cd $INSTALLED && ls -1 > $CACHE/installed.list
320 [ "$DEPENDS" ] && gettext -e "Checking build dependencies...\n"
321 for dep in $BUILD_DEPENDS
322 do
323 if [ ! -d "$INSTALLED/$dep" ]; then
324 # Try local package first
325 if [ -f "$PKGS/$dep-*.tazpkg" ]; then
326 gettext "Installing dep (local):"; echo " $dep"
327 cd $PKGS && tazpkg install $dep-*.tazpkg >/dev/null
328 else
329 gettext "Installing dep (web/cache):"; echo " $dep"
330 tazpkg get-install $dep >/dev/null
331 fi
332 fi
333 done
334 ls -1 > $CACHE/installed.cook && cd $CACHE
336 # If a cook failed deps are not remove since we exit 1.
337 [ ! -s "installed.diff" ] && \
338 diff installed.list installed.cook > installed.diff
339 deps=$(cat installed.diff | grep ^+[a-zA-Z0-9] | wc -l)
341 # Get source tarball and make sure we have source dir named:
342 # $PACKAGE-$VERSION to be standard in receipts. Her we use tar.lzma
343 # tarball if it exist.
344 if [ "$WGET_URL" ] && [ ! -f "$SRC/$TARBALL" ]; then
345 if [ -f "$SRC/${SOURCE:-$PACKAGE}-$VERSION.tar.lzma" ]; then
346 TARBALL=${SOURCE:-$PACKAGE}-$VERSION.tar.lzma
347 else
348 get_source || exit 1
349 fi
350 fi
351 if [ ! "$WANTED" ] && [ "$TARBALL" ] && [ ! -d "$src" ]; then
352 mkdir -p $pkgdir/source/tmp && cd $pkgdir/source/tmp
353 extract_source || exit 1
354 mv * ../$PACKAGE-$VERSION
355 cd .. && rm -rf tmp
356 fi
358 # Execute receipt rules.
359 if grep -q ^compile_rules $pkgdir/receipt; then
360 gettext -e "Executing: compile_rules\n"
361 [ -d "$src" ] && cd $src
362 compile_rules || exit 1
363 # Stay compatible with _pkg
364 [ -d $src/_pkg ] && mv $src/_pkg $install
365 # QA: compile_rules success so valid.
366 mkdir -p $install
367 else
368 # QA: No compile_rules so no error, valid.
369 mkdir -p $install
370 fi
371 if grep -q ^genpkg_rules $pkgdir/receipt; then
372 gettext -e "Executing: genpkg_rules\n"
373 cd $pkgdir
374 mkdir -p $fs && genpkg_rules || ( echo -e \
375 "\nERROR: genpkg_rules failed\n" | \
376 tee -a $LOGS/$pkg.log && exit 1 )
377 gettext -e "Now will pack the package.\n"
378 fi
379 separator && echo ""
380 }
382 # Cook quality assurance.
383 cookit_quality() {
384 if [ ! -d "$WOK/$pkg/install" ] && [ ! "$WANTED" ]; then
385 echo -e "\nERROR: cook failed\n" | tee -a $LOGS/$pkg.log
386 fi
387 # ERROR can be echoed any time in cookit()
388 if grep -q ^ERROR $LOGS/$pkg.log; then
389 exit 1
390 fi
391 }
393 # Create the package.
394 packit() {
395 set_paths
396 echo "Packing: $PACKAGE ${VERSION}${EXTRAVERSION}"
397 separator
398 cd $pkgdir/taz
399 strip_package
400 for file in receipt description.txt
401 do
402 [ ! -f "../$file" ] && continue
403 gettext "Copying"; echo -n " $file..."
404 cp -f ../$file $pack && chown 0.0 $pack/$file && status
405 done
406 copy_generic_files
407 # Use Tazpkg to create a tazpkg package...
408 tazpkg pack $PACKAGE-${VERSION}${EXTRAVERSION} | grep "\[*\]"
409 separator && echo ""
410 }
412 # Verify package quality and consitensy.
413 packit_quality() {
414 if grep -q ^ERROR $LOGS/$pkg.log; then
415 exit 1
416 fi
417 if ! grep -q ^/ $WOK/$pkg/taz/$pkg-*/files.list; then
418 echo -e "ERROR: empty package\n" | tee -a $LOGS/$pkg.log && exit 1
419 else
420 mv -f $WOK/$pkg/taz/$pkg-*.tazpkg $PKGS
421 sed -i /^${pkg}$/d $broken
422 fi
423 }
425 #
426 # Commands
427 #
429 case "$1" in
430 usage|help)
431 usage ;;
432 list-wok)
433 gettext "List of packages in:"; echo " $WOK"
434 separator
435 cd $WOK && ls -1
436 separator
437 echo -n "Packages: " && ls | wc -l
438 echo "" ;;
439 setup)
440 # Setup a build environment
441 check_root
442 echo "Cook: setting up the environment" | log
443 gettext -e "\nSetting up your environment\n"
444 separator && cd $SLITAZ
445 gettext "Creating directories structure in:"; echo " $SLITAZ"
446 mkdir -p $WOK $PKGS $SRC $CACHE $LOGS
447 gettext -e "Checking for packages to install...\n"
448 for pkg in slitaz-toolchain tazdev intltool gettext
449 do
450 [ ! -d "$INSTALLED/$pkg" ] && tazpkg get-install $pkg
451 done
453 # Handle --options
454 case "$2" in
455 --wok)
456 [ ! -d "$INSTALLED/mercurial" ] && tazpkg get-install mercurial
457 [ -d "$WOK" ] && echo -e "A wok already exist.\n" && exit 1
458 hg clone $HG_URL ;;
459 --chroot)
460 echo "TODO: create a chroot with tazdev" ;;
461 esac
463 # SliTaz group and permissions
464 if ! grep -q ^slitaz /etc/group; then
465 gettext -e "Adding group: slitaz\n"
466 addgroup slitaz
467 fi
468 gettext -e "Setting permissions for slitaz group...\n"
469 chown -R root.slitaz $SLITAZ
470 chmod -R g+w $SLITAZ
471 separator
472 gettext -e "All done, ready to cook packages :-)\n\n" ;;
473 test)
474 # Test a cook environment.
475 echo "cook test: testing the cook environment" | log
476 [ ! -d "$WOK" ] && exit 1
477 [ ! -d "$WOK/cooktest" ] && cp -r $DATA/cooktest $WOK
478 cook cooktest ;;
479 new)
480 # Create the package folder and an empty receipt.
481 pkg="$2"
482 [ "$pkg" ] || usage
483 echo ""
484 if [ -d "$WOK/$pkg" ]; then
485 echo -n "$pkg " && gettext "package already exist."
486 echo -e "\n" && exit 1
487 fi
488 gettext "Creating"; echo -n " $WOK/$pkg"
489 mkdir $WOK/$pkg && cd $WOK/$pkg && status
490 gettext "Preparing the package receipt..."
491 cp $DATA/receipt .
492 sed -i s"/^PACKAGE=.*/PACKAGE=\"$pkg\"/" receipt
493 status && echo "" ;;
494 list)
495 # Cook a list of packages (better use the Cooker since it will order
496 # packages before executing cook).
497 check_root
498 [ -z "$2" ] && gettext -e "\nNo list in argument.\n\n" && exit 1
499 [ ! -f "$2" ] && gettext -e "\nNo list found:" && \
500 echo -e " $2\n" && exit 1
501 echo "cook list: Starting to cook: $2" | log
502 for pkg in $(cat $2)
503 do
504 cook $pkg || broken
505 done ;;
506 clean-wok)
507 check_root
508 gettext -e "\nCleaning all packages files..."
509 rm -rf $WOK/*/taz $WOK/*/install $WOK/*/source
510 status && echo "" ;;
511 clean-src)
512 check_root
513 gettext -e "\nCleaning all packages source..."
514 rm -rf $WOK/*/source
515 status && echo "" ;;
516 pkglist)
517 # Create suitable packages list for TazPKG and only for builded packages.
518 [ "$2" ] && PKGS="$2"
519 [ ! -d "$PKGS" ] && \
520 gettext -e "\nPackages directory dont exist\n\n" && exit 1
521 cd $PKGS
522 echo "cook pkglist: Creating packages list" | log
523 gettext -e "\nCreating lists for:"; echo " $PKGS"
524 separator
525 rm -f packages.* files.list*
526 gettext -e "Creating: packages.list\n"
527 ls -1 | sed s'/.tazpkg//' > $PKGS/packages.list
528 gettext -e "Creating: packages.md5\n"
529 md5sum *.tazpkg > $PKGS/packages.md5
530 gettext -e "Creating: packages.desc\n"
531 gettext -e "Creating: packages.equiv\n"
532 cd $WOK
533 for pkg in *
534 do
535 unset_receipt
536 . $pkg/receipt
537 # packages.desc let us search easily in DB
538 if [ -f "$PKGS/$PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg" ]; then
539 cat >> $PKGS/packages.desc << EOT
540 $PACKAGE | $VERSION$EXTRAVERSION | $SHORT_DESC | $CATEGORY | $WEB_SITE
541 EOT
542 # Packages.equiv is used by tazpkg install to check depends.
543 for i in $PROVIDE; do
544 DEST=""
545 echo $i | fgrep -q : && DEST="${i#*:}:"
546 if grep -qs ^${i%:*}= $PKGS/packages.equiv; then
547 sed -i "s/^${i%:*}=/${i%:*}=$DEST$PACKAGE /" \
548 $PKGS/packages.equiv
549 else
550 echo "${i%:*}=$DEST$PACKAGE" >> $PKGS/packages.equiv
551 fi
552 done
553 fi
554 done
555 # files.list.lzma
556 #lzma e files.list files.list.lzma
557 separator
558 nb=$(ls $PKGS/*.tazpkg | wc -l)
559 echo -e "Packages: $nb\n" ;;
560 *)
561 # Just cook and generate a package.
562 check_root
563 time=$(date +%s)
564 pkg="$1"
565 [ -z "$pkg" ] && usage
566 check_pkg_in_wok && echo ""
567 unset inst
568 unset_receipt
569 cd $WOK/$pkg && . ./receipt
571 # Handle --options
572 case "$2" in
573 --clean|-c)
574 gettext -e "Cleaning package:"; echo -n " $pkg"
575 cd $WOK/$pkg && rm -rf install taz source
576 status && echo "" && exit 0 ;;
577 --install|-i)
578 inst='yes' ;;
579 esac
581 # Check if wanted is build now so we have separate log files.
582 if [ "$WANTED" ] && [ ! -d "$WOK/$WANTED/install" ]; then
583 cook "$WANTED"
584 fi
586 # Cook and pack or exit on error and log everything.
587 cookit | tee $LOGS/$pkg.log
588 remove_deps
589 cookit_quality
590 packit | tee -a $LOGS/$pkg.log
591 clean_log
592 packit_quality
594 # Time and summary
595 time=$(($(date +%s) - $time))
596 summary | tee -a $LOGS/$pkg.log
598 # Install package if requested
599 if [ "$inst" ]; then
600 if [ -f "$PKGS/$PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg" ]; then
601 cd $PKGS && tazpkg install \
602 $PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg --forced
603 else
604 gettext -e "Unable to install package, build have failed.\n\n"
605 exit 1
606 fi
607 fi
608 # Finally we DONT WANT to build the *-dev or packages with WANTED="$pkg"
609 # You automation: use the Cooker Build Bot.
610 #[ -d "$WOK/$pkg-dev" ] && cook $pkg-dev
611 ;;
612 esac
614 exit 0