From 5fbfda6a833f3bbc3d714459b5194f7fef1e0b43 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Fri, 6 Jul 2018 13:59:06 -0700 Subject: [PATCH] cmd/go: add LDFLAGS to cache ID when using cgo The cgo tool records the value of the CGO_LDFLAGS environment variable in the generated file, so that the linker can later read and use it. Therefore, we must add CGO_LDFLAGS to the cache ID, as otherwise changing CGO_LDFLAGS may cause a build result to be incorrectly read from the cache, producing a different final program. Change-Id: Ic89c1edc4069837451a36376710ca9b56fb87455 Reviewed-on: https://go-review.googlesource.com/122520 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- src/cmd/go/go_test.go | 24 ++++++++++++++++++++++++ src/cmd/go/internal/work/exec.go | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index 0186ad51d4..2434f0f5f2 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -6300,6 +6300,30 @@ echo $* >>`+tg.path("pkg-config.out")) } } +func TestCgoCache(t *testing.T) { + if !canCgo { + t.Skip("no cgo") + } + tg := testgo(t) + defer tg.cleanup() + tg.parallel() + tg.tempFile("src/x/a.go", `package main + // #ifndef VAL + // #define VAL 0 + // #endif + // int val = VAL; + import "C" + import "fmt" + func main() { fmt.Println(C.val) } + `) + tg.setenv("GOPATH", tg.path(".")) + exe := tg.path("x.exe") + tg.run("build", "-o", exe, "x") + tg.setenv("CGO_LDFLAGS", "-lnosuchlibraryexists") + tg.runFail("build", "-o", exe, "x") + tg.grepStderr(`nosuchlibraryexists`, "did not run linker with changed CGO_LDFLAGS") +} + // Issue 23982 func TestFilepathUnderCwdFormat(t *testing.T) { tg := testgo(t) diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go index e886020cb7..3842594454 100644 --- a/src/cmd/go/internal/work/exec.go +++ b/src/cmd/go/internal/work/exec.go @@ -208,8 +208,8 @@ func (b *Builder) buildActionID(a *Action) cache.ActionID { } if len(p.CgoFiles)+len(p.SwigFiles) > 0 { fmt.Fprintf(h, "cgo %q\n", b.toolID("cgo")) - cppflags, cflags, cxxflags, fflags, _, _ := b.CFlags(p) - fmt.Fprintf(h, "CC=%q %q %q\n", b.ccExe(), cppflags, cflags) + cppflags, cflags, cxxflags, fflags, ldflags, _ := b.CFlags(p) + fmt.Fprintf(h, "CC=%q %q %q %q\n", b.ccExe(), cppflags, cflags, ldflags) if len(p.CXXFiles)+len(p.SwigFiles) > 0 { fmt.Fprintf(h, "CXX=%q %q\n", b.cxxExe(), cxxflags) } -- 2.50.0