3 # Copyright (c) 2013-2018 The FreeBSD Foundation
4 # Copyright (c) 2013 Glen Barber
5 # Copyright (c) 2011 Nathan Whitehorn
8 # Portions of this software were developed by Glen Barber
9 # under sponsorship from the FreeBSD Foundation.
11 # Redistribution and use in source and binary forms, with or without
12 # modification, are permitted provided that the following conditions
14 # 1. Redistributions of source code must retain the above copyright
15 # notice, this list of conditions and the following disclaimer.
16 # 2. Redistributions in binary form must reproduce the above copyright
17 # notice, this list of conditions and the following disclaimer in the
18 # documentation and/or other materials provided with the distribution.
20 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 # release.sh: check out source trees, and build release components with
33 # totally clean, fresh trees.
34 # Based on release/generate-release.sh written by Nathan Whitehorn
39 export PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"
43 # Prototypes that can be redefined per-chroot or per-target.
49 echo "Usage: $0 [-c release.conf]"
53 # env_setup(): Set up the default build environment variables, such as the
54 # CHROOTDIR, VCSCMD, GITROOT, etc. This is called before the release.conf
55 # file is sourced, if '-c <release.conf>' is specified.
57 # The directory within which the release will be built.
59 RELENGDIR="$(dirname $(realpath ${0}))"
61 # The default version control system command to obtain the sources.
62 for _dir in /usr/bin /usr/local/bin; do
63 [ -x "${_dir}/git" ] && VCSCMD="/${_dir}/git"
64 [ ! -z "${VCSCMD}" ] && break 2
67 if [ -z "${VCSCMD}" ]; then
68 echo "*** The devel/git port/package is required."
71 VCSCMD="/usr/local/bin/git clone -q"
73 # The default git checkout server, and branches for src/, doc/,
75 GITROOT="https://cgit-beta.FreeBSD.org/"
83 # Set for embedded device builds.
86 # The default make.conf and src.conf to use. Set to /dev/null
87 # by default to avoid polluting the chroot(8) environment with
88 # non-default settings.
92 # The number of make(1) jobs, defaults to the number of CPUs available
93 # for buildworld, and half of number of CPUs available for buildkernel.
94 WORLD_FLAGS="-j$(sysctl -n hw.ncpu)"
95 KERNEL_FLAGS="-j$(( $(( $(sysctl -n hw.ncpu) + 1 )) / 2))"
99 # The name of the kernel to build, defaults to GENERIC.
102 # Set to non-empty value to disable checkout of doc/ and/or ports/.
103 # Disabling ports/ checkout also forces NODOC to be set.
107 # Set to non-empty value to disable distributing source tree.
110 # Set to non-empty value to build dvd1.iso as part of the release.
112 WITH_COMPRESSED_IMAGES=
114 # Set to non-empty value to build virtual machine images as part of
117 WITH_COMPRESSED_VMIMAGES=
120 # Set to non-empty value to build virtual machine images for various
121 # cloud providers as part of the release.
127 # env_check(): Perform sanity tests on the build environment, such as ensuring
128 # files/directories exist, as well as adding backwards-compatibility hacks if
129 # necessary. This is called unconditionally, and overrides the defaults set
130 # in env_setup() if '-c <release.conf>' is specified.
132 chroot_build_release_cmd="chroot_build_release"
134 # Prefix the branches with the GITROOT for the full checkout URL.
135 SRC="${GITROOT}${GITSRC}"
136 DOC="${GITROOT}${GITDOC}"
137 PORT="${GITROOT}${GITPORTS}"
139 if [ -n "${EMBEDDEDBUILD}" ]; then
141 WITH_COMPRESSED_IMAGES=
143 case ${EMBEDDED_TARGET}:${EMBEDDED_TARGET_ARCH} in
144 arm:arm*|arm64:aarch64)
145 chroot_build_release_cmd="chroot_arm_build_release"
152 # If PORTS is set and NODOC is unset, force NODOC=yes because the ports
153 # tree is required to build the documentation set.
154 if [ -n "${NOPORTS}" ] && [ -z "${NODOC}" ]; then
155 echo "*** NOTICE: Setting NODOC=1 since ports tree is required"
156 echo " and NOPORTS is set."
160 # If NOSRC, NOPORTS and/or NODOC are unset, they must not pass to make
161 # as variables. The release makefile verifies definedness of the
162 # NOPORTS/NODOC variables instead of their values.
164 if [ -n "${NOPORTS}" ]; then
165 SRCDOCPORTS="NOPORTS=yes"
167 if [ -n "${NODOC}" ]; then
168 SRCDOCPORTS="${SRCDOCPORTS}${SRCDOCPORTS:+ }NODOC=yes"
170 if [ -n "${NOSRC}" ]; then
171 SRCDOCPORTS="${SRCDOCPORTS}${SRCDOCPORTS:+ }NOSRC=yes"
174 # The aggregated build-time flags based upon variables defined within
175 # this file, unless overridden by release.conf. In most cases, these
176 # will not need to be changed.
177 CONF_FILES="__MAKE_CONF=${MAKE_CONF} SRCCONF=${SRC_CONF}"
178 if [ -n "${TARGET}" ] && [ -n "${TARGET_ARCH}" ]; then
179 ARCH_FLAGS="TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}"
184 if [ -z "${CHROOTDIR}" ]; then
185 echo "Please set CHROOTDIR."
189 if [ $(id -u) -ne 0 ]; then
190 echo "Needs to be run as root."
194 # Unset CHROOTBUILD_SKIP if the chroot(8) does not appear to exist.
195 if [ ! -z "${CHROOTBUILD_SKIP}" -a ! -e ${CHROOTDIR}/bin/sh ]; then
199 CHROOT_MAKEENV="${CHROOT_MAKEENV} \
200 MAKEOBJDIRPREFIX=${CHROOTDIR}/tmp/obj"
201 CHROOT_WMAKEFLAGS="${MAKE_FLAGS} ${WORLD_FLAGS} ${CONF_FILES}"
202 CHROOT_IMAKEFLAGS="${WORLD_FLAGS} ${CONF_FILES}"
203 CHROOT_DMAKEFLAGS="${WORLD_FLAGS} ${CONF_FILES}"
204 RELEASE_WMAKEFLAGS="${MAKE_FLAGS} ${WORLD_FLAGS} ${ARCH_FLAGS} \
206 RELEASE_KMAKEFLAGS="${MAKE_FLAGS} ${KERNEL_FLAGS} \
207 KERNCONF=\"${KERNEL}\" ${ARCH_FLAGS} ${CONF_FILES}"
208 RELEASE_RMAKEFLAGS="${ARCH_FLAGS} \
209 KERNCONF=\"${KERNEL}\" ${CONF_FILES} ${SRCDOCPORTS} \
210 WITH_DVD=${WITH_DVD} WITH_VMIMAGES=${WITH_VMIMAGES} \
211 WITH_CLOUDWARE=${WITH_CLOUDWARE} XZ_THREADS=${XZ_THREADS}"
216 # chroot_setup(): Prepare the build chroot environment for the release build.
219 mkdir -p ${CHROOTDIR}/usr
221 if [ -z "${SRC_UPDATE_SKIP}" ]; then
222 ${VCSCMD} ${SRC} -b ${SRCBRANCH} ${CHROOTDIR}/usr/src
224 if [ -z "${NODOC}" ] && [ -z "${DOC_UPDATE_SKIP}" ]; then
225 ${VCSCMD} ${DOC} -b ${DOCBRANCH} ${CHROOTDIR}/usr/doc
227 if [ -z "${NOPORTS}" ] && [ -z "${PORTS_UPDATE_SKIP}" ]; then
228 ${VCSCMD} ${PORT} -b ${PORTBRANCH} ${CHROOTDIR}/usr/ports
231 if [ -z "${CHROOTBUILD_SKIP}" ]; then
232 cd ${CHROOTDIR}/usr/src
233 env ${CHROOT_MAKEENV} make ${CHROOT_WMAKEFLAGS} buildworld
234 env ${CHROOT_MAKEENV} make ${CHROOT_IMAKEFLAGS} installworld \
236 env ${CHROOT_MAKEENV} make ${CHROOT_DMAKEFLAGS} distribution \
243 # extra_chroot_setup(): Prepare anything additional within the build
244 # necessary for the release build.
245 extra_chroot_setup() {
246 mkdir -p ${CHROOTDIR}/dev
247 mount -t devfs devfs ${CHROOTDIR}/dev
248 [ -e /etc/resolv.conf -a ! -e ${CHROOTDIR}/etc/resolv.conf ] && \
249 cp /etc/resolv.conf ${CHROOTDIR}/etc/resolv.conf
250 # Run ldconfig(8) in the chroot directory so /var/run/ld-elf*.so.hints
251 # is created. This is needed by ports-mgmt/pkg.
252 eval chroot ${CHROOTDIR} /etc/rc.d/ldconfig forcerestart
254 # If MAKE_CONF and/or SRC_CONF are set and not character devices
255 # (/dev/null), copy them to the chroot.
256 if [ -e ${MAKE_CONF} ] && [ ! -c ${MAKE_CONF} ]; then
257 mkdir -p ${CHROOTDIR}/$(dirname ${MAKE_CONF})
258 cp ${MAKE_CONF} ${CHROOTDIR}/${MAKE_CONF}
260 if [ -e ${SRC_CONF} ] && [ ! -c ${SRC_CONF} ]; then
261 mkdir -p ${CHROOTDIR}/$(dirname ${SRC_CONF})
262 cp ${SRC_CONF} ${CHROOTDIR}/${SRC_CONF}
265 # Install git from ports or packages if the ports tree is
266 # available and VCSCMD is unset.
267 if [ -d ${CHROOTDIR}/usr/ports ]; then
268 # Trick the ports 'run-autotools-fixup' target to do the right
270 _OSVERSION=$(chroot ${CHROOTDIR} /usr/bin/uname -U)
271 REVISION=$(chroot ${CHROOTDIR} make -C /usr/src/release -V REVISION)
272 BRANCH=$(chroot ${CHROOTDIR} make -C /usr/src/release -V BRANCH)
273 UNAME_r=${REVISION}-${BRANCH}
274 GITUNSETOPTS="CONTRIB CURL CVS GITWEB GUI HTMLDOCS"
275 GITUNSETOPTS="${GITUNSETOPTS} ICONV NLS P4 PERL"
276 GITUNSETOPTS="${GITUNSETOPTS} SEND_EMAIL SUBTREE SVN"
277 GITUNSETOPTS="${GITUNSETOPTS} PCRE PCRE2"
278 eval chroot ${CHROOTDIR} env OPTIONS_UNSET="${GITUNSETOPTS}" \
279 make -C /usr/ports/devel/git FORCE_PKG_REGISTER=1 \
280 WRKDIRPREFIX=/tmp/ports \
281 DISTDIR=/tmp/distfiles \
282 install clean distclean
284 if [ -d ${CHROOTDIR}/usr/ports ]; then
285 # Trick the ports 'run-autotools-fixup' target to do the right
287 _OSVERSION=$(chroot ${CHROOTDIR} /usr/bin/uname -U)
288 REVISION=$(chroot ${CHROOTDIR} make -C /usr/src/release -V REVISION)
289 BRANCH=$(chroot ${CHROOTDIR} make -C /usr/src/release -V BRANCH)
290 UNAME_r=${REVISION}-${BRANCH}
291 if [ -d ${CHROOTDIR}/usr/doc ] && [ -z "${NODOC}" ]; then
292 PBUILD_FLAGS="OSVERSION=${_OSVERSION} BATCH=yes"
293 PBUILD_FLAGS="${PBUILD_FLAGS} UNAME_r=${UNAME_r}"
294 PBUILD_FLAGS="${PBUILD_FLAGS} OSREL=${REVISION}"
295 PBUILD_FLAGS="${PBUILD_FLAGS} WRKDIRPREFIX=/tmp/ports"
296 PBUILD_FLAGS="${PBUILD_FLAGS} DISTDIR=/tmp/distfiles"
297 chroot ${CHROOTDIR} env ${PBUILD_FLAGS} \
298 OPTIONS_UNSET="AVAHI FOP IGOR" make -C \
299 /usr/ports/textproc/docproj \
300 FORCE_PKG_REGISTER=1 \
301 install clean distclean
305 if [ ! -z "${EMBEDDEDPORTS}" ]; then
306 _OSVERSION=$(chroot ${CHROOTDIR} /usr/bin/uname -U)
307 REVISION=$(chroot ${CHROOTDIR} make -C /usr/src/release -V REVISION)
308 BRANCH=$(chroot ${CHROOTDIR} make -C /usr/src/release -V BRANCH)
309 PBUILD_FLAGS="OSVERSION=${_OSVERSION} BATCH=yes"
310 PBUILD_FLAGS="${PBUILD_FLAGS} UNAME_r=${UNAME_r}"
311 PBUILD_FLAGS="${PBUILD_FLAGS} OSREL=${REVISION}"
312 PBUILD_FLAGS="${PBUILD_FLAGS} WRKDIRPREFIX=/tmp/ports"
313 PBUILD_FLAGS="${PBUILD_FLAGS} DISTDIR=/tmp/distfiles"
314 for _PORT in ${EMBEDDEDPORTS}; do
315 eval chroot ${CHROOTDIR} env ${PBUILD_FLAGS} make -C \
316 /usr/ports/${_PORT} \
317 FORCE_PKG_REGISTER=1 deinstall install clean distclean
324 } # extra_chroot_setup()
326 # chroot_build_target(): Build the userland and kernel for the build target.
327 chroot_build_target() {
329 if [ ! -z "${EMBEDDEDBUILD}" ]; then
330 RELEASE_WMAKEFLAGS="${RELEASE_WMAKEFLAGS} \
331 TARGET=${EMBEDDED_TARGET} \
332 TARGET_ARCH=${EMBEDDED_TARGET_ARCH}"
333 RELEASE_KMAKEFLAGS="${RELEASE_KMAKEFLAGS} \
334 TARGET=${EMBEDDED_TARGET} \
335 TARGET_ARCH=${EMBEDDED_TARGET_ARCH}"
337 eval chroot ${CHROOTDIR} make -C /usr/src ${RELEASE_WMAKEFLAGS} buildworld
338 eval chroot ${CHROOTDIR} make -C /usr/src ${RELEASE_KMAKEFLAGS} buildkernel
341 } # chroot_build_target
343 # chroot_build_release(): Invoke the 'make release' target.
344 chroot_build_release() {
346 if [ ! -z "${WITH_VMIMAGES}" ]; then
347 if [ -z "${VMFORMATS}" ]; then
348 VMFORMATS="$(eval chroot ${CHROOTDIR} \
349 make -C /usr/src/release -V VMFORMATS)"
351 if [ -z "${VMSIZE}" ]; then
352 VMSIZE="$(eval chroot ${CHROOTDIR} \
353 make -C /usr/src/release -V VMSIZE)"
355 RELEASE_RMAKEFLAGS="${RELEASE_RMAKEFLAGS} \
356 VMFORMATS=\"${VMFORMATS}\" VMSIZE=${VMSIZE}"
358 eval chroot ${CHROOTDIR} make -C /usr/src/release \
359 ${RELEASE_RMAKEFLAGS} release
360 eval chroot ${CHROOTDIR} make -C /usr/src/release \
361 ${RELEASE_RMAKEFLAGS} install DESTDIR=/R \
362 WITH_COMPRESSED_IMAGES=${WITH_COMPRESSED_IMAGES} \
363 WITH_COMPRESSED_VMIMAGES=${WITH_COMPRESSED_VMIMAGES}
366 } # chroot_build_release()
383 # chroot_arm_build_release(): Create arm SD card image.
384 chroot_arm_build_release() {
386 case ${EMBEDDED_TARGET} in
388 if [ -e "${RELENGDIR}/tools/arm.subr" ]; then
389 . "${RELENGDIR}/tools/arm.subr"
395 [ ! -z "${RELEASECONF}" ] && . "${RELEASECONF}"
396 export MAKE_FLAGS="${MAKE_FLAGS} TARGET=${EMBEDDED_TARGET}"
397 export MAKE_FLAGS="${MAKE_FLAGS} TARGET_ARCH=${EMBEDDED_TARGET_ARCH}"
398 export MAKE_FLAGS="${MAKE_FLAGS} ${CONF_FILES}"
399 eval chroot ${CHROOTDIR} env WITH_UNIFIED_OBJDIR=1 make ${MAKE_FLAGS} -C /usr/src/release obj
400 export WORLDDIR="$(eval chroot ${CHROOTDIR} make ${MAKE_FLAGS} -C /usr/src/release -V WORLDDIR)"
401 export OBJDIR="$(eval chroot ${CHROOTDIR} env WITH_UNIFIED_OBJDIR=1 make ${MAKE_FLAGS} -C /usr/src/release -V .OBJDIR)"
402 export DESTDIR="${OBJDIR}/${KERNEL}"
403 export IMGBASE="${CHROOTDIR}/${OBJDIR}/${BOARDNAME}.img"
404 export OSRELEASE="$(eval chroot ${CHROOTDIR} make ${MAKE_FLAGS} -C /usr/src/release \
405 TARGET=${EMBEDDED_TARGET} TARGET_ARCH=${EMBEDDED_TARGET_ARCH} \
407 chroot ${CHROOTDIR} mkdir -p ${DESTDIR}
408 chroot ${CHROOTDIR} truncate -s ${IMAGE_SIZE} ${IMGBASE##${CHROOTDIR}}
409 export mddev=$(chroot ${CHROOTDIR} \
410 mdconfig -f ${IMGBASE##${CHROOTDIR}} ${MD_ARGS})
415 mdconfig -d -u ${mddev}
416 chroot ${CHROOTDIR} rmdir ${DESTDIR}
417 mv ${IMGBASE} ${CHROOTDIR}/${OBJDIR}/${OSRELEASE}-${BOARDNAME}.img
418 chroot ${CHROOTDIR} mkdir -p /R
419 chroot ${CHROOTDIR} cp -p ${OBJDIR}/${OSRELEASE}-${BOARDNAME}.img \
420 /R/${OSRELEASE}-${BOARDNAME}.img
421 chroot ${CHROOTDIR} xz -T ${XZ_THREADS} /R/${OSRELEASE}-${BOARDNAME}.img
422 cd ${CHROOTDIR}/R && sha512 ${OSRELEASE}* \
424 cd ${CHROOTDIR}/R && sha256 ${OSRELEASE}* \
428 } # chroot_arm_build_release()
430 # main(): Start here.
432 set -e # Everything must succeed
434 while getopts c: opt; do
437 RELEASECONF="$(realpath ${OPTARG})"
444 shift $(($OPTIND - 1))
445 if [ ! -z "${RELEASECONF}" ]; then
446 if [ -e "${RELEASECONF}" ]; then
449 echo "Nonexistent configuration file: ${RELEASECONF}"
450 echo "Using default build environment."
454 trap "umount ${CHROOTDIR}/dev" EXIT # Clean up devfs mount on exit
458 ${chroot_build_release_cmd}