]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: add -env key=value flag
authorRuss Cox <rsc@golang.org>
Wed, 5 Jul 2023 19:11:41 +0000 (15:11 -0400)
committerRuss Cox <rsc@golang.org>
Thu, 10 Aug 2023 15:53:49 +0000 (15:53 +0000)
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 <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>

src/cmd/compile/internal/base/flag.go

index 7bd27c92a3db65526d108a91b96eb8463797a914..6d9497c3a93e5257319408eea885f3e3d641638b 100644 (file)
@@ -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