1
0
mirror of https://github.com/golang/go synced 2024-11-19 17:04:41 -07:00

cmd/go: use objdir as consistent variable name for per-package work dir

Before it was obj, but if you don't have everything paged in
that sounds a bit like an object file. Use objdir, which is more
clearly a directory and also matches the Action.Objdir struct field.

Change-Id: I268042800f9ca05721814d7f18c728acb4831232
Reviewed-on: https://go-review.googlesource.com/56277
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Russ Cox 2017-06-09 10:45:49 -04:00
parent d52413d62d
commit 4a52038c03

View File

@ -1262,8 +1262,8 @@ func (b *Builder) build(a *Action) (err error) {
} }
// Make build directory. // Make build directory.
obj := a.Objdir objdir := a.Objdir
if err := b.Mkdir(obj); err != nil { if err := b.Mkdir(objdir); err != nil {
return err return err
} }
@ -1293,7 +1293,7 @@ func (b *Builder) build(a *Action) (err error) {
// Each run will generate two files, a .go file and a .c or .cxx file. // Each run will generate two files, a .go file and a .c or .cxx file.
// The .go file will use import "C" and is to be processed by cgo. // The .go file will use import "C" and is to be processed by cgo.
if a.Package.UsesSwig() { if a.Package.UsesSwig() {
outGo, outC, outCXX, err := b.swig(a.Package, obj, pcCFLAGS) outGo, outC, outCXX, err := b.swig(a.Package, objdir, pcCFLAGS)
if err != nil { if err != nil {
return err return err
} }
@ -1344,12 +1344,12 @@ func (b *Builder) build(a *Action) (err error) {
} else { } else {
cgoExe = base.Tool("cgo") cgoExe = base.Tool("cgo")
} }
outGo, outObj, err := b.cgo(a, cgoExe, obj, pcCFLAGS, pcLDFLAGS, cgofiles, objdirCgofiles, gccfiles, cxxfiles, a.Package.MFiles, a.Package.FFiles) outGo, outObj, err := b.cgo(a, cgoExe, objdir, pcCFLAGS, pcLDFLAGS, cgofiles, objdirCgofiles, gccfiles, cxxfiles, a.Package.MFiles, a.Package.FFiles)
if err != nil { if err != nil {
return err return err
} }
if cfg.BuildToolchainName == "gccgo" { if cfg.BuildToolchainName == "gccgo" {
cgoObjects = append(cgoObjects, filepath.Join(a.Objdir, "_cgo_flags")) cgoObjects = append(cgoObjects, a.Objdir+"_cgo_flags")
} }
cgoObjects = append(cgoObjects, outObj...) cgoObjects = append(cgoObjects, outObj...)
gofiles = append(gofiles, outGo...) gofiles = append(gofiles, outGo...)
@ -1369,11 +1369,11 @@ func (b *Builder) build(a *Action) (err error) {
// cgo files have absolute paths // cgo files have absolute paths
base := filepath.Base(file) base := filepath.Base(file)
sourceFile = file sourceFile = file
coverFile = filepath.Join(obj, base) coverFile = objdir + base
key = strings.TrimSuffix(base, ".cgo1.go") + ".go" key = strings.TrimSuffix(base, ".cgo1.go") + ".go"
} else { } else {
sourceFile = filepath.Join(a.Package.Dir, file) sourceFile = filepath.Join(a.Package.Dir, file)
coverFile = filepath.Join(obj, file) coverFile = objdir + file
key = file key = file
} }
cover := a.Package.Internal.CoverVars[key] cover := a.Package.Internal.CoverVars[key]
@ -1392,7 +1392,7 @@ func (b *Builder) build(a *Action) (err error) {
inc := b.includeArgs("-I", allArchiveActions(a)) inc := b.includeArgs("-I", allArchiveActions(a))
// Compile Go. // Compile Go.
ofile, out, err := BuildToolchain.gc(b, a.Package, a.Objpkg, obj, len(sfiles) > 0, inc, gofiles) ofile, out, err := BuildToolchain.gc(b, a.Package, a.Objpkg, objdir, len(sfiles) > 0, inc, gofiles)
if len(out) > 0 { if len(out) > 0 {
b.showOutput(a.Package.Dir, a.Package.ImportPath, b.processOutput(out)) b.showOutput(a.Package.Dir, a.Package.ImportPath, b.processOutput(out))
if err != nil { if err != nil {
@ -1417,17 +1417,17 @@ func (b *Builder) build(a *Action) (err error) {
switch { switch {
case strings.HasSuffix(name, _goos_goarch): case strings.HasSuffix(name, _goos_goarch):
targ := file[:len(name)-len(_goos_goarch)] + "_GOOS_GOARCH." + ext targ := file[:len(name)-len(_goos_goarch)] + "_GOOS_GOARCH." + ext
if err := b.copyFile(a, obj+targ, filepath.Join(a.Package.Dir, file), 0666, true); err != nil { if err := b.copyFile(a, objdir+targ, filepath.Join(a.Package.Dir, file), 0666, true); err != nil {
return err return err
} }
case strings.HasSuffix(name, _goarch): case strings.HasSuffix(name, _goarch):
targ := file[:len(name)-len(_goarch)] + "_GOARCH." + ext targ := file[:len(name)-len(_goarch)] + "_GOARCH." + ext
if err := b.copyFile(a, obj+targ, filepath.Join(a.Package.Dir, file), 0666, true); err != nil { if err := b.copyFile(a, objdir+targ, filepath.Join(a.Package.Dir, file), 0666, true); err != nil {
return err return err
} }
case strings.HasSuffix(name, _goos): case strings.HasSuffix(name, _goos):
targ := file[:len(name)-len(_goos)] + "_GOOS." + ext targ := file[:len(name)-len(_goos)] + "_GOOS." + ext
if err := b.copyFile(a, obj+targ, filepath.Join(a.Package.Dir, file), 0666, true); err != nil { if err := b.copyFile(a, objdir+targ, filepath.Join(a.Package.Dir, file), 0666, true); err != nil {
return err return err
} }
} }
@ -1435,7 +1435,7 @@ func (b *Builder) build(a *Action) (err error) {
for _, file := range cfiles { for _, file := range cfiles {
out := file[:len(file)-len(".c")] + ".o" out := file[:len(file)-len(".c")] + ".o"
if err := BuildToolchain.cc(b, a.Package, obj, obj+out, file); err != nil { if err := BuildToolchain.cc(b, a.Package, objdir, objdir+out, file); err != nil {
return err return err
} }
objects = append(objects, out) objects = append(objects, out)
@ -1443,7 +1443,7 @@ func (b *Builder) build(a *Action) (err error) {
// Assemble .s files. // Assemble .s files.
if len(sfiles) > 0 { if len(sfiles) > 0 {
ofiles, err := BuildToolchain.asm(b, a.Package, obj, sfiles) ofiles, err := BuildToolchain.asm(b, a.Package, objdir, sfiles)
if err != nil { if err != nil {
return err return err
} }
@ -1461,13 +1461,13 @@ func (b *Builder) build(a *Action) (err error) {
objects = append(objects, filepath.Join(a.Package.Dir, syso)) objects = append(objects, filepath.Join(a.Package.Dir, syso))
} }
// Pack into archive in obj directory. // Pack into archive in objdir directory.
// If the Go compiler wrote an archive, we only need to add the // If the Go compiler wrote an archive, we only need to add the
// object files for non-Go sources to the archive. // object files for non-Go sources to the archive.
// If the Go compiler wrote an archive and the package is entirely // If the Go compiler wrote an archive and the package is entirely
// Go sources, there is no pack to execute at all. // Go sources, there is no pack to execute at all.
if len(objects) > 0 { if len(objects) > 0 {
if err := BuildToolchain.pack(b, a.Package, obj, a.Objpkg, objects); err != nil { if err := BuildToolchain.pack(b, a.Package, objdir, a.Objpkg, objects); err != nil {
return err return err
} }
} }
@ -2111,19 +2111,19 @@ func mkAbs(dir, f string) string {
type toolchain interface { type toolchain interface {
// gc runs the compiler in a specific directory on a set of files // gc runs the compiler in a specific directory on a set of files
// and returns the name of the generated output file. // and returns the name of the generated output file.
gc(b *Builder, p *load.Package, archive, obj string, asmhdr bool, importArgs []string, gofiles []string) (ofile string, out []byte, err error) gc(b *Builder, p *load.Package, archive, objdir string, asmhdr bool, importArgs []string, gofiles []string) (ofile string, out []byte, err error)
// cc runs the toolchain's C compiler in a directory on a C file // cc runs the toolchain's C compiler in a directory on a C file
// to produce an output file. // to produce an output file.
cc(b *Builder, p *load.Package, objdir, ofile, cfile string) error cc(b *Builder, p *load.Package, objdir, ofile, cfile string) error
// asm runs the assembler in a specific directory on specific files // asm runs the assembler in a specific directory on specific files
// and returns a list of named output files. // and returns a list of named output files.
asm(b *Builder, p *load.Package, obj string, sfiles []string) ([]string, error) asm(b *Builder, p *load.Package, objdir string, sfiles []string) ([]string, error)
// pkgpath builds an appropriate path for a temporary package file. // pkgpath builds an appropriate path for a temporary package file.
Pkgpath(basedir string, p *load.Package) string Pkgpath(basedir string, p *load.Package) string
// pack runs the archive packer in a specific directory to create // pack runs the archive packer in a specific directory to create
// an archive from a set of object files. // an archive from a set of object files.
// typically it is run in the object directory. // typically it is run in the object directory.
pack(b *Builder, p *load.Package, objDir, afile string, ofiles []string) error pack(b *Builder, p *load.Package, objdir, afile string, ofiles []string) error
// ld runs the linker to create an executable starting at mainpkg. // ld runs the linker to create an executable starting at mainpkg.
ld(b *Builder, root *Action, out string, allactions []*Action, mainpkg string, ofiles []string) error ld(b *Builder, root *Action, out string, allactions []*Action, mainpkg string, ofiles []string) error
// ldShared runs the linker to create a shared library containing the pkgs built by toplevelactions // ldShared runs the linker to create a shared library containing the pkgs built by toplevelactions
@ -2150,11 +2150,11 @@ func (noToolchain) linker() string {
return "" return ""
} }
func (noToolchain) gc(b *Builder, p *load.Package, archive, obj string, asmhdr bool, importArgs []string, gofiles []string) (ofile string, out []byte, err error) { func (noToolchain) gc(b *Builder, p *load.Package, archive, objdir string, asmhdr bool, importArgs []string, gofiles []string) (ofile string, out []byte, err error) {
return "", nil, noCompiler() return "", nil, noCompiler()
} }
func (noToolchain) asm(b *Builder, p *load.Package, obj string, sfiles []string) ([]string, error) { func (noToolchain) asm(b *Builder, p *load.Package, objdir string, sfiles []string) ([]string, error) {
return nil, noCompiler() return nil, noCompiler()
} }
@ -2163,7 +2163,7 @@ func (noToolchain) Pkgpath(basedir string, p *load.Package) string {
return "" return ""
} }
func (noToolchain) pack(b *Builder, p *load.Package, objDir, afile string, ofiles []string) error { func (noToolchain) pack(b *Builder, p *load.Package, objdir, afile string, ofiles []string) error {
return noCompiler() return noCompiler()
} }
@ -2190,12 +2190,12 @@ func (gcToolchain) linker() string {
return base.Tool("link") return base.Tool("link")
} }
func (gcToolchain) gc(b *Builder, p *load.Package, archive, obj string, asmhdr bool, importArgs []string, gofiles []string) (ofile string, output []byte, err error) { func (gcToolchain) gc(b *Builder, p *load.Package, archive, objdir string, asmhdr bool, importArgs []string, gofiles []string) (ofile string, output []byte, err error) {
if archive != "" { if archive != "" {
ofile = archive ofile = archive
} else { } else {
out := "_go_.o" out := "_go_.o"
ofile = obj + out ofile = objdir + out
} }
gcargs := []string{"-p", p.ImportPath} gcargs := []string{"-p", p.ImportPath}
@ -2265,7 +2265,7 @@ func (gcToolchain) gc(b *Builder, p *load.Package, archive, obj string, asmhdr b
args = append(args, "-pack") args = append(args, "-pack")
} }
if asmhdr { if asmhdr {
args = append(args, "-asmhdr", obj+"go_asm.h") args = append(args, "-asmhdr", objdir+"go_asm.h")
} }
// Add -c=N to use concurrent backend compilation, if possible. // Add -c=N to use concurrent backend compilation, if possible.
@ -2352,10 +2352,10 @@ CheckFlags:
return c return c
} }
func (gcToolchain) asm(b *Builder, p *load.Package, obj string, sfiles []string) ([]string, error) { func (gcToolchain) asm(b *Builder, p *load.Package, objdir string, sfiles []string) ([]string, error) {
// Add -I pkg/GOOS_GOARCH so #include "textflag.h" works in .s files. // Add -I pkg/GOOS_GOARCH so #include "textflag.h" works in .s files.
inc := filepath.Join(cfg.GOROOT, "pkg", "include") inc := filepath.Join(cfg.GOROOT, "pkg", "include")
args := []interface{}{cfg.BuildToolexec, base.Tool("asm"), "-trimpath", b.WorkDir, "-I", obj, "-I", inc, "-D", "GOOS_" + cfg.Goos, "-D", "GOARCH_" + cfg.Goarch, buildAsmflags} args := []interface{}{cfg.BuildToolexec, base.Tool("asm"), "-trimpath", b.WorkDir, "-I", objdir, "-I", inc, "-D", "GOOS_" + cfg.Goos, "-D", "GOARCH_" + cfg.Goarch, buildAsmflags}
if p.ImportPath == "runtime" && cfg.Goarch == "386" { if p.ImportPath == "runtime" && cfg.Goarch == "386" {
for _, arg := range buildAsmflags { for _, arg := range buildAsmflags {
if arg == "-dynlink" { if arg == "-dynlink" {
@ -2365,7 +2365,7 @@ func (gcToolchain) asm(b *Builder, p *load.Package, obj string, sfiles []string)
} }
var ofiles []string var ofiles []string
for _, sfile := range sfiles { for _, sfile := range sfiles {
ofile := obj + sfile[:len(sfile)-len(".s")] + ".o" ofile := objdir + sfile[:len(sfile)-len(".s")] + ".o"
ofiles = append(ofiles, ofile) ofiles = append(ofiles, ofile)
a := append(args, "-o", ofile, mkAbs(p.Dir, sfile)) a := append(args, "-o", ofile, mkAbs(p.Dir, sfile))
if err := b.run(p.Dir, p.ImportPath, nil, a...); err != nil { if err := b.run(p.Dir, p.ImportPath, nil, a...); err != nil {
@ -2406,12 +2406,12 @@ func (gcToolchain) Pkgpath(basedir string, p *load.Package) string {
return filepath.Join(basedir, end) return filepath.Join(basedir, end)
} }
func (gcToolchain) pack(b *Builder, p *load.Package, objDir, afile string, ofiles []string) error { func (gcToolchain) pack(b *Builder, p *load.Package, objdir, afile string, ofiles []string) error {
var absOfiles []string var absOfiles []string
for _, f := range ofiles { for _, f := range ofiles {
absOfiles = append(absOfiles, mkAbs(objDir, f)) absOfiles = append(absOfiles, mkAbs(objdir, f))
} }
absAfile := mkAbs(objDir, afile) absAfile := mkAbs(objdir, afile)
// The archive file should have been created by the compiler. // The archive file should have been created by the compiler.
// Since it used to not work that way, verify. // Since it used to not work that way, verify.
@ -2637,9 +2637,9 @@ func checkGccgoBin() {
os.Exit(2) os.Exit(2)
} }
func (tools gccgoToolchain) gc(b *Builder, p *load.Package, archive, obj string, asmhdr bool, importArgs []string, gofiles []string) (ofile string, output []byte, err error) { func (tools gccgoToolchain) gc(b *Builder, p *load.Package, archive, objdir string, asmhdr bool, importArgs []string, gofiles []string) (ofile string, output []byte, err error) {
out := "_go_.o" out := "_go_.o"
ofile = obj + out ofile = objdir + out
gcargs := []string{"-g"} gcargs := []string{"-g"}
gcargs = append(gcargs, b.gccArchArgs()...) gcargs = append(gcargs, b.gccArchArgs()...)
if pkgpath := gccgoPkgpath(p); pkgpath != "" { if pkgpath := gccgoPkgpath(p); pkgpath != "" {
@ -2708,10 +2708,10 @@ func (tools gccgoToolchain) gc(b *Builder, p *load.Package, archive, obj string,
return ofile, output, err return ofile, output, err
} }
func (tools gccgoToolchain) asm(b *Builder, p *load.Package, obj string, sfiles []string) ([]string, error) { func (tools gccgoToolchain) asm(b *Builder, p *load.Package, objdir string, sfiles []string) ([]string, error) {
var ofiles []string var ofiles []string
for _, sfile := range sfiles { for _, sfile := range sfiles {
ofile := obj + sfile[:len(sfile)-len(".s")] + ".o" ofile := objdir + sfile[:len(sfile)-len(".s")] + ".o"
ofiles = append(ofiles, ofile) ofiles = append(ofiles, ofile)
sfile = mkAbs(p.Dir, sfile) sfile = mkAbs(p.Dir, sfile)
defs := []string{"-D", "GOOS_" + cfg.Goos, "-D", "GOARCH_" + cfg.Goarch} defs := []string{"-D", "GOOS_" + cfg.Goos, "-D", "GOARCH_" + cfg.Goarch}
@ -2720,7 +2720,7 @@ func (tools gccgoToolchain) asm(b *Builder, p *load.Package, obj string, sfiles
} }
defs = tools.maybePIC(defs) defs = tools.maybePIC(defs)
defs = append(defs, b.gccArchArgs()...) defs = append(defs, b.gccArchArgs()...)
err := b.run(p.Dir, p.ImportPath, nil, tools.compiler(), "-xassembler-with-cpp", "-I", obj, "-c", "-o", ofile, defs, sfile) err := b.run(p.Dir, p.ImportPath, nil, tools.compiler(), "-xassembler-with-cpp", "-I", objdir, "-c", "-o", ofile, defs, sfile)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -2735,12 +2735,12 @@ func (gccgoToolchain) Pkgpath(basedir string, p *load.Package) string {
return filepath.Join(filepath.Dir(afile), "lib"+filepath.Base(afile)) return filepath.Join(filepath.Dir(afile), "lib"+filepath.Base(afile))
} }
func (gccgoToolchain) pack(b *Builder, p *load.Package, objDir, afile string, ofiles []string) error { func (gccgoToolchain) pack(b *Builder, p *load.Package, objdir, afile string, ofiles []string) error {
var absOfiles []string var absOfiles []string
for _, f := range ofiles { for _, f := range ofiles {
absOfiles = append(absOfiles, mkAbs(objDir, f)) absOfiles = append(absOfiles, mkAbs(objdir, f))
} }
return b.run(p.Dir, p.ImportPath, nil, "ar", "rc", mkAbs(objDir, afile), absOfiles) return b.run(p.Dir, p.ImportPath, nil, "ar", "rc", mkAbs(objdir, afile), absOfiles)
} }
func (tools gccgoToolchain) link(b *Builder, root *Action, out string, allactions []*Action, mainpkg string, ofiles []string, buildmode, desc string) error { func (tools gccgoToolchain) link(b *Builder, root *Action, out string, allactions []*Action, mainpkg string, ofiles []string, buildmode, desc string) error {
@ -3119,14 +3119,14 @@ func (b *Builder) ccompile(p *load.Package, outfile string, flags []string, file
} }
// gccld runs the gcc linker to create an executable from a set of object files. // gccld runs the gcc linker to create an executable from a set of object files.
func (b *Builder) gccld(p *load.Package, out string, flags []string, obj []string) error { func (b *Builder) gccld(p *load.Package, out string, flags []string, objs []string) error {
var cmd []string var cmd []string
if len(p.CXXFiles) > 0 || len(p.SwigCXXFiles) > 0 { if len(p.CXXFiles) > 0 || len(p.SwigCXXFiles) > 0 {
cmd = b.GxxCmd(p.Dir) cmd = b.GxxCmd(p.Dir)
} else { } else {
cmd = b.GccCmd(p.Dir) cmd = b.GccCmd(p.Dir)
} }
return b.run(p.Dir, p.ImportPath, nil, cmd, "-o", out, obj, flags) return b.run(p.Dir, p.ImportPath, nil, cmd, "-o", out, objs, flags)
} }
// gccCmd returns a gcc command line prefix // gccCmd returns a gcc command line prefix
@ -3294,7 +3294,7 @@ func (b *Builder) CFlags(p *load.Package) (cppflags, cflags, cxxflags, fflags, l
var cgoRe = regexp.MustCompile(`[/\\:]`) var cgoRe = regexp.MustCompile(`[/\\:]`)
func (b *Builder) cgo(a *Action, cgoExe, obj 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, objdirCgofiles, gccfiles, gxxfiles, mfiles, ffiles []string) (outGo, outObj []string, err error) {
p := a.Package p := a.Package
cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS := b.CFlags(p) cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, cgoFFLAGS, cgoLDFLAGS := b.CFlags(p)
cgoCPPFLAGS = append(cgoCPPFLAGS, pcCFLAGS...) cgoCPPFLAGS = append(cgoCPPFLAGS, pcCFLAGS...)
@ -3323,7 +3323,7 @@ func (b *Builder) cgo(a *Action, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofil
} }
// 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", objdir)
// If we have cgo files in the object directory, then copy any // If we have cgo files in the object directory, then copy any
// other cgo files into the object directory, and pass a // other cgo files into the object directory, and pass a
@ -3331,21 +3331,21 @@ func (b *Builder) cgo(a *Action, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofil
var srcdirarg []string var srcdirarg []string
if len(objdirCgofiles) > 0 { if len(objdirCgofiles) > 0 {
for _, fn := range cgofiles { for _, fn := range cgofiles {
if err := b.copyFile(a, obj+filepath.Base(fn), filepath.Join(p.Dir, fn), 0666, false); err != nil { if err := b.copyFile(a, objdir+filepath.Base(fn), filepath.Join(p.Dir, fn), 0666, false); err != nil {
return nil, nil, err return nil, nil, err
} }
} }
cgofiles = append(cgofiles, objdirCgofiles...) cgofiles = append(cgofiles, objdirCgofiles...)
srcdirarg = []string{"-srcdir", obj} srcdirarg = []string{"-srcdir", objdir}
} }
// cgo // cgo
// TODO: CGO_FLAGS? // TODO: CGO_FLAGS?
gofiles := []string{obj + "_cgo_gotypes.go"} gofiles := []string{objdir + "_cgo_gotypes.go"}
cfiles := []string{"_cgo_export.c"} cfiles := []string{"_cgo_export.c"}
for _, fn := range cgofiles { for _, fn := range cgofiles {
f := cgoRe.ReplaceAllString(fn[:len(fn)-2], "_") f := cgoRe.ReplaceAllString(fn[:len(fn)-2], "_")
gofiles = append(gofiles, obj+f+"cgo1.go") gofiles = append(gofiles, objdir+f+"cgo1.go")
cfiles = append(cfiles, f+"cgo2.c") cfiles = append(cfiles, f+"cgo2.c")
} }
@ -3385,10 +3385,10 @@ func (b *Builder) cgo(a *Action, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofil
// Tell cgo that if there are any exported functions // Tell cgo that if there are any exported functions
// it should generate a header file that C code can // it should generate a header file that C code can
// #include. // #include.
cgoflags = append(cgoflags, "-exportheader="+obj+"_cgo_install.h") cgoflags = append(cgoflags, "-exportheader="+objdir+"_cgo_install.h")
} }
if err := b.run(p.Dir, p.ImportPath, cgoenv, cfg.BuildToolexec, cgoExe, srcdirarg, "-objdir", obj, "-importpath", p.ImportPath, cgoflags, "--", cgoCPPFLAGS, cgoCFLAGS, cgofiles); err != nil { if err := b.run(p.Dir, p.ImportPath, cgoenv, cfg.BuildToolexec, cgoExe, srcdirarg, "-objdir", objdir, "-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...)
@ -3396,8 +3396,8 @@ func (b *Builder) cgo(a *Action, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofil
// gcc // gcc
cflags := str.StringList(cgoCPPFLAGS, cgoCFLAGS) cflags := str.StringList(cgoCPPFLAGS, cgoCFLAGS)
for _, cfile := range cfiles { for _, cfile := range cfiles {
ofile := obj + cfile[:len(cfile)-1] + "o" ofile := objdir + cfile[:len(cfile)-1] + "o"
if err := b.gcc(p, ofile, cflags, obj+cfile); err != nil { if err := b.gcc(p, ofile, cflags, objdir+cfile); err != nil {
return nil, nil, err return nil, nil, err
} }
outObj = append(outObj, ofile) outObj = append(outObj, ofile)
@ -3405,7 +3405,7 @@ func (b *Builder) cgo(a *Action, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofil
for _, file := range gccfiles { for _, file := range gccfiles {
base := filepath.Base(file) base := filepath.Base(file)
ofile := obj + cgoRe.ReplaceAllString(base[:len(base)-1], "_") + "o" ofile := objdir + 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
} }
@ -3415,7 +3415,7 @@ func (b *Builder) cgo(a *Action, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofil
cxxflags := str.StringList(cgoCPPFLAGS, cgoCXXFLAGS) cxxflags := str.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(filepath.Base(file), "_") + ".o" ofile := objdir + 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
} }
@ -3424,7 +3424,7 @@ func (b *Builder) cgo(a *Action, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofil
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(filepath.Base(file), "_") + ".o" ofile := objdir + 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
} }
@ -3434,7 +3434,7 @@ func (b *Builder) cgo(a *Action, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofil
fflags := str.StringList(cgoCPPFLAGS, cgoFFLAGS) fflags := str.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(filepath.Base(file), "_") + ".o" ofile := objdir + 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
} }
@ -3443,22 +3443,22 @@ func (b *Builder) cgo(a *Action, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofil
switch cfg.BuildToolchainName { switch cfg.BuildToolchainName {
case "gc": case "gc":
importGo := obj + "_cgo_import.go" importGo := objdir + "_cgo_import.go"
if err := b.dynimport(p, obj, importGo, cgoExe, cflags, cgoLDFLAGS, outObj); err != nil { if err := b.dynimport(p, objdir, importGo, cgoExe, cflags, cgoLDFLAGS, outObj); err != nil {
return nil, nil, err return nil, nil, err
} }
outGo = append(outGo, importGo) outGo = append(outGo, importGo)
ofile := obj + "_all.o" ofile := objdir + "_all.o"
if err := b.collect(p, obj, ofile, cgoLDFLAGS, outObj); err != nil { if err := b.collect(p, objdir, ofile, cgoLDFLAGS, outObj); err != nil {
return nil, nil, err return nil, nil, err
} }
outObj = []string{ofile} outObj = []string{ofile}
case "gccgo": case "gccgo":
defunC := obj + "_cgo_defun.c" defunC := objdir + "_cgo_defun.c"
defunObj := obj + "_cgo_defun.o" defunObj := objdir + "_cgo_defun.o"
if err := BuildToolchain.cc(b, p, obj, defunObj, defunC); err != nil { if err := BuildToolchain.cc(b, p, objdir, defunObj, defunC); err != nil {
return nil, nil, err return nil, nil, err
} }
outObj = append(outObj, defunObj) outObj = append(outObj, defunObj)
@ -3473,15 +3473,15 @@ func (b *Builder) cgo(a *Action, cgoExe, obj string, pcCFLAGS, pcLDFLAGS, cgofil
// dynimport creates a Go source file named importGo containing // dynimport creates a Go source file named importGo containing
// //go:cgo_import_dynamic directives for each symbol or library // //go:cgo_import_dynamic directives for each symbol or library
// dynamically imported by the object files outObj. // dynamically imported by the object files outObj.
func (b *Builder) dynimport(p *load.Package, obj, importGo, cgoExe string, cflags, cgoLDFLAGS, outObj []string) error { func (b *Builder) dynimport(p *load.Package, objdir, importGo, cgoExe string, cflags, cgoLDFLAGS, outObj []string) error {
cfile := obj + "_cgo_main.c" cfile := objdir + "_cgo_main.c"
ofile := obj + "_cgo_main.o" ofile := objdir + "_cgo_main.o"
if err := b.gcc(p, ofile, cflags, cfile); err != nil { if err := b.gcc(p, ofile, cflags, cfile); err != nil {
return err return err
} }
linkobj := str.StringList(ofile, outObj, p.SysoFiles) linkobj := str.StringList(ofile, outObj, p.SysoFiles)
dynobj := obj + "_cgo_.o" dynobj := objdir + "_cgo_.o"
// we need to use -pie for Linux/ARM to get accurate imported sym // we need to use -pie for Linux/ARM to get accurate imported sym
ldflags := cgoLDFLAGS ldflags := cgoLDFLAGS
@ -3502,7 +3502,7 @@ func (b *Builder) dynimport(p *load.Package, obj, importGo, cgoExe string, cflag
// collect partially links the object files outObj into a single // collect partially links the object files outObj into a single
// relocatable object file named ofile. // relocatable object file named ofile.
func (b *Builder) collect(p *load.Package, obj, ofile string, cgoLDFLAGS, outObj []string) error { func (b *Builder) collect(p *load.Package, objdir, ofile string, cgoLDFLAGS, outObj []string) error {
// When linking relocatable objects, various flags need to be // When linking relocatable objects, various flags need to be
// filtered out as they are inapplicable and can cause some linkers // filtered out as they are inapplicable and can cause some linkers
// to fail. // to fail.
@ -3562,18 +3562,18 @@ func (b *Builder) collect(p *load.Package, obj, ofile string, cgoLDFLAGS, outObj
// Run SWIG on all SWIG input files. // Run SWIG on all SWIG input files.
// TODO: Don't build a shared library, once SWIG emits the necessary // TODO: Don't build a shared library, once SWIG emits the necessary
// pragmas for external linking. // pragmas for external linking.
func (b *Builder) swig(p *load.Package, obj string, pcCFLAGS []string) (outGo, outC, outCXX []string, err error) { func (b *Builder) swig(p *load.Package, objdir string, pcCFLAGS []string) (outGo, outC, outCXX []string, err error) {
if err := b.swigVersionCheck(); err != nil { if err := b.swigVersionCheck(); err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }
intgosize, err := b.swigIntSize(obj) intgosize, err := b.swigIntSize(objdir)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }
for _, f := range p.SwigFiles { for _, f := range p.SwigFiles {
goFile, cFile, err := b.swigOne(p, f, obj, pcCFLAGS, false, intgosize) goFile, cFile, err := b.swigOne(p, f, objdir, pcCFLAGS, false, intgosize)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }
@ -3585,7 +3585,7 @@ func (b *Builder) swig(p *load.Package, obj string, pcCFLAGS []string) (outGo, o
} }
} }
for _, f := range p.SwigCXXFiles { for _, f := range p.SwigCXXFiles {
goFile, cxxFile, err := b.swigOne(p, f, obj, pcCFLAGS, true, intgosize) goFile, cxxFile, err := b.swigOne(p, f, objdir, pcCFLAGS, true, intgosize)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }
@ -3680,7 +3680,7 @@ const i int = 1 << 32
// Determine the size of int on the target system for the -intgosize option // Determine the size of int on the target system for the -intgosize option
// of swig >= 2.0.9. Run only once. // of swig >= 2.0.9. Run only once.
func (b *Builder) swigDoIntSize(obj string) (intsize string, err error) { func (b *Builder) swigDoIntSize(objdir string) (intsize string, err error) {
if cfg.BuildN { if cfg.BuildN {
return "$INTBITS", nil return "$INTBITS", nil
} }
@ -3692,7 +3692,7 @@ func (b *Builder) swigDoIntSize(obj string) (intsize string, err error) {
p := load.GoFilesPackage(srcs) p := load.GoFilesPackage(srcs)
if _, _, e := BuildToolchain.gc(b, p, "", obj, false, nil, srcs); e != nil { if _, _, e := BuildToolchain.gc(b, p, "", objdir, false, nil, srcs); e != nil {
return "32", nil return "32", nil
} }
return "64", nil return "64", nil
@ -3700,15 +3700,15 @@ func (b *Builder) swigDoIntSize(obj string) (intsize string, err error) {
// Determine the size of int on the target system for the -intgosize option // Determine the size of int on the target system for the -intgosize option
// of swig >= 2.0.9. // of swig >= 2.0.9.
func (b *Builder) swigIntSize(obj string) (intsize string, err error) { func (b *Builder) swigIntSize(objdir string) (intsize string, err error) {
swigIntSizeOnce.Do(func() { swigIntSizeOnce.Do(func() {
swigIntSize, swigIntSizeError = b.swigDoIntSize(obj) swigIntSize, swigIntSizeError = b.swigDoIntSize(objdir)
}) })
return swigIntSize, swigIntSizeError return swigIntSize, swigIntSizeError
} }
// Run SWIG on one SWIG input file. // Run SWIG on one SWIG input file.
func (b *Builder) swigOne(p *load.Package, file, obj string, pcCFLAGS []string, cxx bool, intgosize string) (outGo, outC string, err error) { func (b *Builder) swigOne(p *load.Package, file, objdir string, pcCFLAGS []string, cxx bool, intgosize string) (outGo, outC string, err error) {
cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _ := b.CFlags(p) cgoCPPFLAGS, cgoCFLAGS, cgoCXXFLAGS, _, _ := b.CFlags(p)
var cflags []string var cflags []string
if cxx { if cxx {
@ -3737,8 +3737,8 @@ func (b *Builder) swigOne(p *load.Package, file, obj string, pcCFLAGS []string,
"-cgo", "-cgo",
"-intgosize", intgosize, "-intgosize", intgosize,
"-module", base, "-module", base,
"-o", obj + gccBase + gccExt, "-o", objdir + gccBase + gccExt,
"-outdir", obj, "-outdir", objdir,
} }
for _, f := range cflags { for _, f := range cflags {
@ -3772,7 +3772,7 @@ func (b *Builder) swigOne(p *load.Package, file, obj string, pcCFLAGS []string,
b.showOutput(p.Dir, p.ImportPath, b.processOutput(out)) // swig warning b.showOutput(p.Dir, p.ImportPath, b.processOutput(out)) // swig warning
} }
return goFile, obj + gccBase + gccExt, nil return goFile, objdir + gccBase + gccExt, nil
} }
// disableBuildID adjusts a linker command line to avoid creating a // disableBuildID adjusts a linker command line to avoid creating a