github.com/kdevb0x/go@v0.0.0-20180115030120-39687051e9e7/src/bootstrap.bash (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2015 The Go Authors. All rights reserved.
     3  # Use of this source code is governed by a BSD-style
     4  # license that can be found in the LICENSE file.
     5  
     6  # When run as (for example)
     7  #
     8  #	GOOS=linux GOARCH=ppc64 bootstrap.bash
     9  #
    10  # this script cross-compiles a toolchain for that GOOS/GOARCH
    11  # combination, leaving the resulting tree in ../../go-${GOOS}-${GOARCH}-bootstrap.
    12  # That tree can be copied to a machine of the given target type
    13  # and used as $GOROOT_BOOTSTRAP to bootstrap a local build.
    14  #
    15  # Only changes that have been committed to Git (at least locally,
    16  # not necessary reviewed and submitted to master) are included in the tree.
    17  #
    18  # As a special case for Go's internal use only, if the
    19  # BOOTSTRAP_FORMAT environment variable is set to "mintgz", the
    20  # resulting archive is intended for use by the Go build system and
    21  # differs in that the mintgz file:
    22  #   * is a tar.gz file instead of bz2
    23  #   * has many unnecessary files deleted to reduce its size
    24  #   * does not have a shared directory component for each tar entry
    25  # Do not depend on the mintgz format.
    26  
    27  set -e
    28  
    29  if [ "$GOOS" = "" -o "$GOARCH" = "" ]; then
    30  	echo "usage: GOOS=os GOARCH=arch ./bootstrap.bash" >&2
    31  	exit 2
    32  fi
    33  
    34  targ="../../go-${GOOS}-${GOARCH}-bootstrap"
    35  if [ -e $targ ]; then
    36  	echo "$targ already exists; remove before continuing"
    37  	exit 2
    38  fi
    39  
    40  if [ "$BOOTSTRAP_FORMAT" != "mintgz" -a "$BOOTSTRAP_FORMAT" != "" ]; then
    41  	echo "unknown BOOTSTRAP_FORMAT format"
    42  	exit 2
    43  fi
    44  
    45  unset GOROOT
    46  src=$(cd .. && pwd)
    47  echo "#### Copying to $targ"
    48  cp -R "$src" "$targ"
    49  cd "$targ"
    50  echo
    51  echo "#### Cleaning $targ"
    52  rm -f .gitignore
    53  if [ -e .git ]; then
    54  	git clean -f -d
    55  fi
    56  echo
    57  echo "#### Building $targ"
    58  echo
    59  cd src
    60  ./make.bash --no-banner
    61  gohostos="$(../bin/go env GOHOSTOS)"
    62  gohostarch="$(../bin/go env GOHOSTARCH)"
    63  goos="$(../bin/go env GOOS)"
    64  goarch="$(../bin/go env GOARCH)"
    65  
    66  # NOTE: Cannot invoke go command after this point.
    67  # We're about to delete all but the cross-compiled binaries.
    68  cd ..
    69  if [ "$goos" = "$gohostos" -a "$goarch" = "$gohostarch" ]; then
    70  	# cross-compile for local system. nothing to copy.
    71  	# useful if you've bootstrapped yourself but want to
    72  	# prepare a clean toolchain for others.
    73  	true
    74  else
    75  	mv bin/*_*/* bin
    76  	rmdir bin/*_*
    77  	rm -rf "pkg/${gohostos}_${gohostarch}" "pkg/tool/${gohostos}_${gohostarch}"
    78  fi
    79  
    80  GITREV=$(git rev-parse --short HEAD)
    81  rm -rf pkg/bootstrap pkg/obj .git
    82  
    83  # Support for building minimal tar.gz for the builders.
    84  # The build system doesn't support bzip2, and by deleting more stuff,
    85  # they start faster, especially on machines without fast filesystems
    86  # and things like tmpfs configures.
    87  # Do not depend on this format. It's for internal use only.
    88  if [ "$BOOTSTRAP_FORMAT" = "mintgz" ]; then
    89  	OUTGZ="gobootstrap-${GOOS}-${GOARCH}-${GITREV}.tar.gz"
    90  	echo "Preparing to generate build system's ${OUTGZ}; cleaning ..."
    91  	rm -rf bin/gofmt
    92  	rm -rf src/runtime/race/race_*.syso
    93  	rm -rf api test doc misc/cgo/test misc/trace
    94  	rm -rf pkg/tool/*_*/{addr2line,api,cgo,cover,doc,fix,nm,objdump,pack,pprof,test2json,trace,vet}
    95  	rm -rf pkg/*_*/{image,database,cmd}
    96  	rm -rf $(find . -type d -name testdata)
    97  	find . -type f -name '*_test.go' -exec rm {} \;
    98  	# git clean doesn't clean symlinks apparently, and the buildlet
    99  	# rejects them, so:
   100  	find . -type l -exec rm {} \;
   101  
   102  	echo "Writing ${OUTGZ} ..."
   103  	tar cf - . | gzip -9 > ../$OUTGZ
   104  	cd ..
   105  	ls -l "$(pwd)/$OUTGZ"
   106  	exit 0
   107  fi
   108  
   109  echo ----
   110  echo Bootstrap toolchain for "$GOOS/$GOARCH" installed in "$(pwd)".
   111  echo Building tbz.
   112  cd ..
   113  tar cf - "go-${GOOS}-${GOARCH}-bootstrap" | bzip2 -9 >"go-${GOOS}-${GOARCH}-bootstrap.tbz"
   114  ls -l "$(pwd)/go-${GOOS}-${GOARCH}-bootstrap.tbz"
   115  exit 0