]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/dist: more robust cleanup
authorRuss Cox <rsc@golang.org>
Tue, 6 Jun 2023 12:18:21 +0000 (08:18 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 13 Jun 2023 20:44:00 +0000 (20:44 +0000)
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>

src/cmd/dist/build.go
src/cmd/dist/buildgo.go

index 11f897af4c657e31155accedbd03736774cffce0..4b77ed36f720e665cc5a4f361ba4ddf675a7af1c 100644 (file)
@@ -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.
index 67400bb93f239507943d26b0d6d6a4b0d6768992..884e9d729a6a3596c7713a30ebeee413fd2b3245 100644 (file)
@@ -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