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 # Set up chroot mounts and runtime configuration
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
42 # Unmount all chroot directories
44 # Short-circuit if nothing is mounted
45 mount | grep -q "${chroot_dir}" || return 0
46 umount "${chroot_dir}/var/ports/distfiles"
47 umount "${chroot_dir}/usr/ports"
48 umount "${chroot_dir}/dev"
52 # Evaluate a command line within the chroot
54 chroot "${chroot_dir}" env -i ${chroot_env} /bin/sh -c "cd ${chroot_pkg_dir}; ${*}"
59 local port="/usr/ports/${1##/usr/ports/}"
61 cheval "make -C ${port} ${*}"
66 # Port Dependency Tracking
70 # Translate port origin to package name
74 chmake "${1}" -V PKGNAME
79 # Build dependencies (shallow, recursive, package)
83 chmake "${1}" build-depends-list | sed -e 's#^/usr/ports/##'
88 # rdeps for rdeps are rdeps, rdeps for bdeps are bdeps; thus:
89 ( port_bdeps "${@}"; port_all_rdeps "${@}" ) | while read port
91 port_all_bdeps "${port}"
92 port_all_rdeps "${port}"
96 port2pkg $(port_all_bdeps "${@}")
99 # Runtime dependencies (shallow, recursive, package)
103 chmake "${1}" run-depends-list | sed -e 's#^/usr/ports/##'
108 port_rdeps "${@}" | while read port
111 port_all_rdeps "${port}"
115 port2pkg $(port_all_rdeps "${@}")
118 # All dependencies (shallow, recursive, package)
122 chmake "${1}" all-depends-list | sed -e 's#^/usr/ports/##'
127 port_deps "${@}" | while read port
134 port2pkg $(port_all_deps "${@}")
139 # Port Configuration Handling
143 # Run make config on a list of ports
147 meh "port config ${1}"
148 chmake "${1}" config < /dev/tty
153 # Make config-conditional for a list of ports, and all dependencies
154 port_config_recursive() {
155 # Clear cache if first value isn't '-r'
156 [ "${1}" = '-r' ] && shift || _port_config_recursive_cache=""
159 # Do not use config-recursive because it computes the depchain first;
160 # instead, manually compute the depchain after each config to ensure the
161 # proper depchain is followed. Use config-conditional to avoid dialog
162 # if the config is already complete. Also use a cache to avoid re-config
163 # and re-recurse on previously handled port branches.
164 if echo "${_port_config_recursive_cache}" | grep -qv " ${1} "
166 meh "port config-recursive ${1}"
167 chmake "${1}" config-conditional < /dev/tty
168 port_config_recursive -r $(port_deps "${1}")
169 _port_config_recursive_cache="${_port_config_recursive_cache} ${1} "
175 unset _port_config_recursive_cache
177 # Remove saved config for a list of ports
181 meh "port rmconfig ${1}"
182 chmake "${1}" rmconfig
187 # Remove saved config for a list of ports and all dependencies
188 port_rmconfig_recursive() {
189 meh "port rmconfig-recursive ${*}"
190 port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq)
193 # Obliterate saved configuration for all ports
194 port_rmconfig_all() {
195 meh "port rmconfig-all"
196 cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null"
199 # Restore port build options from cpio
201 meh "port load-config"
202 cheval "cd /var/db/ports; cpio -iv"
205 # Dump port build options to cpio
207 meh "port save-config"
208 cheval "cd /var/db/ports; find . -type d -o -type f -name options | cpio -ovHnewc"
213 # Port distfile handling
217 # Recursively retrieve distfiles
218 port_fetch_recursive() {
221 meh "fetch-recursive ${1}"
222 chmake "${1}" fetch-recursive
228 # Port building and packaging
232 # Copy in and install dependency packages
235 for pkg in $(port2pkg $(port_all_deps "${port}"))
237 cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}"
239 if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1
241 meh "Installing dependencies"
242 cheval "cd ${chroot_bdeps_dir}; pkg_add -F *" || wtf "port_load_deps ${port} failed"
246 # Build and install a port
249 meh "Building ${port}"
250 chmake "${port}" clean build install clean || wtf "port_build ${port} failed"
256 meh "Creating rdep package tree for ${port}"
257 cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed"
260 # Package port build dependencies, unless they're already run deps
262 meh "Stashing unsaved bdeps"
263 # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison)
264 #for pkg in $(pkg_bdeps "${1}")
266 # [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}"
269 # Instead, just save everything that's not already in rdeps as bdeps
270 for pkg in $(cheval pkg_info | awk '{print $1}')
272 if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps_dir}/${pkg}.tbz" ]
274 cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed"
279 # Copy generated packages out of tree
281 meh "Moving created packages to repo"
282 ls "${pkg_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${pkg_dir}"/*.tbz "${final_pkg_dir}"
283 ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${bdeps_dir}"/*.tbz "${final_bdeps_dir}"
284 # link everything into ${bdeps_dir} so we can find it easily later
285 ( cd "${final_pkg_dir}"; find . -type f | cpio -plu --quiet "${final_bdeps_dir}" )
288 # Delete all installed packages (hope you saved them first)
290 meh "Clearing out installed packages"
291 cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed"
300 # Execute a complete port build, using prebuilt packages to fulfill dependencies when available
301 # Be sure to chsetup and populate your config before running!
304 port_load_deps "${port}"
306 port_package "${port}"
315 # Configuration variable setup
322 ROOT="$(realpath "$(dirname "${0}")/../worlds")"
324 # Base directory for everything
325 base_dir="${ROOT}/${TARGET}/${CONFIG}"
327 # Root tree for chroot seeding
328 root_dir="${base_dir}/root"
330 # Directory where distfiles will be stored between builds (common to all configs)
331 dist_dir="${ROOT}/seed/distfiles"
333 # Final directory for built packages (Outside chroot)
334 final_pkg_dir="${base_dir}/pkg"
335 final_bdeps_dir="${base_dir}/bdeps"
338 chroot_dir="${base_dir}/chroot"
340 # Package directories (must be under ${chroot_dir})
341 pkg_dir="${chroot_dir}/pkg"
342 bdeps_dir="${pkg_dir}/bdeps"
344 # Compute in-chroot pkg and bdeps dirs
345 chroot_pkg_dir="${pkg_dir##${chroot_dir}}"
346 chroot_bdeps_dir="${bdeps_dir##${chroot_dir}}"
353 PATH=:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin