Commit be012e1e authored by Ian Lance Taylor's avatar Ian Lance Taylor

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 <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent a5f19812
......@@ -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
......
......@@ -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())
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment