blob: ff60e212ba4235faf5cc9c260b93da1e41846e73 [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
Dan Willemsenf0ca9012015-07-13 18:11:49 -070014# BUILDDIR
Jamie Gennis7330a232014-06-13 16:25:09 -070015# BOOTSTRAP_MANIFEST
16# GOROOT
17# GOOS
18# GOARCH
19# GOCHAR
20#
21# The invoking script should then run this script, passing along all of its
22# command line arguments.
23
24set -e
25
Dan Willemsen87ba2942015-06-23 17:21:00 -070026EXTRA_ARGS=""
27
Jamie Gennis7330a232014-06-13 16:25:09 -070028# BOOTSTRAP should be set to the path of the bootstrap script. It can be
29# either an absolute path or one relative to the build directory (which of
30# these is used should probably match what's used for SRCDIR).
31[ -z "$BOOTSTRAP" ] && BOOTSTRAP="${BASH_SOURCE[0]}"
32
Jamie Gennis1bc967e2014-05-27 16:34:41 -070033# SRCDIR should be set to the path of the root source directory. It can be
34# either an absolute path or a path relative to the build directory. Whether
Jamie Gennis7330a232014-06-13 16:25:09 -070035# its an absolute or relative path determines whether the build directory can
36# be moved relative to or along with the source directory without re-running
37# the bootstrap script.
38[ -z "$SRCDIR" ] && SRCDIR=`dirname "${BOOTSTRAP}"`
Jamie Gennis1bc967e2014-05-27 16:34:41 -070039
Dan Willemsenf0ca9012015-07-13 18:11:49 -070040# BUILDDIR should be set to the path to store build results. By default, this
41# is the current directory, but it may be set to an absolute or relative path.
42[ -z "$BUILDDIR" ] && BUILDDIR=.
43
Dan Willemsen2527cc62015-06-10 16:55:02 -070044# TOPNAME should be set to the name of the top-level Blueprints file
45[ -z "$TOPNAME" ] && TOPNAME="Blueprints"
46
Jamie Gennis7330a232014-06-13 16:25:09 -070047# BOOTSTRAP_MANIFEST is the path to the bootstrap Ninja file that is part of
48# the source tree. It is used to bootstrap a build output directory from when
49# the script is run manually by a user.
50[ -z "$BOOTSTRAP_MANIFEST" ] && BOOTSTRAP_MANIFEST="${SRCDIR}/build.ninja.in"
Jamie Gennis1bc967e2014-05-27 16:34:41 -070051
Jamie Gennis7330a232014-06-13 16:25:09 -070052# These variables should be set by auto-detecting or knowing a priori the host
53# Go toolchain properties.
54[ -z "$GOROOT" ] && GOROOT=`go env GOROOT`
55[ -z "$GOOS" ] && GOOS=`go env GOHOSTOS`
56[ -z "$GOARCH" ] && GOARCH=`go env GOHOSTARCH`
57[ -z "$GOCHAR" ] && GOCHAR=`go env GOCHAR`
Jamie Gennis1bc967e2014-05-27 16:34:41 -070058
Dan Willemsen87ba2942015-06-23 17:21:00 -070059# If RUN_TESTS is set, behave like -t was passed in as an option.
60[ ! -z "$RUN_TESTS" ] && EXTRA_ARGS="$EXTRA_ARGS -t"
61
Dan Willemsenc20adea2015-08-01 15:07:27 -070062GOTOOLDIR="$GOROOT/pkg/tool/${GOOS}_$GOARCH"
63GOCOMPILE="$GOTOOLDIR/${GOCHAR}g"
64GOLINK="$GOTOOLDIR/${GOCHAR}l"
65
66if [ ! -f $GOCOMPILE ]; then
67 GOCOMPILE="$GOTOOLDIR/compile"
68fi
69if [ ! -f $GOLINK ]; then
70 GOLINK="$GOTOOLDIR/link"
71fi
72if [[ ! -f $GOCOMPILE || ! -f $GOLINK ]]; then
73 echo "Cannot find go tools under $GOROOT"
74 exit 1
75fi
76
Jamie Gennis7330a232014-06-13 16:25:09 -070077usage() {
78 echo "Usage of ${BOOTSTRAP}:"
79 echo " -h: print a help message and exit"
80 echo " -r: regenerate ${BOOTSTRAP_MANIFEST}"
Dan Willemsen87ba2942015-06-23 17:21:00 -070081 echo " -t: include tests when regenerating manifest"
Jamie Gennis7330a232014-06-13 16:25:09 -070082}
83
84# Parse the command line flags.
85IN="$BOOTSTRAP_MANIFEST"
86REGEN_BOOTSTRAP_MANIFEST=false
Dan Willemsenf0ca9012015-07-13 18:11:49 -070087while getopts ":b:hi:rt" opt; do
Jamie Gennis7330a232014-06-13 16:25:09 -070088 case $opt in
Dan Willemsenf0ca9012015-07-13 18:11:49 -070089 b) BUILDDIR="$OPTARG";;
Jamie Gennis7330a232014-06-13 16:25:09 -070090 h)
91 usage
92 exit 1
93 ;;
94 i) IN="$OPTARG";;
95 r) REGEN_BOOTSTRAP_MANIFEST=true;;
Dan Willemsen87ba2942015-06-23 17:21:00 -070096 t) EXTRA_ARGS="$EXTRA_ARGS -t";;
Jamie Gennis7330a232014-06-13 16:25:09 -070097 \?)
98 echo "Invalid option: -$OPTARG" >&2
99 usage
100 exit 1
101 ;;
102 :)
103 echo "Option -$OPTARG requires an argument." >&2
104 exit 1
105 ;;
106 esac
107done
108
109if [ $REGEN_BOOTSTRAP_MANIFEST = true ]; then
110 # This assumes that the script is being run from a build output directory
111 # that has been built in the past.
Dan Willemsenf0ca9012015-07-13 18:11:49 -0700112 if [ -x $BUILDDIR/.bootstrap/bin/minibp ]; then
Jamie Gennis7330a232014-06-13 16:25:09 -0700113 echo "Regenerating $BOOTSTRAP_MANIFEST"
Dan Willemsenf0ca9012015-07-13 18:11:49 -0700114 $BUILDDIR/.bootstrap/bin/minibp $EXTRA_ARGS -o $BOOTSTRAP_MANIFEST $SRCDIR/$TOPNAME
Jamie Gennis7330a232014-06-13 16:25:09 -0700115 else
Dan Willemsenf0ca9012015-07-13 18:11:49 -0700116 echo "Executable minibp not found at $BUILDDIR/.bootstrap/bin/minibp" >&2
Jamie Gennis7330a232014-06-13 16:25:09 -0700117 exit 1
118 fi
119fi
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700120
Dan Willemsenf0ca9012015-07-13 18:11:49 -0700121mkdir -p $BUILDDIR
122
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700123sed -e "s|@@SrcDir@@|$SRCDIR|g" \
Dan Willemsenf0ca9012015-07-13 18:11:49 -0700124 -e "s|@@BuildDir@@|$BUILDDIR|g" \
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700125 -e "s|@@GoRoot@@|$GOROOT|g" \
Dan Willemsenc20adea2015-08-01 15:07:27 -0700126 -e "s|@@GoCompile@@|$GOCOMPILE|g" \
127 -e "s|@@GoLink@@|$GOLINK|g" \
Jamie Gennis1bc967e2014-05-27 16:34:41 -0700128 -e "s|@@Bootstrap@@|$BOOTSTRAP|g" \
129 -e "s|@@BootstrapManifest@@|$BOOTSTRAP_MANIFEST|g" \
Dan Willemsenf0ca9012015-07-13 18:11:49 -0700130 $IN > $BUILDDIR/build.ninja