1
0
mirror of https://github.com/golang/go synced 2024-11-19 06:54:39 -07:00

cmd/buildid: fix rewrite algorithm

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>
This commit is contained in:
Hiroshi Ioka 2017-12-19 23:59:56 +09:00 committed by Russ Cox
parent e676dbbdaf
commit afd090c0c0
3 changed files with 28 additions and 7 deletions

View 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 {

View 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)
}

View 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 {