make.bash raw

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