]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: fix swig support and run swig tests during run.bash
authorRuss Cox <rsc@golang.org>
Thu, 16 Nov 2017 02:14:21 +0000 (21:14 -0500)
committerRuss Cox <rsc@golang.org>
Thu, 16 Nov 2017 17:19:19 +0000 (17:19 +0000)
(The tests only run when swig is already installed on the local system.)

Change-Id: I172d106a68cfc746a1058f5a4bcf6761bab88912
Reviewed-on: https://go-review.googlesource.com/78175
Reviewed-by: Ian Lance Taylor <iant@golang.org>
misc/swig/stdio/file.go [new file with mode: 0644]
misc/swig/stdio/file_test.go
src/cmd/dist/test.go
src/cmd/go/internal/work/exec.go

diff --git a/misc/swig/stdio/file.go b/misc/swig/stdio/file.go
new file mode 100644 (file)
index 0000000..a582f77
--- /dev/null
@@ -0,0 +1,15 @@
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// This file is here just to cause problems.
+// file.swig turns into a file also named file.go.
+// Make sure cmd/go keeps them separate
+// when both are passed to cgo.
+
+package file
+
+//int F(void) { return 1; }
+import "C"
+
+func F() int { return int(C.F()) }
index b1a520e6bc9cd5e32c69f032a887a697ab0b7474..aea92aafd55775a43f4058930864449af7c15a43 100644 (file)
@@ -20,3 +20,9 @@ func TestRead(t *testing.T) {
                t.Error("fclose failed")
        }
 }
+
+func TestF(t *testing.T) {
+       if x := F(); x != 1 {
+               t.Fatalf("x = %d, want 1", x)
+       }
+}
index 9b4cd819c168af148e708e99b17472660345987f..69c85f046e56be90c1443de522233296965ca7aa 100644 (file)
@@ -573,6 +573,26 @@ func (t *tester) registerTests() {
                                },
                        })
                }
+               if swig, _ := exec.LookPath("swig"); swig != "" {
+                       t.tests = append(t.tests, distTest{
+                               name:    "swig_stdio",
+                               heading: "../misc/swig/stdio",
+                               fn: func(dt *distTest) error {
+                                       t.addCmd(dt, "misc/swig/stdio", "go", "test")
+                                       return nil
+                               },
+                       })
+                       if cxx, _ := exec.LookPath(compilerEnvLookup(defaultcxx, goos, goarch)); cxx != "" {
+                               t.tests = append(t.tests, distTest{
+                                       name:    "swig_callback",
+                                       heading: "../misc/swig/callback",
+                                       fn: func(dt *distTest) error {
+                                               t.addCmd(dt, "misc/swig/callback", "go", "test")
+                                               return nil
+                                       },
+                               })
+                       }
+               }
        }
        if t.cgoEnabled {
                t.tests = append(t.tests, distTest{
index 43dbf40e51f1eef69da58a847a3c6d4b01b2393b..1ffa14249ec3f378ec22d6ac8e37268e90801e4e 100644 (file)
@@ -363,7 +363,7 @@ func (b *Builder) build(a *Action) (err error) {
                }
        }
 
-       var gofiles, cgofiles, objdirCgofiles, cfiles, sfiles, cxxfiles, objects, cgoObjects, pcCFLAGS, pcLDFLAGS []string
+       var gofiles, cgofiles, cfiles, sfiles, cxxfiles, objects, cgoObjects, pcCFLAGS, pcLDFLAGS []string
 
        gofiles = append(gofiles, a.Package.GoFiles...)
        cgofiles = append(cgofiles, a.Package.CgoFiles...)
@@ -385,7 +385,7 @@ func (b *Builder) build(a *Action) (err error) {
                if err != nil {
                        return err
                }
-               objdirCgofiles = append(objdirCgofiles, outGo...)
+               cgofiles = append(cgofiles, outGo...)
                cfiles = append(cfiles, outC...)
                cxxfiles = append(cxxfiles, outCXX...)
        }
@@ -460,7 +460,7 @@ func (b *Builder) build(a *Action) (err error) {
                        sfiles = nil
                }
 
-               outGo, outObj, err := b.cgo(a, base.Tool("cgo"), objdir, pcCFLAGS, pcLDFLAGS, mkAbsFiles(a.Package.Dir, cgofiles), objdirCgofiles, gccfiles, cxxfiles, a.Package.MFiles, a.Package.FFiles)
+               outGo, outObj, err := b.cgo(a, base.Tool("cgo"), objdir, pcCFLAGS, pcLDFLAGS, mkAbsFiles(a.Package.Dir, cgofiles), gccfiles, cxxfiles, a.Package.MFiles, a.Package.FFiles)
                if err != nil {
                        return err
                }
@@ -1843,7 +1843,7 @@ func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, l
 
 var cgoRe = regexp.MustCompile(`[/\\:]`)
 
-func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgofiles, objdirCgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) {
+func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) {
        p := a.Package
        cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS := b.CFlags(p)
        cgoCPPFLAGS = append(cgoCPPFLAGS, pcCFLAGS...)
@@ -1874,20 +1874,6 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
        // Allows including _cgo_export.h from .[ch] files in the package.
        cgoCPPFLAGS = append(cgoCPPFLAGS, "-I", objdir)
 
-       // If we have cgo files in the object directory, then copy any
-       // other cgo files into the object directory, and pass a
-       // -srcdir option to cgo.
-       var srcdirarg []string
-       if len(objdirCgofiles) > 0 {
-               for _, fn := range cgofiles {
-                       if err := b.copyFile(a, objdir+filepath.Base(fn), filepath.Join(p.Dir, fn), 0666, false); err != nil {
-                               return nil, nil, err
-                       }
-               }
-               cgofiles = append(cgofiles, objdirCgofiles...)
-               srcdirarg = []string{"-srcdir", objdir}
-       }
-
        // cgo
        // TODO: CGO_FLAGS?
        gofiles := []string{objdir + "_cgo_gotypes.go"}
@@ -1937,7 +1923,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
                cgoflags = append(cgoflags, "-exportheader="+objdir+"_cgo_install.h")
        }
 
-       if err := b.run(a, p.Dir, p.ImportPath, cgoenv, cfg.BuildToolexec, cgoExe, srcdirarg, "-objdir", objdir, "-importpath", p.ImportPath, cgoflags, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil {
+       if err := b.run(a, p.Dir, p.ImportPath, cgoenv, cfg.BuildToolexec, cgoExe, "-objdir", objdir, "-importpath", p.ImportPath, cgoflags, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil {
                return nil, nil, err
        }
        outGo = append(outGo, gofiles...)
@@ -2264,7 +2250,20 @@ func (b *Builder) swigOne(a *Action, p *load.Package, file, objdir string, pcCFL
                b.showOutput(a, p.Dir, p.ImportPath, b.processOutput(out)) // swig warning
        }
 
-       return goFile, objdir + gccBase + gccExt, nil
+       // If the input was x.swig, the output is x.go in the objdir.
+       // But there might be an x.go in the original dir too, and if it
+       // uses cgo as well, cgo will be processing both and will
+       // translate both into x.cgo1.go in the objdir, overwriting one.
+       // Rename x.go to _x_swig.go to avoid this problem.
+       // We ignore files in the original dir that begin with underscore
+       // so _x_swig.go cannot conflict with an original file we were
+       // going to compile.
+       goFile = objdir + goFile
+       newGoFile := objdir + "_" + base + "_swig.go"
+       if err := os.Rename(goFile, newGoFile); err != nil {
+               return "", "", err
+       }
+       return newGoFile, objdir + gccBase + gccExt, nil
 }
 
 // disableBuildID adjusts a linker command line to avoid creating a