Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 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 | # SRCDIR |
Dan Willemsen | f0ca901 | 2015-07-13 18:11:49 -0700 | [diff] [blame] | 14 | # BUILDDIR |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 15 | # 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 | |
| 24 | set -e |
| 25 | |
Dan Willemsen | 87ba294 | 2015-06-23 17:21:00 -0700 | [diff] [blame] | 26 | EXTRA_ARGS="" |
| 27 | |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 28 | # 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 Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 33 | # 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 Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 35 | # 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 Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 39 | |
Dan Willemsen | f0ca901 | 2015-07-13 18:11:49 -0700 | [diff] [blame] | 40 | # 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 Willemsen | 2527cc6 | 2015-06-10 16:55:02 -0700 | [diff] [blame] | 44 | # TOPNAME should be set to the name of the top-level Blueprints file |
| 45 | [ -z "$TOPNAME" ] && TOPNAME="Blueprints" |
| 46 | |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 47 | # 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 Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 51 | |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 52 | # 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 Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 58 | |
Dan Willemsen | 87ba294 | 2015-06-23 17:21:00 -0700 | [diff] [blame] | 59 | # 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 Willemsen | c20adea | 2015-08-01 15:07:27 -0700 | [diff] [blame] | 62 | GOTOOLDIR="$GOROOT/pkg/tool/${GOOS}_$GOARCH" |
| 63 | GOCOMPILE="$GOTOOLDIR/${GOCHAR}g" |
| 64 | GOLINK="$GOTOOLDIR/${GOCHAR}l" |
| 65 | |
| 66 | if [ ! -f $GOCOMPILE ]; then |
| 67 | GOCOMPILE="$GOTOOLDIR/compile" |
| 68 | fi |
| 69 | if [ ! -f $GOLINK ]; then |
| 70 | GOLINK="$GOTOOLDIR/link" |
| 71 | fi |
| 72 | if [[ ! -f $GOCOMPILE || ! -f $GOLINK ]]; then |
| 73 | echo "Cannot find go tools under $GOROOT" |
| 74 | exit 1 |
| 75 | fi |
| 76 | |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 77 | usage() { |
| 78 | echo "Usage of ${BOOTSTRAP}:" |
| 79 | echo " -h: print a help message and exit" |
| 80 | echo " -r: regenerate ${BOOTSTRAP_MANIFEST}" |
Dan Willemsen | 87ba294 | 2015-06-23 17:21:00 -0700 | [diff] [blame] | 81 | echo " -t: include tests when regenerating manifest" |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | # Parse the command line flags. |
| 85 | IN="$BOOTSTRAP_MANIFEST" |
| 86 | REGEN_BOOTSTRAP_MANIFEST=false |
Dan Willemsen | f0ca901 | 2015-07-13 18:11:49 -0700 | [diff] [blame] | 87 | while getopts ":b:hi:rt" opt; do |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 88 | case $opt in |
Dan Willemsen | f0ca901 | 2015-07-13 18:11:49 -0700 | [diff] [blame] | 89 | b) BUILDDIR="$OPTARG";; |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 90 | h) |
| 91 | usage |
| 92 | exit 1 |
| 93 | ;; |
| 94 | i) IN="$OPTARG";; |
| 95 | r) REGEN_BOOTSTRAP_MANIFEST=true;; |
Dan Willemsen | 87ba294 | 2015-06-23 17:21:00 -0700 | [diff] [blame] | 96 | t) EXTRA_ARGS="$EXTRA_ARGS -t";; |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 97 | \?) |
| 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 |
| 107 | done |
| 108 | |
| 109 | if [ $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 Willemsen | f0ca901 | 2015-07-13 18:11:49 -0700 | [diff] [blame] | 112 | if [ -x $BUILDDIR/.bootstrap/bin/minibp ]; then |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 113 | echo "Regenerating $BOOTSTRAP_MANIFEST" |
Dan Willemsen | f0ca901 | 2015-07-13 18:11:49 -0700 | [diff] [blame] | 114 | $BUILDDIR/.bootstrap/bin/minibp $EXTRA_ARGS -o $BOOTSTRAP_MANIFEST $SRCDIR/$TOPNAME |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 115 | else |
Dan Willemsen | f0ca901 | 2015-07-13 18:11:49 -0700 | [diff] [blame] | 116 | echo "Executable minibp not found at $BUILDDIR/.bootstrap/bin/minibp" >&2 |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 117 | exit 1 |
| 118 | fi |
| 119 | fi |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 120 | |
Dan Willemsen | f0ca901 | 2015-07-13 18:11:49 -0700 | [diff] [blame] | 121 | mkdir -p $BUILDDIR |
| 122 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 123 | sed -e "s|@@SrcDir@@|$SRCDIR|g" \ |
Dan Willemsen | f0ca901 | 2015-07-13 18:11:49 -0700 | [diff] [blame] | 124 | -e "s|@@BuildDir@@|$BUILDDIR|g" \ |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 125 | -e "s|@@GoRoot@@|$GOROOT|g" \ |
Dan Willemsen | c20adea | 2015-08-01 15:07:27 -0700 | [diff] [blame] | 126 | -e "s|@@GoCompile@@|$GOCOMPILE|g" \ |
| 127 | -e "s|@@GoLink@@|$GOLINK|g" \ |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 128 | -e "s|@@Bootstrap@@|$BOOTSTRAP|g" \ |
| 129 | -e "s|@@BootstrapManifest@@|$BOOTSTRAP_MANIFEST|g" \ |
Dan Willemsen | f0ca901 | 2015-07-13 18:11:49 -0700 | [diff] [blame] | 130 | $IN > $BUILDDIR/build.ninja |