add target_compatible_with stanza for host targets

Soong modules that are specific for the host platform (e.g.
java_library_host, cc_binary_host, java_genrule_host, etc.) should
not be built on the target platform (Android), so we add a
target_compatible_with attribute to skip this type of module on an
Android target build.

Bug: 215229742
Test: go test ./bp2build
Change-Id: Ifb76ef4e0dc4cb3adb6a64b5c375ce36f7973e48
This commit is contained in:
Sam Delmerico
2022-01-31 14:37:29 +00:00
parent d519b331f2
commit 75539d62ae
9 changed files with 236 additions and 95 deletions

View File

@@ -11,19 +11,51 @@ import (
// TODO(alexmarquez): Should be lifted into a generic Bp2Build file
type PythonLibBp2Build func(ctx android.TopDownMutatorContext)
func runPythonLibraryTestCase(t *testing.T, tc bp2buildTestCase) {
type pythonLibBp2BuildTestCase struct {
description string
filesystem map[string]string
blueprint string
expectedBazelTargets []testBazelTarget
}
func convertPythonLibTestCaseToBp2build_Host(tc pythonLibBp2BuildTestCase) bp2buildTestCase {
for i := range tc.expectedBazelTargets {
tc.expectedBazelTargets[i].attrs["target_compatible_with"] = `select({
"//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
"//conditions:default": [],
})`
}
return convertPythonLibTestCaseToBp2build(tc)
}
func convertPythonLibTestCaseToBp2build(tc pythonLibBp2BuildTestCase) bp2buildTestCase {
var bp2BuildTargets []string
for _, t := range tc.expectedBazelTargets {
bp2BuildTargets = append(bp2BuildTargets, makeBazelTarget(t.typ, t.name, t.attrs))
}
return bp2buildTestCase{
description: tc.description,
filesystem: tc.filesystem,
blueprint: tc.blueprint,
expectedBazelTargets: bp2BuildTargets,
}
}
func runPythonLibraryTestCase(t *testing.T, tc pythonLibBp2BuildTestCase) {
t.Helper()
testCase := tc
testCase := convertPythonLibTestCaseToBp2build(tc)
testCase.description = fmt.Sprintf(testCase.description, "python_library")
testCase.blueprint = fmt.Sprintf(testCase.blueprint, "python_library")
testCase.moduleTypeUnderTest = "python_library"
testCase.moduleTypeUnderTestFactory = python.PythonLibraryFactory
runBp2BuildTestCaseSimple(t, testCase)
}
func runPythonLibraryHostTestCase(t *testing.T, tc bp2buildTestCase) {
func runPythonLibraryHostTestCase(t *testing.T, tc pythonLibBp2BuildTestCase) {
t.Helper()
testCase := tc
testCase := convertPythonLibTestCaseToBp2build_Host(tc)
testCase.description = fmt.Sprintf(testCase.description, "python_library_host")
testCase.blueprint = fmt.Sprintf(testCase.blueprint, "python_library_host")
testCase.moduleTypeUnderTest = "python_library_host"
@@ -34,14 +66,14 @@ func runPythonLibraryHostTestCase(t *testing.T, tc bp2buildTestCase) {
testCase)
}
func runPythonLibraryTestCases(t *testing.T, tc bp2buildTestCase) {
func runPythonLibraryTestCases(t *testing.T, tc pythonLibBp2BuildTestCase) {
t.Helper()
runPythonLibraryTestCase(t, tc)
runPythonLibraryHostTestCase(t, tc)
}
func TestSimplePythonLib(t *testing.T) {
testCases := []bp2buildTestCase{
testCases := []pythonLibBp2BuildTestCase{
{
description: "simple %s converts to a native py_library",
filesystem: map[string]string{
@@ -64,17 +96,21 @@ func TestSimplePythonLib(t *testing.T) {
srcs: ["b/e.py"],
bazel_module: { bp2build_available: false },
}`,
expectedBazelTargets: []string{
makeBazelTarget("py_library", "foo", attrNameToString{
"data": `["files/data.txt"]`,
"deps": `[":bar"]`,
"srcs": `[
expectedBazelTargets: []testBazelTarget{
{
typ: "py_library",
name: "foo",
attrs: attrNameToString{
"data": `["files/data.txt"]`,
"deps": `[":bar"]`,
"srcs": `[
"a.py",
"b/c.py",
"b/d.py",
]`,
"srcs_version": `"PY3"`,
}),
"srcs_version": `"PY3"`,
},
},
},
},
{
@@ -93,11 +129,15 @@ func TestSimplePythonLib(t *testing.T) {
bazel_module: { bp2build_available: true },
}`,
expectedBazelTargets: []string{
makeBazelTarget("py_library", "foo", attrNameToString{
"srcs": `["a.py"]`,
"srcs_version": `"PY2"`,
}),
expectedBazelTargets: []testBazelTarget{
{
typ: "py_library",
name: "foo",
attrs: attrNameToString{
"srcs": `["a.py"]`,
"srcs_version": `"PY2"`,
},
},
},
},
{
@@ -116,11 +156,15 @@ func TestSimplePythonLib(t *testing.T) {
bazel_module: { bp2build_available: true },
}`,
expectedBazelTargets: []string{
makeBazelTarget("py_library", "foo", attrNameToString{
"srcs": `["a.py"]`,
"srcs_version": `"PY3"`,
}),
expectedBazelTargets: []testBazelTarget{
{
typ: "py_library",
name: "foo",
attrs: attrNameToString{
"srcs": `["a.py"]`,
"srcs_version": `"PY3"`,
},
},
},
},
{
@@ -139,11 +183,15 @@ func TestSimplePythonLib(t *testing.T) {
bazel_module: { bp2build_available: true },
}`,
expectedBazelTargets: []string{
// srcs_version is PY2ANDPY3 by default.
makeBazelTarget("py_library", "foo", attrNameToString{
"srcs": `["a.py"]`,
}),
expectedBazelTargets: []testBazelTarget{
{
// srcs_version is PY2ANDPY3 by default.
typ: "py_library",
name: "foo",
attrs: attrNameToString{
"srcs": `["a.py"]`,
},
},
},
},
}
@@ -156,7 +204,7 @@ func TestSimplePythonLib(t *testing.T) {
}
func TestPythonArchVariance(t *testing.T) {
runPythonLibraryTestCases(t, bp2buildTestCase{
runPythonLibraryTestCases(t, pythonLibBp2BuildTestCase{
description: "test %s arch variants",
filesystem: map[string]string{
"dir/arm.py": "",
@@ -173,15 +221,19 @@ func TestPythonArchVariance(t *testing.T) {
},
},
}`,
expectedBazelTargets: []string{
makeBazelTarget("py_library", "foo", attrNameToString{
"srcs": `select({
expectedBazelTargets: []testBazelTarget{
{
typ: "py_library",
name: "foo",
attrs: attrNameToString{
"srcs": `select({
"//build/bazel/platforms/arch:arm": ["arm.py"],
"//build/bazel/platforms/arch:x86": ["x86.py"],
"//conditions:default": [],
})`,
"srcs_version": `"PY3"`,
}),
"srcs_version": `"PY3"`,
},
},
},
})
}