3 # Load shlib and modules
4 _root="$(dirname "${0}")"; . "${_root}/lib/env.sh"
8 [ "${*}" ] && printf "%s\n\n" "${*}"
9 echo "Usage: $(basename "${0}") -m <makeopts> -a <arch> -c <conf> <target, ...>"
10 echo " -m <makeopts> Provide additional flags to make"
11 echo " -a <arch> World architecture (i386, amd64)"
12 echo " -c <conf> World configuration (in worlds/<arch>/<conf>)"
13 echo " -s <srctree> Override source tree selection"
16 echo "If, for some reason, you wish to spread out builds across multiple"
17 echo "invocations, these might come in handy:"
18 echo " -p Prepare chroot environment"
19 echo " -d Don't prepare or clean up chroot during run"
20 echo " -q Clean up chroot environment"
22 echo "Available make targets:"
23 for make_tgt in ${make_tgts}
30 # Prepare chroot for build
32 [ "${CHROOT_DIRTY}" -a ! "${CHROOT_PREPARE}" ] && return 0
33 # Verify environment sanity
34 [ -d "${build}" ] && omg "${build}: directory exists; purging" && cleanup
35 mount | grep -q "${build}" && wtf "Stuff is mounted under ${build}; cannot continue"
36 ls -1 "${seed}"/base.?? >/dev/null 2>&1 || wtf "Populate seed directory ${seed} first"
37 [ -f "${SRCS}/sys/conf/newvers.sh" ] || wtf "Need sources in ${SRCS} to build"
39 # Cleanup trap here, so that an abort during prepare can clean up properly
40 trap "cleanup" exit hup int term kill
42 meh "Preparing build chroot"
43 [ -d "${build}" ] && wtf "${build}: directory exists"
44 mkdir -p "${build}" || wtf
45 cat "${seed}"/base.?? | tar xCf "${build}" - || wtf
46 mkdir -p "${build}/usr/obj" || wtf
48 meh "Mounting chroot filesystems"
49 mkdir -p "${world}/log"
50 mkdir -p "${world}/obj"
51 mkdir -p "${world}/root"
52 mount -t devfs devfs "${build}/dev" || wtf
53 # Mount /usr/src as a union, so that changes to it will not affect the underlying tree
54 # unionfs suffers from deadlocks; don't use it
55 mount -t nullfs -r "${SRCS}" "${build}/usr/src" || wtf
56 mount -t nullfs "${world}/obj" "${build}/usr/obj" || wtf
57 mount -t nullfs "${world}/root" "${build}/mnt" || wtf
59 if [ -d "${world}/config" ]
61 meh "Installing build-time configuration"
62 [ -f "${world}/config/${CONF}" ] && cp "${world}/config/${CONF}" "${SRCS}/sys/${ARCH}/conf/"
63 [ -f "${world}/config/make.conf" ] && cp "${world}/config/make.conf" "${build}/etc/"
64 [ -f "${world}/config/src.conf" ] && cp "${world}/config/src.conf" "${build}/etc/"
71 [ "${CHROOT_DIRTY}" -a ! "${CHROOT_CLEAN}" ] && return 0
73 umount -f "${build}/mnt"
74 umount -f "${build}/usr/obj"
75 umount -f "${build}/usr/src"
76 umount -f "${build}/dev"
77 mount | grep -q "${build}/" && wtf "Stuff is still mounted under ${build}; not removing"
78 chflags -R noschg "${build}"
80 trap "" exit hup int term kill
84 # Root directory of makeworld
85 ROOT="$(realpath "$(dirname "${0}")/..")"
88 WORLDS="${ROOT}/worlds"
90 # Compute make -j<cpus*2>
91 make_cpus="$(sysctl -n hw.ncpu)"
92 make_jobs="$(( ${make_cpus} * 2 ))"
93 make_tgts="buildworld buildkernel distrib-dirs installworld installkernel distribution"
98 MAKEOPTS="-j${make_jobs}"
100 while getopts "m:a:c:hpdq" opt
103 m) MAKEOPTS="${MAKEOPTS} ${OPTARG}" ;;
104 a) ARCH="${OPTARG}" ;;
105 c) CONF="${OPTARG}" ;;
106 s) SRCS="${OPTARG}" ;;
107 p) CHROOT_PREPARE="TRUE" ;;
108 d) CHROOT_DIRTY="TRUE" ;;
109 q) CHROOT_CLEAN="TRUE" ;;
111 [?]) pebkac "Unrecognized option ${opt}" ;;
114 shift $(( $OPTIND - 1 ))
116 # Should be root after this point
119 # Build make target sequence
120 sequence="${*:-${make_tgts}}"
122 # Target world directory
123 world="${WORLDS}/${ARCH}/${CONF}"
124 # Source chroot seed directory
125 seed="${ROOT}/seed/base/$(uname -m)"
126 # Root directory for chroot
127 build="${world}/chroot"
131 # Locate a usable source tree
132 # Pick world/src over /usr/src if it exists at all, even if it is not usable, to provide warning of bad configs
134 [ -L "${SRCS}" -o -d "${SRCS}" ] || SRCS=/usr/src
136 SRCS="$(realpath "${SRCS}")"
138 # Environment for chroot build
142 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
146 date="$(date +%Y%m%d)"
148 # Check if target config exists
149 [ -d "${world}" ] || wtf "${ARCH}/${CONF} doesn't exist"
151 if [ "${CHROOT_PREPARE}" ]
155 meh "Chroot prepared"
159 if [ "${CHROOT_CLEAN}" ]
166 meh "Making world for ${ARCH}/${CONF}"
171 meh "Srctree: ${SRCS}"
172 meh "Config: ${ARCH}/${CONF}"
173 meh "Builddir: ${build}"
174 meh "make ${MAKEOPTS}"
175 meh "DESTDIR=${world}/root"
177 for step in ${sequence}
179 meh "==> Step: ${step}"
180 script "${world}/log/${date}-${step}.log" env -i ${env} chroot "${build}" sh -c \
181 "cd /usr/src; time make ${MAKEOPTS} ${step} TARGET=${ARCH} KERNCONF=${CONF} DESTDIR=/mnt" || wtf "chroot-cmd ${step}"
184 # Copy the config files into the target, to keep a record of the build options
185 [ -f "${world}/config/${CONF}" ] && cp "${world}/config/${CONF}" "${build}/boot/kernel/"
186 [ -f "${world}/config/make.conf" ] && cp "${world}/config/make.conf" "${build}/etc/"
187 [ -f "${world}/config/src.conf" ] && cp "${world}/config/src.conf" "${build}/etc/"