blob: 78882d98341f6eede9a9d68200cfe41eceeaf5cf [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#
37# CC: Command line to run to compile C code for GOHOSTARCH.
38# Default is "gcc". Also supported: "clang".
39#
40# CC_FOR_TARGET: Command line to run to compile C code for GOARCH.
41# This is used by cgo. Default is CC.
42#
43# CXX_FOR_TARGET: Command line to run to compile C++ code for GOARCH.
Dan Willemsenf3f2eb62018-08-28 11:28:58 -070044# This is used by cgo. Default is CXX, or, if that is not set,
Colin Cross7bb052a2015-02-03 12:59:37 -080045# "g++" or "clang++".
46#
Dan Willemsen0c157092016-07-08 13:57:52 -070047# FC: Command line to run to compile Fortran code for GOARCH.
48# This is used by cgo. Default is "gfortran".
49#
Dan Willemsenbbdf6642017-01-13 22:57:23 -080050# PKG_CONFIG: Path to pkg-config tool. Default is "pkg-config".
51#
Dan Willemsen0c157092016-07-08 13:57:52 -070052# GO_DISTFLAGS: extra flags to provide to "dist bootstrap".
Dan Willemsene1b3b182018-02-27 19:36:27 -080053# (Or just pass them to the make.bash command line.)
54#
55# GOBUILDTIMELOGFILE: If set, make.bash and all.bash write
56# timing information to this file. Useful for profiling where the
57# time goes when these scripts run.
58#
59# GOROOT_BOOTSTRAP: A working Go tree >= Go 1.4 for bootstrap.
60# If $GOROOT_BOOTSTRAP/bin/go is missing, $(go env GOROOT) is
61# tried for all "go" in $PATH. $HOME/go1.4 by default.
Colin Cross7bb052a2015-02-03 12:59:37 -080062
63set -e
Dan Willemsen0c157092016-07-08 13:57:52 -070064
65unset GOBIN # Issue 14340
Dan Willemsenf3f2eb62018-08-28 11:28:58 -070066unset GOFLAGS
Dan Willemsen0c157092016-07-08 13:57:52 -070067
Colin Cross7bb052a2015-02-03 12:59:37 -080068if [ ! -f run.bash ]; then
69 echo 'make.bash must be run from $GOROOT/src' 1>&2
70 exit 1
71fi
72
Dan Willemsene1b3b182018-02-27 19:36:27 -080073if [ "$GOBUILDTIMELOGFILE" != "" ]; then
74 echo $(LC_TIME=C date) start make.bash >"$GOBUILDTIMELOGFILE"
75fi
76
Colin Cross7bb052a2015-02-03 12:59:37 -080077# Test for Windows.
78case "$(uname)" in
79*MINGW* | *WIN32* | *CYGWIN*)
80 echo 'ERROR: Do not use make.bash to build on Windows.'
81 echo 'Use make.bat instead.'
82 echo
83 exit 1
84 ;;
85esac
86
87# Test for bad ld.
88if ld --version 2>&1 | grep 'gold.* 2\.20' >/dev/null; then
89 echo 'ERROR: Your system has gold 2.20 installed.'
90 echo 'This version is shipped by Ubuntu even though'
91 echo 'it is known not to work on Ubuntu.'
92 echo 'Binaries built with this linker are likely to fail in mysterious ways.'
93 echo
94 echo 'Run sudo apt-get remove binutils-gold.'
95 echo
96 exit 1
97fi
98
99# Test for bad SELinux.
100# On Fedora 16 the selinux filesystem is mounted at /sys/fs/selinux,
101# so loop through the possible selinux mount points.
102for se_mount in /selinux /sys/fs/selinux
103do
104 if [ -d $se_mount -a -f $se_mount/booleans/allow_execstack -a -x /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled; then
105 if ! cat $se_mount/booleans/allow_execstack | grep -c '^1 1$' >> /dev/null ; then
106 echo "WARNING: the default SELinux policy on, at least, Fedora 12 breaks "
107 echo "Go. You can enable the features that Go needs via the following "
108 echo "command (as root):"
109 echo " # setsebool -P allow_execstack 1"
110 echo
111 echo "Note that this affects your system globally! "
112 echo
113 echo "The build will continue in five seconds in case we "
114 echo "misdiagnosed the issue..."
115
116 sleep 5
117 fi
118 fi
119done
120
121# Test for debian/kFreeBSD.
122# cmd/dist will detect kFreeBSD as freebsd/$GOARCH, but we need to
123# disable cgo manually.
Dan Willemsenc78f7142017-07-26 13:08:14 -0700124if [ "$(uname -s)" = "GNU/kFreeBSD" ]; then
125 export CGO_ENABLED=0
Colin Cross7bb052a2015-02-03 12:59:37 -0800126fi
127
128# Clean old generated file that will cause problems in the build.
129rm -f ./runtime/runtime_defs.go
130
131# Finally! Run the build.
132
Dan Willemsene1b3b182018-02-27 19:36:27 -0800133verbose=false
134vflag=""
135if [ "$1" = "-v" ]; then
136 verbose=true
137 vflag=-v
138 shift
139fi
140
141export GOROOT_BOOTSTRAP=${GOROOT_BOOTSTRAP:-$HOME/go1.4}
Colin Cross7bb052a2015-02-03 12:59:37 -0800142export GOROOT="$(cd .. && pwd)"
Dan Willemsene1b3b182018-02-27 19:36:27 -0800143IFS=$'\n'; for go_exe in $(type -ap go); do
144 if [ ! -x "$GOROOT_BOOTSTRAP/bin/go" ]; then
Dan Willemsenf3f2eb62018-08-28 11:28:58 -0700145 goroot=$(GOROOT='' GOOS='' GOARCH='' "$go_exe" env GOROOT)
Dan Willemsene1b3b182018-02-27 19:36:27 -0800146 if [ "$goroot" != "$GOROOT" ]; then
147 GOROOT_BOOTSTRAP=$goroot
148 fi
149 fi
150done; unset IFS
151echo "Building Go cmd/dist using $GOROOT_BOOTSTRAP."
152if $verbose; then
153 echo cmd/dist
154fi
Dan Willemsen6ff23252015-09-15 13:49:18 -0700155if [ ! -x "$GOROOT_BOOTSTRAP/bin/go" ]; then
156 echo "ERROR: Cannot find $GOROOT_BOOTSTRAP/bin/go." >&2
157 echo "Set \$GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4." >&2
Dan Willemsen0c157092016-07-08 13:57:52 -0700158 exit 1
159fi
Dan Willemsenc78f7142017-07-26 13:08:14 -0700160if [ "$GOROOT_BOOTSTRAP" = "$GOROOT" ]; then
Dan Willemsen0c157092016-07-08 13:57:52 -0700161 echo "ERROR: \$GOROOT_BOOTSTRAP must not be set to \$GOROOT" >&2
162 echo "Set \$GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4." >&2
163 exit 1
Colin Cross7bb052a2015-02-03 12:59:37 -0800164fi
Dan Willemsen6ff23252015-09-15 13:49:18 -0700165rm -f cmd/dist/dist
166GOROOT="$GOROOT_BOOTSTRAP" GOOS="" GOARCH="" "$GOROOT_BOOTSTRAP/bin/go" build -o cmd/dist/dist ./cmd/dist
Colin Cross7bb052a2015-02-03 12:59:37 -0800167
168# -e doesn't propagate out of eval, so check success by hand.
169eval $(./cmd/dist/dist env -p || echo FAIL=true)
170if [ "$FAIL" = true ]; then
171 exit 1
172fi
173
Dan Willemsene1b3b182018-02-27 19:36:27 -0800174if $verbose; then
175 echo
176fi
Colin Cross7bb052a2015-02-03 12:59:37 -0800177
178if [ "$1" = "--dist-tool" ]; then
179 # Stop after building dist tool.
180 mkdir -p "$GOTOOLDIR"
181 if [ "$2" != "" ]; then
182 cp cmd/dist/dist "$2"
183 fi
184 mv cmd/dist/dist "$GOTOOLDIR"/dist
185 exit 0
186fi
187
Colin Cross7bb052a2015-02-03 12:59:37 -0800188buildall="-a"
189if [ "$1" = "--no-clean" ]; then
190 buildall=""
191 shift
192fi
Dan Willemsen0c157092016-07-08 13:57:52 -0700193
Dan Willemsene1b3b182018-02-27 19:36:27 -0800194# Run dist bootstrap to complete make.bash.
195# Bootstrap installs a proper cmd/dist, built with the new toolchain.
196# Throw ours, built with Go 1.4, away after bootstrap.
197./cmd/dist/dist bootstrap $buildall $vflag $GO_DISTFLAGS "$@"
198rm -f ./cmd/dist/dist
Colin Cross7bb052a2015-02-03 12:59:37 -0800199
Dan Willemsene1b3b182018-02-27 19:36:27 -0800200# DO NOT ADD ANY NEW CODE HERE.
201# The bootstrap+rm above are the final step of make.bash.
202# If something must be added, add it to cmd/dist's cmdbootstrap,
203# to avoid needing three copies in three different shell languages
204# (make.bash, make.bat, make.rc).