cookutils view cook @ rev 8

Handle correctly installed/uninstalled build deps
author Christophe Lincoln <pankso@slitaz.org>
date Tue May 03 23:09:59 2011 +0200 (2011-05-03)
parents 04526db035f3
children 02bf2a847c08
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 #
14 # Functions
15 #
17 usage() {
18 cat << EOT
20 $(echo -e "\033[1m$(gettext "Usage:")\033[0m") cook [package|command|list] [--option]
22 $(echo -e "\033[1m$(gettext "Commands:")\033[0m")
23 usage|help $(gettext "Display this short usage.")
24 list-wok $(gettext "List packages in the wok.")
25 setup $(gettext "Setup your build environment.")
26 new $(gettext "Create a new package with receipt".)
27 list $(gettext "Cook a list of packages.")
28 clean-wok $(gettext "Clean-up all packages files.")
29 clean-src $(gettext "Clean-up all packages source.")
30 pkglist $(gettext "Create all packages.* lists.")
32 $(echo -e "\033[1m$(gettext "Options:")\033[0m")
33 --clean|-c $(gettext "Clean the package in the wok.")
34 --install|-i $(gettext "Cook and install the package.")
35 --wok|-w $(gettext "Setup also a wok from Hg repo.")
37 EOT
38 exit 0
39 }
41 # Be sure we root.
42 check_root() {
43 [ $(id -u) != 0 ] && gettext -e "\nYou must be root to cook.\n\n" && exit 0
44 }
46 separator() {
47 echo "================================================================================"
48 }
50 status() {
51 echo -en "\\033[70G[ "
52 if [ $? = 0 ]; then
53 echo -en "\\033[1;32mOK"
54 else
55 echo -en "\\033[1;31mFailed"
56 fi
57 echo -e "\\033[0;39m ]"
58 }
60 clean_log() {
61 sed -i -e s'|\[70G\[ \[1;32m| |' \
62 -e s'|\[0;39m \]||' $LOGS/$pkg.log
63 }
65 unset_receipt() {
66 unset DEPENDS BUILD_DEPENDS WANTED EXTRAVERSION WGET_URL PROVIDE
67 }
69 # Be sure package exist in wok.
70 check_pkg_in_wok() {
71 if [ ! -d "$WOK/$pkg" ]; then
72 gettext -e "\nUnable to find package in the wok:"
73 echo -e " $pkg\n" && exit 1
74 fi
75 }
77 # Path's used in receipt and by cook itself.
78 set_paths() {
79 pkgdir=$WOK/$PACKAGE
80 src=$pkgdir/source/$PACKAGE-$VERSION
81 pack=$pkgdir/taz/$PACKAGE-${VERSION}${EXTRAVERSION}
82 fs=$pack/fs
83 stuff=$pkgdir/stuff
84 install=$pkgdir/install
85 if [ "$WANTED" ]; then
86 src=$WOK/$WANTED/source/$WANTED-$VERSION
87 install=$WOK/$WANTED/install
88 fi
89 # Old way compatibility
90 _pkg=$install
91 }
93 # Get package source
94 get_source() {
95 wget -P $SRC $WGET_URL
96 }
98 # Extract source package
99 extract_source() {
100 gettext "Extracting:"; echo " $TARBALL"
101 case "$TARBALL" in
102 *.tar.gz|*.tgz) tar xzf $SRC/$TARBALL ;;
103 *.tar.bz2) tar xjf $SRC/$TARBALL ;;
104 *.tar.lzma) tar xaf $SRC/$TARBALL ;;
105 *.zip) unzip $SRC/$TARBALL ;;
106 esac
107 }
109 # Display cooked package summary
110 summary() {
111 cd $WOK/$pkg
112 [ -d install ] && prod=$(du -sh install | awk '{print $1}' 2>/dev/null)
113 fs=$(du -sh taz/* | awk '{print $1}')
114 size=$(du -sh $PKGS/$PACKAGE-${VERSION}${EXTRAVERSION}.* | awk '{print $1}')
115 files=$(cat taz/$PACKAGE-*/files.list | wc -l)
116 gettext "Summary for:"; echo " $PACKAGE $VERSION"
117 separator
118 [ "$prod" ] && echo "Produce : $prod"
119 cat << EOT
120 Packed : $fs
121 Compressed : $size
122 Files : $files
123 Cook time : ${time}s
124 $(separator)
126 EOT
127 }
129 # Copy all generic files (locale, pixmaps, .desktop). We use standard paths,
130 # so some packages need to copy these files with the receipt and genpkg_rules.
131 copy_generic_files()
132 {
133 # $LOCALE is set in cook.conf
134 if [ "$LOCALE" ]; then
135 if [ -d "$_pkg/usr/share/locale" ]; then
136 mkdir -p $fs/usr/share/locale
137 for i in $LOCALE
138 do
139 if [ -d "$_pkg/usr/share/locale/$i" ]; then
140 cp -a $_pkg/usr/share/locale/$i $fs/usr/share/locale
141 fi
142 done
143 fi
144 fi
146 # Generic pixmaps copy can be disabled with GENERIC_PIXMAPS="no"
147 if [ "$GENERIC_PIXMAPS" != "no" ]; then
148 if [ -d "$_pkg/usr/share/pixmaps" ]; then
149 mkdir -p $fs/usr/share/pixmaps
150 cp -a $_pkg/usr/share/pixmaps/$PACKAGE.png \
151 $fs/usr/share/pixmaps 2>/dev/null
152 cp -a $_pkg/usr/share/pixmaps/$PACKAGE.xpm \
153 $fs/usr/share/pixmaps 2>/dev/null
154 fi
156 # Custom or homemade PNG pixmap can be in stuff.
157 if [ -f "$stuff/$PACKAGE.png" ]; then
158 mkdir -p $fs/usr/share/pixmaps
159 cp -a $stuff/$PACKAGE.png $fs/usr/share/pixmaps
160 fi
161 fi
163 # Desktop entry (.desktop).
164 if [ -d "$_pkg/usr/share/applications" ]; then
165 cp -a $_pkg/usr/share/applications $fs/usr/share
166 fi
168 # Homemade desktop file(s) can be in stuff.
169 if [ -d "$stuff/applications" ]; then
170 mkdir -p $fs/usr/share
171 cp -a $stuff/applications $fs/usr/share
172 fi
173 if [ -f "$stuff/$PACKAGE.desktop" ]; then
174 mkdir -p $fs/usr/share/applications
175 cp -a $stuff/$PACKAGE.desktop $fs/usr/share/applications
176 fi
177 }
179 # Find and strip : --strip-all (-s) or --strip-debug on static libs.
180 strip_package()
181 {
182 gettext "Executing strip on all files"
183 for dir in $fs/bin $fs/sbin $fs/usr/bin $fs/usr/sbin $fs/usr/games
184 do
185 if [ -d "$dir" ]; then
186 find $dir -type f -exec strip -s '{}' 2>/dev/null \;
187 fi
188 done
189 find $fs -name "*.so*" -exec strip -s '{}' 2>/dev/null \;
190 find $fs -name "*.a" -exec strip --strip-debug '{}' 2>/dev/null \;
191 status
192 }
194 # Remove installed deps.
195 remove_deps() {
196 # Now remove installed build deps.
197 deps=$(cat $CACHE/installed.diff | grep ^+[a-zA-Z0-9] | sed s/^+//)
198 nb=$(echo $deps | wc -l)
199 if [ -s "$CACHE/installed.diff" ]; then
200 gettext "Build dependencies to remove:"; echo " $nb"
201 gettext "Removing:"
202 for dep in $deps
203 do
204 echo -n " $dep"
205 yes | tazpkg remove $dep >/dev/null
206 done
207 echo ""
208 mv -f $CACHE/installed.diff $CACHE/installed.last.diff
209 fi
210 }
212 # The main cook function.
213 cookit() {
214 set_paths
215 unset error
216 echo "Cooking: $PACKAGE $VERSION"
217 separator
218 rm -rf install taz source $CACHE/error
220 # Disable -pipe if less than 512Mb free RAM.
221 free=$(free | fgrep '/+ buffers' | tr -s ' ' | cut -f 4 -d ' ')
222 if [ "$free" -lt 524288 ] && [ "$CFLAGS" != "${CFLAGS/-pipe}" ]; then
223 gettext -e "Disabling -pipe compile flag: $free RAM\n"
224 CFLAGS="${CFLAGS/-pipe}" && CFLAGS=$(echo "$CFLAGS" | tr -s ' ')
225 CXXFLAGS="${CXXFLAGS/-pipe}" && CXXFLAGS=$(echo "$CXXFLAGS" | tr -s ' ')
226 fi
227 unset free
229 # Export flags and path to be used by make
230 DESTDIR=$WOK/$PACKAGE/install
231 export DESTDIR MAKEFLAGS CFLAGS CXXFLAGS BUILD_HOST CONFIG_SITE
232 local LC_ALL=POSIX LANG=POSIX
234 # Check for build dep.
235 cd $INSTALLED && ls -1 > $CACHE/installed.list
236 [ "$DEPENDS" ] && gettext -e "Checking build dependencies...\n"
237 for dep in $BUILD_DEPENDS
238 do
239 if [ ! -d "$INSTALLED/$dep" ]; then
240 # Try local package first
241 if [ -f "$PKGS/$dep-*.tazpkg" ]; then
242 gettext "Installing dep (local):"; echo " $dep"
243 cd $PKGS && tazpkg install $dep-*.tazpkg >/dev/null
244 else
245 gettext "Installing dep (web/cache):"; echo " $dep"
246 tazpkg get-install $dep >/dev/null
247 fi
248 fi
249 done
250 ls -1 > $CACHE/installed.cook && cd $CACHE
252 # If a cook failed deps are not remove since we exit 1.
253 [ ! -s "installed.diff" ] && \
254 diff installed.list installed.cook > installed.diff
255 deps=$(cat installed.diff | grep ^+[a-zA-Z0-9] | wc -l)
257 # Get source tarball and make sure we have source dir named:
258 # $PACKAGE-$VERSION to be standard in receipts. Her we use tar.lzma
259 # tarball if it exist.
260 if [ "$WGET_URL" ] && [ ! -f "$SRC/$TARBALL" ]; then
261 if [ -f "$SRC/${SOURCE:-$PACKAGE}-$VERSION.tar.lzma" ]; then
262 TARBALL=$SRC/${SOURCE:-$PACKAGE}-$VERSION.tar.lzma
263 else
264 get_source || exit 1
265 fi
266 fi
267 if [ ! "$WANTED" ] && [ ! -d "$src" ]; then
268 mkdir -p $pkgdir/source/tmp && cd $pkgdir/source/tmp
269 extract_source || exit 1
270 mv * ../$PACKAGE-$VERSION
271 cd .. && rm -rf tmp
272 fi
274 # Execute receipt rules and stay compatible with _pkg.
275 if grep -q ^compile_rules $pkgdir/receipt; then
276 gettext -e "Executing: compile_rules\n"
277 cd $src && compile_rules || exit 1
278 [ -d $src/_pkg ] && mv $src/_pkg $install
279 fi
280 if grep -q ^genpkg_rules $pkgdir/receipt; then
281 gettext -e "Executing: genpkg_rules\n"
282 mkdir -p $fs && genpkg_rules || ( echo -e \
283 "\nERROR: genpkg_rules failed\n" | \
284 tee -a $LOGS/$pkg.log && exit 1 )
285 fi
286 separator && echo ""
287 }
289 # Cook quality assurance.
290 cookit_quality() {
291 if grep -q ^ERROR $LOGS/$pkg.log; then
292 exit 1
293 fi
294 if [ ! -d "$WOK/$pkg/install" ] && [ ! "$WANTED" ]; then
295 echo -e "\nERROR: cook failed\n" | \
296 tee -a $LOGS/$pkg.log && exit 1
297 fi
298 }
300 # Create the package.
301 packit() {
302 set_paths
303 echo "Packing: $PACKAGE ${VERSION}${EXTRAVERSION}"
304 separator
305 cd $pkgdir/taz
306 strip_package
307 for file in receipt description.txt
308 do
309 [ ! -f "../$file" ] && continue
310 gettext "Copying"; echo -n " $file..."
311 cp -f ../$file $pack && chown 0.0 $pack/$file && status
312 done
313 copy_generic_files
314 # Use Tazpkg to create a tazpkg package...
315 tazpkg pack $PACKAGE-${VERSION}${EXTRAVERSION} | grep "\[*\]"
316 separator && echo ""
317 }
319 # Verify package quality and consitensy.
320 packit_quality() {
321 if grep -q ^ERROR $LOGS/$pkg.log; then
322 exit 1
323 fi
324 if ! grep -q ^/ $WOK/$pkg/taz/$pkg-*/files.list; then
325 echo -e "ERROR: empty package\n" | tee -a $LOGS/$pkg.log && exit 1
326 else
327 mv -f $WOK/$pkg/taz/$pkg-*.tazpkg $PKGS
328 fi
329 }
331 #
332 # Commands
333 #
335 case "$1" in
336 usage|help)
337 usage ;;
338 list-wok)
339 gettext "List of packages in:"; echo " $WOK"
340 separator
341 cd $WOK && ls -1
342 separator
343 echo -n "Packages: " && ls | wc -l
344 echo "" ;;
345 setup)
346 # Setup a build environment
347 check_root
348 gettext -e "\nSetting up your environment\n"
349 separator && cd $SLITAZ
350 gettext "Creating directories structure in:"; echo " $SLITAZ"
351 mkdir -p $WOK $PKGS $SRC $CACHE $LOGS
352 gettext -e "Checking for packages to install...\n"
353 for pkg in slitaz-toolchain tazdev intltool gettext
354 do
355 [ ! -d "$INSTALLED/$pkg" ] && tazpkg get-install $pkg
356 done
358 # Handle --options
359 case "$2" in
360 --wok)
361 [ ! -d "$INSTALLED/mercurial" ] && tazpkg get-install mercurial
362 [ -d "$WOK" ] && echo -e "A wok already exist.\n" && exit 1
363 hg clone $HG_URL ;;
364 --chroot)
365 echo "TODO: create a chroot with tazdev" ;;
366 esac
368 # SliTaz group and permissions
369 if ! grep -q ^slitaz /etc/group; then
370 gettext -e "Adding group: slitaz\n"
371 addgroup slitaz
372 fi
373 gettext -e "Setting permissions for slitaz group...\n"
374 chown -R root.slitaz $SLITAZ
375 chmod -R g+w $SLITAZ
376 separator
377 gettext -e "All done, ready to cook packages :-)\n\n" ;;
378 new)
379 # Create the package folder and an empty receipt.
380 pkg="$2"
381 [ "$pkg" ] || usage
382 echo ""
383 if [ -d "$WOK/$pkg" ]; then
384 echo -n "$pkg " && gettext "package already exist."
385 echo -e "\n" && exit 1
386 fi
387 gettext "Creating"; echo -n " $WOK/$pkg"
388 mkdir $WOK/$pkg && cd $WOK/$pkg && status
389 gettext "Preparing the package receipt..."
390 cp $DATA/receipt .
391 sed -i s"/^PACKAGE=.*/PACKAGE=\"$pkg\"/" receipt
392 status && echo "" ;;
393 list)
394 # Cook a list of packages (better use the Cooker since it will order
395 # packages before executing cook).
396 check_root
397 [ -z "$2" ] && gettext -e "\nNo list in argument.\n\n" && exit 1
398 [ ! -f "$2" ] && gettext -e "\nNo list found:" && \
399 echo -e " $2\n" && exit 1
400 for pkg in $(cat $2)
401 do
402 cook $pkg || broken
403 done ;;
404 clean-wok)
405 check_root
406 gettext -e "\nCleaning all packages files..."
407 rm -rf $WOK/*/taz $WOK/*/install $WOK/*/source
408 status && echo "" ;;
409 clean-src)
410 check_root
411 gettext -e "\nCleaning all packages source..."
412 rm -rf $WOK/*/source
413 status && echo "" ;;
414 pkglist)
415 # Create suitable packages list for TazPKG and only for builded packages.
416 [ "$2" ] && PKGS="$2"
417 [ ! -d "$PKGS" ] && \
418 gettext -e "\nPackages directory dont exist\n\n" && exit 1
419 cd $PKGS
420 gettext -e "\nCreating lists for:"; echo " $PKGS"
421 separator
422 rm -f packages.* files.list*
423 gettext -e "Creating: packages.list\n"
424 ls -1 | sed s'/.tazpkg//' > $PKGS/packages.list
425 gettext -e "Creating: packages.md5\n"
426 md5sum *.tazpkg > $PKGS/packages.md5
427 gettext -e "Creating: packages.desc\n"
428 gettext -e "Creating: packages.equiv\n"
429 cd $WOK
430 for pkg in *
431 do
432 unset_receipt
433 . $pkg/receipt
434 # packages.desc let us search easily in DB
435 if [ -f "$PKGS/$PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg" ]; then
436 cat >> $PKGS/packages.desc << EOT
437 $PACKAGE | $VERSION$EXTRAVERSION | $SHORT_DESC | $CATEGORY | $WEB_SITE
438 EOT
439 # Packages.equiv is used by tazpkg install to check depends.
440 for i in $PROVIDE; do
441 DEST=""
442 echo $i | fgrep -q : && DEST="${i#*:}:"
443 if grep -qs ^${i%:*}= $PKGS/packages.equiv; then
444 sed -i "s/^${i%:*}=/${i%:*}=$DEST$PACKAGE /" \
445 $PKGS/packages.equiv
446 else
447 echo "${i%:*}=$DEST$PACKAGE" >> $PKGS/packages.equiv
448 fi
449 done
450 fi
451 done
452 # files.list.lzma
453 #lzma e files.list files.list.lzma
454 separator
455 nb=$(ls $PKGS/*.tazpkg | wc -l)
456 echo -e "Packages: $nb\n" ;;
457 *)
458 # Just cook and generate a package.
459 check_root
460 time=$(date +%s)
461 pkg="$1"
462 [ -z "$pkg" ] && usage
463 check_pkg_in_wok && echo ""
464 unset inst
465 unset_receipt
466 cd $WOK/$pkg && . ./receipt
468 # Handle --options
469 case "$2" in
470 --clean|-c)
471 gettext -e "Cleaning package:"; echo -n " $pkg"
472 cd $WOK/$pkg && rm -rf install taz source
473 status && echo "" && exit 0 ;;
474 --install|-i)
475 inst='yes' ;;
476 esac
478 # Check if wanted is build now so we have separate log files.
479 if [ "$WANTED" ] && [ ! -d "$WOK/$WANTED/install" ]; then
480 cook "$WANTED"
481 fi
483 # Cook and pack or exit on error and log everything.
484 cookit | tee $LOGS/$pkg.log
485 remove_deps
486 cookit_quality
487 packit | tee -a $LOGS/$pkg.log
488 clean_log
489 packit_quality
491 # Time and summary
492 time=$(($(date +%s) - $time))
493 summary | tee -a $LOGS/$pkg.log
495 # Install package if requested
496 if [ "$inst" ]; then
497 if [ -f "$PKGS/$PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg" ]; then
498 cd $PKGS && tazpkg install \
499 $PACKAGE-${VERSION}${EXTRAVERSION}.tazpkg --forced
500 else
501 gettext -e "Unable to install package, build have failed.\n\n"
502 exit 1
503 fi
504 fi
505 # Finally we may want to build the *-dev package
506 #[ -d "$WOK/$pkg-dev" ] && cook $pkg-dev
507 ;;
508 esac
510 exit 0