3 # Load shlib and modules
4 _root="$(dirname "${0}")"; . "${_root}/lib/env.sh"
7 # Dump a list of leaf ports from a working system
8 # Leaf ports are ports that have nothing depending upon them
9 # These are generally the top-level ports; everything else should
10 # be pulled in by them
12 ( cd /var/db/pkg; ls -1 ) | while read pkg
14 pkg_info -Rq "${pkg}" | grep -q '' || pkg_info -oq "${pkg}"
24 # Prepare a chroot for work
26 # Verify environment sanity
27 [ -d "${chroot_dir}" ] && omg "${chroot_dir}: directory exists; purging" && chdestroy
28 mount | grep -q "${chroot_dir}" && wtf "Stuff is mounted under ${chroot_dir}; cannot continue"
29 [ -d "${seed_dir}" -a -f "${seed_dir}/COPYRIGHT" ] || wtf "Use 'makeworld' to create a root build first"
30 [ -f "/usr/ports/UPDATING" ] || wtf "Need ports tree in /usr/ports to build"
31 [ -f "/usr/src/sys/conf/newvers.sh" ] || omg "No base sourcetree in /usr/src"
33 meh "Setting up chroot tree"
35 # Just in case we're aborted partway through the prepare
36 trap "chdestroy" exit hup int term kill
38 # Populate initial seed
39 mkdir -p "$(dirname "${chroot_dir}")" || wtf "chroot path create failed"
40 cp -pR "${seed_dir}" "${chroot_dir}" || wtf "chroot seeding failed"
42 # Create distfile cache
43 mkdir -p "${dist_dir}" || wtf "mkdir ${dist_dir} failed"
45 # Create mountpoint directories
46 mkdir -p "${chroot_dir}/dev" || wtf "mkdir /dev failed"
47 mkdir -p "${chroot_dir}/usr/src" || wtf "mkdir /usr/src failed"
48 mkdir -p "${chroot_dir}/usr/obj" || wtf "mkdir /usr/obj failed"
49 mkdir -p "${chroot_dir}/usr/ports" || wtf "mkdir /usr/ports failed"
50 mkdir -p "${chroot_dir}/var/ports/distfiles" || wtf "mkdir /var/ports/distfiles failed"
52 # Create pkg directories
53 mkdir -p "${pkg_dir}" || wtf "mkdir ${chroot_pkg_dir} failed"
54 mkdir -p "${bdeps_dir}" || wtf "mkdir ${chroot_bdeps_dir} failed"
56 # Copy in cached configuration, if necessary
57 [ -f "${conf_dir}/make.conf" ] && cp -p "${conf_dir}/make.conf" "${chroot_dir}/etc/make.conf"
58 [ -f "${conf_dir}/port_options.cpio" ] && ( cd ${chroot_dir}/var/db/ports; cpio -iv ) < "${conf_dir}/port_options.cpio"
60 trap "" exit hup int term kill
61 meh "Chroot tree set up in ${chroot_dir}"
66 # Set up chroot mounts and runtime configuration
68 [ -d "${chroot_dir}" -a -f "${chroot_dir}/COPYRIGHT" -a -d "${chroot_dir}/dev" ] || wtf "Chroot not prepared"
70 # Rollback if a problem occurs during startup
71 trap "chshutdown" exit hup int term kill
73 meh "Starting up chroot"
75 # Necessary mountpoints
76 mount -t devfs devfs "${chroot_dir}/dev" || wtf "mount /dev failed"
77 mount -t nullfs -r /usr/src "${chroot_dir}/usr/src" || wtf "mount /usr/src failed"
78 mount -t nullfs -r /usr/ports "${chroot_dir}/usr/ports" || wtf "mount /usr/ports failed"
79 mount -t nullfs -w "${dist_dir}" "${chroot_dir}/var/ports/distfiles" || wtf "mount /var/ports/distfiles failed"
81 # Chroot configuration
82 cp -f /etc/resolv.conf "${chroot_dir}/etc/resolv.conf" || wtf "seeding /etc/resolv.conf failed"
84 trap "" exit hup int term kill
85 meh "Chroot up and running in ${chroot_dir}"
90 # Unmount all chroot directories
92 # Short-circuit if nothing is mounted
93 mount | grep -q "${chroot_dir}" || return 0
94 meh "Shutting down chroot"
95 umount "${chroot_dir}/var/ports/distfiles"
96 umount "${chroot_dir}/usr/ports"
97 umount "${chroot_dir}/usr/src"
98 umount "${chroot_dir}/dev"
102 # Remove all chroot contents
105 meh "Destroying chroot"
106 chflags -R noschg "${chroot_dir}" || wtf "noschg failed"
107 rm -Rf "${chroot_dir}" || wtf "chroot removal failed"
111 # Evaluate a command line within the chroot
113 chroot "${chroot_dir}" env -i ${chroot_env} /bin/sh -c "cd ${chroot_pkg_dir}; ${*}"
119 local port="/usr/ports/${1##/usr/ports/}"
121 cheval "make -C ${port} ${*}"
127 # Port Dependency Tracking
131 # Translate port origin to package name
135 chmake "${1}" -V PKGNAME
140 # Build dependencies (shallow, recursive, package)
144 chmake "${1}" build-depends-list | sed -e 's#^/usr/ports/##'
149 # rdeps for rdeps are rdeps, rdeps for bdeps are bdeps; thus:
150 ( port_bdeps "${@}"; port_all_rdeps "${@}" ) | while read port
152 port_all_bdeps "${port}"
153 port_all_rdeps "${port}"
157 port2pkg $(port_all_bdeps "${@}")
160 # Runtime dependencies (shallow, recursive, package)
164 chmake "${1}" run-depends-list | sed -e 's#^/usr/ports/##'
169 port_rdeps "${@}" | while read port
172 port_all_rdeps "${port}"
176 port2pkg $(port_all_rdeps "${@}")
179 # All dependencies (shallow, recursive, package)
183 chmake "${1}" all-depends-list | sed -e 's#^/usr/ports/##'
188 port_deps "${@}" | while read port
195 port2pkg $(port_all_deps "${@}")
200 # Port Configuration Handling
204 # Run make config on a list of ports
208 meh "port config ${1}"
209 chmake "${1}" config < /dev/tty
214 # Make config-conditional for a list of ports, and all dependencies
215 port_config_recursive() {
216 # Clear cache if first value isn't '-r'
217 [ "${1}" = '-r' ] && shift || _port_config_recursive_cache=""
220 # Do not use config-recursive because it computes the depchain first;
221 # instead, manually compute the depchain after each config to ensure the
222 # proper depchain is followed. Use config-conditional to avoid dialog
223 # if the config is already complete. Also use a cache to avoid re-config
224 # and re-recurse on previously handled port branches.
225 if echo "${_port_config_recursive_cache}" | grep -qv " ${1} "
227 meh "port config-recursive ${1}"
228 chmake "${1}" config-conditional < /dev/tty
229 port_config_recursive -r $(port_deps "${1}")
230 _port_config_recursive_cache="${_port_config_recursive_cache} ${1} "
236 unset _port_config_recursive_cache
238 # Remove saved config for a list of ports
242 meh "port rmconfig ${1}"
243 chmake "${1}" rmconfig
248 # Remove saved config for a list of ports and all dependencies
249 port_rmconfig_recursive() {
250 meh "port rmconfig-recursive ${*}"
251 port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq)
254 # Obliterate saved configuration for all ports
255 port_rmconfig_all() {
256 meh "port rmconfig-all"
257 cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null"
260 # Restore port build options from cpio
262 [ -f "${conf_dir}/port_options.cpio" ] || return 0
263 meh "port load-config"
264 cheval "cd /var/db/ports; cpio -iv" < "${conf_dir}/port_options.cpio"
267 # Dump port build options to cpio
269 meh "port save-config"
270 cheval "cd /var/db/ports; find . -type d -o -type f -name options | cpio -ovHnewc" > "${conf_dir}/port_options.cpio.tmp" || wtf "port save-config failed"
271 mv -f "${conf_dir}/port_options.cpio.tmp" "${conf_dir}/port_options.cpio" || wtf "port save-config atomic commit failed"
276 # Port distfile handling
280 # Recursively retrieve distfiles
281 port_fetch_recursive() {
284 meh "fetch-recursive ${1}"
285 chmake "${1}" fetch-recursive
292 # Port building and packaging
296 # Copy in and install dependency packages
299 for pkg in $(port2pkg $(port_all_deps "${port}"))
301 cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}"
303 if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1
305 meh "Installing dependencies"
306 cheval "cd ${chroot_bdeps_dir}; pkg_add -F *" || wtf "port_load_deps ${port} failed"
310 # Build and install a port
313 meh "Building ${port}"
314 chmake "${port}" clean build install clean || wtf "port_build ${port} failed"
320 meh "Creating rdep package tree for ${port}"
321 cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed"
324 # Package port build dependencies, unless they're already run deps
326 meh "Stashing unsaved bdeps"
327 # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison)
328 #for pkg in $(pkg_bdeps "${1}")
330 # [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}"
333 # Instead, just save everything that's not already in rdeps as bdeps
334 for pkg in $(cheval pkg_info | awk '{print $1}')
336 if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps_dir}/${pkg}.tbz" ]
338 cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed"
343 # Copy generated packages out of tree
345 meh "Moving created packages to repo"
346 mkdir -p "${final_pkg_dir}"
347 ls "${pkg_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${pkg_dir}"/*.tbz "${final_pkg_dir}"
348 mkdir -p "${final_bdeps_dir}"
349 ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${bdeps_dir}"/*.tbz "${final_bdeps_dir}"
350 # link everything into ${bdeps_dir} so we can find it easily later
351 ( cd "${final_pkg_dir}"; find . -type f | cpio -plu --quiet "${final_bdeps_dir}" )
354 # Delete all installed packages (hope you saved them first)
356 meh "Clearing out installed packages"
357 cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed"
366 # Execute a complete port build, using prebuilt packages to fulfill dependencies when available
367 # Be sure to chsetup and populate your config before running!
370 port_load_deps "${port}"
372 port_package "${port}"
381 # Configuration variable setup
385 TARGET="${TARGET:-i386}"
386 CONFIG="${CONFIG:-GENERIC}"
388 ROOT="$(realpath "$(dirname "${0}")/../worlds")"
390 # Base directory for everything
391 base_dir="${ROOT}/${TARGET}/${CONFIG}"
393 # Directory holding configuration
394 conf_dir="${base_dir}/config"
396 # Root tree for chroot seeding
397 seed_dir="${base_dir}/root"
399 # Directory where distfiles will be stored between builds (common to all configs)
400 dist_dir="${ROOT}/seed/distfiles"
402 # Final directory for built packages (Outside chroot)
403 final_pkg_dir="${base_dir}/pkg"
404 final_bdeps_dir="${base_dir}/bdeps"
407 chroot_dir="${base_dir}/chroot"
409 # Package directories (must be under ${chroot_dir})
410 pkg_dir="${chroot_dir}/pkg"
411 bdeps_dir="${pkg_dir}/bdeps"
413 # Compute in-chroot pkg and bdeps dirs
414 chroot_pkg_dir="${pkg_dir##${chroot_dir}}"
415 chroot_bdeps_dir="${bdeps_dir##${chroot_dir}}"
422 PATH=:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
427 # Blind passthru for testing