From: Srdjan Petrovic Date: Tue, 9 Jun 2015 21:36:05 +0000 (-0700) Subject: cmd/go: force-overwrite destination files when installing cgo headers X-Git-Tag: go1.5beta1~253 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=85b333dbf8a367bfd4f6d946c92096d21c61111f;p=gostls13.git cmd/go: force-overwrite destination files when installing cgo headers 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 Reviewed-by: Ian Lance Taylor --- diff --git a/src/cmd/go/build.go b/src/cmd/go/build.go index 9f6054070e..4c8f319217 100644 --- a/src/cmd/go/build.go +++ b/src/cmd/go/build.go @@ -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,