defer os.RemoveAll(GOPATH)
os.Setenv("GOPATH", GOPATH)
- // Copy testdata into GOPATH/src/testarchive, along with a go.mod file
- // declaring the same path.
modRoot := filepath.Join(GOPATH, "src", "testcshared")
if err := overlayDir(modRoot, "testdata"); err != nil {
log.Panic(err)
}
func createHeaders() error {
- args := []string{"go", "install", "-i", "-buildmode=c-shared",
- "-installsuffix", "testcshared", "./libgo"}
+ // The 'cgo' command generates a number of additional artifacts,
+ // but we're only interested in the header.
+ // Shunt the rest of the outputs to a temporary directory.
+ objDir, err := ioutil.TempDir("", "testcshared_obj")
+ if err != nil {
+ return err
+ }
+ defer os.RemoveAll(objDir)
+
+ // Generate a C header file for p, which is a non-main dependency
+ // of main package libgo.
+ //
+ // TODO(golang.org/issue/35715): This should be simpler.
+ args := []string{"go", "tool", "cgo",
+ "-objdir", objDir,
+ "-exportheader", "p.h",
+ filepath.Join(".", "p", "p.go")}
cmd := exec.Command(args[0], args[1:]...)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("command failed: %v\n%v\n%s\n", args, err, out)
}
+ // Generate a C header file for libgo itself.
+ args = []string{"go", "install", "-buildmode=c-shared",
+ "-installsuffix", "testcshared", "./libgo"}
+ cmd = exec.Command(args[0], args[1:]...)
+ out, err = cmd.CombinedOutput()
+ if err != nil {
+ return fmt.Errorf("command failed: %v\n%v\n%s\n", args, err, out)
+ }
+
args = []string{"go", "build", "-buildmode=c-shared",
"-installsuffix", "testcshared",
"-o", libgoname,
}
}
-// Test that installing a second time recreates the header files.
+// Test that installing a second time recreates the header file.
func TestCachedInstall(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "cshared")
if err != nil {
env := append(os.Environ(), "GOPATH="+tmpdir, "GOBIN="+filepath.Join(tmpdir, "bin"))
- buildcmd := []string{"go", "install", "-x", "-i", "-buildmode=c-shared", "-installsuffix", "testcshared", "./libgo"}
+ buildcmd := []string{"go", "install", "-x", "-buildmode=c-shared", "-installsuffix", "testcshared", "./libgo"}
cmd := exec.Command(buildcmd[0], buildcmd[1:]...)
cmd.Dir = filepath.Join(tmpdir, "src", "testcshared")
if libgoh == "" {
t.Fatal("libgo.h not installed")
}
- if ph == "" {
- t.Fatal("p.h not installed")
- }
if err := os.Remove(libgoh); err != nil {
t.Fatal(err)
}
- if err := os.Remove(ph); err != nil {
- t.Fatal(err)
- }
cmd = exec.Command(buildcmd[0], buildcmd[1:]...)
cmd.Dir = filepath.Join(tmpdir, "src", "testcshared")
if _, err := os.Stat(libgoh); err != nil {
t.Errorf("libgo.h not installed in second run: %v", err)
}
- if _, err := os.Stat(ph); err != nil {
- t.Errorf("p.h not installed in second run: %v", err)
- }
}
// copyFile copies src to dst.