Implement bp2build converter for aidl_library
Test: go test
Bug: 278704136
(cherry picked from https://android-review.googlesource.com/q/commit:3d16990b29fe2a4a8deed9769908f8496755adde)
Merged-In: Ia9c3772257af58e1de9041ba465130740b555fe4
Change-Id: Ia9c3772257af58e1de9041ba465130740b555fe4
diff --git a/aidl_library/aidl_library.go b/aidl_library/aidl_library.go
index 2a42170..9b5f0a8 100644
--- a/aidl_library/aidl_library.go
+++ b/aidl_library/aidl_library.go
@@ -16,6 +16,7 @@
import (
"android/soong/android"
+ "android/soong/bazel"
"github.com/google/blueprint"
"github.com/google/blueprint/proptools"
@@ -48,9 +49,57 @@
type AidlLibrary struct {
android.ModuleBase
+ android.BazelModuleBase
properties aidlLibraryProperties
}
+type bazelAidlLibraryAttributes struct {
+ Srcs bazel.LabelListAttribute
+ Hdrs bazel.LabelListAttribute
+ Strip_import_prefix *string
+ Deps bazel.LabelListAttribute
+}
+
+func (lib *AidlLibrary) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
+ srcs := bazel.MakeLabelListAttribute(
+ android.BazelLabelForModuleSrc(
+ ctx,
+ lib.properties.Srcs,
+ ),
+ )
+
+ hdrs := bazel.MakeLabelListAttribute(
+ android.BazelLabelForModuleSrc(
+ ctx,
+ lib.properties.Hdrs,
+ ),
+ )
+
+ tags := []string{"apex_available=//apex_available:anyapex"}
+ deps := bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, lib.properties.Deps))
+
+ attrs := &bazelAidlLibraryAttributes{
+ Srcs: srcs,
+ Hdrs: hdrs,
+ Strip_import_prefix: lib.properties.Strip_import_prefix,
+ Deps: deps,
+ }
+
+ props := bazel.BazelTargetModuleProperties{
+ Rule_class: "aidl_library",
+ Bzl_load_location: "//build/bazel/rules/aidl:aidl_library.bzl",
+ }
+
+ ctx.CreateBazelTargetModule(
+ props,
+ android.CommonAttributes{
+ Name: lib.Name(),
+ Tags: bazel.MakeStringListAttribute(tags),
+ },
+ attrs,
+ )
+}
+
type AidlLibraryInfo struct {
// The direct aidl files of the module
Srcs android.Paths
@@ -104,6 +153,7 @@
module := &AidlLibrary{}
module.AddProperties(&module.properties)
android.InitAndroidModule(module)
+ android.InitBazelModule(module)
return module
}
diff --git a/bp2build/Android.bp b/bp2build/Android.bp
index b6635c4..9ec3a40 100644
--- a/bp2build/Android.bp
+++ b/bp2build/Android.bp
@@ -19,6 +19,7 @@
"testing.go",
],
deps: [
+ "soong-aidl-library",
"soong-android",
"soong-android-allowlists",
"soong-android-soongconfig",
@@ -37,6 +38,7 @@
],
testSrcs: [
"aar_conversion_test.go",
+ "aidl_library_conversion_test.go",
"android_app_certificate_conversion_test.go",
"android_app_conversion_test.go",
"apex_conversion_test.go",
diff --git a/bp2build/aidl_library_conversion_test.go b/bp2build/aidl_library_conversion_test.go
new file mode 100644
index 0000000..0522da4
--- /dev/null
+++ b/bp2build/aidl_library_conversion_test.go
@@ -0,0 +1,119 @@
+// Copyright 2023 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package bp2build
+
+import (
+ "testing"
+
+ "android/soong/aidl_library"
+ "android/soong/android"
+)
+
+func runAidlLibraryTestCase(t *testing.T, tc Bp2buildTestCase) {
+ t.Helper()
+ (&tc).ModuleTypeUnderTest = "aidl_library"
+ (&tc).ModuleTypeUnderTestFactory = aidl_library.AidlLibraryFactory
+ RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
+}
+
+func TestAidlLibrary(t *testing.T) {
+ testcases := []struct {
+ name string
+ bp string
+ expectedBazelAttrs AttrNameToString
+ }{
+ {
+ name: "aidl_library with strip_import_prefix",
+ bp: `
+ aidl_library {
+ name: "foo",
+ srcs: ["aidl/foo.aidl"],
+ hdrs: ["aidl/header.aidl"],
+ strip_import_prefix: "aidl",
+ }`,
+ expectedBazelAttrs: AttrNameToString{
+ "srcs": `["aidl/foo.aidl"]`,
+ "hdrs": `["aidl/header.aidl"]`,
+ "strip_import_prefix": `"aidl"`,
+ "tags": `["apex_available=//apex_available:anyapex"]`,
+ },
+ },
+ {
+ name: "aidl_library without strip_import_prefix",
+ bp: `
+ aidl_library {
+ name: "foo",
+ srcs: ["aidl/foo.aidl"],
+ hdrs: ["aidl/header.aidl"],
+ }`,
+ expectedBazelAttrs: AttrNameToString{
+ "srcs": `["aidl/foo.aidl"]`,
+ "hdrs": `["aidl/header.aidl"]`,
+ "tags": `["apex_available=//apex_available:anyapex"]`,
+ },
+ },
+ }
+
+ for _, test := range testcases {
+ t.Run(test.name, func(t *testing.T) {
+ expectedBazelTargets := []string{
+ MakeBazelTargetNoRestrictions("aidl_library", "foo", test.expectedBazelAttrs),
+ }
+ runAidlLibraryTestCase(t, Bp2buildTestCase{
+ Description: test.name,
+ Blueprint: test.bp,
+ ExpectedBazelTargets: expectedBazelTargets,
+ })
+ })
+ }
+}
+
+func TestAidlLibraryWithDeps(t *testing.T) {
+ bp := `
+ aidl_library {
+ name: "bar",
+ srcs: ["Bar.aidl"],
+ hdrs: ["aidl/BarHeader.aidl"],
+ }
+ aidl_library {
+ name: "foo",
+ srcs: ["aidl/Foo.aidl"],
+ hdrs: ["aidl/FooHeader.aidl"],
+ strip_import_prefix: "aidl",
+ deps: ["bar"],
+ }`
+
+ t.Run("aidl_library with deps", func(t *testing.T) {
+ expectedBazelTargets := []string{
+ MakeBazelTargetNoRestrictions("aidl_library", "bar", AttrNameToString{
+ "srcs": `["Bar.aidl"]`,
+ "hdrs": `["aidl/BarHeader.aidl"]`,
+ "tags": `["apex_available=//apex_available:anyapex"]`,
+ }),
+ MakeBazelTargetNoRestrictions("aidl_library", "foo", AttrNameToString{
+ "srcs": `["aidl/Foo.aidl"]`,
+ "hdrs": `["aidl/FooHeader.aidl"]`,
+ "strip_import_prefix": `"aidl"`,
+ "deps": `[":bar"]`,
+ "tags": `["apex_available=//apex_available:anyapex"]`,
+ }),
+ }
+ runAidlLibraryTestCase(t, Bp2buildTestCase{
+ Description: "aidl_library with deps",
+ Blueprint: bp,
+ ExpectedBazelTargets: expectedBazelTargets,
+ })
+ })
+}