curl: updated to 7.19.0 and enabled ldaps now.
[pkgfs.git] / pkgfs.sh
blobed85114794cd4e32f6a2f0fe79ee84eea1db2f14
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 a routine that checks if installed version is sufficient
32 # to satisfy the required dependency... right now it's very prone
33 # to errors and slow.
34 # - Multiple distfiles in a package.
35 # - Multiple URLs to download source distribution files.
37 # Default path to configuration file, can be overriden
38 # via the environment or command line.
40 : ${PKGFS_CONFIG_FILE:=/usr/local/etc/pkgfs.conf}
42 : ${progname:=$(basename $0)}
43 : ${topdir:=$(/bin/pwd -P 2>/dev/null)}
44 : ${fetch_cmd:=/usr/bin/ftp -a}
45 : ${cksum_cmd:=/usr/bin/cksum -a rmd160}
46 : ${awk_cmd:=/usr/bin/awk}
47 : ${mkdir_cmd:=/bin/mkdir -p}
48 : ${tar_cmd:=/usr/bin/tar}
49 : ${rm_cmd:=/bin/rm}
50 : ${mv_cmd:=/bin/mv}
51 : ${cp_cmd:=/bin/cp}
52 : ${sed_cmd=/usr/bin/sed}
53 : ${grep_cmd=/usr/bin/grep}
54 : ${gunzip_cmd:=/usr/bin/gunzip}
55 : ${bunzip2_cmd:=/usr/bin/bunzip2}
56 : ${patch_cmd:=/usr/bin/patch}
57 : ${find_cmd:=/usr/bin/find}
58 : ${file_cmd:=/usr/bin/file}
59 : ${ln_cmd:=/bin/ln}
60 : ${chmod_cmd:=/bin/chmod}
61 : ${db_cmd:=/usr/bin/db -q}
62 : ${chmod_cmd:=/bin/chmod}
63 : ${touch_cmd:=/usr/bin/touch}
65 : ${xstow_args:=-ap}
66 : ${xstow_ignore_files:=perllocal.pod} # XXX For now ignore them.
68 set_defvars()
70 # Directories
71 : ${PKGFS_TEMPLATESDIR:=$PKGFS_DISTRIBUTIONDIR/templates}
72 : ${PKGFS_DEPSDIR:=$PKGFS_DISTRIBUTIONDIR/dependencies}
73 : ${PKGFS_BUILD_DEPS_DB:=$PKGFS_DEPSDIR/build-depends.db}
74 : ${PKGFS_TMPLHELPDIR:=$PKGFS_DISTRIBUTIONDIR/helper-templates}
75 : ${PKGFS_REGPKG_DB:=$PKGFS_DESTDIR/.pkgfs-registered-pkgs.db}
77 local DDIRS="PKGFS_DEPSDIR PKGFS_TEMPLATESDIR PKGFS_TMPLHELPDIR"
78 for i in ${DDIRS}; do
79 eval val="\$$i"
80 if [ ! -d "$val" ]; then
81 echo "**** ERROR: cannot find $i, aborting ***"
82 exit 1
84 done
87 usage()
89 cat << _EOF
90 $progname: [-C] [-c <config_file>] <target> [package_name]
92 Targets
93 build Builds a package, only build phase is done.
94 configure Configure a package, only configure phase is done.
95 extract Extract distribution file(s) into build directory.
96 fetch Download distribution file(s).
97 info Show information about <package_name>.
98 install build + configure + install into destdir + stow.
99 list Lists all currently ``stowned´´ packages.
100 remove Remove package completely (unstow + remove data)
101 stow Create links in master directory.
102 unstow Remove links in master directory.
104 Options
105 -C Do not remove build directory after successful installation.
106 -c Path to global configuration file:
107 if not specified /usr/local/etc/pkgfs.conf is used.
108 _EOF
109 exit 1
112 check_path()
114 eval local orig="$1"
116 case "$orig" in
120 orig="${orig%/}"
123 orig="$topdir/${orig%/}"
125 esac
127 path_fixed="$orig"
130 run_file()
132 local file="$1"
134 check_path "$file"
135 . $path_fixed
139 # This function merges two GNU info dirs into one and puts the result
140 # into PKGFS_MASTERDIR/share/info/dir.
142 merge_infodir_tmpl()
144 local pkgname="$1"
145 local merge_info_cmd="$PKGFS_MASTERDIR/bin/merge-info"
147 [ -z "$pkgname" -o ! -r "$PKGFS_MASTERDIR/share/info/dir" \
148 -o ! -r "$PKGFS_DESTDIR/$pkgname/share/info/dir" ] && return 1
150 $merge_info_cmd -d $PKGFS_MASTERDIR/share/info/dir \
151 $PKGFS_DESTDIR/$pkgname/share/info/dir -o \
152 $PKGFS_MASTERDIR/share/info/dir.new
153 if [ $? -ne 0 ]; then
154 echo -n "*** WARNING: there was an error merging info dir from"
155 echo " $pkgname, aborting ***"
156 return 1
159 $mv_cmd -f $PKGFS_MASTERDIR/share/info/dir.new \
160 $PKGFS_MASTERDIR/share/info/dir
164 # Shows info about a template.
166 info_tmpl()
168 echo "pkgname: $pkgname"
169 echo "version: $version"
170 for i in "${distfiles}"; do
171 [ -n "$i" ] && echo "distfile: $i"
172 done
173 echo "URL: $url"
174 echo "maintainer: $maintainer"
175 [ -n $checksum ] && echo "checksum: $checksum"
176 echo "build_style: $build_style"
177 echo "short_desc: $short_desc"
178 echo "$long_desc"
179 echo
180 check_build_depends_pkg $pkgname-$version
181 if [ $? -eq 0 ]; then
182 local list="$($db_cmd -V btree $PKGFS_BUILD_DEPS_DB $pkgname)"
183 echo "This package requires the following dependencies to be built:"
184 for i in ${list}; do
185 echo " $i"
186 done
191 # Checks that all required variables specified in the configuration
192 # file are properly working.
194 check_config_vars()
196 local cffound=
198 if [ -z "$config_file_specified" ]; then
199 config_file_paths="$PKGFS_CONFIG_FILE ./pkgfs.conf"
200 for f in $config_file_paths; do
201 [ -f $f ] && PKGFS_CONFIG_FILE=$f && \
202 cffound=yes && break
203 done
204 if [ -z "$cffound" ]; then
205 echo -n "*** ERROR: config file not specified "
206 echo "and not in default location or current dir ***"
207 exit 1
211 run_file ${PKGFS_CONFIG_FILE}
212 PKGFS_CONFIG_FILE=$path_fixed
214 if [ ! -f "$PKGFS_CONFIG_FILE" ]; then
215 echo -n "*** ERROR: cannot find configuration file: "
216 echo "'$PKGFS_CONFIG_FILE' ***"
217 exit 1
220 local PKGFS_VARS="PKGFS_MASTERDIR PKGFS_DESTDIR PKGFS_BUILDDIR \
221 PKGFS_SRCDISTDIR PKGFS_SYSCONFDIR"
223 for f in ${PKGFS_VARS}; do
224 eval val="\$$f"
225 if [ -z "$val" ]; then
226 echo -n "**** ERROR: '$f' not set in configuration "
227 echo "file, aborting ***"
228 exit 1
231 if [ ! -d "$val" ]; then
232 $mkdir_cmd "$val"
233 if [ "$?" -ne 0 ]; then
234 echo -n "*** ERROR: couldn't create '$f'"
235 echo "directory, aborting ***"
236 exit 1
239 done
243 # Resets all vars used by a template.
245 reset_tmpl_vars()
247 local TMPL_VARS="pkgname extract_sufx distfiles url configure_args \
248 make_build_args make_install_args build_style \
249 short_desc maintainer long_desc checksum wrksrc \
250 patch_files configure_env make_cmd pkgconfig_override \
251 configure_env make_env run_stuff_before run_stuff_after \
252 run_stuff_before_configure_file run_stuff_before_build_file \
253 run_stuff_before_install_file run_stuff_after_install \
254 run_stuff_after_install_file 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 for i in "$run_stuff_before"; do
596 if [ "$i" = "configure" ]; then
597 [ -f $run_stuff_before_configure_file ] && \
598 . $run_stuff_before_configure_file
599 [ -n "$run_stuff_before_configure_cmd" ] && \
600 ${run_stuff_before_configure_cmd}
602 done
604 # Export configure_env vars.
605 for f in ${configure_env}; do
606 export "$f"
607 done
609 set_build_vars
612 # Packages using GNU autoconf
614 if [ "$build_style" = "gnu_configure" ]; then
615 cd $wrksrc
617 # Pass consistent arguments to not have unexpected
618 # surprises later.
620 ./configure \
621 --prefix="$PKGFS_MASTERDIR" \
622 --mandir="$PKGFS_DESTDIR/$pkgname-$version/man" \
623 --infodir="$PKGFS_DESTDIR/$pkgname-$version/share/info" \
624 --sysconfdir="$PKGFS_SYSCONFDIR" \
625 ${configure_args}
628 # Packages using propietary configure scripts.
630 elif [ "$build_style" = "configure" ]; then
631 cd $wrksrc
632 if [ -n "$configure_script" ]; then
633 ./$configure_script ${configure_args}
634 else
635 ./configure ${configure_args}
638 # Packages that are perl modules and use Makefile.PL files.
639 # They are all handled by the helper perl-module.sh.
641 elif [ "$build_style" = "perl_module" ]; then
642 . $PKGFS_TMPLHELPDIR/perl-module.sh
643 perl_module_build $pkgname
646 # Packages with BSD or GNU Makefiles are easy, just skip
647 # the configure stage and proceed.
649 elif [ "$build_style" = "bsd_makefile" -o \
650 "$build_style" = "gnu_makefile" ]; then
652 cd $wrksrc
654 # Unknown build_style type won't work :-)
656 else
657 echo "*** ERROR unknown build_style \`$build_style' ***"
658 exit 1
661 if [ "$build_style" != "perl_module" -a "$?" -ne 0 ]; then
662 echo "*** ERROR building (configure state) \`$pkg' ***"
663 exit 1
666 # unset configure_env vars.
667 for f in ${configure_env}; do
668 unset eval ${f%=*}
669 done
671 $touch_cmd -f $PKGFS_CONFIGURE_DONE
675 # Runs the "build" phase for a pkg. This builds the binaries and other
676 # related stuff.
678 build_src_phase()
680 local pkgparam="$1"
681 local pkg="$pkgname-$version"
683 [ -z $pkgparam ] && [ -z $pkgname -o -z $version ] && return 1
686 # There's nothing of interest if we are a meta template.
688 [ "$build_style" = "meta-template" ] && return 0
690 if [ ! -d $wrksrc ]; then
691 echo "*** ERROR: unexistent build directory \`$wrksrc' ***"
692 exit 1
695 cd $wrksrc || exit 1
697 echo "=> Running \`\`build´´ phase for \`$pkg'."
700 # Assume BSD make if make_cmd not set in template.
702 if [ -z "$make_cmd" ]; then
703 make_cmd="/usr/bin/make"
706 # Fixup libtool script if necessary
707 fixup_tmpl_libtool
710 # Run template stuff before building.
712 for i in ${run_stuff_before}; do
713 if [ "$i" = "build" ]; then
714 [ -f $run_stuff_before_build_file ] && \
715 . $run_stuff_before_build_file
716 [ -n "$run_stuff_before_build_cmd" ] && \
717 ${run_stuff_before_build_cmd}
719 done
721 [ -z "$make_build_target" ] && make_build_target=
722 [ -n "$PKGFS_MAKEJOBS" ] && PKGFS_MAKEJOBS="-j$PKGFS_MAKEJOBS"
724 # Export make_env vars.
725 for f in ${make_env}; do
726 export "$f"
727 done
730 # Build package via make.
732 ${make_cmd} ${PKGFS_MAKEJOBS} ${make_build_args} ${make_build_target}
733 if [ "$?" -ne 0 ]; then
734 echo "*** ERROR building (make stage) \`$pkg' ***"
735 exit 1
739 # Run template stuff before installing.
741 for i in ${run_stuff_before}; do
742 if [ "$i" = "install" ]; then
743 [ -f $run_stuff_before_install_file ] && \
744 . $run_stuff_before_install_file
745 [ -n "$run_stuff_before_install_cmd" ] && \
746 ${run_stuff_before_install_cmd}
748 done
750 $touch_cmd -f $PKGFS_BUILD_DONE
754 # Runs the "install" phase for a pkg. This consists in installing package
755 # into the destination directory.
757 install_src_phase()
759 local pkg="$1"
761 [ -z $pkg ] && [ -z $pkgname ] && return 1
763 [ -z "$make_install_target" ] && make_install_target=install
766 # There's nothing we can do if we are a meta template.
768 [ "$build_style" = "meta-template" ] && return 0
770 if [ ! -d $wrksrc ]; then
771 echo "*** ERROR: unexistent build directory \`$wrksrc' ***"
772 exit 1
775 echo "=> Running \`\`install´´ phase for: \`$pkgname-$version´."
778 # Install package via make.
780 ${make_cmd} ${make_install_args} ${make_install_target} \
781 prefix="$PKGFS_DESTDIR/$pkgname-$version"
782 if [ "$?" -ne 0 ]; then
783 echo "*** ERROR instaling \`$pkgname-$version' ***"
784 exit 1
787 # Unset make_env vars.
788 for f in ${make_env}; do
789 unset eval ${f%=*}
790 done
793 # Run template stuff after installing.
795 for i in ${run_stuff_after}; do
796 if [ "$i" = "install" ]; then
797 [ -f $run_stuff_after_install_file ] && \
798 . $run_stuff_after_install_file
799 [ -n "$run_stuff_after_install_cmd" ] && \
800 ${run_stuff_after_install_cmd}
802 done
805 # Transform pkg-config files if requested by template.
807 for i in ${pkgconfig_override}; do
808 local tmpf="$PKGFS_DESTDIR/$pkgname-$version/lib/pkgconfig/$i"
809 [ -f "$tmpf" ] && \
810 [ -f $PKGFS_TMPLHELPDIR/pkg-config-transform.sh ] && \
811 . $PKGFS_TMPLHELPDIR/pkg-config-transform.sh && \
812 pkgconfig_transform_file $tmpf
813 done
815 # Unset build vars.
816 unset_build_vars
818 echo "==> Installed \`$pkgname-$version' into $PKGFS_DESTDIR."
820 $touch_cmd -f $PKGFS_INSTALL_DONE
823 # Remove $wrksrc if -C not specified.
825 if [ -d "$wrksrc" -a -z "$dontrm_builddir" ]; then
826 $rm_cmd -rf $wrksrc
827 [ "$?" -eq 0 ] && \
828 echo "=> Removed \`$pkgname-$version' build directory."
831 cd $PKGFS_BUILDDIR
835 # Registers or unregisters a package from the db file.
837 register_pkg_handler()
839 local action="$1"
840 local pkg="$2"
841 local version="$3"
843 [ -z "$action" -o -z "$pkg" -o -z "$version" ] && return 1
845 if [ "$action" = "register" ]; then
846 $db_cmd -w btree $PKGFS_REGPKG_DB $pkg $version 2>&1 >/dev/null
847 if [ "$?" -ne 0 ]; then
848 echo -n "*** ERROR: couldn't register \`$pkg'"
849 echo " in db file ***"
850 exit 1
852 elif [ "$action" = "unregister" ]; then
853 $db_cmd -d btree $PKGFS_REGPKG_DB $pkg 2>&1 >/dev/null
854 if [ "$?" -ne 0 ]; then
855 echo -n "*** ERROR: \`$pkg' not registered "
856 echo "in db file? ***"
857 exit 1
859 else
860 return 1
865 # Recursive function that founds dependencies in all required
866 # packages.
868 add_dependency_tolist()
870 local curpkg="$1"
872 [ -z "$curpkg" ] && return 1
873 [ -n "$prev_pkg" ] && curpkg=$prev_pkg
875 for i in $($db_cmd -V btree $PKGFS_BUILD_DEPS_DB ${curpkg%-[0-9]*.*}); do
877 # Check if dep already installed.
879 if [ -r "$PKGFS_REGPKG_DB" ]; then
880 check_installed_pkg $i ${i##[aA-zZ]*-}
882 # If dep is already installed, put it on the
883 # installed deps list and continue.
885 if [ $? -eq 0 ]; then
886 installed_deps_list="$i $installed_deps_list"
887 continue
890 deps_list="$i $deps_list"
891 [ -n "$prev_pkg" ] && unset prev_pkg
893 # Check if dependency needs more deps.
895 check_build_depends_pkg ${i%-[0-9]*.*}
896 if [ $? -eq 0 ]; then
897 add_dependency_tolist $i
898 prev_pkg="$i"
901 done
905 # Removes duplicate deps in the installed or not installed list.
907 find_dupdeps_inlist()
909 local action="$1"
910 local tmp_list=
911 local dup=
913 [ -z "$action" ] && return 1
915 case "$action" in
916 installed)
917 list=$installed_deps_list
919 notinstalled)
920 list=$deps_list
923 return 1
925 esac
927 for f in $list; do
928 if [ -z "$tmp_list" ]; then
929 tmp_list="$f"
930 else
931 for i in $tmp_list; do
932 [ "$f" = "$i" ] && dup=yes
933 done
935 [ -z "$dup" ] && tmp_list="$tmp_list $f"
936 unset dup
938 done
940 case "$action" in
941 installed)
942 installed_deps_list="$tmp_list"
944 notinstalled)
945 deps_list="$tmp_list"
948 return 1
950 esac
954 # Installs all dependencies required by a package.
956 install_dependencies_pkg()
958 local pkg="$1"
959 deps_list=
960 installed_deps_list=
962 [ -z "$pkg" ] && return 1
964 doing_deps=true
966 add_dependency_tolist $pkg
967 find_dupdeps_inlist installed
968 find_dupdeps_inlist notinstalled
970 [ -z "$deps_list" -a -z "$installed_deps_list" ] && return 0
972 echo "==> Required dependencies for $(basename $pkg):"
973 for i in ${installed_deps_list}; do
974 fpkg="$($db_cmd -O '-' btree $PKGFS_REGPKG_DB ${i%-[0-9]*.*})"
975 echo " $i: found $fpkg."
976 done
978 for i in ${deps_list}; do
979 echo " $i: not installed."
980 done
982 for i in ${deps_list}; do
983 # skip dup deps
984 check_installed_pkg $i ${i##[aA-zZ]*-}
985 [ $? -eq 0 ] && continue
986 # continue installing deps
987 echo "==> Installing \`$pkg´ dependency: \`$i´."
988 install_pkg ${i%-[0-9]*.*}
989 done
991 unset installed_deps_list
992 unset deps_list
995 install_builddeps_required_pkg()
997 local pkg="$1"
999 [ -z "$pkg" ] && return 1
1001 for dep in $($db_cmd -V btree $PKGFS_BUILD_DEPS_DB ${pkg%-[0-9]*.*}); do
1002 check_installed_pkg $dep ${dep##[aA-zZ]*-}
1003 if [ $? -ne 0 ]; then
1004 echo "==> Installing \`$pkg´ dependency: $dep."
1005 install_pkg ${dep%-[0-9]*.*}
1007 done
1011 # Checks the registered pkgs db file and returns 0 if a pkg that satisfies
1012 # the minimal required version is there, or 1 otherwise.
1014 check_installed_pkg()
1016 local pkg="$1"
1017 local reqver="$2"
1018 local iver=
1020 [ -z "$pkg" -o -z "$reqver" -o ! -r $PKGFS_REGPKG_DB ] && return 1
1022 run_file $PKGFS_TEMPLATESDIR/${pkg%-[0-9]*.*}.tmpl
1024 reqver="$(echo $reqver | $sed_cmd 's|[[:punct:]]||g;s|[[:alpha:]]||g')"
1026 $db_cmd -K btree $PKGFS_REGPKG_DB $pkgname 2>&1 >/dev/null
1027 if [ $? -eq 0 ]; then
1029 # Package is installed, let's check the version.
1031 iver="$($db_cmd -V btree $PKGFS_REGPKG_DB $pkgname)"
1032 if [ -n "$iver" ]; then
1034 # As shell only supports decimal arith expressions,
1035 # we simply remove anything except the numbers.
1036 # It's not optimal and may fail, but it is enough
1037 # for now.
1039 iver="$(echo $iver | $sed_cmd 's|[[:punct:]]||g;s|[[:alpha:]]||g')"
1040 if [ "$iver" -eq "$reqver" \
1041 -o "$iver" -gt "$reqver" ]; then
1042 return 0
1047 return 1
1051 # Checks the build depends db file and returns 0 if pkg has dependencies,
1052 # otherwise returns 1.
1054 check_build_depends_pkg()
1056 local pkg="$1"
1058 [ -z $pkg -o ! -r $PKGFS_BUILD_DEPS_DB ] && return 1
1060 $db_cmd -V btree $PKGFS_BUILD_DEPS_DB ${pkg%-[0-9]*.*} 2>&1 >/dev/null
1061 return $?
1065 # Installs a pkg by reading its build template file.
1067 install_pkg()
1069 local pkg=
1070 local curpkgn="$1"
1072 local cur_tmpl="$PKGFS_TEMPLATESDIR/$curpkgn.tmpl"
1073 if [ -z $cur_tmpl -o ! -f $cur_tmpl ]; then
1074 echo "*** ERROR: cannot find \`$cur_tmpl´ template file ***"
1075 exit 1
1078 reset_tmpl_vars
1079 run_file $cur_tmpl
1080 pkg="$curpkgn-$version"
1083 # If we are the originator package save the path this template in
1084 # other var for future use.
1086 [ -z "$origin_tmpl" ] && origin_tmpl=$pkgname
1089 # Install xstow if it's not there.
1091 install_xstow_pkg
1094 # We are going to install a new package.
1096 prepare_tmpl
1099 # Install dependencies required by this package.
1101 if [ -z "$doing_deps" ]; then
1102 install_dependencies_pkg $pkg
1104 # At this point all required deps are installed, and
1105 # only remaining is the origin template; install it.
1107 unset doing_deps
1108 reset_tmpl_vars
1109 setup_tmpl $curpkgn
1113 # Fetch, extract, build and install into the destination directory.
1115 fetch_distfiles
1117 if [ ! -f "$PKGFS_EXTRACT_DONE" ]; then
1118 extract_distfiles
1121 if [ ! -f "$PKGFS_CONFIGURE_DONE" ]; then
1122 configure_src_phase
1125 if [ ! -f "$PKGFS_BUILD_DONE" ]; then
1126 build_src_phase
1129 install_src_phase
1132 # Just announce that meta-template is installed and exit.
1134 if [ "$build_style" = "meta-template" ]; then
1135 register_pkg_handler register $pkgname $version
1136 echo "==> Installed meta-template \`$pkg'."
1137 return 0
1140 stow_pkg $pkg
1144 # Installs and stows the "xstow" package.
1146 install_xstow_pkg()
1148 [ -x "$PKGFS_XSTOW_CMD" ] && return 0
1150 echo "=> xstow application not found, will install it now."
1152 reset_tmpl_vars
1153 setup_tmpl xstow
1154 fetch_distfiles
1156 if [ ! -f "$PKGFS_EXTRACT_DONE" ]; then
1157 extract_distfiles
1160 if [ ! -f "$PKGFS_CONFIGURE_DONE" ]; then
1161 configure_src_phase
1164 if [ ! -f "$PKGFS_BUILD_DONE" ]; then
1165 build_src_phase
1168 install_src_phase
1170 PKGFS_XSTOW_CMD="$PKGFS_DESTDIR/$pkgname-$version/bin/xstow"
1171 stow_pkg $pkgname-$version
1174 # Continue with package that called us.
1176 run_file $PKGFS_TEMPLATESDIR/$origin_tmpl.tmpl
1180 # Lists all currently installed packages.
1182 list_pkgs()
1184 if [ ! -r "$PKGFS_REGPKG_DB" ]; then
1185 echo "=> No packages registered or missing register db file."
1186 exit 0
1189 for i in $($db_cmd -K btree $PKGFS_REGPKG_DB); do
1190 # Run file to get short_desc and print something useful
1191 run_file $PKGFS_TEMPLATESDIR/$i.tmpl
1192 echo "$i-$version $short_desc"
1193 reset_tmpl_vars
1194 done
1198 # Removes a currently installed package (unstow + removed from destdir).
1200 remove_pkg()
1202 local pkg="$1"
1204 if [ -z "$pkg" ]; then
1205 echo "*** ERROR: unexistent package, aborting ***"
1206 exit 1
1209 if [ ! -f "$PKGFS_TEMPLATESDIR/$pkg.tmpl" ]; then
1210 echo "*** ERROR: cannot find template file ***"
1211 exit 1
1214 run_file $PKGFS_TEMPLATESDIR/$pkg.tmpl
1217 # If it's a meta-template, just unregister it from the db.
1219 if [ "$build_style" = "meta-template" ]; then
1220 register_pkg_handler unregister $pkgname $version
1221 [ $? -eq 0 ] && \
1222 echo "=> Removed meta-template \`$pkg'."
1223 return $?
1226 if [ ! -d "$PKGFS_DESTDIR/$pkg-$version" ]; then
1227 echo "*** ERROR: cannot find package on $PKGFS_DESTDIR ***"
1228 exit 1
1231 unstow_pkg $pkg
1232 $rm_cmd -rf $PKGFS_DESTDIR/$pkg-$version
1233 return $?
1237 # Stows a currently installed package, i.e creates the links
1238 # on the master directory.
1240 stow_pkg()
1242 local pkg="$1"
1243 local infodir_pkg="share/info/dir"
1244 local infodir_master="$PKGFS_MASTERDIR/share/info/dir"
1245 local real_xstowargs="$xstow_args"
1246 local real_xstow_ignore="$xstow_ignore_files"
1248 [ -z "$pkg" ] && return 2
1250 if [ -n "$stow_flag" ]; then
1251 pkg=$PKGFS_TEMPLATESDIR/$pkg.tmpl
1252 run_file $pkg
1253 pkg=$pkgname-$version
1255 # You cannot stow a meta-template.
1257 [ "$build_style" = "meta-template" ] && return 0
1260 if [ -r "$PKGFS_DESTDIR/$pkg/$infodir_pkg" ]; then
1261 merge_infodir_tmpl $pkg
1264 if [ -r "$PKGFS_DESTDIR/$pkg/$infodir_pkg" \
1265 -a -r "$infodir_master" ]; then
1266 xstow_args="$xstow_args -i-file-in-dir $infodir_pkg"
1269 if [ -n "$ignore_files" ]; then
1270 xstow_ignore_files="$xstow_ignore_files $ignore_files"
1273 $PKGFS_XSTOW_CMD -ignore "${xstow_ignore_files}" ${xstow_args} \
1274 -pd-targets $PKGFS_MASTERDIR \
1275 -dir $PKGFS_DESTDIR -target $PKGFS_MASTERDIR \
1276 $PKGFS_DESTDIR/$pkg
1277 if [ "$?" -ne 0 ]; then
1278 echo "*** ERROR: couldn't create symlinks for \`$pkg' ***"
1279 exit 1
1280 else
1281 echo "==> Created \`$pkg' symlinks into master directory."
1284 register_pkg_handler register $pkgname $version
1287 # Run template postinstall helpers if requested.
1289 if [ "$pkgname" != "${pkg%%-$version}" ]; then
1290 run_file $PKGFS_TEMPLATESDIR/${pkg%%-$version}.tmpl
1293 for i in ${postinstall_helpers}; do
1294 local pihf="$PKGFS_TMPLHELPDIR/$i"
1295 [ -f "$pihf" ] && . $pihf
1296 done
1298 xstow_ignore_files="$real_xstow_ignore"
1299 xstow_args="$real_xstowargs"
1303 # Unstows a currently stowned package, i.e removes its links
1304 # from the master directory.
1306 unstow_pkg()
1308 local pkg="$1"
1309 local real_xstow_ignore="$xstow_ignore_files"
1311 if [ -z "$pkg" ]; then
1312 echo "*** ERROR: template wasn't specified? ***"
1313 exit 1
1316 if [ "$pkg" = "xstow" ]; then
1317 echo "*** INFO: You aren't allowed to unstow \`$pkg'."
1318 exit 1
1321 run_file $PKGFS_TEMPLATESDIR/$pkg.tmpl
1324 # You cannot unstow a meta-template.
1326 [ "$build_style" = "meta-template" ] && return 0
1328 if [ -n "$ignore_files" ]; then
1329 xstow_ignore_files="$xstow_ignore_files $ignore_files"
1332 $PKGFS_XSTOW_CMD -dir $PKGFS_DESTDIR -target $PKGFS_MASTERDIR \
1333 -D -i-file-in-dir share/info/dir -ignore \
1334 "${xstow_ignore_files}" $PKGFS_DESTDIR/$pkgname-$version
1335 if [ $? -ne 0 ]; then
1336 exit 1
1337 else
1338 $rm_cmd -f $PKGFS_DESTDIR/$pkgname-$version/share/info/dir
1339 echo "==> Removed \`$pkg' symlinks from master directory."
1342 register_pkg_handler unregister $pkgname $version
1344 xstow_ignore_files="$real_xstow_ignore"
1348 # main()
1350 args=$(getopt Cc $*)
1351 [ "$?" -ne 0 ] && usage
1353 set -- $args
1354 while [ "$#" -gt 0 ]; do
1355 case "$1" in
1357 dontrm_builddir=yes
1360 config_file_specified=yes
1361 PKGFS_CONFIG_FILE="$2"
1362 shift
1365 shift
1366 break
1368 esac
1369 shift
1370 done
1372 [ "$#" -gt 2 ] && usage
1374 target="$1"
1375 if [ -z "$target" ]; then
1376 echo "*** ERROR: missing target ***"
1377 usage
1381 # Check configuration vars before anyting else, and set defaults vars.
1383 check_config_vars
1384 set_defvars
1386 # Main switch
1387 case "$target" in
1388 build)
1389 setup_tmpl $2
1390 fetch_distfiles $2
1391 if [ ! -f "$PKGFS_EXTRACT_DONE" ]; then
1392 extract_distfiles $2
1395 if [ ! -f "$PKGFS_CONFIGURE_DONE" ]; then
1396 configure_src_phase $2
1398 build_src_phase $2
1400 configure)
1401 setup_tmpl $2
1402 fetch_distfiles $2
1403 if [ ! -f "$PKGFS_EXTRACT_DONE" ]; then
1404 extract_distfiles $2
1406 configure_src_phase $2
1408 extract)
1409 setup_tmpl $2
1410 fetch_distfiles $2
1411 extract_distfiles $2
1413 fetch)
1414 setup_tmpl $2
1415 fetch_distfiles $2
1417 info)
1418 setup_tmpl $2
1419 info_tmpl $2
1421 install)
1422 install_pkg $2
1424 list)
1425 list_pkgs
1427 remove)
1428 remove_pkg $2
1430 stow)
1431 stow_flag=yes
1432 setup_tmpl $2
1433 stow_pkg $2
1435 unstow)
1436 setup_tmpl $2
1437 unstow_pkg $2
1440 echo "*** ERROR: invalid target \`$target' ***"
1441 usage
1442 esac
1444 # Agur
1445 exit 0