1251652Sgjb#!/bin/sh
2251652Sgjb#-
3262762Sgjb# Copyright (c) 2013, 2014 The FreeBSD Foundation
4251652Sgjb# Copyright (c) 2013 Glen Barber
5251652Sgjb# Copyright (c) 2011 Nathan Whitehorn
6251652Sgjb# All rights reserved.
7251652Sgjb#
8262762Sgjb# Portions of this software were developed by Glen Barber
9262762Sgjb# under sponsorship from the FreeBSD Foundation.
10262762Sgjb#
11251652Sgjb# Redistribution and use in source and binary forms, with or without
12251652Sgjb# modification, are permitted provided that the following conditions
13251652Sgjb# are met:
14251652Sgjb# 1. Redistributions of source code must retain the above copyright
15251652Sgjb#    notice, this list of conditions and the following disclaimer.
16251652Sgjb# 2. Redistributions in binary form must reproduce the above copyright
17251652Sgjb#    notice, this list of conditions and the following disclaimer in the
18251652Sgjb#    documentation and/or other materials provided with the distribution.
19251652Sgjb#
20251652Sgjb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21251652Sgjb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22251652Sgjb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23251652Sgjb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24251652Sgjb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25251652Sgjb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26251652Sgjb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27251652Sgjb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28251652Sgjb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29251652Sgjb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30251652Sgjb# SUCH DAMAGE.
31251652Sgjb#
32251652Sgjb# release.sh: check out source trees, and build release components with
33251652Sgjb#  totally clean, fresh trees.
34251652Sgjb# Based on release/generate-release.sh written by Nathan Whitehorn
35251652Sgjb#
36251652Sgjb# $FreeBSD$
37251652Sgjb#
38251652Sgjb
39251652SgjbPATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"
40251652Sgjbexport PATH
41251652Sgjb
42251652Sgjb# The directory within which the release will be built.
43251652SgjbCHROOTDIR="/scratch"
44264141SgjbRELENGDIR="$(realpath $(dirname $(basename ${0})))"
45251652Sgjb
46262762Sgjb# The default version control system command to obtain the sources.
47262762SgjbVCSCMD="svn checkout"
48262762Sgjb
49251652Sgjb# The default svn checkout server, and svn branches for src/, doc/,
50251652Sgjb# and ports/.
51262762SgjbSVNROOT="svn://svn.FreeBSD.org/"
52260072SgjbSRCBRANCH="base/head@rHEAD"
53260072SgjbDOCBRANCH="doc/head@rHEAD"
54260072SgjbPORTBRANCH="ports/head@rHEAD"
55251652Sgjb
56264141Sgjb# Set for embedded device builds.
57264141SgjbEMBEDDEDBUILD=
58264141Sgjb
59253019Sgjb# Sometimes one needs to checkout src with --force svn option.
60253019Sgjb# If custom kernel configs copied to src tree before checkout, e.g.
61253019SgjbSRC_FORCE_CHECKOUT=
62253019Sgjb
63251652Sgjb# The default make.conf and src.conf to use.  Set to /dev/null
64251652Sgjb# by default to avoid polluting the chroot(8) environment with
65251652Sgjb# non-default settings.
66251652SgjbMAKE_CONF="/dev/null"
67251652SgjbSRC_CONF="/dev/null"
68251652Sgjb
69251652Sgjb# The number of make(1) jobs, defaults to the number of CPUs available for
70251652Sgjb# buildworld, and half of number of CPUs available for buildkernel.
71262762SgjbWORLD_FLAGS="-j$(sysctl -n hw.ncpu)"
72262762SgjbKERNEL_FLAGS="-j$(( $(( $(sysctl -n hw.ncpu) + 1 )) / 2))"
73262762Sgjb
74251652SgjbMAKE_FLAGS="-s"
75251652Sgjb
76251652Sgjb# The name of the kernel to build, defaults to GENERIC.
77251652SgjbKERNEL="GENERIC"
78251652Sgjb
79251652Sgjb# Set to non-empty value to disable checkout of doc/ and/or ports/.  Disabling
80251652Sgjb# ports/ checkout also forces NODOC to be set.
81251652SgjbNODOC=
82251652SgjbNOPORTS=
83251652Sgjb
84259530Sgjb# Set to non-empty value to build dvd1.iso as part of the release.
85259530SgjbWITH_DVD=
86264246SgjbWITH_COMPRESSED_IMAGES=
87259530Sgjb
88251652Sgjbusage() {
89251652Sgjb	echo "Usage: $0 [-c release.conf]"
90251652Sgjb	exit 1
91251652Sgjb}
92251652Sgjb
93251652Sgjbwhile getopts c: opt; do
94251652Sgjb	case ${opt} in
95251652Sgjb	c)
96251652Sgjb		RELEASECONF="${OPTARG}"
97251652Sgjb		if [ ! -e "${RELEASECONF}" ]; then
98251652Sgjb			echo "ERROR: Configuration file ${RELEASECONF} does not exist."
99251652Sgjb			exit 1
100251652Sgjb		fi
101251652Sgjb		# Source the specified configuration file for overrides
102251652Sgjb		. ${RELEASECONF}
103251652Sgjb		;;
104251652Sgjb	\?)
105251652Sgjb		usage
106251652Sgjb		;;
107251652Sgjb	esac
108251652Sgjbdone
109251652Sgjbshift $(($OPTIND - 1))
110251652Sgjb
111264141Sgjb# Fix for backwards-compatibility with release.conf that does not have the
112264141Sgjb# trailing '/'.
113264141Sgjbcase ${SVNROOT} in
114264141Sgjb	*svn*)
115264141Sgjb		SVNROOT="${SVNROOT}/"
116264141Sgjb		;;
117264141Sgjb	*)
118264141Sgjb		;;
119264141Sgjbesac
120264141Sgjb
121262762Sgjb# Prefix the branches with the SVNROOT for the full checkout URL.
122262762SgjbSRCBRANCH="${SVNROOT}${SRCBRANCH}"
123262762SgjbDOCBRANCH="${SVNROOT}${DOCBRANCH}"
124262762SgjbPORTBRANCH="${SVNROOT}${PORTBRANCH}"
125262762Sgjb
126264141Sgjbif [ -n "${EMBEDDEDBUILD}" ]; then
127264141Sgjb	if [ -z "${XDEV}" ] || [ -z "${XDEV_ARCH}" ]; then
128264141Sgjb		echo "ERROR: XDEV and XDEV_ARCH must be set in ${RELEASECONF}."
129264141Sgjb		exit 1
130264141Sgjb	fi
131264141Sgjb	WITH_DVD=
132264246Sgjb	WITH_COMPRESSED_IMAGES=
133264141Sgjb	NODOC=yes
134264141Sgjbfi
135264141Sgjb
136253019Sgjb# If PORTS is set and NODOC is unset, force NODOC=yes because the ports tree
137253019Sgjb# is required to build the documentation set.
138264141Sgjbif [ -n "${NOPORTS}" ] && [ -z "${NODOC}" ]; then
139253019Sgjb	echo "*** NOTICE: Setting NODOC=1 since ports tree is required"
140253019Sgjb	echo "            and NOPORTS is set."
141253019Sgjb	NODOC=yes
142253019Sgjbfi
143253019Sgjb
144253019Sgjb# If NOPORTS and/or NODOC are unset, they must not pass to make as variables.
145253019Sgjb# The release makefile verifies definedness of NOPORTS/NODOC variables
146253019Sgjb# instead of their values.
147253019SgjbDOCPORTS=
148264141Sgjbif [ -n "${NOPORTS}" ]; then
149258313Sgjb	DOCPORTS="NOPORTS=yes "
150253019Sgjbfi
151264141Sgjbif [ -n "${NODOC}" ]; then
152258313Sgjb	DOCPORTS="${DOCPORTS}NODOC=yes"
153253019Sgjbfi
154253019Sgjb
155251652Sgjb# The aggregated build-time flags based upon variables defined within
156251652Sgjb# this file, unless overridden by release.conf.  In most cases, these
157251652Sgjb# will not need to be changed.
158251652SgjbCONF_FILES="__MAKE_CONF=${MAKE_CONF} SRCCONF=${SRC_CONF}"
159264141Sgjbif [ -n "${TARGET}" ] && [ -n "${TARGET_ARCH}" ]; then
160260072Sgjb	ARCH_FLAGS="TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}"
161260072Sgjbelse
162260072Sgjb	ARCH_FLAGS=
163260072Sgjbfi
164264141SgjbCHROOT_MAKEENV="${CHROOT_MAKEENV} MAKEOBJDIRPREFIX=${CHROOTDIR}/tmp/obj"
165251652SgjbCHROOT_WMAKEFLAGS="${MAKE_FLAGS} ${WORLD_FLAGS} ${CONF_FILES}"
166251652SgjbCHROOT_IMAKEFLAGS="${CONF_FILES}"
167251652SgjbCHROOT_DMAKEFLAGS="${CONF_FILES}"
168251652SgjbRELEASE_WMAKEFLAGS="${MAKE_FLAGS} ${WORLD_FLAGS} ${ARCH_FLAGS} ${CONF_FILES}"
169253019SgjbRELEASE_KMAKEFLAGS="${MAKE_FLAGS} ${KERNEL_FLAGS} KERNCONF=\"${KERNEL}\" ${ARCH_FLAGS} ${CONF_FILES}"
170253019SgjbRELEASE_RMAKEFLAGS="${ARCH_FLAGS} KERNCONF=\"${KERNEL}\" ${CONF_FILES} \
171259530Sgjb	${DOCPORTS} WITH_DVD=${WITH_DVD}"
172251652Sgjb
173253019Sgjb# Force src checkout if configured
174253019SgjbFORCE_SRC_KEY=
175264141Sgjbif [ -n "${SRC_FORCE_CHECKOUT}" ]; then
176258313Sgjb	FORCE_SRC_KEY="--force"
177251652Sgjbfi
178251652Sgjb
179264141Sgjbif [ -z "${CHROOTDIR}" ]; then
180251652Sgjb	echo "Please set CHROOTDIR."
181251652Sgjb	exit 1
182251652Sgjbfi
183251652Sgjb
184251652Sgjbif [ $(id -u) -ne 0 ]; then
185251652Sgjb	echo "Needs to be run as root."
186251652Sgjb	exit 1
187251652Sgjbfi
188251652Sgjb
189251652Sgjbset -e # Everything must succeed
190251652Sgjb
191251652Sgjbmkdir -p ${CHROOTDIR}/usr
192251652Sgjb
193264441Sgjbif [ -z "${SRC_UPDATE_SKIP}" ]; then
194264441Sgjb	${VCSCMD} ${FORCE_SRC_KEY} ${SRCBRANCH} ${CHROOTDIR}/usr/src
195264441Sgjbfi
196264441Sgjbif [ -z "${NODOC}" ] && [ -z "${DOC_UPDATE_SKIP}" ]; then
197262762Sgjb	${VCSCMD} ${DOCBRANCH} ${CHROOTDIR}/usr/doc
198251652Sgjbfi
199264441Sgjbif [ -z "${NOPORTS}" ] && [ -z "${PORTS_UPDATE_SKIP}" ]; then
200262762Sgjb	${VCSCMD} ${PORTBRANCH} ${CHROOTDIR}/usr/ports
201251652Sgjbfi
202251652Sgjb
203264141Sgjbif [ -z "${CHROOTBUILD_SKIP}" ]; then
204264141Sgjb	cd ${CHROOTDIR}/usr/src
205264141Sgjb	env ${CHROOT_MAKEENV} make ${CHROOT_WMAKEFLAGS} buildworld
206264141Sgjb	env ${CHROOT_MAKEENV} make ${CHROOT_IMAKEFLAGS} installworld \
207264141Sgjb		DESTDIR=${CHROOTDIR}
208264141Sgjb	env ${CHROOT_MAKEENV} make ${CHROOT_DMAKEFLAGS} distribution \
209264141Sgjb		DESTDIR=${CHROOTDIR}
210264141Sgjbfi
211251652Sgjbmount -t devfs devfs ${CHROOTDIR}/dev
212260072Sgjbcp /etc/resolv.conf ${CHROOTDIR}/etc/resolv.conf
213251652Sgjbtrap "umount ${CHROOTDIR}/dev" EXIT # Clean up devfs mount on exit
214251652Sgjb
215260072Sgjb# If MAKE_CONF and/or SRC_CONF are set and not character devices (/dev/null),
216260072Sgjb# copy them to the chroot.
217260072Sgjbif [ -e ${MAKE_CONF} ] && [ ! -c ${MAKE_CONF} ]; then
218260072Sgjb	mkdir -p ${CHROOTDIR}/$(dirname ${MAKE_CONF})
219260072Sgjb	cp ${MAKE_CONF} ${CHROOTDIR}/${MAKE_CONF}
220260072Sgjbfi
221260072Sgjbif [ -e ${SRC_CONF} ] && [ ! -c ${SRC_CONF} ]; then
222260072Sgjb	mkdir -p ${CHROOTDIR}/$(dirname ${SRC_CONF})
223260072Sgjb	cp ${SRC_CONF} ${CHROOTDIR}/${SRC_CONF}
224260072Sgjbfi
225260072Sgjb
226264141Sgjb# Embedded builds do not use the 'make release' target.
227264141Sgjbif [ -n "${EMBEDDEDBUILD}" ]; then
228264141Sgjb	# If a crochet configuration file exists in *this* checkout of
229264141Sgjb	# release/, copy it to the /tmp/external directory within the chroot.
230264141Sgjb	# This allows building embedded releases without relying on updated
231264141Sgjb	# scripts and/or configurations to exist in the branch being built.
232264141Sgjb	if [ -e ${RELENGDIR}/tools/${XDEV}/crochet-${KERNEL}.conf ] && \
233264141Sgjb		[ -e ${RELENGDIR}/${XDEV}/release.sh ]; then
234264141Sgjb			mkdir -p ${CHROOTDIR}/tmp/external/${XDEV}/
235264141Sgjb			cp ${RELENGDIR}/tools/${XDEV}/crochet-${KERNEL}.conf \
236264141Sgjb				${CHROOTDIR}/tmp/external/${XDEV}/crochet-${KERNEL}.conf
237264141Sgjb			/bin/sh ${RELENGDIR}/${XDEV}/release.sh
238264141Sgjb	fi
239264141Sgjb	# If the script does not exist for this architecture, exit.
240264141Sgjb	# This probably should be checked earlier, but allowing the rest
241264141Sgjb	# of the build process to get this far will at least set up the
242264141Sgjb	# chroot environment for testing.
243264141Sgjb	exit 0
244264141Sgjbelse
245264141Sgjb	# Not embedded.
246264141Sgjb	continue
247264141Sgjbfi
248264141Sgjb
249260072Sgjbif [ -d ${CHROOTDIR}/usr/ports ]; then
250257836Sgjb	# Run ldconfig(8) in the chroot directory so /var/run/ld-elf*.so.hints
251257836Sgjb	# is created.  This is needed by ports-mgmt/pkg.
252257836Sgjb	chroot ${CHROOTDIR} /etc/rc.d/ldconfig forcerestart
253257836Sgjb
254251652Sgjb	## Trick the ports 'run-autotools-fixup' target to do the right thing.
255251652Sgjb	_OSVERSION=$(sysctl -n kern.osreldate)
256264141Sgjb	if [ -d ${CHROOTDIR}/usr/doc ] && [ -z "${NODOC}" ]; then
257258313Sgjb		PBUILD_FLAGS="OSVERSION=${_OSVERSION} BATCH=yes"
258259530Sgjb		PBUILD_FLAGS="${PBUILD_FLAGS}"
259251652Sgjb		chroot ${CHROOTDIR} make -C /usr/ports/textproc/docproj \
260259530Sgjb			${PBUILD_FLAGS} OPTIONS_UNSET="FOP IGOR" install clean distclean
261251652Sgjb	fi
262252224Sgjbfi
263252224Sgjb
264253019Sgjbeval chroot ${CHROOTDIR} make -C /usr/src ${RELEASE_WMAKEFLAGS} buildworld
265253019Sgjbeval chroot ${CHROOTDIR} make -C /usr/src ${RELEASE_KMAKEFLAGS} buildkernel
266253019Sgjbeval chroot ${CHROOTDIR} make -C /usr/src/release ${RELEASE_RMAKEFLAGS} \
267264141Sgjb	release
268253019Sgjbeval chroot ${CHROOTDIR} make -C /usr/src/release ${RELEASE_RMAKEFLAGS} \
269264246Sgjb	install DESTDIR=/R WITH_COMPRESSED_IMAGES=${WITH_COMPRESSED_IMAGES}
270