From 9b6111aaed4596dacb7692fc964c6dd8c6a9ee31 Mon Sep 17 00:00:00 2001 From: Cole Faust Date: Wed, 2 Feb 2022 15:38:33 -0800 Subject: [PATCH] Remove --root, require the cwd to be the root This allows us to use relative paths everywhere. It also produces more accurate emulation of TOPDIR, which should be an empty string. Bug: 213508006 Test: go test Change-Id: Ie4e357687486e84e9f4aad0f6776d8feb2b9fc63 --- mk2rbc/cmd/mk2rbc.go | 27 +++++++++++++-------------- mk2rbc/mk2rbc.go | 8 ++------ mk2rbc/mk2rbc_test.go | 1 - 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/mk2rbc/cmd/mk2rbc.go b/mk2rbc/cmd/mk2rbc.go index 8e23a53c9..e84eacd86 100644 --- a/mk2rbc/cmd/mk2rbc.go +++ b/mk2rbc/cmd/mk2rbc.go @@ -40,7 +40,6 @@ import ( ) var ( - rootDir = flag.String("root", ".", "the value of // for load paths") // TODO(asmundak): remove this option once there is a consensus on suffix suffix = flag.String("suffix", ".rbc", "generated files' suffix") dryRun = flag.Bool("dry_run", false, "dry run") @@ -71,7 +70,6 @@ func init() { quit("cannot alias unknown flag " + target) } flagAlias("suffix", "s") - flagAlias("root", "d") flagAlias("dry_run", "n") flagAlias("convert_dependents", "r") flagAlias("error_stat", "e") @@ -91,6 +89,10 @@ func main() { } flag.Parse() + if _, err := os.Stat("build/soong/mk2rbc"); err != nil { + quit("Must be run from the root of the android tree. (build/soong/mk2rbc does not exist)") + } + // Delouse if *suffix == ".mk" { quit("cannot use .mk as generated file suffix") @@ -228,17 +230,16 @@ func buildProductConfigMap() map[string]string { const androidProductsMk = "AndroidProducts.mk" // Build the list of AndroidProducts.mk files: it's // build/make/target/product/AndroidProducts.mk + device/**/AndroidProducts.mk plus + vendor/**/AndroidProducts.mk - targetAndroidProductsFile := filepath.Join(*rootDir, "build", "make", "target", "product", androidProductsMk) + targetAndroidProductsFile := filepath.Join("build", "make", "target", "product", androidProductsMk) if _, err := os.Stat(targetAndroidProductsFile); err != nil { - fmt.Fprintf(os.Stderr, "%s: %s\n(hint: %s is not a source tree root)\n", - targetAndroidProductsFile, err, *rootDir) + fmt.Fprintf(os.Stderr, "%s: %s\n", targetAndroidProductsFile, err) } productConfigMap := make(map[string]string) if err := mk2rbc.UpdateProductConfigMap(productConfigMap, targetAndroidProductsFile); err != nil { fmt.Fprintf(os.Stderr, "%s: %s\n", targetAndroidProductsFile, err) } for _, t := range []string{"device", "vendor"} { - _ = filepath.WalkDir(filepath.Join(*rootDir, t), + _ = filepath.WalkDir(t, func(path string, d os.DirEntry, err error) error { if err != nil || d.IsDir() || filepath.Base(path) != androidProductsMk { return nil @@ -254,10 +255,9 @@ func buildProductConfigMap() map[string]string { } func getConfigVariables() { - path := filepath.Join(*rootDir, "build", "make", "core", "product.mk") + path := filepath.Join("build", "make", "core", "product.mk") if err := mk2rbc.FindConfigVariables(path, mk2rbc.KnownVariables); err != nil { - quit(fmt.Errorf("%s\n(check --root[=%s], it should point to the source root)", - err, *rootDir)) + quit(err) } } @@ -270,11 +270,11 @@ func (s fileNameScope) Get(name string) string { if name != "BUILD_SYSTEM" { return fmt.Sprintf("$(%s)", name) } - return filepath.Join(*rootDir, "build", "make", "core") + return filepath.Join("build", "make", "core") } func getSoongVariables() { - path := filepath.Join(*rootDir, "build", "make", "core", "soong_config.mk") + path := filepath.Join("build", "make", "core", "soong_config.mk") err := mk2rbc.FindSoongVariables(path, fileNameScope{}, mk2rbc.KnownVariables) if err != nil { quit(err) @@ -325,12 +325,11 @@ func convertOne(mkFile string) (ok bool) { mk2starRequest := mk2rbc.Request{ MkFile: mkFile, Reader: nil, - RootDir: *rootDir, OutputDir: *outputTop, OutputSuffix: *suffix, TracedVariables: tracedVariables, TraceCalls: *traceCalls, - SourceFS: os.DirFS(*rootDir), + SourceFS: os.DirFS("."), MakefileFinder: makefileFinder, ErrorLogger: errorLogger, } @@ -587,7 +586,7 @@ type FileListMakefileFinder struct { func (l *FileListMakefileFinder) Find(root string) []string { root, err1 := filepath.Abs(root) - wd, err2 := filepath.Abs(*rootDir) + wd, err2 := os.Getwd() if root != wd || err1 != nil || err2 != nil { return l.FindCommandMakefileFinder.Find(root) } diff --git a/mk2rbc/mk2rbc.go b/mk2rbc/mk2rbc.go index 03cf21e4a..b8fe16239 100644 --- a/mk2rbc/mk2rbc.go +++ b/mk2rbc/mk2rbc.go @@ -142,7 +142,6 @@ var identifierFullMatchRegex = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$") type Request struct { MkFile string // file to convert Reader io.Reader // if set, read input from this stream instead - RootDir string // root directory path used to resolve included files OutputSuffix string // generated Starlark files suffix OutputDir string // if set, root of the output hierarchy ErrorLogger ErrorLogger @@ -378,7 +377,6 @@ type StarlarkScript struct { nodes []starlarkNode inherited []*moduleInfo hasErrors bool - topDir string traceCalls bool // print enter/exit each init function sourceFS fs.FS makefileFinder MakefileFinder @@ -414,11 +412,10 @@ type parseContext struct { } func newParseContext(ss *StarlarkScript, nodes []mkparser.Node) *parseContext { - topdir, _ := filepath.Split(filepath.Join(ss.topDir, "foo")) predefined := []struct{ name, value string }{ {"SRC_TARGET_DIR", filepath.Join("build", "make", "target")}, {"LOCAL_PATH", filepath.Dir(ss.mkFile)}, - {"TOPDIR", topdir}, + {"TOPDIR", ""}, // TOPDIR is just set to an empty string in cleanbuild.mk and core.mk // TODO(asmundak): maybe read it from build/make/core/envsetup.mk? {"TARGET_COPY_OUT_SYSTEM", "system"}, {"TARGET_COPY_OUT_SYSTEM_OTHER", "system_other"}, @@ -827,7 +824,7 @@ func (ctx *parseContext) handleSubConfig( } func (ctx *parseContext) findMatchingPaths(pattern []string) []string { - files := ctx.script.makefileFinder.Find(ctx.script.topDir) + files := ctx.script.makefileFinder.Find(".") if len(pattern) == 0 { return files } @@ -1864,7 +1861,6 @@ func Convert(req Request) (*StarlarkScript, error) { starScript := &StarlarkScript{ moduleName: moduleNameForFile(req.MkFile), mkFile: req.MkFile, - topDir: req.RootDir, traceCalls: req.TraceCalls, sourceFS: req.SourceFS, makefileFinder: req.MakefileFinder, diff --git a/mk2rbc/mk2rbc_test.go b/mk2rbc/mk2rbc_test.go index c49939851..2083121ab 100644 --- a/mk2rbc/mk2rbc_test.go +++ b/mk2rbc/mk2rbc_test.go @@ -1387,7 +1387,6 @@ func TestGood(t *testing.T) { ss, err := Convert(Request{ MkFile: test.mkname, Reader: bytes.NewBufferString(test.in), - RootDir: ".", OutputSuffix: ".star", SourceFS: fs, MakefileFinder: &testMakefileFinder{fs: fs},