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}"
20 # Configuration variable setup
24 # Base directory for everything
25 base_dir="$(realpath "$(dirname "${0}")/../worlds")"
27 # Directory where distfiles will be stored between builds
28 dist_dir="${base_dir}/distfiles"
30 # Final directory for built packages (Outside chroot)
31 final_pkg_dir="${base_dir}/pkg"
32 final_bdeps_dir="${base_dir}/bdeps"
35 chroot_dir="${base_dir}/root"
37 # Package directories, must be under ${chroot_dir}
38 pkg_dir="${chroot_dir}/pkg"
39 bdeps_dir="${pkg_dir}/bdeps"
41 # Compute in-chroot pkg and bdeps dirs
42 chroot_pkg_dir="${pkg_dir##${chroot_dir}}"
43 chroot_bdeps_dir="${bdeps_dir##${chroot_dir}}"
50 PATH=:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
56 unset _port_config_recursive_cache
66 # Necessary mountpoints
67 mkdir -p "${chroot_dir}/dev" || return 1
68 mount -t devfs devfs "${chroot_dir}/dev" || return 1
69 mkdir -p "${chroot_dir}/usr/ports" || return 1
70 mount -t nullfs -r /usr/ports "${chroot_dir}/usr/ports" || return 1
71 mkdir -p "${chroot_dir}/var/ports/distfiles" || return 1
72 mount -t nullfs -w "${dist_dir}" "${chroot_dir}/var/ports/distfiles" || return 1
74 # Chroot configuration
75 cp -f /etc/resolv.conf "${chroot_dir}/etc/resolv.conf" || return 1
77 # Target package directories
78 mkdir -p "${pkg_dir}" || return 1
79 mkdir -p "${bdeps_dir}" || return 1
84 umount "${chroot_dir}/var/ports/distfiles"
85 umount "${chroot_dir}/usr/ports"
86 umount "${chroot_dir}/dev"
89 # Evaluate a command line within the chroot
91 chroot "${chroot_dir}" env -i ${chroot_env} /bin/sh -c "cd ${chroot_pkg_dir}; ${*}"
96 local port="/usr/ports/${1##/usr/ports/}"
98 cheval "make -C ${port} ${*}"
103 # Port Dependency Tracking
107 # Translate port origin to package name
111 chmake "${1}" -V PKGNAME
116 # Build dependencies (shallow, recursive, package)
120 chmake "${1}" build-depends-list | sed -e 's#^/usr/ports/##'
125 # rdeps for rdeps are rdeps, rdeps for bdeps are bdeps; thus:
126 ( port_bdeps "${@}"; port_all_rdeps "${@}" ) | while read port
128 port_all_bdeps "${port}"
129 port_all_rdeps "${port}"
133 port2pkg $(port_all_bdeps "${@}")
136 # Runtime dependencies (shallow, recursive, package)
140 chmake "${1}" run-depends-list | sed -e 's#^/usr/ports/##'
145 port_rdeps "${@}" | while read port
148 port_all_rdeps "${port}"
152 port2pkg $(port_all_rdeps "${@}")
155 # All dependencies (shallow, recursive, package)
159 chmake "${1}" all-depends-list | sed -e 's#^/usr/ports/##'
164 port_deps "${@}" | while read port
171 port2pkg $(port_all_deps "${@}")
176 # Port Configuration Handling
180 # Run make config on a list of ports
184 meh "port config ${1}"
185 chmake "${1}" config < /dev/tty
190 # Make config-conditional for a list of ports, and all dependencies
191 port_config_recursive() {
194 # Do not use config-recursive because it computes the depchain first;
195 # instead, manually compute the depchain after each config to ensure the
196 # proper depchain is followed. Use config-conditional to avoid dialog
197 # if the config is already complete. Also use a cache to avoid re-config
198 # and re-recurse on previously handled port branches.
199 if echo "${_port_config_recursive_cache}" | grep -qv " ${1} "
201 meh "port config-recursive ${1}"
202 chmake "${1}" config-conditional < /dev/tty
203 port_config_recursive $(port_deps "${1}")
204 _port_config_recursive_cache="${_port_config_recursive_cache} ${1} "
210 # Remove saved config for a list of ports
214 meh "port rmconfig ${1}"
215 chmake "${1}" rmconfig
220 # Remove saved config for a list of ports and all dependencies
221 port_rmconfig_recursive() {
222 meh "port rmconfig-recursive ${*}"
223 port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq)
226 # Obliterate saved configuration for all ports
227 port_rmconfig_all() {
228 meh "port rmconfig-all"
229 cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null"
232 # Restore port build options from cpio
234 meh "port load-config"
235 cheval "cd /var/db/ports; cpio -iv"
238 # Dump port build options to cpio
240 meh "port save-config"
241 cheval "cd /var/db/ports; find . -type d -o -type f -name options | cpio -ovHnewc"
246 # Port distfile handling
250 # Recursively retrieve distfiles
251 port_fetch_recursive() {
254 meh "fetch-recursive ${1}"
255 chmake "${1}" fetch-recursive
261 # Port building and packaging
265 # Copy in and install dependency packages
268 for pkg in $(port2pkg $(port_all_deps "${port}"))
270 cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}"
272 if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1
274 meh "Installing dependencies"
275 cheval "cd ${chroot_bdeps_dir}; pkg_add -F *" || wtf "port_load_deps ${port} failed"
279 # Build and install a port
282 meh "Building ${port}"
283 chmake "${port}" clean build install clean || wtf "port_build ${port} failed"
289 meh "Creating rdep package tree for ${port}"
290 cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed"
293 # Package port build dependencies, unless they're already run deps
295 meh "Stashing unsaved bdeps"
296 # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison)
297 #for pkg in $(pkg_bdeps "${1}")
299 # [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}"
302 # Instead, just save everything that's not already in rdeps as bdeps
303 for pkg in $(cheval pkg_info | awk '{print $1}')
305 if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps_dir}/${pkg}.tbz" ]
307 cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed"
312 # Copy generated packages out of tree
314 meh "Moving created packages to repo"
315 ls "${pkg_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${pkg_dir}"/*.tbz "${final_pkg_dir}"
316 ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${bdeps_dir}"/*.tbz "${final_bdeps_dir}"
317 # link everything into ${bdeps_dir} so we can find it easily later
318 ( cd "${final_pkg_dir}"; find . -type f | cpio -plu --quiet "${final_bdeps_dir}" )
321 # Delete all installed packages (hope you saved them first)
323 meh "Clearing out installed packages"
324 cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed"
333 # Execute a complete port build, using prebuilt packages to fulfill dependencies when available
334 # Be sure to chsetup and populate your config before running!
337 port_load_deps "${port}"
339 port_package "${port}"