]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: clean up installHeader action
authorRuss Cox <rsc@golang.org>
Fri, 31 Jul 2015 19:07:44 +0000 (15:07 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 3 Aug 2015 20:00:07 +0000 (20:00 +0000)
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>
src/cmd/go/build.go

index b876c51ecfa386de2ae31b4f3c07780febc31b34..718edd2f77fdb1163c0d2785b5a0bee9299d53f6 100644 (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,