Skip to content

Commit afd090c

Browse files
hirochachacharsc
authored andcommitted
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>
1 parent e676dbb commit afd090c

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/cmd/buildid/buildid.go

+23-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ func usage() {
2222

2323
var wflag = flag.Bool("w", false, "write build ID")
2424

25+
// taken from cmd/go/internal/work/buildid.go
26+
func hashToString(h [32]byte) string {
27+
const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
28+
const chunks = 5
29+
var dst [chunks * 4]byte
30+
for i := 0; i < chunks; i++ {
31+
v := uint32(h[3*i])<<16 | uint32(h[3*i+1])<<8 | uint32(h[3*i+2])
32+
dst[4*i+0] = b64[(v>>18)&0x3F]
33+
dst[4*i+1] = b64[(v>>12)&0x3F]
34+
dst[4*i+2] = b64[(v>>6)&0x3F]
35+
dst[4*i+3] = b64[v&0x3F]
36+
}
37+
return string(dst[:])
38+
}
39+
2540
func main() {
2641
log.SetPrefix("buildid: ")
2742
log.SetFlags(0)
@@ -41,6 +56,8 @@ func main() {
4156
return
4257
}
4358

59+
// Keep in sync with src/cmd/go/internal/work/buildid.go:updateBuildID
60+
4461
f, err := os.Open(file)
4562
if err != nil {
4663
log.Fatal(err)
@@ -51,14 +68,14 @@ func main() {
5168
}
5269
f.Close()
5370

54-
tail := id
55-
if i := strings.LastIndex(id, "."); i >= 0 {
56-
tail = tail[i+1:]
71+
newID := id[:strings.LastIndex(id, "/")] + "/" + hashToString(hash)
72+
if len(newID) != len(id) {
73+
log.Fatalf("%s: build ID length mismatch %q vs %q", file, id, newID)
5774
}
58-
if len(tail) != len(hash)*2 {
59-
log.Fatalf("%s: cannot find %d-byte hash in id %s", file, len(hash), id)
75+
76+
if len(matches) == 0 {
77+
return
6078
}
61-
newID := id[:len(id)-len(tail)] + fmt.Sprintf("%x", hash)
6279

6380
f, err = os.OpenFile(file, os.O_WRONLY, 0)
6481
if err != nil {

src/cmd/go/go_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -4773,12 +4773,14 @@ func TestExecBuildX(t *testing.T) {
47734773
tg := testgo(t)
47744774
defer tg.cleanup()
47754775

4776+
tg.setenv("GOCACHE", "off")
4777+
47764778
tg.tempFile("main.go", `package main; import "C"; func main() { print("hello") }`)
47774779
src := tg.path("main.go")
47784780
obj := tg.path("main")
47794781
tg.run("build", "-x", "-o", obj, src)
47804782
sh := tg.path("test.sh")
4781-
err := ioutil.WriteFile(sh, []byte(tg.getStderr()), 0666)
4783+
err := ioutil.WriteFile(sh, []byte("set -e\n"+tg.getStderr()), 0666)
47824784
if err != nil {
47834785
t.Fatal(err)
47844786
}

src/cmd/go/internal/work/buildid.go

+2
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,8 @@ func (b *Builder) flushOutput(a *Action) {
408408
// a.buildID to record as the build ID in the resulting package or binary.
409409
// updateBuildID computes the final content ID and updates the build IDs
410410
// in the binary.
411+
//
412+
// Keep in sync with src/cmd/buildid/buildid.go
411413
func (b *Builder) updateBuildID(a *Action, target string, rewrite bool) error {
412414
if cfg.BuildX || cfg.BuildN {
413415
if rewrite {

0 commit comments

Comments
 (0)