github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/build/xgo/build.sh (about)

     1  #!/bin/bash
     2  #
     3  # Patched script of xgo build.sh (MIT license)
     4  # Fork: https://github.com/techknowlogick/xgo
     5  # Original script https://github.com/techknowlogick/xgo/blob/master/docker/base/build.sh
     6  #
     7  #
     8  # Contains the main cross compiler, that individually sets up each target build
     9  # platform, compiles all the C dependencies, then build the requested executable
    10  # itself.
    11  #
    12  # Usage: build.sh <import path>
    13  #
    14  # Needed environment variables:
    15  #   REPO_REMOTE    - Optional VCS remote if not the primary repository is needed
    16  #   REPO_BRANCH    - Optional VCS branch to use, if not the master branch
    17  #   DEPS           - Optional list of C dependency packages to build
    18  #   ARGS           - Optional arguments to pass to C dependency configure scripts
    19  #   PACK           - Optional sub-package, if not the import path is being built
    20  #   OUT            - Optional output prefix to override the package name
    21  #   FLAG_V         - Optional verbosity flag to set on the Go builder
    22  #   FLAG_X         - Optional flag to print the build progress commands
    23  #   FLAG_RACE      - Optional race flag to set on the Go builder
    24  #   FLAG_TAGS      - Optional tag flag to set on the Go builder
    25  #   FLAG_LDFLAGS   - Optional ldflags flag to set on the Go builder
    26  #   FLAG_BUILDMODE - Optional buildmode flag to set on the Go builder
    27  #   TARGETS        - Space separated list of build targets to compile for
    28  #   GO_VERSION     - Bootstrapped version of Go to disable uncupported targets
    29  #   EXT_GOPATH     - GOPATH elements mounted from the host filesystem
    30  
    31  # Define a function that figures out the binary extension
    32  function extension {
    33    if [ "$FLAG_BUILDMODE" == "archive" ] || [ "$FLAG_BUILDMODE" == "c-archive" ]; then
    34      if [ "$1" == "windows" ]; then
    35        echo ".lib"
    36      else
    37        echo ".a"
    38      fi
    39    elif [ "$FLAG_BUILDMODE" == "shared" ] || [ "$FLAG_BUILDMODE" == "c-shared" ]; then
    40      if [ "$1" == "windows" ]; then
    41        echo ".dll"
    42      elif [ "$1" == "darwin" ]; then
    43        echo ".dylib"
    44      else
    45        echo ".so"
    46      fi
    47    else
    48      if [ "$1" == "windows" ]; then
    49        echo ".exe"
    50      fi
    51    fi
    52  }
    53  
    54  mkdir -p /build
    55  
    56  # Detect if we are using go modules
    57  if [[ "$GO111MODULE" == "on" || "$GO111MODULE" == "auto" ]]; then
    58    USEMODULES=true
    59    else
    60    USEMODULES=false
    61  fi
    62  
    63  # Either set a local build environemnt, or pull any remote imports
    64  if [ "$EXT_GOPATH" != "" ]; then
    65    # If local builds are requested, inject the sources
    66    echo "Building locally $1..."
    67    export GOPATH=$GOPATH:$EXT_GOPATH
    68    set -e
    69  
    70    # Find and change into the package folder
    71    cd `go list -e -f {{.Dir}} $1`
    72    export GOPATH=$GOPATH:`pwd`/Godeps/_workspace
    73  elif [[ "$USEMODULES" == true ]]; then
    74    # Go module builds should assume a local repository
    75    # at mapped to /source containing at least a go.mod file.
    76    if [[ ! -d /source ]]; then
    77      echo "Go modules are enabled but go.mod was not found in the source folder."
    78      exit 10
    79    fi
    80    # Change into the repo/source folder
    81    cd /source
    82    echo "Building /source/go.mod..."
    83  else
    84    # Inject all possible Godep paths to short circuit go gets
    85    GOPATH_ROOT=$GOPATH/src
    86    IMPORT_PATH=$1
    87    while [ "$IMPORT_PATH" != "." ]; do
    88      export GOPATH=$GOPATH:$GOPATH_ROOT/$IMPORT_PATH/Godeps/_workspace
    89      IMPORT_PATH=`dirname $IMPORT_PATH`
    90    done
    91  
    92    # Otherwise download the canonical import path (may fail, don't allow failures beyond)
    93    echo "Fetching main repository $1..."
    94    go get -v -d $1
    95    set -e
    96  
    97    cd $GOPATH_ROOT/$1
    98  
    99    # Switch over the code-base to another checkout if requested
   100    if [ "$REPO_REMOTE" != "" ] || [ "$REPO_BRANCH" != "" ]; then
   101      # Detect the version control system type
   102      IMPORT_PATH=$1
   103      while [ "$IMPORT_PATH" != "." ] && [ "$REPO_TYPE" == "" ]; do
   104        if [ -d "$GOPATH_ROOT/$IMPORT_PATH/.git" ]; then
   105          REPO_TYPE="git"
   106        elif  [ -d "$GOPATH_ROOT/$IMPORT_PATH/.hg" ]; then
   107          REPO_TYPE="hg"
   108        fi
   109        IMPORT_PATH=`dirname $IMPORT_PATH`
   110      done
   111  
   112      if [ "$REPO_TYPE" == "" ]; then
   113        echo "Unknown version control system type, cannot switch remotes and branches."
   114        exit -1
   115      fi
   116      # If we have a valid VCS, execute the switch operations
   117      if [ "$REPO_REMOTE" != "" ]; then
   118        echo "Switching over to remote $REPO_REMOTE..."
   119        if [ "$REPO_TYPE" == "git" ]; then
   120          git remote set-url origin $REPO_REMOTE
   121          git fetch --all
   122          git reset --hard origin/HEAD
   123          git clean -dxf
   124        elif [ "$REPO_TYPE" == "hg" ]; then
   125          echo -e "[paths]\ndefault = $REPO_REMOTE\n" >> .hg/hgrc
   126          hg pull
   127        fi
   128      fi
   129      if [ "$REPO_BRANCH" != "" ]; then
   130        echo "Switching over to branch $REPO_BRANCH..."
   131        if [ "$REPO_TYPE" == "git" ]; then
   132          git reset --hard origin/$REPO_BRANCH
   133          git clean -dxf
   134        elif [ "$REPO_TYPE" == "hg" ]; then
   135          hg checkout $REPO_BRANCH
   136        fi
   137      fi
   138    fi
   139  fi
   140  
   141  # Download all the C dependencies
   142  mkdir /deps
   143  DEPS=($DEPS) && for dep in "${DEPS[@]}"; do
   144    if [ "${dep##*.}" == "tar" ]; then cat "/deps-cache/`basename $dep`" | tar -C /deps -x; fi
   145    if [ "${dep##*.}" == "gz" ];  then cat "/deps-cache/`basename $dep`" | tar -C /deps -xz; fi
   146    if [ "${dep##*.}" == "bz2" ]; then cat "/deps-cache/`basename $dep`" | tar -C /deps -xj; fi
   147  done
   148  
   149  DEPS_ARGS=($ARGS)
   150  
   151  # Save the contents of the pre-build /usr/local folder for post cleanup
   152  USR_LOCAL_CONTENTS=`ls /usr/local`
   153  
   154  # Configure some global build parameters
   155  NAME=`basename $1/$PACK`
   156  
   157  # Go module-based builds error with 'cannot find main module'
   158  # when $PACK is defined
   159  PACK_RELPATH="./$PACK"
   160  if [[ "$USEMODULES" = true ]]; then
   161    NAME=`sed -n 's/module\ \(.*\)/\1/p' /source/go.mod`
   162  fi
   163  
   164  if [ "$OUT" != "" ]; then
   165    NAME=$OUT
   166  fi
   167  
   168  if [ "$FLAG_V" == "true" ];    then V=-v; fi
   169  if [ "$FLAG_X" == "true" ];    then X=-x; fi
   170  if [ "$FLAG_RACE" == "true" ]; then R=-race; fi
   171  if [ "$FLAG_TAGS" != "" ];     then T=(--tags "$FLAG_TAGS"); fi
   172  if [ "$FLAG_LDFLAGS" != "" ];  then LD="$FLAG_LDFLAGS"; fi
   173  
   174  if [ "$FLAG_BUILDMODE" != "" ] && [ "$FLAG_BUILDMODE" != "default" ]; then BM="--buildmode=$FLAG_BUILDMODE"; fi
   175  if [ "$FLAG_MOD" != "" ]; then MOD="--mod=$FLAG_MOD"; fi
   176  
   177  # If no build targets were specified, inject a catch all wildcard
   178  if [ "$TARGETS" == "" ]; then
   179    TARGETS="./."
   180  fi
   181  
   182  # Build for each requested platform individually
   183  for TARGET in $TARGETS; do
   184    # Split the target into platform and architecture
   185    XGOOS=`echo $TARGET | cut -d '/' -f 1`
   186    XGOARCH=`echo $TARGET | cut -d '/' -f 2`
   187  
   188    # Check and build for Linux targets
   189    if ([ $XGOOS == "." ] || [ $XGOOS == "linux" ]) && ([ $XGOARCH == "." ] || [ $XGOARCH == "amd64" ]); then
   190      echo "Compiling for linux/amd64..."
   191      HOST=x86_64-linux PREFIX=/usr/local $BUILD_DEPS /deps ${DEPS_ARGS[@]}
   192      if [[ "$USEMODULES" == false ]]; then
   193        GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go get $V $X "${T[@]}" --ldflags="$V $LD" -d $PACK_RELPATH
   194      fi
   195      GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build $V $X $MOD "${T[@]}" --ldflags="$V $LD" $R $BM -o "/build/$NAME-linux-amd64$R`extension linux`" $PACK_RELPATH
   196    fi
   197    if ([ $XGOOS == "." ] || [ $XGOOS == "linux" ]) && ([ $XGOARCH == "." ] || [ $XGOARCH == "386" ]); then
   198      echo "Compiling for linux/386..."
   199      HOST=i686-linux PREFIX=/usr/local $BUILD_DEPS /deps ${DEPS_ARGS[@]}
   200      if [[ "$USEMODULES" == false ]]; then
   201        GOOS=linux GOARCH=386 CGO_ENABLED=1 go get $V $X "${T[@]}" --ldflags="$V $LD" -d $PACK_RELPATH
   202      fi
   203      GOOS=linux GOARCH=386 CGO_ENABLED=1 go build $V $X $MOD "${T[@]}" --ldflags="$V $LD" $BM -o "/build/$NAME-linux-386`extension linux`" $PACK_RELPATH
   204    fi
   205    if ([ $XGOOS == "." ] || [ $XGOOS == "linux" ]) && ([ $XGOARCH == "." ] || [ $XGOARCH == "arm" ] || [ $XGOARCH == "arm-5" ]); then
   206      if [ "$GO_VERSION" -ge 150 ]; then
   207        echo "Bootstrapping linux/arm-5..."
   208        CC=arm-linux-gnueabi-gcc-6 GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=1 CGO_CFLAGS="-march=armv5" CGO_CXXFLAGS="-march=armv5" go install std
   209      fi
   210      echo "Compiling for linux/arm-5..."
   211      CC=arm-linux-gnueabi-gcc-6 CXX=arm-linux-gnueabi-g++-6 HOST=arm-linux-gnueabi PREFIX=/usr/arm-linux-gnueabi CFLAGS="-march=armv5" CXXFLAGS="-march=armv5" $BUILD_DEPS /deps ${DEPS_ARGS[@]}
   212      export PKG_CONFIG_PATH=/usr/arm-linux-gnueabi/lib/pkgconfig
   213  
   214      if [[ "$USEMODULES" == false ]]; then
   215        CC=arm-linux-gnueabi-gcc-6 CXX=arm-linux-gnueabi-g++-6 GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=1 CGO_CFLAGS="-march=armv5" CGO_CXXFLAGS="-march=armv5" go get $V $X "${T[@]}" --ldflags="$V $LD" -d $PACK_RELPATH
   216      fi
   217      CC=arm-linux-gnueabi-gcc-6 CXX=arm-linux-gnueabi-g++-6 GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=1 CGO_CFLAGS="-march=armv5" CGO_CXXFLAGS="-march=armv5" go build $V $X $MOD "${T[@]}" --ldflags="$V $LD" $BM -o "/build/$NAME-linux-arm-5`extension linux`" $PACK_RELPATH
   218      if [ "$GO_VERSION" -ge 150 ]; then
   219        echo "Cleaning up Go runtime for linux/arm-5..."
   220        rm -rf /usr/local/go/pkg/linux_arm
   221      fi
   222    fi
   223    if ([ $XGOOS == "." ] || [ $XGOOS == "linux" ]) && ([ $XGOARCH == "." ] || [ $XGOARCH == "arm-6" ]); then
   224      if [ "$GO_VERSION" -lt 150 ]; then
   225        echo "Go version too low, skipping linux/arm-6..."
   226      else
   227        echo "Bootstrapping linux/arm-6..."
   228        CC=arm-linux-gnueabi-gcc-6 GOOS=linux GOARCH=arm GOARM=6 CGO_ENABLED=1 CGO_CFLAGS="-march=armv6" CGO_CXXFLAGS="-march=armv6" go install std
   229  
   230        echo "Compiling for linux/arm-6..."
   231        CC=arm-linux-gnueabi-gcc-6 CXX=arm-linux-gnueabi-g++-6 HOST=arm-linux-gnueabi PREFIX=/usr/arm-linux-gnueabi CFLAGS="-march=armv6" CXXFLAGS="-march=armv6" $BUILD_DEPS /deps ${DEPS_ARGS[@]}
   232        export PKG_CONFIG_PATH=/usr/arm-linux-gnueabi/lib/pkgconfig
   233  
   234        if [[ "$USEMODULES" == false ]]; then
   235          CC=arm-linux-gnueabi-gcc-6 CXX=arm-linux-gnueabi-g++-6 GOOS=linux GOARCH=arm GOARM=6 CGO_ENABLED=1 CGO_CFLAGS="-march=armv6" CGO_CXXFLAGS="-march=armv6" go get $V $X "${T[@]}" --ldflags="$V $LD" -d $PACK_RELPATH
   236        fi
   237        CC=arm-linux-gnueabi-gcc-6 CXX=arm-linux-gnueabi-g++-6 GOOS=linux GOARCH=arm GOARM=6 CGO_ENABLED=1 CGO_CFLAGS="-march=armv6" CGO_CXXFLAGS="-march=armv6" go build $V $X $MOD "${T[@]}" --ldflags="$V $LD" $BM -o "/build/$NAME-linux-arm-6`extension linux`" $PACK_RELPATH
   238  
   239        echo "Cleaning up Go runtime for linux/arm-6..."
   240        rm -rf /usr/local/go/pkg/linux_arm
   241      fi
   242    fi
   243    if ([ $XGOOS == "." ] || [ $XGOOS == "linux" ]) && ([ $XGOARCH == "." ] || [ $XGOARCH == "arm-7" ]); then
   244      if [ "$GO_VERSION" -lt 150 ]; then
   245        echo "Go version too low, skipping linux/arm-7..."
   246      else
   247        echo "Bootstrapping linux/arm-7..."
   248        CC=arm-linux-gnueabihf-gcc-6 GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=1 CGO_CFLAGS="-march=armv7-a" CGO_CXXFLAGS="-march=armv7-a" go install std
   249  
   250        echo "Compiling for linux/arm-7..."
   251        CC=arm-linux-gnueabihf-gcc-6 CXX=arm-linux-gnueabihf-g++-6 HOST=arm-linux-gnueabihf PREFIX=/usr/arm-linux-gnueabihf CFLAGS="-march=armv7-a -fPIC" CXXFLAGS="-march=armv7-a -fPIC" $BUILD_DEPS /deps ${DEPS_ARGS[@]}
   252        export PKG_CONFIG_PATH=/usr/arm-linux-gnueabihf/lib/pkgconfig
   253  
   254        if [[ "$USEMODULES" == false ]]; then
   255          CC=arm-linux-gnueabihf-gcc-6 CXX=arm-linux-gnueabihf-g++-6 GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=1 CGO_CFLAGS="-march=armv7-a -fPIC" CGO_CXXFLAGS="-march=armv7-a -fPIC" go get $V $X "${T[@]}" --ldflags="$V $LD" -d $PACK_RELPATH
   256        fi
   257        CC=arm-linux-gnueabihf-gcc-6 CXX=arm-linux-gnueabihf-g++-6 GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=1 CGO_CFLAGS="-march=armv7-a -fPIC" CGO_CXXFLAGS="-march=armv7-a -fPIC" go build $V $X $MOD "${T[@]}" --ldflags="$V $LD" $BM -o "/build/$NAME-linux-arm-7`extension linux`" $PACK_RELPATH
   258  
   259        echo "Cleaning up Go runtime for linux/arm-7..."
   260        rm -rf /usr/local/go/pkg/linux_arm
   261      fi
   262    fi
   263    if ([ $XGOOS == "." ] || [ $XGOOS == "linux" ]) && ([ $XGOARCH == "." ] || [ $XGOARCH == "arm64" ]); then
   264      if [ "$GO_VERSION" -lt 150 ]; then
   265        echo "Go version too low, skipping linux/arm64..."
   266      else
   267        echo "Compiling for linux/arm64..."
   268        CC=aarch64-linux-gnu-gcc-6 CXX=aarch64-linux-gnu-g++-6 HOST=aarch64-linux-gnu PREFIX=/usr/aarch64-linux-gnu $BUILD_DEPS /deps ${DEPS_ARGS[@]}
   269        export PKG_CONFIG_PATH=/usr/aarch64-linux-gnu/lib/pkgconfig
   270  
   271         if [[ "$USEMODULES" == false ]]; then
   272          CC=aarch64-linux-gnu-gcc-6 CXX=aarch64-linux-gnu-g++-6 GOOS=linux GOARCH=arm64 CGO_ENABLED=1 go get $V $X "${T[@]}" --ldflags="$V $LD" -d $PACK_RELPATH
   273        fi
   274        CC=aarch64-linux-gnu-gcc-6 CXX=aarch64-linux-gnu-g++-6 GOOS=linux GOARCH=arm64 CGO_ENABLED=1 go build $V $X $MOD "${T[@]}" --ldflags="$V $LD" $BM -o "/build/$NAME-linux-arm64`extension linux`" $PACK_RELPATH
   275      fi
   276    fi
   277    if ([ $XGOOS == "." ] || [ $XGOOS == "linux" ]) && ([ $XGOARCH == "." ] || [ $XGOARCH == "mips64" ]); then
   278      if [ "$GO_VERSION" -lt 170 ]; then
   279        echo "Go version too low, skipping linux/mips64..."
   280      else
   281        echo "Compiling for linux/mips64..."
   282        CC=mips64-linux-gnuabi64-gcc-6 CXX=mips64-linux-gnuabi64-g++-6 HOST=mips64-linux-gnuabi64 PREFIX=/usr/mips64-linux-gnuabi64 $BUILD_DEPS /deps ${DEPS_ARGS[@]}
   283        export PKG_CONFIG_PATH=/usr/mips64-linux-gnuabi64/lib/pkgconfig
   284  
   285              if [[ "$USEMODULES" == false ]]; then
   286          CC=mips64-linux-gnuabi64-gcc-6 CXX=mips64-linux-gnuabi64-g++-6 GOOS=linux GOARCH=mips64 CGO_ENABLED=1 go get $V $X "${T[@]}" --ldflags="$V $LD" -d $PACK_RELPATH
   287        fi
   288        CC=mips64-linux-gnuabi64-gcc-6 CXX=mips64-linux-gnuabi64-g++-6 GOOS=linux GOARCH=mips64 CGO_ENABLED=1 go build $V $X $MOD "${T[@]}" --ldflags="$V $LD" $BM -o "/build/$NAME-linux-mips64`extension linux`" $PACK_RELPATH
   289      fi
   290    fi
   291    if ([ $XGOOS == "." ] || [ $XGOOS == "linux" ]) && ([ $XGOARCH == "." ] || [ $XGOARCH == "mips64le" ]); then
   292      if [ "$GO_VERSION" -lt 170 ]; then
   293        echo "Go version too low, skipping linux/mips64le..."
   294      else
   295        echo "Compiling for linux/mips64le..."
   296        CC=mips64el-linux-gnuabi64-gcc-6 CXX=mips64el-linux-gnuabi64-g++-6 HOST=mips64el-linux-gnuabi64 PREFIX=/usr/mips64el-linux-gnuabi64 $BUILD_DEPS /deps ${DEPS_ARGS[@]}
   297        export PKG_CONFIG_PATH=/usr/mips64le-linux-gnuabi64/lib/pkgconfig
   298  
   299        if [[ "$USEMODULES" == false ]]; then
   300          CC=mips64el-linux-gnuabi64-gcc-6 CXX=mips64el-linux-gnuabi64-g++-6 GOOS=linux GOARCH=mips64le CGO_ENABLED=1 go get $V $X "${T[@]}" --ldflags="$V $LD" -d $PACK_RELPATH
   301        fi
   302        CC=mips64el-linux-gnuabi64-gcc-6 CXX=mips64el-linux-gnuabi64-g++-6 GOOS=linux GOARCH=mips64le CGO_ENABLED=1 go build $V $X $MOD "${T[@]}" --ldflags="$V $LD" $BM -o "/build/$NAME-linux-mips64le`extension linux`" $PACK_RELPATH
   303      fi
   304    fi
   305    if ([ $XGOOS == "." ] || [ $XGOOS == "linux" ]) && ([ $XGOARCH == "." ] || [ $XGOARCH == "mips" ]); then
   306      if [ "$GO_VERSION" -lt 180 ]; then
   307        echo "Go version too low, skipping linux/mips..."
   308      else
   309        echo "Compiling for linux/mips..."
   310        CC=mips-linux-gnu-gcc-6 CXX=mips-linux-gnu-g++-6 HOST=mips-linux-gnu PREFIX=/usr/mips-linux-gnu $BUILD_DEPS /deps ${DEPS_ARGS[@]}
   311        export PKG_CONFIG_PATH=/usr/mips-linux-gnu/lib/pkgconfig
   312  
   313        if [[ "$USEMODULES" == false ]]; then
   314          CC=mips-linux-gnu-gcc-6 CXX=mips-linux-gnu-g++-6 GOOS=linux GOARCH=mips CGO_ENABLED=1 go get $V $X "${T[@]}" --ldflags="$V $LD" -d $PACK_RELPATH
   315        fi
   316        CC=mips-linux-gnu-gcc-6 CXX=mips-linux-gnu-g++-6 GOOS=linux GOARCH=mips CGO_ENABLED=1 go build $V $X $MOD "${T[@]}" --ldflags="$V $LD" $BM -o "/build/$NAME-linux-mips`extension linux`" $PACK_RELPATH
   317      fi
   318    fi
   319    if ([ $XGOOS == "." ] || [ $XGOOS == "linux" ]) && ([ $XGOARCH == "." ] || [ $XGOARCH == "mipsle" ]); then
   320      if [ "$GO_VERSION" -lt 180 ]; then
   321        echo "Go version too low, skipping linux/mipsle..."
   322      else
   323        echo "Compiling for linux/mipsle..."
   324        CC=mipsel-linux-gnu-gcc-6 CXX=mipsel-linux-gnu-g++-6 HOST=mipsel-linux-gnu PREFIX=/usr/mipsel-linux-gnu $BUILD_DEPS /deps ${DEPS_ARGS[@]}
   325        export PKG_CONFIG_PATH=/usr/mipsle-linux-gnu/lib/pkgconfig
   326  
   327        if [[ "$USEMODULES" == false ]]; then
   328          CC=mipsel-linux-gnu-gcc-6 CXX=mipsel-linux-gnu-g++-6 GOOS=linux GOARCH=mipsle CGO_ENABLED=1 go get $V $X "${T[@]}" --ldflags="$V $LD" -d $PACK_RELPATH
   329        fi
   330        CC=mipsel-linux-gnu-gcc-6 CXX=mipsel-linux-gnu-g++-6 GOOS=linux GOARCH=mipsle CGO_ENABLED=1 go build $V $X $MOD "${T[@]}" --ldflags="$V $LD" $BM -o "/build/$NAME-linux-mipsle`extension linux`" $PACK_RELPATH
   331      fi
   332    fi
   333    if ([ $XGOOS == "." ] || [ $XGOOS == "linux" ]) && ([ $XGOARCH == "." ] || [ $XGOARCH == "s390x" ]); then
   334      if [ "$GO_VERSION" -lt 170 ]; then
   335        echo "Go version too low, skipping linux/s390x..."
   336      else
   337        echo "Compiling for linux/s390x..."
   338        CC=s390x-linux-gnu-gcc-6 CXX=s390x-linux-gnu-g++-6 HOST=s390x-linux-gnu PREFIX=/usr/s390x-linux-gnu $BUILD_DEPS /deps ${DEPS_ARGS[@]}
   339        export PKG_CONFIG_PATH=/usr/s390x-linux-gnu/lib/pkgconfig
   340  
   341        if [[ "$USEMODULES" == false ]]; then
   342          CC=s390x-linux-gnu-gcc-6 CXX=s390x-linux-gnu-g++-6 GOOS=linux GOARCH=s390x CGO_ENABLED=1 go get $V $X "${T[@]}" --ldflags="$V $LD" -d $PACK_RELPATH
   343        fi
   344        CC=s390x-linux-gnu-gcc-6 CXX=s390x-linux-gnu-g++-6 GOOS=linux GOARCH=s390x CGO_ENABLED=1 go build $V $X $MOD "${T[@]}" --ldflags="$V $LD" $BM -o "/build/$NAME-linux-s390x`extension linux`" $PACK_RELPATH
   345      fi
   346    fi
   347    # Check and build for Windows targets
   348    if ([ $XGOOS == "." ] || [ $XGOOS == "linux" ]) && ([ $XGOARCH == "." ] || [ $XGOARCH == "ppc64le" ]); then
   349      if [ "$GO_VERSION" -lt 170 ]; then
   350        echo "Go version too low, skipping linux/powerpc64le..."
   351      else
   352        echo "Compiling for linux/ppc64le..."
   353        CC=powerpc64le-linux-gnu-gcc-6 CXX=powerpc64le-linux-gnu-g++-6 HOST=ppc64le-linux-gnu PREFIX=/usr/ppc64le-linux-gnu $BUILD_DEPS /deps ${DEPS_ARGS[@]}
   354        export PKG_CONFIG_PATH=/usr/ppc64le-linux-gnu/lib/pkgconfig
   355  
   356        if [[ "$USEMODULES" == false ]]; then
   357          CC=powerpc64le-linux-gnu-gcc-6 CXX=powerpc64le-linux-gnu-g++-6 GOOS=linux GOARCH=ppc64le CGO_ENABLED=1 go get $V $X "${T[@]}" --ldflags="$V $LD" -d $PACK_RELPATH
   358        fi
   359        CC=powerpc64le-linux-gnu-gcc-6 CXX=powerpc64le-linux-gnu-g++-6 GOOS=linux GOARCH=ppc64le CGO_ENABLED=1 go build $V $X $MOD "${T[@]}" --ldflags="$V $LD" $BM -o "/build/$NAME-linux-ppc64le`extension linux`" $PACK_RELPATH
   360      fi
   361    fi
   362    if [ $XGOOS == "." ] || [[ $XGOOS == windows* ]]; then
   363      # Split the platform version and configure the Windows NT version
   364      PLATFORM=`echo $XGOOS | cut -d '-' -f 2`
   365      if [ "$PLATFORM" == "" ] || [ "$PLATFORM" == "." ] || [ "$PLATFORM" == "windows" ]; then
   366        PLATFORM=4.0 # Windows NT
   367      fi
   368  
   369      MAJOR=`echo $PLATFORM | cut -d '.' -f 1`
   370      if [ "${PLATFORM/.}" != "$PLATFORM" ] ; then
   371        MINOR=`echo $PLATFORM | cut -d '.' -f 2`
   372      fi
   373      CGO_NTDEF="-D_WIN32_WINNT=0x`printf "%02d" $MAJOR``printf "%02d" $MINOR`"
   374  
   375      # Build the requested windows binaries
   376      if [ $XGOARCH == "." ] || [ $XGOARCH == "amd64" ]; then
   377        echo "Compiling for windows-$PLATFORM/amd64..."
   378        CC=x86_64-w64-mingw32-gcc-posix CXX=x86_64-w64-mingw32-g++-posix HOST=x86_64-w64-mingw32 PREFIX=/usr/x86_64-w64-mingw32 $BUILD_DEPS /deps ${DEPS_ARGS[@]}
   379        export PKG_CONFIG_PATH=/usr/x86_64-w64-mingw32/lib/pkgconfig
   380  
   381        if [[ "$USEMODULES" == false ]]; then
   382          CC=x86_64-w64-mingw32-gcc-posix CXX=x86_64-w64-mingw32-g++-posix GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CGO_CFLAGS="$CGO_NTDEF" CGO_CXXFLAGS="$CGO_NTDEF" go get $V $X "${T[@]}" --ldflags="$V $LD" -d $PACK_RELPATH
   383        fi
   384        CC=x86_64-w64-mingw32-gcc-posix CXX=x86_64-w64-mingw32-g++-posix GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CGO_CFLAGS="$CGO_NTDEF" CGO_CXXFLAGS="$CGO_NTDEF" go build $V $X $MOD "${T[@]}" --ldflags="$V $LD" $R $BM -o "/build/$NAME-windows-$PLATFORM-amd64$R`extension windows`" $PACK_RELPATH
   385      fi
   386      if [ $XGOARCH == "." ] || [ $XGOARCH == "386" ]; then
   387        echo "Compiling for windows-$PLATFORM/386..."
   388        CC=i686-w64-mingw32-gcc-posix CXX=i686-w64-mingw32-g++-posix HOST=i686-w64-mingw32 PREFIX=/usr/i686-w64-mingw32 $BUILD_DEPS /deps ${DEPS_ARGS[@]}
   389        export PKG_CONFIG_PATH=/usr/i686-w64-mingw32/lib/pkgconfig
   390  
   391        if [[ "$USEMODULES" == false ]]; then
   392          CC=i686-w64-mingw32-gcc-posix CXX=i686-w64-mingw32-g++-posix GOOS=windows GOARCH=386 CGO_ENABLED=1 CGO_CFLAGS="$CGO_NTDEF" CGO_CXXFLAGS="$CGO_NTDEF" go get $V $X "${T[@]}" --ldflags="$V $LD" -d $PACK_RELPATH
   393        fi
   394        CC=i686-w64-mingw32-gcc-posix CXX=i686-w64-mingw32-g++-posix GOOS=windows GOARCH=386 CGO_ENABLED=1 CGO_CFLAGS="$CGO_NTDEF" CGO_CXXFLAGS="$CGO_NTDEF" go build $V $X $MOD "${T[@]}" --ldflags="$V $LD" $BM -o "/build/$NAME-windows-$PLATFORM-386`extension windows`" $PACK_RELPATH
   395      fi
   396    fi
   397    # Check and build for OSX targets
   398    if [ $XGOOS == "." ] || [[ $XGOOS == darwin* ]]; then
   399      # Split the platform version and configure the deployment target
   400      PLATFORM=`echo $XGOOS | cut -d '-' -f 2`
   401      if [ "$PLATFORM" == "" ] || [ "$PLATFORM" == "." ] || [ "$PLATFORM" == "darwin" ]; then
   402        PLATFORM=10.6 # OS X Snow Leopard
   403      fi
   404      export MACOSX_DEPLOYMENT_TARGET=$PLATFORM
   405  
   406      # Strip symbol table below Go 1.6 to prevent DWARF issues
   407      LDSTRIP=""
   408      if [ "$GO_VERSION" -lt 160 ]; then
   409        LDSTRIP="-s"
   410      fi
   411      # Build the requested darwin binaries
   412      if [ $XGOARCH == "." ] || [ $XGOARCH == "amd64" ]; then
   413        echo "Compiling for darwin-$PLATFORM/amd64..."
   414        CC=o64-clang CXX=o64-clang++ HOST=x86_64-apple-darwin15 PREFIX=/usr/local $BUILD_DEPS /deps ${DEPS_ARGS[@]}
   415        if [[ "$USEMODULES" == false ]]; then
   416          CC=o64-clang CXX=o64-clang++ GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go get $V $X "${T[@]}" --ldflags="$LDSTRIP $V $LD" -d $PACK_RELPATH
   417        fi
   418        CC=o64-clang CXX=o64-clang++ GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go build $V $X $MOD "${T[@]}" --ldflags="$LDSTRIP $V $LD" $R $BM -o "/build/$NAME-darwin-$PLATFORM-amd64$R`extension darwin`" $PACK_RELPATH
   419      fi
   420      if [ $XGOARCH == "." ] || [ $XGOARCH == "386" ]; then
   421        echo "Compiling for darwin-$PLATFORM/386..."
   422        CC=o32-clang CXX=o32-clang++ HOST=i386-apple-darwin15 PREFIX=/usr/local $BUILD_DEPS /deps ${DEPS_ARGS[@]}
   423        if [[ "$USEMODULES" == false ]]; then
   424          CC=o32-clang CXX=o32-clang++ GOOS=darwin GOARCH=386 CGO_ENABLED=1 go get $V $X "${T[@]}" --ldflags="$LDSTRIP $V $LD" -d $PACK_RELPATH
   425        fi
   426        CC=o32-clang CXX=o32-clang++ GOOS=darwin GOARCH=386 CGO_ENABLED=1 go build $V $X $MOD "${T[@]}" --ldflags="$LDSTRIP $V $LD" $BM -o "/build/$NAME-darwin-$PLATFORM-386`extension darwin`" $PACK_RELPATH
   427      fi
   428      # Remove any automatically injected deployment target vars
   429      unset MACOSX_DEPLOYMENT_TARGET
   430    fi
   431  done
   432  
   433  # Clean up any leftovers for subsequent build invocations
   434  echo "Cleaning up build environment..."
   435  rm -rf /deps
   436  
   437  for dir in `ls /usr/local`; do
   438    keep=0
   439  
   440    # Check against original folder contents
   441    for old in $USR_LOCAL_CONTENTS; do
   442      if [ "$old" == "$dir" ]; then
   443        keep=1
   444      fi
   445    done
   446    # Delete anything freshly generated
   447    if [ "$keep" == "0" ]; then
   448      rm -rf "/usr/local/$dir"
   449    fi
   450  done
   451  
   452  # Preserve parent folder permissions
   453  cp -R /build/* /dist
   454  chown -R $(stat -c '%u:%g' /dist) /dist