2021-08-19 21:21:30 +02:00
|
|
|
package bp2build
|
|
|
|
|
|
|
|
import (
|
2021-08-23 22:21:19 +02:00
|
|
|
"fmt"
|
2022-05-18 19:57:33 +02:00
|
|
|
"strings"
|
2021-08-19 21:21:30 +02:00
|
|
|
"testing"
|
|
|
|
|
2021-08-23 22:21:19 +02:00
|
|
|
"android/soong/android"
|
2021-08-19 21:21:30 +02:00
|
|
|
"android/soong/python"
|
|
|
|
)
|
|
|
|
|
2021-08-23 22:21:19 +02:00
|
|
|
// TODO(alexmarquez): Should be lifted into a generic Bp2Build file
|
|
|
|
type PythonLibBp2Build func(ctx android.TopDownMutatorContext)
|
|
|
|
|
2022-01-31 15:37:29 +01:00
|
|
|
type pythonLibBp2BuildTestCase struct {
|
|
|
|
description string
|
|
|
|
filesystem map[string]string
|
|
|
|
blueprint string
|
|
|
|
expectedBazelTargets []testBazelTarget
|
2022-05-18 19:57:33 +02:00
|
|
|
dir string
|
|
|
|
expectedError error
|
2022-01-31 15:37:29 +01:00
|
|
|
}
|
|
|
|
|
2022-06-21 21:28:33 +02:00
|
|
|
func convertPythonLibTestCaseToBp2build_Host(tc pythonLibBp2BuildTestCase) Bp2buildTestCase {
|
2022-01-31 15:37:29 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-06-21 21:28:33 +02:00
|
|
|
func convertPythonLibTestCaseToBp2build(tc pythonLibBp2BuildTestCase) Bp2buildTestCase {
|
2022-01-31 15:37:29 +01:00
|
|
|
var bp2BuildTargets []string
|
|
|
|
for _, t := range tc.expectedBazelTargets {
|
2022-08-31 20:28:19 +02:00
|
|
|
bp2BuildTargets = append(bp2BuildTargets, MakeBazelTarget(t.typ, t.name, t.attrs))
|
2022-01-31 15:37:29 +01:00
|
|
|
}
|
2022-05-18 19:57:33 +02:00
|
|
|
// Copy the filesystem so that we can change stuff in it later without it
|
|
|
|
// affecting the original pythonLibBp2BuildTestCase
|
|
|
|
filesystemCopy := make(map[string]string)
|
|
|
|
for k, v := range tc.filesystem {
|
|
|
|
filesystemCopy[k] = v
|
|
|
|
}
|
2022-06-21 21:28:33 +02:00
|
|
|
return Bp2buildTestCase{
|
|
|
|
Description: tc.description,
|
|
|
|
Filesystem: filesystemCopy,
|
|
|
|
Blueprint: tc.blueprint,
|
|
|
|
ExpectedBazelTargets: bp2BuildTargets,
|
|
|
|
Dir: tc.dir,
|
|
|
|
ExpectedErr: tc.expectedError,
|
2022-01-31 15:37:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runPythonLibraryTestCase(t *testing.T, tc pythonLibBp2BuildTestCase) {
|
2021-11-08 18:56:31 +01:00
|
|
|
t.Helper()
|
2022-01-31 15:37:29 +01:00
|
|
|
testCase := convertPythonLibTestCaseToBp2build(tc)
|
2022-06-21 21:28:33 +02:00
|
|
|
testCase.Description = fmt.Sprintf(testCase.Description, "python_library")
|
|
|
|
testCase.Blueprint = fmt.Sprintf(testCase.Blueprint, "python_library")
|
|
|
|
for name, contents := range testCase.Filesystem {
|
2022-05-18 19:57:33 +02:00
|
|
|
if strings.HasSuffix(name, "Android.bp") {
|
2022-06-21 21:28:33 +02:00
|
|
|
testCase.Filesystem[name] = fmt.Sprintf(contents, "python_library")
|
2022-05-18 19:57:33 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-21 21:28:33 +02:00
|
|
|
testCase.ModuleTypeUnderTest = "python_library"
|
|
|
|
testCase.ModuleTypeUnderTestFactory = python.PythonLibraryFactory
|
2022-01-31 15:37:29 +01:00
|
|
|
|
2022-09-01 20:57:01 +02:00
|
|
|
RunBp2BuildTestCaseSimple(t, testCase)
|
2021-08-23 22:21:19 +02:00
|
|
|
}
|
|
|
|
|
2022-01-31 15:37:29 +01:00
|
|
|
func runPythonLibraryHostTestCase(t *testing.T, tc pythonLibBp2BuildTestCase) {
|
2021-11-08 18:56:31 +01:00
|
|
|
t.Helper()
|
2022-01-31 15:37:29 +01:00
|
|
|
testCase := convertPythonLibTestCaseToBp2build_Host(tc)
|
2022-06-21 21:28:33 +02:00
|
|
|
testCase.Description = fmt.Sprintf(testCase.Description, "python_library_host")
|
|
|
|
testCase.Blueprint = fmt.Sprintf(testCase.Blueprint, "python_library_host")
|
|
|
|
for name, contents := range testCase.Filesystem {
|
2022-05-18 19:57:33 +02:00
|
|
|
if strings.HasSuffix(name, "Android.bp") {
|
2022-06-21 21:28:33 +02:00
|
|
|
testCase.Filesystem[name] = fmt.Sprintf(contents, "python_library_host")
|
2022-05-18 19:57:33 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-21 21:28:33 +02:00
|
|
|
testCase.ModuleTypeUnderTest = "python_library_host"
|
|
|
|
testCase.ModuleTypeUnderTestFactory = python.PythonLibraryHostFactory
|
|
|
|
RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
|
2021-11-08 18:56:31 +01:00
|
|
|
ctx.RegisterModuleType("python_library", python.PythonLibraryFactory)
|
|
|
|
},
|
|
|
|
testCase)
|
2021-08-23 22:21:19 +02:00
|
|
|
}
|
|
|
|
|
2022-01-31 15:37:29 +01:00
|
|
|
func runPythonLibraryTestCases(t *testing.T, tc pythonLibBp2BuildTestCase) {
|
2021-08-23 22:21:19 +02:00
|
|
|
t.Helper()
|
2021-11-08 18:56:31 +01:00
|
|
|
runPythonLibraryTestCase(t, tc)
|
|
|
|
runPythonLibraryHostTestCase(t, tc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimplePythonLib(t *testing.T) {
|
2022-01-31 15:37:29 +01:00
|
|
|
testCases := []pythonLibBp2BuildTestCase{
|
2021-11-08 18:56:31 +01:00
|
|
|
{
|
|
|
|
description: "simple %s converts to a native py_library",
|
|
|
|
filesystem: map[string]string{
|
|
|
|
"a.py": "",
|
|
|
|
"b/c.py": "",
|
|
|
|
"b/d.py": "",
|
|
|
|
"b/e.py": "",
|
|
|
|
"files/data.txt": "",
|
|
|
|
},
|
|
|
|
blueprint: `%s {
|
2021-08-19 21:21:30 +02:00
|
|
|
name: "foo",
|
|
|
|
srcs: ["**/*.py"],
|
|
|
|
exclude_srcs: ["b/e.py"],
|
|
|
|
data: ["files/data.txt",],
|
2021-08-26 22:13:29 +02:00
|
|
|
libs: ["bar"],
|
2021-08-19 21:21:30 +02:00
|
|
|
bazel_module: { bp2build_available: true },
|
2021-08-26 22:13:29 +02:00
|
|
|
}
|
|
|
|
python_library {
|
|
|
|
name: "bar",
|
|
|
|
srcs: ["b/e.py"],
|
|
|
|
bazel_module: { bp2build_available: false },
|
2021-11-08 18:56:31 +01:00
|
|
|
}`,
|
2022-01-31 15:37:29 +01:00
|
|
|
expectedBazelTargets: []testBazelTarget{
|
|
|
|
{
|
|
|
|
typ: "py_library",
|
|
|
|
name: "foo",
|
2022-06-21 21:28:33 +02:00
|
|
|
attrs: AttrNameToString{
|
2022-01-31 15:37:29 +01:00
|
|
|
"data": `["files/data.txt"]`,
|
|
|
|
"deps": `[":bar"]`,
|
|
|
|
"srcs": `[
|
2021-08-19 21:21:30 +02:00
|
|
|
"a.py",
|
|
|
|
"b/c.py",
|
|
|
|
"b/d.py",
|
2021-11-08 18:56:31 +01:00
|
|
|
]`,
|
2022-01-31 15:37:29 +01:00
|
|
|
"srcs_version": `"PY3"`,
|
2022-05-18 19:57:33 +02:00
|
|
|
"imports": `["."]`,
|
2022-01-31 15:37:29 +01:00
|
|
|
},
|
|
|
|
},
|
2021-11-08 18:56:31 +01:00
|
|
|
},
|
2021-08-19 21:21:30 +02:00
|
|
|
},
|
2021-11-08 18:56:31 +01:00
|
|
|
{
|
|
|
|
description: "py2 %s converts to a native py_library",
|
|
|
|
blueprint: `%s {
|
2021-08-19 21:21:30 +02:00
|
|
|
name: "foo",
|
|
|
|
srcs: ["a.py"],
|
|
|
|
version: {
|
|
|
|
py2: {
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
py3: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
bazel_module: { bp2build_available: true },
|
2021-11-08 18:56:31 +01:00
|
|
|
}`,
|
2022-01-31 15:37:29 +01:00
|
|
|
expectedBazelTargets: []testBazelTarget{
|
|
|
|
{
|
|
|
|
typ: "py_library",
|
|
|
|
name: "foo",
|
2022-06-21 21:28:33 +02:00
|
|
|
attrs: AttrNameToString{
|
2022-01-31 15:37:29 +01:00
|
|
|
"srcs": `["a.py"]`,
|
|
|
|
"srcs_version": `"PY2"`,
|
2022-05-18 19:57:33 +02:00
|
|
|
"imports": `["."]`,
|
2022-01-31 15:37:29 +01:00
|
|
|
},
|
|
|
|
},
|
2021-11-08 18:56:31 +01:00
|
|
|
},
|
2021-08-19 21:21:30 +02:00
|
|
|
},
|
2021-11-08 18:56:31 +01:00
|
|
|
{
|
|
|
|
description: "py3 %s converts to a native py_library",
|
|
|
|
blueprint: `%s {
|
2021-08-19 21:21:30 +02:00
|
|
|
name: "foo",
|
|
|
|
srcs: ["a.py"],
|
|
|
|
version: {
|
|
|
|
py2: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
|
|
|
py3: {
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
bazel_module: { bp2build_available: true },
|
2021-11-08 18:56:31 +01:00
|
|
|
}`,
|
2022-01-31 15:37:29 +01:00
|
|
|
expectedBazelTargets: []testBazelTarget{
|
|
|
|
{
|
|
|
|
typ: "py_library",
|
|
|
|
name: "foo",
|
2022-06-21 21:28:33 +02:00
|
|
|
attrs: AttrNameToString{
|
2022-01-31 15:37:29 +01:00
|
|
|
"srcs": `["a.py"]`,
|
|
|
|
"srcs_version": `"PY3"`,
|
2022-05-18 19:57:33 +02:00
|
|
|
"imports": `["."]`,
|
2022-01-31 15:37:29 +01:00
|
|
|
},
|
|
|
|
},
|
2021-11-08 18:56:31 +01:00
|
|
|
},
|
2021-08-19 21:21:30 +02:00
|
|
|
},
|
2021-11-08 18:56:31 +01:00
|
|
|
{
|
|
|
|
description: "py2&3 %s converts to a native py_library",
|
|
|
|
blueprint: `%s {
|
2021-08-19 21:21:30 +02:00
|
|
|
name: "foo",
|
|
|
|
srcs: ["a.py"],
|
|
|
|
version: {
|
|
|
|
py2: {
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
py3: {
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
bazel_module: { bp2build_available: true },
|
2021-11-08 18:56:31 +01:00
|
|
|
}`,
|
2022-01-31 15:37:29 +01:00
|
|
|
expectedBazelTargets: []testBazelTarget{
|
|
|
|
{
|
|
|
|
// srcs_version is PY2ANDPY3 by default.
|
|
|
|
typ: "py_library",
|
|
|
|
name: "foo",
|
2022-06-21 21:28:33 +02:00
|
|
|
attrs: AttrNameToString{
|
2022-05-18 19:57:33 +02:00
|
|
|
"srcs": `["a.py"]`,
|
|
|
|
"imports": `["."]`,
|
2022-01-31 15:37:29 +01:00
|
|
|
},
|
|
|
|
},
|
2021-11-08 18:56:31 +01:00
|
|
|
},
|
2021-08-19 21:21:30 +02:00
|
|
|
},
|
2022-05-18 19:57:33 +02:00
|
|
|
{
|
|
|
|
description: "%s: pkg_path in a subdirectory of the same name converts correctly",
|
|
|
|
dir: "mylib/subpackage",
|
|
|
|
filesystem: map[string]string{
|
|
|
|
"mylib/subpackage/a.py": "",
|
|
|
|
"mylib/subpackage/Android.bp": `%s {
|
|
|
|
name: "foo",
|
|
|
|
srcs: ["a.py"],
|
|
|
|
pkg_path: "mylib/subpackage",
|
|
|
|
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
blueprint: `%s {name: "bar"}`,
|
|
|
|
expectedBazelTargets: []testBazelTarget{
|
|
|
|
{
|
|
|
|
// srcs_version is PY2ANDPY3 by default.
|
|
|
|
typ: "py_library",
|
|
|
|
name: "foo",
|
2022-06-21 21:28:33 +02:00
|
|
|
attrs: AttrNameToString{
|
2022-05-18 19:57:33 +02:00
|
|
|
"srcs": `["a.py"]`,
|
|
|
|
"imports": `["../.."]`,
|
|
|
|
"srcs_version": `"PY3"`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "%s: pkg_path in a subdirectory of a different name fails",
|
|
|
|
dir: "mylib/subpackage",
|
|
|
|
filesystem: map[string]string{
|
|
|
|
"mylib/subpackage/a.py": "",
|
|
|
|
"mylib/subpackage/Android.bp": `%s {
|
|
|
|
name: "foo",
|
|
|
|
srcs: ["a.py"],
|
|
|
|
pkg_path: "mylib/subpackage2",
|
|
|
|
bazel_module: { bp2build_available: true },
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
blueprint: `%s {name: "bar"}`,
|
|
|
|
expectedError: fmt.Errorf("Currently, bp2build only supports pkg_paths that are the same as the folders the Android.bp file is in."),
|
|
|
|
},
|
2021-11-08 18:56:31 +01:00
|
|
|
}
|
2021-09-17 22:30:21 +02:00
|
|
|
|
2021-11-08 18:56:31 +01:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.description, func(t *testing.T) {
|
|
|
|
runPythonLibraryTestCases(t, tc)
|
|
|
|
})
|
|
|
|
}
|
2021-09-17 22:30:21 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 18:56:31 +01:00
|
|
|
func TestPythonArchVariance(t *testing.T) {
|
2022-01-31 15:37:29 +01:00
|
|
|
runPythonLibraryTestCases(t, pythonLibBp2BuildTestCase{
|
2021-11-08 18:56:31 +01:00
|
|
|
description: "test %s arch variants",
|
2021-09-17 22:30:21 +02:00
|
|
|
filesystem: map[string]string{
|
|
|
|
"dir/arm.py": "",
|
|
|
|
"dir/x86.py": "",
|
|
|
|
},
|
2021-11-08 18:56:31 +01:00
|
|
|
blueprint: `%s {
|
2021-09-17 22:30:21 +02:00
|
|
|
name: "foo",
|
|
|
|
arch: {
|
|
|
|
arm: {
|
|
|
|
srcs: ["arm.py"],
|
|
|
|
},
|
|
|
|
x86: {
|
|
|
|
srcs: ["x86.py"],
|
|
|
|
},
|
|
|
|
},
|
2021-11-08 18:56:31 +01:00
|
|
|
}`,
|
2022-01-31 15:37:29 +01:00
|
|
|
expectedBazelTargets: []testBazelTarget{
|
|
|
|
{
|
|
|
|
typ: "py_library",
|
|
|
|
name: "foo",
|
2022-06-21 21:28:33 +02:00
|
|
|
attrs: AttrNameToString{
|
2022-01-31 15:37:29 +01:00
|
|
|
"srcs": `select({
|
2021-09-17 22:30:21 +02:00
|
|
|
"//build/bazel/platforms/arch:arm": ["arm.py"],
|
|
|
|
"//build/bazel/platforms/arch:x86": ["x86.py"],
|
|
|
|
"//conditions:default": [],
|
2021-11-08 18:56:31 +01:00
|
|
|
})`,
|
2022-01-31 15:37:29 +01:00
|
|
|
"srcs_version": `"PY3"`,
|
2022-05-18 19:57:33 +02:00
|
|
|
"imports": `["."]`,
|
2022-01-31 15:37:29 +01:00
|
|
|
},
|
|
|
|
},
|
2021-09-17 22:30:21 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2022-05-13 00:37:02 +02:00
|
|
|
|
|
|
|
func TestPythonLibraryWithProtobufs(t *testing.T) {
|
|
|
|
runPythonLibraryTestCases(t, pythonLibBp2BuildTestCase{
|
|
|
|
description: "test %s protobuf",
|
|
|
|
filesystem: map[string]string{
|
|
|
|
"dir/mylib.py": "",
|
|
|
|
"dir/myproto.proto": "",
|
|
|
|
},
|
|
|
|
blueprint: `%s {
|
|
|
|
name: "foo",
|
|
|
|
srcs: [
|
|
|
|
"dir/mylib.py",
|
|
|
|
"dir/myproto.proto",
|
|
|
|
],
|
|
|
|
}`,
|
|
|
|
expectedBazelTargets: []testBazelTarget{
|
|
|
|
{
|
|
|
|
typ: "proto_library",
|
|
|
|
name: "foo_proto",
|
2022-06-21 21:28:33 +02:00
|
|
|
attrs: AttrNameToString{
|
2022-05-13 00:37:02 +02:00
|
|
|
"srcs": `["dir/myproto.proto"]`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
typ: "py_proto_library",
|
|
|
|
name: "foo_py_proto",
|
2022-06-21 21:28:33 +02:00
|
|
|
attrs: AttrNameToString{
|
2022-05-13 00:37:02 +02:00
|
|
|
"deps": `[":foo_proto"]`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
typ: "py_library",
|
|
|
|
name: "foo",
|
2022-06-21 21:28:33 +02:00
|
|
|
attrs: AttrNameToString{
|
2022-05-13 00:37:02 +02:00
|
|
|
"srcs": `["dir/mylib.py"]`,
|
|
|
|
"srcs_version": `"PY3"`,
|
|
|
|
"imports": `["."]`,
|
|
|
|
"deps": `[":foo_py_proto"]`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2023-08-22 21:24:07 +02:00
|
|
|
|
|
|
|
func TestPythonLibraryWithProtobufsAndPkgPath(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
runBp2BuildTestCaseWithPythonLibraries(t, Bp2buildTestCase{
|
|
|
|
Description: "test python_library protobuf with pkg_path",
|
|
|
|
Filesystem: map[string]string{
|
|
|
|
"dir/foo.proto": "",
|
|
|
|
"dir/bar.proto": "", // bar contains "import dir/foo.proto"
|
|
|
|
"dir/Android.bp": `
|
|
|
|
python_library {
|
|
|
|
name: "foo",
|
|
|
|
pkg_path: "dir",
|
|
|
|
srcs: [
|
|
|
|
"foo.proto",
|
|
|
|
"bar.proto",
|
|
|
|
],
|
|
|
|
bazel_module: {bp2build_available: true},
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
Dir: "dir",
|
|
|
|
ExpectedBazelTargets: []string{
|
|
|
|
MakeBazelTarget("proto_library", "foo_proto", AttrNameToString{
|
|
|
|
"import_prefix": `"dir"`,
|
|
|
|
"strip_import_prefix": `""`,
|
|
|
|
"srcs": `[
|
|
|
|
"foo.proto",
|
|
|
|
"bar.proto",
|
|
|
|
]`,
|
|
|
|
}),
|
|
|
|
MakeBazelTarget("py_proto_library", "foo_py_proto", AttrNameToString{
|
|
|
|
"deps": `[":foo_proto"]`,
|
|
|
|
}),
|
|
|
|
MakeBazelTarget("py_library", "foo", AttrNameToString{
|
|
|
|
"srcs_version": `"PY3"`,
|
|
|
|
"imports": `[".."]`,
|
|
|
|
"deps": `[":foo_py_proto"]`,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|