1
0
mirror of https://github.com/golang/go synced 2024-11-23 09:30:03 -07:00
go/src/make.rc
Russ Cox c1a4fc3b36 cmd/go: add $GOFLAGS environment variable
People sometimes want to turn on a particular go command flag by default.
In Go 1.11 we have at least two different cases where users may need this.

1. Linking can be noticeably slower on underpowered systems
due to DWARF, and users may want to set -ldflags=-w by default.

2. For modules, some users or CI systems will want vendoring always,
so they want -getmode=vendor (soon to be -mod=vendor) by default.

This CL generalizes the problem to “set default flags for the go command.”

$GOFLAGS can be a space-separated list of flag settings, but each
space-separated entry in the list must be a standalone flag.
That is, you must do 'GOFLAGS=-ldflags=-w' not 'GOFLAGS=-ldflags -w'.
The latter would mean to pass -w to go commands that understand it
(if any do; if not, it's an error to mention it).

For #26074.
For #26318.
Fixes #26585.

Change-Id: I428f79c1fbfb9e41e54d199c68746405aed2319c
Reviewed-on: https://go-review.googlesource.com/126656
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
2018-08-01 00:35:21 +00:00

109 lines
3.1 KiB
Bash
Executable File

#!/bin/rc -e
# Copyright 2012 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
# See golang.org/s/go15bootstrap for an overview of the build process.
# Environment variables that control make.rc:
#
# GOROOT_FINAL: The expected final Go root, baked into binaries.
# The default is the location of the Go tree during the build.
#
# GOHOSTARCH: The architecture for host tools (compilers and
# binaries). Binaries of this type must be executable on the current
# system, so the only common reason to set this is to set
# GOHOSTARCH=386 on an amd64 machine.
#
# GOARCH: The target architecture for installed packages and tools.
#
# GOOS: The target operating system for installed packages and tools.
#
# GO_GCFLAGS: Additional go tool compile arguments to use when
# building the packages and commands.
#
# GO_LDFLAGS: Additional go tool link arguments to use when
# building the commands.
#
# CGO_ENABLED: Controls cgo usage during the build. Set it to 1
# to include all cgo related files, .c and .go file with "cgo"
# build directive, in the build. Set it to 0 to ignore them.
rfork e
if(! test -f run.rc){
echo 'make.rc must be run from $GOROOT/src' >[1=2]
exit wrongdir
}
# Clean old generated file that will cause problems in the build.
rm -f ./runtime/runtime_defs.go
# Determine the host compiler toolchain.
eval `{grep '^(CC|LD|O)=' /$objtype/mkfile}
vflag=()
if(~ $1 -v) {
vflag=(-v)
shift
}
GOFLAGS=()
GOROOT = `{cd .. && pwd}
if(! ~ $#GOROOT_BOOTSTRAP 1)
GOROOT_BOOTSTRAP = $home/go1.4
for(p in $path){
if(! test -x $GOROOT_BOOTSTRAP/bin/go){
if(go_exe = `{path=$p whatis go}){
goroot = `{GOROOT='' $go_exe env GOROOT}
if(! ~ $goroot $GOROOT){
GOROOT_BOOTSTRAP = $goroot
}
}
}
}
if(! test -x $GOROOT_BOOTSTRAP/bin/go){
echo 'ERROR: Cannot find '$GOROOT_BOOTSTRAP'/bin/go.' >[1=2]
echo 'Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.' >[1=2]
exit bootstrap
}
if(~ $GOROOT_BOOTSTRAP $GOROOT){
echo 'ERROR: $GOROOT_BOOTSTRAP must not be set to $GOROOT' >[1=2]
echo 'Set $GOROOT_BOOTSTRAP to a working Go tree >= Go 1.4.' >[1=2]
exit bootstrap
}
echo 'Building Go cmd/dist using '^$GOROOT_BOOTSTRAP
if(~ $#vflag 1)
echo cmd/dist
GOROOT=$GOROOT_BOOTSTRAP GOOS='' GOARCH='' $GOROOT_BOOTSTRAP/bin/go build -o cmd/dist/dist ./cmd/dist
eval `{./cmd/dist/dist env -9}
if(~ $#vflag 1)
echo
if(~ $1 --dist-tool){
# Stop after building dist tool.
mkdir -p $GOTOOLDIR
if(! ~ $2 '')
cp cmd/dist/dist $2
mv cmd/dist/dist $GOTOOLDIR/dist
exit
}
buildall = -a
if(~ $1 --no-clean) {
buildall = ()
shift
}
# Run dist bootstrap to complete make.bash.
# Bootstrap installs a proper cmd/dist, built with the new toolchain.
# Throw ours, built with Go 1.4, away after bootstrap.
./cmd/dist/dist bootstrap $vflag $buildall $*
rm -f ./cmd/dist/dist
# DO NOT ADD ANY NEW CODE HERE.
# The bootstrap+rm above are the final step of make.rc.
# If something must be added, add it to cmd/dist's cmdbootstrap,
# to avoid needing three copies in three different shell languages
# (make.bash, make.bat, make.rc).