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, SVNROOT, 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 for _svn in svn svnlite; do
64 [ -x "${_dir}/${_svn}" ] && VCSCMD="${_dir}/${_svn}"
65 [ ! -z "${VCSCMD}" ] && break 2
68 VCSCMD="${VCSCMD} checkout"
70 # The default svn checkout server, and svn branches for src/, doc/,
72 SVNROOT="svn://svn.FreeBSD.org/"
73 SRCBRANCH="base/stable/12@rHEAD"
75 GITROOT="https://git.FreeBSD.org/"
78 # Set for embedded device builds.
81 # Sometimes one needs to checkout src with --force svn option.
82 # If custom kernel configs copied to src tree before checkout, e.g.
85 # The default make.conf and src.conf to use. Set to /dev/null
86 # by default to avoid polluting the chroot(8) environment with
87 # non-default settings.
91 # The number of make(1) jobs, defaults to the number of CPUs available
92 # for buildworld, and half of number of CPUs available for buildkernel.
93 WORLD_FLAGS="-j$(sysctl -n hw.ncpu)"
94 KERNEL_FLAGS="-j$(( $(( $(sysctl -n hw.ncpu) + 1 )) / 2))"
98 # The name of the kernel to build, defaults to GENERIC.
101 # Set to non-empty value to disable checkout of doc/ and/or ports/.
104 # Set to non-empty value to disable distributing source tree.
107 # Set to non-empty value to build dvd1.iso as part of the release.
109 WITH_COMPRESSED_IMAGES=
111 # Set to non-empty value to build virtual machine images as part of
114 WITH_COMPRESSED_VMIMAGES=
117 # Set to non-empty value to build virtual machine images for various
118 # cloud providers as part of the release.
124 # env_check(): Perform sanity tests on the build environment, such as ensuring
125 # files/directories exist, as well as adding backwards-compatibility hacks if
126 # necessary. This is called unconditionally, and overrides the defaults set
127 # in env_setup() if '-c <release.conf>' is specified.
129 chroot_build_release_cmd="chroot_build_release"
130 # Fix for backwards-compatibility with release.conf that does not have
134 SVNROOT="${SVNROOT}/"
140 # Prefix the branches with the SVNROOT for the full checkout URL.
141 SRCBRANCH="${SVNROOT}${SRCBRANCH}"
142 # Prefix the branches with the GITROOT for the full checkout URL.
143 PORT="${GITROOT}${GITPORTS}"
145 if [ -n "${EMBEDDEDBUILD}" ]; then
147 WITH_COMPRESSED_IMAGES=
148 case ${EMBEDDED_TARGET}:${EMBEDDED_TARGET_ARCH} in
149 arm:arm*|arm64:aarch64)
150 chroot_build_release_cmd="chroot_arm_build_release"
157 # If NOSRC and/or NOPORTS are unset, they must not pass to make
158 # as variables. The release makefile verifies definedness of the
159 # NOPORTS variable instead of its value.
161 if [ -n "${NOPORTS}" ]; then
162 SRCPORTS="NOPORTS=yes"
164 if [ -n "${NOSRC}" ]; then
165 SRCPORTS="${SRCPORTS}${SRCPORTS:+ }NOSRC=yes"
168 # The aggregated build-time flags based upon variables defined within
169 # this file, unless overridden by release.conf. In most cases, these
170 # will not need to be changed.
171 CONF_FILES="__MAKE_CONF=${MAKE_CONF} SRCCONF=${SRC_CONF}"
172 if [ -n "${TARGET}" ] && [ -n "${TARGET_ARCH}" ]; then
173 ARCH_FLAGS="TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}"
177 # Force src checkout if configured
179 if [ -n "${SRC_FORCE_CHECKOUT}" ]; then
180 FORCE_SRC_KEY="--force"
183 if [ -z "${CHROOTDIR}" ]; then
184 echo "Please set CHROOTDIR."
188 if [ $(id -u) -ne 0 ]; then
189 echo "Needs to be run as root."
193 # Unset CHROOTBUILD_SKIP if the chroot(8) does not appear to exist.
194 if [ ! -z "${CHROOTBUILD_SKIP}" -a ! -e ${CHROOTDIR}/bin/sh ]; then
198 CHROOT_MAKEENV="${CHROOT_MAKEENV} \
199 MAKEOBJDIRPREFIX=${CHROOTDIR}/tmp/obj"
200 CHROOT_WMAKEFLAGS="${MAKE_FLAGS} ${WORLD_FLAGS} ${CONF_FILES}"
201 CHROOT_IMAKEFLAGS="${WORLD_FLAGS} ${CONF_FILES}"
202 CHROOT_DMAKEFLAGS="${WORLD_FLAGS} ${CONF_FILES}"
203 RELEASE_WMAKEFLAGS="${MAKE_FLAGS} ${WORLD_FLAGS} ${ARCH_FLAGS} \
205 RELEASE_KMAKEFLAGS="${MAKE_FLAGS} ${KERNEL_FLAGS} \
206 KERNCONF=\"${KERNEL}\" ${ARCH_FLAGS} ${CONF_FILES}"
207 RELEASE_RMAKEFLAGS="${ARCH_FLAGS} \
208 KERNCONF=\"${KERNEL}\" ${CONF_FILES} ${SRCPORTS} \
209 WITH_DVD=${WITH_DVD} WITH_VMIMAGES=${WITH_VMIMAGES} \
210 WITH_CLOUDWARE=${WITH_CLOUDWARE} XZ_THREADS=${XZ_THREADS}"
215 # chroot_setup(): Prepare the build chroot environment for the release build.
218 mkdir -p ${CHROOTDIR}/usr
220 if [ -z "${SRC_UPDATE_SKIP}" ]; then
221 ${VCSCMD} ${FORCE_SRC_KEY} ${SRCBRANCH} ${CHROOTDIR}/usr/src
223 if [ -z "${NOPORTS}" ] && [ -z "${PORTS_UPDATE_SKIP}" ]; then
224 if [ -d "${CHROOTDIR}/usr/ports/.git" ]; then
225 git -C ${CHROOTDIR}/usr/ports pull -q
227 git -C ${CHROOTDIR}/usr/ports -b ${PORTBRANCH} clone
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 if [ -z "${NOGIT}" ]; then
266 # Install git from ports or packages if the ports tree is
267 # available and VCSCMD is unset.
268 _gitcmd="$(which git)"
269 if [ -d ${CHROOTDIR}/usr/ports -a -z "${_gitcmd}" ]; then
270 # Trick the ports 'run-autotools-fixup' target to do the right
272 _OSVERSION=$(chroot ${CHROOTDIR} /usr/bin/uname -U)
273 REVISION=$(chroot ${CHROOTDIR} make -C /usr/src/release -V REVISION)
274 BRANCH=$(chroot ${CHROOTDIR} make -C /usr/src/release -V BRANCH)
275 UNAME_r=${REVISION}-${BRANCH}
276 GITUNSETOPTS="CONTRIB CURL CVS GITWEB GUI HTMLDOCS"
277 GITUNSETOPTS="${GITUNSETOPTS} ICONV NLS P4 PERL"
278 GITUNSETOPTS="${GITUNSETOPTS} SEND_EMAIL SUBTREE SVN"
279 GITUNSETOPTS="${GITUNSETOPTS} PCRE PCRE2"
280 PBUILD_FLAGS="OSVERSION=${_OSVERSION} BATCH=yes"
281 PBUILD_FLAGS="${PBUILD_FLAGS} UNAME_r=${UNAME_r}"
282 PBUILD_FLAGS="${PBUILD_FLAGS} OSREL=${REVISION}"
283 PBUILD_FLAGS="${PBUILD_FLAGS} WRKDIRPREFIX=/tmp/ports"
284 PBUILD_FLAGS="${PBUILD_FLAGS} DISTDIR=/tmp/distfiles"
285 eval chroot ${CHROOTDIR} env OPTIONS_UNSET=\"${GITUNSETOPTS}\" \
287 make -C /usr/ports/devel/git FORCE_PKG_REGISTER=1 \
288 WRKDIRPREFIX=/tmp/ports \
289 DISTDIR=/tmp/distfiles \
290 install clean distclean
292 eval chroot ${CHROOTDIR} env ASSUME_ALWAYS_YES=yes \
293 pkg install -y devel/git
294 eval chroot ${CHROOTDIR} env ASSUME_ALWAYS_YES=yes \
299 if [ ! -z "${EMBEDDEDPORTS}" ]; then
300 _OSVERSION=$(chroot ${CHROOTDIR} /usr/bin/uname -U)
301 REVISION=$(chroot ${CHROOTDIR} make -C /usr/src/release -V REVISION)
302 BRANCH=$(chroot ${CHROOTDIR} make -C /usr/src/release -V BRANCH)
303 UNAME_r=${REVISION}-${BRANCH}
304 PBUILD_FLAGS="OSVERSION=${_OSVERSION} BATCH=yes"
305 PBUILD_FLAGS="${PBUILD_FLAGS} UNAME_r=${UNAME_r}"
306 PBUILD_FLAGS="${PBUILD_FLAGS} OSREL=${REVISION}"
307 PBUILD_FLAGS="${PBUILD_FLAGS} WRKDIRPREFIX=/tmp/ports"
308 PBUILD_FLAGS="${PBUILD_FLAGS} DISTDIR=/tmp/distfiles"
309 for _PORT in ${EMBEDDEDPORTS}; do
310 eval chroot ${CHROOTDIR} env ${PBUILD_FLAGS} make -C \
311 /usr/ports/${_PORT} \
312 FORCE_PKG_REGISTER=1 deinstall install clean distclean
319 } # extra_chroot_setup()
321 # chroot_build_target(): Build the userland and kernel for the build target.
322 chroot_build_target() {
324 if [ ! -z "${EMBEDDEDBUILD}" ]; then
325 RELEASE_WMAKEFLAGS="${RELEASE_WMAKEFLAGS} \
326 TARGET=${EMBEDDED_TARGET} \
327 TARGET_ARCH=${EMBEDDED_TARGET_ARCH}"
328 RELEASE_KMAKEFLAGS="${RELEASE_KMAKEFLAGS} \
329 TARGET=${EMBEDDED_TARGET} \
330 TARGET_ARCH=${EMBEDDED_TARGET_ARCH}"
332 eval chroot ${CHROOTDIR} make -C /usr/src ${RELEASE_WMAKEFLAGS} buildworld
333 eval chroot ${CHROOTDIR} make -C /usr/src ${RELEASE_KMAKEFLAGS} buildkernel
336 } # chroot_build_target
338 # chroot_build_release(): Invoke the 'make release' target.
339 chroot_build_release() {
341 if [ ! -z "${WITH_VMIMAGES}" ]; then
342 if [ -z "${VMFORMATS}" ]; then
343 VMFORMATS="$(eval chroot ${CHROOTDIR} \
344 make -C /usr/src/release -V VMFORMATS)"
346 if [ -z "${VMSIZE}" ]; then
347 VMSIZE="$(eval chroot ${CHROOTDIR} \
348 make -C /usr/src/release -V VMSIZE)"
350 RELEASE_RMAKEFLAGS="${RELEASE_RMAKEFLAGS} \
351 VMFORMATS=\"${VMFORMATS}\" VMSIZE=${VMSIZE}"
353 eval chroot ${CHROOTDIR} make -C /usr/src/release \
354 ${RELEASE_RMAKEFLAGS} release
355 eval chroot ${CHROOTDIR} make -C /usr/src/release \
356 ${RELEASE_RMAKEFLAGS} install DESTDIR=/R \
357 WITH_COMPRESSED_IMAGES=${WITH_COMPRESSED_IMAGES} \
358 WITH_COMPRESSED_VMIMAGES=${WITH_COMPRESSED_VMIMAGES}
361 } # chroot_build_release()
378 # chroot_arm_build_release(): Create arm SD card image.
379 chroot_arm_build_release() {
381 case ${EMBEDDED_TARGET} in
383 if [ -e "${RELENGDIR}/tools/arm.subr" ]; then
384 . "${RELENGDIR}/tools/arm.subr"
390 [ ! -z "${RELEASECONF}" ] && . "${RELEASECONF}"
391 export MAKE_FLAGS="${MAKE_FLAGS} TARGET=${EMBEDDED_TARGET}"
392 export MAKE_FLAGS="${MAKE_FLAGS} TARGET_ARCH=${EMBEDDED_TARGET_ARCH}"
393 export MAKE_FLAGS="${MAKE_FLAGS} ${CONF_FILES}"
394 eval chroot ${CHROOTDIR} env WITH_UNIFIED_OBJDIR=1 make ${MAKE_FLAGS} -C /usr/src/release obj
395 export WORLDDIR="$(eval chroot ${CHROOTDIR} make ${MAKE_FLAGS} -C /usr/src/release -V WORLDDIR)"
396 export OBJDIR="$(eval chroot ${CHROOTDIR} env WITH_UNIFIED_OBJDIR=1 make ${MAKE_FLAGS} -C /usr/src/release -V .OBJDIR)"
397 export DESTDIR="${OBJDIR}/${KERNEL}"
398 export IMGBASE="${CHROOTDIR}/${OBJDIR}/${BOARDNAME}.img"
399 export OSRELEASE="$(eval chroot ${CHROOTDIR} make ${MAKE_FLAGS} -C /usr/src/release \
400 TARGET=${EMBEDDED_TARGET} TARGET_ARCH=${EMBEDDED_TARGET_ARCH} \
402 chroot ${CHROOTDIR} mkdir -p ${DESTDIR}
403 chroot ${CHROOTDIR} truncate -s ${IMAGE_SIZE} ${IMGBASE##${CHROOTDIR}}
404 export mddev=$(chroot ${CHROOTDIR} \
405 mdconfig -f ${IMGBASE##${CHROOTDIR}} ${MD_ARGS})
410 mdconfig -d -u ${mddev}
411 chroot ${CHROOTDIR} rmdir ${DESTDIR}
412 mv ${IMGBASE} ${CHROOTDIR}/${OBJDIR}/${OSRELEASE}-${BOARDNAME}.img
413 chroot ${CHROOTDIR} mkdir -p /R
414 chroot ${CHROOTDIR} cp -p ${OBJDIR}/${OSRELEASE}-${BOARDNAME}.img \
415 /R/${OSRELEASE}-${BOARDNAME}.img
416 chroot ${CHROOTDIR} xz -T ${XZ_THREADS} /R/${OSRELEASE}-${BOARDNAME}.img
417 cd ${CHROOTDIR}/R && sha512 ${OSRELEASE}* \
419 cd ${CHROOTDIR}/R && sha256 ${OSRELEASE}* \
423 } # chroot_arm_build_release()
425 # main(): Start here.
427 set -e # Everything must succeed
429 while getopts c: opt; do
432 RELEASECONF="$(realpath ${OPTARG})"
439 shift $(($OPTIND - 1))
440 if [ ! -z "${RELEASECONF}" ]; then
441 if [ -e "${RELEASECONF}" ]; then
444 echo "Nonexistent configuration file: ${RELEASECONF}"
445 echo "Using default build environment."
449 trap "umount ${CHROOTDIR}/dev" EXIT # Clean up devfs mount on exit
453 ${chroot_build_release_cmd}