1#!/bin/sh
2#-
3# Copyright (c) 2011 Nathan Whitehorn
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD$
28#
29
30# generate-release.sh: check out source trees, and build release components with
31#  totally clean, fresh trees.
32#
33#  Usage: generate-release.sh svn-branch[@revision] scratch-dir
34#
35# Environment variables:
36#  SVNROOTBASE: SVN base URL to FreeBSD repository (svn://svn.freebsd.org)
37#  SVNROOTSRC:  URL to FreeBSD src tree (${SVNROOTBASE}/base)
38#  SVNROOTDOC:  URL to FreeBSD doc tree (${SVNROOTBASE}/doc)
39#  SVNROOTPORTS:URL to FreeBSD ports tree (${SVNROOTBASE}/ports)
40#  BRANCHSRC:   branch name of src (svn-branch[@revision])
41#  BRANCHDOC:   branch name of doc (head)
42#  BRANCHPORTS: branch name of ports (head)
43#  WORLD_FLAGS: optional flags to pass to buildworld (e.g. -j)
44#  KERNEL_FLAGS: optional flags to pass to buildkernel (e.g. -j)
45#
46
47usage()
48{
49	echo "Usage: $0 svn-branch[@revision] scratch-dir" 2>&1
50	exit 1
51}
52
53if [ $# -lt 2 ]; then
54	usage
55fi
56
57: ${SVNROOTBASE:=svn://svn.freebsd.org}
58: ${SVNROOTSRC:=${SVNROOTBASE}/base}
59: ${SVNROOTDOC:=${SVNROOTBASE}/doc}
60: ${SVNROOTPORTS:=${SVNROOTBASE}/ports}
61: ${SVNROOT:=${SVNROOTSRC}} # for backward compatibility
62: ${SVN_CMD:=/usr/local/bin/svn}
63BRANCHSRC=$1
64: ${BRANCHDOC:=head}
65: ${BRANCHPORTS:=head}
66: ${WORLD_FLAGS:=${MAKE_FLAGS}}
67: ${KERNEL_FLAGS:=${MAKE_FLAGS}}
68: ${CHROOTDIR:=$2}
69 
70if [ ! -r "${CHROOTDIR}" ]; then
71	echo "${CHROOTDIR}: scratch dir not found."
72	exit 1
73fi
74
75CHROOT_CMD="/usr/sbin/chroot ${CHROOTDIR}"
76case ${TARGET} in
77"")	;;
78*)	SETENV_TARGET="TARGET=$TARGET" ;;
79esac
80case ${TARGET_ARCH} in
81"")	;;
82*)	SETENV_TARGET_ARCH="TARGET_ARCH=$TARGET_ARCH" ;;
83esac
84SETENV="env -i PATH=/bin:/usr/bin:/sbin:/usr/sbin"
85CROSSENV="${SETENV_TARGET} ${SETENV_TARGET_ARCH}"
86WMAKE="make -C /usr/src ${WORLD_FLAGS}"
87NWMAKE="${WMAKE} __MAKE_CONF=/dev/null SRCCONF=/dev/null"
88KMAKE="make -C /usr/src ${KERNEL_FLAGS}"
89RMAKE="make -C /usr/src/release"
90
91if [ $(id -u) -ne 0 ]; then
92	echo "Needs to be run as root."
93	exit 1
94fi
95
96set -e # Everything must succeed
97
98mkdir -p ${CHROOTDIR}/usr/src
99${SVN_CMD} co ${SVNROOT}/${BRANCHSRC} ${CHROOTDIR}/usr/src
100${SVN_CMD} co ${SVNROOTDOC}/${BRANCHDOC} ${CHROOTDIR}/usr/doc
101${SVN_CMD} co ${SVNROOTPORTS}/${BRANCHPORTS} ${CHROOTDIR}/usr/ports
102
103${SETENV} ${NWMAKE} -C ${CHROOTDIR}/usr/src ${WORLD_FLAGS} buildworld
104${SETENV} ${NWMAKE} -C ${CHROOTDIR}/usr/src installworld distribution DESTDIR=${CHROOTDIR}
105mount -t devfs devfs ${CHROOTDIR}/dev
106trap "umount ${CHROOTDIR}/dev" EXIT # Clean up devfs mount on exit
107
108if [ -d ${CHROOTDIR}/usr/doc ]; then 
109	cp /etc/resolv.conf ${CHROOTDIR}/etc/resolv.conf
110
111	# Install docproj to build release documentation
112	${CHROOT_CMD} /bin/sh -c \
113		'make -C /usr/ports/textproc/docproj \
114			BATCH=yes \
115			WITHOUT_SVN=yes \
116			WITHOUT_JADETEX=yes \
117			WITHOUT_X11=yes \
118			WITHOUT_PYTHON=yes \
119			install'
120fi
121
122${CHROOT_CMD} ${SETENV} ${CROSSENV} ${WMAKE} buildworld
123${CHROOT_CMD} ${SETENV} ${CROSSENV} ${KMAKE} buildkernel
124${CHROOT_CMD} ${SETENV} ${CROSSENV} ${RMAKE} release
125${CHROOT_CMD} ${SETENV} ${CROSSENV} ${RMAKE} install DESTDIR=/R
126