From be012e1e2e5fb24c765157fd2ca7e05675d6a1c2 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Thu, 19 Apr 2018 12:56:29 -0700 Subject: [PATCH] cmd/cgo: don't use absolute paths in the export header file We were using absolute paths in the #line directives in the export header file. This makes the header file change if you move GOPATH. The absolute paths aren't helpful for the final user, which is some C program elsewhere. Fixes #24945 Change-Id: I2da32c9b477df578bd5087435a03fe97abe462e3 Reviewed-on: https://go-review.googlesource.com/108315 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- misc/cgo/testcarchive/carchive_test.go | 31 ++++++++++++++++++++++++++ src/cmd/cgo/out.go | 10 ++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/misc/cgo/testcarchive/carchive_test.go b/misc/cgo/testcarchive/carchive_test.go index 06e74fa2fe..71232305f6 100644 --- a/misc/cgo/testcarchive/carchive_test.go +++ b/misc/cgo/testcarchive/carchive_test.go @@ -13,6 +13,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "strings" "syscall" "testing" @@ -166,6 +167,28 @@ func testInstall(t *testing.T, exe, libgoa, libgoh string, buildcmd ...string) { t.Logf("%s", out) t.Fatal(err) } + + checkLineComments(t, libgoh) +} + +var badLineRegexp = regexp.MustCompile(`(?m)^#line [0-9]+ "/.*$`) + +// checkLineComments checks that the export header generated by +// -buildmode=c-archive doesn't have any absolute paths in the #line +// comments. We don't want those paths because they are unhelpful for +// the user and make the files change based on details of the location +// of GOPATH. +func checkLineComments(t *testing.T, hdrname string) { + hdr, err := ioutil.ReadFile(hdrname) + if err != nil { + if !os.IsNotExist(err) { + t.Error(err) + } + return + } + if line := badLineRegexp.Find(hdr); line != nil { + t.Errorf("bad #line directive with absolute path in %s: %q", hdrname, line) + } } func TestInstall(t *testing.T) { @@ -209,6 +232,7 @@ func TestEarlySignalHandler(t *testing.T) { t.Logf("%s", out) t.Fatal(err) } + checkLineComments(t, "libgo2.h") ccArgs := append(cc, "-o", "testp"+exeSuffix, "main2.c", "libgo2.a") if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil { @@ -238,6 +262,7 @@ func TestSignalForwarding(t *testing.T) { t.Logf("%s", out) t.Fatal(err) } + checkLineComments(t, "libgo2.h") ccArgs := append(cc, "-o", "testp"+exeSuffix, "main5.c", "libgo2.a") if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil { @@ -278,6 +303,7 @@ func TestSignalForwardingExternal(t *testing.T) { t.Logf("%s", out) t.Fatal(err) } + checkLineComments(t, "libgo2.h") ccArgs := append(cc, "-o", "testp"+exeSuffix, "main5.c", "libgo2.a") if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil { @@ -390,6 +416,7 @@ func TestOsSignal(t *testing.T) { t.Logf("%s", out) t.Fatal(err) } + checkLineComments(t, "libgo3.h") ccArgs := append(cc, "-o", "testp"+exeSuffix, "main3.c", "libgo3.a") if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil { @@ -422,6 +449,7 @@ func TestSigaltstack(t *testing.T) { t.Logf("%s", out) t.Fatal(err) } + checkLineComments(t, "libgo4.h") ccArgs := append(cc, "-o", "testp"+exeSuffix, "main4.c", "libgo4.a") if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil { @@ -473,6 +501,7 @@ func TestExtar(t *testing.T) { t.Logf("%s", out) t.Fatal(err) } + checkLineComments(t, "libgo4.h") if _, err := os.Stat("testar.ran"); err != nil { if os.IsNotExist(err) { @@ -573,6 +602,7 @@ func TestSIGPROF(t *testing.T) { t.Logf("%s", out) t.Fatal(err) } + checkLineComments(t, "libgo6.h") ccArgs := append(cc, "-o", "testp6"+exeSuffix, "main6.c", "libgo6.a") if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil { @@ -611,6 +641,7 @@ func TestCompileWithoutShared(t *testing.T) { if err != nil { t.Fatal(err) } + checkLineComments(t, "libgo2.h") exe := "./testnoshared" + exeSuffix diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go index 070429f442..399e96d01e 100644 --- a/src/cmd/cgo/out.go +++ b/src/cmd/cgo/out.go @@ -16,6 +16,7 @@ import ( "io" "os" "path/filepath" + "regexp" "sort" "strings" ) @@ -1154,8 +1155,15 @@ func (p *Package) writeExportHeader(fgcch io.Writer) { fmt.Fprintf(fgcch, "/* package %s */\n\n", pkg) fmt.Fprintf(fgcch, "%s\n", builtinExportProlog) + // Remove absolute paths from #line comments in the preamble. + // They aren't useful for people using the header file, + // and they mean that the header files change based on the + // exact location of GOPATH. + re := regexp.MustCompile(`(?m)^(#line\s+[0-9]+\s+")[^"]*[/\\]([^"]*")`) + preamble := re.ReplaceAllString(p.Preamble, "$1$2") + fmt.Fprintf(fgcch, "/* Start of preamble from import \"C\" comments. */\n\n") - fmt.Fprintf(fgcch, "%s\n", p.Preamble) + fmt.Fprintf(fgcch, "%s\n", preamble) fmt.Fprintf(fgcch, "\n/* End of preamble from import \"C\" comments. */\n\n") fmt.Fprintf(fgcch, "%s\n", p.gccExportHeaderProlog()) -- 2.50.0