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 ( cd /usr/ports/${port}
250 ( make build-depends-list
251 [ "${2:-0}" -gt 0 ] && make run-depends-list
252 ) | sort -u | while read port
254 bdep_tree "${port}" $(( ${2:-0} + 1 ))
259 # Display a tree of all runtime dependencies. Individual
260 # ports may appear more than once, as it is a tree and
263 port="${1##/usr/ports/}"
264 printf "%${2}s%s\n" "" "${port}"
266 ( cd /usr/ports/${port}
267 make run-depends-list | sort -u | while read port
269 rdep_tree "${port}" $(( ${2:-0} + 1 ))
276 # Port Configuration Handling
280 # Run make config on a list of ports
284 meh "port config ${1}"
285 chmake "${1}" config < /dev/tty
290 # Make config-conditional for a list of ports, and all dependencies
291 port_config_recursive() {
292 # Clear cache if first value isn't '-r'
293 [ "${1}" = '-r' ] && shift || _port_config_recursive_cache=""
296 # Do not use config-recursive because it computes the depchain first;
297 # instead, manually compute the depchain after each config to ensure the
298 # proper depchain is followed. Use config-conditional to avoid dialog
299 # if the config is already complete. Also use a cache to avoid re-config
300 # and re-recurse on previously handled port branches.
301 if echo "${_port_config_recursive_cache}" | grep -qv " ${1} "
303 meh "port config-recursive ${1}"
304 chmake "${1}" config-conditional < /dev/tty
305 port_config_recursive -r $(port_deps "${1}")
306 _port_config_recursive_cache="${_port_config_recursive_cache} ${1} "
312 unset _port_config_recursive_cache
314 # Remove saved config for a list of ports
318 meh "port rmconfig ${1}"
319 chmake "${1}" rmconfig
324 # Remove saved config for a list of ports and all dependencies
325 port_rmconfig_recursive() {
326 meh "port rmconfig-recursive ${*}"
327 port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq)
330 # Obliterate saved configuration for all ports
331 port_rmconfig_all() {
332 meh "port rmconfig-all"
333 cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null"
336 # Restore port build options from cpio
338 [ -f "${conf_dir}/port_options.cpio" ] || return 0
339 meh "port load-config"
340 cheval "cd /var/db/ports; cpio -i" < "${conf_dir}/port_options.cpio"
343 # Dump port build options to cpio
345 meh "port save-config"
346 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"
347 mv -f "${conf_dir}/port_options.cpio.tmp" "${conf_dir}/port_options.cpio" || wtf "port save-config atomic commit failed"
352 # Port distfile handling
356 # Recursively retrieve distfiles
357 port_fetch_recursive() {
360 meh "fetch-recursive ${1}"
361 chmake "${1}" fetch-recursive
368 # Port building and packaging
372 # Copy in and install dependency packages
375 for pkg in $(port2pkg $(port_all_deps "${port}"))
377 cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}"
379 if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1
381 meh "Installing dependencies"
382 cheval "cd ${chroot_bdeps_dir}; pkg_add -F *" || wtf "port_load_deps ${port} failed"
386 # Build and install a port
389 meh "Building ${port}"
390 chmake "${port}" clean build install clean || wtf "port_build ${port} failed"
396 meh "Creating rdep package tree for ${port}"
397 cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed"
400 # Package port build dependencies, unless they're already run deps
402 meh "Stashing unsaved bdeps"
403 # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison)
404 #for pkg in $(pkg_bdeps "${1}")
406 # [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}"
409 # Instead, just save everything that's not already in rdeps as bdeps
410 for pkg in $(cheval pkg_info | awk '{print $1}')
412 if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps_dir}/${pkg}.tbz" ]
414 cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed"
419 # Copy generated packages out of tree
421 meh "Moving created packages to repo"
422 mkdir -p "${final_pkg_dir}"
423 ls "${pkg_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${pkg_dir}"/*.tbz "${final_pkg_dir}"
424 mkdir -p "${final_bdeps_dir}"
425 ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${bdeps_dir}"/*.tbz "${final_bdeps_dir}"
426 # link everything into ${bdeps_dir} so we can find it easily later
427 ( cd "${final_pkg_dir}"; find . -type f | cpio -plu --quiet "${final_bdeps_dir}" )
430 # Delete all installed packages (hope you saved them first)
432 meh "Clearing out installed packages"
433 cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed"
442 # Execute a complete port build, using prebuilt packages to fulfill dependencies when available
443 # Be sure to chsetup and populate your config before running!
446 meh "config-recursive"
447 port_config_recursive "${port}"
448 meh "fetch-recursive"
449 port_fetch_recursive "${port}"
451 port_load_deps "${port}"
455 port_package "${port}"
467 # Configuration variable setup
471 TARGET="${TARGET:-i386}"
472 CONFIG="${CONFIG:-GENERIC}"
474 ROOT="$(realpath "$(dirname "${0}")/../worlds")"
476 # Base directory for everything
477 base_dir="${ROOT}/${TARGET}/${CONFIG}"
479 # Directory holding configuration
480 conf_dir="${base_dir}/config"
482 # Root tree for chroot seeding
483 seed_dir="${base_dir}/root"
485 # Directory where distfiles will be stored between builds (common to all configs)
486 dist_dir="${ROOT}/seed/distfiles"
488 # Final directory for built packages (Outside chroot)
489 final_pkg_dir="${base_dir}/pkg"
490 final_bdeps_dir="${base_dir}/bdeps"
493 chroot_dir="${base_dir}/chroot"
495 # Package directories (must be under ${chroot_dir})
496 pkg_dir="${chroot_dir}/pkg"
497 bdeps_dir="${pkg_dir}/bdeps"
499 # Compute in-chroot pkg and bdeps dirs
500 chroot_pkg_dir="${pkg_dir##${chroot_dir}}"
501 chroot_bdeps_dir="${bdeps_dir##${chroot_dir}}"
503 # Cache files to speed up recursive bdep/rdep scanning
504 _port_all_bdeps_cache="${chroot_dir}/tmp/_port_all_bdeps_cache"
505 _port_all_rdeps_cache="${chroot_dir}/tmp/_port_all_rdeps_cache"
512 PATH=:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
517 # Blind passthru for testing
520 # Todo: Clean up entrypoint to support proper options
521 # Todo: Add methods to autoprocess a ports.lst file to produce packages
522 # Todo: Unify chroot handling with makeworld