release.sh revision 259530
176105Sphantom#!/bin/sh
276105Sphantom#-
376105Sphantom# Copyright (c) 2013 Glen Barber
476105Sphantom# Copyright (c) 2011 Nathan Whitehorn
576105Sphantom# All rights reserved.
676105Sphantom#
790936Sphantom# Redistribution and use in source and binary forms, with or without
876105Sphantom# modification, are permitted provided that the following conditions
990936Sphantom# are met:
1076105Sphantom# 1. Redistributions of source code must retain the above copyright
1176105Sphantom#    notice, this list of conditions and the following disclaimer.
1276105Sphantom# 2. Redistributions in binary form must reproduce the above copyright
1376105Sphantom#    notice, this list of conditions and the following disclaimer in the
1476105Sphantom#    documentation and/or other materials provided with the distribution.
1576105Sphantom#
1676105Sphantom# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1776105Sphantom# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1876105Sphantom# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1976105Sphantom# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2076105Sphantom# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2176105Sphantom# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2276105Sphantom# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2376105Sphantom# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2476105Sphantom# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2576105Sphantom# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2676105Sphantom# SUCH DAMAGE.
2776105Sphantom#
2876105Sphantom# release.sh: check out source trees, and build release components with
2976105Sphantom#  totally clean, fresh trees.
3076105Sphantom# Based on release/generate-release.sh written by Nathan Whitehorn
3176105Sphantom#
3276105Sphantom# $FreeBSD: stable/9/release/release.sh 259530 2013-12-18 00:07:52Z gjb $
3376105Sphantom#
3476105Sphantom
3576105SphantomPATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"
3676105Sphantomexport PATH
37
38# The directory within which the release will be built.
39CHROOTDIR="/scratch"
40
41# The default svn checkout server, and svn branches for src/, doc/,
42# and ports/.
43SVNROOT="svn://svn.freebsd.org"
44SRCBRANCH="base/head"
45DOCBRANCH="doc/head"
46PORTBRANCH="ports/head"
47
48# Sometimes one needs to checkout src with --force svn option.
49# If custom kernel configs copied to src tree before checkout, e.g.
50SRC_FORCE_CHECKOUT=
51
52# The default src/, doc/, and ports/ revisions.
53SRCREVISION="-rHEAD"
54DOCREVISION="-rHEAD"
55PORTREVISION="-rHEAD"
56
57# The default make.conf and src.conf to use.  Set to /dev/null
58# by default to avoid polluting the chroot(8) environment with
59# non-default settings.
60MAKE_CONF="/dev/null"
61SRC_CONF="/dev/null"
62
63# The number of make(1) jobs, defaults to the number of CPUs available for
64# buildworld, and half of number of CPUs available for buildkernel.
65WORLD_FLAGS="-j$(sysctl -n hw.ncpu)"
66KERNEL_FLAGS="-j$(expr $(sysctl -n hw.ncpu) / 2)"
67MAKE_FLAGS="-s"
68
69# The name of the kernel to build, defaults to GENERIC.
70KERNEL="GENERIC"
71
72# The TARGET and TARGET_ARCH to build, defaults to the running system.
73TARGET="$(uname -p)"
74TARGET_ARCH="${TARGET}"
75
76# Set to non-empty value to disable checkout of doc/ and/or ports/.  Disabling
77# ports/ checkout also forces NODOC to be set.
78NODOC=
79NOPORTS=
80
81# Set to non-empty value to build dvd1.iso as part of the release.
82WITH_DVD=
83
84get_rev_branch () {
85	# Set up the OSVERSION, BRANCH, and REVISION based on the src/ tree
86	# checked out.
87	OSVERSION=$(grep '#define __FreeBSD_version' ${CHROOTDIR}/usr/src/sys/sys/param.h | awk '{print $3}')
88	BRANCH=$(grep '^BRANCH=' ${CHROOTDIR}/usr/src/sys/conf/newvers.sh \
89		| awk -F\= '{print $2}' | sed -e 's,",,g')
90	REVISION=$(grep '^REVISION=' ${CHROOTDIR}/usr/src/sys/conf/newvers.sh \
91		| awk -F\= '{print $2}' | sed -e 's,",,g')
92	OSRELEASE="${REVISION}-${BRANCH}"
93}
94
95usage() {
96	echo "Usage: $0 [-c release.conf]"
97	exit 1
98}
99
100while getopts c: opt; do
101	case ${opt} in
102	c)
103		RELEASECONF="${OPTARG}"
104		if [ ! -e "${RELEASECONF}" ]; then
105			echo "ERROR: Configuration file ${RELEASECONF} does not exist."
106			exit 1
107		fi
108		# Source the specified configuration file for overrides
109		. ${RELEASECONF}
110		;;
111	\?)
112		usage
113		;;
114	esac
115done
116shift $(($OPTIND - 1))
117
118# If PORTS is set and NODOC is unset, force NODOC=yes because the ports tree
119# is required to build the documentation set.
120if [ "x${NOPORTS}" != "x" ] && [ "x${NODOC}" = "x" ]; then
121	echo "*** NOTICE: Setting NODOC=1 since ports tree is required"
122	echo "            and NOPORTS is set."
123	NODOC=yes
124fi
125
126# If NOPORTS and/or NODOC are unset, they must not pass to make as variables.
127# The release makefile verifies definedness of NOPORTS/NODOC variables
128# instead of their values.
129DOCPORTS=
130if [ "x${NOPORTS}" != "x" ]; then
131	DOCPORTS="NOPORTS=yes "
132fi
133if [ "x${NODOC}" != "x" ]; then
134	DOCPORTS="${DOCPORTS}NODOC=yes"
135fi
136
137# The aggregated build-time flags based upon variables defined within
138# this file, unless overridden by release.conf.  In most cases, these
139# will not need to be changed.
140CONF_FILES="__MAKE_CONF=${MAKE_CONF} SRCCONF=${SRC_CONF}"
141ARCH_FLAGS="TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH}"
142CHROOT_WMAKEFLAGS="${MAKE_FLAGS} ${WORLD_FLAGS} ${CONF_FILES}"
143CHROOT_IMAKEFLAGS="${CONF_FILES}"
144CHROOT_DMAKEFLAGS="${CONF_FILES}"
145RELEASE_WMAKEFLAGS="${MAKE_FLAGS} ${WORLD_FLAGS} ${ARCH_FLAGS} ${CONF_FILES}"
146RELEASE_KMAKEFLAGS="${MAKE_FLAGS} ${KERNEL_FLAGS} KERNCONF=\"${KERNEL}\" ${ARCH_FLAGS} ${CONF_FILES}"
147RELEASE_RMAKEFLAGS="${ARCH_FLAGS} KERNCONF=\"${KERNEL}\" ${CONF_FILES} \
148	${DOCPORTS} WITH_DVD=${WITH_DVD}"
149
150# Force src checkout if configured
151FORCE_SRC_KEY=
152if [ "x${SRC_FORCE_CHECKOUT}" != "x" ]; then
153	FORCE_SRC_KEY="--force"
154fi
155
156if [ ! ${CHROOTDIR} ]; then
157	echo "Please set CHROOTDIR."
158	exit 1
159fi
160
161if [ $(id -u) -ne 0 ]; then
162	echo "Needs to be run as root."
163	exit 1
164fi
165
166set -e # Everything must succeed
167
168mkdir -p ${CHROOTDIR}/usr
169
170svn co ${FORCE_SRC_KEY} ${SVNROOT}/${SRCBRANCH} ${CHROOTDIR}/usr/src $SRCREVISION
171if [ "x${NODOC}" = "x" ]; then
172	svn co ${SVNROOT}/${DOCBRANCH} ${CHROOTDIR}/usr/doc $DOCREVISION
173fi
174if [ "x${NOPORTS}" = "x" ]; then
175	svn co ${SVNROOT}/${PORTBRANCH} ${CHROOTDIR}/usr/ports $PORTREVISION
176fi
177
178get_rev_branch
179
180cp /etc/resolv.conf ${CHROOTDIR}/etc/resolv.conf
181cd ${CHROOTDIR}/usr/src
182make ${CHROOT_WMAKEFLAGS} buildworld
183make ${CHROOT_IMAKEFLAGS} installworld DESTDIR=${CHROOTDIR}
184make ${CHROOT_DMAKEFLAGS} distribution DESTDIR=${CHROOTDIR}
185mount -t devfs devfs ${CHROOTDIR}/dev
186trap "umount ${CHROOTDIR}/dev" EXIT # Clean up devfs mount on exit
187
188build_doc_ports() {
189	# Run ldconfig(8) in the chroot directory so /var/run/ld-elf*.so.hints
190	# is created.  This is needed by ports-mgmt/pkg.
191	chroot ${CHROOTDIR} /etc/rc.d/ldconfig forcerestart
192
193	## Trick the ports 'run-autotools-fixup' target to do the right thing.
194	_OSVERSION=$(sysctl -n kern.osreldate)
195	if [ -d ${CHROOTDIR}/usr/doc ] && [ "x${NODOC}" = "x" ]; then
196		PBUILD_FLAGS="OSVERSION=${_OSVERSION} BATCH=yes"
197		PBUILD_FLAGS="${PBUILD_FLAGS}"
198		chroot ${CHROOTDIR} make -C /usr/ports/textproc/docproj \
199			${PBUILD_FLAGS} OPTIONS_UNSET="FOP IGOR" install clean distclean
200	fi
201}
202
203# If MAKE_CONF and/or SRC_CONF are set and not character devices (/dev/null),
204# copy them to the chroot.
205if [ -e ${MAKE_CONF} ] && [ ! -c ${MAKE_CONF} ]; then
206	mkdir -p ${CHROOTDIR}/$(dirname ${MAKE_CONF})
207	cp ${MAKE_CONF} ${CHROOTDIR}/${MAKE_CONF}
208fi
209if [ -e ${SRC_CONF} ] && [ ! -c ${SRC_CONF} ]; then
210	mkdir -p ${CHROOTDIR}/$(dirname ${SRC_CONF})
211	cp ${SRC_CONF} ${CHROOTDIR}/${SRC_CONF}
212fi
213
214if [ -d ${CHROOTDIR}/usr/ports ]; then
215	build_doc_ports ${CHROOTDIR}
216fi
217
218if [ "x${RELSTRING}" = "x" ]; then
219	RELSTRING="$(chroot ${CHROOTDIR} uname -s)-${OSRELEASE}-${TARGET_ARCH}"
220fi
221
222eval chroot ${CHROOTDIR} make -C /usr/src ${RELEASE_WMAKEFLAGS} buildworld
223eval chroot ${CHROOTDIR} make -C /usr/src ${RELEASE_KMAKEFLAGS} buildkernel
224eval chroot ${CHROOTDIR} make -C /usr/src/release ${RELEASE_RMAKEFLAGS} \
225	release RELSTRING=${RELSTRING}
226eval chroot ${CHROOTDIR} make -C /usr/src/release ${RELEASE_RMAKEFLAGS} \
227	install DESTDIR=/R RELSTRING=${RELSTRING}
228