Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

python_{binary,library}{,_host} handle lib property

Have binary and library bp2build convert `libs` to
`deps` for bazel

Bug: 196083751
Test: python_{binary,library}_conversion_test.go
Test: build/bazel/ci/mixed_{libc,droid}.sh
Change-Id: I2d5f6ef2e83dd608910edb7adb2eb9a56d25293c
parent 5602d330
Loading
Loading
Loading
Loading
+16 −2
Original line number Diff line number Diff line
@@ -3,11 +3,19 @@ package bp2build
import (
	"testing"

	"android/soong/android"
	"android/soong/python"
)

func runBp2BuildTestCaseWithLibs(t *testing.T, tc bp2buildTestCase) {
	runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {
		ctx.RegisterModuleType("python_library", python.PythonLibraryFactory)
		ctx.RegisterModuleType("python_library_host", python.PythonLibraryHostFactory)
	}, tc)
}

func TestPythonBinaryHostSimple(t *testing.T) {
	runBp2BuildTestCaseSimple(t, bp2buildTestCase{
	runBp2BuildTestCaseWithLibs(t, bp2buildTestCase{
		description:                        "simple python_binary_host converts to a native py_binary",
		moduleTypeUnderTest:                "python_binary_host",
		moduleTypeUnderTestFactory:         python.PythonBinaryHostFactory,
@@ -25,12 +33,18 @@ func TestPythonBinaryHostSimple(t *testing.T) {
    srcs: ["**/*.py"],
    exclude_srcs: ["b/e.py"],
    data: ["files/data.txt",],
    libs: ["bar"],
    bazel_module: { bp2build_available: true },
}
`,
    python_library_host {
      name: "bar",
      srcs: ["b/e.py"],
      bazel_module: { bp2build_available: true },
    }`,
		expectedBazelTargets: []string{`py_binary(
    name = "foo",
    data = ["files/data.txt"],
    deps = [":bar"],
    main = "a.py",
    srcs = [
        "a.py",
+17 −5
Original line number Diff line number Diff line
@@ -13,19 +13,24 @@ type PythonLibBp2Build func(ctx android.TopDownMutatorContext)

func TestPythonLibrary(t *testing.T) {
	testPythonLib(t, "python_library",
		python.PythonLibraryFactory, python.PythonLibraryBp2Build)
		python.PythonLibraryFactory, python.PythonLibraryBp2Build,
		func(ctx android.RegistrationContext) {})
}

func TestPythonLibraryHost(t *testing.T) {
	testPythonLib(t, "python_library_host",
		python.PythonLibraryHostFactory, python.PythonLibraryHostBp2Build)
		python.PythonLibraryHostFactory, python.PythonLibraryHostBp2Build,
		func(ctx android.RegistrationContext) {
			ctx.RegisterModuleType("python_library", python.PythonLibraryFactory)
		})
}

func testPythonLib(t *testing.T, modType string,
	factory android.ModuleFactory, mutator PythonLibBp2Build) {
	factory android.ModuleFactory, mutator PythonLibBp2Build,
	registration func(ctx android.RegistrationContext)) {
	t.Helper()
	// Simple
	runBp2BuildTestCaseSimple(t, bp2buildTestCase{
	runBp2BuildTestCase(t, registration, bp2buildTestCase{
		description:                        fmt.Sprintf("simple %s converts to a native py_library", modType),
		moduleTypeUnderTest:                modType,
		moduleTypeUnderTestFactory:         factory,
@@ -42,11 +47,18 @@ func testPythonLib(t *testing.T, modType string,
    srcs: ["**/*.py"],
    exclude_srcs: ["b/e.py"],
    data: ["files/data.txt",],
    libs: ["bar"],
    bazel_module: { bp2build_available: true },
}
    python_library {
      name: "bar",
      srcs: ["b/e.py"],
      bazel_module: { bp2build_available: false },
    }`, modType),
		expectedBazelTargets: []string{`py_library(
    name = "foo",
    data = ["files/data.txt"],
    deps = [":bar"],
    srcs = [
        "a.py",
        "b/c.py",
+2 −1
Original line number Diff line number Diff line
@@ -101,7 +101,8 @@ func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.Regi
	codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
	bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
	if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount {
		t.Errorf("%s: Expected %d bazel target, got %d", tc.description, expectedCount, actualCount)
		t.Errorf("%s: Expected %d bazel target, got %d; %v",
			tc.description, expectedCount, actualCount, bazelTargets)
	} else {
		for i, target := range bazelTargets {
			if w, g := tc.expectedBazelTargets[i], target.content; w != g {
+3 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ type bazelPythonBinaryAttributes struct {
	Main           string
	Srcs           bazel.LabelListAttribute
	Data           bazel.LabelListAttribute
	Deps           bazel.LabelListAttribute
	Python_version string
}

@@ -81,11 +82,13 @@ func PythonBinaryBp2Build(ctx android.TopDownMutatorContext) {

	srcs := android.BazelLabelForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs)
	data := android.BazelLabelForModuleSrc(ctx, m.properties.Data)
	deps := android.BazelLabelForModuleDeps(ctx, m.properties.Libs)

	attrs := &bazelPythonBinaryAttributes{
		Main:           main,
		Srcs:           bazel.MakeLabelListAttribute(srcs),
		Data:           bazel.MakeLabelListAttribute(data),
		Deps:           bazel.MakeLabelListAttribute(deps),
		Python_version: python_version,
	}

+3 −0
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ func PythonLibraryHostFactory() android.Module {
type bazelPythonLibraryAttributes struct {
	Srcs         bazel.LabelListAttribute
	Data         bazel.LabelListAttribute
	Deps         bazel.LabelListAttribute
	Srcs_version string
}

@@ -89,10 +90,12 @@ func pythonLibBp2Build(ctx android.TopDownMutatorContext, modType string) {

	srcs := android.BazelLabelForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs)
	data := android.BazelLabelForModuleSrc(ctx, m.properties.Data)
	deps := android.BazelLabelForModuleDeps(ctx, m.properties.Libs)

	attrs := &bazelPythonLibraryAttributes{
		Srcs:         bazel.MakeLabelListAttribute(srcs),
		Data:         bazel.MakeLabelListAttribute(data),
		Deps:         bazel.MakeLabelListAttribute(deps),
		Srcs_version: python_version,
	}