]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: don't use absolute paths in the export header file
authorIan Lance Taylor <iant@golang.org>
Thu, 19 Apr 2018 19:56:29 +0000 (12:56 -0700)
committerIan Lance Taylor <iant@golang.org>
Wed, 25 Apr 2018 00:36:09 +0000 (00:36 +0000)
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 <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
misc/cgo/testcarchive/carchive_test.go
src/cmd/cgo/out.go

index 06e74fa2fee26ce0ac0230334ea281e79430136d..71232305f60b8434f3914859dc3c7cdf7c082853 100644 (file)
@@ -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
 
index 070429f442db0bffab2e8aefbf01f526b0a402ea..399e96d01e0e704aac6bc0374a988e53cc9e16ca 100644 (file)
@@ -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())