1#!/bin/sh
2#-
3# Copyright (c) 2013-2018 The FreeBSD Foundation
4# Copyright (c) 2013 Glen Barber
5# Copyright (c) 2011 Nathan Whitehorn
6# All rights reserved.
7#
8# Portions of this software were developed by Glen Barber
9# under sponsorship from the FreeBSD Foundation.
10#
11# Redistribution and use in source and binary forms, with or without
12# modification, are permitted provided that the following conditions
13# are met:
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.
19#
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
30# SUCH DAMAGE.
31#
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
35#
36# $FreeBSD: stable/10/release/release.sh 328429 2018-01-26 04:32:31Z gjb $
37#
38
39export PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"
40
41VERSION=2
42
43# Prototypes that can be redefined per-chroot or per-target.
44load_chroot_env() { }
45load_target_env() { }
46buildenv_setup() { }
47
48usage() {
49	echo "Usage: $0 [-c release.conf]"
50	exit 1
51}
52
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.
56env_setup() {
57	# The directory within which the release will be built.
58	CHROOTDIR="/scratch"
59	RELENGDIR="$(dirname $(realpath ${0}))"
60
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
66		done
67	done
68	VCSCMD="${VCSCMD} checkout"
69
70	# The default svn checkout server, and svn branches for src/, doc/,
71	# and ports/.
72	SVNROOT="svn://svn.FreeBSD.org/"
73	SRCBRANCH="base/head@rHEAD"
74	DOCBRANCH="doc/head@rHEAD"
75	PORTBRANCH="ports/head@rHEAD"
76
77	# Set for embedded device builds.
78	EMBEDDEDBUILD=
79
80	# Sometimes one needs to checkout src with --force svn option.
81	# If custom kernel configs copied to src tree before checkout, e.g.
82	SRC_FORCE_CHECKOUT=
83
84	# The default make.conf and src.conf to use.  Set to /dev/null
85	# by default to avoid polluting the chroot(8) environment with
86	# non-default settings.
87	MAKE_CONF="/dev/null"
88	SRC_CONF="/dev/null"
89
90	# The number of make(1) jobs, defaults to the number of CPUs available
91	# for buildworld, and half of number of CPUs available for buildkernel.
92	WORLD_FLAGS="-j$(sysctl -n hw.ncpu)"
93	KERNEL_FLAGS="-j$(( $(( $(sysctl -n hw.ncpu) + 1 )) / 2))"
94
95	MAKE_FLAGS="-s"
96
97	# The name of the kernel to build, defaults to GENERIC.
98	KERNEL="GENERIC"
99
100	# Set to non-empty value to disable checkout of doc/ and/or ports/.
101	# Disabling ports/ checkout also forces NODOC to be set.
102	NODOC=
103	NOPORTS=
104
105	# Set to non-empty value to build dvd1.iso as part of the release.
106	WITH_DVD=
107	WITH_COMPRESSED_IMAGES=
108
109	# Set to non-empty value to build virtual machine images as part of
110	# the release.
111	WITH_VMIMAGES=
112	WITH_COMPRESSED_VMIMAGES=
113	XZ_THREADS=0
114
115	# Set to non-empty value to build virtual machine images for various
116	# cloud providers as part of the release.
117	WITH_CLOUDWARE=
118
119	return 0
120} # env_setup()
121
122# env_check(): Perform sanity tests on the build environment, such as ensuring
123# files/directories exist, as well as adding backwards-compatibility hacks if
124# necessary.  This is called unconditionally, and overrides the defaults set
125# in env_setup() if '-c <release.conf>' is specified.
126env_check() {
127	chroot_build_release_cmd="chroot_build_release"
128	# Fix for backwards-compatibility with release.conf that does not have
129	# the trailing '/'.
130	case ${SVNROOT} in
131		*svn*)
132			SVNROOT="${SVNROOT}/"
133			;;
134		*)
135			;;
136	esac
137
138	# Prefix the branches with the SVNROOT for the full checkout URL.
139	SRCBRANCH="${SVNROOT}${SRCBRANCH}"
140	DOCBRANCH="${SVNROOT}${DOCBRANCH}"
141	PORTBRANCH="${SVNROOT}${PORTBRANCH}"
142
143	if [ -n "${EMBEDDEDBUILD}" ]; then
144		WITH_DVD=
145		WITH_COMPRESSED_IMAGES=
146		NODOC=yes
147		case ${EMBEDDED_TARGET}:${EMBEDDED_TARGET_ARCH} in
148			arm:arm*|arm64:aarch64)
149				chroot_build_release_cmd="chroot_arm_build_release"
150				;;
151			*)
152				;;
153		esac
154	fi
155
156	# If PORTS is set and NODOC is unset, force NODOC=yes because the ports
157	# tree is required to build the documentation set.
158	if [ -n "${NOPORTS}" ] && [ -z "${NODOC}" ]; then
159		echo "*** NOTICE: Setting NODOC=1 since ports tree is required"
160		echo "            and NOPORTS is set."
161		NODOC=yes
162	fi
163
164	# If NOPORTS and/or NODOC are unset, they must not pass to make as
165	# variables.  The release makefile verifies definedness of the
166	# NOPORTS/NODOC variables instead of their values.
167	DOCPORTS=
168	if [ -n "${NOPORTS}" ]; then
169		DOCPORTS="NOPORTS=yes "
170	fi
171	if [ -n "${NODOC}" ]; then
172		DOCPORTS="${DOCPORTS}NODOC=yes"
173	fi
174
175	# The aggregated build-time flags based upon variables defined within
176	# this file, unless overridden by release.conf.  In most cases, these
177	# will not need to be changed.
178	CONF_FILES="__MAKE_CONF=${MAKE_CONF} SRCCONF=${SRC_CONF}"
179	if [ -n "${TARGET}" ] && [ -n "${TARGET_ARCH}" ]; then
180		ARCH_FLAGS="TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}"
181	else
182		ARCH_FLAGS=
183	fi
184	# Force src checkout if configured
185	FORCE_SRC_KEY=
186	if [ -n "${SRC_FORCE_CHECKOUT}" ]; then
187		FORCE_SRC_KEY="--force"
188	fi
189
190	if [ -z "${CHROOTDIR}" ]; then
191		echo "Please set CHROOTDIR."
192		exit 1
193	fi
194
195	if [ $(id -u) -ne 0 ]; then
196		echo "Needs to be run as root."
197		exit 1
198	fi
199
200	# Unset CHROOTBUILD_SKIP if the chroot(8) does not appear to exist.
201	if [ ! -z "${CHROOTBUILD_SKIP}" -a ! -e ${CHROOTDIR}/bin/sh ]; then
202		CHROOTBUILD_SKIP=
203	fi
204
205	CHROOT_MAKEENV="${CHROOT_MAKEENV} \
206		MAKEOBJDIRPREFIX=${CHROOTDIR}/tmp/obj"
207	CHROOT_WMAKEFLAGS="${MAKE_FLAGS} ${WORLD_FLAGS} ${CONF_FILES}"
208	CHROOT_IMAKEFLAGS="${CONF_FILES}"
209	CHROOT_DMAKEFLAGS="${CONF_FILES}"
210	RELEASE_WMAKEFLAGS="${MAKE_FLAGS} ${WORLD_FLAGS} ${ARCH_FLAGS} \
211		${CONF_FILES}"
212	RELEASE_KMAKEFLAGS="${MAKE_FLAGS} ${KERNEL_FLAGS} \
213		KERNCONF=\"${KERNEL}\" ${ARCH_FLAGS} ${CONF_FILES}"
214	RELEASE_RMAKEFLAGS="${ARCH_FLAGS} \
215		KERNCONF=\"${KERNEL}\" ${CONF_FILES} ${DOCPORTS} \
216		WITH_DVD=${WITH_DVD} WITH_VMIMAGES=${WITH_VMIMAGES} \
217		WITH_CLOUDWARE=${WITH_CLOUDWARE} XZ_THREADS=${XZ_THREADS}"
218
219	return 0
220} # env_check()
221
222# chroot_setup(): Prepare the build chroot environment for the release build.
223chroot_setup() {
224	load_chroot_env
225	mkdir -p ${CHROOTDIR}/usr
226
227	if [ -z "${SRC_UPDATE_SKIP}" ]; then
228		${VCSCMD} ${FORCE_SRC_KEY} ${SRCBRANCH} ${CHROOTDIR}/usr/src
229	fi
230	if [ -z "${NODOC}" ] && [ -z "${DOC_UPDATE_SKIP}" ]; then
231		${VCSCMD} ${DOCBRANCH} ${CHROOTDIR}/usr/doc
232	fi
233	if [ -z "${NOPORTS}" ] && [ -z "${PORTS_UPDATE_SKIP}" ]; then
234		${VCSCMD} ${PORTBRANCH} ${CHROOTDIR}/usr/ports
235	fi
236
237	if [ -z "${CHROOTBUILD_SKIP}" ]; then
238		cd ${CHROOTDIR}/usr/src
239		env ${CHROOT_MAKEENV} make ${CHROOT_WMAKEFLAGS} buildworld
240		env ${CHROOT_MAKEENV} make ${CHROOT_IMAKEFLAGS} installworld \
241			DESTDIR=${CHROOTDIR}
242		env ${CHROOT_MAKEENV} make ${CHROOT_DMAKEFLAGS} distribution \
243			DESTDIR=${CHROOTDIR}
244	fi
245
246	return 0
247} # chroot_setup()
248
249# extra_chroot_setup(): Prepare anything additional within the build
250# necessary for the release build.
251extra_chroot_setup() {
252	mkdir -p ${CHROOTDIR}/dev
253	mount -t devfs devfs ${CHROOTDIR}/dev
254	[ -e /etc/resolv.conf -a ! -e ${CHROOTDIR}/etc/resolv.conf ] && \
255		cp /etc/resolv.conf ${CHROOTDIR}/etc/resolv.conf
256	# Run ldconfig(8) in the chroot directory so /var/run/ld-elf*.so.hints
257	# is created.  This is needed by ports-mgmt/pkg.
258	eval chroot ${CHROOTDIR} /etc/rc.d/ldconfig forcerestart
259
260	# If MAKE_CONF and/or SRC_CONF are set and not character devices
261	# (/dev/null), copy them to the chroot.
262	if [ -e ${MAKE_CONF} ] && [ ! -c ${MAKE_CONF} ]; then
263		mkdir -p ${CHROOTDIR}/$(dirname ${MAKE_CONF})
264		cp ${MAKE_CONF} ${CHROOTDIR}/${MAKE_CONF}
265	fi
266	if [ -e ${SRC_CONF} ] && [ ! -c ${SRC_CONF} ]; then
267		mkdir -p ${CHROOTDIR}/$(dirname ${SRC_CONF})
268		cp ${SRC_CONF} ${CHROOTDIR}/${SRC_CONF}
269	fi
270
271	if [ -d ${CHROOTDIR}/usr/ports ]; then
272		# Trick the ports 'run-autotools-fixup' target to do the right
273		# thing.
274		_OSVERSION=$(chroot ${CHROOTDIR} /usr/bin/uname -U)
275		REVISION=$(chroot ${CHROOTDIR} make -C /usr/src/release -V REVISION)
276		BRANCH=$(chroot ${CHROOTDIR} make -C /usr/src/release -V BRANCH)
277		UNAME_r=${REVISION}-${BRANCH}
278		if [ -d ${CHROOTDIR}/usr/doc ] && [ -z "${NODOC}" ]; then
279			PBUILD_FLAGS="OSVERSION=${_OSVERSION} BATCH=yes"
280			PBUILD_FLAGS="${PBUILD_FLAGS} UNAME_r=${UNAME_r}"
281			PBUILD_FLAGS="${PBUILD_FLAGS} OSREL=${REVISION}"
282			PBUILD_FLAGS="${PBUILD_FLAGS} WRKDIRPREFIX=/tmp/ports"
283			PBUILD_FLAGS="${PBUILD_FLAGS} DISTDIR=/tmp/distfiles"
284			chroot ${CHROOTDIR} env ${PBUILD_FLAGS} make -C \
285				/usr/ports/textproc/docproj \
286				OPTIONS_UNSET="FOP IGOR" \
287				FORCE_PKG_REGISTER=1 \
288				install clean distclean
289		fi
290	fi
291
292	if [ ! -z "${EMBEDDEDPORTS}" ]; then
293		_OSVERSION=$(chroot ${CHROOTDIR} /usr/bin/uname -U)
294		REVISION=$(chroot ${CHROOTDIR} make -C /usr/src/release -V REVISION)
295		BRANCH=$(chroot ${CHROOTDIR} make -C /usr/src/release -V BRANCH)
296		PBUILD_FLAGS="OSVERSION=${_OSVERSION} BATCH=yes"
297		PBUILD_FLAGS="${PBUILD_FLAGS} UNAME_r=${UNAME_r}"
298		PBUILD_FLAGS="${PBUILD_FLAGS} OSREL=${REVISION}"
299		PBUILD_FLAGS="${PBUILD_FLAGS} WRKDIRPREFIX=/tmp/ports"
300		PBUILD_FLAGS="${PBUILD_FLAGS} DISTDIR=/tmp/distfiles"
301		for _PORT in ${EMBEDDEDPORTS}; do
302			eval chroot ${CHROOTDIR} env ${PBUILD_FLAGS} make -C \
303				/usr/ports/${_PORT} \
304				FORCE_PKG_REGISTER=1 install clean distclean
305		done
306	fi
307
308	buildenv_setup
309
310	return 0
311} # extra_chroot_setup()
312
313# chroot_build_target(): Build the userland and kernel for the build target.
314chroot_build_target() {
315	load_target_env
316	if [ ! -z "${EMBEDDEDBUILD}" ]; then
317		RELEASE_WMAKEFLAGS="${RELEASE_WMAKEFLAGS} \
318			TARGET=${EMBEDDED_TARGET} \
319			TARGET_ARCH=${EMBEDDED_TARGET_ARCH}"
320		RELEASE_KMAKEFLAGS="${RELEASE_KMAKEFLAGS} \
321			TARGET=${EMBEDDED_TARGET} \
322			TARGET_ARCH=${EMBEDDED_TARGET_ARCH}"
323	fi
324	eval chroot ${CHROOTDIR} make -C /usr/src ${RELEASE_WMAKEFLAGS} buildworld
325	eval chroot ${CHROOTDIR} make -C /usr/src ${RELEASE_KMAKEFLAGS} buildkernel
326
327	return 0
328} # chroot_build_target
329
330# chroot_build_release(): Invoke the 'make release' target.
331chroot_build_release() {
332	load_target_env
333	if [ ! -z "${WITH_VMIMAGES}" ]; then
334		if [ -z "${VMFORMATS}" ]; then
335			VMFORMATS="$(eval chroot ${CHROOTDIR} \
336				make -C /usr/src/release -V VMFORMATS)"
337		fi
338		if [ -z "${VMSIZE}" ]; then
339			VMSIZE="$(eval chroot ${CHROOTDIR} \
340				make -C /usr/src/release -V VMSIZE)"
341		fi
342		RELEASE_RMAKEFLAGS="${RELEASE_RMAKEFLAGS} \
343			VMFORMATS=\"${VMFORMATS}\" VMSIZE=${VMSIZE}"
344	fi
345	eval chroot ${CHROOTDIR} make -C /usr/src/release \
346		${RELEASE_RMAKEFLAGS} release
347	eval chroot ${CHROOTDIR} make -C /usr/src/release \
348		${RELEASE_RMAKEFLAGS} install DESTDIR=/R \
349		WITH_COMPRESSED_IMAGES=${WITH_COMPRESSED_IMAGES} \
350		WITH_COMPRESSED_VMIMAGES=${WITH_COMPRESSED_VMIMAGES}
351
352	return 0
353} # chroot_build_release()
354
355# chroot_arm_build_release(): Create arm SD card image.
356chroot_arm_build_release() {
357	load_target_env
358	eval chroot ${CHROOTDIR} make -C /usr/src/release obj
359	case ${EMBEDDED_TARGET} in
360		arm|arm64)
361			if [ -e "${RELENGDIR}/tools/arm.subr" ]; then
362				. "${RELENGDIR}/tools/arm.subr"
363			fi
364			;;
365		*)
366			;;
367	esac
368	[ ! -z "${RELEASECONF}" ] && . "${RELEASECONF}"
369	WORLDDIR="$(eval chroot ${CHROOTDIR} make -C /usr/src/release -V WORLDDIR)"
370	OBJDIR="$(eval chroot ${CHROOTDIR} make -C /usr/src/release -V .OBJDIR)"
371	DESTDIR="${OBJDIR}/${KERNEL}"
372	IMGBASE="${CHROOTDIR}/${OBJDIR}/${KERNEL}.img"
373	OSRELEASE="$(eval chroot ${CHROOTDIR} make -C /usr/src/release \
374		TARGET=${EMBEDDED_TARGET} TARGET_ARCH=${EMBEDDED_TARGET_ARCH} \
375		-V OSRELEASE)"
376	chroot ${CHROOTDIR} mkdir -p ${DESTDIR}
377	chroot ${CHROOTDIR} truncate -s ${IMAGE_SIZE} ${IMGBASE##${CHROOTDIR}}
378	export mddev=$(chroot ${CHROOTDIR} \
379		mdconfig -f ${IMGBASE##${CHROOTDIR}} ${MD_ARGS})
380	arm_create_disk
381	arm_install_base
382	arm_install_uboot
383	mdconfig -d -u ${mddev}
384	chroot ${CHROOTDIR} rmdir ${DESTDIR}
385	mv ${IMGBASE} ${CHROOTDIR}/${OBJDIR}/${OSRELEASE}-${KERNEL}.img
386	chroot ${CHROOTDIR} mkdir -p /R
387	chroot ${CHROOTDIR} cp -p ${OBJDIR}/${OSRELEASE}-${KERNEL}.img \
388		/R/${OSRELEASE}-${KERNEL}.img
389	chroot ${CHROOTDIR} xz -T ${XZ_THREADS} /R/${OSRELEASE}-${KERNEL}.img
390	cd ${CHROOTDIR}/R && sha512 ${OSRELEASE}* \
391		> CHECKSUM.SHA512
392	cd ${CHROOTDIR}/R && sha256 ${OSRELEASE}* \
393		> CHECKSUM.SHA256
394
395	return 0
396} # chroot_arm_build_release()
397
398# main(): Start here.
399main() {
400	set -e # Everything must succeed
401	env_setup
402	while getopts c: opt; do
403		case ${opt} in
404			c)
405				RELEASECONF="$(realpath ${OPTARG})"
406				;;
407			\?)
408				usage
409				;;
410		esac
411	done
412	shift $(($OPTIND - 1))
413	if [ ! -z "${RELEASECONF}" ]; then
414		if [ -e "${RELEASECONF}" ]; then
415			. ${RELEASECONF}
416		else
417			echo "Nonexistent configuration file: ${RELEASECONF}"
418			echo "Using default build environment."
419		fi
420	fi
421	env_check
422	trap "umount ${CHROOTDIR}/dev" EXIT # Clean up devfs mount on exit
423	chroot_setup
424	extra_chroot_setup
425	chroot_build_target
426	${chroot_build_release_cmd}
427
428	return 0
429} # main()
430
431main "${@}"
432