Commit 91318dc7 authored by Michael Hudson-Doyle's avatar Michael Hudson-Doyle Committed by Ian Lance Taylor

cmd/go: refactor creation of top-level actions for -buildmode=shared

Change-Id: I429402dd91243cd9415b054ee17bfebccc68ed57
Reviewed-on: https://go-review.googlesource.com/9197Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
parent 1ccc577b
...@@ -402,7 +402,6 @@ func runBuild(cmd *Command, args []string) { ...@@ -402,7 +402,6 @@ func runBuild(cmd *Command, args []string) {
} }
depMode := modeBuild depMode := modeBuild
mode := modeBuild
if buildI { if buildI {
depMode = modeInstall depMode = modeInstall
} }
...@@ -423,23 +422,12 @@ func runBuild(cmd *Command, args []string) { ...@@ -423,23 +422,12 @@ func runBuild(cmd *Command, args []string) {
var a *action var a *action
if buildBuildmode == "shared" { if buildBuildmode == "shared" {
a = b.libaction(libname(args)) a = b.libaction(libname(args), pkgsFilter(packages(args)), modeBuild, depMode)
mode = depMode
// Currently build mode shared forces external linking
// mode, and external linking mode forces an import of
// runtime/cgo.
var stk importStack
p := loadPackage("runtime/cgo", &stk)
if p.Error != nil {
fatalf("load runtime/cgo: %v", p.Error)
}
a.deps = append(a.deps, b.action(mode, depMode, p))
} else { } else {
a = &action{} a = &action{}
} for _, p := range pkgsFilter(packages(args)) {
for _, p := range pkgsFilter(packages(args)) { a.deps = append(a.deps, b.action(modeBuild, depMode, p))
a.deps = append(a.deps, b.action(mode, depMode, p)) }
} }
b.do(a) b.do(a)
} }
...@@ -504,32 +492,7 @@ func runInstall(cmd *Command, args []string) { ...@@ -504,32 +492,7 @@ func runInstall(cmd *Command, args []string) {
b.init() b.init()
a := &action{} a := &action{}
if buildBuildmode == "shared" { if buildBuildmode == "shared" {
var libdir string a = b.libaction(libname(args), pkgs, modeInstall, modeInstall)
for _, p := range pkgs {
plibdir := p.build.PkgTargetRoot
if libdir == "" {
libdir = plibdir
} else if libdir != plibdir {
fatalf("multiple roots %s & %s", libdir, plibdir)
}
}
a.f = (*builder).install
libfilename := libname(args)
linkSharedAction := b.libaction(libfilename)
a.target = filepath.Join(libdir, libfilename)
a.deps = append(a.deps, linkSharedAction)
for _, p := range pkgs {
if p.target == "" {
continue
}
shlibnameaction := &action{}
shlibnameaction.f = (*builder).installShlibname
shlibnameaction.target = p.target[:len(p.target)-2] + ".shlibname"
a.deps = append(a.deps, shlibnameaction)
shlibnameaction.deps = append(shlibnameaction.deps, linkSharedAction)
linkSharedAction.deps = append(linkSharedAction.deps, b.action(modeInstall, modeInstall, p))
}
} else { } else {
var tools []*action var tools []*action
for _, p := range pkgs { for _, p := range pkgs {
...@@ -849,10 +812,53 @@ func (b *builder) action(mode buildMode, depMode buildMode, p *Package) *action ...@@ -849,10 +812,53 @@ func (b *builder) action(mode buildMode, depMode buildMode, p *Package) *action
return a return a
} }
func (b *builder) libaction(libname string) *action { func (b *builder) libaction(libname string, pkgs []*Package, mode, depMode buildMode) *action {
a := &action{} a := &action{}
a.f = (*builder).linkShared if mode == modeBuild {
a.target = filepath.Join(b.work, libname) a.f = (*builder).linkShared
a.target = filepath.Join(b.work, libname)
for _, p := range pkgs {
if p.target == "" {
continue
}
a.deps = append(a.deps, b.action(depMode, depMode, p))
}
// Currently build mode shared forces external linking
// mode, and external linking mode forces an import of
// runtime/cgo.
var stk importStack
p := loadPackage("runtime/cgo", &stk)
if p.Error != nil {
fatalf("load runtime/cgo: %v", p.Error)
}
a.deps = append(a.deps, b.action(depMode, depMode, p))
} else if mode == modeInstall {
a.f = (*builder).install
var libdir string
for _, p := range pkgs {
plibdir := p.build.PkgTargetRoot
if libdir == "" {
libdir = plibdir
} else if libdir != plibdir {
fatalf("multiple roots %s & %s", libdir, plibdir)
}
}
a.target = filepath.Join(libdir, libname)
linkSharedAction := b.libaction(libname, pkgs, modeBuild, depMode)
a.deps = append(a.deps, linkSharedAction)
for _, p := range pkgs {
if p.target == "" {
continue
}
shlibnameaction := &action{}
shlibnameaction.f = (*builder).installShlibname
shlibnameaction.target = p.target[:len(p.target)-2] + ".shlibname"
a.deps = append(a.deps, shlibnameaction)
shlibnameaction.deps = append(shlibnameaction.deps, linkSharedAction)
}
} else {
fatalf("unregonized mode %v", mode)
}
return a return a
} }
...@@ -1297,9 +1303,19 @@ func (b *builder) linkShared(a *action) (err error) { ...@@ -1297,9 +1303,19 @@ func (b *builder) linkShared(a *action) (err error) {
ldflags = append(ldflags, "-buildmode="+ldBuildmode) ldflags = append(ldflags, "-buildmode="+ldBuildmode)
ldflags = append(ldflags, buildLdflags...) ldflags = append(ldflags, buildLdflags...)
for _, d := range a.deps { for _, d := range a.deps {
if d.target != "" { // omit unsafe etc if d.target == "" { // omit unsafe etc
ldflags = append(ldflags, d.p.ImportPath+"="+d.target) continue
}
if d.p.ImportPath == "runtime/cgo" {
// Fudge: we always ensure runtime/cgo is built, but sometimes it is
// already available as a shared library. The linker will always look
// for runtime/cgo and knows how to tell if it's in a shared library so
// rather than duplicate the logic here, just don't pass it.
// TODO(mwhudson): fix this properly as part of implementing the
// rebuilding of stale shared libraries
continue
} }
ldflags = append(ldflags, d.p.ImportPath+"="+d.target)
} }
return b.run(".", a.target, nil, buildToolExec, tool(archChar()+"l"), "-o", a.target, importArgs, ldflags) return b.run(".", a.target, nil, buildToolExec, tool(archChar()+"l"), "-o", a.target, importArgs, ldflags)
} }
......
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