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 # Populate initial seed
28 mkdir -p "$(dirname "${chroot_dir}")" || wtf "chroot path create failed"
29 cp -pR "${seed_dir}" "${chroot_dir}" || wtf "chroot seeding failed"
31 # Create distfile cache
32 mkdir -p "${dist_dir}" || wtf "mkdir ${dist_dir} failed"
34 # Create mountpoint directories
35 mkdir -p "${chroot_dir}/dev" || wtf "mkdir /dev failed"
36 mkdir -p "${chroot_dir}/usr/src" || wtf "mkdir /usr/src failed"
37 mkdir -p "${chroot_dir}/usr/obj" || wtf "mkdir /usr/obj failed"
38 mkdir -p "${chroot_dir}/usr/ports" || wtf "mkdir /usr/ports failed"
39 mkdir -p "${chroot_dir}/var/ports/distfiles" || wtf "mkdir /var/ports/distfiles failed"
41 # Create pkg directories
42 mkdir -p "${pkg_dir}" || wtf "mkdir ${chroot_pkg_dir} failed"
43 mkdir -p "${bdeps_dir}" || wtf "mkdir ${chroot_bdeps_dir} failed"
45 # Copy in cached configuration, if necessary
46 [ -f "${conf_dir}/make.conf" ] && cp -p "${conf_dir}/make.conf" "${chroot_dir}/etc/make.conf"
47 [ -f "${conf_dir}/port_options.cpio" ] && ( cd ${chroot_dir}/var/db/ports; cpio -i ) < "${conf_dir}/port_options.cpio"
49 # If you have WITHOUT_INFO set in src.conf during buildworld/installworld, ports that need
50 # makeinfo and install-info will fail horribly; stub them
51 [ -f "${chroot_dir}/usr/bin/makeinfo" ] || ln -sf true "${chroot_dir}/usr/bin/makeinfo"
52 [ -f "${chroot_dir}/usr/bin/install-info" ] || ln -sf true "${chroot_dir}/usr/bin/install-info"
54 trap "" exit hup int term kill
55 meh "Chroot tree set up in ${chroot_dir}"
60 # Set up chroot mounts and runtime configuration
62 [ -d "${chroot_dir}" -a -f "${chroot_dir}/COPYRIGHT" -a -d "${chroot_dir}/dev" ] || wtf "Chroot not prepared"
65 # Rollback if a problem occurs during startup
66 trap "chshutdown" exit hup int term kill
68 meh "Starting up chroot"
70 # Necessary mountpoints
71 mount -t devfs devfs "${chroot_dir}/dev" || wtf "mount /dev failed"
72 mount -t nullfs -r /usr/src "${chroot_dir}/usr/src" || wtf "mount /usr/src failed"
73 mount -t nullfs -r /usr/ports "${chroot_dir}/usr/ports" || wtf "mount /usr/ports failed"
74 mount -t nullfs -w "${dist_dir}" "${chroot_dir}/var/ports/distfiles" || wtf "mount /var/ports/distfiles failed"
76 # Chroot configuration
77 cp -f /etc/resolv.conf "${chroot_dir}/etc/resolv.conf" || wtf "seeding /etc/resolv.conf failed"
79 trap "" exit hup int term kill
80 meh "Chroot up and running in ${chroot_dir}"
85 # Check if the chroot is probably ready for use
87 [ -d "${chroot_dir}" -a -f "${chroot_dir}/COPYRIGHT" -a -c "${chroot_dir}/dev/null" ]
91 # Unmount all chroot directories
93 # Short-circuit if nothing is mounted
94 mount | grep -q "${chroot_dir}" || return 0
95 meh "Shutting down chroot"
96 umount "${chroot_dir}/var/ports/distfiles"
97 umount "${chroot_dir}/usr/ports"
98 umount "${chroot_dir}/usr/src"
99 umount "${chroot_dir}/dev"
103 # Remove all chroot contents
106 meh "Destroying chroot"
107 chflags -R noschg "${chroot_dir}" || wtf "noschg failed"
108 rm -Rf "${chroot_dir}" || wtf "chroot removal failed"
112 # Evaluate a command line within the chroot
114 chup? || wtf "Chroot not ready"
115 chroot "${chroot_dir}" env -i ${chroot_env} /bin/sh -c "cd ${chroot_pkg_dir}; ${*}"
121 local port="/usr/ports/${1##/usr/ports/}"
123 cheval "make -C ${port} ${*}"
129 # Port Dependency Tracking
133 # Translate port origin to package name
137 chmake "${1}" -V PKGNAME
142 # Build dependencies (shallow, recursive, package)
146 chmake "${1}" build-depends-list | sed -e 's#^/usr/ports/##'
151 # Clear cache if first value isn't '-r'
152 [ "${1}" = '-r' ] && shift || { : > "${_port_all_bdeps_cache}"; : > "${_port_all_rdeps_cache}"; }
153 # rdeps for rdeps are rdeps, bdeps for rdeps are bdeps, rdeps for bdeps are bdeps; thus:
155 port_bdeps "${@}" | while read port
157 [ "${VERBOSE_CACHE}" ] && logf "**** bdep cache %s: '%s'\n" "$(grep -q "${port}" \
158 "${_port_all_bdeps_cache}" && echo "hit" || echo "miss")" "${port}" >&2
159 if ! grep -q "${port}" "${_port_all_bdeps_cache}"
161 echo "${port}" >> "${_port_all_bdeps_cache}"
163 port_all_bdeps -r "${port}"
164 port_all_rdeps -r "${port}"
167 port_all_rdeps -r "${@}" | 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}"
174 port_all_bdeps -r "${port}"
180 port2pkg $(port_all_bdeps "${@}")
183 # Runtime dependencies (shallow, recursive, package)
187 chmake "${1}" run-depends-list | sed -e 's#^/usr/ports/##'
192 # Clear cache if first value isn't '-r'
193 [ "${1}" = '-r' ] && shift || { : > "${_port_all_rdeps_cache}"; }
194 port_rdeps "${@}" | while read port
196 [ "${VERBOSE_CACHE}" ] && logf "**** rdep cache %s: '%s'\n" "$(grep -q "${port}" \
197 "${_port_all_rdeps_cache}" && echo "hit" || echo "miss")" "${port}" >&2
198 if ! grep -q "${port}" "${_port_all_rdeps_cache}"
200 echo "${port}" >> "${_port_all_rdeps_cache}"
202 port_all_rdeps -r "${port}"
207 port2pkg $(port_all_rdeps "${@}")
210 # All dependencies (shallow, recursive, package)
214 chmake "${1}" all-depends-list | sed -e 's#^/usr/ports/##'
219 port_deps "${@}" | while read port
226 port2pkg $(port_all_deps "${@}")
229 # Dump a list of leaf ports from a working system
230 # Leaf ports are ports that have nothing depending upon them
231 # These are generally the top-level ports; everything else should
232 # be pulled in by them; on a system that has had updates applied
233 # this may pick up orphaned packages as well, so try cutleaves?
235 ( cd /var/db/pkg; ls -1 ) | while read pkg
237 pkg_info -Rq "${pkg}" | grep -q '' || pkg_info -oq "${pkg}"
241 # Display a tree of all build dependencies (and
242 # any runtime dependencies those build dependencies
243 # depend upon). Individual ports may appear more than
244 # once, as it is a tree and not a list.
246 port="${1##/usr/ports/}"
247 printf "%${2}s%s\n" "" "${port}"
249 ( chmake "${port}" build-depends-list
250 [ "${2:-0}" -gt 0 ] && chmake "${port}" run-depends-list
251 ) | sort -u | while read port
253 port_bdep_tree "${port}" $(( ${2:-0} + 1 ))
257 # Display a tree of all runtime dependencies. Individual
258 # ports may appear more than once, as it is a tree and
261 port="${1##/usr/ports/}"
262 printf "%${2}s%s\n" "" "${port}"
264 chmake "${port}" run-depends-list | sort -u | while read port
266 port_rdep_tree "${port}" $(( ${2:-0} + 1 ))
272 # Port Configuration Handling
276 # Run make config on a list of ports
280 meh "port config ${1}"
281 chmake "${1}" config < /dev/tty
286 # Make config-conditional for a list of ports, and all dependencies
287 port_config_recursive() {
288 # Clear cache if first value isn't '-r'
289 [ "${1}" = '-r' ] && shift || _port_config_recursive_cache=""
292 # Do not use config-recursive because it computes the depchain first;
293 # instead, manually compute the depchain after each config to ensure the
294 # proper depchain is followed. Use config-conditional to avoid dialog
295 # if the config is already complete. Also use a cache to avoid re-config
296 # and re-recurse on previously handled port branches.
297 if echo "${_port_config_recursive_cache}" | grep -qv " ${1} "
299 meh "port config-recursive ${1}"
300 chmake "${1}" config-conditional < /dev/tty
301 port_config_recursive -r $(port_deps "${1}")
302 _port_config_recursive_cache="${_port_config_recursive_cache} ${1} "
308 unset _port_config_recursive_cache
310 # Remove saved config for a list of ports
314 meh "port rmconfig ${1}"
315 chmake "${1}" rmconfig
320 # Remove saved config for a list of ports and all dependencies
321 port_rmconfig_recursive() {
322 meh "port rmconfig-recursive ${*}"
323 port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq)
326 # Obliterate saved configuration for all ports
327 port_rmconfig_all() {
328 meh "port rmconfig-all"
329 cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null"
332 # Restore port build options from cpio
334 [ -f "${conf_dir}/port_options.cpio" ] || return 0
335 meh "port load-config"
336 cheval "cd /var/db/ports; cpio -i" < "${conf_dir}/port_options.cpio"
339 # Dump port build options to cpio
341 meh "port save-config"
342 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"
343 mv -f "${conf_dir}/port_options.cpio.tmp" "${conf_dir}/port_options.cpio" || wtf "port save-config atomic commit failed"
348 # Port distfile handling
352 # Recursively retrieve distfiles
353 port_fetch_recursive() {
356 meh "fetch-recursive ${1}"
357 chmake "${1}" fetch-recursive
364 # Port building and packaging
368 # Copy in and install dependency packages
371 for pkg in $(port2pkg $(port_all_deps "${port}"))
373 cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}"
375 if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1
377 meh "Installing dependencies"
378 cheval "cd ${chroot_bdeps_dir}; pkg_add -F *" || wtf "port_load_deps ${port} failed"
382 # Build and install a port
385 meh "Building ${port}"
386 chmake "${port}" clean build install clean || wtf "port_build ${port} failed"
392 meh "Creating rdep package tree for ${port}"
393 cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed"
396 # Package port build dependencies, unless they're already run deps
398 meh "Stashing unsaved bdeps"
399 # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison)
400 #for pkg in $(pkg_bdeps "${1}")
402 # [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}"
405 # Instead, just save everything that's not already in rdeps as bdeps
406 for pkg in $(cheval pkg_info | awk '{print $1}')
408 if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps_dir}/${pkg}.tbz" ]
410 cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed"
415 # Copy generated packages out of tree
417 meh "Moving created packages to repo"
418 mkdir -p "${final_pkg_dir}"
419 ls "${pkg_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${pkg_dir}"/*.tbz "${final_pkg_dir}"
420 mkdir -p "${final_bdeps_dir}"
421 ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${bdeps_dir}"/*.tbz "${final_bdeps_dir}"
422 # link everything into ${bdeps_dir} so we can find it easily later
423 ( cd "${final_pkg_dir}"; find . -type f | cpio -plu --quiet "${final_bdeps_dir}" )
426 # Delete all installed packages (hope you saved them first)
428 meh "Clearing out installed packages"
429 cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed"
438 # Execute a complete port build, using prebuilt packages to fulfill dependencies when available
439 # Be sure to chsetup and populate your config before running!
442 meh "config-recursive"
443 port_config_recursive "${port}"
444 meh "fetch-recursive"
445 port_fetch_recursive "${port}"
447 port_load_deps "${port}"
451 port_package "${port}"
463 # Configuration variable setup
467 TARGET="${TARGET:-i386}"
468 CONFIG="${CONFIG:-GENERIC}"
470 ROOT="$(realpath "$(dirname "${0}")/../worlds")"
472 # Base directory for everything
473 base_dir="${ROOT}/${TARGET}/${CONFIG}"
475 # Directory holding configuration
476 conf_dir="${base_dir}/config"
478 # Root tree for chroot seeding
479 seed_dir="${base_dir}/root"
481 # Directory where distfiles will be stored between builds (common to all configs)
482 dist_dir="${ROOT}/seed/distfiles"
484 # Final directory for built packages (Outside chroot)
485 final_pkg_dir="${base_dir}/pkg"
486 final_bdeps_dir="${base_dir}/bdeps"
489 chroot_dir="${base_dir}/chroot"
491 # Package directories (must be under ${chroot_dir})
492 pkg_dir="${chroot_dir}/pkg"
493 bdeps_dir="${pkg_dir}/bdeps"
495 # Compute in-chroot pkg and bdeps dirs
496 chroot_pkg_dir="${pkg_dir##${chroot_dir}}"
497 chroot_bdeps_dir="${bdeps_dir##${chroot_dir}}"
499 # Cache files to speed up recursive bdep/rdep scanning
500 _port_all_bdeps_cache="${chroot_dir}/tmp/_port_all_bdeps_cache"
501 _port_all_rdeps_cache="${chroot_dir}/tmp/_port_all_rdeps_cache"
508 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
513 # Blind passthru for testing
516 # Todo: Clean up entrypoint to support proper options
517 # Todo: Add methods to autoprocess a ports.lst file to produce packages
518 # Todo: Unify chroot handling with makeworld