]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/dist: do not rewrite files that already have the correct content
authorRuss Cox <rsc@golang.org>
Mon, 20 Apr 2015 15:41:31 +0000 (11:41 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 3 Jun 2015 20:39:35 +0000 (20:39 +0000)
In particular, this avoids moving the mtime on runtime/zversion.go
forward unless the file is out of date. In turn, this makes cross compiles
that run dist multiple times coexist nicely.

(It's no longer necessary to run dist multiple times to set up cross compiles,
but people still might, and it's easy to fix regardless.)

Fixes #4749.

Change-Id: Id430525f168f106bc4b821ca74b2ca498a748f14
Reviewed-on: https://go-review.googlesource.com/9152
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/cmd/dist/build.go
src/cmd/dist/buildruntime.go
src/cmd/dist/buildtool.go
src/cmd/dist/util.go

index 2262a736de2a3ae23acc16beff1bfcde45c69120..2fcb12c82639f11cc7a5026d27b9fb9c067af7af 100644 (file)
@@ -809,11 +809,11 @@ func shouldbuild(file, dir string) bool {
 }
 
 // copy copies the file src to dst, via memory (so only good for small files).
-func copyfile(dst, src string, exec int) {
+func copyfile(dst, src string, flag int) {
        if vflag > 1 {
                errprintf("cp %s %s\n", src, dst)
        }
-       writefile(readfile(src), dst, exec)
+       writefile(readfile(src), dst, flag)
 }
 
 // dopack copies the package src to dst,
index 70aafe9183f9db143f7212f0a3e5e6f47cd39189..9b8d8f0cf65f132d7893193f8203e49a9e746e09 100644 (file)
@@ -35,7 +35,7 @@ func mkzversion(dir, file string) {
                        "const stackGuardMultiplier = %d\n"+
                        "var buildVersion = theVersion\n", goroot_final, findgoversion(), os.Getenv("GOEXPERIMENT"), stackGuardMultiplier())
 
-       writefile(out, file, 0)
+       writefile(out, file, writeSkipSame)
 }
 
 // mkzbootstrap writes cmd/internal/obj/zbootstrap.go:
@@ -80,7 +80,7 @@ func mkzbootstrap(file string) {
                        "const goexperiment = `%s`\n",
                goroot_final, go386, goarm, goextlinkenabled, findgoversion(), stackGuardMultiplier(), os.Getenv("GOEXPERIMENT"))
 
-       writefile(out, file, 0)
+       writefile(out, file, writeSkipSame)
 }
 
 // stackGuardMultiplier returns a multiplier to apply to the default
index 2840f71749228ca886f2bd349a9df7334120ac0b..be54ac46de528fa8d908f058558cc1a6c0432e81 100644 (file)
@@ -118,7 +118,7 @@ func bootstrapBuildTools() {
        // Copy binaries into tool binary directory.
        for _, name := range bootstrapDirs {
                if !strings.Contains(name, "/") {
-                       copyfile(pathf("%s/%s%s", tooldir, name, exe), pathf("%s/bin/%s%s", workspace, name, exe), 1)
+                       copyfile(pathf("%s/%s%s", tooldir, name, exe), pathf("%s/bin/%s%s", workspace, name, exe), writeExec)
                }
        }
 
index cae5d699d4cdf200edf7095f3c69085fda46cb1c..f13210f4dea731b93a10ae8ea0cbda08aee1f9d8 100644 (file)
@@ -5,6 +5,7 @@
 package main
 
 import (
+       "bytes"
        "fmt"
        "io/ioutil"
        "os"
@@ -245,14 +246,28 @@ func readfile(file string) string {
        return string(data)
 }
 
-// writefile writes b to the named file, creating it if needed.  if
-// exec is non-zero, marks the file as executable.
-func writefile(b, file string, exec int) {
+const (
+       writeExec = 1 << iota
+       writeSkipSame
+)
+
+// writefile writes b to the named file, creating it if needed.
+// if exec is non-zero, marks the file as executable.
+// If the file already exists and has the expected content,
+// it is not rewritten, to avoid changing the time stamp.
+func writefile(b, file string, flag int) {
+       new := []byte(b)
+       if flag&writeSkipSame != 0 {
+               old, err := ioutil.ReadFile(file)
+               if err == nil && bytes.Equal(old, new) {
+                       return
+               }
+       }
        mode := os.FileMode(0666)
-       if exec != 0 {
+       if flag&writeExec != 0 {
                mode = 0777
        }
-       err := ioutil.WriteFile(file, []byte(b), mode)
+       err := ioutil.WriteFile(file, new, mode)
        if err != nil {
                fatal("%v", err)
        }