blob: 32b736ad7885e7547e1abb74354ea4c90c525aa3 [file] [log] [blame]
Shenghou Mab73d8fb2016-01-13 21:33:33 -05001#!/usr/bin/env bash
Emmanuel Odeke53fd5222016-04-10 14:32:26 -07002# Copyright 2015 The Go Authors. All rights reserved.
Russ Coxdeff22d2015-03-01 19:44:03 -05003# 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 Fitzpatrickf0f62fc2017-11-30 02:26:51 +000017#
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 Coxdeff22d2015-03-01 19:44:03 -050026
27set -e
28
29if [ "$GOOS" = "" -o "$GOARCH" = "" ]; then
30 echo "usage: GOOS=os GOARCH=arch ./bootstrap.bash" >&2
31 exit 2
32fi
33
34targ="../../go-${GOOS}-${GOARCH}-bootstrap"
35if [ -e $targ ]; then
36 echo "$targ already exists; remove before continuing"
37 exit 2
38fi
39
Brad Fitzpatrickf0f62fc2017-11-30 02:26:51 +000040if [ "$BOOTSTRAP_FORMAT" != "mintgz" -a "$BOOTSTRAP_FORMAT" != "" ]; then
41 echo "unknown BOOTSTRAP_FORMAT format"
42 exit 2
43fi
44
Russ Coxdeff22d2015-03-01 19:44:03 -050045unset GOROOT
46src=$(cd .. && pwd)
47echo "#### Copying to $targ"
48cp -R "$src" "$targ"
49cd "$targ"
50echo
51echo "#### Cleaning $targ"
Fabian Wickborn1ac84d42015-08-20 10:45:51 +020052rm -f .gitignore
53if [ -e .git ]; then
54 git clean -f -d
55fi
Russ Coxdeff22d2015-03-01 19:44:03 -050056echo
57echo "#### Building $targ"
58echo
59cd src
60./make.bash --no-banner
61gohostos="$(../bin/go env GOHOSTOS)"
62gohostarch="$(../bin/go env GOHOSTARCH)"
63goos="$(../bin/go env GOOS)"
64goarch="$(../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.
68cd ..
69if [ "$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
74else
75 mv bin/*_*/* bin
76 rmdir bin/*_*
77 rm -rf "pkg/${gohostos}_${gohostarch}" "pkg/tool/${gohostos}_${gohostarch}"
78fi
Brad Fitzpatrickf0f62fc2017-11-30 02:26:51 +000079
Ian Lance Taylor32a08d02018-01-29 20:35:43 -080080if [ "$BOOTSTRAP_FORMAT" = "mintgz" ]; then
81 # Fetch git revision before rm -rf .git.
82 GITREV=$(git rev-parse --short HEAD)
83fi
84
Russ Coxdeff22d2015-03-01 19:44:03 -050085rm -rf pkg/bootstrap pkg/obj .git
86
Brad Fitzpatrickf0f62fc2017-11-30 02:26:51 +000087# Support for building minimal tar.gz for the builders.
88# The build system doesn't support bzip2, and by deleting more stuff,
89# they start faster, especially on machines without fast filesystems
90# and things like tmpfs configures.
91# Do not depend on this format. It's for internal use only.
92if [ "$BOOTSTRAP_FORMAT" = "mintgz" ]; then
93 OUTGZ="gobootstrap-${GOOS}-${GOARCH}-${GITREV}.tar.gz"
94 echo "Preparing to generate build system's ${OUTGZ}; cleaning ..."
95 rm -rf bin/gofmt
96 rm -rf src/runtime/race/race_*.syso
97 rm -rf api test doc misc/cgo/test misc/trace
98 rm -rf pkg/tool/*_*/{addr2line,api,cgo,cover,doc,fix,nm,objdump,pack,pprof,test2json,trace,vet}
99 rm -rf pkg/*_*/{image,database,cmd}
100 rm -rf $(find . -type d -name testdata)
101 find . -type f -name '*_test.go' -exec rm {} \;
102 # git clean doesn't clean symlinks apparently, and the buildlet
103 # rejects them, so:
104 find . -type l -exec rm {} \;
105
106 echo "Writing ${OUTGZ} ..."
107 tar cf - . | gzip -9 > ../$OUTGZ
108 cd ..
109 ls -l "$(pwd)/$OUTGZ"
110 exit 0
111fi
112
Russ Coxdeff22d2015-03-01 19:44:03 -0500113echo ----
114echo Bootstrap toolchain for "$GOOS/$GOARCH" installed in "$(pwd)".
115echo Building tbz.
116cd ..
117tar cf - "go-${GOOS}-${GOARCH}-bootstrap" | bzip2 -9 >"go-${GOOS}-${GOARCH}-bootstrap.tbz"
118ls -l "$(pwd)/go-${GOOS}-${GOARCH}-bootstrap.tbz"
119exit 0