diff --git a/bp2build/Android.bp b/bp2build/Android.bp index 6ab062e0c..35f8c059b 100644 --- a/bp2build/Android.bp +++ b/bp2build/Android.bp @@ -28,6 +28,7 @@ bootstrap_go_package { "soong-sh", ], testSrcs: [ + "android_app_certificate_conversion_test.go", "apex_conversion_test.go", "apex_key_conversion_test.go", "build_conversion_test.go", diff --git a/bp2build/android_app_certificate_conversion_test.go b/bp2build/android_app_certificate_conversion_test.go new file mode 100644 index 000000000..022c6871e --- /dev/null +++ b/bp2build/android_app_certificate_conversion_test.go @@ -0,0 +1,49 @@ +// Copyright 2021 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 ( + "android/soong/android" + "android/soong/java" + + "testing" +) + +func runAndroidAppCertificateTestCase(t *testing.T, tc bp2buildTestCase) { + t.Helper() + runBp2BuildTestCase(t, registerAndroidAppCertificateModuleTypes, tc) +} + +func registerAndroidAppCertificateModuleTypes(ctx android.RegistrationContext) { +} + +func TestAndroidAppCertificateSimple(t *testing.T) { + runAndroidAppCertificateTestCase(t, bp2buildTestCase{ + description: "Android app certificate - simple example", + moduleTypeUnderTest: "android_app_certificate", + moduleTypeUnderTestFactory: java.AndroidAppCertificateFactory, + moduleTypeUnderTestBp2BuildMutator: java.AndroidAppCertificateBp2Build, + filesystem: map[string]string{}, + blueprint: ` +android_app_certificate { + name: "com.android.apogee.cert", + certificate: "chamber_of_secrets_dir", +} +`, + expectedBazelTargets: []string{`android_app_certificate( + name = "com.android.apogee.cert", + certificate = "chamber_of_secrets_dir", +)`}}) +} diff --git a/java/Android.bp b/java/Android.bp index e5b8f9604..9ffa12384 100644 --- a/java/Android.bp +++ b/java/Android.bp @@ -10,6 +10,7 @@ bootstrap_go_package { "blueprint-pathtools", "soong", "soong-android", + "soong-bazel", "soong-cc", "soong-dexpreopt", "soong-genrule", diff --git a/java/app.go b/java/app.go index c69210f39..35ed27f52 100755 --- a/java/app.go +++ b/java/app.go @@ -26,6 +26,7 @@ import ( "github.com/google/blueprint/proptools" "android/soong/android" + "android/soong/bazel" "android/soong/cc" "android/soong/dexpreopt" "android/soong/tradefed" @@ -42,6 +43,8 @@ func RegisterAppBuildComponents(ctx android.RegistrationContext) { ctx.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory) ctx.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory) ctx.RegisterModuleType("override_android_test", OverrideAndroidTestModuleFactory) + + android.RegisterBp2BuildMutator("android_app_certificate", AndroidAppCertificateBp2Build) } // AndroidManifest.xml merging @@ -1371,3 +1374,61 @@ func (u *usesLibrary) verifyUsesLibrariesAPK(ctx android.ModuleContext, apk andr outputFile := android.PathForModuleOut(ctx, "verify_uses_libraries", apk.Base()) return outputFile } + +// For Bazel / bp2build + +type bazelAndroidAppCertificateAttributes struct { + Certificate string +} + +type bazelAndroidAppCertificate struct { + android.BazelTargetModuleBase + bazelAndroidAppCertificateAttributes +} + +func BazelAndroidAppCertificateFactory() android.Module { + module := &bazelAndroidAppCertificate{} + module.AddProperties(&module.bazelAndroidAppCertificateAttributes) + android.InitBazelTargetModule(module) + return module +} + +func AndroidAppCertificateBp2Build(ctx android.TopDownMutatorContext) { + module, ok := ctx.Module().(*AndroidAppCertificate) + if !ok { + // Not an Android app certificate + return + } + if !module.ConvertWithBp2build(ctx) { + return + } + if ctx.ModuleType() != "android_app_certificate" { + return + } + + androidAppCertificateBp2BuildInternal(ctx, module) +} + +func androidAppCertificateBp2BuildInternal(ctx android.TopDownMutatorContext, module *AndroidAppCertificate) { + var certificate string + if module.properties.Certificate != nil { + certificate = *module.properties.Certificate + } + + attrs := &bazelAndroidAppCertificateAttributes{ + Certificate: certificate, + } + + props := bazel.BazelTargetModuleProperties{ + Rule_class: "android_app_certificate", + Bzl_load_location: "//build/bazel/rules:android_app_certificate.bzl", + } + + ctx.CreateBazelTargetModule(BazelAndroidAppCertificateFactory, module.Name(), props, attrs) +} + +func (m *bazelAndroidAppCertificate) Name() string { + return m.BaseModuleName() +} + +func (m *bazelAndroidAppCertificate) GenerateAndroidBuildActions(ctx android.ModuleContext) {}