1
0
mirror of https://github.com/golang/go synced 2024-11-12 10:20:27 -07:00

cmd/go: clean up installHeader action

This was confusing when I was trying to fix go build -o.
Perhaps due to that fix, this can now be simplified from
three functions to one.

Change-Id: I878a6d243b14132a631e7c62a3bb6d101bc243ea
Reviewed-on: https://go-review.googlesource.com/13027
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Russ Cox 2015-07-31 15:07:44 -04:00
parent 961f456a1d
commit b3bf38e79d

View File

@ -909,7 +909,19 @@ func (b *builder) action1(mode buildMode, depMode buildMode, p *Package, looksha
a.f = (*builder).install
a.deps = []*action{b.action1(modeBuild, depMode, p, lookshared)}
a.target = a.p.target
a = b.maybeAddHeaderAction(a, true)
// Install header for cgo in c-archive and c-shared modes.
if p.usesCgo() && (buildBuildmode == "c-archive" || buildBuildmode == "c-shared") {
ah := &action{
p: a.p,
deps: []*action{a.deps[0]},
f: (*builder).installHeader,
pkgdir: a.pkgdir,
objdir: a.objdir,
target: a.target[:len(a.target)-len(filepath.Ext(a.target))] + ".h",
}
a.deps = append(a.deps, ah)
}
case modeBuild:
a.f = (*builder).build
@ -1034,49 +1046,6 @@ func (b *builder) libaction(libname string, pkgs []*Package, mode, depMode build
return a
}
// In c-archive/c-shared mode, if the package for the action uses cgo,
// add a dependency to install the generated export header file, if
// there is one.
// The isInstall parameter is whether a is an install action.
func (b *builder) maybeAddHeaderAction(a *action, isInstall bool) *action {
switch buildBuildmode {
case "c-archive", "c-shared":
default:
return a
}
if !a.p.usesCgo() {
return a
}
if isInstall {
// For an install action, change the action function.
// We can't add an action after the install action,
// because it deletes the working directory.
// Adding an action before the install action is painful,
// because it uses deps[0] to find the source.
a.f = (*builder).installWithHeader
return a
}
return &action{
p: a.p,
deps: []*action{a},
f: (*builder).installHeader,
pkgdir: a.pkgdir,
objdir: a.objdir,
target: a.target[:len(a.target)-len(filepath.Ext(a.target))] + ".h",
}
}
// Install the library and the cgo export header if there is one.
func (b *builder) installWithHeader(a *action) error {
target := a.target[:len(a.target)-len(filepath.Ext(a.target))] + ".h"
if err := b.doInstallHeader(a, a.objdir, target); err != nil {
return err
}
return b.install(a)
}
// actionList returns the list of actions in the dag rooted at root
// as visited in a depth-first post-order traversal.
func actionList(root *action) []*action {
@ -1692,25 +1661,21 @@ func (b *builder) copyFile(a *action, dst, src string, perm os.FileMode, force b
// Install the cgo export header file, if there is one.
func (b *builder) installHeader(a *action) error {
return b.doInstallHeader(a, a.objdir, a.target)
}
func (b *builder) doInstallHeader(a *action, objdir, target string) error {
src := objdir + "_cgo_install.h"
src := a.objdir + "_cgo_install.h"
if _, err := os.Stat(src); os.IsNotExist(err) {
// If the file does not exist, there are no exported
// functions, and we do not install anything.
return nil
}
dir, _ := filepath.Split(target)
dir, _ := filepath.Split(a.target)
if dir != "" {
if err := b.mkdir(dir); err != nil {
return err
}
}
return b.moveOrCopyFile(a, target, src, 0644, true)
return b.moveOrCopyFile(a, a.target, src, 0644, true)
}
// cover runs, in effect,