"os"
"os/exec"
"path/filepath"
+ "regexp"
"strings"
"syscall"
"testing"
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) {
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 {
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 {
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 {
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 {
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 {
t.Logf("%s", out)
t.Fatal(err)
}
+ checkLineComments(t, "libgo4.h")
if _, err := os.Stat("testar.ran"); err != nil {
if os.IsNotExist(err) {
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 {
if err != nil {
t.Fatal(err)
}
+ checkLineComments(t, "libgo2.h")
exe := "./testnoshared" + exeSuffix
"io"
"os"
"path/filepath"
+ "regexp"
"sort"
"strings"
)
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())