Allow dynamically calculated inherit-product path

Bug: 193566316
Test: internal
Change-Id: Iaa7b68cf459f9a694ae9d37a32c9372cf8a8335a
This commit is contained in:
Sasha Smundak
2021-07-22 18:32:56 -07:00
parent e083a05ad0
commit 6609ba7664
7 changed files with 475 additions and 55 deletions

View File

@@ -16,6 +16,8 @@ package mk2rbc
import (
"bytes"
"io/fs"
"path/filepath"
"strings"
"testing"
)
@@ -100,10 +102,13 @@ def init(g, handle):
desc: "Unknown function",
mkname: "product.mk",
in: `
PRODUCT_NAME := $(call foo, bar)
PRODUCT_NAME := $(call foo1, bar)
PRODUCT_NAME := $(call foo0)
`,
expected: `# MK2RBC TRANSLATION ERROR: cannot handle invoking foo
# PRODUCT_NAME := $(call foo, bar)
expected: `# MK2RBC TRANSLATION ERROR: cannot handle invoking foo1
# PRODUCT_NAME := $(call foo1, bar)
# MK2RBC TRANSLATION ERROR: cannot handle invoking foo0
# PRODUCT_NAME := $(call foo0)
load("//build/make/core:product_config.rbc", "rblf")
def init(g, handle):
@@ -130,7 +135,7 @@ def init(g, handle):
rblf.inherit(handle, "part", _part_init)
else:
# Comment
rblf.inherit(handle, "./part", _part_init)
rblf.inherit(handle, "part", _part_init)
`,
},
{
@@ -144,7 +149,7 @@ load(":part.star|init", _part_init = "init")
def init(g, handle):
cfg = rblf.cfg(handle)
if _part_init != None:
if _part_init:
rblf.inherit(handle, "part", _part_init)
`,
},
@@ -160,7 +165,7 @@ else
endif
`,
expected: `load("//build/make/core:product_config.rbc", "rblf")
load(":part.star", _part_init = "init")
load(":part.star|init", _part_init = "init")
def init(g, handle):
cfg = rblf.cfg(handle)
@@ -176,8 +181,7 @@ def init(g, handle):
desc: "Synonymous inherited configurations",
mkname: "path/product.mk",
in: `
$(call inherit-product, foo/font.mk)
$(call inherit-product, bar/font.mk)
$(call inherit-product, */font.mk)
`,
expected: `load("//build/make/core:product_config.rbc", "rblf")
load("//foo:font.star", _font_init = "init")
@@ -254,6 +258,8 @@ def init(g, handle):
in: `
ifdef PRODUCT_NAME
# Comment
else
TARGET_COPY_OUT_VENDOR := foo
endif
`,
expected: `load("//build/make/core:product_config.rbc", "rblf")
@@ -263,6 +269,10 @@ def init(g, handle):
if g.get("PRODUCT_NAME") != None:
# Comment
pass
else:
# MK2RBC TRANSLATION ERROR: cannot set predefined variable TARGET_COPY_OUT_VENDOR to "foo", its value should be "||VENDOR-PATH-PH||"
pass
rblf.warning("product.mk", "partially successful conversion")
`,
},
{
@@ -840,6 +850,30 @@ def init(g, handle):
g["V2"] = g.get("PRODUCT_ADB_KEYS", "")
g["PRODUCT_ADB_KEYS"] = "foo"
g["V3"] = g["PRODUCT_ADB_KEYS"]
`,
},
{
desc: "Dynamic inherit path",
mkname: "product.mk",
in: `
MY_PATH=foo
$(call inherit-product,vendor/$(MY_PATH)/cfg.mk)
`,
expected: `load("//build/make/core:product_config.rbc", "rblf")
load("//vendor/foo1:cfg.star|init", _cfg_init = "init")
load("//vendor/bar/baz:cfg.star|init", _cfg1_init = "init")
def init(g, handle):
cfg = rblf.cfg(handle)
g["MY_PATH"] = "foo"
_entry = {
"vendor/foo1/cfg.mk": ("_cfg", _cfg_init),
"vendor/bar/baz/cfg.mk": ("_cfg1", _cfg1_init),
}.get("vendor/%s/cfg.mk" % g["MY_PATH"])
(_varmod, _varmod_init) = _entry if _entry else (None, None)
if not _varmod_init:
rblf.mkerror("cannot")
rblf.inherit(handle, _varmod, _varmod_init)
`,
},
}
@@ -865,10 +899,47 @@ var known_variables = []struct {
{"PLATFORM_LIST", VarClassSoong, starlarkTypeList}, // TODO(asmundak): make it local instead of soong
}
type testMakefileFinder struct {
fs fs.FS
root string
files []string
}
func (t *testMakefileFinder) Find(root string) []string {
if t.files != nil || root == t.root {
return t.files
}
t.files = make([]string, 0)
fs.WalkDir(t.fs, root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
base := filepath.Base(path)
if base[0] == '.' && len(base) > 1 {
return fs.SkipDir
}
return nil
}
if strings.HasSuffix(path, ".mk") {
t.files = append(t.files, path)
}
return nil
})
return t.files
}
func TestGood(t *testing.T) {
for _, v := range known_variables {
KnownVariables.NewVariable(v.name, v.class, v.starlarkType)
}
fs := NewFindMockFS([]string{
"vendor/foo1/cfg.mk",
"vendor/bar/baz/cfg.mk",
"part.mk",
"foo/font.mk",
"bar/font.mk",
})
for _, test := range testCases {
t.Run(test.desc,
func(t *testing.T) {
@@ -878,6 +949,8 @@ func TestGood(t *testing.T) {
RootDir: ".",
OutputSuffix: ".star",
WarnPartialSuccess: true,
SourceFS: fs,
MakefileFinder: &testMakefileFinder{fs: fs},
})
if err != nil {
t.Error(err)