Fixup fromcvs/togit conversion
[minix-pkgsrc.git] / pkgtools / pkg_comp / files / pkg_comp.sh
blobcb67f4cc9a8241488897ca05b35db920f0d68763
1 #!/bin/sh
3 # $NetBSD: pkg_comp.sh,v 1.40 2012/02/27 22:42:27 jmmv Exp $
5 # pkg_comp - Build packages inside a clean chroot environment
6 # Copyright (c) 2002, 2003, 2004, 2005 Julio M. Merino Vidal <jmmv@NetBSD.org>
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 # 1. Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in
15 # the documentation and/or other materials provided with the
16 # distribution.
17 # 3. Neither the name of author nor the names of its contributors may
18 # be used to endorse or promote products derived from this software
19 # without specific prior written permission.
21 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
34 ProgName="`basename $0`"
36 # ----------------------------------------------------------------------
37 # Default environment values and functions
38 # ----------------------------------------------------------------------
40 # USE_GCC3, CFLAGS, CPPFLAGS and CXXFLAGS are ommited from _MKCONF_VARS
41 # as they require special handling.
42 _MKCONF_VARS="WRKDIR_BASENAME MKOBJDIRS BSDSRCDIR WRKOBJDIR DISTDIR PACKAGES \
43 PKG_DEVELOPER CLEANDEPENDS LOCALBASE PKG_SYSCONFBASE \
44 CFLAGS CPPFLAGS CXXFLAGS USE_AUDIT_PACKAGES PKGVULNDIR \
45 USE_XPKGWEDGE PKGSRC_COMPILER \
46 LIBKVER_STANDALONE_PREFIX PKG_DBDIR"
48 _TEMPLATE_VARS="DESTDIR ROOTSHELL COPYROOTCFG BUILD_TARGET DISTRIBDIR SETS \
49 SETS_X11 REAL_SRC REAL_SRC_OPTS REAL_PKGSRC \
50 REAL_PKGSRC_OPTS REAL_DISTFILES REAL_DISTFILES_OPTS \
51 REAL_PACKAGES REAL_PACKAGES_OPTS REAL_PKGVULNDIR \
52 NETBSD_RELEASE MAKEROOT_HOOKS MOUNT_HOOKS UMOUNT_HOOKS \
53 SYNC_UMOUNT AUTO_TARGET AUTO_PACKAGES BUILD_PACKAGES \
54 REAL_CCACHE LIBKVER_STANDALONE_PREFIX GENERATE_PKG_SUMMARY"
56 _BUILD_RESUME=
58 # env_clean
60 # Sets all variables that may appear in the config file to the null
61 # string, so that we know the environment is in a consistent state.
63 env_clean()
65 MKCONF_VARS=""
66 TEMPLATE_VARS=""
67 for var in ${_MKCONF_VARS} ${_TEMPLATE_VARS}; do
68 eval $var=\"\"
69 done
72 # env_setdefaults
74 # Sets several reasonable defaults for many variables we will use.
75 # Also checks for deprecated variables and warns the user about them.
76 # To be called after reading the configuration file.
78 env_setdefaults()
80 MKCONF_VARS="$MKCONF_VARS ${_MKCONF_VARS}"
81 TEMPLATE_VARS="$TEMPLATE_VARS ${_TEMPLATE_VARS}"
83 # Default values for variables that will be written to mk.conf.
84 : ${WRKDIR_BASENAME:=default}
85 : ${MKOBJDIRS:=yes}
86 : ${BSDSRCDIR:=/usr/src}
87 : ${WRKOBJDIR:=/pkg_comp/obj/pkgsrc}
88 : ${DISTDIR:=/pkg_comp/distfiles}
89 : ${PACKAGES:=/pkg_comp/packages}
90 : ${PKG_DEVELOPER:=yes}
91 : ${CLEANDEPENDS:=yes}
92 : ${LOCALBASE:=/usr/pkg}
93 : ${PKG_SYSCONFBASE:=/usr/pkg/etc}
94 : ${CFLAGS:=}
95 : ${CPPFLAGS:=}
96 : ${CXXFLAGS:=}
97 : ${USE_GCC3:=no}
98 : ${USE_AUDIT_PACKAGES:=yes}
99 : ${PKGVULNDIR:=/usr/pkg/share}
100 : ${USE_XPKGWEDGE:=yes}
101 : ${PKGSRC_COMPILER:=gcc}
102 : ${PKG_DBDIR:=/var/db/pkg}
104 # Default values for global variables used in the script.
105 : ${DESTDIR:=/var/chroot/pkg_comp/default}
106 : ${ROOTSHELL:=/bin/ksh}
107 : ${COPYROOTCFG:=no}
108 : ${AUTO_TARGET:=package}
109 : ${BUILD_TARGET:=package}
110 : ${DISTRIBDIR:=/var/pub/NetBSD}
111 : ${SETS:=base.tgz comp.tgz etc.tgz kern-GENERIC.tgz text.tgz}
112 : ${SETS_X11:=xbase.tgz xcomp.tgz xetc.tgz xfont.tgz xserver.tgz}
113 : ${REAL_SRC:=/usr/src}
114 : ${REAL_SRC_OPTS:=-t null -o ro}
115 : ${REAL_PKGSRC:=/usr/pkgsrc}
116 : ${REAL_PKGSRC_OPTS:=-t null -o ro}
117 : ${REAL_DISTFILES:=/usr/pkgsrc/distfiles}
118 : ${REAL_DISTFILES_OPTS:=-t null -o rw}
119 : ${REAL_PACKAGES:=/usr/pkgsrc/packages}
120 : ${REAL_PACKAGES_OPTS:=-t null -o rw}
121 : ${REAL_PKGVULNDIR:=/usr/pkgsrc/distfiles}
122 : ${NETBSD_RELEASE:=no}
123 : ${LIBKVER_STANDALONE_PREFIX:=/libkver}
124 : ${MAKEROOT_HOOKS:=}
125 : ${MOUNT_HOOKS:=}
126 : ${UMOUNT_HOOKS:=}
127 : ${SYNC_UMOUNT:=no}
128 : ${REAL_CCACHE:=}
129 : ${GENERATE_PKG_SUMMARY:=yes}
131 if [ -n "${MAKE_PACKAGES}" ]; then
132 warn "MAKE_PACKAGES is deprecated; use {AUTO,BUILD}_PACKAGES instead."
133 : ${AUTO_PACKAGES:=${MAKE_PACKAGES}}
134 : ${BUILD_PACKAGES:=${MAKE_PACKAGES}}
137 if [ -n "${MOUNT_SCRIPT}" ]; then
138 warn "MOUNT_SCRIPT is deprecated; use MOUNT_HOOKS instead."
139 : ${MOUNT_HOOKS:=${MOUNT_SCRIPT}}
142 if [ -n "${UMOUNT_SCRIPT}" ]; then
143 warn "UMOUNT_SCRIPT is deprecated; use UMOUNT_HOOKS instead."
144 : ${UMOUNT_HOOKS:=${UMOUNT_SCRIPT}}
148 # ----------------------------------------------------------------------
149 # Misc functions
150 # ----------------------------------------------------------------------
152 # err msg
154 # Shows the given error message and exit.
156 err()
158 echo "$ProgName: $1" 1>&2
159 exit 1
162 # warn msg
164 # Shows the given warning message.
166 warn()
168 echo "$ProgName: $1" 1>&2
171 # usage
173 # Shows an usage message and exits.
175 usage()
177 echo "usage: $ProgName [-(c|C) conf_file] [-Nn] target [pkg_names]" 1>&2
178 exit 1
181 # copy_vulnerabilities
183 # If USE_AUDIT_PACKAGES is set to 'yes', this function copies the
184 # system-wide pkg-vulnerabilities file inside the sandbox.
186 copy_vulnerabilities()
188 if [ "$USE_AUDIT_PACKAGES" = "yes" ]; then
189 echo "PKG_COMP ==> Installing new \`pkg-vulnerabilities' file"
190 if [ ! -f "$REAL_PKGVULNDIR/pkg-vulnerabilities" ]; then
191 echo "$REAL_PKGVULNDIR/pkg-vulnerabilities not found."
192 else
193 mkdir -p $DESTDIR/$PKGVULNDIR
194 cp $REAL_PKGVULNDIR/pkg-vulnerabilities $DESTDIR/$PKGVULNDIR
199 # init_script filename
201 # Create a script that will be used within the sandbox and write some
202 # common content to it.
204 init_script()
206 rm -f $1
207 cat >$1 <<EOF
208 #!/bin/sh
209 # Generated by pkg_comp on `date`
211 if [ -f ${LIBKVER_STANDALONE_PREFIX}/lib/libkver.so ]; then
212 LD_PRELOAD=${LIBKVER_STANDALONE_PREFIX}/lib/libkver.so; export LD_PRELOAD
218 # ----------------------------------------------------------------------
219 # Filesystem functions
220 # ----------------------------------------------------------------------
222 # fsmount
224 # Mounts all sandboxed filesystems, if they are not mounted yet.
226 fsmount()
228 echo "PKG_COMP ==> Mounting sandboxed filesystems"
229 if [ -f $fsstate ]; then
230 count=`cat $fsstate`
231 count=$(($count + 1))
232 echo "$count" > $fsstate
233 echo "Already mounted (maybe by another pkg_comp process?)"
234 return
235 else
236 echo "1" > $fsstate
239 if [ -n "$REAL_SRC" ]; then
240 if [ ! -d "$REAL_SRC" ]; then
241 echo " failed."
242 fsumount
243 err "REAL_SRC $REAL_SRC disappeared"
245 mount $REAL_SRC_OPTS $REAL_SRC $DESTDIR/usr/src
248 if [ -n "$REAL_PKGSRC" ]; then
249 if [ ! -d "$REAL_PKGSRC" ]; then
250 echo " failed."
251 fsumount
252 err "REAL_PKGSRC $REAL_PKGSRC disappeared"
254 mount $REAL_PKGSRC_OPTS $REAL_PKGSRC $DESTDIR/usr/pkgsrc
257 if [ -n "$REAL_DISTFILES" ]; then
258 if [ ! -d "$REAL_DISTFILES" ]; then
259 echo " failed."
260 fsumount
261 err "REAL_DISTFILES $REAL_DISTFILES disappeared"
263 mount $REAL_DISTFILES_OPTS $REAL_DISTFILES $DESTDIR/pkg_comp/distfiles
266 if [ -n "$REAL_PACKAGES" ]; then
267 if [ ! -d "$REAL_PACKAGES" ]; then
268 echo " failed."
269 fsumount
270 err "REAL_PACKAGES $REAL_PACKAGES disappeared"
272 mount $REAL_PACKAGES_OPTS $REAL_PACKAGES $DESTDIR/pkg_comp/packages
275 if [ -n "${REAL_CCACHE}" ]; then
276 if [ ! -d "${REAL_CCACHE}" ]; then
277 echo " failed."
278 fsumount
279 err "REAL_CCACHE ${REAL_CCACHE} disappeared"
281 mount -t null -o rw ${REAL_CCACHE} ${DESTDIR}/pkg_comp/ccache
284 touch $fsstate
286 if [ -n "${MOUNT_HOOKS}" ]; then
287 echo "Executing mount hooks."
288 for h in ${MOUNT_HOOKS}; do
289 ${h} ${DESTDIR} mount
290 done
294 # fsumount
296 # Unmounts all sandboxed filesystems, if they are not in use any more.
298 fsumount()
300 echo "PKG_COMP ==> Unmounting sandboxed filesystems"
302 if [ ! -f $fsstate ]; then
303 echo "None mounted."
304 return
307 count=`cat $fsstate`
308 if [ $count -gt 1 ]; then
309 count=$(($count - 1))
310 echo "$count" > $fsstate
311 echo "Still in use (maybe by another pkg_comp process?)"
312 return
315 if [ -n "${UMOUNT_HOOKS}" ]; then
316 echo "Executing umount hooks."
317 for h in ${UMOUNT_HOOKS}; do
318 ${h} ${DESTDIR} umount
319 done
322 fsfailed=no
324 if [ -n "$REAL_SRC" -a -d "$REAL_SRC" ]; then
325 umount $DESTDIR/usr/src || fsfailed=yes
328 if [ -n "$REAL_PKGSRC" -a -d "$REAL_PKGSRC" ]; then
329 umount $DESTDIR/usr/pkgsrc || fsfailed=yes
332 if [ -n "$REAL_DISTFILES" -a -d "$REAL_DISTFILES" ]; then
333 umount $DESTDIR/pkg_comp/distfiles || fsfailed=yes
336 if [ -n "$REAL_PACKAGES" -a -d "$REAL_PACKAGES" ]; then
337 umount $DESTDIR/pkg_comp/packages || fsfailed=yes
340 if [ -n "${REAL_CCACHE}" -a -d "${REAL_CCACHE}" ]; then
341 umount ${DESTDIR}/pkg_comp/ccache || fsfailed=yes
344 if [ "$SYNC_UMOUNT" != "no" ]; then
345 printf "Syncing: 1"
346 sync ; sleep 1
347 printf " 2"
348 sync ; sleep 1
349 printf " 3"
350 sync ; sleep 1
351 echo " done."
354 if [ "$fsfailed" = "yes" ]; then
355 err "FATAL: failed to umount all filesystems"
356 else
357 rm $fsstate
361 # ----------------------------------------------------------------------
362 # maketemplate target
363 # ----------------------------------------------------------------------
365 # pkg_maketemplate
367 # Generates a sample configuration file based on the list of variables
368 # given in TEMPLATE_VARS and MKCONF_VARS.
370 pkg_maketemplate()
372 if [ -f "$conffile" ]; then
373 err "$conffile already exists"
376 mkdir -p $confdir
377 cat > $conffile <<EOF
378 # -*- sh -*-
380 # pkg_comp - configuration file
381 # See pkg_comp(8) for a detailed description of each variable.
386 echo "# Variables used internally by pkg_comp." >> $conffile
387 for var in `echo $TEMPLATE_VARS | tr ' ' '\n' | sort`; do
388 eval val=\""\$$var"\"
389 echo "$var=\"$val\"" >> $conffile
390 done
392 echo >> $conffile
393 echo "# Default variables written to the generated mk.conf." >> $conffile
394 for var in `echo $MKCONF_VARS | tr ' ' '\n' | sort`; do
395 eval val=\""\$$var"\"
396 echo "$var=\"$val\"" >> $conffile
397 done
399 echo "pkg_comp: $conffile created. Edit the file by hand now."
402 # ----------------------------------------------------------------------
403 # makeroot target
404 # ----------------------------------------------------------------------
406 # pkg_makeroot
408 # The 'makeroot' target. This creates a new sandbox and then issues
409 # some stuff to be called only when using this specific target.
411 pkg_makeroot()
413 makeroot
415 [ "$nflag" = "no" -a -n "$INSTALL_PACKAGES" ] &&
416 pkg_install $INSTALL_PACKAGES
418 if [ "$nflag" = "no" -a -n "$BUILD_PACKAGES" ]; then
419 for pkg in $BUILD_PACKAGES; do
420 build_and_install $pkg
421 done
425 # makeroot
427 # Creates a new sandbox. This is independent from 'makeroot' and
428 # 'auto' targets.
430 makeroot()
432 # Check for directories that will be null mounted.
433 if [ -n "$REAL_SRC" -a ! -d "$REAL_SRC" ]; then
434 err "REAL_SRC $REAL_SRC does not exist"
437 if [ -n "$REAL_PKGSRC" -a ! -d "$REAL_PKGSRC" ]; then
438 err "REAL_PKGSRC $REAL_PKGSRC does not exist"
441 if [ -n "$REAL_DISTFILES" -a ! -d "$REAL_DISTFILES" ]; then
442 err "REAL_DISTFILES $REAL_DISTFILES does not exist"
445 if [ -n "$REAL_PACKAGES" -a ! -d "$REAL_PACKAGES" ]; then
446 err "REAL_PACKAGES $REAL_PACKAGES does not exist"
449 if echo ${PKGSRC_COMPILER} | grep ccache >/dev/null 2>&1 && \
450 [ -z "${REAL_CCACHE}" ]; then
451 warn "PKGSRC_COMPILER contains 'ccache' but REAL_CCACHE is unset"
452 elif [ -n "${REAL_CCACHE}" -a ! -d "${REAL_CCACHE}" ]; then
453 err "REAL_CCACHE ${REAL_CCACHE} does not exist"
456 # Check for required directories.
457 if [ ! -d $DISTRIBDIR ]; then
458 err "DISTRIBDIR $DISTRIBDIR does not exist"
461 if [ -d $DESTDIR ]; then
462 err "DESTDIR $DESTDIR already exists"
465 if [ "$SETS_X11" = "no" ]; then
466 allsets="$SETS"
467 else
468 allsets="$SETS $SETS_X11"
471 for s in $allsets; do
472 if [ ! -f $DISTRIBDIR/binary/sets/$s ]; then
473 err "$DISTRIBDIR/binary/sets/$s does not exist"
475 done
477 echo "PKG_COMP ==> Creating sandbox \`${DESTDIR}'"
479 mkdir -p $DESTDIR
480 cd $DESTDIR
482 for s in $allsets; do
483 echo "Extracting $s..."
484 tar xzpf $DISTRIBDIR/binary/sets/$s
485 done
487 [ -f ${DESTDIR}/netbsd ] || touch ${DESTDIR}/netbsd
489 echo "Making device nodes..."
490 cd $DESTDIR/dev
491 ./MAKEDEV all
492 cd $DESTDIR
494 echo "Setting root's environment..."
495 chroot $DESTDIR chpass -s $ROOTSHELL
496 if [ "$COPYROOTCFG" = "yes" ]; then
497 cp /root/.* $DESTDIR/root >/dev/null 2>&1
499 if [ -n "${REAL_CCACHE}" ]; then
500 # This is a workaround for older versions of ccache.mk that do not
501 # pass the CCACHE_DIR variable down to ccache.
502 ( cd ${DESTDIR}/root && ln -fs ../pkg_comp/ccache .ccache )
505 echo "Setting up initial configuration..."
507 mkdir -p $DESTDIR/usr/src
508 mkdir -p $DESTDIR/usr/pkgsrc
509 mkdir -p $DESTDIR/pkg_comp/distfiles
510 mkdir -p $DESTDIR/pkg_comp/packages
511 mkdir -p $DESTDIR/pkg_comp/tmp
512 mkdir -p $DESTDIR/pkg_comp/obj/pkgsrc
513 [ -n "${REAL_CCACHE}" ] && mkdir -p ${DESTDIR}/pkg_comp/ccache
514 ( cd $DESTDIR && ln -s pkg_comp p )
516 # Set sh configuration
517 echo "umask 022" >> $DESTDIR/etc/profile
518 echo "ENV=/etc/shrc" >> $DESTDIR/etc/profile
519 echo "export PS1=\"pkg_comp:`basename $conffile`# \"" >> $DESTDIR/etc/shrc
520 echo "set -o emacs" >> $DESTDIR/etc/shrc
521 echo "export PKG_DBDIR=\"${PKG_DBDIR}\"" >> ${DESTDIR}/etc/shrc
523 # Set csh configuration
524 echo "umask 022" >> $DESTDIR/etc/csh.login
525 echo "set prompt=\"pkg_comp:`basename $conffile`# \"" >> $DESTDIR/etc/csh.login
526 echo "set prompt=\"pkg_comp:`basename $conffile`# \"" >> $DESTDIR/etc/csh.cshrc
527 echo "setenv PKG_DBDIR \"${PKG_DBDIR}\"" >> ${DESTDIR}/etc/csh.cshrc
529 cp /etc/resolv.conf $DESTDIR/etc/resolv.conf
531 makeroot_mkconf
533 echo "PKG_DBDIR=${PKG_DBDIR}" >> ${DESTDIR}/etc/pkg_install.conf
535 # From now on, filesystems may be mounted, so we need to trap
536 # signals to umount them.
537 trap "echo \"*** Process aborted ***\" ; fsumount ; exit 1" INT QUIT
539 makeroot_libkver
541 if [ "$USE_GCC3" = "yes" -a "$Nflag" = "no" ]; then
542 if [ -z "`echo $BUILD_PACKAGES $INSTALL_PACKAGES | grep gcc3`" ]; then
543 AVOID_GCC3=yes build_and_install lang/gcc3
547 makeroot_x11
549 if [ -n "${MAKEROOT_HOOKS}" ]; then
550 echo "Executing makeroot hooks."
551 for h in ${MAKEROOT_HOOKS}; do
552 ${h} ${DESTDIR} makeroot
553 done
557 # makeroot_mkconf
559 # Generates a mk.conf file inside the sandbox, based on the content
560 # of MKCONF_VARS and EXTRAMK. Also handles some special stuff.
562 makeroot_mkconf()
564 file="$DESTDIR/etc/mk.conf"
566 cat >> $file <<EOF
568 # /etc/mk.conf
569 # File automatically generated by pkg_comp on `date`
571 .ifdef BSD_PKG_MK
575 for var in $MKCONF_VARS; do
576 eval val=\""\$$var"\"
577 echo "$var ?= $val" >> $file
578 done
580 if [ -n "$EXTRAMK" ]; then
581 for mkfile in $EXTRAMK; do
582 if [ ! -f "$mkfile" ]; then
583 err "Cannot find $mkfile"
584 else
585 cat $mkfile >> $file
587 done
590 if [ "$USE_AUDIT_PACKAGES" != "yes" ]; then
591 echo "ALLOW_VULNERABLE_PACKAGES ?= YES" >> $file
594 if [ "$USE_GCC3" = "yes" ]; then
595 cat >>$file <<EOF
596 .if !defined(AVOID_GCC3) && exists(/usr/pkg/share/examples/gcc-3.3/mk.conf)
597 USE_GCC3 = yes
598 CFLAGS += $CFLAGS
599 CPPFLAGS += $CPPFLAGS
600 CXXFLAGS += $CXXFLAGS
601 .include "/usr/pkg/share/examples/gcc-3.3/mk.conf"
602 .endif
604 else
605 cat >>$file <<EOF
606 CFLAGS += $CFLAGS
607 CPPFLAGS += $CPPFLAGS
608 CXXFLAGS += $CXXFLAGS
610 .endif # BSD_PKG_MK
615 # makeroot_libkver
617 # If NETBSD_RELEASE is set to a version string, installs libkver
618 # inside the sandbox and configures it.
620 makeroot_libkver()
622 local prefix script statfile
624 if [ "$NETBSD_RELEASE" != "no" ]; then
625 _BUILD_TARGET="$BUILD_TARGET"
626 BUILD_TARGET="standalone-install"
627 build_and_install pkgtools/libkver
628 BUILD_TARGET="$_BUILD_TARGET"
629 echo "LD_PRELOAD=${LIBKVER_STANDALONE_PREFIX}/lib/libkver.so; export LD_PRELOAD" >> $DESTDIR/etc/shrc
630 echo "setenv LD_PRELOAD ${LIBKVER_STANDALONE_PREFIX}/lib/libkver.so" >> $DESTDIR/etc/csh.login
631 echo "setenv LD_PRELOAD ${LIBKVER_STANDALONE_PREFIX}/lib/libkver.so" >> $DESTDIR/etc/csh.cshrc
632 ln -s "$NETBSD_RELEASE" $DESTDIR/libkver_osrelease
636 # makeroot_x11
638 # If X11 is enabled, installs x11-links inside the sandbox.
639 # If USE_XPKGWEDGE is yes, configures xpkgwedge too (but does not
640 # install it; pkgsrc will take care of that when needed).
642 makeroot_x11()
644 if [ "$SETS_X11" != "no" ]; then
645 if [ "$USE_XPKGWEDGE" = "yes" ]; then
646 echo "export XAPPLRESDIR=${LOCALBASE}/lib/X11/app-defaults" >> $DESTDIR/etc/profile
647 echo "setenv XAPPLRESDIR ${LOCALBASE}/lib/X11/app-defaults" >> $DESTDIR/etc/csh.login
649 [ "$Nflag" = "no" ] && build_and_install pkgtools/x11-links
653 # ----------------------------------------------------------------------
654 # auto target
655 # ----------------------------------------------------------------------
657 # pkg_auto pkgs
659 # The 'auto' target.
661 pkg_auto()
663 local pkgs rfile target
665 pkgs="${*:-${AUTO_PACKAGES}}"
666 rfile=${DESTDIR}/pkg_comp/tmp/auto.list
667 target="${AUTO_TARGET}"
669 [ -z "${REAL_PACKAGES}" ] && err "this is useless without REAL_PACKAGES"
670 [ -z "${pkgs}" ] &&
671 err "this is useless without AUTO_PACKAGES nor package names"
673 if [ "$1" = resume ]; then
674 [ -f ${rfile} ] || err "there is no auto build to resume"
675 pkgs=`cat ${rfile}`
676 elif [ -f ${rfile} ]; then
677 err "there is an stopped auto build; removeroot first or resume it"
678 else
679 makeroot
681 [ "$nflag" = "no" -a -n "$INSTALL_PACKAGES" ] &&
682 pkg_install $INSTALL_PACKAGES
684 echo ${pkgs} | tr ' ' '\n' > ${rfile}
687 checkroot
688 _BUILD_RESUME=${rfile} BUILD_TARGET=${target} pkg_build ${pkgs}
689 pkg_removeroot
692 # generate_pkg_summary directory
694 # Generates a pkg_summary.gz file in the specified directory.
695 generate_pkg_summary()
697 local directory="${1}"; shift
699 echo "PKG_COMP ==> Generating pkg_summary.tgz"
700 for pkg in "${directory}"/*.tgz; do pkg_info -X "${pkg}"; done \
701 | gzip -c >"${directory}"/pkg_summary.gz
704 # ----------------------------------------------------------------------
705 # build target
706 # ----------------------------------------------------------------------
708 # pkg_build pkgs
710 # The build target. Also used as a helper function within this script
711 # to build several packages when needed.
713 pkg_build()
715 local failed invalid p pkgs script statfile
717 pkgs=
718 invalid=
719 for pkg in "${@}"; do
720 local match="$(find_pkg "${pkg}")"
721 if [ -z "${match}" ]; then
722 invalid="${invalid} ${pkg}"
723 else
724 pkgs="${pkgs} ${match}"
726 done
727 if [ -n "$invalid" ]; then
728 err "invalid packages:$invalid"
731 # Build them
732 fsmount
733 copy_vulnerabilities
734 failed=""
735 for p in $pkgs; do
736 echo "PKG_COMP ==> Building and installing $p"
737 prefix=$(mktemp $DESTDIR/pkg_comp/tmp/pkg_comp-XXXXXX)
738 rm $prefix
739 script="$prefix.sh"
740 statfile="$prefix.stat"
741 init_script $script
742 cat >> $script <<EOF
743 cd /usr/pkgsrc/$p
744 make $BUILD_TARGET
745 if [ \$? != 0 ]; then
746 touch /pkg_comp/tmp/`basename $statfile`
748 make clean
750 chmod +x $script
751 chroot $DESTDIR /pkg_comp/tmp/`basename $script`
752 rm $script
753 if [ -f $statfile ]; then
754 failed="$failed $p"
755 rm $statfile
756 elif [ -n "${_BUILD_RESUME}" ]; then
757 grep -v "^${p}\$" < ${_BUILD_RESUME} > ${_BUILD_RESUME}.new
758 mv ${_BUILD_RESUME}.new ${_BUILD_RESUME}
760 done
761 fsumount
762 if [ -n "$failed" ]; then
763 echo "PKG_COMP ==> Build error summary"
764 echo "Build failed for:"
765 for p in $failed; do
766 echo " $p"
767 done
769 if [ "${GENERATE_PKG_SUMMARY}" = yes ]; then
770 generate_pkg_summary "${REAL_PACKAGES}/All"
774 # find_pkg name
776 # Checks if the given package exists and outputs its path within pkgsrc.
777 # Outputs nothing if the package is not found.
778 find_pkg()
780 local name="${1}"
782 case "${name}" in
783 */*)
784 if [ -d "${REAL_PKGSRC}/${name}" ]; then
785 echo "${name}"
786 else
787 : # Not found; output nothing.
791 local match="$(cd "${REAL_PKGSRC}" && echo */"${name}")"
792 if [ -d "${REAL_PKGSRC}/${match}" ]; then
793 echo "${match}"
794 else
795 : # Not found; output nothing.
797 esac
800 # build_and_install pkg
802 # Builds a package and ensures it gets installed. The use of destdir to
803 # build packages may cause a package to get built but not installed,
804 # which is not OK for this script. This is for internal usage only.
806 build_and_install()
808 pkg=${1}
809 fsmount
810 if pkg_build ${pkg}; then
811 script=$(mktemp ${DESTDIR}/pkg_comp/tmp/pkg_comp-XXXXXX).sh
812 init_script ${script}
813 cat >>${script} <<EOF
814 cd /usr/pkgsrc/${pkg}
815 pkgname=\$(make show-var VARNAME=PKGNAME)
816 if pkg_info -E \${pkgname} 2>/dev/null; then
818 else
819 echo "PKG_COMP ==> Forcing installation of \${pkgname}"
820 cd /pkg_comp/packages/All
821 pkg_add \${pkgname}
824 chmod +x ${script}
825 chroot ${DESTDIR} /pkg_comp/tmp/$(basename ${script})
826 rm ${script}
828 fsumount
831 # ----------------------------------------------------------------------
832 # install target
833 # ----------------------------------------------------------------------
835 # pkg_install pkgs
837 # The install target. Also used as a helper function within this
838 # script to install several packages when needed.
840 pkg_install()
842 local failed pkgs stat
844 pkgs="$*"
846 [ -z "$REAL_PACKAGES" ] && err "REAL_PACKAGES is not set"
848 copy_vulnerabilities
850 fsmount
851 failed=""
852 for p in $(cd ${REAL_PACKAGES}/All && echo ${pkgs}); do
853 echo "PKG_COMP ==> Installing binary package: $p"
854 stat=$DESTDIR/pkg_comp/tmp/install.sh
855 init_script $stat
856 cat >> $stat <<EOF
857 cd /pkg_comp/packages/All
858 pkg_add $p
860 chmod +x $stat
861 chroot $DESTDIR /pkg_comp/tmp/install.sh || failed="$failed $p"
862 rm $stat
863 done
864 fsumount
865 [ -n "$failed" ] && echo "Installation failed for:$failed"
868 # ----------------------------------------------------------------------
869 # chroot target
870 # ----------------------------------------------------------------------
872 # pkg_chroot cmd
874 # The 'chroot' target.
876 pkg_chroot()
878 local prefix script exitstatus
880 [ -d $DESTDIR ] || err "$DESTDIR does not exist"
882 copy_vulnerabilities
884 fsmount
885 echo "PKG_COMP ==> Entering sandbox \`$DESTDIR'"
886 prefix=$(mktemp $DESTDIR/pkg_comp/tmp/pkg_comp-XXXXXX)
887 rm $prefix
888 script="$prefix.sh"
889 init_script $script
890 if [ $# -eq 0 ]; then
891 cat >> $script <<EOF
892 ENV=/etc/shrc $ROOTSHELL
894 else
895 cat >> $script <<EOF
899 chmod +x $script
900 ENV=/etc/shrc chroot $DESTDIR /pkg_comp/tmp/`basename $script`
901 exitstatus=$?
902 echo
903 rm $script
904 fsumount
905 return $exitstatus
908 # ----------------------------------------------------------------------
909 # removeroot target
910 # ----------------------------------------------------------------------
912 # pkg_removeroot
914 # The 'removeroot' target.
916 pkg_removeroot()
918 [ -f $fsstate ] && err "filesystems may still be mounted; cannot remove"
919 echo "PKG_COMP ==> Removing sandbox \`${DESTDIR}'"
920 rm -rf $DESTDIR
923 # ----------------------------------------------------------------------
924 # Main program
925 # ----------------------------------------------------------------------
927 confdir="$HOME/pkg_comp"
929 # Parse options
930 args=`getopt c:C:nN $*`
931 if [ $? != 0 ]; then
932 usage
934 set -- $args
935 conffile=
936 nflag=no
937 Nflag=no
938 while [ $# -gt 0 ]; do
939 case "$1" in
941 [ -n "$conffile" ] && usage
942 conffile="$confdir/$2.conf"
943 shift
946 [ -n "$conffile" ] && usage
947 conffile="$2"
948 shift
951 nflag=yes
954 nflag=yes
955 Nflag=yes
958 shift; break
960 esac
961 shift
962 done
964 if [ $# -lt 1 ]; then
965 usage
968 if [ -z "$conffile" ]; then
969 conffile="$confdir/default.conf"
972 case "$1" in
973 pkg_*)
974 target=chroot
977 target="$1"
978 shift
980 esac
981 args="$*"
983 # readconf
985 # Reads the configuration file and ensures that the environment is in
986 # a consistent state.
988 readconf()
990 if [ -f $conffile ]; then
991 [ `id -u` -ne 0 ] && err "must be run as root"
992 env_clean
993 . $conffile
994 env_setdefaults
995 else
996 err "$conffile does not exist"
999 fsstate="$DESTDIR/pkg_comp/tmp/mount.stat"
1002 # checkroot
1004 # Checks that the sandbox is initialized, that it's a valid directory
1005 # and configures a signal trap for SIGINT and SIGQUIT.
1007 checkroot()
1009 if [ ! -d "$DESTDIR" ]; then
1010 err "sandbox not initialized; use makeroot first."
1012 if [ "$DESTDIR" = "/" ]; then
1013 err "DESTDIR cannot be /"
1016 # From now on, filesystems may be mounted, so we need to trap
1017 # signals to umount them.
1018 trap "echo \"*** Process aborted ***\" ; fsumount ; exit 1" INT QUIT
1021 case "$target" in
1022 maketemplate)
1023 env_clean
1024 env_setdefaults
1025 pkg_maketemplate
1026 exit 0
1028 makeroot)
1029 readconf
1030 pkg_makeroot
1031 exit 0
1033 build)
1034 readconf
1035 checkroot
1036 pkg_build $args
1037 exit 0
1039 install)
1040 readconf
1041 checkroot
1042 pkg_install $args
1043 exit 0
1045 chroot)
1046 readconf
1047 checkroot
1048 pkg_chroot $args
1049 exit $?
1051 removeroot)
1052 readconf
1053 checkroot
1054 pkg_removeroot
1056 auto)
1057 readconf
1058 pkg_auto $args
1061 err "unknown target \`$target'"
1063 esac
1065 exit 0
1067 # vim: expandtab:softtabstop=4:shiftwidth=4