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 ${VCSCMD} ${PORTBRANCH} ${CHROOTDIR}/usr/ports
227 if [ -z "${CHROOTBUILD_SKIP}" ]; then
228 cd ${CHROOTDIR}/usr/src
229 env ${CHROOT_MAKEENV} make ${CHROOT_WMAKEFLAGS} buildworld
230 env ${CHROOT_MAKEENV} make ${CHROOT_IMAKEFLAGS} installworld \
232 env ${CHROOT_MAKEENV} make ${CHROOT_DMAKEFLAGS} distribution \
239 # extra_chroot_setup(): Prepare anything additional within the build
240 # necessary for the release build.
241 extra_chroot_setup() {
242 mkdir -p ${CHROOTDIR}/dev
243 mount -t devfs devfs ${CHROOTDIR}/dev
244 [ -e /etc/resolv.conf -a ! -e ${CHROOTDIR}/etc/resolv.conf ] && \
245 cp /etc/resolv.conf ${CHROOTDIR}/etc/resolv.conf
246 # Run ldconfig(8) in the chroot directory so /var/run/ld-elf*.so.hints
247 # is created. This is needed by ports-mgmt/pkg.
248 eval chroot ${CHROOTDIR} /etc/rc.d/ldconfig forcerestart
250 # If MAKE_CONF and/or SRC_CONF are set and not character devices
251 # (/dev/null), copy them to the chroot.
252 if [ -e ${MAKE_CONF} ] && [ ! -c ${MAKE_CONF} ]; then
253 mkdir -p ${CHROOTDIR}/$(dirname ${MAKE_CONF})
254 cp ${MAKE_CONF} ${CHROOTDIR}/${MAKE_CONF}
256 if [ -e ${SRC_CONF} ] && [ ! -c ${SRC_CONF} ]; then
257 mkdir -p ${CHROOTDIR}/$(dirname ${SRC_CONF})
258 cp ${SRC_CONF} ${CHROOTDIR}/${SRC_CONF}
261 if [ -z "${NOGIT}" ]; then
262 # Install git from ports or packages if the ports tree is
263 # available and VCSCMD is unset.
264 _gitcmd="$(which git)"
265 if [ -d ${CHROOTDIR}/usr/ports -a -z "${_gitcmd}" ]; then
266 # Trick the ports 'run-autotools-fixup' target to do the right
268 _OSVERSION=$(chroot ${CHROOTDIR} /usr/bin/uname -U)
269 REVISION=$(chroot ${CHROOTDIR} make -C /usr/src/release -V REVISION)
270 BRANCH=$(chroot ${CHROOTDIR} make -C /usr/src/release -V BRANCH)
271 UNAME_r=${REVISION}-${BRANCH}
272 GITUNSETOPTS="CONTRIB CURL CVS GITWEB GUI HTMLDOCS"
273 GITUNSETOPTS="${GITUNSETOPTS} ICONV NLS P4 PERL"
274 GITUNSETOPTS="${GITUNSETOPTS} SEND_EMAIL SUBTREE SVN"
275 GITUNSETOPTS="${GITUNSETOPTS} PCRE PCRE2"
276 PBUILD_FLAGS="OSVERSION=${_OSVERSION} BATCH=yes"
277 PBUILD_FLAGS="${PBUILD_FLAGS} UNAME_r=${UNAME_r}"
278 PBUILD_FLAGS="${PBUILD_FLAGS} OSREL=${REVISION}"
279 PBUILD_FLAGS="${PBUILD_FLAGS} WRKDIRPREFIX=/tmp/ports"
280 PBUILD_FLAGS="${PBUILD_FLAGS} DISTDIR=/tmp/distfiles"
281 eval chroot ${CHROOTDIR} env OPTIONS_UNSET=\"${GITUNSETOPTS}\" \
283 make -C /usr/ports/devel/git FORCE_PKG_REGISTER=1 \
284 WRKDIRPREFIX=/tmp/ports \
285 DISTDIR=/tmp/distfiles \
286 install clean distclean
288 eval chroot ${CHROOTDIR} env ASSUME_ALWAYS_YES=yes \
289 pkg install -y devel/git
290 eval chroot ${CHROOTDIR} env ASSUME_ALWAYS_YES=yes \
295 if [ ! -z "${EMBEDDEDPORTS}" ]; then
296 _OSVERSION=$(chroot ${CHROOTDIR} /usr/bin/uname -U)
297 REVISION=$(chroot ${CHROOTDIR} make -C /usr/src/release -V REVISION)
298 BRANCH=$(chroot ${CHROOTDIR} make -C /usr/src/release -V BRANCH)
299 PBUILD_FLAGS="OSVERSION=${_OSVERSION} BATCH=yes"
300 PBUILD_FLAGS="${PBUILD_FLAGS} UNAME_r=${UNAME_r}"
301 PBUILD_FLAGS="${PBUILD_FLAGS} OSREL=${REVISION}"
302 PBUILD_FLAGS="${PBUILD_FLAGS} WRKDIRPREFIX=/tmp/ports"
303 PBUILD_FLAGS="${PBUILD_FLAGS} DISTDIR=/tmp/distfiles"
304 for _PORT in ${EMBEDDEDPORTS}; do
305 eval chroot ${CHROOTDIR} env ${PBUILD_FLAGS} make -C \
306 /usr/ports/${_PORT} \
307 FORCE_PKG_REGISTER=1 deinstall install clean distclean
314 } # extra_chroot_setup()
316 # chroot_build_target(): Build the userland and kernel for the build target.
317 chroot_build_target() {
319 if [ ! -z "${EMBEDDEDBUILD}" ]; then
320 RELEASE_WMAKEFLAGS="${RELEASE_WMAKEFLAGS} \
321 TARGET=${EMBEDDED_TARGET} \
322 TARGET_ARCH=${EMBEDDED_TARGET_ARCH}"
323 RELEASE_KMAKEFLAGS="${RELEASE_KMAKEFLAGS} \
324 TARGET=${EMBEDDED_TARGET} \
325 TARGET_ARCH=${EMBEDDED_TARGET_ARCH}"
327 eval chroot ${CHROOTDIR} make -C /usr/src ${RELEASE_WMAKEFLAGS} buildworld
328 eval chroot ${CHROOTDIR} make -C /usr/src ${RELEASE_KMAKEFLAGS} buildkernel
331 } # chroot_build_target
333 # chroot_build_release(): Invoke the 'make release' target.
334 chroot_build_release() {
336 if [ ! -z "${WITH_VMIMAGES}" ]; then
337 if [ -z "${VMFORMATS}" ]; then
338 VMFORMATS="$(eval chroot ${CHROOTDIR} \
339 make -C /usr/src/release -V VMFORMATS)"
341 if [ -z "${VMSIZE}" ]; then
342 VMSIZE="$(eval chroot ${CHROOTDIR} \
343 make -C /usr/src/release -V VMSIZE)"
345 RELEASE_RMAKEFLAGS="${RELEASE_RMAKEFLAGS} \
346 VMFORMATS=\"${VMFORMATS}\" VMSIZE=${VMSIZE}"
348 eval chroot ${CHROOTDIR} make -C /usr/src/release \
349 ${RELEASE_RMAKEFLAGS} release
350 eval chroot ${CHROOTDIR} make -C /usr/src/release \
351 ${RELEASE_RMAKEFLAGS} install DESTDIR=/R \
352 WITH_COMPRESSED_IMAGES=${WITH_COMPRESSED_IMAGES} \
353 WITH_COMPRESSED_VMIMAGES=${WITH_COMPRESSED_VMIMAGES}
356 } # chroot_build_release()
373 # chroot_arm_build_release(): Create arm SD card image.
374 chroot_arm_build_release() {
376 case ${EMBEDDED_TARGET} in
378 if [ -e "${RELENGDIR}/tools/arm.subr" ]; then
379 . "${RELENGDIR}/tools/arm.subr"
385 [ ! -z "${RELEASECONF}" ] && . "${RELEASECONF}"
386 export MAKE_FLAGS="${MAKE_FLAGS} TARGET=${EMBEDDED_TARGET}"
387 export MAKE_FLAGS="${MAKE_FLAGS} TARGET_ARCH=${EMBEDDED_TARGET_ARCH}"
388 export MAKE_FLAGS="${MAKE_FLAGS} ${CONF_FILES}"
389 eval chroot ${CHROOTDIR} env WITH_UNIFIED_OBJDIR=1 make ${MAKE_FLAGS} -C /usr/src/release obj
390 export WORLDDIR="$(eval chroot ${CHROOTDIR} make ${MAKE_FLAGS} -C /usr/src/release -V WORLDDIR)"
391 export OBJDIR="$(eval chroot ${CHROOTDIR} env WITH_UNIFIED_OBJDIR=1 make ${MAKE_FLAGS} -C /usr/src/release -V .OBJDIR)"
392 export DESTDIR="${OBJDIR}/${KERNEL}"
393 export IMGBASE="${CHROOTDIR}/${OBJDIR}/${BOARDNAME}.img"
394 export OSRELEASE="$(eval chroot ${CHROOTDIR} make ${MAKE_FLAGS} -C /usr/src/release \
395 TARGET=${EMBEDDED_TARGET} TARGET_ARCH=${EMBEDDED_TARGET_ARCH} \
397 chroot ${CHROOTDIR} mkdir -p ${DESTDIR}
398 chroot ${CHROOTDIR} truncate -s ${IMAGE_SIZE} ${IMGBASE##${CHROOTDIR}}
399 export mddev=$(chroot ${CHROOTDIR} \
400 mdconfig -f ${IMGBASE##${CHROOTDIR}} ${MD_ARGS})
405 mdconfig -d -u ${mddev}
406 chroot ${CHROOTDIR} rmdir ${DESTDIR}
407 mv ${IMGBASE} ${CHROOTDIR}/${OBJDIR}/${OSRELEASE}-${BOARDNAME}.img
408 chroot ${CHROOTDIR} mkdir -p /R
409 chroot ${CHROOTDIR} cp -p ${OBJDIR}/${OSRELEASE}-${BOARDNAME}.img \
410 /R/${OSRELEASE}-${BOARDNAME}.img
411 chroot ${CHROOTDIR} xz -T ${XZ_THREADS} /R/${OSRELEASE}-${BOARDNAME}.img
412 cd ${CHROOTDIR}/R && sha512 ${OSRELEASE}* \
414 cd ${CHROOTDIR}/R && sha256 ${OSRELEASE}* \
418 } # chroot_arm_build_release()
420 # main(): Start here.
422 set -e # Everything must succeed
424 while getopts c: opt; do
427 RELEASECONF="$(realpath ${OPTARG})"
434 shift $(($OPTIND - 1))
435 if [ ! -z "${RELEASECONF}" ]; then
436 if [ -e "${RELEASECONF}" ]; then
439 echo "Nonexistent configuration file: ${RELEASECONF}"
440 echo "Using default build environment."
444 trap "umount ${CHROOTDIR}/dev" EXIT # Clean up devfs mount on exit
448 ${chroot_build_release_cmd}