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}"
26 # Necessary mountpoints
27 mkdir -p "${chroot_dir}/dev" || return 1
28 mount -t devfs devfs "${chroot_dir}/dev" || return 1
29 mkdir -p "${chroot_dir}/usr/ports" || return 1
30 mount -t nullfs -r /usr/ports "${chroot_dir}/usr/ports" || return 1
31 mkdir -p "${chroot_dir}/var/ports/distfiles" || return 1
32 mount -t nullfs -w "${dist_dir}" "${chroot_dir}/var/ports/distfiles" || return 1
34 # Chroot configuration
35 cp -f /etc/resolv.conf "${chroot_dir}/etc/resolv.conf" || return 1
37 # Target package directories
38 mkdir -p "${pkg_dir}" || return 1
39 mkdir -p "${bdeps_dir}" || return 1
44 umount "${chroot_dir}/var/ports/distfiles"
45 umount "${chroot_dir}/usr/ports"
46 umount "${chroot_dir}/dev"
49 # Evaluate a command line within the chroot
51 chroot "${chroot_dir}" env -i ${chroot_env} /bin/sh -c "cd ${chroot_pkg_dir}; ${*}"
56 local port="/usr/ports/${1##/usr/ports/}"
58 cheval "make -C ${port} ${*}"
63 # Port Dependency Tracking
67 # Translate port origin to package name
71 chmake "${1}" -V PKGNAME
76 # Build dependencies (shallow, recursive, package)
80 chmake "${1}" build-depends-list | sed -e 's#^/usr/ports/##'
85 # rdeps for rdeps are rdeps, rdeps for bdeps are bdeps; thus:
86 ( port_bdeps "${@}"; port_all_rdeps "${@}" ) | while read port
88 port_all_bdeps "${port}"
89 port_all_rdeps "${port}"
93 port2pkg $(port_all_bdeps "${@}")
96 # Runtime dependencies (shallow, recursive, package)
100 chmake "${1}" run-depends-list | sed -e 's#^/usr/ports/##'
105 port_rdeps "${@}" | while read port
108 port_all_rdeps "${port}"
112 port2pkg $(port_all_rdeps "${@}")
115 # All dependencies (shallow, recursive, package)
119 chmake "${1}" all-depends-list | sed -e 's#^/usr/ports/##'
124 port_deps "${@}" | while read port
131 port2pkg $(port_all_deps "${@}")
136 # Port Configuration Handling
140 # Run make config on a list of ports
144 meh "port config ${1}"
145 chmake "${1}" config < /dev/tty
150 # Make config-conditional for a list of ports, and all dependencies
151 port_config_recursive() {
154 # Do not use config-recursive because it computes the depchain first;
155 # instead, manually compute the depchain after each config to ensure the
156 # proper depchain is followed. Use config-conditional to avoid dialog
157 # if the config is already complete. Also use a cache to avoid re-config
158 # and re-recurse on previously handled port branches.
159 if echo "${_port_config_recursive_cache}" | grep -qv " ${1} "
161 meh "port config-recursive ${1}"
162 chmake "${1}" config-conditional < /dev/tty
163 port_config_recursive $(port_deps "${1}")
164 _port_config_recursive_cache="${_port_config_recursive_cache} ${1} "
170 unset _port_config_recursive_cache
172 # Remove saved config for a list of ports
176 meh "port rmconfig ${1}"
177 chmake "${1}" rmconfig
182 # Remove saved config for a list of ports and all dependencies
183 port_rmconfig_recursive() {
184 meh "port rmconfig-recursive ${*}"
185 port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq)
188 # Obliterate saved configuration for all ports
189 port_rmconfig_all() {
190 meh "port rmconfig-all"
191 cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null"
194 # Restore port build options from cpio
196 meh "port load-config"
197 cheval "cd /var/db/ports; cpio -iv"
200 # Dump port build options to cpio
202 meh "port save-config"
203 cheval "cd /var/db/ports; find . -type d -o -type f -name options | cpio -ovHnewc"
208 # Port distfile handling
212 # Recursively retrieve distfiles
213 port_fetch_recursive() {
216 meh "fetch-recursive ${1}"
217 chmake "${1}" fetch-recursive
223 # Port building and packaging
227 # Copy in and install dependency packages
230 for pkg in $(port2pkg $(port_all_deps "${port}"))
232 cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}"
234 if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1
236 meh "Installing dependencies"
237 cheval "cd ${chroot_bdeps_dir}; pkg_add -F *" || wtf "port_load_deps ${port} failed"
241 # Build and install a port
244 meh "Building ${port}"
245 chmake "${port}" clean build install clean || wtf "port_build ${port} failed"
251 meh "Creating rdep package tree for ${port}"
252 cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed"
255 # Package port build dependencies, unless they're already run deps
257 meh "Stashing unsaved bdeps"
258 # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison)
259 #for pkg in $(pkg_bdeps "${1}")
261 # [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}"
264 # Instead, just save everything that's not already in rdeps as bdeps
265 for pkg in $(cheval pkg_info | awk '{print $1}')
267 if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps_dir}/${pkg}.tbz" ]
269 cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed"
274 # Copy generated packages out of tree
276 meh "Moving created packages to repo"
277 ls "${pkg_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${pkg_dir}"/*.tbz "${final_pkg_dir}"
278 ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${bdeps_dir}"/*.tbz "${final_bdeps_dir}"
279 # link everything into ${bdeps_dir} so we can find it easily later
280 ( cd "${final_pkg_dir}"; find . -type f | cpio -plu --quiet "${final_bdeps_dir}" )
283 # Delete all installed packages (hope you saved them first)
285 meh "Clearing out installed packages"
286 cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed"
295 # Execute a complete port build, using prebuilt packages to fulfill dependencies when available
296 # Be sure to chsetup and populate your config before running!
299 port_load_deps "${port}"
301 port_package "${port}"
310 # Configuration variable setup
314 # Base directory for everything
315 base_dir="$(realpath "$(dirname "${0}")/../worlds")"
317 # Directory where distfiles will be stored between builds
318 dist_dir="${base_dir}/distfiles"
320 # Final directory for built packages (Outside chroot)
321 final_pkg_dir="${base_dir}/pkg"
322 final_bdeps_dir="${base_dir}/bdeps"
325 chroot_dir="${base_dir}/root"
327 # Package directories, must be under ${chroot_dir}
328 pkg_dir="${chroot_dir}/pkg"
329 bdeps_dir="${pkg_dir}/bdeps"
331 # Compute in-chroot pkg and bdeps dirs
332 chroot_pkg_dir="${pkg_dir##${chroot_dir}}"
333 chroot_bdeps_dir="${bdeps_dir##${chroot_dir}}"
340 PATH=:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin