cookutils view cook @ rev 10

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