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() {
152 # Clear cache if first value isn't '-r'
153 [ "${1}" = '-r' ] && shift || _port_config_recursive_cache=""
156 # Do not use config-recursive because it computes the depchain first;
157 # instead, manually compute the depchain after each config to ensure the
158 # proper depchain is followed. Use config-conditional to avoid dialog
159 # if the config is already complete. Also use a cache to avoid re-config
160 # and re-recurse on previously handled port branches.
161 if echo "${_port_config_recursive_cache}" | grep -qv " ${1} "
163 meh "port config-recursive ${1}"
164 chmake "${1}" config-conditional < /dev/tty
165 port_config_recursive -r $(port_deps "${1}")
166 _port_config_recursive_cache="${_port_config_recursive_cache} ${1} "
172 unset _port_config_recursive_cache
174 # Remove saved config for a list of ports
178 meh "port rmconfig ${1}"
179 chmake "${1}" rmconfig
184 # Remove saved config for a list of ports and all dependencies
185 port_rmconfig_recursive() {
186 meh "port rmconfig-recursive ${*}"
187 port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq)
190 # Obliterate saved configuration for all ports
191 port_rmconfig_all() {
192 meh "port rmconfig-all"
193 cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null"
196 # Restore port build options from cpio
198 meh "port load-config"
199 cheval "cd /var/db/ports; cpio -iv"
202 # Dump port build options to cpio
204 meh "port save-config"
205 cheval "cd /var/db/ports; find . -type d -o -type f -name options | cpio -ovHnewc"
210 # Port distfile handling
214 # Recursively retrieve distfiles
215 port_fetch_recursive() {
218 meh "fetch-recursive ${1}"
219 chmake "${1}" fetch-recursive
225 # Port building and packaging
229 # Copy in and install dependency packages
232 for pkg in $(port2pkg $(port_all_deps "${port}"))
234 cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}"
236 if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1
238 meh "Installing dependencies"
239 cheval "cd ${chroot_bdeps_dir}; pkg_add -F *" || wtf "port_load_deps ${port} failed"
243 # Build and install a port
246 meh "Building ${port}"
247 chmake "${port}" clean build install clean || wtf "port_build ${port} failed"
253 meh "Creating rdep package tree for ${port}"
254 cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed"
257 # Package port build dependencies, unless they're already run deps
259 meh "Stashing unsaved bdeps"
260 # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison)
261 #for pkg in $(pkg_bdeps "${1}")
263 # [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}"
266 # Instead, just save everything that's not already in rdeps as bdeps
267 for pkg in $(cheval pkg_info | awk '{print $1}')
269 if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps_dir}/${pkg}.tbz" ]
271 cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed"
276 # Copy generated packages out of tree
278 meh "Moving created packages to repo"
279 ls "${pkg_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${pkg_dir}"/*.tbz "${final_pkg_dir}"
280 ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${bdeps_dir}"/*.tbz "${final_bdeps_dir}"
281 # link everything into ${bdeps_dir} so we can find it easily later
282 ( cd "${final_pkg_dir}"; find . -type f | cpio -plu --quiet "${final_bdeps_dir}" )
285 # Delete all installed packages (hope you saved them first)
287 meh "Clearing out installed packages"
288 cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed"
297 # Execute a complete port build, using prebuilt packages to fulfill dependencies when available
298 # Be sure to chsetup and populate your config before running!
301 port_load_deps "${port}"
303 port_package "${port}"
312 # Configuration variable setup
316 # Base directory for everything
317 base_dir="$(realpath "$(dirname "${0}")/../worlds")"
319 # Directory where distfiles will be stored between builds
320 dist_dir="${base_dir}/distfiles"
322 # Final directory for built packages (Outside chroot)
323 final_pkg_dir="${base_dir}/pkg"
324 final_bdeps_dir="${base_dir}/bdeps"
327 chroot_dir="${base_dir}/root"
329 # Package directories, must be under ${chroot_dir}
330 pkg_dir="${chroot_dir}/pkg"
331 bdeps_dir="${pkg_dir}/bdeps"
333 # Compute in-chroot pkg and bdeps dirs
334 chroot_pkg_dir="${pkg_dir##${chroot_dir}}"
335 chroot_bdeps_dir="${bdeps_dir##${chroot_dir}}"
342 PATH=:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin