Added libgcrypt-1.4.3 template.
[pkgfs.git] / pkgfs.sh
blob01d5cf0e8c6febafb85db7a00c3b30df6f6070d5
1 #!/bin/sh
3 # pkgfs - A simple, minimal, fast and uncomplete build package system.
5 #-
6 # Copyright (c) 2008 Juan Romero Pardines.
7 # All rights reserved.
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 # 1. Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # 2. Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in the
16 # documentation and/or other materials provided with the distribution.
18 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 # TODO
31 # - Implement support for packages that need personalized installation.
32 # - Implement a chroot target that builds packages as root on it, for
33 # packages that need it (setuid, setgid).
34 # - More robust and fast dependency checking.
35 # - Multiple distfiles in a package, personalized stuff to unpack them.
36 # - Multiple URLs to download source distribution files, aliases, etc.
38 # Default path to configuration file, can be overriden
39 # via the environment or command line.
41 : ${PKGFS_CONFIG_FILE:=/usr/local/etc/pkgfs.conf}
43 : ${progname:=$(basename $0)}
44 : ${topdir:=$(/bin/pwd -P 2>/dev/null)}
45 : ${fetch_cmd:=/usr/bin/ftp -a}
46 : ${cksum_cmd:=/usr/bin/cksum -a rmd160}
47 : ${awk_cmd:=/usr/bin/awk}
48 : ${mkdir_cmd:=/bin/mkdir -p}
49 : ${tar_cmd:=/usr/bin/tar}
50 : ${rm_cmd:=/bin/rm}
51 : ${mv_cmd:=/bin/mv}
52 : ${cp_cmd:=/bin/cp}
53 : ${sed_cmd=/usr/bin/sed}
54 : ${grep_cmd=/usr/bin/grep}
55 : ${gunzip_cmd:=/usr/bin/gunzip}
56 : ${bunzip2_cmd:=/usr/bin/bunzip2}
57 : ${patch_cmd:=/usr/bin/patch}
58 : ${find_cmd:=/usr/bin/find}
59 : ${file_cmd:=/usr/bin/file}
60 : ${ln_cmd:=/bin/ln}
61 : ${chmod_cmd:=/bin/chmod}
62 : ${db_cmd:=/usr/bin/db -q}
63 : ${chmod_cmd:=/bin/chmod}
64 : ${touch_cmd:=/usr/bin/touch}
66 : ${xstow_args:=-ap}
67 : ${xstow_ignore_files:=perllocal.pod} # XXX For now ignore them.
69 set_defvars()
71 # Directories
72 : ${PKGFS_TEMPLATESDIR:=$PKGFS_DISTRIBUTIONDIR/templates}
73 : ${PKGFS_DEPSDIR:=$PKGFS_DISTRIBUTIONDIR/dependencies}
74 : ${PKGFS_BUILD_DEPS_DB:=$PKGFS_DEPSDIR/build-depends.db}
75 : ${PKGFS_TMPLHELPDIR:=$PKGFS_DISTRIBUTIONDIR/helper-templates}
76 : ${PKGFS_REGPKG_DB:=$PKGFS_DESTDIR/.pkgfs-registered-pkgs.db}
78 local DDIRS="PKGFS_DEPSDIR PKGFS_TEMPLATESDIR PKGFS_TMPLHELPDIR"
79 for i in ${DDIRS}; do
80 eval val="\$$i"
81 if [ ! -d "$val" ]; then
82 echo "**** ERROR: cannot find $i, aborting ***"
83 exit 1
85 done
88 usage()
90 cat << _EOF
91 $progname: [-C] [-c <config_file>] <target> [package_name]
93 Targets:
94 build Builds a package, only build phase is done.
95 configure Configure a package, only configure phase is done.
96 extract Extract distribution file(s) into build directory.
97 fetch Download distribution file(s).
98 info Show information about <package_name>.
99 install-destdir build + configure + install into destdir.
100 install Same than \`install-destdir´ but also stows package.
101 list Lists all currently \`stowned´ packages.
102 remove Remove package completely (unstow + remove data)
103 listfiles Lists files installed from <package_name>.
104 stow Create links in master directory.
105 unstow Remove links in master directory.
107 Options:
108 -C Do not remove build directory after successful installation.
109 -c Path to global configuration file:
110 if not specified /usr/local/etc/pkgfs.conf is used.
111 _EOF
112 exit 1
115 check_path()
117 eval local orig="$1"
119 case "$orig" in
123 orig="${orig%/}"
126 orig="$topdir/${orig%/}"
128 esac
130 path_fixed="$orig"
133 run_file()
135 local file="$1"
137 check_path "$file"
138 . $path_fixed
142 # This function merges two GNU info dirs into one and puts the result
143 # into PKGFS_MASTERDIR/share/info/dir.
145 merge_infodir_tmpl()
147 local pkgname="$1"
148 local merge_info_cmd="$PKGFS_MASTERDIR/bin/merge-info"
150 [ -z "$pkgname" -o ! -r "$PKGFS_MASTERDIR/share/info/dir" \
151 -o ! -r "$PKGFS_DESTDIR/$pkgname/share/info/dir" ] && return 1
153 $merge_info_cmd -d $PKGFS_MASTERDIR/share/info/dir \
154 $PKGFS_DESTDIR/$pkgname/share/info/dir -o \
155 $PKGFS_MASTERDIR/share/info/dir.new
156 if [ $? -ne 0 ]; then
157 echo -n "*** WARNING: there was an error merging info dir from"
158 echo " $pkgname, aborting ***"
159 return 1
162 $mv_cmd -f $PKGFS_MASTERDIR/share/info/dir.new \
163 $PKGFS_MASTERDIR/share/info/dir
167 # Shows info about a template.
169 info_tmpl()
171 echo "pkgname: $pkgname"
172 echo "version: $version"
173 for i in "${distfiles}"; do
174 [ -n "$i" ] && echo "distfile: $i"
175 done
176 echo "URL: $url"
177 echo "maintainer: $maintainer"
178 [ -n $checksum ] && echo "checksum: $checksum"
179 echo "build_style: $build_style"
180 echo "short_desc: $short_desc"
181 echo "$long_desc"
182 echo
183 check_build_depends_pkg $pkgname-$version
184 if [ $? -eq 0 ]; then
185 local list="$($db_cmd -V btree $PKGFS_BUILD_DEPS_DB $pkgname)"
186 echo "This package requires the following dependencies to be built:"
187 for i in ${list}; do
188 echo " $i"
189 done
194 # Checks that all required variables specified in the configuration
195 # file are properly working.
197 check_config_vars()
199 local cffound=
201 if [ -z "$config_file_specified" ]; then
202 config_file_paths="$PKGFS_CONFIG_FILE ./pkgfs.conf"
203 for f in $config_file_paths; do
204 [ -f $f ] && PKGFS_CONFIG_FILE=$f && \
205 cffound=yes && break
206 done
207 if [ -z "$cffound" ]; then
208 echo -n "*** ERROR: config file not specified "
209 echo "and not in default location or current dir ***"
210 exit 1
214 run_file ${PKGFS_CONFIG_FILE}
215 PKGFS_CONFIG_FILE=$path_fixed
217 if [ ! -f "$PKGFS_CONFIG_FILE" ]; then
218 echo -n "*** ERROR: cannot find configuration file: "
219 echo "'$PKGFS_CONFIG_FILE' ***"
220 exit 1
223 local PKGFS_VARS="PKGFS_MASTERDIR PKGFS_DESTDIR PKGFS_BUILDDIR \
224 PKGFS_SRCDISTDIR PKGFS_SYSCONFDIR"
226 for f in ${PKGFS_VARS}; do
227 eval val="\$$f"
228 if [ -z "$val" ]; then
229 echo -n "**** ERROR: '$f' not set in configuration "
230 echo "file, aborting ***"
231 exit 1
234 if [ ! -d "$val" ]; then
235 $mkdir_cmd "$val"
236 if [ "$?" -ne 0 ]; then
237 echo -n "*** ERROR: couldn't create '$f'"
238 echo "directory, aborting ***"
239 exit 1
242 done
246 # Resets all vars used by a template.
248 reset_tmpl_vars()
250 local TMPL_VARS="pkgname extract_sufx distfiles url configure_args \
251 make_build_args make_install_args build_style \
252 short_desc maintainer long_desc checksum wrksrc \
253 patch_files configure_env make_cmd pkgconfig_override \
254 configure_env make_env make_build_target \
255 run_stuff_before_configure_cmd run_stuff_before_build_cmd \
256 run_stuff_before_install_cmd run_stuff_after_install_cmd \
257 make_install_target postinstall_helpers version \
258 ignore_files \
259 PKGFS_EXTRACT_DONE PKGFS_CONFIGURE_DONE \
260 PKGFS_BUILD_DONE PKGFS_INSTALL_DONE"
262 for i in ${TMPL_VARS}; do
263 eval unset "$i"
264 done
266 unset_build_vars
270 # Reads a template file and setups required variables for operations.
272 setup_tmpl()
274 local pkg="$1"
276 if [ -f "$PKGFS_TEMPLATESDIR/$pkg.tmpl" ]; then
277 run_file $PKGFS_TEMPLATESDIR/$pkg.tmpl
278 prepare_tmpl
279 else
280 echo "*** ERROR: cannot find \`$pkg´ template file ***"
281 exit 1
286 # Checks some vars used in templates and sets some of them required.
288 prepare_tmpl()
290 local dfile=""
293 # There's nothing of interest if we are a meta template.
295 [ "$build_style" = "meta-template" ] && return 0
297 REQ_VARS="pkgname version extract_sufx url build_style"
299 # Check if required vars weren't set.
300 for i in ${REQ_VARS}; do
301 eval val="\$$i"
302 if [ -z "$val" -o -z "$i" ]; then
303 echo -n "*** ERROR: $i not set (incomplete template"
304 echo " build file), aborting ***"
305 exit 1
307 done
309 if [ -z "$distfiles" ]; then
310 dfile="$pkgname-$version$extract_sufx"
311 elif [ -n "${distfiles}" ]; then
312 dfile="$distfiles$extract_sufx"
313 else
314 echo "*** ERROR unsupported fetch state ***"
315 exit 1
318 dfile="$PKGFS_SRCDISTDIR/$dfile"
320 case "$extract_sufx" in
321 .tar.bz2|.tar.gz|.tgz|.tbz)
322 extract_cmd="$tar_cmd xfz $dfile -C $PKGFS_BUILDDIR"
324 .tar)
325 extract_cmd="$tar_cmd xf $dfile -C $PKGFS_BUILDDIR"
327 .zip)
328 if [ -f "$PKGFS_TMPLHELPDIR/unzip-extraction.sh" ]; then
329 . $PKGFS_TMPLHELPDIR/unzip-extraction.sh
330 unset wrksrc
332 # $extract_cmd set by the helper
335 echo -n "*** ERROR: unknown 'extract_sufx' argument in build "
336 echo "file ***"
337 exit 1
339 esac
341 unset PKGFS_EXTRACT_DONE PKGFS_APPLYPATCHES_DONE
342 unset PKGFS_CONFIGURE_DONE PKGFS_BUILD_DONE PKGFS_INSTALL_DONE
344 if [ -n "$wrksrc" ]; then
345 wrksrc=$PKGFS_BUILDDIR/$wrksrc
346 elif [ -z "$wrksrc" -a -z "$distfiles" ]; then
347 wrksrc=$PKGFS_BUILDDIR/$pkgname-$version
348 elif [ -z "$wrksrc" -a -n "$distfiles" ]; then
349 wrksrc=$PKGFS_BUILDDIR/$distfiles
350 else
351 echo "*** ERROR: can't guess what's the correct \$wrksrc! ***"
352 exit 1
355 PKGFS_EXTRACT_DONE="$wrksrc/.pkgfs_extract_done"
356 PKGFS_APPLYPATCHES_DONE="$wrksrc/.pkgfs_applypatches_done"
357 PKGFS_CONFIGURE_DONE="$wrksrc/.pkgfs_configure_done"
358 PKGFS_BUILD_DONE="$wrksrc/.pkgfs_build_done"
359 PKGFS_INSTALL_DONE="$wrksrc/.pkgfs_install_done"
361 export PATH="/bin:/sbin:/usr/bin:/usr/sbin:$PKGFS_MASTERDIR/bin:$PKGFS_MASTERDIR/sbin"
365 # Extracts contents of distfiles specified in a template into
366 # the build directory.
368 extract_distfiles()
370 local pkg="$1"
373 # If we are being called via the target, just extract and return.
375 [ -n "$pkg" -a -z "$pkgname" ] && return 1
378 # There's nothing of interest if we are a meta template.
380 [ "$build_style" = "meta-template" ] && return 0
382 echo "==> Extracting \`$pkgname-$version' into $PKGFS_BUILDDIR."
384 $extract_cmd
385 if [ "$?" -ne 0 ]; then
386 echo -n "*** ERROR: there was an error extracting the "
387 echo "distfile(s), aborting *** "
388 exit 1
391 unset extract_cmd
392 $touch_cmd -f $PKGFS_EXTRACT_DONE
396 # Verifies that a checksum of a distfile is correct.
398 check_rmd160_cksum()
400 local file="$1"
401 local dfile=
403 [ -z "$file" ] && return 1
405 if [ -z "${distfiles}" ]; then
406 dfile="$pkgname-$version$extract_sufx"
407 elif [ -n "${distfiles}" ]; then
408 dfile="$distfiles$extract_sufx"
409 else
410 dfile="$file$extract_sufx"
413 if [ -z "$checksum" ]; then
414 echo "*** ERROR: checksum unset in template file for \`$pkgname' ***"
415 exit 1
418 origsum="$checksum"
419 dfile="$PKGFS_SRCDISTDIR/$dfile"
420 filesum="$($cksum_cmd $dfile | $awk_cmd '{print $4}')"
421 if [ "$origsum" != "$filesum" ]; then
422 echo "*** WARNING: RMD160 checksum doesn't match for \`$dfile' ***"
423 exit 1
426 echo "=> checksum (RMD160) OK for \`$pkgname-$version'."
430 # Downloads the distfiles for a template from $url.
432 fetch_distfiles()
434 local pkg="$1"
435 local file=""
436 local file2=""
437 local only_fetch=
440 # If we are being called by the target, just fetch distfiles
441 # and return.
443 [ -n $pkg ] && only_fetch=yes
444 [ -z $pkgname ] && exit 1
447 # There's nothing of interest if we are a meta template.
449 [ "$build_style" = "meta-template" ] && return 0
451 if [ -z "$distfiles" ]; then
452 file="$pkgname-$version"
453 else
454 file="$distfiles"
457 for f in "$file"; do
458 file2="$f$extract_sufx"
459 if [ -f "$PKGFS_SRCDISTDIR/$file2" ]; then
460 check_rmd160_cksum $f
461 [ $? -eq 0 ] && continue
464 echo "==> Fetching distfile: \`$file2'."
466 cd $PKGFS_SRCDISTDIR && $fetch_cmd $url/$file2
467 if [ $? -ne 0 ]; then
468 if [ ! -f $PKGFS_SRCDISTDIR/$file2 ]; then
469 echo -n "*** ERROR: couldn't fetch '$file2', "
470 echo "aborting ***"
471 else
472 echo -n "*** ERROR: there was an error "
473 echo "fetching '$file2', aborting ***"
475 exit 1
476 else
477 check_rmd160_cksum $f
479 done
482 fixup_tmpl_libtool()
484 local lt_file="$wrksrc/libtool"
487 # If package has a libtool file replace it with ours, so that
488 # we use the master directory while relinking, all will be fine
489 # once the package is stowned.
491 if [ -f "$lt_file" -a -f "$PKGFS_MASTERDIR/bin/libtool" ]; then
492 $rm_cmd -f $wrksrc/libtool
493 $rm_cmd -f $wrksrc/ltmain.sh
494 $ln_cmd -s $PKGFS_MASTERDIR/bin/libtool $lt_file
495 $ln_cmd -s $PKGFS_MASTERDIR/share/libtool/config/ltmain.sh \
496 $wrksrc/ltmain.sh
497 elif [ -f "$PKGFS_MASTERDIR/bin/libtool" ]; then
498 $ln_cmd -s $PKGFS_MASTERDIR/bin/libtool $lt_file
502 set_build_vars()
504 LDFLAGS="-L$PKGFS_MASTERDIR/lib -Wl,-R$PKGFS_MASTERDIR/lib $LDFLAGS"
505 LDFLAGS="-L$PKGFS_DESTDIR/$pkgname-$version/lib $LDFLAGS"
506 CFLAGS="$CFLAGS $PKGFS_CFLAGS"
507 CXXFLAGS="$CXXFLAGS $PKGFS_CXXFLAGS"
508 CPPFLAGS="-I$PKGFS_MASTERDIR/include $CPPFLAGS"
509 PKG_CONFIG="$PKGFS_MASTERDIR/bin/pkg-config"
511 export LDFLAGS="$LDFLAGS" CFLAGS="$CFLAGS" CXXFLAGS="$CXXFLAGS"
512 export CPPFLAGS="$CPPFLAGS" PKG_CONFIG="$PKG_CONFIG"
515 unset_build_vars()
517 unset LDFLAGS CFLAGS CXXFLAGS CPPFLAGS PKG_CONFIG
521 # Applies to the build directory the patches specified by a template.
523 apply_tmpl_patches()
525 local patch=
528 # If package needs some patches applied before building,
529 # apply them now.
531 if [ -n "$patch_files" ]; then
532 for i in ${patch_files}; do
533 patch="$PKGFS_TEMPLATESDIR/$i"
534 if [ ! -f "$patch" ]; then
535 echo "*** WARNING: unexistent patch '$i' ***"
536 continue
539 $cp_cmd -f $patch $wrksrc
541 # Try to guess if its a compressed patch.
542 if $(echo $patch|$grep_cmd -q .gz); then
543 $gunzip_cmd $wrksrc/$i
544 patch=${i%%.gz}
545 elif $(echo $patch|$grep_cmd -q .bz2); then
546 $bunzip2_cmd $wrksrc/$i
547 patch=${i%%.bz2}
548 elif $(echo $patch|$grep_cmd -q .diff); then
549 patch=$i
550 else
551 echo "*** WARNING: unknown patch type '$i' ***"
552 continue
555 cd $wrksrc && $patch_cmd < $patch 2>/dev/null
556 if [ "$?" -eq 0 ]; then
557 echo "=> Patch applied: \`$i'."
558 else
559 echo -n "*** ERROR: couldn't apply patch '$i'"
560 echo ", aborting ***"
561 exit 1
563 done
566 $touch_cmd -f $PKGFS_APPLYPATCHES_DONE
570 # Runs the "configure" phase for a pkg. This setups the Makefiles or any
571 # other stuff required to be able to build binaries or such.
573 configure_src_phase()
575 local pkg="$1"
577 [ -z $pkg ] && [ -z $pkgname ] && return 1
580 # There's nothing we can do if we are a meta template.
582 [ "$build_style" = "meta-template" ] && return 0
584 if [ ! -d $wrksrc ]; then
585 echo "*** ERROR: unexistent build directory \`$wrksrc' ***"
586 exit 1
589 echo "=> Running \`\`configure´´ phase for \`$pkgname-$version'."
591 # Apply patches if requested by template file
592 [ ! -f $PKGFS_APPLYPATCHES_DONE ] && apply_tmpl_patches
594 # Run stuff before configure.
595 local rbcf="$PKGFS_TEMPLATESDIR/$pkgname-runstuff-before-configure.sh"
596 [ -f $rbcf ] && . $rbcf
597 [ -n "$run_stuff_before_configure_cmd" ] && \
598 ${run_stuff_before_configure_cmd}
600 # Export configure_env vars.
601 for f in ${configure_env}; do
602 export "$f"
603 done
605 set_build_vars
608 # Packages using GNU autoconf
610 if [ "$build_style" = "gnu_configure" ]; then
611 cd $wrksrc
613 # Pass consistent arguments to not have unexpected
614 # surprises later.
616 ./configure \
617 --prefix="$PKGFS_MASTERDIR" \
618 --mandir="$PKGFS_DESTDIR/$pkgname-$version/man" \
619 --infodir="$PKGFS_DESTDIR/$pkgname-$version/share/info" \
620 --sysconfdir="$PKGFS_SYSCONFDIR" \
621 ${configure_args}
624 # Packages using propietary configure scripts.
626 elif [ "$build_style" = "configure" ]; then
627 cd $wrksrc
628 if [ -n "$configure_script" ]; then
629 ./$configure_script ${configure_args}
630 else
631 ./configure ${configure_args}
634 # Packages that are perl modules and use Makefile.PL files.
635 # They are all handled by the helper perl-module.sh.
637 elif [ "$build_style" = "perl_module" ]; then
638 . $PKGFS_TMPLHELPDIR/perl-module.sh
639 perl_module_build $pkgname
642 # Packages with BSD or GNU Makefiles are easy, just skip
643 # the configure stage and proceed.
645 elif [ "$build_style" = "bsd_makefile" -o \
646 "$build_style" = "gnu_makefile" ]; then
648 cd $wrksrc
650 # Unknown build_style type won't work :-)
652 else
653 echo "*** ERROR unknown build_style \`$build_style' ***"
654 exit 1
657 if [ "$build_style" != "perl_module" -a "$?" -ne 0 ]; then
658 echo "*** ERROR building (configure state) \`$pkg' ***"
659 exit 1
662 # unset configure_env vars.
663 for f in ${configure_env}; do
664 unset eval ${f%=*}
665 done
667 $touch_cmd -f $PKGFS_CONFIGURE_DONE
671 # Runs the "build" phase for a pkg. This builds the binaries and other
672 # related stuff.
674 build_src_phase()
676 local pkgparam="$1"
677 local pkg="$pkgname-$version"
679 [ -z $pkgparam ] && [ -z $pkgname -o -z $version ] && return 1
682 # There's nothing of interest if we are a meta template.
684 [ "$build_style" = "meta-template" ] && return 0
686 if [ ! -d $wrksrc ]; then
687 echo "*** ERROR: unexistent build directory \`$wrksrc' ***"
688 exit 1
691 cd $wrksrc || exit 1
693 echo "=> Running \`\`build´´ phase for \`$pkg'."
696 # Assume BSD make if make_cmd not set in template.
698 if [ -z "$make_cmd" ]; then
699 make_cmd="/usr/bin/make"
702 # Fixup libtool script if necessary
703 fixup_tmpl_libtool
706 # Run template stuff before building.
708 local rbbf="$PKGFS_TEMPLATESDIR/$pkgname-runstuff-before-build.sh"a
709 [ -f $rbbf ] && . $rbbf
710 [ -n "$run_stuff_before_build_cmd" ] && ${run_stuff_before_build_cmd}
713 [ -z "$make_build_target" ] && make_build_target=
714 [ -n "$PKGFS_MAKEJOBS" ] && PKGFS_MAKEJOBS="-j$PKGFS_MAKEJOBS"
716 # Export make_env vars.
717 for f in ${make_env}; do
718 export "$f"
719 done
722 # Build package via make.
724 ${make_cmd} ${PKGFS_MAKEJOBS} ${make_build_args} ${make_build_target}
725 if [ "$?" -ne 0 ]; then
726 echo "*** ERROR building (make stage) \`$pkg' ***"
727 exit 1
731 # Run template stuff before installing.
733 local rbif="$PKGFS_TEMPLATESDIR/$pkgname-runstuff-before-install.sh"
734 [ -f $rbif ] && . $rbif
735 [ -n "$run_stuff_before_install_cmd" ] && ${run_stuff_before_install_cmd}
737 $touch_cmd -f $PKGFS_BUILD_DONE
741 # Runs the "install" phase for a pkg. This consists in installing package
742 # into the destination directory.
744 install_src_phase()
746 local pkg="$1"
748 [ -z $pkg ] && [ -z $pkgname ] && return 1
750 [ -z "$make_install_target" ] && make_install_target=install
753 # There's nothing we can do if we are a meta template.
755 [ "$build_style" = "meta-template" ] && return 0
757 if [ ! -d $wrksrc ]; then
758 echo "*** ERROR: unexistent build directory \`$wrksrc' ***"
759 exit 1
762 cd $wrksrc || exit 1
764 echo "=> Running \`\`install´´ phase for: \`$pkgname-$version´."
767 # Install package via make.
769 ${make_cmd} ${make_install_args} ${make_install_target} \
770 prefix="$PKGFS_DESTDIR/$pkgname-$version"
771 if [ "$?" -ne 0 ]; then
772 echo "*** ERROR instaling \`$pkgname-$version' ***"
773 exit 1
776 # Unset make_env vars.
777 for f in ${make_env}; do
778 unset eval ${f%=*}
779 done
782 # Run template stuff after installing.
784 local raif="$PKGFS_TEMPLATESDIR/$pkgname-runstuff-after-install.sh"
785 [ -f $raif ] && . $raif
786 [ -n "$run_stuff_after_install_cmd" ] && ${run_stuff_after_install_cmd}
789 # Transform pkg-config files if requested by template.
791 for i in ${pkgconfig_override}; do
792 local tmpf="$PKGFS_DESTDIR/$pkgname-$version/lib/pkgconfig/$i"
793 [ -f "$tmpf" ] && \
794 [ -f $PKGFS_TMPLHELPDIR/pkg-config-transform.sh ] && \
795 . $PKGFS_TMPLHELPDIR/pkg-config-transform.sh && \
796 pkgconfig_transform_file $tmpf
797 done
799 # Unset build vars.
800 unset_build_vars
802 echo "==> Installed \`$pkgname-$version' into $PKGFS_DESTDIR."
804 $touch_cmd -f $PKGFS_INSTALL_DONE
807 # Remove $wrksrc if -C not specified.
809 if [ -d "$wrksrc" -a -z "$dontrm_builddir" ]; then
810 $rm_cmd -rf $wrksrc
811 [ "$?" -eq 0 ] && \
812 echo "=> Removed \`$pkgname-$version' build directory."
815 cd $PKGFS_BUILDDIR
819 # Registers or unregisters a package from the db file.
821 register_pkg_handler()
823 local action="$1"
824 local pkg="$2"
825 local version="$3"
827 [ -z "$action" -o -z "$pkg" -o -z "$version" ] && return 1
829 if [ "$action" = "register" ]; then
830 $db_cmd -w btree $PKGFS_REGPKG_DB $pkg $version 2>&1 >/dev/null
831 if [ "$?" -ne 0 ]; then
832 echo -n "*** ERROR: couldn't register \`$pkg'"
833 echo " in db file ***"
834 exit 1
836 elif [ "$action" = "unregister" ]; then
837 $db_cmd -d btree $PKGFS_REGPKG_DB $pkg 2>&1 >/dev/null
838 if [ "$?" -ne 0 ]; then
839 echo -n "*** ERROR: \`$pkg' not registered "
840 echo "in db file? ***"
841 exit 1
843 else
844 return 1
849 # Recursive function that founds dependencies in all required
850 # packages.
852 add_dependency_tolist()
854 local curpkg="$1"
856 [ -z "$curpkg" ] && return 1
857 [ -n "$prev_pkg" ] && curpkg=$prev_pkg
859 for i in $($db_cmd -V btree $PKGFS_BUILD_DEPS_DB ${curpkg%-[0-9]*.*}); do
861 # Check if dep already installed.
863 if [ -r "$PKGFS_REGPKG_DB" ]; then
864 check_installed_pkg $i ${i##[aA-zZ]*-}
866 # If dep is already installed, put it on the
867 # installed deps list and continue.
869 if [ $? -eq 0 ]; then
870 installed_deps_list="$i $installed_deps_list"
871 continue
874 deps_list="$i $deps_list"
875 [ -n "$prev_pkg" ] && unset prev_pkg
877 # Check if dependency needs more deps.
879 check_build_depends_pkg ${i%-[0-9]*.*}
880 if [ $? -eq 0 ]; then
881 add_dependency_tolist $i
882 prev_pkg="$i"
885 done
889 # Removes duplicate deps in the installed or not installed list.
891 find_dupdeps_inlist()
893 local action="$1"
894 local tmp_list=
895 local dup=
897 [ -z "$action" ] && return 1
899 case "$action" in
900 installed)
901 list=$installed_deps_list
903 notinstalled)
904 list=$deps_list
907 return 1
909 esac
911 for f in $list; do
912 if [ -z "$tmp_list" ]; then
913 tmp_list="$f"
914 else
915 for i in $tmp_list; do
916 [ "$f" = "$i" ] && dup=yes
917 done
919 [ -z "$dup" ] && tmp_list="$tmp_list $f"
920 unset dup
922 done
924 case "$action" in
925 installed)
926 installed_deps_list="$tmp_list"
928 notinstalled)
929 deps_list="$tmp_list"
932 return 1
934 esac
938 # Installs all dependencies required by a package.
940 install_dependencies_pkg()
942 local pkg="$1"
943 deps_list=
944 installed_deps_list=
946 [ -z "$pkg" ] && return 1
948 doing_deps=true
950 add_dependency_tolist $pkg
951 find_dupdeps_inlist installed
952 find_dupdeps_inlist notinstalled
954 [ -z "$deps_list" -a -z "$installed_deps_list" ] && return 0
956 echo "==> Required dependencies for $(basename $pkg):"
957 for i in ${installed_deps_list}; do
958 fpkg="$($db_cmd -O '-' btree $PKGFS_REGPKG_DB ${i%-[0-9]*.*})"
959 echo " $i: found $fpkg."
960 done
962 for i in ${deps_list}; do
963 echo " $i: not installed."
964 done
966 for i in ${deps_list}; do
967 # skip dup deps
968 check_installed_pkg $i ${i##[aA-zZ]*-}
969 [ $? -eq 0 ] && continue
970 # continue installing deps
971 echo "==> Installing \`$pkg´ dependency: \`$i´."
972 install_pkg ${i%-[0-9]*.*}
973 done
975 unset installed_deps_list
976 unset deps_list
979 install_builddeps_required_pkg()
981 local pkg="$1"
983 [ -z "$pkg" ] && return 1
985 for dep in $($db_cmd -V btree $PKGFS_BUILD_DEPS_DB ${pkg%-[0-9]*.*}); do
986 check_installed_pkg $dep ${dep##[aA-zZ]*-}
987 if [ $? -ne 0 ]; then
988 echo "==> Installing \`$pkg´ dependency: $dep."
989 install_pkg ${dep%-[0-9]*.*}
991 done
995 # Checks the registered pkgs db file and returns 0 if a pkg that satisfies
996 # the minimal required version is there, or 1 otherwise.
998 check_installed_pkg()
1000 local pkg="$1"
1001 local reqver="$2"
1002 local iver=
1004 [ -z "$pkg" -o -z "$reqver" -o ! -r $PKGFS_REGPKG_DB ] && return 1
1006 run_file $PKGFS_TEMPLATESDIR/${pkg%-[0-9]*.*}.tmpl
1008 reqver="$(echo $reqver | $sed_cmd 's|[[:punct:]]||g;s|[[:alpha:]]||g')"
1010 $db_cmd -K btree $PKGFS_REGPKG_DB $pkgname 2>&1 >/dev/null
1011 if [ $? -eq 0 ]; then
1013 # Package is installed, let's check the version.
1015 iver="$($db_cmd -V btree $PKGFS_REGPKG_DB $pkgname)"
1016 if [ -n "$iver" ]; then
1018 # As shell only supports decimal arith expressions,
1019 # we simply remove anything except the numbers.
1020 # It's not optimal and may fail, but it is enough
1021 # for now.
1023 iver="$(echo $iver | $sed_cmd 's|[[:punct:]]||g;s|[[:alpha:]]||g')"
1024 if [ "$iver" -eq "$reqver" \
1025 -o "$iver" -gt "$reqver" ]; then
1026 return 0
1031 return 1
1035 # Checks the build depends db file and returns 0 if pkg has dependencies,
1036 # otherwise returns 1.
1038 check_build_depends_pkg()
1040 local pkg="$1"
1042 [ -z $pkg -o ! -r $PKGFS_BUILD_DEPS_DB ] && return 1
1044 $db_cmd -V btree $PKGFS_BUILD_DEPS_DB ${pkg%-[0-9]*.*} 2>&1 >/dev/null
1045 return $?
1049 # Installs a pkg by reading its build template file.
1051 install_pkg()
1053 local pkg=
1054 local curpkgn="$1"
1056 local cur_tmpl="$PKGFS_TEMPLATESDIR/$curpkgn.tmpl"
1057 if [ -z $cur_tmpl -o ! -f $cur_tmpl ]; then
1058 echo "*** ERROR: cannot find \`$cur_tmpl´ template file ***"
1059 exit 1
1062 reset_tmpl_vars
1063 run_file $cur_tmpl
1064 pkg="$curpkgn-$version"
1067 # If we are the originator package save the path this template in
1068 # other var for future use.
1070 [ -z "$origin_tmpl" ] && origin_tmpl=$pkgname
1073 # Install xstow if it's not there.
1075 install_xstow_pkg
1078 # We are going to install a new package.
1080 prepare_tmpl
1083 # Install dependencies required by this package.
1085 if [ -z "$doing_deps" ]; then
1086 install_dependencies_pkg $pkg
1088 # At this point all required deps are installed, and
1089 # only remaining is the origin template; install it.
1091 unset doing_deps
1092 reset_tmpl_vars
1093 setup_tmpl $curpkgn
1097 # Fetch, extract, build and install into the destination directory.
1099 fetch_distfiles
1101 if [ ! -f "$PKGFS_EXTRACT_DONE" ]; then
1102 extract_distfiles
1105 if [ ! -f "$PKGFS_CONFIGURE_DONE" ]; then
1106 configure_src_phase
1109 if [ ! -f "$PKGFS_BUILD_DONE" ]; then
1110 build_src_phase
1113 install_src_phase
1116 # Just register meta-template and exit.
1118 if [ "$build_style" = "meta-template" ]; then
1119 register_pkg_handler register $pkgname $version
1120 echo "==> Installed meta-template \`$pkg'."
1121 return 0
1125 # Do not stow package if it wasn't requested.
1127 [ -z "$install_destdir_target" ] && stow_pkg $pkg
1131 # Installs and stows the "xstow" package.
1133 install_xstow_pkg()
1135 [ -x "$PKGFS_XSTOW_CMD" ] && return 0
1137 echo "=> xstow application not found, will install it now."
1139 reset_tmpl_vars
1140 setup_tmpl xstow
1141 fetch_distfiles
1143 if [ ! -f "$PKGFS_EXTRACT_DONE" ]; then
1144 extract_distfiles
1147 if [ ! -f "$PKGFS_CONFIGURE_DONE" ]; then
1148 configure_src_phase
1151 if [ ! -f "$PKGFS_BUILD_DONE" ]; then
1152 build_src_phase
1155 install_src_phase
1157 PKGFS_XSTOW_CMD="$PKGFS_DESTDIR/$pkgname-$version/bin/xstow"
1158 stow_pkg $pkgname-$version
1161 # Continue with package that called us.
1163 run_file $PKGFS_TEMPLATESDIR/$origin_tmpl.tmpl
1167 # Lists all currently installed packages.
1169 list_pkgs()
1171 if [ ! -r "$PKGFS_REGPKG_DB" ]; then
1172 echo "=> No packages registered or missing register db file."
1173 exit 0
1176 for i in $($db_cmd -K btree $PKGFS_REGPKG_DB); do
1177 # Run file to get short_desc and print something useful
1178 run_file $PKGFS_TEMPLATESDIR/$i.tmpl
1179 echo "$i-$version $short_desc"
1180 reset_tmpl_vars
1181 done
1185 # Lists files installed by a package.
1187 list_pkg_files()
1189 local pkg="$1"
1191 if [ -z $pkg ]; then
1192 echo "*** ERROR: unexistent package, aborting ***"
1193 exit 1
1196 if [ ! -d "$PKGFS_DESTDIR/$pkg" ]; then
1197 echo "*** ERROR: cannot find \`$pkg' in $PKGFS_DESTDIR ***"
1198 exit 1
1201 for f in $($find_cmd $PKGFS_DESTDIR/$pkg -type f -print | sort -u); do
1202 echo "${f##$PKGFS_DESTDIR/$pkg/}"
1203 done
1207 # Removes a currently installed package (unstow + removed from destdir).
1209 remove_pkg()
1211 local pkg="$1"
1213 if [ -z "$pkg" ]; then
1214 echo "*** ERROR: unexistent package, aborting ***"
1215 exit 1
1218 if [ ! -f "$PKGFS_TEMPLATESDIR/$pkg.tmpl" ]; then
1219 echo "*** ERROR: cannot find template file ***"
1220 exit 1
1223 run_file $PKGFS_TEMPLATESDIR/$pkg.tmpl
1226 # If it's a meta-template, just unregister it from the db.
1228 if [ "$build_style" = "meta-template" ]; then
1229 register_pkg_handler unregister $pkgname $version
1230 [ $? -eq 0 ] && \
1231 echo "=> Removed meta-template \`$pkg'."
1232 return $?
1235 if [ ! -d "$PKGFS_DESTDIR/$pkg-$version" ]; then
1236 echo "*** ERROR: cannot find package on $PKGFS_DESTDIR ***"
1237 exit 1
1240 unstow_pkg $pkg
1241 $rm_cmd -rf $PKGFS_DESTDIR/$pkg-$version
1242 return $?
1246 # Stows a currently installed package, i.e creates the links
1247 # on the master directory.
1249 stow_pkg()
1251 local pkg="$1"
1252 local infodir_pkg="share/info/dir"
1253 local infodir_master="$PKGFS_MASTERDIR/share/info/dir"
1254 local real_xstowargs="$xstow_args"
1255 local real_xstow_ignore="$xstow_ignore_files"
1257 [ -z "$pkg" ] && return 2
1259 if [ -n "$stow_flag" ]; then
1260 pkg=$PKGFS_TEMPLATESDIR/$pkg.tmpl
1261 run_file $pkg
1262 pkg=$pkgname-$version
1264 # You cannot stow a meta-template.
1266 [ "$build_style" = "meta-template" ] && return 0
1269 if [ -r "$PKGFS_DESTDIR/$pkg/$infodir_pkg" ]; then
1270 merge_infodir_tmpl $pkg
1273 if [ -r "$PKGFS_DESTDIR/$pkg/$infodir_pkg" \
1274 -a -r "$infodir_master" ]; then
1275 xstow_args="$xstow_args -i-file-in-dir $infodir_pkg"
1278 if [ -n "$ignore_files" ]; then
1279 xstow_ignore_files="$xstow_ignore_files $ignore_files"
1282 $PKGFS_XSTOW_CMD -ignore "${xstow_ignore_files}" ${xstow_args} \
1283 -pd-targets $PKGFS_MASTERDIR \
1284 -dir $PKGFS_DESTDIR -target $PKGFS_MASTERDIR \
1285 $PKGFS_DESTDIR/$pkg
1286 if [ "$?" -ne 0 ]; then
1287 echo "*** ERROR: couldn't create symlinks for \`$pkg' ***"
1288 exit 1
1289 else
1290 echo "==> Created \`$pkg' symlinks into master directory."
1293 register_pkg_handler register $pkgname $version
1296 # Run template postinstall helpers if requested.
1298 if [ "$pkgname" != "${pkg%%-$version}" ]; then
1299 run_file $PKGFS_TEMPLATESDIR/${pkg%%-$version}.tmpl
1302 for i in ${postinstall_helpers}; do
1303 local pihf="$PKGFS_TMPLHELPDIR/$i"
1304 [ -f "$pihf" ] && . $pihf
1305 done
1307 xstow_ignore_files="$real_xstow_ignore"
1308 xstow_args="$real_xstowargs"
1312 # Unstows a currently stowned package, i.e removes its links
1313 # from the master directory.
1315 unstow_pkg()
1317 local pkg="$1"
1318 local real_xstow_ignore="$xstow_ignore_files"
1320 if [ -z "$pkg" ]; then
1321 echo "*** ERROR: template wasn't specified? ***"
1322 exit 1
1325 if [ "$pkg" = "xstow" ]; then
1326 echo "*** INFO: You aren't allowed to unstow \`$pkg'."
1327 exit 1
1330 run_file $PKGFS_TEMPLATESDIR/$pkg.tmpl
1333 # You cannot unstow a meta-template.
1335 [ "$build_style" = "meta-template" ] && return 0
1337 if [ -n "$ignore_files" ]; then
1338 xstow_ignore_files="$xstow_ignore_files $ignore_files"
1341 $PKGFS_XSTOW_CMD -dir $PKGFS_DESTDIR -target $PKGFS_MASTERDIR \
1342 -D -i-file-in-dir share/info/dir -ignore \
1343 "${xstow_ignore_files}" $PKGFS_DESTDIR/$pkgname-$version
1344 if [ $? -ne 0 ]; then
1345 exit 1
1346 else
1347 $rm_cmd -f $PKGFS_DESTDIR/$pkgname-$version/share/info/dir
1348 echo "==> Removed \`$pkg' symlinks from master directory."
1351 register_pkg_handler unregister $pkgname $version
1353 xstow_ignore_files="$real_xstow_ignore"
1357 # main()
1359 args=$(getopt Cc $*)
1360 [ "$?" -ne 0 ] && usage
1362 set -- $args
1363 while [ "$#" -gt 0 ]; do
1364 case "$1" in
1366 dontrm_builddir=yes
1369 config_file_specified=yes
1370 PKGFS_CONFIG_FILE="$2"
1371 shift
1374 shift
1375 break
1377 esac
1378 shift
1379 done
1381 [ "$#" -gt 2 ] && usage
1383 target="$1"
1384 if [ -z "$target" ]; then
1385 echo "*** ERROR: missing target ***"
1386 usage
1390 # Check configuration vars before anyting else, and set defaults vars.
1392 check_config_vars
1393 set_defvars
1395 # Main switch
1396 case "$target" in
1397 build)
1398 setup_tmpl $2
1399 fetch_distfiles $2
1400 if [ ! -f "$PKGFS_EXTRACT_DONE" ]; then
1401 extract_distfiles $2
1404 if [ ! -f "$PKGFS_CONFIGURE_DONE" ]; then
1405 configure_src_phase $2
1407 build_src_phase $2
1409 configure)
1410 setup_tmpl $2
1411 fetch_distfiles $2
1412 if [ ! -f "$PKGFS_EXTRACT_DONE" ]; then
1413 extract_distfiles $2
1415 configure_src_phase $2
1417 extract)
1418 setup_tmpl $2
1419 fetch_distfiles $2
1420 extract_distfiles $2
1422 fetch)
1423 setup_tmpl $2
1424 fetch_distfiles $2
1426 info)
1427 setup_tmpl $2
1428 info_tmpl $2
1430 install-destdir)
1431 install_destdir_target=yes
1432 install_pkg $2
1434 install)
1435 install_pkg $2
1437 list)
1438 list_pkgs
1440 listfiles)
1441 list_pkg_files $2
1443 remove)
1444 remove_pkg $2
1446 stow)
1447 stow_flag=yes
1448 setup_tmpl $2
1449 stow_pkg $2
1451 unstow)
1452 setup_tmpl $2
1453 unstow_pkg $2
1456 echo "*** ERROR: invalid target \`$target' ***"
1457 usage
1458 esac
1460 # Agur
1461 exit 0