...

Text file src/make.bash

Documentation: Index

     1#!/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
     6# See golang.org/s/go15bootstrap for an overview of the build process.
     7
     8# 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#
    22# GO_GCFLAGS: Additional go tool compile arguments to use when
    23# building the packages and commands.
    24#
    25# GO_LDFLAGS: Additional go tool link arguments to use when
    26# building the commands.
    27#
    28# 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# GO_LDSO: Sets the default dynamic linker/loader (ld.so) to be used
    38# by the internal linker.
    39#
    40# 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# CC_FOR_${GOOS}_${GOARCH}: Command line to run to compile C code for specified ${GOOS} and ${GOARCH}.
    47# (for example, CC_FOR_linux_arm)
    48# If this is not set, the build will use CC_FOR_TARGET if appropriate, or CC.
    49#
    50# CXX_FOR_TARGET: Command line to run to compile C++ code for GOARCH.
    51# This is used by cgo. Default is CXX, or, if that is not set,
    52# "g++" or "clang++".
    53#
    54# CXX_FOR_${GOOS}_${GOARCH}: Command line to run to compile C++ code for specified ${GOOS} and ${GOARCH}.
    55# (for example, CXX_FOR_linux_arm)
    56# If this is not set, the build will use CXX_FOR_TARGET if appropriate, or CXX.
    57#
    58# FC: Command line to run to compile Fortran code for GOARCH.
    59# This is used by cgo. Default is "gfortran".
    60#
    61# PKG_CONFIG: Path to pkg-config tool. Default is "pkg-config".
    62#
    63# GO_DISTFLAGS: extra flags to provide to "dist bootstrap".
    64# (Or just pass them to the make.bash command line.)
    65#
    66# GOBUILDTIMELOGFILE: If set, make.bash and all.bash write
    67# timing information to this file. Useful for profiling where the
    68# time goes when these scripts run.
    69#
    70# GOROOT_BOOTSTRAP: A working Go tree >= Go 1.4 for bootstrap.
    71# If $GOROOT_BOOTSTRAP/bin/go is missing, $(go env GOROOT) is
    72# tried for all "go" in $PATH. $HOME/go1.4 by default.
    73
    74set -e
    75
    76if [ ! -f run.bash ]; then
    77	echo 'make.bash must be run from $GOROOT/src' 1>&2
    78	exit 1
    79fi
    80
    81if [ "$GOBUILDTIMELOGFILE" != "" ]; then
    82	echo $(LC_TIME=C date) start make.bash >"$GOBUILDTIMELOGFILE"
    83fi
    84
    85# Test for Windows.
    86case "$(uname)" in
    87*MINGW* | *WIN32* | *CYGWIN*)
    88	echo 'ERROR: Do not use make.bash to build on Windows.'
    89	echo 'Use make.bat instead.'
    90	echo
    91	exit 1
    92	;;
    93esac
    94
    95# Test for bad ld.
    96if ld --version 2>&1 | grep 'gold.* 2\.20' >/dev/null; then
    97	echo 'ERROR: Your system has gold 2.20 installed.'
    98	echo 'This version is shipped by Ubuntu even though'
    99	echo 'it is known not to work on Ubuntu.'
   100	echo 'Binaries built with this linker are likely to fail in mysterious ways.'
   101	echo
   102	echo 'Run sudo apt-get remove binutils-gold.'
   103	echo
   104	exit 1
   105fi
   106
   107# Test for bad SELinux.
   108# On Fedora 16 the selinux filesystem is mounted at /sys/fs/selinux,
   109# so loop through the possible selinux mount points.
   110for se_mount in /selinux /sys/fs/selinux
   111do
   112	if [ -d $se_mount -a -f $se_mount/booleans/allow_execstack -a -x /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled; then
   113		if ! cat $se_mount/booleans/allow_execstack | grep -c '^1 1$' >> /dev/null ; then
   114			echo "WARNING: the default SELinux policy on, at least, Fedora 12 breaks "
   115			echo "Go. You can enable the features that Go needs via the following "
   116			echo "command (as root):"
   117			echo "  # setsebool -P allow_execstack 1"
   118			echo
   119			echo "Note that this affects your system globally! "
   120			echo
   121			echo "The build will continue in five seconds in case we "
   122			echo "misdiagnosed the issue..."
   123
   124			sleep 5
   125		fi
   126	fi
   127done
   128
   129# Test for debian/kFreeBSD.
   130# cmd/dist will detect kFreeBSD as freebsd/$GOARCH, but we need to
   131# disable cgo manually.
   132if [ "$(uname -s)" = "GNU/kFreeBSD" ]; then
   133	export CGO_ENABLED=0
   134fi
   135
   136# Test which linker/loader our system is using, if GO_LDSO is not set.
   137if [ -z "$GO_LDSO" ] && type readelf >/dev/null 2>&1; then
   138	if echo "int main() { return 0; }" | ${CC:-cc} -o ./test-musl-ldso -x c - >/dev/null 2>&1; then
   139		LDSO=$(readelf -l ./test-musl-ldso | grep 'interpreter:' | sed -e 's/^.*interpreter: \(.*\)[]]/\1/') >/dev/null 2>&1
   140		[ -z "$LDSO" ] || export GO_LDSO="$LDSO"
   141		rm -f ./test-musl-ldso
   142	fi
   143fi
   144
   145# Clean old generated file that will cause problems in the build.
   146rm -f ./runtime/runtime_defs.go
   147
   148# Finally!  Run the build.
   149
   150verbose=false
   151vflag=""
   152if [ "$1" = "-v" ]; then
   153	verbose=true
   154	vflag=-v
   155	shift
   156fi
   157
   158goroot_bootstrap_set=${GOROOT_BOOTSTRAP+"true"}
   159if [ -z "$GOROOT_BOOTSTRAP" ]; then
   160	GOROOT_BOOTSTRAP="$HOME/go1.4"
   161	for d in sdk/go1.17 go1.17; do
   162		if [ -d "$HOME/$d" ]; then
   163			GOROOT_BOOTSTRAP="$HOME/$d"
   164		fi
   165	done
   166fi
   167export GOROOT_BOOTSTRAP
   168
   169export GOROOT="$(cd .. && pwd)"
   170IFS=$'\n'; for go_exe in $(type -ap go); do
   171	if [ ! -x "$GOROOT_BOOTSTRAP/bin/go" ]; then
   172		goroot=$(GOROOT='' GOOS='' GOARCH='' "$go_exe" env GOROOT)
   173		if [ "$goroot" != "$GOROOT" ]; then
   174			if [ "$goroot_bootstrap_set" = "true" ]; then
   175				printf 'WARNING: %s does not exist, found %s from env\n' "$GOROOT_BOOTSTRAP/bin/go" "$go_exe" >&2
   176				printf 'WARNING: set %s as GOROOT_BOOTSTRAP\n' "$goroot" >&2
   177			fi
   178			GOROOT_BOOTSTRAP=$goroot
   179		fi
   180	fi
   181done; unset IFS
   182if [ ! -x "$GOROOT_BOOTSTRAP/bin/go" ]; then
   183	echo "ERROR: Cannot find $GOROOT_BOOTSTRAP/bin/go." >&2
   184	echo "Set \$GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4." >&2
   185	exit 1
   186fi
   187# Get the exact bootstrap toolchain version to help with debugging.
   188# We clear GOOS and GOARCH to avoid an ominous but harmless warning if
   189# the bootstrap doesn't support them.
   190GOROOT_BOOTSTRAP_VERSION=$(GOOS= GOARCH= GOEXPERIMENT= $GOROOT_BOOTSTRAP/bin/go version | sed 's/go version //')
   191echo "Building Go cmd/dist using $GOROOT_BOOTSTRAP. ($GOROOT_BOOTSTRAP_VERSION)"
   192if $verbose; then
   193	echo cmd/dist
   194fi
   195if [ "$GOROOT_BOOTSTRAP" = "$GOROOT" ]; then
   196	echo "ERROR: \$GOROOT_BOOTSTRAP must not be set to \$GOROOT" >&2
   197	echo "Set \$GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4." >&2
   198	exit 1
   199fi
   200rm -f cmd/dist/dist
   201GOROOT="$GOROOT_BOOTSTRAP" GOOS="" GOARCH="" GO111MODULE=off GOEXPERIMENT="" GOENV=off GOFLAGS="" "$GOROOT_BOOTSTRAP/bin/go" build -o cmd/dist/dist ./cmd/dist
   202
   203# -e doesn't propagate out of eval, so check success by hand.
   204eval $(./cmd/dist/dist env -p || echo FAIL=true)
   205if [ "$FAIL" = true ]; then
   206	exit 1
   207fi
   208
   209if $verbose; then
   210	echo
   211fi
   212
   213if [ "$1" = "--dist-tool" ]; then
   214	# Stop after building dist tool.
   215	mkdir -p "$GOTOOLDIR"
   216	if [ "$2" != "" ]; then
   217		cp cmd/dist/dist "$2"
   218	fi
   219	mv cmd/dist/dist "$GOTOOLDIR"/dist
   220	exit 0
   221fi
   222
   223# Run dist bootstrap to complete make.bash.
   224# Bootstrap installs a proper cmd/dist, built with the new toolchain.
   225# Throw ours, built with Go 1.4, away after bootstrap.
   226./cmd/dist/dist bootstrap -a $vflag $GO_DISTFLAGS "$@"
   227rm -f ./cmd/dist/dist
   228
   229# DO NOT ADD ANY NEW CODE HERE.
   230# The bootstrap+rm above are the final step of make.bash.
   231# If something must be added, add it to cmd/dist's cmdbootstrap,
   232# to avoid needing three copies in three different shell languages
   233# (make.bash, make.bat, make.rc).

View as plain text