1 # Chroot build handler functions
3 if [ -z "${__chroot_sh_loaded}" ]
7 # Where should this one go?
9 # Dump a list of leaf ports from a working system
10 # Leaf ports are ports that have nothing depending upon them
11 # These are generally the top-level ports; everything else should
12 # be pulled in by them
14 ( cd /var/db/pkg; ls -1 ) | while read pkg
16 pkg_info -Rq "${pkg}" | grep -q '' || pkg_info -oq "${pkg}"
22 # Configuration variable setup
26 # Base directory for everything
27 base_dir="${basedir:-/usr/home/cyberleo/vitanitest}"
29 # Ports tree to use for building
30 ports_dir="${ports_dir:-/usr/ports}"
32 # Directory where distfiles will be stored between builds
33 dist_dir="${dist_dir:-${base_dir}/distfiles}"
35 # Final directory for built packages (Outside chroot)
36 final_pkg_dir="${final_pkg_dir:-${base_dir}/pkg}"
37 final_bdeps_dir="${final_bdeps_dir:-${base_dir}/bdeps}"
40 chroot_dir="${chroot_dir:-${base_dir}/root}"
42 # Package directories, must be under ${chroot_dir}
43 pkg_dir="${pkg_dir:-${chroot_dir}/pkg}"
44 bdeps_dir="${bdeps_dir:-${pkg_dir}/bdeps}"
46 # Compute in-chroot pkg and bdeps dirs
47 chroot_pkg_dir="${chroot_pkg_dir:-${pkg_dir##${chroot_dir}}}"
48 chroot_bdeps_dir="${chroot_bdeps_dir:-${bdeps_dir##${chroot_dir}}}"
51 chroot_env="${chroot_env:-
55 PATH=:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
62 # Boilerplate functions
66 meh() { printf " \033[1;32m*\033[0m %s\n" "${*}" >&2; }
67 omg() { printf " \033[1;33m*\033[0m %s\n" "${*}" >&2; }
68 wtf() { printf " \033[1;31m*\033[0m %s\n" "${*}" >&2; exit 1; }
78 # Necessary mountpoints
79 mkdir -p "${chroot_dir}/dev" || return 1
80 mount -t devfs devfs "${chroot_dir}/dev" || return 1
81 mkdir -p "${chroot_dir}/usr/ports" || return 1
82 mount -t nullfs -r "${ports_dir}" "${chroot_dir}/usr/ports" || return 1
83 mkdir -p "${chroot_dir}/var/ports/distfiles" || return 1
84 mount -t nullfs -w "${dist_dir}" "${chroot_dir}/var/ports/distfiles" || return 1
86 # Chroot configuration
87 cp -f /etc/resolv.conf "${chroot_dir}/etc/resolv.conf" || return 1
89 # Target package directories
90 mkdir -p "${pkg_dir}" || return 1
91 mkdir -p "${bdeps_dir}" || return 1
96 umount "${chroot_dir}/var/ports/distfiles"
97 umount "${chroot_dir}/usr/ports"
98 umount "${chroot_dir}/dev"
101 # Evaluate a command line within the chroot
103 chroot "${chroot_dir}" env -i ${chroot_env} /bin/sh -c "cd ${chroot_pkg_dir}; ${*}"
108 local port="/usr/ports/${1##/usr/ports/}"
110 cheval "make -C ${port} ${*}"
115 # Port Dependency Tracking
119 # Translate port origin to package name
123 chmake "${1}" -V PKGNAME
128 # Build dependencies (shallow, recursive, package)
132 chmake "${1}" build-depends-list | sed -e 's#^/usr/ports/##'
136 port_all_bdeps() { # Test this one further; I'm not confident in the logic
137 # rdeps for rdeps are rdeps, rdeps for bdeps are bdeps; thus:
139 # Handle bdeps, bdeps of bdeps, and rdeps of bdeps
140 port_bdeps "${@}" | while read port
143 port_all_bdeps "${port}"
144 port_all_rdeps "${port}"
146 # Handle bdeps of rdeps
147 port_all_rdeps "${@}" | while read port
149 port_all_bdeps "${port}"
154 port2pkg $(port_all_bdeps "${@}")
157 # Runtime dependencies (shallow, recursive, package)
161 chmake "${1}" run-depends-list | sed -e 's#^/usr/ports/##'
166 port_rdeps "${@}" | while read port
169 port_all_rdeps "${port}"
173 port2pkg $(port_all_rdeps "${@}")
176 # All dependencies (shallow, recursive, package)
180 chmake "${1}" all-depends-list | sed -e 's#^/usr/ports/##'
185 port_deps "${@}" | while read port
192 port2pkg $(port_all_deps "${@}")
197 # Port Configuration Handling
201 # Run make config on a list of ports
205 meh "port config ${1}"
206 chmake "${1}" config < /dev/tty
211 # Make config-conditional for a list of ports, and all dependencies
212 port_config_recursive() {
213 # If the first parameter is not --, then clear the cache
214 [ "${1}" == "--" ] && shift || unset _port_config_recursive_cache
218 # Do not use config-recursive because it computes the depchain first;
219 # instead, manually compute the depchain after each config to ensure the
220 # proper depchain is followed. Use config-conditional to avoid dialog
221 # if the config is already complete. Also use a cache to avoid re-config
222 # and re-recurse on previously handled port branches.
223 if echo "${_port_config_recursive_cache}" | grep -qv " ${1} "
225 meh "port config-recursive ${1}"
226 chmake "${1}" config-conditional < /dev/tty
227 port_config_recursive -- $(port_deps "${1}")
228 _port_config_recursive_cache="${_port_config_recursive_cache} ${1} "
234 # Remove saved config for a list of ports
238 meh "port rmconfig ${1}"
239 chmake "${1}" rmconfig
244 # Remove saved config for a list of ports and all dependencies
245 port_rmconfig_recursive() {
246 meh "port rmconfig-recursive ${*}"
247 port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq)
250 # Obliterate saved configuration for all ports
251 port_rmconfig_all() {
252 meh "port rmconfig-all"
253 cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null"
256 # Restore port build options from cpio
258 meh "port load-config"
259 cheval "cd /var/db/ports; cpio -iv"
262 # Dump port build options to cpio
264 meh "port save-config"
265 cheval "cd /var/db/ports; find . -type d -o -type f -name options | cpio -ovHnewc"
270 # Port distfile handling
274 # Recursively retrieve distfiles
275 port_fetch_recursive() {
278 meh "fetch-recursive ${1}"
279 chmake "${1}" fetch-recursive
285 # Port building and packaging
289 # Copy in and install dependency packages
292 for pkg in $(port2pkg $(port_all_deps "${port}"))
294 cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}"
296 if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1
298 meh "Installing dependencies"
299 cheval "cd ${chroot_bdeps_dir}; pkg_add -F *" || wtf "port_load_deps ${port} failed"
303 # Build and install a port
306 meh "Building ${port}"
307 chmake "${port}" clean build install clean || wtf "port_build ${port} failed"
313 meh "Creating rdep package tree for ${port}"
314 cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed"
317 # Package port build dependencies, unless they're already run deps
319 meh "Stashing unsaved bdeps"
320 # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison)
321 #for pkg in $(pkg_bdeps "${1}")
323 # [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}"
326 # Instead, just save everything that's not already in rdeps as bdeps
327 for pkg in $(cheval pkg_info | awk '{print $1}')
329 if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps_dir}/${pkg}.tbz" ]
331 cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed"
336 # Copy generated packages out of tree
338 meh "Moving created packages to repo"
339 ls "${pkg_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${pkg_dir}"/*.tbz "${final_pkg_dir}"
340 ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${bdeps_dir}"/*.tbz "${final_bdeps_dir}"
341 # link everything into ${bdeps_dir} so we can find it easily later
342 ( cd "${final_pkg_dir}"; find . -type f | cpio -plu --quiet "${final_bdeps_dir}" )
345 # Delete all installed packages (hope you saved them first)
347 meh "Clearing out installed packages"
348 cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed"
357 # Execute a complete port build, using prebuilt packages to fulfill dependencies when available
358 # Be sure to chsetup and populate your config before running!
361 port_load_deps "${port}"
363 port_package "${port}"