]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/buildid: fix rewrite algorithm
authorHiroshi Ioka <hirochachacha@gmail.com>
Tue, 19 Dec 2017 14:59:56 +0000 (23:59 +0900)
committerRuss Cox <rsc@golang.org>
Thu, 4 Jan 2018 16:56:51 +0000 (16:56 +0000)
Update rewrite algorithm by coping code from
go/internal/work/buildid:updateBuildID.

Probably, this is not the best option. We could provide high-level API
in cmd/internal/buildid in the future.

Fixes #23181

Change-Id: I336a7c50426ab39bc9998b55c372af61a4fb21a7
Reviewed-on: https://go-review.googlesource.com/84735
Reviewed-by: Russ Cox <rsc@golang.org>
src/cmd/buildid/buildid.go
src/cmd/go/go_test.go
src/cmd/go/internal/work/buildid.go

index 8d810ffdd9900f1ac5a91627ce34a2577bf6fa39..1c7b228c98710b8c84fc0c1bd311a51f1bd8231f 100644 (file)
@@ -22,6 +22,21 @@ func usage() {
 
 var wflag = flag.Bool("w", false, "write build ID")
 
+// taken from cmd/go/internal/work/buildid.go
+func hashToString(h [32]byte) string {
+       const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
+       const chunks = 5
+       var dst [chunks * 4]byte
+       for i := 0; i < chunks; i++ {
+               v := uint32(h[3*i])<<16 | uint32(h[3*i+1])<<8 | uint32(h[3*i+2])
+               dst[4*i+0] = b64[(v>>18)&0x3F]
+               dst[4*i+1] = b64[(v>>12)&0x3F]
+               dst[4*i+2] = b64[(v>>6)&0x3F]
+               dst[4*i+3] = b64[v&0x3F]
+       }
+       return string(dst[:])
+}
+
 func main() {
        log.SetPrefix("buildid: ")
        log.SetFlags(0)
@@ -41,6 +56,8 @@ func main() {
                return
        }
 
+       // Keep in sync with src/cmd/go/internal/work/buildid.go:updateBuildID
+
        f, err := os.Open(file)
        if err != nil {
                log.Fatal(err)
@@ -51,14 +68,14 @@ func main() {
        }
        f.Close()
 
-       tail := id
-       if i := strings.LastIndex(id, "."); i >= 0 {
-               tail = tail[i+1:]
+       newID := id[:strings.LastIndex(id, "/")] + "/" + hashToString(hash)
+       if len(newID) != len(id) {
+               log.Fatalf("%s: build ID length mismatch %q vs %q", file, id, newID)
        }
-       if len(tail) != len(hash)*2 {
-               log.Fatalf("%s: cannot find %d-byte hash in id %s", file, len(hash), id)
+
+       if len(matches) == 0 {
+               return
        }
-       newID := id[:len(id)-len(tail)] + fmt.Sprintf("%x", hash)
 
        f, err = os.OpenFile(file, os.O_WRONLY, 0)
        if err != nil {
index b3fb5dce4f3b2efec1c5a7eb83fa83b30365dc34..18e5eae2bc1de72c3677bb8be821cfebf91a1344 100644 (file)
@@ -4773,12 +4773,14 @@ func TestExecBuildX(t *testing.T) {
        tg := testgo(t)
        defer tg.cleanup()
 
+       tg.setenv("GOCACHE", "off")
+
        tg.tempFile("main.go", `package main; import "C"; func main() { print("hello") }`)
        src := tg.path("main.go")
        obj := tg.path("main")
        tg.run("build", "-x", "-o", obj, src)
        sh := tg.path("test.sh")
-       err := ioutil.WriteFile(sh, []byte(tg.getStderr()), 0666)
+       err := ioutil.WriteFile(sh, []byte("set -e\n"+tg.getStderr()), 0666)
        if err != nil {
                t.Fatal(err)
        }
index 3c90c15a7015d8ebe2ac359bd3a1c8aa1866fb5e..c68526314109eb09caea706d4ca17d2cb117b683 100644 (file)
@@ -408,6 +408,8 @@ func (b *Builder) flushOutput(a *Action) {
 // a.buildID to record as the build ID in the resulting package or binary.
 // updateBuildID computes the final content ID and updates the build IDs
 // in the binary.
+//
+// Keep in sync with src/cmd/buildid/buildid.go
 func (b *Builder) updateBuildID(a *Action, target string, rewrite bool) error {
        if cfg.BuildX || cfg.BuildN {
                if rewrite {