release.sh revision 278985
1251652Sgjb#!/bin/sh
2251652Sgjb#-
3278985Sgjb# Copyright (c) 2013-2015 The FreeBSD Foundation
4251652Sgjb# Copyright (c) 2013 Glen Barber
5251652Sgjb# Copyright (c) 2011 Nathan Whitehorn
6251652Sgjb# All rights reserved.
7251652Sgjb#
8262761Sgjb# Portions of this software were developed by Glen Barber
9262761Sgjb# under sponsorship from the FreeBSD Foundation.
10262761Sgjb#
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: stable/10/release/release.sh 278985 2015-02-19 03:57:47Z gjb $
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"
44264106SgjbRELENGDIR="$(realpath $(dirname $(basename ${0})))"
45251652Sgjb
46262761Sgjb# The default version control system command to obtain the sources.
47262761SgjbVCSCMD="svn checkout"
48262761Sgjb
49251652Sgjb# The default svn checkout server, and svn branches for src/, doc/,
50251652Sgjb# and ports/.
51262761SgjbSVNROOT="svn://svn.FreeBSD.org/"
52254293SgjbSRCBRANCH="base/head@rHEAD"
53254293SgjbDOCBRANCH="doc/head@rHEAD"
54254293SgjbPORTBRANCH="ports/head@rHEAD"
55251652Sgjb
56264106Sgjb# Set for embedded device builds.
57264106SgjbEMBEDDEDBUILD=
58264106Sgjb
59252846Sgjb# Sometimes one needs to checkout src with --force svn option.
60252846Sgjb# If custom kernel configs copied to src tree before checkout, e.g.
61252846SgjbSRC_FORCE_CHECKOUT=
62252846Sgjb
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.
71262761SgjbWORLD_FLAGS="-j$(sysctl -n hw.ncpu)"
72262761SgjbKERNEL_FLAGS="-j$(( $(( $(sysctl -n hw.ncpu) + 1 )) / 2))"
73262761Sgjb
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
84259151Sgjb# Set to non-empty value to build dvd1.iso as part of the release.
85259151SgjbWITH_DVD=
86264245SgjbWITH_COMPRESSED_IMAGES=
87259151Sgjb
88273080Sgjb# Set to non-empty value to build virtual machine images as part of
89273080Sgjb# the release.
90273080SgjbWITH_VMIMAGES=
91273080SgjbWITH_COMPRESSED_VMIMAGES=
92273080Sgjb
93278985Sgjb# Set to non-empty value to build virtual machine images for various
94278985Sgjb# cloud providers as part of the release.
95278985SgjbWITH_CLOUDWARE=
96278985Sgjb
97251652Sgjbusage() {
98251652Sgjb	echo "Usage: $0 [-c release.conf]"
99251652Sgjb	exit 1
100251652Sgjb}
101251652Sgjb
102251652Sgjbwhile getopts c: opt; do
103251652Sgjb	case ${opt} in
104251652Sgjb	c)
105251652Sgjb		RELEASECONF="${OPTARG}"
106251652Sgjb		if [ ! -e "${RELEASECONF}" ]; then
107251652Sgjb			echo "ERROR: Configuration file ${RELEASECONF} does not exist."
108251652Sgjb			exit 1
109251652Sgjb		fi
110251652Sgjb		# Source the specified configuration file for overrides
111251652Sgjb		. ${RELEASECONF}
112251652Sgjb		;;
113251652Sgjb	\?)
114251652Sgjb		usage
115251652Sgjb		;;
116251652Sgjb	esac
117251652Sgjbdone
118251652Sgjbshift $(($OPTIND - 1))
119251652Sgjb
120264106Sgjb# Fix for backwards-compatibility with release.conf that does not have the
121264106Sgjb# trailing '/'.
122264106Sgjbcase ${SVNROOT} in
123264106Sgjb	*svn*)
124264106Sgjb		SVNROOT="${SVNROOT}/"
125264106Sgjb		;;
126264106Sgjb	*)
127264106Sgjb		;;
128264106Sgjbesac
129264106Sgjb
130262761Sgjb# Prefix the branches with the SVNROOT for the full checkout URL.
131262761SgjbSRCBRANCH="${SVNROOT}${SRCBRANCH}"
132262761SgjbDOCBRANCH="${SVNROOT}${DOCBRANCH}"
133262761SgjbPORTBRANCH="${SVNROOT}${PORTBRANCH}"
134262761Sgjb
135264106Sgjbif [ -n "${EMBEDDEDBUILD}" ]; then
136264106Sgjb	if [ -z "${XDEV}" ] || [ -z "${XDEV_ARCH}" ]; then
137264106Sgjb		echo "ERROR: XDEV and XDEV_ARCH must be set in ${RELEASECONF}."
138264106Sgjb		exit 1
139264106Sgjb	fi
140264106Sgjb	WITH_DVD=
141264245Sgjb	WITH_COMPRESSED_IMAGES=
142264106Sgjb	NODOC=yes
143264106Sgjbfi
144264106Sgjb
145252846Sgjb# If PORTS is set and NODOC is unset, force NODOC=yes because the ports tree
146252846Sgjb# is required to build the documentation set.
147264106Sgjbif [ -n "${NOPORTS}" ] && [ -z "${NODOC}" ]; then
148252846Sgjb	echo "*** NOTICE: Setting NODOC=1 since ports tree is required"
149252846Sgjb	echo "            and NOPORTS is set."
150252846Sgjb	NODOC=yes
151252846Sgjbfi
152252846Sgjb
153252846Sgjb# If NOPORTS and/or NODOC are unset, they must not pass to make as variables.
154252846Sgjb# The release makefile verifies definedness of NOPORTS/NODOC variables
155252846Sgjb# instead of their values.
156252846SgjbDOCPORTS=
157264106Sgjbif [ -n "${NOPORTS}" ]; then
158259225Sgjb	DOCPORTS="NOPORTS=yes "
159252846Sgjbfi
160264106Sgjbif [ -n "${NODOC}" ]; then
161259225Sgjb	DOCPORTS="${DOCPORTS}NODOC=yes"
162252846Sgjbfi
163252846Sgjb
164251652Sgjb# The aggregated build-time flags based upon variables defined within
165251652Sgjb# this file, unless overridden by release.conf.  In most cases, these
166251652Sgjb# will not need to be changed.
167251652SgjbCONF_FILES="__MAKE_CONF=${MAKE_CONF} SRCCONF=${SRC_CONF}"
168264106Sgjbif [ -n "${TARGET}" ] && [ -n "${TARGET_ARCH}" ]; then
169254293Sgjb	ARCH_FLAGS="TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}"
170254293Sgjbelse
171254293Sgjb	ARCH_FLAGS=
172254293Sgjbfi
173264106SgjbCHROOT_MAKEENV="${CHROOT_MAKEENV} MAKEOBJDIRPREFIX=${CHROOTDIR}/tmp/obj"
174251652SgjbCHROOT_WMAKEFLAGS="${MAKE_FLAGS} ${WORLD_FLAGS} ${CONF_FILES}"
175251652SgjbCHROOT_IMAKEFLAGS="${CONF_FILES}"
176251652SgjbCHROOT_DMAKEFLAGS="${CONF_FILES}"
177251652SgjbRELEASE_WMAKEFLAGS="${MAKE_FLAGS} ${WORLD_FLAGS} ${ARCH_FLAGS} ${CONF_FILES}"
178252846SgjbRELEASE_KMAKEFLAGS="${MAKE_FLAGS} ${KERNEL_FLAGS} KERNCONF=\"${KERNEL}\" ${ARCH_FLAGS} ${CONF_FILES}"
179252846SgjbRELEASE_RMAKEFLAGS="${ARCH_FLAGS} KERNCONF=\"${KERNEL}\" ${CONF_FILES} \
180278985Sgjb	${DOCPORTS} WITH_DVD=${WITH_DVD} WITH_VMIMAGES=${WITH_VMIMAGES} \
181278985Sgjb	WITH_CLOUDWARE=${WITH_CLOUDWARE}"
182251652Sgjb
183252846Sgjb# Force src checkout if configured
184252846SgjbFORCE_SRC_KEY=
185264106Sgjbif [ -n "${SRC_FORCE_CHECKOUT}" ]; then
186259225Sgjb	FORCE_SRC_KEY="--force"
187251652Sgjbfi
188251652Sgjb
189264106Sgjbif [ -z "${CHROOTDIR}" ]; then
190251652Sgjb	echo "Please set CHROOTDIR."
191251652Sgjb	exit 1
192251652Sgjbfi
193251652Sgjb
194251652Sgjbif [ $(id -u) -ne 0 ]; then
195251652Sgjb	echo "Needs to be run as root."
196251652Sgjb	exit 1
197251652Sgjbfi
198251652Sgjb
199251652Sgjbset -e # Everything must succeed
200251652Sgjb
201251652Sgjbmkdir -p ${CHROOTDIR}/usr
202251652Sgjb
203264440Sgjbif [ -z "${SRC_UPDATE_SKIP}" ]; then
204264440Sgjb	${VCSCMD} ${FORCE_SRC_KEY} ${SRCBRANCH} ${CHROOTDIR}/usr/src
205264440Sgjbfi
206264440Sgjbif [ -z "${NODOC}" ] && [ -z "${DOC_UPDATE_SKIP}" ]; then
207262761Sgjb	${VCSCMD} ${DOCBRANCH} ${CHROOTDIR}/usr/doc
208251652Sgjbfi
209264440Sgjbif [ -z "${NOPORTS}" ] && [ -z "${PORTS_UPDATE_SKIP}" ]; then
210262761Sgjb	${VCSCMD} ${PORTBRANCH} ${CHROOTDIR}/usr/ports
211251652Sgjbfi
212251652Sgjb
213264106Sgjbif [ -z "${CHROOTBUILD_SKIP}" ]; then
214264106Sgjb	cd ${CHROOTDIR}/usr/src
215264106Sgjb	env ${CHROOT_MAKEENV} make ${CHROOT_WMAKEFLAGS} buildworld
216264106Sgjb	env ${CHROOT_MAKEENV} make ${CHROOT_IMAKEFLAGS} installworld \
217264106Sgjb		DESTDIR=${CHROOTDIR}
218264106Sgjb	env ${CHROOT_MAKEENV} make ${CHROOT_DMAKEFLAGS} distribution \
219264106Sgjb		DESTDIR=${CHROOTDIR}
220264106Sgjbfi
221251652Sgjbmount -t devfs devfs ${CHROOTDIR}/dev
222260071Sgjbcp /etc/resolv.conf ${CHROOTDIR}/etc/resolv.conf
223251652Sgjbtrap "umount ${CHROOTDIR}/dev" EXIT # Clean up devfs mount on exit
224251652Sgjb
225260071Sgjb# If MAKE_CONF and/or SRC_CONF are set and not character devices (/dev/null),
226260071Sgjb# copy them to the chroot.
227260071Sgjbif [ -e ${MAKE_CONF} ] && [ ! -c ${MAKE_CONF} ]; then
228260071Sgjb	mkdir -p ${CHROOTDIR}/$(dirname ${MAKE_CONF})
229260071Sgjb	cp ${MAKE_CONF} ${CHROOTDIR}/${MAKE_CONF}
230260071Sgjbfi
231260071Sgjbif [ -e ${SRC_CONF} ] && [ ! -c ${SRC_CONF} ]; then
232260071Sgjb	mkdir -p ${CHROOTDIR}/$(dirname ${SRC_CONF})
233260071Sgjb	cp ${SRC_CONF} ${CHROOTDIR}/${SRC_CONF}
234260071Sgjbfi
235260071Sgjb
236264106Sgjb# Embedded builds do not use the 'make release' target.
237264106Sgjbif [ -n "${EMBEDDEDBUILD}" ]; then
238264106Sgjb	# If a crochet configuration file exists in *this* checkout of
239264106Sgjb	# release/, copy it to the /tmp/external directory within the chroot.
240264106Sgjb	# This allows building embedded releases without relying on updated
241264106Sgjb	# scripts and/or configurations to exist in the branch being built.
242264106Sgjb	if [ -e ${RELENGDIR}/tools/${XDEV}/crochet-${KERNEL}.conf ] && \
243264106Sgjb		[ -e ${RELENGDIR}/${XDEV}/release.sh ]; then
244264106Sgjb			mkdir -p ${CHROOTDIR}/tmp/external/${XDEV}/
245264106Sgjb			cp ${RELENGDIR}/tools/${XDEV}/crochet-${KERNEL}.conf \
246264106Sgjb				${CHROOTDIR}/tmp/external/${XDEV}/crochet-${KERNEL}.conf
247264106Sgjb			/bin/sh ${RELENGDIR}/${XDEV}/release.sh
248264106Sgjb	fi
249264106Sgjb	# If the script does not exist for this architecture, exit.
250264106Sgjb	# This probably should be checked earlier, but allowing the rest
251264106Sgjb	# of the build process to get this far will at least set up the
252264106Sgjb	# chroot environment for testing.
253264106Sgjb	exit 0
254264106Sgjbelse
255264106Sgjb	# Not embedded.
256264106Sgjb	continue
257264106Sgjbfi
258264106Sgjb
259260071Sgjbif [ -d ${CHROOTDIR}/usr/ports ]; then
260257776Sgjb	# Run ldconfig(8) in the chroot directory so /var/run/ld-elf*.so.hints
261257776Sgjb	# is created.  This is needed by ports-mgmt/pkg.
262257776Sgjb	chroot ${CHROOTDIR} /etc/rc.d/ldconfig forcerestart
263257776Sgjb
264251652Sgjb	## Trick the ports 'run-autotools-fixup' target to do the right thing.
265251652Sgjb	_OSVERSION=$(sysctl -n kern.osreldate)
266270688Sgjb	REVISION=$(chroot ${CHROOTDIR} make -C /usr/src/release -V REVISION)
267270688Sgjb	BRANCH=$(chroot ${CHROOTDIR} make -C /usr/src/release -V BRANCH)
268270688Sgjb	UNAME_r=${REVISION}-${BRANCH}
269264106Sgjb	if [ -d ${CHROOTDIR}/usr/doc ] && [ -z "${NODOC}" ]; then
270258261Sgjb		PBUILD_FLAGS="OSVERSION=${_OSVERSION} BATCH=yes"
271270688Sgjb		PBUILD_FLAGS="${PBUILD_FLAGS} UNAME_r=${UNAME_r}"
272270688Sgjb		PBUILD_FLAGS="${PBUILD_FLAGS} OSREL=${REVISION}"
273251652Sgjb		chroot ${CHROOTDIR} make -C /usr/ports/textproc/docproj \
274270688Sgjb			${PBUILD_FLAGS} OPTIONS_UNSET="FOP IGOR" \
275270688Sgjb			install clean distclean
276251652Sgjb	fi
277252101Sgjbfi
278252101Sgjb
279252846Sgjbeval chroot ${CHROOTDIR} make -C /usr/src ${RELEASE_WMAKEFLAGS} buildworld
280252846Sgjbeval chroot ${CHROOTDIR} make -C /usr/src ${RELEASE_KMAKEFLAGS} buildkernel
281252846Sgjbeval chroot ${CHROOTDIR} make -C /usr/src/release ${RELEASE_RMAKEFLAGS} \
282264106Sgjb	release
283252846Sgjbeval chroot ${CHROOTDIR} make -C /usr/src/release ${RELEASE_RMAKEFLAGS} \
284273080Sgjb	install DESTDIR=/R WITH_COMPRESSED_IMAGES=${WITH_COMPRESSED_IMAGES} \
285273080Sgjb	WITH_COMPRESSED_VMIMAGES=${WITH_COMPRESSED_VMIMAGES}
286