From c14655da986e6c43268ad3edec1cf984a763694f Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 5 Jul 2023 15:11:41 -0400 Subject: [PATCH] cmd/compile: add -env key=value flag This flag is not terribly useful with the go command, which will pass all environment variables through to subprocesses it invokes, but it can be useful in other build systems, notably blaze and bazel, to pass compiler-debugging variables like GOSSAFUNC through to the compiler. We have been maintaining this as a patch against Google's internal toolchain for many years, and it has proven useful in those non-go-command contexts. Change-Id: Ic123193319f3c838a694eda2575347c516b85ac7 Reviewed-on: https://go-review.googlesource.com/c/go/+/507977 Reviewed-by: Cherry Mui TryBot-Result: Gopher Robot Run-TryBot: Russ Cox --- src/cmd/compile/internal/base/flag.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/cmd/compile/internal/base/flag.go b/src/cmd/compile/internal/base/flag.go index 7bd27c92a3..6d9497c3a9 100644 --- a/src/cmd/compile/internal/base/flag.go +++ b/src/cmd/compile/internal/base/flag.go @@ -98,6 +98,7 @@ type CmdFlags struct { DwarfLocationLists *bool "help:\"add location lists to DWARF in optimized mode\"" // &Ctxt.Flag_locationlists, set below Dynlink *bool "help:\"support references to Go symbols defined in other shared libraries\"" // &Ctxt.Flag_dynlink, set below EmbedCfg func(string) "help:\"read go:embed configuration from `file`\"" + Env func(string) "help:\"add `definition` of the form key=value to environment\"" GenDwarfInl int "help:\"generate DWARF inline info records\"" // 0=disabled, 1=funcs, 2=funcs+formals/locals GoVersion string "help:\"required version of the runtime\"" ImportCfg func(string) "help:\"read import configuration from `file`\"" @@ -143,6 +144,14 @@ type CmdFlags struct { } } +func addEnv(s string) { + i := strings.Index(s, "=") + if i < 0 { + log.Fatal("-env argument must be of the form key=value") + } + os.Setenv(s[:i], s[i+1:]) +} + // ParseFlags parses the command-line flags into Flag. func ParseFlags() { Flag.I = addImportDir @@ -158,6 +167,7 @@ func ParseFlags() { *Flag.DwarfLocationLists = true Flag.Dynlink = &Ctxt.Flag_dynlink Flag.EmbedCfg = readEmbedCfg + Flag.Env = addEnv Flag.GenDwarfInl = 2 Flag.ImportCfg = readImportCfg Flag.CoverageCfg = readCoverageCfg -- 2.51.0