blob: ee5ddb72304aea1bd3f92140a415332cce1d0939 [file] [log] [blame]
Jamie Gennis1bc967e2014-05-27 16:34:41 -07001#!/bin/bash
2
Jamie Gennis7330a232014-06-13 16:25:09 -07003# 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# SRCDIR
14# BOOTSTRAP_MANIFEST
15# GOROOT
16# GOOS
17# GOARCH
18# GOCHAR
19#
20# The invoking script should then run this script, passing along all of its
21# command line arguments.
22
23set -e
24
Dan Willemsen87ba2942015-06-23 17:21:00 -070025EXTRA_ARGS=""
26
Jamie Gennis7330a232014-06-13 16:25:09 -070027# 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).
30[ -z "$BOOTSTRAP" ] && BOOTSTRAP="${BASH_SOURCE[0]}"
31
Jamie Gennis1bc967e2014-05-27 16:34:41 -070032# SRCDIR should be set to the path of the root source directory. It can be
33# either an absolute path or a path relative to the build directory. Whether
Jamie Gennis7330a232014-06-13 16:25:09 -070034# its an absolute or relative path determines whether the build directory can
35# be moved relative to or along with the source directory without re-running
36# the bootstrap script.
37[ -z "$SRCDIR" ] && SRCDIR=`dirname "${BOOTSTRAP}"`
Jamie Gennis1bc967e2014-05-27 16:34:41 -070038
Dan Willemsen2527cc62015-06-10 16:55:02 -070039# TOPNAME should be set to the name of the top-level Blueprints file
40[ -z "$TOPNAME" ] && TOPNAME="Blueprints"
41
Jamie Gennis7330a232014-06-13 16:25:09 -070042# BOOTSTRAP_MANIFEST is the path to the bootstrap Ninja file that is part of
43# the source tree. It is used to bootstrap a build output directory from when
44# the script is run manually by a user.
45[ -z "$BOOTSTRAP_MANIFEST" ] && BOOTSTRAP_MANIFEST="${SRCDIR}/build.ninja.in"
Jamie Gennis1bc967e2014-05-27 16:34:41 -070046
Jamie Gennis7330a232014-06-13 16:25:09 -070047# These variables should be set by auto-detecting or knowing a priori the host
48# Go toolchain properties.
49[ -z "$GOROOT" ] && GOROOT=`go env GOROOT`
50[ -z "$GOOS" ] && GOOS=`go env GOHOSTOS`
51[ -z "$GOARCH" ] && GOARCH=`go env GOHOSTARCH`
52[ -z "$GOCHAR" ] && GOCHAR=`go env GOCHAR`
Jamie Gennis1bc967e2014-05-27 16:34:41 -070053
Dan Willemsen87ba2942015-06-23 17:21:00 -070054# If RUN_TESTS is set, behave like -t was passed in as an option.
55[ ! -z "$RUN_TESTS" ] && EXTRA_ARGS="$EXTRA_ARGS -t"
56
Jamie Gennis7330a232014-06-13 16:25:09 -070057usage() {
58 echo "Usage of ${BOOTSTRAP}:"
59 echo " -h: print a help message and exit"
60 echo " -r: regenerate ${BOOTSTRAP_MANIFEST}"
Dan Willemsen87ba2942015-06-23 17:21:00 -070061 echo " -t: include tests when regenerating manifest"
Jamie Gennis7330a232014-06-13 16:25:09 -070062}
63
64# Parse the command line flags.
65IN="$BOOTSTRAP_MANIFEST"
66REGEN_BOOTSTRAP_MANIFEST=false
Dan Willemsen87ba2942015-06-23 17:21:00 -070067while getopts ":hi:rt" opt; do
Jamie Gennis7330a232014-06-13 16:25:09 -070068 case $opt in
69 h)
70 usage
71 exit 1
72 ;;
73 i) IN="$OPTARG";;
74 r) REGEN_BOOTSTRAP_MANIFEST=true;;
Dan Willemsen87ba2942015-06-23 17:21:00 -070075 t) EXTRA_ARGS="$EXTRA_ARGS -t";;
Jamie Gennis7330a232014-06-13 16:25:09 -070076 \?)
77 echo "Invalid option: -$OPTARG" >&2
78 usage
79 exit 1
80 ;;
81 :)
82 echo "Option -$OPTARG requires an argument." >&2
83 exit 1
84 ;;
85 esac
86done
87
88if [ $REGEN_BOOTSTRAP_MANIFEST = true ]; then
89 # This assumes that the script is being run from a build output directory
90 # that has been built in the past.
91 if [ -x .bootstrap/bin/minibp ]; then
92 echo "Regenerating $BOOTSTRAP_MANIFEST"
Dan Willemsen87ba2942015-06-23 17:21:00 -070093 ./.bootstrap/bin/minibp $EXTRA_ARGS -o $BOOTSTRAP_MANIFEST $SRCDIR/$TOPNAME
Jamie Gennis7330a232014-06-13 16:25:09 -070094 else
95 echo "Executable minibp not found at .bootstrap/bin/minibp" >&2
96 exit 1
97 fi
98fi
Jamie Gennis1bc967e2014-05-27 16:34:41 -070099
100sed -e "s|@@SrcDir@@|$SRCDIR|g" \
101 -e "s|@@GoRoot@@|$GOROOT|g" \
102 -e "s|@@GoOS@@|$GOOS|g" \
103 -e "s|@@GoArch@@|$GOARCH|g" \
104 -e "s|@@GoChar@@|$GOCHAR|g" \
105 -e "s|@@Bootstrap@@|$BOOTSTRAP|g" \
106 -e "s|@@BootstrapManifest@@|$BOOTSTRAP_MANIFEST|g" \
Dan Willemsen87ba2942015-06-23 17:21:00 -0700107 $IN > build.ninja