blob: 82c903eadbb8b67c02a161d2783a3b583dc8982d [file] [log] [blame]
Devon H. O'Dell553be842009-11-14 15:29:09 -08001#!/usr/bin/env bash
Rob Pikedf28e142008-06-11 13:34:08 -07002# 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
Russ Cox20a10e72015-01-07 11:38:00 -05006# See golang.org/s/go15bootstrap for an overview of the build process.
7
Russ Coxb03a5f62012-03-01 12:12:22 -05008# 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#
Jeremy Jackins73549782015-05-28 16:13:23 +090022# GO_GCFLAGS: Additional go tool compile arguments to use when
Russ Coxb03a5f62012-03-01 12:12:22 -050023# building the packages and commands.
24#
Jeremy Jackins73549782015-05-28 16:13:23 +090025# GO_LDFLAGS: Additional go tool link arguments to use when
David Symondsfa6d3ab2012-03-13 12:52:15 +110026# building the commands.
Russ Coxb03a5f62012-03-01 12:12:22 -050027#
Alex Brainman7fbef932012-03-20 14:04:20 +110028# 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.
Russ Cox6d888f12013-02-15 13:37:43 -080031#
Ian Lance Taylor3197be42013-03-29 16:33:35 -070032# 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#
Elias Naur2dc759d2014-02-06 09:11:00 -080037# CC: Command line to run to compile C code for GOHOSTARCH.
Russ Cox6d888f12013-02-15 13:37:43 -080038# Default is "gcc". Also supported: "clang".
Elias Naur2dc759d2014-02-06 09:11:00 -080039#
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.
44# This is used by cgo. Default is CXX, or, if that is not set,
45# "g++" or "clang++".
Shenghou Ma6b188ef2013-10-01 23:44:20 -040046#
kortschak50c38d42016-02-18 21:19:03 +103047# FC: Command line to run to compile Fortran code for GOARCH.
48# This is used by cgo. Default is "gfortran".
49#
Brad Fitzpatrick4577cfc2015-12-17 14:01:17 -080050# GO_DISTFLAGS: extra flags to provide to "dist bootstrap".
Russ Coxb03a5f62012-03-01 12:12:22 -050051
Russ Cox76036192008-09-18 15:06:43 -070052set -e
Russ Cox82905362012-02-04 00:54:08 -050053if [ ! -f run.bash ]; then
Russ Coxda392d92010-08-18 10:08:49 -040054 echo 'make.bash must be run from $GOROOT/src' 1>&2
55 exit 1
56fi
Devon H. O'Dell857d4cf2009-12-11 15:14:09 -080057
Russ Coxb5d81e52012-02-12 23:14:37 -050058# Test for Windows.
59case "$(uname)" in
60*MINGW* | *WIN32* | *CYGWIN*)
61 echo 'ERROR: Do not use make.bash to build on Windows.'
62 echo 'Use make.bat instead.'
63 echo
64 exit 1
65 ;;
66esac
67
Russ Cox82905362012-02-04 00:54:08 -050068# Test for bad ld.
Christopher Wedgwood604bd702011-10-13 12:25:25 -040069if ld --version 2>&1 | grep 'gold.* 2\.20' >/dev/null; then
Russ Coxeedfc442011-03-18 18:23:00 -040070 echo 'ERROR: Your system has gold 2.20 installed.'
71 echo 'This version is shipped by Ubuntu even though'
72 echo 'it is known not to work on Ubuntu.'
73 echo 'Binaries built with this linker are likely to fail in mysterious ways.'
74 echo
75 echo 'Run sudo apt-get remove binutils-gold.'
76 echo
77 exit 1
78fi
79
Russ Cox82905362012-02-04 00:54:08 -050080# Test for bad SELinux.
81# On Fedora 16 the selinux filesystem is mounted at /sys/fs/selinux,
82# so loop through the possible selinux mount points.
Bobby Powers90c50702011-11-11 16:41:37 -050083for se_mount in /selinux /sys/fs/selinux
84do
85 if [ -d $se_mount -a -f $se_mount/booleans/allow_execstack -a -x /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled; then
86 if ! cat $se_mount/booleans/allow_execstack | grep -c '^1 1$' >> /dev/null ; then
87 echo "WARNING: the default SELinux policy on, at least, Fedora 12 breaks "
88 echo "Go. You can enable the features that Go needs via the following "
89 echo "command (as root):"
90 echo " # setsebool -P allow_execstack 1"
91 echo
92 echo "Note that this affects your system globally! "
93 echo
94 echo "The build will continue in five seconds in case we "
95 echo "misdiagnosed the issue..."
Adam Langley2643f742009-11-11 15:02:15 -080096
Bobby Powers90c50702011-11-11 16:41:37 -050097 sleep 5
98 fi
Adam Langley2643f742009-11-11 15:02:15 -080099 fi
Bobby Powers90c50702011-11-11 16:41:37 -0500100done
Adam Langley2643f742009-11-11 15:02:15 -0800101
Dave Cheney3167c122012-12-04 08:27:30 +1100102# Test for debian/kFreeBSD.
103# cmd/dist will detect kFreeBSD as freebsd/$GOARCH, but we need to
104# disable cgo manually.
105if [ "$(uname -s)" == "GNU/kFreeBSD" ]; then
106 export CGO_ENABLED=0
107fi
108
Russ Cox318465b2012-03-07 11:38:05 -0500109# Clean old generated file that will cause problems in the build.
Russ Cox220a6de2014-09-08 00:06:45 -0400110rm -f ./runtime/runtime_defs.go
Russ Cox318465b2012-03-07 11:38:05 -0500111
Russ Cox82905362012-02-04 00:54:08 -0500112# Finally! Run the build.
Russ Cox9ff712e2009-11-10 19:20:34 -0800113
Russ Cox20a10e72015-01-07 11:38:00 -0500114echo '##### Building Go bootstrap tool.'
Russ Coxb5d81e52012-02-12 23:14:37 -0500115echo cmd/dist
Gustavo Niemeyer54f1e1b2012-02-09 20:47:12 -0200116export GOROOT="$(cd .. && pwd)"
Russ Cox20a10e72015-01-07 11:38:00 -0500117GOROOT_BOOTSTRAP=${GOROOT_BOOTSTRAP:-$HOME/go1.4}
118if [ ! -x "$GOROOT_BOOTSTRAP/bin/go" ]; then
119 echo "ERROR: Cannot find $GOROOT_BOOTSTRAP/bin/go." >&2
120 echo "Set \$GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4." >&2
Dave Cheneyf9379eb2015-08-20 12:28:51 +1000121 exit 1
122fi
123if [ "$GOROOT_BOOTSTRAP" == "$GOROOT" ]; then
124 echo "ERROR: \$GOROOT_BOOTSTRAP must not be set to \$GOROOT" >&2
125 echo "Set \$GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4." >&2
126 exit 1
Shenghou Maa5fe79e2013-04-16 13:30:52 -0700127fi
Russ Cox20a10e72015-01-07 11:38:00 -0500128rm -f cmd/dist/dist
Russ Cox1fac6d12015-01-14 15:14:54 -0500129GOROOT="$GOROOT_BOOTSTRAP" GOOS="" GOARCH="" "$GOROOT_BOOTSTRAP/bin/go" build -o cmd/dist/dist ./cmd/dist
Shenghou Ma72801292012-03-13 03:34:22 +0800130
Rob Pikee8140bd2013-08-19 11:18:43 +1000131# -e doesn't propagate out of eval, so check success by hand.
132eval $(./cmd/dist/dist env -p || echo FAIL=true)
133if [ "$FAIL" = true ]; then
134 exit 1
135fi
136
Russ Cox2050a9e2012-01-30 23:43:46 -0500137echo
Russ Cox82905362012-02-04 00:54:08 -0500138
Russ Cox1c290fd2012-02-06 13:48:43 -0500139if [ "$1" = "--dist-tool" ]; then
140 # Stop after building dist tool.
Gustavo Niemeyer7e19e532012-03-02 02:45:01 -0300141 mkdir -p "$GOTOOLDIR"
David Symonds0ab3ea92012-02-15 09:06:24 +1100142 if [ "$2" != "" ]; then
143 cp cmd/dist/dist "$2"
144 fi
Gustavo Niemeyer7e19e532012-03-02 02:45:01 -0300145 mv cmd/dist/dist "$GOTOOLDIR"/dist
Russ Cox1c290fd2012-02-06 13:48:43 -0500146 exit 0
147fi
148
Russ Cox2506fd42012-02-15 11:48:17 -0500149buildall="-a"
150if [ "$1" = "--no-clean" ]; then
151 buildall=""
Russ Cox0c2a7272014-05-20 12:10:19 -0400152 shift
Russ Cox2506fd42012-02-15 11:48:17 -0500153fi
David Chase98232272016-03-18 13:35:34 -0400154./cmd/dist/dist bootstrap $buildall $GO_DISTFLAGS -v # builds go_bootstrap
David Chase09a9ce62016-03-17 14:12:12 -0400155
Russ Cox2506fd42012-02-15 11:48:17 -0500156# Delay move of dist tool to now, because bootstrap may clear tool directory.
Gustavo Niemeyer7e19e532012-03-02 02:45:01 -0300157mv cmd/dist/dist "$GOTOOLDIR"/dist
Russ Cox82905362012-02-04 00:54:08 -0500158echo
159
Russ Cox7b848c62012-02-13 22:31:51 -0500160if [ "$GOHOSTARCH" != "$GOARCH" -o "$GOHOSTOS" != "$GOOS" ]; then
Russ Coxd160d1b2014-12-10 10:45:59 -0500161 echo "##### Building packages and commands for host, $GOHOSTOS/$GOHOSTARCH."
Elias Naur2dc759d2014-02-06 09:11:00 -0800162 # CC_FOR_TARGET is recorded as the default compiler for the go tool. When building for the host, however,
163 # use the host compiler, CC, from `cmd/dist/dist env` instead.
164 CC=$CC GOOS=$GOHOSTOS GOARCH=$GOHOSTARCH \
Russ Cox096b2942015-02-22 12:41:32 -0500165 "$GOTOOLDIR"/go_bootstrap install -gcflags "$GO_GCFLAGS" -ldflags "$GO_LDFLAGS" -v std cmd
Russ Cox7b848c62012-02-13 22:31:51 -0500166 echo
167fi
168
Russ Coxd160d1b2014-12-10 10:45:59 -0500169echo "##### Building packages and commands for $GOOS/$GOARCH."
Russ Cox096b2942015-02-22 12:41:32 -0500170CC=$CC_FOR_TARGET "$GOTOOLDIR"/go_bootstrap install $GO_FLAGS -gcflags "$GO_GCFLAGS" -ldflags "$GO_LDFLAGS" -v std cmd
Russ Cox82905362012-02-04 00:54:08 -0500171echo
172
Gustavo Niemeyer7e19e532012-03-02 02:45:01 -0300173rm -f "$GOTOOLDIR"/go_bootstrap
Russ Cox7b848c62012-02-13 22:31:51 -0500174
Russ Cox82905362012-02-04 00:54:08 -0500175if [ "$1" != "--no-banner" ]; then
Gustavo Niemeyer7e19e532012-03-02 02:45:01 -0300176 "$GOTOOLDIR"/dist banner
Russ Cox41a61652011-12-20 16:50:13 -0500177fi