]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: force-overwrite destination files when installing cgo headers
authorSrdjan Petrovic <spetrovic@google.com>
Tue, 9 Jun 2015 21:36:05 +0000 (14:36 -0700)
committerIan Lance Taylor <iant@golang.org>
Mon, 15 Jun 2015 18:29:39 +0000 (18:29 +0000)
Fixes #11131

When running 'go install -buildmode=c-shared', under the circumstances
described in issue #11131, the install command would fail trying to
install cgo headers if they have already been installed (by a previous
call to 'go install -buildmode=c-shared').

Since it's safe to overwrite said headers (according to iant@), this CL
introduces a parameter to builder's 'copy' and 'move' functions that,
if set to 'true', would force the overwriting of already installed
files.

This parameter value is set to 'true' only when installing cgo headers,
for now.

Change-Id: I5bda17ee757066a8e5d2b39f2e8f3a389eb1e4a2
Reviewed-on: https://go-review.googlesource.com/10870
Run-TryBot: Srdjan Petrovic <spetrovic@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/go/build.go

index 9f6054070e73073e25aa3d6a1b9bbfba071571fa..4c8f3192171e64f714b88440396edd4ada28b943 100644 (file)
@@ -1368,17 +1368,17 @@ func (b *builder) build(a *action) (err error) {
                switch {
                case strings.HasSuffix(name, _goos_goarch):
                        targ := file[:len(name)-len(_goos_goarch)] + "_GOOS_GOARCH." + ext
-                       if err := b.copyFile(a, obj+targ, filepath.Join(a.p.Dir, file), 0644); err != nil {
+                       if err := b.copyFile(a, obj+targ, filepath.Join(a.p.Dir, file), 0644, true); err != nil {
                                return err
                        }
                case strings.HasSuffix(name, _goarch):
                        targ := file[:len(name)-len(_goarch)] + "_GOARCH." + ext
-                       if err := b.copyFile(a, obj+targ, filepath.Join(a.p.Dir, file), 0644); err != nil {
+                       if err := b.copyFile(a, obj+targ, filepath.Join(a.p.Dir, file), 0644, true); err != nil {
                                return err
                        }
                case strings.HasSuffix(name, _goos):
                        targ := file[:len(name)-len(_goos)] + "_GOOS." + ext
-                       if err := b.copyFile(a, obj+targ, filepath.Join(a.p.Dir, file), 0644); err != nil {
+                       if err := b.copyFile(a, obj+targ, filepath.Join(a.p.Dir, file), 0644, true); err != nil {
                                return err
                        }
                }
@@ -1575,7 +1575,7 @@ func (b *builder) install(a *action) (err error) {
                defer os.Remove(a1.target)
        }
 
-       return b.moveOrCopyFile(a, a.target, a1.target, perm)
+       return b.moveOrCopyFile(a, a.target, a1.target, perm, false)
 }
 
 // includeArgs returns the -I or -L directory list for access
@@ -1620,7 +1620,7 @@ func (b *builder) includeArgs(flag string, all []*action) []string {
 }
 
 // moveOrCopyFile is like 'mv src dst' or 'cp src dst'.
-func (b *builder) moveOrCopyFile(a *action, dst, src string, perm os.FileMode) error {
+func (b *builder) moveOrCopyFile(a *action, dst, src string, perm os.FileMode, force bool) error {
        if buildN {
                b.showcmd("", "mv %s %s", src, dst)
                return nil
@@ -1637,11 +1637,11 @@ func (b *builder) moveOrCopyFile(a *action, dst, src string, perm os.FileMode) e
                }
        }
 
-       return b.copyFile(a, dst, src, perm)
+       return b.copyFile(a, dst, src, perm, force)
 }
 
 // copyFile is like 'cp src dst'.
-func (b *builder) copyFile(a *action, dst, src string, perm os.FileMode) error {
+func (b *builder) copyFile(a *action, dst, src string, perm os.FileMode, force bool) error {
        if buildN || buildX {
                b.showcmd("", "cp %s %s", src, dst)
                if buildN {
@@ -1662,7 +1662,7 @@ func (b *builder) copyFile(a *action, dst, src string, perm os.FileMode) error {
                if fi.IsDir() {
                        return fmt.Errorf("build output %q already exists and is a directory", dst)
                }
-               if !isObject(dst) {
+               if !force && !isObject(dst) {
                        return fmt.Errorf("build output %q already exists and is not an object file", dst)
                }
        }
@@ -1719,7 +1719,7 @@ func (b *builder) doInstallHeader(a *action, objdir, target string) error {
                }
        }
 
-       return b.moveOrCopyFile(a, target, src, 0644)
+       return b.moveOrCopyFile(a, target, src, 0644, true)
 }
 
 // cover runs, in effect,