Skip to content

Commit 7bf0fc9

Browse files
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 <[email protected]> Reviewed-by: David Crawshaw <[email protected]>
1 parent e22b5ef commit 7bf0fc9

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

src/cmd/go/build.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,7 @@ func (b *builder) build(a *action) (err error) {
14301430
}
14311431
}
14321432

1433-
var gofiles, cgofiles, cfiles, sfiles, cxxfiles, objects, cgoObjects, pcCFLAGS, pcLDFLAGS []string
1433+
var gofiles, cgofiles, objdirCgofiles, cfiles, sfiles, cxxfiles, objects, cgoObjects, pcCFLAGS, pcLDFLAGS []string
14341434

14351435
gofiles = append(gofiles, a.p.GoFiles...)
14361436
cgofiles = append(cgofiles, a.p.CgoFiles...)
@@ -1452,7 +1452,7 @@ func (b *builder) build(a *action) (err error) {
14521452
if err != nil {
14531453
return err
14541454
}
1455-
cgofiles = append(cgofiles, outGo...)
1455+
objdirCgofiles = append(objdirCgofiles, outGo...)
14561456
cfiles = append(cfiles, outC...)
14571457
cxxfiles = append(cxxfiles, outCXX...)
14581458
}
@@ -1487,7 +1487,7 @@ func (b *builder) build(a *action) (err error) {
14871487
if a.cgo != nil && a.cgo.target != "" {
14881488
cgoExe = a.cgo.target
14891489
}
1490-
outGo, outObj, err := b.cgo(a.p, cgoExe, obj, pcCFLAGS, pcLDFLAGS, cgofiles, gccfiles, cxxfiles, a.p.MFiles, a.p.FFiles)
1490+
outGo, outObj, err := b.cgo(a, cgoExe, obj, pcCFLAGS, pcLDFLAGS, cgofiles, objdirCgofiles, gccfiles, cxxfiles, a.p.MFiles, a.p.FFiles)
14911491
if err != nil {
14921492
return err
14931493
}
@@ -3209,7 +3209,8 @@ func (b *builder) cflags(p *Package) (cppflags, cflags, cxxflags, fflags, ldflag
32093209

32103210
var cgoRe = regexp.MustCompile(`[/\\:]`)
32113211

3212-
func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) {
3212+
func (b *builder) cgo(a *action, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofiles, objdirCgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) {
3213+
p := a.p
32133214
cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS := b.cflags(p)
32143215
cgoCPPFLAGS = append(cgoCPPFLAGS, pcCFLAGS...)
32153216
cgoLDFLAGS = append(cgoLDFLAGS, pcLDFLAGS...)
@@ -3239,6 +3240,20 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
32393240
// Allows including _cgo_export.h from .[ch] files in the package.
32403241
cgoCPPFLAGS = append(cgoCPPFLAGS, "-I", obj)
32413242

3243+
// If we have cgo files in the object directory, then copy any
3244+
// other cgo files into the object directory, and pass a
3245+
// -srcdir option to cgo.
3246+
var srcdirarg []string
3247+
if len(objdirCgofiles) > 0 {
3248+
for _, fn := range cgofiles {
3249+
if err := b.copyFile(a, obj+filepath.Base(fn), filepath.Join(p.Dir, fn), 0666, false); err != nil {
3250+
return nil, nil, err
3251+
}
3252+
}
3253+
cgofiles = append(cgofiles, objdirCgofiles...)
3254+
srcdirarg = []string{"-srcdir", obj}
3255+
}
3256+
32423257
// cgo
32433258
// TODO: CGO_FLAGS?
32443259
gofiles := []string{obj + "_cgo_gotypes.go"}
@@ -3288,7 +3303,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
32883303
cgoflags = append(cgoflags, "-exportheader="+obj+"_cgo_install.h")
32893304
}
32903305

3291-
if err := b.run(p.Dir, p.ImportPath, cgoenv, buildToolExec, cgoExe, "-objdir", obj, "-importpath", p.ImportPath, cgoflags, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil {
3306+
if err := b.run(p.Dir, p.ImportPath, cgoenv, buildToolExec, cgoExe, srcdirarg, "-objdir", obj, "-importpath", p.ImportPath, cgoflags, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil {
32923307
return nil, nil, err
32933308
}
32943309
outGo = append(outGo, gofiles...)
@@ -3304,7 +3319,8 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
33043319
}
33053320

33063321
for _, file := range gccfiles {
3307-
ofile := obj + cgoRe.ReplaceAllString(file[:len(file)-1], "_") + "o"
3322+
base := filepath.Base(file)
3323+
ofile := obj + cgoRe.ReplaceAllString(base[:len(base)-1], "_") + "o"
33083324
if err := b.gcc(p, ofile, cflags, file); err != nil {
33093325
return nil, nil, err
33103326
}
@@ -3314,7 +3330,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
33143330
cxxflags := stringList(cgoCPPFLAGS, cgoCXXFLAGS)
33153331
for _, file := range gxxfiles {
33163332
// Append .o to the file, just in case the pkg has file.c and file.cpp
3317-
ofile := obj + cgoRe.ReplaceAllString(file, "_") + ".o"
3333+
ofile := obj + cgoRe.ReplaceAllString(filepath.Base(file), "_") + ".o"
33183334
if err := b.gxx(p, ofile, cxxflags, file); err != nil {
33193335
return nil, nil, err
33203336
}
@@ -3323,7 +3339,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
33233339

33243340
for _, file := range mfiles {
33253341
// Append .o to the file, just in case the pkg has file.c and file.m
3326-
ofile := obj + cgoRe.ReplaceAllString(file, "_") + ".o"
3342+
ofile := obj + cgoRe.ReplaceAllString(filepath.Base(file), "_") + ".o"
33273343
if err := b.gcc(p, ofile, cflags, file); err != nil {
33283344
return nil, nil, err
33293345
}
@@ -3333,7 +3349,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
33333349
fflags := stringList(cgoCPPFLAGS, cgoFFLAGS)
33343350
for _, file := range ffiles {
33353351
// Append .o to the file, just in case the pkg has file.c and file.f
3336-
ofile := obj + cgoRe.ReplaceAllString(file, "_") + ".o"
3352+
ofile := obj + cgoRe.ReplaceAllString(filepath.Base(file), "_") + ".o"
33373353
if err := b.gfortran(p, ofile, fflags, file); err != nil {
33383354
return nil, nil, err
33393355
}
@@ -3669,7 +3685,7 @@ func (b *builder) swigOne(p *Package, file, obj string, pcCFLAGS []string, cxx b
36693685
b.showOutput(p.Dir, p.ImportPath, b.processOutput(out)) // swig warning
36703686
}
36713687

3672-
return obj + goFile, obj + gccBase + gccExt, nil
3688+
return goFile, obj + gccBase + gccExt, nil
36733689
}
36743690

36753691
// disableBuildID adjusts a linker command line to avoid creating a

0 commit comments

Comments
 (0)