@@ -1430,7 +1430,7 @@ func (b *builder) build(a *action) (err error) {
1430
1430
}
1431
1431
}
1432
1432
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
1434
1434
1435
1435
gofiles = append (gofiles , a .p .GoFiles ... )
1436
1436
cgofiles = append (cgofiles , a .p .CgoFiles ... )
@@ -1452,7 +1452,7 @@ func (b *builder) build(a *action) (err error) {
1452
1452
if err != nil {
1453
1453
return err
1454
1454
}
1455
- cgofiles = append (cgofiles , outGo ... )
1455
+ objdirCgofiles = append (objdirCgofiles , outGo ... )
1456
1456
cfiles = append (cfiles , outC ... )
1457
1457
cxxfiles = append (cxxfiles , outCXX ... )
1458
1458
}
@@ -1487,7 +1487,7 @@ func (b *builder) build(a *action) (err error) {
1487
1487
if a .cgo != nil && a .cgo .target != "" {
1488
1488
cgoExe = a .cgo .target
1489
1489
}
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 )
1491
1491
if err != nil {
1492
1492
return err
1493
1493
}
@@ -3209,7 +3209,8 @@ func (b *builder) cflags(p *Package) (cppflags, cflags, cxxflags, fflags, ldflag
3209
3209
3210
3210
var cgoRe = regexp .MustCompile (`[/\\:]` )
3211
3211
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
3213
3214
cgoCPPFLAGS , cgoCFLAGS , cgoCXXFLAGS , cgoFFLAGS , cgoLDFLAGS := b .cflags (p )
3214
3215
cgoCPPFLAGS = append (cgoCPPFLAGS , pcCFLAGS ... )
3215
3216
cgoLDFLAGS = append (cgoLDFLAGS , pcLDFLAGS ... )
@@ -3239,6 +3240,20 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
3239
3240
// Allows including _cgo_export.h from .[ch] files in the package.
3240
3241
cgoCPPFLAGS = append (cgoCPPFLAGS , "-I" , obj )
3241
3242
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
+
3242
3257
// cgo
3243
3258
// TODO: CGO_FLAGS?
3244
3259
gofiles := []string {obj + "_cgo_gotypes.go" }
@@ -3288,7 +3303,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
3288
3303
cgoflags = append (cgoflags , "-exportheader=" + obj + "_cgo_install.h" )
3289
3304
}
3290
3305
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 {
3292
3307
return nil , nil , err
3293
3308
}
3294
3309
outGo = append (outGo , gofiles ... )
@@ -3304,7 +3319,8 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
3304
3319
}
3305
3320
3306
3321
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"
3308
3324
if err := b .gcc (p , ofile , cflags , file ); err != nil {
3309
3325
return nil , nil , err
3310
3326
}
@@ -3314,7 +3330,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
3314
3330
cxxflags := stringList (cgoCPPFLAGS , cgoCXXFLAGS )
3315
3331
for _ , file := range gxxfiles {
3316
3332
// 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"
3318
3334
if err := b .gxx (p , ofile , cxxflags , file ); err != nil {
3319
3335
return nil , nil , err
3320
3336
}
@@ -3323,7 +3339,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
3323
3339
3324
3340
for _ , file := range mfiles {
3325
3341
// 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"
3327
3343
if err := b .gcc (p , ofile , cflags , file ); err != nil {
3328
3344
return nil , nil , err
3329
3345
}
@@ -3333,7 +3349,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofi
3333
3349
fflags := stringList (cgoCPPFLAGS , cgoFFLAGS )
3334
3350
for _ , file := range ffiles {
3335
3351
// 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"
3337
3353
if err := b .gfortran (p , ofile , fflags , file ); err != nil {
3338
3354
return nil , nil , err
3339
3355
}
@@ -3669,7 +3685,7 @@ func (b *builder) swigOne(p *Package, file, obj string, pcCFLAGS []string, cxx b
3669
3685
b .showOutput (p .Dir , p .ImportPath , b .processOutput (out )) // swig warning
3670
3686
}
3671
3687
3672
- return obj + goFile , obj + gccBase + gccExt , nil
3688
+ return goFile , obj + gccBase + gccExt , nil
3673
3689
}
3674
3690
3675
3691
// disableBuildID adjusts a linker command line to avoid creating a
0 commit comments