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 |
Dan Willemsen | d79f1af | 2015-12-09 18:03:13 -0800 | [diff] [blame] | 13 | # WRAPPER |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 14 | # SRCDIR |
Dan Willemsen | 1e72321 | 2017-07-18 19:37:37 -0700 | [diff] [blame] | 15 | # BLUEPRINTDIR |
Dan Willemsen | f0ca901 | 2015-07-13 18:11:49 -0700 | [diff] [blame] | 16 | # BUILDDIR |
Dan Willemsen | cd4e0ce | 2017-07-19 22:43:30 -0700 | [diff] [blame] | 17 | # NINJA_BUILDDIR |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 18 | # GOROOT |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 19 | # |
| 20 | # The invoking script should then run this script, passing along all of its |
| 21 | # command line arguments. |
| 22 | |
| 23 | set -e |
| 24 | |
Dan Willemsen | 87ba294 | 2015-06-23 17:21:00 -0700 | [diff] [blame] | 25 | EXTRA_ARGS="" |
| 26 | |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 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). |
Dan Willemsen | d79f1af | 2015-12-09 18:03:13 -0800 | [diff] [blame] | 30 | if [ -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" |
| 37 | fi |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 38 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 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 |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 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}"` |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 45 | |
Dan Willemsen | 1e72321 | 2017-07-18 19:37:37 -0700 | [diff] [blame] | 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 | |
Dan Willemsen | f0ca901 | 2015-07-13 18:11:49 -0700 | [diff] [blame] | 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 | |
Dan Willemsen | cd4e0ce | 2017-07-19 22:43:30 -0700 | [diff] [blame] | 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 | |
Dan Willemsen | 2527cc6 | 2015-06-10 16:55:02 -0700 | [diff] [blame] | 58 | # TOPNAME should be set to the name of the top-level Blueprints file |
| 59 | [ -z "$TOPNAME" ] && TOPNAME="Blueprints" |
| 60 | |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 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` |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 64 | |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 65 | usage() { |
| 66 | echo "Usage of ${BOOTSTRAP}:" |
| 67 | echo " -h: print a help message and exit" |
Dan Willemsen | 1e72321 | 2017-07-18 19:37:37 -0700 | [diff] [blame] | 68 | echo " -b <builddir>: set the build directory" |
| 69 | echo " -t: run tests" |
Colin Cross | 0cdec99 | 2020-07-09 14:24:56 -0700 | [diff] [blame] | 70 | echo " -n: use validations to depend on tests" |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | # Parse the command line flags. |
Colin Cross | 0cdec99 | 2020-07-09 14:24:56 -0700 | [diff] [blame] | 74 | while getopts ":b:hnt" opt; do |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 75 | case $opt in |
Dan Willemsen | f0ca901 | 2015-07-13 18:11:49 -0700 | [diff] [blame] | 76 | b) BUILDDIR="$OPTARG";; |
Colin Cross | 0cdec99 | 2020-07-09 14:24:56 -0700 | [diff] [blame] | 77 | n) USE_VALIDATIONS=true;; |
Jeff Gaston | c3e2844 | 2017-08-09 15:13:12 -0700 | [diff] [blame] | 78 | t) RUN_TESTS=true;; |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 79 | h) |
| 80 | usage |
| 81 | exit 1 |
| 82 | ;; |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame] | 83 | \?) |
| 84 | echo "Invalid option: -$OPTARG" >&2 |
| 85 | usage |
| 86 | exit 1 |
| 87 | ;; |
| 88 | :) |
| 89 | echo "Option -$OPTARG requires an argument." >&2 |
| 90 | exit 1 |
| 91 | ;; |
| 92 | esac |
| 93 | done |
| 94 | |
Jeff Gaston | c3e2844 | 2017-08-09 15:13:12 -0700 | [diff] [blame] | 95 | # If RUN_TESTS is set, behave like -t was passed in as an option. |
| 96 | [ ! -z "$RUN_TESTS" ] && EXTRA_ARGS="${EXTRA_ARGS} -t" |
| 97 | |
Colin Cross | 0cdec99 | 2020-07-09 14:24:56 -0700 | [diff] [blame] | 98 | # If $USE_VALIDATIONS is set, pass --use-validations. |
| 99 | [ ! -z "$USE_VALIDATIONS" ] && EXTRA_ARGS="${EXTRA_ARGS} --use-validations" |
| 100 | |
Dan Willemsen | dac90d3 | 2018-10-25 22:02:10 -0700 | [diff] [blame] | 101 | # If EMPTY_NINJA_FILE is set, have the primary build write out a 0-byte ninja |
| 102 | # file instead of a full length one. Useful if you don't plan on executing the |
| 103 | # build, but want to verify the primary builder execution. |
| 104 | [ ! -z "$EMPTY_NINJA_FILE" ] && EXTRA_ARGS="${EXTRA_ARGS} --empty-ninja-file" |
| 105 | |
Jeff Gaston | c3e2844 | 2017-08-09 15:13:12 -0700 | [diff] [blame] | 106 | # Allow the caller to pass in a list of module files |
| 107 | if [ -z "${BLUEPRINT_LIST_FILE}" ]; then |
| 108 | BLUEPRINT_LIST_FILE="${BUILDDIR}/.bootstrap/bplist" |
| 109 | fi |
| 110 | EXTRA_ARGS="${EXTRA_ARGS} -l ${BLUEPRINT_LIST_FILE}" |
| 111 | |
Dan Willemsen | 7d0dddd | 2016-08-13 12:42:11 -0700 | [diff] [blame] | 112 | mkdir -p $BUILDDIR/.minibootstrap |
Dan Willemsen | f0ca901 | 2015-07-13 18:11:49 -0700 | [diff] [blame] | 113 | |
Dan Willemsen | 1e72321 | 2017-07-18 19:37:37 -0700 | [diff] [blame] | 114 | echo "bootstrapBuildDir = $BUILDDIR" > $BUILDDIR/.minibootstrap/build.ninja |
| 115 | echo "topFile = $SRCDIR/$TOPNAME" >> $BUILDDIR/.minibootstrap/build.ninja |
| 116 | echo "extraArgs = $EXTRA_ARGS" >> $BUILDDIR/.minibootstrap/build.ninja |
Dan Willemsen | cd4e0ce | 2017-07-19 22:43:30 -0700 | [diff] [blame] | 117 | echo "builddir = $NINJA_BUILDDIR" >> $BUILDDIR/.minibootstrap/build.ninja |
Dan Willemsen | 1e72321 | 2017-07-18 19:37:37 -0700 | [diff] [blame] | 118 | echo "include $BLUEPRINTDIR/bootstrap/build.ninja" >> $BUILDDIR/.minibootstrap/build.ninja |
Dan Willemsen | d79f1af | 2015-12-09 18:03:13 -0800 | [diff] [blame] | 119 | |
Dan Willemsen | ab223a5 | 2018-07-05 21:56:59 -0700 | [diff] [blame] | 120 | if [ ! -f "$BUILDDIR/.minibootstrap/build-globs.ninja" ]; then |
| 121 | touch "$BUILDDIR/.minibootstrap/build-globs.ninja" |
| 122 | fi |
| 123 | |
Jeff Gaston | c3e2844 | 2017-08-09 15:13:12 -0700 | [diff] [blame] | 124 | echo "BLUEPRINT_BOOTSTRAP_VERSION=2" > $BUILDDIR/.blueprint.bootstrap |
Dan Willemsen | 1e72321 | 2017-07-18 19:37:37 -0700 | [diff] [blame] | 125 | echo "SRCDIR=\"${SRCDIR}\"" >> $BUILDDIR/.blueprint.bootstrap |
| 126 | echo "BLUEPRINTDIR=\"${BLUEPRINTDIR}\"" >> $BUILDDIR/.blueprint.bootstrap |
Dan Willemsen | cd4e0ce | 2017-07-19 22:43:30 -0700 | [diff] [blame] | 127 | echo "NINJA_BUILDDIR=\"${NINJA_BUILDDIR}\"" >> $BUILDDIR/.blueprint.bootstrap |
Dan Willemsen | 1e72321 | 2017-07-18 19:37:37 -0700 | [diff] [blame] | 128 | echo "GOROOT=\"${GOROOT}\"" >> $BUILDDIR/.blueprint.bootstrap |
Jeff Gaston | c3e2844 | 2017-08-09 15:13:12 -0700 | [diff] [blame] | 129 | echo "TOPNAME=\"${TOPNAME}\"" >> $BUILDDIR/.blueprint.bootstrap |
| 130 | |
| 131 | touch "${BUILDDIR}/.out-dir" |
Dan Willemsen | d79f1af | 2015-12-09 18:03:13 -0800 | [diff] [blame] | 132 | |
| 133 | if [ ! -z "$WRAPPER" ]; then |
| 134 | cp $WRAPPER $BUILDDIR/ |
| 135 | fi |