mirror of
https://github.com/golang/go
synced 2024-11-26 18:06:55 -07:00
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>
This commit is contained in:
parent
e712759914
commit
9efa625d84
45
src/cmd/dist/build.go
vendored
45
src/cmd/dist/build.go
vendored
@ -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)
|
||||
}
|
||||
generated := []byte(generatedHeader)
|
||||
|
||||
// remove runtimegen files.
|
||||
path := pathf("%s/src/runtime", goroot)
|
||||
for _, elem := range runtimegen {
|
||||
xremove(pathf("%s/%s", path, elem))
|
||||
}
|
||||
// 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.
|
||||
|
9
src/cmd/dist/buildgo.go
vendored
9
src/cmd/dist/buildgo.go
vendored
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user