Commit 7bf0fc9f authored by Ian Lance Taylor's avatar Ian Lance Taylor

cmd/go: use cgo -srcdir when using SWIG

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: default avatarDavid Crawshaw <crawshaw@golang.org>
parent e22b5efb
...@@ -1430,7 +1430,7 @@ func (b *builder) build(a *action) (err error) { ...@@ -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...) gofiles = append(gofiles, a.p.GoFiles...)
cgofiles = append(cgofiles, a.p.CgoFiles...) cgofiles = append(cgofiles, a.p.CgoFiles...)
...@@ -1452,7 +1452,7 @@ func (b *builder) build(a *action) (err error) { ...@@ -1452,7 +1452,7 @@ func (b *builder) build(a *action) (err error) {
if err != nil { if err != nil {
return err return err
} }
cgofiles = append(cgofiles, outGo...) objdirCgofiles = append(objdirCgofiles, outGo...)
cfiles = append(cfiles, outC...) cfiles = append(cfiles, outC...)
cxxfiles = append(cxxfiles, outCXX...) cxxfiles = append(cxxfiles, outCXX...)
} }
...@@ -1487,7 +1487,7 @@ func (b *builder) build(a *action) (err error) { ...@@ -1487,7 +1487,7 @@ func (b *builder) build(a *action) (err error) {
if a.cgo != nil && a.cgo.target != "" { if a.cgo != nil && a.cgo.target != "" {
cgoExe = 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 { if err != nil {
return err return err
} }
...@@ -3209,7 +3209,8 @@ func (b *builder) cflags(p *Package) (cppflags, cflags, cxxflags, fflags, ldflag ...@@ -3209,7 +3209,8 @@ func (b *builder) cflags(p *Package) (cppflags, cflags, cxxflags, fflags, ldflag
var cgoRe = regexp.MustCompile(`[/\\:]`) 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, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS := b.cflags(p)
cgoCPPFLAGS = append(cgoCPPFLAGS, pcCFLAGS...) cgoCPPFLAGS = append(cgoCPPFLAGS, pcCFLAGS...)
cgoLDFLAGS = append(cgoLDFLAGS, pcLDFLAGS...) cgoLDFLAGS = append(cgoLDFLAGS, pcLDFLAGS...)
...@@ -3239,6 +3240,20 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi ...@@ -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. // Allows including _cgo_export.h from .[ch] files in the package.
cgoCPPFLAGS = append(cgoCPPFLAGS, "-I", obj) 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 // cgo
// TODO: CGO_FLAGS? // TODO: CGO_FLAGS?
gofiles := []string{obj + "_cgo_gotypes.go"} gofiles := []string{obj + "_cgo_gotypes.go"}
...@@ -3288,7 +3303,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi ...@@ -3288,7 +3303,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
cgoflags = append(cgoflags, "-exportheader="+obj+"_cgo_install.h") 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 return nil, nil, err
} }
outGo = append(outGo, gofiles...) outGo = append(outGo, gofiles...)
...@@ -3304,7 +3319,8 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi ...@@ -3304,7 +3319,8 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
} }
for _, file := range gccfiles { 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 { if err := b.gcc(p, ofile, cflags, file); err != nil {
return nil, nil, err return nil, nil, err
} }
...@@ -3314,7 +3330,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi ...@@ -3314,7 +3330,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
cxxflags := stringList(cgoCPPFLAGS, cgoCXXFLAGS) cxxflags := stringList(cgoCPPFLAGS, cgoCXXFLAGS)
for _, file := range gxxfiles { for _, file := range gxxfiles {
// Append .o to the file, just in case the pkg has file.c and file.cpp // 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 { if err := b.gxx(p, ofile, cxxflags, file); err != nil {
return nil, nil, err return nil, nil, err
} }
...@@ -3323,7 +3339,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi ...@@ -3323,7 +3339,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
for _, file := range mfiles { for _, file := range mfiles {
// Append .o to the file, just in case the pkg has file.c and file.m // 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 { if err := b.gcc(p, ofile, cflags, file); err != nil {
return nil, nil, err return nil, nil, err
} }
...@@ -3333,7 +3349,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi ...@@ -3333,7 +3349,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
fflags := stringList(cgoCPPFLAGS, cgoFFLAGS) fflags := stringList(cgoCPPFLAGS, cgoFFLAGS)
for _, file := range ffiles { for _, file := range ffiles {
// Append .o to the file, just in case the pkg has file.c and file.f // 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 { if err := b.gfortran(p, ofile, fflags, file); err != nil {
return nil, nil, err return nil, nil, err
} }
...@@ -3669,7 +3685,7 @@ func (b *builder) swigOne(p *Package, file, obj string, pcCFLAGS []string, cxx b ...@@ -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 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 // disableBuildID adjusts a linker command line to avoid creating a
......
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