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 |
| 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 | |
| 23 | set -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 Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 30 | # 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 Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame^] | 32 | # 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 Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 36 | |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame^] | 37 | # 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 Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 41 | |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame^] | 42 | # 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 Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 48 | |
Jamie Gennis | 7330a23 | 2014-06-13 16:25:09 -0700 | [diff] [blame^] | 49 | usage() { |
| 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. |
| 56 | IN="$BOOTSTRAP_MANIFEST" |
| 57 | REGEN_BOOTSTRAP_MANIFEST=false |
| 58 | while 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 |
| 76 | done |
| 77 | |
| 78 | if [ $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 |
| 88 | fi |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 89 | |
| 90 | sed -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 |