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 # If you have WITHOUT_INFO set in src.conf during buildworld/installworld, ports that need
61 # makeinfo and install-info will fail horribly; stub them
62 [ -f "${chroot_dir}/usr/bin/makeinfo" ] || ln -sf true "${chroot_dir}/usr/bin/makeinfo"
63 [ -f "${chroot_dir}/usr/bin/install-info" ] || ln -sf true "${chroot_dir}/usr/bin/install-info"
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 trap "" exit hup int term kill
91 meh "Chroot up and running in ${chroot_dir}"
96 # Check if the chroot is probably ready for use
98 [ -d "${chroot_dir}" -a -f "${chroot_dir}/COPYRIGHT" -a -c "${chroot_dir}/dev/null" ]
102 # Unmount all chroot directories
104 # Short-circuit if nothing is mounted
105 mount | grep -q "${chroot_dir}" || return 0
106 meh "Shutting down chroot"
107 umount "${chroot_dir}/var/ports/distfiles"
108 umount "${chroot_dir}/usr/ports"
109 umount "${chroot_dir}/usr/src"
110 umount "${chroot_dir}/dev"
114 # Remove all chroot contents
117 meh "Destroying chroot"
118 chflags -R noschg "${chroot_dir}" || wtf "noschg failed"
119 rm -Rf "${chroot_dir}" || wtf "chroot removal failed"
123 # Evaluate a command line within the chroot
125 chup? || wtf "Chroot not ready"
126 chroot "${chroot_dir}" env -i ${chroot_env} /bin/sh -c "cd ${chroot_pkg_dir}; ${*}"
132 local port="/usr/ports/${1##/usr/ports/}"
134 cheval "make -C ${port} ${*}"
140 # Port Dependency Tracking
144 # Translate port origin to package name
148 chmake "${1}" -V PKGNAME
153 # Build dependencies (shallow, recursive, package)
157 chmake "${1}" build-depends-list | sed -e 's#^/usr/ports/##'
162 # Clear cache if first value isn't '-r'
163 [ "${1}" = '-r' ] && shift || { : > "${_port_all_bdeps_cache}"; : > "${_port_all_rdeps_cache}"; }
164 # rdeps for rdeps are rdeps, bdeps for rdeps are bdeps, rdeps for bdeps are bdeps; thus:
166 port_bdeps "${@}" | while read port
168 [ "${VERBOSE_CACHE}" ] && logf "**** bdep cache %s: '%s'\n" "$(grep -q "${port}" \
169 "${_port_all_bdeps_cache}" && echo "hit" || echo "miss")" "${port}" >&2
170 if ! grep -q "${port}" "${_port_all_bdeps_cache}"
172 echo "${port}" >> "${_port_all_bdeps_cache}"
174 port_all_bdeps -r "${port}"
175 port_all_rdeps -r "${port}"
178 port_all_rdeps -r "${@}" | while read port
180 [ "${VERBOSE_CACHE}" ] && logf "**** bdep cache %s: '%s'\n" "$(grep -q "${port}" \
181 "${_port_all_bdeps_cache}" && echo "hit" || echo "miss")" "${port}" >&2
182 if ! grep -q "${port}" "${_port_all_bdeps_cache}"
184 echo "${port}" >> "${_port_all_bdeps_cache}"
185 port_all_bdeps -r "${port}"
191 port2pkg $(port_all_bdeps "${@}")
194 # Runtime dependencies (shallow, recursive, package)
198 chmake "${1}" run-depends-list | sed -e 's#^/usr/ports/##'
203 # Clear cache if first value isn't '-r'
204 [ "${1}" = '-r' ] && shift || { : > "${_port_all_rdeps_cache}"; }
205 port_rdeps "${@}" | while read port
207 [ "${VERBOSE_CACHE}" ] && logf "**** rdep cache %s: '%s'\n" "$(grep -q "${port}" \
208 "${_port_all_rdeps_cache}" && echo "hit" || echo "miss")" "${port}" >&2
209 if ! grep -q "${port}" "${_port_all_rdeps_cache}"
211 echo "${port}" >> "${_port_all_rdeps_cache}"
213 port_all_rdeps -r "${port}"
218 port2pkg $(port_all_rdeps "${@}")
221 # All dependencies (shallow, recursive, package)
225 chmake "${1}" all-depends-list | sed -e 's#^/usr/ports/##'
230 port_deps "${@}" | while read port
237 port2pkg $(port_all_deps "${@}")
242 # Port Configuration Handling
246 # Run make config on a list of ports
250 meh "port config ${1}"
251 chmake "${1}" config < /dev/tty
256 # Make config-conditional for a list of ports, and all dependencies
257 port_config_recursive() {
258 # Clear cache if first value isn't '-r'
259 [ "${1}" = '-r' ] && shift || _port_config_recursive_cache=""
262 # Do not use config-recursive because it computes the depchain first;
263 # instead, manually compute the depchain after each config to ensure the
264 # proper depchain is followed. Use config-conditional to avoid dialog
265 # if the config is already complete. Also use a cache to avoid re-config
266 # and re-recurse on previously handled port branches.
267 if echo "${_port_config_recursive_cache}" | grep -qv " ${1} "
269 meh "port config-recursive ${1}"
270 chmake "${1}" config-conditional < /dev/tty
271 port_config_recursive -r $(port_deps "${1}")
272 _port_config_recursive_cache="${_port_config_recursive_cache} ${1} "
278 unset _port_config_recursive_cache
280 # Remove saved config for a list of ports
284 meh "port rmconfig ${1}"
285 chmake "${1}" rmconfig
290 # Remove saved config for a list of ports and all dependencies
291 port_rmconfig_recursive() {
292 meh "port rmconfig-recursive ${*}"
293 port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq)
296 # Obliterate saved configuration for all ports
297 port_rmconfig_all() {
298 meh "port rmconfig-all"
299 cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null"
302 # Restore port build options from cpio
304 [ -f "${conf_dir}/port_options.cpio" ] || return 0
305 meh "port load-config"
306 cheval "cd /var/db/ports; cpio -iv" < "${conf_dir}/port_options.cpio"
309 # Dump port build options to cpio
311 meh "port save-config"
312 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"
313 mv -f "${conf_dir}/port_options.cpio.tmp" "${conf_dir}/port_options.cpio" || wtf "port save-config atomic commit failed"
318 # Port distfile handling
322 # Recursively retrieve distfiles
323 port_fetch_recursive() {
326 meh "fetch-recursive ${1}"
327 chmake "${1}" fetch-recursive
334 # Port building and packaging
338 # Copy in and install dependency packages
341 for pkg in $(port2pkg $(port_all_deps "${port}"))
343 cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}"
345 if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1
347 meh "Installing dependencies"
348 cheval "cd ${chroot_bdeps_dir}; pkg_add -F *" || wtf "port_load_deps ${port} failed"
352 # Build and install a port
355 meh "Building ${port}"
356 chmake "${port}" clean build install clean || wtf "port_build ${port} failed"
362 meh "Creating rdep package tree for ${port}"
363 cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed"
366 # Package port build dependencies, unless they're already run deps
368 meh "Stashing unsaved bdeps"
369 # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison)
370 #for pkg in $(pkg_bdeps "${1}")
372 # [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}"
375 # Instead, just save everything that's not already in rdeps as bdeps
376 for pkg in $(cheval pkg_info | awk '{print $1}')
378 if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps_dir}/${pkg}.tbz" ]
380 cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed"
385 # Copy generated packages out of tree
387 meh "Moving created packages to repo"
388 mkdir -p "${final_pkg_dir}"
389 ls "${pkg_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${pkg_dir}"/*.tbz "${final_pkg_dir}"
390 mkdir -p "${final_bdeps_dir}"
391 ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${bdeps_dir}"/*.tbz "${final_bdeps_dir}"
392 # link everything into ${bdeps_dir} so we can find it easily later
393 ( cd "${final_pkg_dir}"; find . -type f | cpio -plu --quiet "${final_bdeps_dir}" )
396 # Delete all installed packages (hope you saved them first)
398 meh "Clearing out installed packages"
399 cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed"
408 # Execute a complete port build, using prebuilt packages to fulfill dependencies when available
409 # Be sure to chsetup and populate your config before running!
412 meh "config-recursive"
413 port_config_recursive "${port}"
414 meh "fetch-recursive"
415 port_fetch_recursive "${port}"
417 port_load_deps "${port}"
421 port_package "${port}"
433 # Configuration variable setup
437 TARGET="${TARGET:-i386}"
438 CONFIG="${CONFIG:-GENERIC}"
440 ROOT="$(realpath "$(dirname "${0}")/../worlds")"
442 # Base directory for everything
443 base_dir="${ROOT}/${TARGET}/${CONFIG}"
445 # Directory holding configuration
446 conf_dir="${base_dir}/config"
448 # Root tree for chroot seeding
449 seed_dir="${base_dir}/root"
451 # Directory where distfiles will be stored between builds (common to all configs)
452 dist_dir="${ROOT}/seed/distfiles"
454 # Final directory for built packages (Outside chroot)
455 final_pkg_dir="${base_dir}/pkg"
456 final_bdeps_dir="${base_dir}/bdeps"
459 chroot_dir="${base_dir}/chroot"
461 # Package directories (must be under ${chroot_dir})
462 pkg_dir="${chroot_dir}/pkg"
463 bdeps_dir="${pkg_dir}/bdeps"
465 # Compute in-chroot pkg and bdeps dirs
466 chroot_pkg_dir="${pkg_dir##${chroot_dir}}"
467 chroot_bdeps_dir="${bdeps_dir##${chroot_dir}}"
469 # Cache files to speed up recursive bdep/rdep scanning
470 _port_all_bdeps_cache="${chroot_dir}/tmp/_port_all_bdeps_cache"
471 _port_all_rdeps_cache="${chroot_dir}/tmp/_port_all_rdeps_cache"
478 PATH=:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
483 # Blind passthru for testing