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
This commit is contained in:
Cole Faust 2022-02-02 15:38:33 -08:00
parent 8d47c48082
commit 9b6111aaed
3 changed files with 15 additions and 21 deletions

View file

@ -40,7 +40,6 @@ import (
) )
var ( var (
rootDir = flag.String("root", ".", "the value of // for load paths")
// TODO(asmundak): remove this option once there is a consensus on suffix // TODO(asmundak): remove this option once there is a consensus on suffix
suffix = flag.String("suffix", ".rbc", "generated files' suffix") suffix = flag.String("suffix", ".rbc", "generated files' suffix")
dryRun = flag.Bool("dry_run", false, "dry run") dryRun = flag.Bool("dry_run", false, "dry run")
@ -71,7 +70,6 @@ func init() {
quit("cannot alias unknown flag " + target) quit("cannot alias unknown flag " + target)
} }
flagAlias("suffix", "s") flagAlias("suffix", "s")
flagAlias("root", "d")
flagAlias("dry_run", "n") flagAlias("dry_run", "n")
flagAlias("convert_dependents", "r") flagAlias("convert_dependents", "r")
flagAlias("error_stat", "e") flagAlias("error_stat", "e")
@ -91,6 +89,10 @@ func main() {
} }
flag.Parse() 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 // Delouse
if *suffix == ".mk" { if *suffix == ".mk" {
quit("cannot use .mk as generated file suffix") quit("cannot use .mk as generated file suffix")
@ -228,17 +230,16 @@ func buildProductConfigMap() map[string]string {
const androidProductsMk = "AndroidProducts.mk" const androidProductsMk = "AndroidProducts.mk"
// Build the list of AndroidProducts.mk files: it's // Build the list of AndroidProducts.mk files: it's
// build/make/target/product/AndroidProducts.mk + device/**/AndroidProducts.mk plus + vendor/**/AndroidProducts.mk // 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 { if _, err := os.Stat(targetAndroidProductsFile); err != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n(hint: %s is not a source tree root)\n", fmt.Fprintf(os.Stderr, "%s: %s\n", targetAndroidProductsFile, err)
targetAndroidProductsFile, err, *rootDir)
} }
productConfigMap := make(map[string]string) productConfigMap := make(map[string]string)
if err := mk2rbc.UpdateProductConfigMap(productConfigMap, targetAndroidProductsFile); err != nil { if err := mk2rbc.UpdateProductConfigMap(productConfigMap, targetAndroidProductsFile); err != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n", targetAndroidProductsFile, err) fmt.Fprintf(os.Stderr, "%s: %s\n", targetAndroidProductsFile, err)
} }
for _, t := range []string{"device", "vendor"} { for _, t := range []string{"device", "vendor"} {
_ = filepath.WalkDir(filepath.Join(*rootDir, t), _ = filepath.WalkDir(t,
func(path string, d os.DirEntry, err error) error { func(path string, d os.DirEntry, err error) error {
if err != nil || d.IsDir() || filepath.Base(path) != androidProductsMk { if err != nil || d.IsDir() || filepath.Base(path) != androidProductsMk {
return nil return nil
@ -254,10 +255,9 @@ func buildProductConfigMap() map[string]string {
} }
func getConfigVariables() { 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 { if err := mk2rbc.FindConfigVariables(path, mk2rbc.KnownVariables); err != nil {
quit(fmt.Errorf("%s\n(check --root[=%s], it should point to the source root)", quit(err)
err, *rootDir))
} }
} }
@ -270,11 +270,11 @@ func (s fileNameScope) Get(name string) string {
if name != "BUILD_SYSTEM" { if name != "BUILD_SYSTEM" {
return fmt.Sprintf("$(%s)", name) return fmt.Sprintf("$(%s)", name)
} }
return filepath.Join(*rootDir, "build", "make", "core") return filepath.Join("build", "make", "core")
} }
func getSoongVariables() { 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) err := mk2rbc.FindSoongVariables(path, fileNameScope{}, mk2rbc.KnownVariables)
if err != nil { if err != nil {
quit(err) quit(err)
@ -325,12 +325,11 @@ func convertOne(mkFile string) (ok bool) {
mk2starRequest := mk2rbc.Request{ mk2starRequest := mk2rbc.Request{
MkFile: mkFile, MkFile: mkFile,
Reader: nil, Reader: nil,
RootDir: *rootDir,
OutputDir: *outputTop, OutputDir: *outputTop,
OutputSuffix: *suffix, OutputSuffix: *suffix,
TracedVariables: tracedVariables, TracedVariables: tracedVariables,
TraceCalls: *traceCalls, TraceCalls: *traceCalls,
SourceFS: os.DirFS(*rootDir), SourceFS: os.DirFS("."),
MakefileFinder: makefileFinder, MakefileFinder: makefileFinder,
ErrorLogger: errorLogger, ErrorLogger: errorLogger,
} }
@ -587,7 +586,7 @@ type FileListMakefileFinder struct {
func (l *FileListMakefileFinder) Find(root string) []string { func (l *FileListMakefileFinder) Find(root string) []string {
root, err1 := filepath.Abs(root) root, err1 := filepath.Abs(root)
wd, err2 := filepath.Abs(*rootDir) wd, err2 := os.Getwd()
if root != wd || err1 != nil || err2 != nil { if root != wd || err1 != nil || err2 != nil {
return l.FindCommandMakefileFinder.Find(root) return l.FindCommandMakefileFinder.Find(root)
} }

View file

@ -142,7 +142,6 @@ var identifierFullMatchRegex = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
type Request struct { type Request struct {
MkFile string // file to convert MkFile string // file to convert
Reader io.Reader // if set, read input from this stream instead 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 OutputSuffix string // generated Starlark files suffix
OutputDir string // if set, root of the output hierarchy OutputDir string // if set, root of the output hierarchy
ErrorLogger ErrorLogger ErrorLogger ErrorLogger
@ -378,7 +377,6 @@ type StarlarkScript struct {
nodes []starlarkNode nodes []starlarkNode
inherited []*moduleInfo inherited []*moduleInfo
hasErrors bool hasErrors bool
topDir string
traceCalls bool // print enter/exit each init function traceCalls bool // print enter/exit each init function
sourceFS fs.FS sourceFS fs.FS
makefileFinder MakefileFinder makefileFinder MakefileFinder
@ -414,11 +412,10 @@ type parseContext struct {
} }
func newParseContext(ss *StarlarkScript, nodes []mkparser.Node) *parseContext { func newParseContext(ss *StarlarkScript, nodes []mkparser.Node) *parseContext {
topdir, _ := filepath.Split(filepath.Join(ss.topDir, "foo"))
predefined := []struct{ name, value string }{ predefined := []struct{ name, value string }{
{"SRC_TARGET_DIR", filepath.Join("build", "make", "target")}, {"SRC_TARGET_DIR", filepath.Join("build", "make", "target")},
{"LOCAL_PATH", filepath.Dir(ss.mkFile)}, {"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? // TODO(asmundak): maybe read it from build/make/core/envsetup.mk?
{"TARGET_COPY_OUT_SYSTEM", "system"}, {"TARGET_COPY_OUT_SYSTEM", "system"},
{"TARGET_COPY_OUT_SYSTEM_OTHER", "system_other"}, {"TARGET_COPY_OUT_SYSTEM_OTHER", "system_other"},
@ -827,7 +824,7 @@ func (ctx *parseContext) handleSubConfig(
} }
func (ctx *parseContext) findMatchingPaths(pattern []string) []string { func (ctx *parseContext) findMatchingPaths(pattern []string) []string {
files := ctx.script.makefileFinder.Find(ctx.script.topDir) files := ctx.script.makefileFinder.Find(".")
if len(pattern) == 0 { if len(pattern) == 0 {
return files return files
} }
@ -1864,7 +1861,6 @@ func Convert(req Request) (*StarlarkScript, error) {
starScript := &StarlarkScript{ starScript := &StarlarkScript{
moduleName: moduleNameForFile(req.MkFile), moduleName: moduleNameForFile(req.MkFile),
mkFile: req.MkFile, mkFile: req.MkFile,
topDir: req.RootDir,
traceCalls: req.TraceCalls, traceCalls: req.TraceCalls,
sourceFS: req.SourceFS, sourceFS: req.SourceFS,
makefileFinder: req.MakefileFinder, makefileFinder: req.MakefileFinder,

View file

@ -1387,7 +1387,6 @@ func TestGood(t *testing.T) {
ss, err := Convert(Request{ ss, err := Convert(Request{
MkFile: test.mkname, MkFile: test.mkname,
Reader: bytes.NewBufferString(test.in), Reader: bytes.NewBufferString(test.in),
RootDir: ".",
OutputSuffix: ".star", OutputSuffix: ".star",
SourceFS: fs, SourceFS: fs,
MakefileFinder: &testMakefileFinder{fs: fs}, MakefileFinder: &testMakefileFinder{fs: fs},