]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add LDFLAGS to cache ID when using cgo
authorIan Lance Taylor <iant@golang.org>
Fri, 6 Jul 2018 20:59:06 +0000 (13:59 -0700)
committerIan Lance Taylor <iant@golang.org>
Mon, 9 Jul 2018 23:39:59 +0000 (23:39 +0000)
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 <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/cmd/go/go_test.go
src/cmd/go/internal/work/exec.go

index 0186ad51d47fbe2f25c90b9565ab667f49d6229f..2434f0f5f255a946881a89fc52e0c8ed3c400f35 100644 (file)
@@ -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)
index e886020cb7fc34bb5a0cef2024cdca111a934e8e..38425944542c160d9462ad8d368f9bbdff540921 100644 (file)
@@ -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)
                }