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}
71
72# Parse the command line flags.
73while getopts ":b:ht" opt; do
74    case $opt in
75        b) BUILDDIR="$OPTARG";;
76        t) RUN_TESTS=true;;
77        h)
78            usage
79            exit 1
80            ;;
81        \?)
82            echo "Invalid option: -$OPTARG" >&2
83            usage
84            exit 1
85            ;;
86        :)
87            echo "Option -$OPTARG requires an argument." >&2
88            exit 1
89            ;;
90    esac
91done
92
93# If RUN_TESTS is set, behave like -t was passed in as an option.
94[ ! -z "$RUN_TESTS" ] && EXTRA_ARGS="${EXTRA_ARGS} -t"
95
96# If EMPTY_NINJA_FILE is set, have the primary build write out a 0-byte ninja
97# file instead of a full length one. Useful if you don't plan on executing the
98# build, but want to verify the primary builder execution.
99[ ! -z "$EMPTY_NINJA_FILE" ] && EXTRA_ARGS="${EXTRA_ARGS} --empty-ninja-file"
100
101# Allow the caller to pass in a list of module files
102if [ -z "${BLUEPRINT_LIST_FILE}" ]; then
103  BLUEPRINT_LIST_FILE="${BUILDDIR}/.bootstrap/bplist"
104fi
105EXTRA_ARGS="${EXTRA_ARGS} -l ${BLUEPRINT_LIST_FILE}"
106
107mkdir -p $BUILDDIR/.minibootstrap
108
109echo "bootstrapBuildDir = $BUILDDIR" > $BUILDDIR/.minibootstrap/build.ninja
110echo "topFile = $SRCDIR/$TOPNAME" >> $BUILDDIR/.minibootstrap/build.ninja
111echo "extraArgs = $EXTRA_ARGS" >> $BUILDDIR/.minibootstrap/build.ninja
112echo "builddir = $NINJA_BUILDDIR" >> $BUILDDIR/.minibootstrap/build.ninja
113echo "include $BLUEPRINTDIR/bootstrap/build.ninja" >> $BUILDDIR/.minibootstrap/build.ninja
114
115if [ ! -f "$BUILDDIR/.minibootstrap/build-globs.ninja" ]; then
116    touch "$BUILDDIR/.minibootstrap/build-globs.ninja"
117fi
118
119echo "BLUEPRINT_BOOTSTRAP_VERSION=2" > $BUILDDIR/.blueprint.bootstrap
120echo "SRCDIR=\"${SRCDIR}\"" >> $BUILDDIR/.blueprint.bootstrap
121echo "BLUEPRINTDIR=\"${BLUEPRINTDIR}\"" >> $BUILDDIR/.blueprint.bootstrap
122echo "NINJA_BUILDDIR=\"${NINJA_BUILDDIR}\"" >> $BUILDDIR/.blueprint.bootstrap
123echo "GOROOT=\"${GOROOT}\"" >> $BUILDDIR/.blueprint.bootstrap
124echo "TOPNAME=\"${TOPNAME}\"" >> $BUILDDIR/.blueprint.bootstrap
125
126touch "${BUILDDIR}/.out-dir"
127
128if [ ! -z "$WRAPPER" ]; then
129    cp $WRAPPER $BUILDDIR/
130fi
131