From: Austin Clements Date: Tue, 6 Jun 2023 01:48:59 +0000 (-0400) Subject: cmd/dist: remove deptab and cleanlist in favor of gentab X-Git-Tag: go1.21rc1~89 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=f6ec9b11a6e139aa7e3a63739c588264ed89cbbe;p=gostls13.git cmd/dist: remove deptab and cleanlist in favor of gentab dist's deptab is a list of changes to the automatically derived set of package dependencies. It's as old as dist itself, and the first version of deptab in CL 5620045 was quite complex. From the beginning, some of the entries in deptab have been for generated files that need to be added to the dependency set because they can't be discovered if they don't exist. gentab is also as old as dist itself, and lists the generated dependency files. The interaction between deptab and gentab is rather odd. gentab contains only base file names, not whole paths. To figure out what files to generate, dist takes a Cartesian product of deptab and gentab and calls the generator wherever the basename of a path in deptab matches an entry in gentab. This perhaps made sense at the time because some of the generated files appeared in more than one package in deptab. These days, deptab consists exclusively of generated files because dist can correctly derive all other dependencies, and all of the generated files have unique paths. This makes the Cartesian product approach needlessly complex (and so confusing!), and means that the only purpose served by deptab is to provide full paths for generated files. Furthermore, in the dist clean command, it also needed to expand the file names in gentab to complete paths, but it did so using a different list, cleanlist, and the same Cartesian product algorithm. This CL drops all of this complexity by putting full paths into gentab, which lets us delete deptab and cleanlist. Change-Id: Ie3993983734f6da3be453bb4c17a64e22dcf3e8f Reviewed-on: https://go-review.googlesource.com/c/go/+/501137 Run-TryBot: Austin Clements TryBot-Result: Gopher Robot Reviewed-by: Russ Cox --- diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go index 1bf178d0ad..d920db9b88 100644 --- a/src/cmd/dist/build.go +++ b/src/cmd/dist/build.go @@ -614,33 +614,6 @@ func mustLinkExternal(goos, goarch string, cgoEnabled bool) bool { return false } -// deptab lists changes to the default dependencies for a given prefix. -// deps ending in /* read the whole directory; deps beginning with - -// exclude files with that prefix. -// Note that this table applies only to the build of cmd/go, -// after the main compiler bootstrap. -// Files listed here should also be listed in ../distpack/pack.go's srcArch.Remove list. -var deptab = []struct { - prefix string // prefix of target - dep []string // dependency tweaks for targets with that prefix -}{ - {"cmd/go/internal/cfg", []string{ - "zdefaultcc.go", - }}, - {"go/build", []string{ - "zcgo.go", - }}, - {"internal/platform", []string{ - "zosarch.go", - }}, - {"runtime/internal/sys", []string{ - "zversion.go", - }}, - {"time/tzdata", []string{ - "zzipdata.go", - }}, -} - // depsuffix records the allowed suffixes for source files. var depsuffix = []string{ ".s", @@ -648,15 +621,17 @@ var depsuffix = []string{ } // gentab records how to generate some trivial files. +// Files listed here should also be listed in ../distpack/pack.go's srcArch.Remove list. var gentab = []struct { - nameprefix string - gen func(string, string) + pkg string // Relative to $GOROOT/src + file string + gen func(dir, file string) }{ - {"zcgo.go", mkzcgo}, - {"zdefaultcc.go", mkzdefaultcc}, - {"zosarch.go", mkzosarch}, - {"zversion.go", mkzversion}, - {"zzipdata.go", mktzdata}, + {"go/build", "zcgo.go", mkzcgo}, + {"cmd/go/internal/cfg", "zdefaultcc.go", mkzdefaultcc}, + {"internal/platform", "zosarch.go", mkzosarch}, + {"runtime/internal/sys", "zversion.go", mkzversion}, + {"time/tzdata", "zzipdata.go", mktzdata}, } // installed maps from a dir name (as given to install) to a chan @@ -680,7 +655,7 @@ func startInstall(dir string) chan struct{} { return ch } -// runInstall installs the library, package, or binary associated with dir, +// runInstall installs the library, package, or binary associated with pkg, // which is relative to $GOROOT/src. func runInstall(pkg string, ch chan struct{}) { if pkg == "net" || pkg == "os/user" || pkg == "crypto/x509" { @@ -767,12 +742,10 @@ func runInstall(pkg string, ch chan struct{}) { return !strings.HasPrefix(p, ".") && (!strings.HasPrefix(p, "_") || !strings.HasSuffix(p, ".go")) }) - for _, dt := range deptab { - if pkg == dt.prefix || strings.HasSuffix(dt.prefix, "/") && strings.HasPrefix(pkg, dt.prefix) { - for _, p := range dt.dep { - p = os.ExpandEnv(p) - files = append(files, p) - } + // Add generated files for this package. + for _, gt := range gentab { + if gt.pkg == pkg { + files = append(files, gt.file) } } files = uniq(files) @@ -785,7 +758,7 @@ func runInstall(pkg string, ch chan struct{}) { } // Is the target up-to-date? - var gofiles, sfiles, missing []string + var gofiles, sfiles []string stale := rebuildall files = filter(files, func(p string) bool { for _, suf := range depsuffix { @@ -807,9 +780,6 @@ func runInstall(pkg string, ch chan struct{}) { if t.After(ttarg) { stale = true } - if t.IsZero() { - missing = append(missing, p) - } return true }) @@ -837,32 +807,22 @@ func runInstall(pkg string, ch chan struct{}) { } // Generate any missing files; regenerate existing ones. - for _, p := range files { - elem := filepath.Base(p) - for _, gt := range gentab { - if gt.gen == nil { - continue - } - if strings.HasPrefix(elem, gt.nameprefix) { - if vflag > 1 { - errprintf("generate %s\n", p) - } - gt.gen(dir, p) - // Do not add generated file to clean list. - // In runtime, we want to be able to - // build the package with the go tool, - // and it assumes these generated files already - // exist (it does not know how to build them). - // The 'clean' command can remove - // the generated files. - goto built - } + for _, gt := range gentab { + if gt.pkg != pkg { + continue } - // Did not rebuild p. - if find(p, missing) >= 0 { - fatalf("missing file %s", p) + p := pathf("%s/%s", dir, gt.file) + if vflag > 1 { + errprintf("generate %s\n", p) } - built: + gt.gen(dir, p) + // Do not add generated file to clean list. + // In runtime, we want to be able to + // build the package with the go tool, + // and it assumes these generated files already + // exist (it does not know how to build them). + // The 'clean' command can remove + // the generated files. } // Resolve imported packages to actual package paths. @@ -1186,26 +1146,11 @@ var runtimegen = []string{ "zversion.go", } -// cleanlist is a list of packages with generated files and commands. -var cleanlist = []string{ - "runtime/internal/sys", - "cmd/cgo", - "cmd/go/internal/cfg", - "internal/platform", - "go/build", -} - func clean() { - for _, name := range cleanlist { - path := pathf("%s/src/%s", goroot, name) - // Remove generated files. - for _, elem := range xreaddir(path) { - for _, gt := range gentab { - if strings.HasPrefix(elem, gt.nameprefix) { - xremove(pathf("%s/%s", path, elem)) - } - } - } + // Remove generated files. + for _, gt := range gentab { + path := pathf("%s/src/%s/%s", goroot, gt.pkg, gt.file) + xremove(path) } // remove runtimegen files. diff --git a/src/cmd/distpack/pack.go b/src/cmd/distpack/pack.go index 55e07f88c3..cddbd0747d 100644 --- a/src/cmd/distpack/pack.go +++ b/src/cmd/distpack/pack.go @@ -113,7 +113,7 @@ func main() { "bin/**", "pkg/**", - // Generated during cmd/dist. See ../dist/build.go:/deptab. + // Generated during cmd/dist. See ../dist/build.go:/gentab. "src/cmd/go/internal/cfg/zdefaultcc.go", "src/go/build/zcgo.go", "src/internal/platform/zosarch.go",