Shenghou Ma | b73d8fb | 2016-01-13 21:33:33 -0500 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Emmanuel Odeke | 53fd522 | 2016-04-10 14:32:26 -0700 | [diff] [blame] | 2 | # Copyright 2015 The Go Authors. All rights reserved. |
Russ Cox | deff22d | 2015-03-01 19:44:03 -0500 | [diff] [blame] | 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. |
Brad Fitzpatrick | f0f62fc | 2017-11-30 02:26:51 +0000 | [diff] [blame] | 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. |
Russ Cox | deff22d | 2015-03-01 19:44:03 -0500 | [diff] [blame] | 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 | |
Brad Fitzpatrick | f0f62fc | 2017-11-30 02:26:51 +0000 | [diff] [blame] | 40 | if [ "$BOOTSTRAP_FORMAT" != "mintgz" -a "$BOOTSTRAP_FORMAT" != "" ]; then |
| 41 | echo "unknown BOOTSTRAP_FORMAT format" |
| 42 | exit 2 |
| 43 | fi |
| 44 | |
Russ Cox | deff22d | 2015-03-01 19:44:03 -0500 | [diff] [blame] | 45 | unset GOROOT |
| 46 | src=$(cd .. && pwd) |
| 47 | echo "#### Copying to $targ" |
Dmitri Goutnik | 930d6ec | 2019-04-27 08:16:36 -0500 | [diff] [blame] | 48 | cp -Rp "$src" "$targ" |
Russ Cox | deff22d | 2015-03-01 19:44:03 -0500 | [diff] [blame] | 49 | cd "$targ" |
| 50 | echo |
| 51 | echo "#### Cleaning $targ" |
Ian Lance Taylor | 4a11914 | 2019-04-19 09:48:36 -0700 | [diff] [blame] | 52 | chmod -R +w . |
Fabian Wickborn | 1ac84d4 | 2015-08-20 10:45:51 +0200 | [diff] [blame] | 53 | rm -f .gitignore |
| 54 | if [ -e .git ]; then |
| 55 | git clean -f -d |
| 56 | fi |
Russ Cox | deff22d | 2015-03-01 19:44:03 -0500 | [diff] [blame] | 57 | echo |
| 58 | echo "#### Building $targ" |
| 59 | echo |
| 60 | cd src |
| 61 | ./make.bash --no-banner |
| 62 | gohostos="$(../bin/go env GOHOSTOS)" |
| 63 | gohostarch="$(../bin/go env GOHOSTARCH)" |
| 64 | goos="$(../bin/go env GOOS)" |
| 65 | goarch="$(../bin/go env GOARCH)" |
| 66 | |
| 67 | # NOTE: Cannot invoke go command after this point. |
| 68 | # We're about to delete all but the cross-compiled binaries. |
| 69 | cd .. |
| 70 | if [ "$goos" = "$gohostos" -a "$goarch" = "$gohostarch" ]; then |
| 71 | # cross-compile for local system. nothing to copy. |
| 72 | # useful if you've bootstrapped yourself but want to |
| 73 | # prepare a clean toolchain for others. |
| 74 | true |
| 75 | else |
Tobias Klauser | df1cdbf | 2019-04-12 14:20:21 +0200 | [diff] [blame] | 76 | rm -f bin/go_${goos}_${goarch}_exec |
Russ Cox | deff22d | 2015-03-01 19:44:03 -0500 | [diff] [blame] | 77 | mv bin/*_*/* bin |
| 78 | rmdir bin/*_* |
| 79 | rm -rf "pkg/${gohostos}_${gohostarch}" "pkg/tool/${gohostos}_${gohostarch}" |
| 80 | fi |
Brad Fitzpatrick | f0f62fc | 2017-11-30 02:26:51 +0000 | [diff] [blame] | 81 | |
Ian Lance Taylor | 32a08d0 | 2018-01-29 20:35:43 -0800 | [diff] [blame] | 82 | if [ "$BOOTSTRAP_FORMAT" = "mintgz" ]; then |
| 83 | # Fetch git revision before rm -rf .git. |
| 84 | GITREV=$(git rev-parse --short HEAD) |
| 85 | fi |
| 86 | |
Russ Cox | deff22d | 2015-03-01 19:44:03 -0500 | [diff] [blame] | 87 | rm -rf pkg/bootstrap pkg/obj .git |
| 88 | |
Brad Fitzpatrick | f0f62fc | 2017-11-30 02:26:51 +0000 | [diff] [blame] | 89 | # Support for building minimal tar.gz for the builders. |
| 90 | # The build system doesn't support bzip2, and by deleting more stuff, |
| 91 | # they start faster, especially on machines without fast filesystems |
| 92 | # and things like tmpfs configures. |
| 93 | # Do not depend on this format. It's for internal use only. |
| 94 | if [ "$BOOTSTRAP_FORMAT" = "mintgz" ]; then |
| 95 | OUTGZ="gobootstrap-${GOOS}-${GOARCH}-${GITREV}.tar.gz" |
| 96 | echo "Preparing to generate build system's ${OUTGZ}; cleaning ..." |
| 97 | rm -rf bin/gofmt |
| 98 | rm -rf src/runtime/race/race_*.syso |
| 99 | rm -rf api test doc misc/cgo/test misc/trace |
| 100 | rm -rf pkg/tool/*_*/{addr2line,api,cgo,cover,doc,fix,nm,objdump,pack,pprof,test2json,trace,vet} |
| 101 | rm -rf pkg/*_*/{image,database,cmd} |
| 102 | rm -rf $(find . -type d -name testdata) |
| 103 | find . -type f -name '*_test.go' -exec rm {} \; |
| 104 | # git clean doesn't clean symlinks apparently, and the buildlet |
| 105 | # rejects them, so: |
| 106 | find . -type l -exec rm {} \; |
| 107 | |
| 108 | echo "Writing ${OUTGZ} ..." |
| 109 | tar cf - . | gzip -9 > ../$OUTGZ |
| 110 | cd .. |
| 111 | ls -l "$(pwd)/$OUTGZ" |
| 112 | exit 0 |
| 113 | fi |
| 114 | |
Russ Cox | deff22d | 2015-03-01 19:44:03 -0500 | [diff] [blame] | 115 | echo ---- |
| 116 | echo Bootstrap toolchain for "$GOOS/$GOARCH" installed in "$(pwd)". |
| 117 | echo Building tbz. |
| 118 | cd .. |
| 119 | tar cf - "go-${GOOS}-${GOARCH}-bootstrap" | bzip2 -9 >"go-${GOOS}-${GOARCH}-bootstrap.tbz" |
| 120 | ls -l "$(pwd)/go-${GOOS}-${GOARCH}-bootstrap.tbz" |
| 121 | exit 0 |