blob: b58f27bb1bc6bd661f336d3665c4c4790e3a438f [file] [log] [blame]
Colin Cross7bb052a2015-02-03 12:59:37 -08001#!/usr/bin/env bash
2# Copyright 2009 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
Dan Willemsen6ff23252015-09-15 13:49:18 -07006# See golang.org/s/go15bootstrap for an overview of the build process.
7
Colin Cross7bb052a2015-02-03 12:59:37 -08008# Environment variables that control make.bash:
9#
10# GOROOT_FINAL: The expected final Go root, baked into binaries.
11# The default is the location of the Go tree during the build.
12#
13# GOHOSTARCH: The architecture for host tools (compilers and
14# binaries). Binaries of this type must be executable on the current
15# system, so the only common reason to set this is to set
16# GOHOSTARCH=386 on an amd64 machine.
17#
18# GOARCH: The target architecture for installed packages and tools.
19#
20# GOOS: The target operating system for installed packages and tools.
21#
Dan Willemsen6ff23252015-09-15 13:49:18 -070022# GO_GCFLAGS: Additional go tool compile arguments to use when
Colin Cross7bb052a2015-02-03 12:59:37 -080023# building the packages and commands.
24#
Dan Willemsen6ff23252015-09-15 13:49:18 -070025# GO_LDFLAGS: Additional go tool link arguments to use when
Colin Cross7bb052a2015-02-03 12:59:37 -080026# building the commands.
27#
Colin Cross7bb052a2015-02-03 12:59:37 -080028# CGO_ENABLED: Controls cgo usage during the build. Set it to 1
29# to include all cgo related files, .c and .go file with "cgo"
30# build directive, in the build. Set it to 0 to ignore them.
31#
32# GO_EXTLINK_ENABLED: Set to 1 to invoke the host linker when building
33# packages that use cgo. Set to 0 to do all linking internally. This
34# controls the default behavior of the linker's -linkmode option. The
35# default value depends on the system.
36#
Colin Crossefed6342019-09-07 08:34:44 -070037# GO_LDSO: Sets the default dynamic linker/loader (ld.so) to be used
38# by the internal linker.
39#
Colin Cross7bb052a2015-02-03 12:59:37 -080040# CC: Command line to run to compile C code for GOHOSTARCH.
41# Default is "gcc". Also supported: "clang".
42#
43# CC_FOR_TARGET: Command line to run to compile C code for GOARCH.
44# This is used by cgo. Default is CC.
45#
46# CXX_FOR_TARGET: Command line to run to compile C++ code for GOARCH.
Dan Willemsenf3f2eb62018-08-28 11:28:58 -070047# This is used by cgo. Default is CXX, or, if that is not set,
Colin Cross7bb052a2015-02-03 12:59:37 -080048# "g++" or "clang++".
49#
Dan Willemsen0c157092016-07-08 13:57:52 -070050# FC: Command line to run to compile Fortran code for GOARCH.
51# This is used by cgo. Default is "gfortran".
52#
Dan Willemsenbbdf6642017-01-13 22:57:23 -080053# PKG_CONFIG: Path to pkg-config tool. Default is "pkg-config".
54#
Dan Willemsen0c157092016-07-08 13:57:52 -070055# GO_DISTFLAGS: extra flags to provide to "dist bootstrap".
Dan Willemsene1b3b182018-02-27 19:36:27 -080056# (Or just pass them to the make.bash command line.)
57#
58# GOBUILDTIMELOGFILE: If set, make.bash and all.bash write
59# timing information to this file. Useful for profiling where the
60# time goes when these scripts run.
61#
62# GOROOT_BOOTSTRAP: A working Go tree >= Go 1.4 for bootstrap.
63# If $GOROOT_BOOTSTRAP/bin/go is missing, $(go env GOROOT) is
64# tried for all "go" in $PATH. $HOME/go1.4 by default.
Colin Cross7bb052a2015-02-03 12:59:37 -080065
66set -e
Dan Willemsen0c157092016-07-08 13:57:52 -070067
Colin Crossefed6342019-09-07 08:34:44 -070068export GOENV=off
Dan Willemsen0c157092016-07-08 13:57:52 -070069unset GOBIN # Issue 14340
Dan Willemsenf3f2eb62018-08-28 11:28:58 -070070unset GOFLAGS
Colin Cross1371fe42019-03-19 21:08:48 -070071unset GO111MODULE
Dan Willemsen0c157092016-07-08 13:57:52 -070072
Colin Cross7bb052a2015-02-03 12:59:37 -080073if [ ! -f run.bash ]; then
74 echo 'make.bash must be run from $GOROOT/src' 1>&2
75 exit 1
76fi
77
Dan Willemsene1b3b182018-02-27 19:36:27 -080078if [ "$GOBUILDTIMELOGFILE" != "" ]; then
79 echo $(LC_TIME=C date) start make.bash >"$GOBUILDTIMELOGFILE"
80fi
81
Colin Cross7bb052a2015-02-03 12:59:37 -080082# Test for Windows.
83case "$(uname)" in
84*MINGW* | *WIN32* | *CYGWIN*)
85 echo 'ERROR: Do not use make.bash to build on Windows.'
86 echo 'Use make.bat instead.'
87 echo
88 exit 1
89 ;;
90esac
91
92# Test for bad ld.
93if ld --version 2>&1 | grep 'gold.* 2\.20' >/dev/null; then
94 echo 'ERROR: Your system has gold 2.20 installed.'
95 echo 'This version is shipped by Ubuntu even though'
96 echo 'it is known not to work on Ubuntu.'
97 echo 'Binaries built with this linker are likely to fail in mysterious ways.'
98 echo
99 echo 'Run sudo apt-get remove binutils-gold.'
100 echo
101 exit 1
102fi
103
104# Test for bad SELinux.
105# On Fedora 16 the selinux filesystem is mounted at /sys/fs/selinux,
106# so loop through the possible selinux mount points.
107for se_mount in /selinux /sys/fs/selinux
108do
109 if [ -d $se_mount -a -f $se_mount/booleans/allow_execstack -a -x /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled; then
110 if ! cat $se_mount/booleans/allow_execstack | grep -c '^1 1$' >> /dev/null ; then
111 echo "WARNING: the default SELinux policy on, at least, Fedora 12 breaks "
112 echo "Go. You can enable the features that Go needs via the following "
113 echo "command (as root):"
114 echo " # setsebool -P allow_execstack 1"
115 echo
116 echo "Note that this affects your system globally! "
117 echo
118 echo "The build will continue in five seconds in case we "
119 echo "misdiagnosed the issue..."
120
121 sleep 5
122 fi
123 fi
124done
125
126# Test for debian/kFreeBSD.
127# cmd/dist will detect kFreeBSD as freebsd/$GOARCH, but we need to
128# disable cgo manually.
Dan Willemsenc78f7142017-07-26 13:08:14 -0700129if [ "$(uname -s)" = "GNU/kFreeBSD" ]; then
130 export CGO_ENABLED=0
Colin Cross7bb052a2015-02-03 12:59:37 -0800131fi
132
Colin Crossefed6342019-09-07 08:34:44 -0700133# On Alpine Linux, use the musl dynamic linker/loader
134if [ -f "/etc/alpine-release" ]; then
135 if type readelf >/dev/null 2>&1; then
136 echo "int main() { return 0; }" | ${CC:-gcc} -o ./test-alpine-ldso -x c -
137 export GO_LDSO=$(readelf -l ./test-alpine-ldso | grep 'interpreter:' | sed -e 's/^.*interpreter: \(.*\)[]]/\1/')
138 rm -f ./test-alpine-ldso
139 fi
140fi
141
Colin Cross7bb052a2015-02-03 12:59:37 -0800142# Clean old generated file that will cause problems in the build.
143rm -f ./runtime/runtime_defs.go
144
145# Finally! Run the build.
146
Dan Willemsene1b3b182018-02-27 19:36:27 -0800147verbose=false
148vflag=""
149if [ "$1" = "-v" ]; then
150 verbose=true
151 vflag=-v
152 shift
153fi
154
155export GOROOT_BOOTSTRAP=${GOROOT_BOOTSTRAP:-$HOME/go1.4}
Colin Cross7bb052a2015-02-03 12:59:37 -0800156export GOROOT="$(cd .. && pwd)"
Dan Willemsene1b3b182018-02-27 19:36:27 -0800157IFS=$'\n'; for go_exe in $(type -ap go); do
158 if [ ! -x "$GOROOT_BOOTSTRAP/bin/go" ]; then
Dan Willemsenf3f2eb62018-08-28 11:28:58 -0700159 goroot=$(GOROOT='' GOOS='' GOARCH='' "$go_exe" env GOROOT)
Dan Willemsene1b3b182018-02-27 19:36:27 -0800160 if [ "$goroot" != "$GOROOT" ]; then
161 GOROOT_BOOTSTRAP=$goroot
162 fi
163 fi
164done; unset IFS
Dan Willemsen6ff23252015-09-15 13:49:18 -0700165if [ ! -x "$GOROOT_BOOTSTRAP/bin/go" ]; then
166 echo "ERROR: Cannot find $GOROOT_BOOTSTRAP/bin/go." >&2
167 echo "Set \$GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4." >&2
Dan Willemsen0c157092016-07-08 13:57:52 -0700168 exit 1
169fi
Colin Cross846c3162021-05-14 11:11:40 -0700170# Get the exact bootstrap toolchain version to help with debugging.
171# We clear GOOS and GOARCH to avoid an ominous but harmless warning if
172# the bootstrap doesn't support them.
173GOROOT_BOOTSTRAP_VERSION=$(GOOS= GOARCH= $GOROOT_BOOTSTRAP/bin/go version | sed 's/go version //')
174echo "Building Go cmd/dist using $GOROOT_BOOTSTRAP. ($GOROOT_BOOTSTRAP_VERSION)"
175if $verbose; then
176 echo cmd/dist
177fi
Dan Willemsenc78f7142017-07-26 13:08:14 -0700178if [ "$GOROOT_BOOTSTRAP" = "$GOROOT" ]; then
Dan Willemsen0c157092016-07-08 13:57:52 -0700179 echo "ERROR: \$GOROOT_BOOTSTRAP must not be set to \$GOROOT" >&2
180 echo "Set \$GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4." >&2
181 exit 1
Colin Cross7bb052a2015-02-03 12:59:37 -0800182fi
Dan Willemsen6ff23252015-09-15 13:49:18 -0700183rm -f cmd/dist/dist
Colin Crossefed6342019-09-07 08:34:44 -0700184GOROOT="$GOROOT_BOOTSTRAP" GOOS="" GOARCH="" GO111MODULE=off "$GOROOT_BOOTSTRAP/bin/go" build -o cmd/dist/dist ./cmd/dist
Colin Cross7bb052a2015-02-03 12:59:37 -0800185
186# -e doesn't propagate out of eval, so check success by hand.
187eval $(./cmd/dist/dist env -p || echo FAIL=true)
188if [ "$FAIL" = true ]; then
189 exit 1
190fi
191
Dan Willemsene1b3b182018-02-27 19:36:27 -0800192if $verbose; then
193 echo
194fi
Colin Cross7bb052a2015-02-03 12:59:37 -0800195
196if [ "$1" = "--dist-tool" ]; then
197 # Stop after building dist tool.
198 mkdir -p "$GOTOOLDIR"
199 if [ "$2" != "" ]; then
200 cp cmd/dist/dist "$2"
201 fi
202 mv cmd/dist/dist "$GOTOOLDIR"/dist
203 exit 0
204fi
205
Colin Cross7bb052a2015-02-03 12:59:37 -0800206buildall="-a"
207if [ "$1" = "--no-clean" ]; then
208 buildall=""
209 shift
210fi
Dan Willemsen0c157092016-07-08 13:57:52 -0700211
Dan Willemsene1b3b182018-02-27 19:36:27 -0800212# Run dist bootstrap to complete make.bash.
213# Bootstrap installs a proper cmd/dist, built with the new toolchain.
214# Throw ours, built with Go 1.4, away after bootstrap.
215./cmd/dist/dist bootstrap $buildall $vflag $GO_DISTFLAGS "$@"
216rm -f ./cmd/dist/dist
Colin Cross7bb052a2015-02-03 12:59:37 -0800217
Dan Willemsene1b3b182018-02-27 19:36:27 -0800218# DO NOT ADD ANY NEW CODE HERE.
219# The bootstrap+rm above are the final step of make.bash.
220# If something must be added, add it to cmd/dist's cmdbootstrap,
221# to avoid needing three copies in three different shell languages
222# (make.bash, make.bat, make.rc).