1#!/bin/bash
2
3# This script serves two purposes.  First, it can bootstrap the standalone
4# Blueprint to generate the minibp binary.  To do this simply run the script
5# with no arguments from the desired build directory.
6#
7# It can also be invoked from another script to bootstrap a custom Blueprint-
8# based build system.  To do this, the invoking script must first set some or
9# all of the following environment variables, which are documented below where
10# their default values are set:
11#
12#   BOOTSTRAP
13#   WRAPPER
14#   SRCDIR
15#   BLUEPRINTDIR
16#   BUILDDIR
17#   NINJA_BUILDDIR
18#   GOROOT
19#
20# The invoking script should then run this script, passing along all of its
21# command line arguments.
22
23set -e
24
25EXTRA_ARGS=""
26
27# BOOTSTRAP should be set to the path of the bootstrap script.  It can be
28# either an absolute path or one relative to the build directory (which of
29# these is used should probably match what's used for SRCDIR).
30if [ -z "$BOOTSTRAP" ]; then
31    BOOTSTRAP="${BASH_SOURCE[0]}"
32
33    # WRAPPER should only be set if you want a ninja wrapper script to be
34    # installed into the builddir. It is set to blueprint's blueprint.bash
35    # only if BOOTSTRAP and WRAPPER are unset.
36    [ -z "$WRAPPER" ] && WRAPPER="`dirname "${BOOTSTRAP}"`/blueprint.bash"
37fi
38
39# SRCDIR should be set to the path of the root source directory.  It can be
40# either an absolute path or a path relative to the build directory.  Whether
41# its an absolute or relative path determines whether the build directory can
42# be moved relative to or along with the source directory without re-running
43# the bootstrap script.
44[ -z "$SRCDIR" ] && SRCDIR=`dirname "${BOOTSTRAP}"`
45
46# BLUEPRINTDIR should be set to the path to the blueprint source. It generally
47# should start with SRCDIR.
48[ -z "$BLUEPRINTDIR" ] && BLUEPRINTDIR="${SRCDIR}"
49
50# BUILDDIR should be set to the path to store build results. By default, this
51# is the current directory, but it may be set to an absolute or relative path.
52[ -z "$BUILDDIR" ] && BUILDDIR=.
53
54# NINJA_BUILDDIR should be set to the path to store the .ninja_log/.ninja_deps
55# files. By default this is the same as $BUILDDIR.
56[ -z "$NINJA_BUILDDIR" ] && NINJA_BUILDDIR="${BUILDDIR}"
57
58# TOPNAME should be set to the name of the top-level Blueprints file
59[ -z "$TOPNAME" ] && TOPNAME="Blueprints"
60
61# These variables should be set by auto-detecting or knowing a priori the host
62# Go toolchain properties.
63[ -z "$GOROOT" ] && GOROOT=`go env GOROOT`
64
65usage() {
66    echo "Usage of ${BOOTSTRAP}:"
67    echo "  -h: print a help message and exit"
68    echo "  -b <builddir>: set the build directory"
69    echo "  -t: run tests"
70    echo "  -n: use validations to depend on tests"
71}
72
73# Parse the command line flags.
74while getopts ":b:hnt" opt; do
75    case $opt in
76        b) BUILDDIR="$OPTARG";;
77        n) USE_VALIDATIONS=true;;
78        t) RUN_TESTS=true;;
79        h)
80            usage
81            exit 1
82            ;;
83        \?)
84            echo "Invalid option: -$OPTARG" >&2
85            usage
86            exit 1
87            ;;
88        :)
89            echo "Option -$OPTARG requires an argument." >&2
90            exit 1
91            ;;
92    esac
93done
94
95# If RUN_TESTS is set, behave like -t was passed in as an option.
96[ ! -z "$RUN_TESTS" ] && EXTRA_ARGS="${EXTRA_ARGS} -t"
97
98# If $USE_VALIDATIONS is set, pass --use-validations.
99[ ! -z "$USE_VALIDATIONS" ] && EXTRA_ARGS="${EXTRA_ARGS} --use-validations"
100
101# If EMPTY_NINJA_FILE is set, have the primary build write out a 0-byte ninja
102# file instead of a full length one. Useful if you don't plan on executing the
103# build, but want to verify the primary builder execution.
104[ ! -z "$EMPTY_NINJA_FILE" ] && EXTRA_ARGS="${EXTRA_ARGS} --empty-ninja-file"
105
106# Allow the caller to pass in a list of module files
107if [ -z "${BLUEPRINT_LIST_FILE}" ]; then
108  BLUEPRINT_LIST_FILE="${BUILDDIR}/.bootstrap/bplist"
109fi
110EXTRA_ARGS="${EXTRA_ARGS} -l ${BLUEPRINT_LIST_FILE}"
111
112mkdir -p $BUILDDIR/.minibootstrap
113
114echo "bootstrapBuildDir = $BUILDDIR" > $BUILDDIR/.minibootstrap/build.ninja
115echo "topFile = $SRCDIR/$TOPNAME" >> $BUILDDIR/.minibootstrap/build.ninja
116echo "extraArgs = $EXTRA_ARGS" >> $BUILDDIR/.minibootstrap/build.ninja
117echo "builddir = $NINJA_BUILDDIR" >> $BUILDDIR/.minibootstrap/build.ninja
118echo "include $BLUEPRINTDIR/bootstrap/build.ninja" >> $BUILDDIR/.minibootstrap/build.ninja
119
120if [ ! -f "$BUILDDIR/.minibootstrap/build-globs.ninja" ]; then
121    touch "$BUILDDIR/.minibootstrap/build-globs.ninja"
122fi
123
124echo "BLUEPRINT_BOOTSTRAP_VERSION=2" > $BUILDDIR/.blueprint.bootstrap
125echo "SRCDIR=\"${SRCDIR}\"" >> $BUILDDIR/.blueprint.bootstrap
126echo "BLUEPRINTDIR=\"${BLUEPRINTDIR}\"" >> $BUILDDIR/.blueprint.bootstrap
127echo "NINJA_BUILDDIR=\"${NINJA_BUILDDIR}\"" >> $BUILDDIR/.blueprint.bootstrap
128echo "GOROOT=\"${GOROOT}\"" >> $BUILDDIR/.blueprint.bootstrap
129echo "TOPNAME=\"${TOPNAME}\"" >> $BUILDDIR/.blueprint.bootstrap
130
131touch "${BUILDDIR}/.out-dir"
132
133if [ ! -z "$WRAPPER" ]; then
134    cp $WRAPPER $BUILDDIR/
135fi
136