blob: bfcb5d58eab07f49e7b4affa10edee67083d2b9e [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 Coxb03a5f62012-03-01 12:12:22 -05006# Environment variables that control make.bash:
7#
8# GOROOT_FINAL: The expected final Go root, baked into binaries.
9# The default is the location of the Go tree during the build.
10#
11# GOHOSTARCH: The architecture for host tools (compilers and
12# binaries). Binaries of this type must be executable on the current
13# system, so the only common reason to set this is to set
14# GOHOSTARCH=386 on an amd64 machine.
15#
16# GOARCH: The target architecture for installed packages and tools.
17#
18# GOOS: The target operating system for installed packages and tools.
19#
Gustavo Niemeyer7e19e532012-03-02 02:45:01 -030020# GO_GCFLAGS: Additional 5g/6g/8g arguments to use when
Russ Coxb03a5f62012-03-01 12:12:22 -050021# building the packages and commands.
22#
Gustavo Niemeyer7e19e532012-03-02 02:45:01 -030023# GO_LDFLAGS: Additional 5l/6l/8l arguments to use when
David Symondsfa6d3ab2012-03-13 12:52:15 +110024# building the commands.
Russ Coxb03a5f62012-03-01 12:12:22 -050025#
Elias Naurfe14ee52013-02-01 08:35:33 -080026# GO_CCFLAGS: Additional 5c/6c/8c arguments to use when
27# building.
28#
Alex Brainman7fbef932012-03-20 14:04:20 +110029# CGO_ENABLED: Controls cgo usage during the build. Set it to 1
30# to include all cgo related files, .c and .go file with "cgo"
31# build directive, in the build. Set it to 0 to ignore them.
Russ Cox6d888f12013-02-15 13:37:43 -080032#
Ian Lance Taylor3197be42013-03-29 16:33:35 -070033# GO_EXTLINK_ENABLED: Set to 1 to invoke the host linker when building
34# packages that use cgo. Set to 0 to do all linking internally. This
35# controls the default behavior of the linker's -linkmode option. The
36# default value depends on the system.
37#
Elias Naur2dc759d2014-02-06 09:11:00 -080038# CC: Command line to run to compile C code for GOHOSTARCH.
Russ Cox6d888f12013-02-15 13:37:43 -080039# Default is "gcc". Also supported: "clang".
Elias Naur2dc759d2014-02-06 09:11:00 -080040#
41# CC_FOR_TARGET: Command line to run to compile C code for GOARCH.
42# This is used by cgo. Default is CC.
43#
44# CXX_FOR_TARGET: Command line to run to compile C++ code for GOARCH.
45# This is used by cgo. Default is CXX, or, if that is not set,
46# "g++" or "clang++".
Shenghou Ma6b188ef2013-10-01 23:44:20 -040047#
48# GO_DISTFLAGS: extra flags to provide to "dist bootstrap". Use "-s"
49# to build a statically linked toolchain.
Russ Coxb03a5f62012-03-01 12:12:22 -050050
Russ Cox76036192008-09-18 15:06:43 -070051set -e
Russ Cox82905362012-02-04 00:54:08 -050052if [ ! -f run.bash ]; then
Russ Coxda392d92010-08-18 10:08:49 -040053 echo 'make.bash must be run from $GOROOT/src' 1>&2
54 exit 1
55fi
Devon H. O'Dell857d4cf2009-12-11 15:14:09 -080056
Russ Coxb5d81e52012-02-12 23:14:37 -050057# Test for Windows.
58case "$(uname)" in
59*MINGW* | *WIN32* | *CYGWIN*)
60 echo 'ERROR: Do not use make.bash to build on Windows.'
61 echo 'Use make.bat instead.'
62 echo
63 exit 1
64 ;;
65esac
66
Russ Cox82905362012-02-04 00:54:08 -050067# Test for bad ld.
Christopher Wedgwood604bd702011-10-13 12:25:25 -040068if ld --version 2>&1 | grep 'gold.* 2\.20' >/dev/null; then
Russ Coxeedfc442011-03-18 18:23:00 -040069 echo 'ERROR: Your system has gold 2.20 installed.'
70 echo 'This version is shipped by Ubuntu even though'
71 echo 'it is known not to work on Ubuntu.'
72 echo 'Binaries built with this linker are likely to fail in mysterious ways.'
73 echo
74 echo 'Run sudo apt-get remove binutils-gold.'
75 echo
76 exit 1
77fi
78
Russ Cox82905362012-02-04 00:54:08 -050079# Test for bad SELinux.
80# On Fedora 16 the selinux filesystem is mounted at /sys/fs/selinux,
81# so loop through the possible selinux mount points.
Bobby Powers90c50702011-11-11 16:41:37 -050082for se_mount in /selinux /sys/fs/selinux
83do
84 if [ -d $se_mount -a -f $se_mount/booleans/allow_execstack -a -x /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled; then
85 if ! cat $se_mount/booleans/allow_execstack | grep -c '^1 1$' >> /dev/null ; then
86 echo "WARNING: the default SELinux policy on, at least, Fedora 12 breaks "
87 echo "Go. You can enable the features that Go needs via the following "
88 echo "command (as root):"
89 echo " # setsebool -P allow_execstack 1"
90 echo
91 echo "Note that this affects your system globally! "
92 echo
93 echo "The build will continue in five seconds in case we "
94 echo "misdiagnosed the issue..."
Adam Langley2643f742009-11-11 15:02:15 -080095
Bobby Powers90c50702011-11-11 16:41:37 -050096 sleep 5
97 fi
Adam Langley2643f742009-11-11 15:02:15 -080098 fi
Bobby Powers90c50702011-11-11 16:41:37 -050099done
Adam Langley2643f742009-11-11 15:02:15 -0800100
Dave Cheney3167c122012-12-04 08:27:30 +1100101# Test for debian/kFreeBSD.
102# cmd/dist will detect kFreeBSD as freebsd/$GOARCH, but we need to
103# disable cgo manually.
104if [ "$(uname -s)" == "GNU/kFreeBSD" ]; then
105 export CGO_ENABLED=0
106fi
107
Russ Cox318465b2012-03-07 11:38:05 -0500108# Clean old generated file that will cause problems in the build.
109rm -f ./pkg/runtime/runtime_defs.go
110
Russ Cox82905362012-02-04 00:54:08 -0500111# Finally! Run the build.
Russ Cox9ff712e2009-11-10 19:20:34 -0800112
Russ Cox82905362012-02-04 00:54:08 -0500113echo '# Building C bootstrap tool.'
Russ Coxb5d81e52012-02-12 23:14:37 -0500114echo cmd/dist
Gustavo Niemeyer54f1e1b2012-02-09 20:47:12 -0200115export GOROOT="$(cd .. && pwd)"
116GOROOT_FINAL="${GOROOT_FINAL:-$GOROOT}"
117DEFGOROOT='-DGOROOT_FINAL="'"$GOROOT_FINAL"'"'
Shenghou Ma72801292012-03-13 03:34:22 +0800118
119mflag=""
120case "$GOHOSTARCH" in
121386) mflag=-m32;;
122amd64) mflag=-m64;;
123esac
Shenghou Maa5fe79e2013-04-16 13:30:52 -0700124if [ "$(uname)" == "Darwin" ]; then
125 # golang.org/issue/5261
126 mflag="$mflag -mmacosx-version-min=10.6"
127fi
Russ Cox6d888f12013-02-15 13:37:43 -0800128${CC:-gcc} $mflag -O2 -Wall -Werror -o cmd/dist/dist -Icmd/dist "$DEFGOROOT" cmd/dist/*.c
Shenghou Ma72801292012-03-13 03:34:22 +0800129
Rob Pikee8140bd2013-08-19 11:18:43 +1000130# -e doesn't propagate out of eval, so check success by hand.
131eval $(./cmd/dist/dist env -p || echo FAIL=true)
132if [ "$FAIL" = true ]; then
133 exit 1
134fi
135
Russ Cox2050a9e2012-01-30 23:43:46 -0500136echo
Russ Cox82905362012-02-04 00:54:08 -0500137
Russ Cox1c290fd2012-02-06 13:48:43 -0500138if [ "$1" = "--dist-tool" ]; then
139 # Stop after building dist tool.
Gustavo Niemeyer7e19e532012-03-02 02:45:01 -0300140 mkdir -p "$GOTOOLDIR"
David Symonds0ab3ea92012-02-15 09:06:24 +1100141 if [ "$2" != "" ]; then
142 cp cmd/dist/dist "$2"
143 fi
Gustavo Niemeyer7e19e532012-03-02 02:45:01 -0300144 mv cmd/dist/dist "$GOTOOLDIR"/dist
Russ Cox1c290fd2012-02-06 13:48:43 -0500145 exit 0
146fi
147
Russ Cox7b848c62012-02-13 22:31:51 -0500148echo "# Building compilers and Go bootstrap tool for host, $GOHOSTOS/$GOHOSTARCH."
Russ Cox2506fd42012-02-15 11:48:17 -0500149buildall="-a"
150if [ "$1" = "--no-clean" ]; then
151 buildall=""
152fi
Shenghou Ma6b188ef2013-10-01 23:44:20 -0400153./cmd/dist/dist bootstrap $buildall $GO_DISTFLAGS -v # builds go_bootstrap
Russ Cox2506fd42012-02-15 11:48:17 -0500154# Delay move of dist tool to now, because bootstrap may clear tool directory.
Gustavo Niemeyer7e19e532012-03-02 02:45:01 -0300155mv cmd/dist/dist "$GOTOOLDIR"/dist
156"$GOTOOLDIR"/go_bootstrap clean -i std
Russ Cox82905362012-02-04 00:54:08 -0500157echo
158
Russ Cox7b848c62012-02-13 22:31:51 -0500159if [ "$GOHOSTARCH" != "$GOARCH" -o "$GOHOSTOS" != "$GOOS" ]; then
160 echo "# Building packages and commands for host, $GOHOSTOS/$GOHOSTARCH."
Elias Naur2dc759d2014-02-06 09:11:00 -0800161 # CC_FOR_TARGET is recorded as the default compiler for the go tool. When building for the host, however,
162 # use the host compiler, CC, from `cmd/dist/dist env` instead.
163 CC=$CC GOOS=$GOHOSTOS GOARCH=$GOHOSTARCH \
Rob Pike4ce79092013-03-12 13:55:58 -0700164 "$GOTOOLDIR"/go_bootstrap install -ccflags "$GO_CCFLAGS" -gcflags "$GO_GCFLAGS" -ldflags "$GO_LDFLAGS" -v std
Russ Cox7b848c62012-02-13 22:31:51 -0500165 echo
166fi
167
168echo "# Building packages and commands for $GOOS/$GOARCH."
Elias Naur2dc759d2014-02-06 09:11:00 -0800169CC=$CC_FOR_TARGET "$GOTOOLDIR"/go_bootstrap install $GO_FLAGS -ccflags "$GO_CCFLAGS" -gcflags "$GO_GCFLAGS" -ldflags "$GO_LDFLAGS" -v std
Russ Cox82905362012-02-04 00:54:08 -0500170echo
171
Gustavo Niemeyer7e19e532012-03-02 02:45:01 -0300172rm -f "$GOTOOLDIR"/go_bootstrap
Russ Cox7b848c62012-02-13 22:31:51 -0500173
Russ Cox82905362012-02-04 00:54:08 -0500174if [ "$1" != "--no-banner" ]; then
Gustavo Niemeyer7e19e532012-03-02 02:45:01 -0300175 "$GOTOOLDIR"/dist banner
Russ Cox41a61652011-12-20 16:50:13 -0500176fi