blob: 799bd201e93307fb106f16d8d6599779626e8fa6 [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
25# BOOTSTRAP should be set to the path of the bootstrap script. It can be
26# either an absolute path or one relative to the build directory (which of
27# these is used should probably match what's used for SRCDIR).
28[ -z "$BOOTSTRAP" ] && BOOTSTRAP="${BASH_SOURCE[0]}"
29
Jamie Gennis1bc967e2014-05-27 16:34:41 -070030# SRCDIR should be set to the path of the root source directory. It can be
31# either an absolute path or a path relative to the build directory. Whether
Jamie Gennis7330a232014-06-13 16:25:09 -070032# its an absolute or relative path determines whether the build directory can
33# be moved relative to or along with the source directory without re-running
34# the bootstrap script.
35[ -z "$SRCDIR" ] && SRCDIR=`dirname "${BOOTSTRAP}"`
Jamie Gennis1bc967e2014-05-27 16:34:41 -070036
Jamie Gennis7330a232014-06-13 16:25:09 -070037# BOOTSTRAP_MANIFEST is the path to the bootstrap Ninja file that is part of
38# the source tree. It is used to bootstrap a build output directory from when
39# the script is run manually by a user.
40[ -z "$BOOTSTRAP_MANIFEST" ] && BOOTSTRAP_MANIFEST="${SRCDIR}/build.ninja.in"
Jamie Gennis1bc967e2014-05-27 16:34:41 -070041
Jamie Gennis7330a232014-06-13 16:25:09 -070042# These variables should be set by auto-detecting or knowing a priori the host
43# Go toolchain properties.
44[ -z "$GOROOT" ] && GOROOT=`go env GOROOT`
45[ -z "$GOOS" ] && GOOS=`go env GOHOSTOS`
46[ -z "$GOARCH" ] && GOARCH=`go env GOHOSTARCH`
47[ -z "$GOCHAR" ] && GOCHAR=`go env GOCHAR`
Jamie Gennis1bc967e2014-05-27 16:34:41 -070048
Jamie Gennis7330a232014-06-13 16:25:09 -070049usage() {
50 echo "Usage of ${BOOTSTRAP}:"
51 echo " -h: print a help message and exit"
52 echo " -r: regenerate ${BOOTSTRAP_MANIFEST}"
53}
54
55# Parse the command line flags.
56IN="$BOOTSTRAP_MANIFEST"
57REGEN_BOOTSTRAP_MANIFEST=false
58while getopts ":hi:r" opt; do
59 case $opt in
60 h)
61 usage
62 exit 1
63 ;;
64 i) IN="$OPTARG";;
65 r) REGEN_BOOTSTRAP_MANIFEST=true;;
66 \?)
67 echo "Invalid option: -$OPTARG" >&2
68 usage
69 exit 1
70 ;;
71 :)
72 echo "Option -$OPTARG requires an argument." >&2
73 exit 1
74 ;;
75 esac
76done
77
78if [ $REGEN_BOOTSTRAP_MANIFEST = true ]; then
79 # This assumes that the script is being run from a build output directory
80 # that has been built in the past.
81 if [ -x .bootstrap/bin/minibp ]; then
82 echo "Regenerating $BOOTSTRAP_MANIFEST"
83 ./.bootstrap/bin/minibp -o $BOOTSTRAP_MANIFEST $SRCDIR/Blueprints
84 else
85 echo "Executable minibp not found at .bootstrap/bin/minibp" >&2
86 exit 1
87 fi
88fi
Jamie Gennis1bc967e2014-05-27 16:34:41 -070089
90sed -e "s|@@SrcDir@@|$SRCDIR|g" \
91 -e "s|@@GoRoot@@|$GOROOT|g" \
92 -e "s|@@GoOS@@|$GOOS|g" \
93 -e "s|@@GoArch@@|$GOARCH|g" \
94 -e "s|@@GoChar@@|$GOCHAR|g" \
95 -e "s|@@Bootstrap@@|$BOOTSTRAP|g" \
96 -e "s|@@BootstrapManifest@@|$BOOTSTRAP_MANIFEST|g" \
97 $IN > build.ninja