3 # Load shlib and modules
4 _root="$(dirname "${0}")"; . "${_root}/lib/env.sh"
13 # Prepare a chroot for work
15 # Verify environment sanity
16 [ -d "${chroot_dir}" ] && omg "${chroot_dir}: directory exists; purging" && chdestroy
17 mount | grep -q "${chroot_dir}" && wtf "Stuff is mounted under ${chroot_dir}; cannot continue"
18 [ -d "${seed_dir}" -a -f "${seed_dir}/COPYRIGHT" ] || wtf "Use 'makeworld' to create a root build first"
19 [ -f "/usr/ports/UPDATING" ] || wtf "Need ports tree in /usr/ports to build"
20 [ -f "/usr/src/sys/conf/newvers.sh" ] || omg "No base sourcetree in /usr/src"
22 meh "Setting up chroot tree"
24 # Just in case we're aborted partway through the prepare
25 trap "chdestroy" exit hup int term kill
27 # Make sure config dir exists for later save
28 [ -d "${conf_dir}" ] || mkdir -p "${conf_dir}"
30 # Populate initial seed
31 mkdir -p "$(dirname "${chroot_dir}")" || wtf "chroot path create failed"
32 cp -pR "${seed_dir}" "${chroot_dir}" || wtf "chroot seeding failed"
34 # Create distfile cache
35 mkdir -p "${dist_dir}" || wtf "mkdir ${dist_dir} failed"
37 # Create mountpoint directories
38 mkdir -p "${chroot_dir}/dev" || wtf "mkdir /dev failed"
39 mkdir -p "${chroot_dir}/usr/src" || wtf "mkdir /usr/src failed"
40 mkdir -p "${chroot_dir}/usr/obj" || wtf "mkdir /usr/obj failed"
41 mkdir -p "${chroot_dir}/usr/ports" || wtf "mkdir /usr/ports failed"
42 mkdir -p "${chroot_dir}/var/ports/distfiles" || wtf "mkdir /var/ports/distfiles failed"
44 # Create pkg directories
45 mkdir -p "${pkg_dir}" || wtf "mkdir ${chroot_pkg_dir} failed"
46 mkdir -p "${bdeps_dir}" || wtf "mkdir ${chroot_bdeps_dir} failed"
48 # Copy in cached configuration, if necessary
49 [ -f "${conf_dir}/make.conf" ] && cp -p "${conf_dir}/make.conf" "${chroot_dir}/etc/make.conf"
51 # If you have WITHOUT_INFO set in src.conf during buildworld/installworld, ports that need
52 # makeinfo and install-info will fail horribly; stub them
53 [ -f "${chroot_dir}/usr/bin/makeinfo" ] || ln -sf true "${chroot_dir}/usr/bin/makeinfo"
54 [ -f "${chroot_dir}/usr/bin/install-info" ] || ln -sf true "${chroot_dir}/usr/bin/install-info"
56 # Tweak make.conf to support read-only ports tree
57 cat <<EOF >> "${chroot_dir}/etc/make.conf"
59 # Read-only ports tree
60 DISTDIR=/var/ports/distfiles
61 PACKAGES=/var/ports/packages
65 trap "" exit hup int term kill
66 meh "Chroot tree set up in ${chroot_dir}"
71 # Set up chroot mounts and runtime configuration
73 [ -d "${chroot_dir}" -a -f "${chroot_dir}/COPYRIGHT" -a -d "${chroot_dir}/dev" ] || wtf "Chroot not prepared"
76 # Rollback if a problem occurs during startup
77 trap "chshutdown" exit hup int term kill
79 meh "Starting up chroot"
81 # Necessary mountpoints
82 mount -t devfs devfs "${chroot_dir}/dev" || wtf "mount /dev failed"
83 mount -t nullfs -r /usr/src "${chroot_dir}/usr/src" || wtf "mount /usr/src failed"
84 mount -t nullfs -r /usr/ports "${chroot_dir}/usr/ports" || wtf "mount /usr/ports failed"
85 mount -t nullfs -w "${dist_dir}" "${chroot_dir}/var/ports/distfiles" || wtf "mount /var/ports/distfiles failed"
87 # Chroot configuration
88 cp -f /etc/resolv.conf "${chroot_dir}/etc/resolv.conf" || wtf "seeding /etc/resolv.conf failed"
90 # Load port configuration, now that chroot is running
94 cheval "/etc/rc.d/ldconfig start"
96 trap "" exit hup int term kill
97 meh "Chroot up and running in ${chroot_dir}"
102 # Check if the chroot is probably ready for use
104 [ -d "${chroot_dir}" -a -f "${chroot_dir}/COPYRIGHT" -a -c "${chroot_dir}/dev/null" ]
108 # Unmount all chroot directories
110 # Short-circuit if nothing is mounted
111 mount | grep -q "${chroot_dir}" || return 0
112 chup && port_save_config || meh "Not saving port config"
113 meh "Shutting down chroot"
114 umount "${chroot_dir}/var/ports/distfiles"
115 umount "${chroot_dir}/usr/ports"
116 umount "${chroot_dir}/usr/src"
117 umount "${chroot_dir}/dev"
121 # Remove all chroot contents
124 meh "Destroying chroot"
125 chflags -R noschg "${chroot_dir}" || wtf "noschg failed"
126 rm -Rf "${chroot_dir}" || wtf "chroot removal failed"
130 # Evaluate a command line within the chroot
132 chup || wtf "Chroot not ready"
133 chroot "${chroot_dir}" env -i ${chroot_env} /bin/sh -c "cd ${chroot_pkg_dir}; ${*}"
139 local port="/usr/ports/${1##/usr/ports/}"
141 cheval "make -C ${port} ${*}"
147 # Port Dependency Tracking
151 # Translate port origin to package name
155 chmake "${1}" -V PKGNAME
160 # Build dependencies (shallow, recursive, package)
164 chmake "${1}" build-depends-list | sed -e 's#^/usr/ports/##'
169 # Clear cache if first value isn't '-r'
170 [ "${1}" = '-r' ] && shift || { kvs_unset_all 'port_all_bdeps'; kvs_unset_all 'port_all_rdeps'; }
171 # rdeps for rdeps are rdeps, bdeps for rdeps are bdeps, rdeps for bdeps are bdeps; thus:
173 port_bdeps "${@}" | while read port
175 [ "${VERBOSE_CACHE}" ] && logf "**** bdep cache %s: '%s'\n" "$(kvs_has_key 'port_all_bdeps' "${port}" && echo "hit" || echo "miss")" "${port}" >&2
176 if ! kvs_has_key 'port_all_bdeps' "${port}"
178 kvs_set 'port_all_bdeps' "${port}" ""
180 port_all_bdeps -r "${port}"
181 port_all_rdeps -r "${port}"
184 port_all_rdeps -r "${@}" | while read port
186 [ "${VERBOSE_CACHE}" ] && logf "**** bdep cache %s: '%s'\n" "$(kvs_has_key 'port_all_bdeps' "${port}" && echo "hit" || echo "miss")" "${port}" >&2
187 if ! kvs_has_key 'port_all_bdeps' "${port}"
189 kvs_set 'port_all_bdeps' "${port}" ""
190 port_all_bdeps -r "${port}"
196 port2pkg $(port_all_bdeps "${@}")
199 # Runtime dependencies (shallow, recursive, package)
203 chmake "${1}" run-depends-list | sed -e 's#^/usr/ports/##'
208 # Clear cache if first value isn't '-r'
209 [ "${1}" = '-r' ] && shift || kvs_unset_all 'port_all_rdeps'
210 port_rdeps "${@}" | while read port
212 [ "${VERBOSE_CACHE}" ] && logf "**** rdep cache %s: '%s'\n" "$(kvs_has_key 'port_all_rdeps' "${port}" && echo "hit" || echo "miss")" "${port}" >&2
213 if ! kvs_has_key 'port_all_rdeps' "${port}"
215 kvs_set 'port_all_rdeps' "${port}"
217 port_all_rdeps -r "${port}"
222 port2pkg $(port_all_rdeps "${@}")
225 # All dependencies (shallow, recursive, package)
229 chmake "${1}" all-depends-list | sed -e 's#^/usr/ports/##'
234 port_deps "${@}" | while read port
241 port2pkg $(port_all_deps "${@}")
244 # Dump a list of leaf ports from a working system
245 # Leaf ports are ports that have nothing depending upon them
246 # These are generally the top-level ports; everything else should
247 # be pulled in by them; on a system that has had updates applied
248 # this may pick up orphaned packages as well, so try cutleaves?
250 ( cd /var/db/pkg; ls -1 ) | while read pkg
252 pkg_info -Rq "${pkg}" | grep -q '' || pkg_info -oq "${pkg}"
256 # Display a tree of all build dependencies (and
257 # any runtime dependencies those build dependencies
258 # depend upon). Individual ports may appear more than
259 # once, as it is a tree and not a list.
261 # Clear cache if first value isn't '-r'
262 [ "${1}" = '-r' ] && shift || {
263 [ ${VERBOSE_CACHE} ] && logf "**** bdep_tree cache cleared\n"
264 kvs_unset_all 'port_bdep_tree'
266 port="${1##/usr/ports/}"
267 printf "%${2}s%s\n" "" "${port}"
269 [ "${VERBOSE_CACHE}" ] && logf "**** bdep_tree cache %s: '%s'\n" "$(kvs_has_key 'port_bdep_tree' "${port}" && echo "hit" || echo "miss")" "${port}" >&2
271 if ! kvs_has_key 'port_bdep_tree' "${port}"
273 ( chmake "${port}" build-depends-list
274 chmake "${port}" run-depends-list
275 ) | sort -u | while read port
277 port_bdep_tree -r "${port}" $(( ${2:-0} + 1 ))
278 kvs_set 'port_bdep_tree' "${port}"
281 printf "%${2}s %s\n" "" "..."
285 # Display a tree of all runtime dependencies. Individual
286 # ports may appear more than once, as it is a tree and
288 # Cache already-visted branches into ... to eliminate recursion delays
289 # (x11-drivers/xorg-drivers computes forever!)
291 # Clear cache if first value isn't '-r'
292 [ "${1}" = '-r' ] && shift || {
293 [ ${VERBOSE_CACHE} ] && logf "**** rdep_tree cache cleared\n"
294 kvs_unset_all 'port_rdep_tree'
296 port="${1##/usr/ports/}"
297 printf "%${2}s%s\n" "" "${port}"
299 [ "${VERBOSE_CACHE}" ] && logf "**** rdep_tree cache %s: '%s'\n" "$(kvs_has_key 'port_rdep_tree' "${port}" && echo "hit" || echo "miss")" "${port}" >&2
301 if ! kvs_has_key 'port_rdep_tree' "${port}"
303 chmake "${port}" run-depends-list | sort -u | while read port
305 port_rdep_tree -r "${port}" $(( ${2:-0} + 1 ))
306 kvs_set 'port_rdep_tree' "${port}"
309 printf "%${2}s %s\n" "" "..."
315 # Port Configuration Handling
319 # Run make config on a list of ports
323 meh "port config ${1}"
324 chmake "${1}" config < /dev/tty
329 # Make config-conditional for a list of ports, and all dependencies
330 port_config_recursive() {
331 # Clear cache if first value isn't '-r'
332 [ "${1}" = '-r' ] && shift || kvs_unset_all 'port_config_recursive'
335 # Do not use config-recursive because it computes the depchain first;
336 # instead, manually compute the depchain after each config to ensure the
337 # proper depchain is followed. Use config-conditional to avoid dialog
338 # if the config is already complete. Also use a cache to avoid re-config
339 # and re-recurse on previously handled port branches.
340 if ! kvs_has_key 'port_config_recursive' "${1}"
342 meh "port config-recursive ${1}"
343 chmake "${1}" config-conditional < /dev/tty
344 port_config_recursive -r $(port_deps "${1}")
345 kvs_set 'port_config_recursive' "${1}"
351 # Remove saved config for a list of ports
355 meh "port rmconfig ${1}"
356 chmake "${1}" rmconfig
361 # Remove saved config for a list of ports and all dependencies
362 port_rmconfig_recursive() {
363 meh "port rmconfig-recursive ${*}"
364 port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq)
367 # Obliterate saved configuration for all ports
368 port_rmconfig_all() {
369 meh "port rmconfig-all"
370 cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null"
373 # Restore port build options from directory
375 [ -d "${conf_dir}/port.options" ] || return 0
376 meh "port load-config"
377 ( cd "${conf_dir}/port.options"; find . | cpio -oHnewc ) | cheval "cd /var/db/ports; cpio -i" || wtf "port load-config failed"
380 # Dump port build options to directory
382 meh "port save-config"
383 rm -Rf "${conf_dir}/port.options.tmp" "${conf_dir}/port.options.old"
384 mkdir -p "${conf_dir}/port.options" "${conf_dir}/port.options.tmp"
385 cheval "cd /var/db/ports; find . -type d -o -type f -name options | cpio -oHnewc" | ( cd "${conf_dir}/port.options.tmp"; cpio -i ) || wtf "port safe-config failed"
386 mv -f "${conf_dir}/port.options" "${conf_dir}/port.options.old" && \
387 mv -f "${conf_dir}/port.options.tmp" "${conf_dir}/port.options" && \
388 rm -Rf "${conf_dir}/port.options.old" || \
389 wtf "port save-config atomic commit failed"
394 # Port distfile handling
398 # Recursively retrieve distfiles
399 port_fetch_recursive() {
402 meh "fetch-recursive ${1}"
403 chmake "${1}" fetch-recursive
410 # Port building and packaging
414 # Copy in and install dependency packages
415 # Missing dependencies are not fatal, since a port build will rebuild them anyways
418 # Install portmaster: it's needed to clean up dependencies after an update build
419 portmaster_port="ports-mgmt/portmaster"
420 portmaster=$(port2pkg "${portmaster_port}")
421 if [ -f "${final_bdeps_dir}/${portmaster}.tbz" ]
423 cp -f "${final_bdeps_dir}/${portmaster}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading ${portmaster}"
425 meh "No portmaster package exists; building one"
426 chmake "${portmaster_port}" BATCH=yes clean build install clean || wtf "port_build ${portmaster_port} failed"
429 # And now for the dependencies
430 for pkg in $(port2pkg $(port_all_deps "${port}"))
432 cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}"
435 # Install all selected packages, ignoring already-installed packages and missing dependencies
436 if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1
438 meh "Installing dependencies"
439 cheval "cd ${chroot_bdeps_dir}; pkg_add -Ff *" || wtf "port_load_deps ${port} failed"
443 # Build and install a port
446 meh "Building ${port}"
447 chmake "${port}" clean build install clean || wtf "port_build ${port} failed"
453 meh "Cleaning up dependency tree"
454 cheval "portmaster --check-depends"
455 meh "Creating rdep package tree for ${port}"
456 cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed"
459 # Package port build dependencies, unless they're already run deps
461 meh "Stashing unsaved bdeps"
462 # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison)
463 # Actually, that was due to a bug in port_all_bdeps; but I like the other solution better anyways
464 #for pkg in $(pkg_bdeps "${1}")
466 # [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}"
469 # Instead, just save everything that's not already in rdeps as bdeps
470 for pkg in $(cheval pkg_info | awk '{print $1}')
472 if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps_dir}/${pkg}.tbz" ]
474 cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed"
479 # Copy generated packages out of tree
481 meh "Moving created packages to repo"
482 mkdir -p "${final_pkg_dir}"
483 ls "${pkg_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${pkg_dir}"/*.tbz "${final_pkg_dir}"
484 mkdir -p "${final_bdeps_dir}"
485 ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${bdeps_dir}"/*.tbz "${final_bdeps_dir}"
486 # link everything into ${bdeps_dir} so we can find it easily later
487 ( cd "${final_pkg_dir}"; find . -type f | cpio -plu --quiet "${final_bdeps_dir}" )
490 # Delete all installed packages (hope you saved them first)
492 meh "Clearing out installed packages"
493 cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed"
496 # Fix up dependency information for already-built packages
502 meh "Fixing up dependency information for ${port}"
505 port_load_deps "${port}"
508 pkg="$(port2pkg "${port}")"
509 cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading package ${pkg}"
510 cheval "cd ${chroot_bdeps_dir}; pkg_add -Ff ${pkg}.tbz" || wtf "Installing ${port} failed"
512 # Fixup and stash package
513 port_package "${port}"
534 # Execute a complete port build, using prebuilt packages to fulfill dependencies when available
535 # Be sure to chsetup and populate your config before running!
537 [ "${#}" -gt 0 ] || set -- $(cat ${conf_dir}/port.lst)
541 meh "config-recursive"
542 port_config_recursive "${port}"
543 meh "fetch-recursive"
544 port_fetch_recursive "${port}"
546 port_load_deps "${port}"
548 # Avoid building ports-mgmt/portmaster because port_load_deps already has
549 [ "${port}" = "ports-mgmt/portmaster" ] || port_build "${port}"
551 port_package "${port}"
565 # Configuration variable setup
569 ARCH="${ARCH:-$(uname -m)}"
570 CONF="${CONF:-GENERIC}"
572 # Root directory of makepkg
573 ROOT="$(realpath "$(dirname "${0}")/..")"
575 # Location of targets
576 TARGETS="${ROOT}/targets"
578 # Base directory for selected target
579 base_dir="${TARGETS}/${ARCH}/${CONF}"
581 # Link to appropriate world
582 world_dir="${base_dir}/world"
584 # Verify that world points to a proper world
585 if [ ! -d "${world_dir}" ]
587 omg "World link is not appropriate; defaulting to ${ARCH}/GENERIC"
588 world_dir="${ROOT}/worlds/${ARCH}/GENERIC"
591 # Directory holding configuration
592 conf_dir="${base_dir}/config"
594 # Root tree for chroot seeding
595 seed_dir="${world_dir}/root"
597 # Directory where distfiles will be stored between builds (common to all targets)
598 dist_dir="${ROOT}/seed/distfiles"
600 # Final directory for built packages (Outside chroot)
601 final_pkg_dir="${base_dir}/pkg"
602 final_bdeps_dir="${base_dir}/bdeps"
605 chroot_dir="${base_dir}/chroot"
607 # Package directories (must be under ${chroot_dir})
608 pkg_dir="${chroot_dir}/pkg"
609 bdeps_dir="${pkg_dir}/bdeps"
611 # Compute in-chroot pkg and bdeps dirs
612 chroot_pkg_dir="${pkg_dir##${chroot_dir}}"
613 chroot_bdeps_dir="${bdeps_dir##${chroot_dir}}"
620 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
625 # Configure and load kvs
626 kvs="${chroot_dir}/.makepkg.kvs"
631 Configuration variables:
639 ------------------------------
640 chprepare Prepare chroot
641 chstartup Start up chroot
642 cheval Evaluate a command string within chroot
643 chmake Run port make within chroot
644 chshutdown Shut down chroot
645 chdestroy Destroy chroot
647 Port Dependency Tracking
648 ------------------------------
649 port2pkg Translate a port dirname to a package name
651 port_bdeps Compute port build dependencies
652 port_all_bdeps Recursively compute port build dependencies
653 pkg_bdeps port2pkg port_all_bdeps
655 port_rdeps Compute port run dependencies
656 port_all_rdeps Recursively compute port run dependencies
657 pkg_rdeps port2pkg port_all_rdeps
659 port_deps Compute port build and run dependencies
660 port_all_deps Recursively compute port build and run dependencies
661 pkg_deps port2pkg port_all_deps
663 leaf_ports Dump a list of leaf ports (ports with nothing depending upon them) from a running system
664 port_bdep_tree Display a tree of all build dependencies
665 port_rdep_tree Display a tree of all run dependencies
667 Port Configuration Handling
668 ------------------------------
669 port_config Make config for a port
670 port_config_recursive Make config recursive, recomputing depchain for each
671 port_rmconfig Remove saved configuration
672 port_rmconfig_recursive Remove saved configuration for a port and all dependencies
673 port_rmconfig_all Remove all saved configuration
674 port_load_config Restore port configuration from cpio
675 port_save_config Save port configuration to cpio
677 Port Distfile Handling
678 ------------------------------
679 port_fetch_recursive fetch-recursive
681 Port Building And Packaging
682 ------------------------------
691 ------------------------------
697 # Blind passthru for testing
700 # Todo: Clean up entrypoint to support proper options
701 # Todo: Add methods to autoprocess a ports.lst file to produce packages
702 # Todo: Unify chroot handling with makeworld