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 # rdeps for rdeps are rdeps, rdeps for bdeps are bdeps; thus:
163 ( port_bdeps "${@}"; port_all_rdeps "${@}" ) | while read port
165 port_all_bdeps "${port}"
166 port_all_rdeps "${port}"
170 port2pkg $(port_all_bdeps "${@}")
173 # Runtime dependencies (shallow, recursive, package)
177 chmake "${1}" run-depends-list | sed -e 's#^/usr/ports/##'
182 port_rdeps "${@}" | while read port
185 port_all_rdeps "${port}"
189 port2pkg $(port_all_rdeps "${@}")
192 # All dependencies (shallow, recursive, package)
196 chmake "${1}" all-depends-list | sed -e 's#^/usr/ports/##'
201 port_deps "${@}" | while read port
208 port2pkg $(port_all_deps "${@}")
213 # Port Configuration Handling
217 # Run make config on a list of ports
221 meh "port config ${1}"
222 chmake "${1}" config < /dev/tty
227 # Make config-conditional for a list of ports, and all dependencies
228 port_config_recursive() {
229 # Clear cache if first value isn't '-r'
230 [ "${1}" = '-r' ] && shift || _port_config_recursive_cache=""
233 # Do not use config-recursive because it computes the depchain first;
234 # instead, manually compute the depchain after each config to ensure the
235 # proper depchain is followed. Use config-conditional to avoid dialog
236 # if the config is already complete. Also use a cache to avoid re-config
237 # and re-recurse on previously handled port branches.
238 if echo "${_port_config_recursive_cache}" | grep -qv " ${1} "
240 meh "port config-recursive ${1}"
241 chmake "${1}" config-conditional < /dev/tty
242 port_config_recursive -r $(port_deps "${1}")
243 _port_config_recursive_cache="${_port_config_recursive_cache} ${1} "
249 unset _port_config_recursive_cache
251 # Remove saved config for a list of ports
255 meh "port rmconfig ${1}"
256 chmake "${1}" rmconfig
261 # Remove saved config for a list of ports and all dependencies
262 port_rmconfig_recursive() {
263 meh "port rmconfig-recursive ${*}"
264 port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq)
267 # Obliterate saved configuration for all ports
268 port_rmconfig_all() {
269 meh "port rmconfig-all"
270 cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null"
273 # Restore port build options from cpio
275 [ -f "${conf_dir}/port_options.cpio" ] || return 0
276 meh "port load-config"
277 cheval "cd /var/db/ports; cpio -iv" < "${conf_dir}/port_options.cpio"
280 # Dump port build options to cpio
282 meh "port save-config"
283 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"
284 mv -f "${conf_dir}/port_options.cpio.tmp" "${conf_dir}/port_options.cpio" || wtf "port save-config atomic commit failed"
289 # Port distfile handling
293 # Recursively retrieve distfiles
294 port_fetch_recursive() {
297 meh "fetch-recursive ${1}"
298 chmake "${1}" fetch-recursive
305 # Port building and packaging
309 # Copy in and install dependency packages
312 for pkg in $(port2pkg $(port_all_deps "${port}"))
314 cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}"
316 if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1
318 meh "Installing dependencies"
319 cheval "cd ${chroot_bdeps_dir}; pkg_add -F *" || wtf "port_load_deps ${port} failed"
323 # Build and install a port
326 meh "Building ${port}"
327 chmake "${port}" clean build install clean || wtf "port_build ${port} failed"
333 meh "Creating rdep package tree for ${port}"
334 cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed"
337 # Package port build dependencies, unless they're already run deps
339 meh "Stashing unsaved bdeps"
340 # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison)
341 #for pkg in $(pkg_bdeps "${1}")
343 # [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}"
346 # Instead, just save everything that's not already in rdeps as bdeps
347 for pkg in $(cheval pkg_info | awk '{print $1}')
349 if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps_dir}/${pkg}.tbz" ]
351 cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed"
356 # Copy generated packages out of tree
358 meh "Moving created packages to repo"
359 mkdir -p "${final_pkg_dir}"
360 ls "${pkg_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${pkg_dir}"/*.tbz "${final_pkg_dir}"
361 mkdir -p "${final_bdeps_dir}"
362 ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${bdeps_dir}"/*.tbz "${final_bdeps_dir}"
363 # link everything into ${bdeps_dir} so we can find it easily later
364 ( cd "${final_pkg_dir}"; find . -type f | cpio -plu --quiet "${final_bdeps_dir}" )
367 # Delete all installed packages (hope you saved them first)
369 meh "Clearing out installed packages"
370 cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed"
379 # Execute a complete port build, using prebuilt packages to fulfill dependencies when available
380 # Be sure to chsetup and populate your config before running!
383 port_load_deps "${port}"
385 port_package "${port}"
394 # Configuration variable setup
398 TARGET="${TARGET:-i386}"
399 CONFIG="${CONFIG:-GENERIC}"
401 ROOT="$(realpath "$(dirname "${0}")/../worlds")"
403 # Base directory for everything
404 base_dir="${ROOT}/${TARGET}/${CONFIG}"
406 # Directory holding configuration
407 conf_dir="${base_dir}/config"
409 # Root tree for chroot seeding
410 seed_dir="${base_dir}/root"
412 # Directory where distfiles will be stored between builds (common to all configs)
413 dist_dir="${ROOT}/seed/distfiles"
415 # Final directory for built packages (Outside chroot)
416 final_pkg_dir="${base_dir}/pkg"
417 final_bdeps_dir="${base_dir}/bdeps"
420 chroot_dir="${base_dir}/chroot"
422 # Package directories (must be under ${chroot_dir})
423 pkg_dir="${chroot_dir}/pkg"
424 bdeps_dir="${pkg_dir}/bdeps"
426 # Compute in-chroot pkg and bdeps dirs
427 chroot_pkg_dir="${pkg_dir##${chroot_dir}}"
428 chroot_bdeps_dir="${bdeps_dir##${chroot_dir}}"
435 PATH=:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
440 # Blind passthru for testing