aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2023-06-06 08:18:21 -0400
committerRuss Cox <rsc@golang.org>2023-06-13 20:44:00 +0000
commit9efa625d8425fd97381c80c948689c0eb0f76769 (patch)
treeb15f3841d5d162315cafc2fea5d94650b183bcd9
parente712759914afa063076bcf5207a93cafd003f7bc (diff)
downloadgo-9efa625d8425fd97381c80c948689c0eb0f76769.tar.gz
go-9efa625d8425fd97381c80c948689c0eb0f76769.zip
cmd/dist: more robust cleanup
Identify generated files by name prefix (z*) and content (^// Code generated by go tool dist) instead of having a fixed list. This will be more robust against doing make.bash and then rewinding git and then doing make.bash again. Change-Id: If9b4d02f5ad65345623866176d96e9894a957dc8 Reviewed-on: https://go-review.googlesource.com/c/go/+/501036 Reviewed-by: Austin Clements <austin@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Russ Cox <rsc@golang.org>
-rw-r--r--src/cmd/dist/build.go47
-rw-r--r--src/cmd/dist/buildgo.go9
2 files changed, 40 insertions, 16 deletions
diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go
index 11f897af4c..4b77ed36f7 100644
--- a/src/cmd/dist/build.go
+++ b/src/cmd/dist/build.go
@@ -9,6 +9,8 @@ import (
"encoding/json"
"flag"
"fmt"
+ "io"
+ "io/fs"
"log"
"os"
"os/exec"
@@ -1141,23 +1143,36 @@ func dopack(dst, src string, extra []string) {
writefile(bdst.String(), dst, 0)
}
-var runtimegen = []string{
- "zaexperiment.h",
- "zversion.go",
-}
-
func clean() {
- // Remove generated files.
- for _, gt := range gentab {
- path := pathf("%s/src/%s/%s", goroot, gt.pkg, gt.file)
- xremove(path)
- }
-
- // remove runtimegen files.
- path := pathf("%s/src/runtime", goroot)
- for _, elem := range runtimegen {
- xremove(pathf("%s/%s", path, elem))
- }
+ generated := []byte(generatedHeader)
+
+ // Remove generated source files.
+ filepath.WalkDir(pathf("%s/src", goroot), func(path string, d fs.DirEntry, err error) error {
+ switch {
+ case err != nil:
+ // ignore
+ case d.IsDir() && (d.Name() == "vendor" || d.Name() == "testdata"):
+ return filepath.SkipDir
+ case d.IsDir() && d.Name() != "dist":
+ // Remove generated binary named for directory, but not dist out from under us.
+ exe := filepath.Join(path, d.Name())
+ if info, err := os.Stat(exe); err == nil && !info.IsDir() {
+ xremove(exe)
+ }
+ xremove(exe + ".exe")
+ case !d.IsDir() && strings.HasPrefix(d.Name(), "z"):
+ // Remove generated file, identified by marker string.
+ head := make([]byte, 512)
+ if f, err := os.Open(path); err == nil {
+ io.ReadFull(f, head)
+ f.Close()
+ }
+ if bytes.HasPrefix(head, generated) {
+ xremove(path)
+ }
+ }
+ return nil
+ })
if rebuildall {
// Remove object tree.
diff --git a/src/cmd/dist/buildgo.go b/src/cmd/dist/buildgo.go
index 67400bb93f..884e9d729a 100644
--- a/src/cmd/dist/buildgo.go
+++ b/src/cmd/dist/buildgo.go
@@ -18,6 +18,15 @@ import (
*/
// generatedHeader is the string that all source files generated by dist start with.
+//
+// DO NOT CHANGE THIS STRING. If this string is changed then during
+//
+// ./make.bash
+// git checkout other-rev
+// ./make.bash
+//
+// the second make.bash will not find the files generated by the first make.bash
+// and will not clean up properly.
const generatedHeader = "// Code generated by go tool dist; DO NOT EDIT.\n\n"
// writeHeader emits the standard "generated by" header for all files generated