]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: use cgo -srcdir when using SWIG
authorIan Lance Taylor <iant@golang.org>
Tue, 1 Nov 2016 19:06:22 +0000 (12:06 -0700)
committerIan Lance Taylor <iant@golang.org>
Wed, 2 Nov 2016 13:37:24 +0000 (13:37 +0000)
SWIG generates cgo input files in the work directory. When those files
are passed directly to cgo, cgo generates long file names that include
the object directory (with slashes replaced by underscores). Instead,
use cgo's new -srcdir option to give it short file names.

When using both SWIG and cgo, copy the cgo files into the object
directory first.

Use a shorter object file name when compiling the C file generated by
SWIG.

Fixes #17070.

Change-Id: Ic558603f1731636d9999f3130ad0224b24bd7dcb
Reviewed-on: https://go-review.googlesource.com/32485
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
src/cmd/go/build.go

index e3a23b8d266f24b4c431baf14edbf3ce1f1e8338..8c438f6f6f3317306610f311a691f8ae9a262d0c 100644 (file)
@@ -1430,7 +1430,7 @@ func (b *builder) build(a *action) (err error) {
                }
        }
 
-       var gofiles, cgofiles, cfiles, sfiles, cxxfiles, objects, cgoObjects, pcCFLAGS, pcLDFLAGS []string
+       var gofiles, cgofiles, objdirCgofiles, cfiles, sfiles, cxxfiles, objects, cgoObjects, pcCFLAGS, pcLDFLAGS []string
 
        gofiles = append(gofiles, a.p.GoFiles...)
        cgofiles = append(cgofiles, a.p.CgoFiles...)
@@ -1452,7 +1452,7 @@ func (b *builder) build(a *action) (err error) {
                if err != nil {
                        return err
                }
-               cgofiles = append(cgofiles, outGo...)
+               objdirCgofiles = append(objdirCgofiles, outGo...)
                cfiles = append(cfiles, outC...)
                cxxfiles = append(cxxfiles, outCXX...)
        }
@@ -1487,7 +1487,7 @@ func (b *builder) build(a *action) (err error) {
                if a.cgo != nil && a.cgo.target != "" {
                        cgoExe = a.cgo.target
                }
-               outGo, outObj, err := b.cgo(a.p, cgoExe, obj, pcCFLAGS, pcLDFLAGS, cgofiles, gccfiles, cxxfiles, a.p.MFiles, a.p.FFiles)
+               outGo, outObj, err := b.cgo(a, cgoExe, obj, pcCFLAGS, pcLDFLAGS, cgofiles, objdirCgofiles, gccfiles, cxxfiles, a.p.MFiles, a.p.FFiles)
                if err != nil {
                        return err
                }
@@ -3209,7 +3209,8 @@ func (b *builder) cflags(p *Package) (cppflags, cflags, cxxflags, fflags, ldflag
 
 var cgoRe = regexp.MustCompile(`[/\\:]`)
 
-func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) {
+func (b *builder) cgo(a *action, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofiles, objdirCgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) {
+       p := a.p
        cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS := b.cflags(p)
        cgoCPPFLAGS = append(cgoCPPFLAGS, pcCFLAGS...)
        cgoLDFLAGS = append(cgoLDFLAGS, pcLDFLAGS...)
@@ -3239,6 +3240,20 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
        // Allows including _cgo_export.h from .[ch] files in the package.
        cgoCPPFLAGS = append(cgoCPPFLAGS, "-I", obj)
 
+       // 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, obj+filepath.Base(fn), filepath.Join(p.Dir, fn), 0666, false); err != nil {
+                               return nil, nil, err
+                       }
+               }
+               cgofiles = append(cgofiles, objdirCgofiles...)
+               srcdirarg = []string{"-srcdir", obj}
+       }
+
        // cgo
        // TODO: CGO_FLAGS?
        gofiles := []string{obj + "_cgo_gotypes.go"}
@@ -3288,7 +3303,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
                cgoflags = append(cgoflags, "-exportheader="+obj+"_cgo_install.h")
        }
 
-       if err := b.run(p.Dir, p.ImportPath, cgoenv, buildToolExec, cgoExe, "-objdir", obj, "-importpath", p.ImportPath, cgoflags, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil {
+       if err := b.run(p.Dir, p.ImportPath, cgoenv, buildToolExec, cgoExe, srcdirarg, "-objdir", obj, "-importpath", p.ImportPath, cgoflags, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil {
                return nil, nil, err
        }
        outGo = append(outGo, gofiles...)
@@ -3304,7 +3319,8 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
        }
 
        for _, file := range gccfiles {
-               ofile := obj + cgoRe.ReplaceAllString(file[:len(file)-1], "_") + "o"
+               base := filepath.Base(file)
+               ofile := obj + cgoRe.ReplaceAllString(base[:len(base)-1], "_") + "o"
                if err := b.gcc(p, ofile, cflags, file); err != nil {
                        return nil, nil, err
                }
@@ -3314,7 +3330,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
        cxxflags := stringList(cgoCPPFLAGS, cgoCXXFLAGS)
        for _, file := range gxxfiles {
                // Append .o to the file, just in case the pkg has file.c and file.cpp
-               ofile := obj + cgoRe.ReplaceAllString(file, "_") + ".o"
+               ofile := obj + cgoRe.ReplaceAllString(filepath.Base(file), "_") + ".o"
                if err := b.gxx(p, ofile, cxxflags, file); err != nil {
                        return nil, nil, err
                }
@@ -3323,7 +3339,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
 
        for _, file := range mfiles {
                // Append .o to the file, just in case the pkg has file.c and file.m
-               ofile := obj + cgoRe.ReplaceAllString(file, "_") + ".o"
+               ofile := obj + cgoRe.ReplaceAllString(filepath.Base(file), "_") + ".o"
                if err := b.gcc(p, ofile, cflags, file); err != nil {
                        return nil, nil, err
                }
@@ -3333,7 +3349,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
        fflags := stringList(cgoCPPFLAGS, cgoFFLAGS)
        for _, file := range ffiles {
                // Append .o to the file, just in case the pkg has file.c and file.f
-               ofile := obj + cgoRe.ReplaceAllString(file, "_") + ".o"
+               ofile := obj + cgoRe.ReplaceAllString(filepath.Base(file), "_") + ".o"
                if err := b.gfortran(p, ofile, fflags, file); err != nil {
                        return nil, nil, err
                }
@@ -3669,7 +3685,7 @@ func (b *builder) swigOne(p *Package, file, obj string, pcCFLAGS []string, cxx b
                b.showOutput(p.Dir, p.ImportPath, b.processOutput(out)) // swig warning
        }
 
-       return obj + goFile, obj + gccBase + gccExt, nil
+       return goFile, obj + gccBase + gccExt, nil
 }
 
 // disableBuildID adjusts a linker command line to avoid creating a