1 # Dump a list of leaf ports from a working system
2 # Leaf ports are ports that have nothing depending upon them
3 # These are generally the top-level ports; everything else should
6 ( cd /var/db/pkg; ls -1 ) | while read pkg
8 pkg_info -Rq "${pkg}" | grep -q '' || pkg_info -oq "${pkg}"
14 # Configuration variable setup
18 # Base directory for everything
19 base_dir="$(realpath "$(dirname "${0}")/../worlds")"
21 # Directory where distfiles will be stored between builds
22 dist_dir="${base_dir}/distfiles"
24 # Final directory for built packages (Outside chroot)
25 final_pkg_dir="${base_dir}/pkg"
26 final_bdeps_dir="${base_dir}/bdeps"
29 chroot_dir="${base_dir}/root"
31 # Package directories, must be under ${chroot_dir}
32 pkg_dir="${chroot_dir}/pkg"
33 bdeps_dir="${pkg_dir}/bdeps"
35 # Compute in-chroot pkg and bdeps dirs
36 chroot_pkg_dir="${pkg_dir##${chroot_dir}}"
37 chroot_bdeps_dir="${bdeps_dir##${chroot_dir}}"
44 PATH=:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
50 unset _port_config_recursive_cache
54 # Boilerplate functions
58 meh() { printf " \033[1;32m*\033[0m %s\n" "${*}" >&2; }
59 omg() { printf " \033[1;33m*\033[0m %s\n" "${*}" >&2; }
60 wtf() { printf " \033[1;31m*\033[0m %s\n" "${*}" >&2; exit 1; }
70 # Necessary mountpoints
71 mkdir -p "${chroot_dir}/dev" || return 1
72 mount -t devfs devfs "${chroot_dir}/dev" || return 1
73 mkdir -p "${chroot_dir}/usr/ports" || return 1
74 mount -t nullfs -r /usr/ports "${chroot_dir}/usr/ports" || return 1
75 mkdir -p "${chroot_dir}/var/ports/distfiles" || return 1
76 mount -t nullfs -w "${dist_dir}" "${chroot_dir}/var/ports/distfiles" || return 1
78 # Chroot configuration
79 cp -f /etc/resolv.conf "${chroot_dir}/etc/resolv.conf" || return 1
81 # Target package directories
82 mkdir -p "${pkg_dir}" || return 1
83 mkdir -p "${bdeps_dir}" || return 1
88 umount "${chroot_dir}/var/ports/distfiles"
89 umount "${chroot_dir}/usr/ports"
90 umount "${chroot_dir}/dev"
93 # Evaluate a command line within the chroot
95 chroot "${chroot_dir}" env -i ${chroot_env} /bin/sh -c "cd ${chroot_pkg_dir}; ${*}"
100 local port="/usr/ports/${1##/usr/ports/}"
102 cheval "make -C ${port} ${*}"
107 # Port Dependency Tracking
111 # Translate port origin to package name
115 chmake "${1}" -V PKGNAME
120 # Build dependencies (shallow, recursive, package)
124 chmake "${1}" build-depends-list | sed -e 's#^/usr/ports/##'
129 # rdeps for rdeps are rdeps, rdeps for bdeps are bdeps; thus:
130 ( port_bdeps "${@}"; port_all_rdeps "${@}" ) | while read port
132 port_all_bdeps "${port}"
133 port_all_rdeps "${port}"
137 port2pkg $(port_all_bdeps "${@}")
140 # Runtime dependencies (shallow, recursive, package)
144 chmake "${1}" run-depends-list | sed -e 's#^/usr/ports/##'
149 port_rdeps "${@}" | while read port
152 port_all_rdeps "${port}"
156 port2pkg $(port_all_rdeps "${@}")
159 # All dependencies (shallow, recursive, package)
163 chmake "${1}" all-depends-list | sed -e 's#^/usr/ports/##'
168 port_deps "${@}" | while read port
175 port2pkg $(port_all_deps "${@}")
180 # Port Configuration Handling
184 # Run make config on a list of ports
188 meh "port config ${1}"
189 chmake "${1}" config < /dev/tty
194 # Make config-conditional for a list of ports, and all dependencies
195 port_config_recursive() {
198 # Do not use config-recursive because it computes the depchain first;
199 # instead, manually compute the depchain after each config to ensure the
200 # proper depchain is followed. Use config-conditional to avoid dialog
201 # if the config is already complete. Also use a cache to avoid re-config
202 # and re-recurse on previously handled port branches.
203 if echo "${_port_config_recursive_cache}" | grep -qv " ${1} "
205 meh "port config-recursive ${1}"
206 chmake "${1}" config-conditional < /dev/tty
207 port_config_recursive $(port_deps "${1}")
208 _port_config_recursive_cache="${_port_config_recursive_cache} ${1} "
214 # Remove saved config for a list of ports
218 meh "port rmconfig ${1}"
219 chmake "${1}" rmconfig
224 # Remove saved config for a list of ports and all dependencies
225 port_rmconfig_recursive() {
226 meh "port rmconfig-recursive ${*}"
227 port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq)
230 # Obliterate saved configuration for all ports
231 port_rmconfig_all() {
232 meh "port rmconfig-all"
233 cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null"
236 # Restore port build options from cpio
238 meh "port load-config"
239 cheval "cd /var/db/ports; cpio -iv"
242 # Dump port build options to cpio
244 meh "port save-config"
245 cheval "cd /var/db/ports; find . -type d -o -type f -name options | cpio -ovHnewc"
250 # Port distfile handling
254 # Recursively retrieve distfiles
255 port_fetch_recursive() {
258 meh "fetch-recursive ${1}"
259 chmake "${1}" fetch-recursive
265 # Port building and packaging
269 # Copy in and install dependency packages
272 for pkg in $(port2pkg $(port_all_deps "${port}"))
274 cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}"
276 if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1
278 meh "Installing dependencies"
279 cheval "cd ${chroot_bdeps_dir}; pkg_add -F *" || wtf "port_load_deps ${port} failed"
283 # Build and install a port
286 meh "Building ${port}"
287 chmake "${port}" clean build install clean || wtf "port_build ${port} failed"
293 meh "Creating rdep package tree for ${port}"
294 cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed"
297 # Package port build dependencies, unless they're already run deps
299 meh "Stashing unsaved bdeps"
300 # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison)
301 #for pkg in $(pkg_bdeps "${1}")
303 # [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}"
306 # Instead, just save everything that's not already in rdeps as bdeps
307 for pkg in $(cheval pkg_info | awk '{print $1}')
309 if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps_dir}/${pkg}.tbz" ]
311 cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed"
316 # Copy generated packages out of tree
318 meh "Moving created packages to repo"
319 ls "${pkg_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${pkg_dir}"/*.tbz "${final_pkg_dir}"
320 ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${bdeps_dir}"/*.tbz "${final_bdeps_dir}"
321 # link everything into ${bdeps_dir} so we can find it easily later
322 ( cd "${final_pkg_dir}"; find . -type f | cpio -plu --quiet "${final_bdeps_dir}" )
325 # Delete all installed packages (hope you saved them first)
327 meh "Clearing out installed packages"
328 cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed"
337 # Execute a complete port build, using prebuilt packages to fulfill dependencies when available
338 # Be sure to chsetup and populate your config before running!
341 port_load_deps "${port}"
343 port_package "${port}"