Add python_library_host -> py_library bp2build support

Bug: 196081770
Test: bp2build/python_library_conversion_test.go
Test: build/bazel/ci/mixed_{libc,droid}.sh
Change-Id: I4da9938eb0b039f97b83badd2269af153c7edbcc
This commit is contained in:
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux
2021-08-23 20:21:19 +00:00
parent b37a92ccc8
commit dc212c0b24
2 changed files with 62 additions and 41 deletions

View File

@@ -1,17 +1,35 @@
package bp2build package bp2build
import ( import (
"fmt"
"testing" "testing"
"android/soong/android"
"android/soong/python" "android/soong/python"
) )
func TestPythonLibrarySimple(t *testing.T) { // TODO(alexmarquez): Should be lifted into a generic Bp2Build file
type PythonLibBp2Build func(ctx android.TopDownMutatorContext)
func TestPythonLibrary(t *testing.T) {
testPythonLib(t, "python_library",
python.PythonLibraryFactory, python.PythonLibraryBp2Build)
}
func TestPythonLibraryHost(t *testing.T) {
testPythonLib(t, "python_library_host",
python.PythonLibraryHostFactory, python.PythonLibraryHostBp2Build)
}
func testPythonLib(t *testing.T, modType string,
factory android.ModuleFactory, mutator PythonLibBp2Build) {
t.Helper()
// Simple
runBp2BuildTestCaseSimple(t, bp2buildTestCase{ runBp2BuildTestCaseSimple(t, bp2buildTestCase{
description: "simple python_library converts to a native py_library", description: fmt.Sprintf("simple %s converts to a native py_library", modType),
moduleTypeUnderTest: "python_library", moduleTypeUnderTest: modType,
moduleTypeUnderTestFactory: python.PythonLibraryFactory, moduleTypeUnderTestFactory: factory,
moduleTypeUnderTestBp2BuildMutator: python.PythonLibraryBp2Build, moduleTypeUnderTestBp2BuildMutator: mutator,
filesystem: map[string]string{ filesystem: map[string]string{
"a.py": "", "a.py": "",
"b/c.py": "", "b/c.py": "",
@@ -19,14 +37,13 @@ func TestPythonLibrarySimple(t *testing.T) {
"b/e.py": "", "b/e.py": "",
"files/data.txt": "", "files/data.txt": "",
}, },
blueprint: `python_library { blueprint: fmt.Sprintf(`%s {
name: "foo", name: "foo",
srcs: ["**/*.py"], srcs: ["**/*.py"],
exclude_srcs: ["b/e.py"], exclude_srcs: ["b/e.py"],
data: ["files/data.txt",], data: ["files/data.txt",],
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }`, modType),
`,
expectedBazelTargets: []string{`py_library( expectedBazelTargets: []string{`py_library(
name = "foo", name = "foo",
data = ["files/data.txt"], data = ["files/data.txt"],
@@ -39,15 +56,14 @@ func TestPythonLibrarySimple(t *testing.T) {
)`, )`,
}, },
}) })
}
func TestPythonLibraryPy2(t *testing.T) { // PY2
runBp2BuildTestCaseSimple(t, bp2buildTestCase{ runBp2BuildTestCaseSimple(t, bp2buildTestCase{
description: "py2 python_library", description: fmt.Sprintf("py2 %s converts to a native py_library", modType),
moduleTypeUnderTest: "python_library", moduleTypeUnderTest: modType,
moduleTypeUnderTestFactory: python.PythonLibraryFactory, moduleTypeUnderTestFactory: factory,
moduleTypeUnderTestBp2BuildMutator: python.PythonLibraryBp2Build, moduleTypeUnderTestBp2BuildMutator: mutator,
blueprint: `python_library { blueprint: fmt.Sprintf(`%s {
name: "foo", name: "foo",
srcs: ["a.py"], srcs: ["a.py"],
version: { version: {
@@ -60,8 +76,7 @@ func TestPythonLibraryPy2(t *testing.T) {
}, },
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }`, modType),
`,
expectedBazelTargets: []string{`py_library( expectedBazelTargets: []string{`py_library(
name = "foo", name = "foo",
srcs = ["a.py"], srcs = ["a.py"],
@@ -69,15 +84,14 @@ func TestPythonLibraryPy2(t *testing.T) {
)`, )`,
}, },
}) })
}
func TestPythonLibraryPy3(t *testing.T) { // PY3
runBp2BuildTestCaseSimple(t, bp2buildTestCase{ runBp2BuildTestCaseSimple(t, bp2buildTestCase{
description: "py3 python_library", description: fmt.Sprintf("py3 %s converts to a native py_library", modType),
moduleTypeUnderTest: "python_library", moduleTypeUnderTest: modType,
moduleTypeUnderTestFactory: python.PythonLibraryFactory, moduleTypeUnderTestFactory: factory,
moduleTypeUnderTestBp2BuildMutator: python.PythonLibraryBp2Build, moduleTypeUnderTestBp2BuildMutator: mutator,
blueprint: `python_library { blueprint: fmt.Sprintf(`%s {
name: "foo", name: "foo",
srcs: ["a.py"], srcs: ["a.py"],
version: { version: {
@@ -90,25 +104,22 @@ func TestPythonLibraryPy3(t *testing.T) {
}, },
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }`, modType),
`, expectedBazelTargets: []string{`py_library(
expectedBazelTargets: []string{
`py_library(
name = "foo", name = "foo",
srcs = ["a.py"], srcs = ["a.py"],
srcs_version = "PY3", srcs_version = "PY3",
)`, )`,
}, },
}) })
}
func TestPythonLibraryPyBoth(t *testing.T) { // Both
runBp2BuildTestCaseSimple(t, bp2buildTestCase{ runBp2BuildTestCaseSimple(t, bp2buildTestCase{
description: "py3 python_library", description: fmt.Sprintf("py2&3 %s converts to a native py_library", modType),
moduleTypeUnderTest: "python_library", moduleTypeUnderTest: modType,
moduleTypeUnderTestFactory: python.PythonLibraryFactory, moduleTypeUnderTestFactory: factory,
moduleTypeUnderTestBp2BuildMutator: python.PythonLibraryBp2Build, moduleTypeUnderTestBp2BuildMutator: mutator,
blueprint: `python_library { blueprint: fmt.Sprintf(`%s {
name: "foo", name: "foo",
srcs: ["a.py"], srcs: ["a.py"],
version: { version: {
@@ -121,8 +132,7 @@ func TestPythonLibraryPyBoth(t *testing.T) {
}, },
bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true },
} }`, modType),
`,
expectedBazelTargets: []string{ expectedBazelTargets: []string{
// srcs_version is PY2ANDPY3 by default. // srcs_version is PY2ANDPY3 by default.
`py_library( `py_library(

View File

@@ -26,6 +26,7 @@ import (
func init() { func init() {
registerPythonLibraryComponents(android.InitRegistrationContext) registerPythonLibraryComponents(android.InitRegistrationContext)
android.RegisterBp2BuildMutator("python_library_host", PythonLibraryHostBp2Build)
android.RegisterBp2BuildMutator("python_library", PythonLibraryBp2Build) android.RegisterBp2BuildMutator("python_library", PythonLibraryBp2Build)
} }
@@ -37,6 +38,8 @@ func registerPythonLibraryComponents(ctx android.RegistrationContext) {
func PythonLibraryHostFactory() android.Module { func PythonLibraryHostFactory() android.Module {
module := newModule(android.HostSupported, android.MultilibFirst) module := newModule(android.HostSupported, android.MultilibFirst)
android.InitBazelModule(module)
return module.init() return module.init()
} }
@@ -46,14 +49,22 @@ type bazelPythonLibraryAttributes struct {
Srcs_version string Srcs_version string
} }
func PythonLibraryHostBp2Build(ctx android.TopDownMutatorContext) {
pythonLibBp2Build(ctx, "python_library_host")
}
func PythonLibraryBp2Build(ctx android.TopDownMutatorContext) { func PythonLibraryBp2Build(ctx android.TopDownMutatorContext) {
pythonLibBp2Build(ctx, "python_library")
}
func pythonLibBp2Build(ctx android.TopDownMutatorContext, modType string) {
m, ok := ctx.Module().(*Module) m, ok := ctx.Module().(*Module)
if !ok || !m.ConvertWithBp2build(ctx) { if !ok || !m.ConvertWithBp2build(ctx) {
return return
} }
// a Module can be something other than a python_library // a Module can be something other than a `modType`
if ctx.ModuleType() != "python_library" { if ctx.ModuleType() != modType {
return return
} }
@@ -70,8 +81,8 @@ func PythonLibraryBp2Build(ctx android.TopDownMutatorContext) {
python_version = "PY3" python_version = "PY3"
} else if !py2Enabled && !py3Enabled { } else if !py2Enabled && !py3Enabled {
panic(fmt.Errorf( panic(fmt.Errorf(
"error for '%s' module: bp2build's python_library converter doesn't understand having "+ "error for '%s' module: bp2build's %s converter doesn't understand having "+
"neither py2 nor py3 enabled", m.Name())) "neither py2 nor py3 enabled", m.Name(), modType))
} else { } else {
// do nothing, since python_version defaults to PY2ANDPY3 // do nothing, since python_version defaults to PY2ANDPY3
} }