Commit b3bf38e7 authored by Russ Cox's avatar Russ Cox

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/13027Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent 961f456a
......@@ -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,
......
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