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"
50 [ -f "${conf_dir}/port_options.cpio" ] && ( cd ${chroot_dir}/var/db/ports; cpio -i ) < "${conf_dir}/port_options.cpio"
52 # If you have WITHOUT_INFO set in src.conf during buildworld/installworld, ports that need
53 # makeinfo and install-info will fail horribly; stub them
54 [ -f "${chroot_dir}/usr/bin/makeinfo" ] || ln -sf true "${chroot_dir}/usr/bin/makeinfo"
55 [ -f "${chroot_dir}/usr/bin/install-info" ] || ln -sf true "${chroot_dir}/usr/bin/install-info"
57 # Tweak make.conf to support read-only ports tree
58 cat <<EOF >> "${chroot_dir}/etc/make.conf"
60 # Read-only ports tree
61 DISTDIR=/var/ports/distfiles
62 PACKAGES=/var/ports/packages
66 trap "" exit hup int term kill
67 meh "Chroot tree set up in ${chroot_dir}"
72 # Set up chroot mounts and runtime configuration
74 [ -d "${chroot_dir}" -a -f "${chroot_dir}/COPYRIGHT" -a -d "${chroot_dir}/dev" ] || wtf "Chroot not prepared"
77 # Rollback if a problem occurs during startup
78 trap "chshutdown" exit hup int term kill
80 meh "Starting up chroot"
82 # Necessary mountpoints
83 mount -t devfs devfs "${chroot_dir}/dev" || wtf "mount /dev failed"
84 mount -t nullfs -r /usr/src "${chroot_dir}/usr/src" || wtf "mount /usr/src failed"
85 mount -t nullfs -r /usr/ports "${chroot_dir}/usr/ports" || wtf "mount /usr/ports failed"
86 mount -t nullfs -w "${dist_dir}" "${chroot_dir}/var/ports/distfiles" || wtf "mount /var/ports/distfiles failed"
88 # Chroot configuration
89 cp -f /etc/resolv.conf "${chroot_dir}/etc/resolv.conf" || wtf "seeding /etc/resolv.conf failed"
91 trap "" exit hup int term kill
92 meh "Chroot up and running in ${chroot_dir}"
97 # Check if the chroot is probably ready for use
99 [ -d "${chroot_dir}" -a -f "${chroot_dir}/COPYRIGHT" -a -c "${chroot_dir}/dev/null" ]
103 # Unmount all chroot directories
105 # Short-circuit if nothing is mounted
106 mount | grep -q "${chroot_dir}" || return 0
107 meh "Shutting down chroot"
108 umount "${chroot_dir}/var/ports/distfiles"
109 umount "${chroot_dir}/usr/ports"
110 umount "${chroot_dir}/usr/src"
111 umount "${chroot_dir}/dev"
115 # Remove all chroot contents
118 meh "Destroying chroot"
119 chflags -R noschg "${chroot_dir}" || wtf "noschg failed"
120 rm -Rf "${chroot_dir}" || wtf "chroot removal failed"
124 # Evaluate a command line within the chroot
126 chup? || wtf "Chroot not ready"
127 chroot "${chroot_dir}" env -i ${chroot_env} /bin/sh -c "cd ${chroot_pkg_dir}; ${*}"
133 local port="/usr/ports/${1##/usr/ports/}"
135 cheval "make -C ${port} ${*}"
141 # Port Dependency Tracking
145 # Translate port origin to package name
149 chmake "${1}" -V PKGNAME
154 # Build dependencies (shallow, recursive, package)
158 chmake "${1}" build-depends-list | sed -e 's#^/usr/ports/##'
163 # Clear cache if first value isn't '-r'
164 [ "${1}" = '-r' ] && shift || { : > "${_port_all_bdeps_cache}"; : > "${_port_all_rdeps_cache}"; }
165 # rdeps for rdeps are rdeps, bdeps for rdeps are bdeps, rdeps for bdeps are bdeps; thus:
167 port_bdeps "${@}" | while read port
169 [ "${VERBOSE_CACHE}" ] && logf "**** bdep cache %s: '%s'\n" "$(grep -q "${port}" \
170 "${_port_all_bdeps_cache}" && echo "hit" || echo "miss")" "${port}" >&2
171 if ! grep -q "${port}" "${_port_all_bdeps_cache}"
173 echo "${port}" >> "${_port_all_bdeps_cache}"
175 port_all_bdeps -r "${port}"
176 port_all_rdeps -r "${port}"
179 port_all_rdeps -r "${@}" | while read port
181 [ "${VERBOSE_CACHE}" ] && logf "**** bdep cache %s: '%s'\n" "$(grep -q "${port}" \
182 "${_port_all_bdeps_cache}" && echo "hit" || echo "miss")" "${port}" >&2
183 if ! grep -q "${port}" "${_port_all_bdeps_cache}"
185 echo "${port}" >> "${_port_all_bdeps_cache}"
186 port_all_bdeps -r "${port}"
192 port2pkg $(port_all_bdeps "${@}")
195 # Runtime dependencies (shallow, recursive, package)
199 chmake "${1}" run-depends-list | sed -e 's#^/usr/ports/##'
204 # Clear cache if first value isn't '-r'
205 [ "${1}" = '-r' ] && shift || { : > "${_port_all_rdeps_cache}"; }
206 port_rdeps "${@}" | while read port
208 [ "${VERBOSE_CACHE}" ] && logf "**** rdep cache %s: '%s'\n" "$(grep -q "${port}" \
209 "${_port_all_rdeps_cache}" && echo "hit" || echo "miss")" "${port}" >&2
210 if ! grep -q "${port}" "${_port_all_rdeps_cache}"
212 echo "${port}" >> "${_port_all_rdeps_cache}"
214 port_all_rdeps -r "${port}"
219 port2pkg $(port_all_rdeps "${@}")
222 # All dependencies (shallow, recursive, package)
226 chmake "${1}" all-depends-list | sed -e 's#^/usr/ports/##'
231 port_deps "${@}" | while read port
238 port2pkg $(port_all_deps "${@}")
241 # Dump a list of leaf ports from a working system
242 # Leaf ports are ports that have nothing depending upon them
243 # These are generally the top-level ports; everything else should
244 # be pulled in by them; on a system that has had updates applied
245 # this may pick up orphaned packages as well, so try cutleaves?
247 ( cd /var/db/pkg; ls -1 ) | while read pkg
249 pkg_info -Rq "${pkg}" | grep -q '' || pkg_info -oq "${pkg}"
253 # Display a tree of all build dependencies (and
254 # any runtime dependencies those build dependencies
255 # depend upon). Individual ports may appear more than
256 # once, as it is a tree and not a list.
258 port="${1##/usr/ports/}"
259 printf "%${2}s%s\n" "" "${port}"
261 ( chmake "${port}" build-depends-list
262 [ "${2:-0}" -gt 0 ] && chmake "${port}" run-depends-list
263 ) | sort -u | while read port
265 port_bdep_tree "${port}" $(( ${2:-0} + 1 ))
269 # Display a tree of all runtime dependencies. Individual
270 # ports may appear more than once, as it is a tree and
273 port="${1##/usr/ports/}"
274 printf "%${2}s%s\n" "" "${port}"
276 chmake "${port}" run-depends-list | sort -u | while read port
278 port_rdep_tree "${port}" $(( ${2:-0} + 1 ))
284 # Port Configuration Handling
288 # Run make config on a list of ports
292 meh "port config ${1}"
293 chmake "${1}" config < /dev/tty
298 # Make config-conditional for a list of ports, and all dependencies
299 port_config_recursive() {
300 # Clear cache if first value isn't '-r'
301 [ "${1}" = '-r' ] && shift || _port_config_recursive_cache=""
304 # Do not use config-recursive because it computes the depchain first;
305 # instead, manually compute the depchain after each config to ensure the
306 # proper depchain is followed. Use config-conditional to avoid dialog
307 # if the config is already complete. Also use a cache to avoid re-config
308 # and re-recurse on previously handled port branches.
309 if echo "${_port_config_recursive_cache}" | grep -qv " ${1} "
311 meh "port config-recursive ${1}"
312 chmake "${1}" config-conditional < /dev/tty
313 port_config_recursive -r $(port_deps "${1}")
314 _port_config_recursive_cache="${_port_config_recursive_cache} ${1} "
320 unset _port_config_recursive_cache
322 # Remove saved config for a list of ports
326 meh "port rmconfig ${1}"
327 chmake "${1}" rmconfig
332 # Remove saved config for a list of ports and all dependencies
333 port_rmconfig_recursive() {
334 meh "port rmconfig-recursive ${*}"
335 port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq)
338 # Obliterate saved configuration for all ports
339 port_rmconfig_all() {
340 meh "port rmconfig-all"
341 cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null"
344 # Restore port build options from cpio
346 [ -f "${conf_dir}/port_options.cpio" ] || return 0
347 meh "port load-config"
348 cheval "cd /var/db/ports; cpio -i" < "${conf_dir}/port_options.cpio"
351 # Dump port build options to cpio
353 meh "port save-config"
354 cheval "cd /var/db/ports; find . -type d -o -type f -name options | cpio -oHnewc" > "${conf_dir}/port_options.cpio.tmp" || wtf "port save-config failed"
355 mv -f "${conf_dir}/port_options.cpio.tmp" "${conf_dir}/port_options.cpio" || wtf "port save-config atomic commit failed"
360 # Port distfile handling
364 # Recursively retrieve distfiles
365 port_fetch_recursive() {
368 meh "fetch-recursive ${1}"
369 chmake "${1}" fetch-recursive
376 # Port building and packaging
380 # Copy in and install dependency packages
383 for pkg in $(port2pkg $(port_all_deps "${port}"))
385 cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}"
387 if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1
389 meh "Installing dependencies"
390 cheval "cd ${chroot_bdeps_dir}; pkg_add -F *" || wtf "port_load_deps ${port} failed"
394 # Build and install a port
397 meh "Building ${port}"
398 chmake "${port}" clean build install clean || wtf "port_build ${port} failed"
404 meh "Creating rdep package tree for ${port}"
405 cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed"
408 # Package port build dependencies, unless they're already run deps
410 meh "Stashing unsaved bdeps"
411 # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison)
412 #for pkg in $(pkg_bdeps "${1}")
414 # [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}"
417 # Instead, just save everything that's not already in rdeps as bdeps
418 for pkg in $(cheval pkg_info | awk '{print $1}')
420 if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps_dir}/${pkg}.tbz" ]
422 cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed"
427 # Copy generated packages out of tree
429 meh "Moving created packages to repo"
430 mkdir -p "${final_pkg_dir}"
431 ls "${pkg_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${pkg_dir}"/*.tbz "${final_pkg_dir}"
432 mkdir -p "${final_bdeps_dir}"
433 ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${bdeps_dir}"/*.tbz "${final_bdeps_dir}"
434 # link everything into ${bdeps_dir} so we can find it easily later
435 ( cd "${final_pkg_dir}"; find . -type f | cpio -plu --quiet "${final_bdeps_dir}" )
438 # Delete all installed packages (hope you saved them first)
440 meh "Clearing out installed packages"
441 cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed"
450 # Execute a complete port build, using prebuilt packages to fulfill dependencies when available
451 # Be sure to chsetup and populate your config before running!
454 meh "config-recursive"
455 port_config_recursive "${port}"
456 meh "fetch-recursive"
457 port_fetch_recursive "${port}"
459 port_load_deps "${port}"
463 port_package "${port}"
475 # Configuration variable setup
479 TARGET="${TARGET:-i386}"
480 CONFIG="${CONFIG:-GENERIC}"
482 ROOT="$(realpath "$(dirname "${0}")/../worlds")"
484 # Base directory for everything
485 base_dir="${ROOT}/${TARGET}/${CONFIG}"
487 # Directory holding configuration
488 conf_dir="${base_dir}/config"
490 # Root tree for chroot seeding
491 seed_dir="${base_dir}/root"
493 # Directory where distfiles will be stored between builds (common to all configs)
494 dist_dir="${ROOT}/seed/distfiles"
496 # Final directory for built packages (Outside chroot)
497 final_pkg_dir="${base_dir}/pkg"
498 final_bdeps_dir="${base_dir}/bdeps"
501 chroot_dir="${base_dir}/chroot"
503 # Package directories (must be under ${chroot_dir})
504 pkg_dir="${chroot_dir}/pkg"
505 bdeps_dir="${pkg_dir}/bdeps"
507 # Compute in-chroot pkg and bdeps dirs
508 chroot_pkg_dir="${pkg_dir##${chroot_dir}}"
509 chroot_bdeps_dir="${bdeps_dir##${chroot_dir}}"
511 # Cache files to speed up recursive bdep/rdep scanning
512 _port_all_bdeps_cache="${chroot_dir}/tmp/_port_all_bdeps_cache"
513 _port_all_rdeps_cache="${chroot_dir}/tmp/_port_all_rdeps_cache"
520 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
527 Configuration variables:
535 ------------------------------
536 chprepare Prepare chroot
537 chstartup Start up chroot
538 cheval Evaluate a command string within chroot
539 chmake Run port make within chroot
540 chshutdown Shut down chroot
541 chdestroy Destroy chroot
543 Port Dependency Tracking
544 ------------------------------
545 port2pkg Translate a port dirname to a package name
547 port_bdeps Compute port build dependencies
548 port_all_bdeps Recursively compute port build dependencies
549 pkg_bdeps port2pkg port_all_bdeps
551 port_rdeps Compute port run dependencies
552 port_all_rdeps Recursively compute port run dependencies
553 pkg_rdeps port2pkg port_all_rdeps
555 port_deps Compute port build and run dependencies
556 port_all_deps Recursively compute port build and run dependencies
557 pkg_deps port2pkg port_all_deps
559 leaf_ports Dump a list of leaf ports (ports with nothing depending upon them) from a running system
560 port_bdep_tree Display a tree of all build dependencies
561 port_rdep_tree Display a tree of all run dependencies
563 Port Configuration Handling
564 ------------------------------
565 port_config Make config for a port
566 port_config_recursive Make config recursive, recomputing depchain for each
567 port_rmconfig Remove saved configuration
568 port_rmconfig_recursive Remove saved configuration for a port and all dependencies
569 port_rmconfig_all Remove all saved configuration
570 port_load_config Restore port configuration from cpio
571 port_save_config Save port configuration to cpio
573 Port Distfile Handling
574 ------------------------------
575 port_fetch_recursive fetch-recursive
577 Port Building And Packaging
578 ------------------------------
587 ------------------------------
593 # Blind passthru for testing
596 # Todo: Clean up entrypoint to support proper options
597 # Todo: Add methods to autoprocess a ports.lst file to produce packages
598 # Todo: Unify chroot handling with makeworld