From b2179e397aa6c8d8c3ad12e8a9d19621d627b670 Mon Sep 17 00:00:00 2001 From: Zi Wang Date: Tue, 31 Jan 2023 15:53:30 -0800 Subject: [PATCH] Add limited bp2build converter of java_sdk_library Only public, system, test, module_lib and system_server are converted in order to generate api_fingerprint.txt in Bazel. Test: java_sdk_library_conversion_test.go and TH Bug: 266973526 Change-Id: I67a00806165e5afad3876b6cd5cdbc6b0dd65d8b --- android/allowlists/allowlists.go | 3 +- bp2build/java_sdk_library_conversion_test.go | 148 +++++++++++++++++++ java/sdk_library.go | 50 ++++++- 3 files changed, 196 insertions(+), 5 deletions(-) create mode 100644 bp2build/java_sdk_library_conversion_test.go diff --git a/android/allowlists/allowlists.go b/android/allowlists/allowlists.go index 4fc6ae368..988aed961 100644 --- a/android/allowlists/allowlists.go +++ b/android/allowlists/allowlists.go @@ -683,12 +683,13 @@ var ( Bp2buildModuleTypeAlwaysConvertList = []string{ "aidl_interface_headers", + "bpf", "license", "linker_config", "java_import", "java_import_host", + "java_sdk_library", "sysprop_library", - "bpf", } // Add the names of modules that bp2build should never convert, if it is diff --git a/bp2build/java_sdk_library_conversion_test.go b/bp2build/java_sdk_library_conversion_test.go new file mode 100644 index 000000000..9ce7446c2 --- /dev/null +++ b/bp2build/java_sdk_library_conversion_test.go @@ -0,0 +1,148 @@ +// 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/android" + "android/soong/java" +) + +func runJavaSdkLibraryTestCaseWithRegistrationCtxFunc(t *testing.T, tc Bp2buildTestCase, registrationCtxFunc func(ctx android.RegistrationContext)) { + t.Helper() + (&tc).ModuleTypeUnderTest = "java_sdk_library" + (&tc).ModuleTypeUnderTestFactory = java.SdkLibraryFactory + RunBp2BuildTestCase(t, registrationCtxFunc, tc) +} + +func runJavaSdkLibraryTestCase(t *testing.T, tc Bp2buildTestCase) { + t.Helper() + runJavaSdkLibraryTestCaseWithRegistrationCtxFunc(t, tc, func(ctx android.RegistrationContext) {}) +} + +func TestJavaSdkLibraryApiSurfaceGeneral(t *testing.T) { + runJavaSdkLibraryTestCase(t, Bp2buildTestCase{ + Description: "limited java_sdk_library for api surfaces, general conversion", + Filesystem: map[string]string{ + "build/soong/scripts/gen-java-current-api-files.sh": "", + "api/current.txt": "", + "api/system-current.txt": "", + "api/test-current.txt": "", + "api/module-lib-current.txt": "", + "api/system-server-current.txt": "", + "api/removed.txt": "", + "api/system-removed.txt": "", + "api/test-removed.txt": "", + "api/module-lib-removed.txt": "", + "api/system-server-removed.txt": "", + }, + Blueprint: `java_sdk_library { + name: "java-sdk-lib", + srcs: ["a.java"], + public: {enabled: true}, + system: {enabled: true}, + test: {enabled: true}, + module_lib: {enabled: true}, + system_server: {enabled: true}, +}`, + ExpectedBazelTargets: []string{ + MakeBazelTarget("java_sdk_library", "java-sdk-lib", AttrNameToString{ + "public": `"api/current.txt"`, + "system": `"api/system-current.txt"`, + "test": `"api/test-current.txt"`, + "module_lib": `"api/module-lib-current.txt"`, + "system_server": `"api/system-server-current.txt"`, + }), + }, + }) +} + +func TestJavaSdkLibraryApiSurfacePublicDefault(t *testing.T) { + runJavaSdkLibraryTestCase(t, Bp2buildTestCase{ + Description: "limited java_sdk_library for api surfaces, public prop uses default value", + Filesystem: map[string]string{ + "build/soong/scripts/gen-java-current-api-files.sh": "", + "api/current.txt": "", + "api/system-current.txt": "", + "api/test-current.txt": "", + "api/module-lib-current.txt": "", + "api/system-server-current.txt": "", + "api/removed.txt": "", + "api/system-removed.txt": "", + "api/test-removed.txt": "", + "api/module-lib-removed.txt": "", + "api/system-server-removed.txt": "", + }, + Blueprint: `java_sdk_library { + name: "java-sdk-lib", + srcs: ["a.java"], + system: {enabled: false}, + test: {enabled: false}, + module_lib: {enabled: false}, + system_server: {enabled: false}, +}`, + ExpectedBazelTargets: []string{ + MakeBazelTarget("java_sdk_library", "java-sdk-lib", AttrNameToString{ + "public": `"api/current.txt"`, + }), + }, + }) +} + +func TestJavaSdkLibraryApiSurfacePublicNotEnabled(t *testing.T) { + runJavaSdkLibraryTestCase(t, Bp2buildTestCase{ + Description: "limited java_sdk_library for api surfaces, public enable is false", + Filesystem: map[string]string{ + "build/soong/scripts/gen-java-current-api-files.sh": "", + "api/current.txt": "", + "api/removed.txt": "", + }, + Blueprint: `java_sdk_library { + name: "java-sdk-lib", + srcs: ["a.java"], + public: {enabled: false}, +}`, + ExpectedBazelTargets: []string{ + MakeBazelTarget("java_sdk_library", "java-sdk-lib", AttrNameToString{}), + }, + }) +} + +func TestJavaSdkLibraryApiSurfaceNoScopeIsSet(t *testing.T) { + runJavaSdkLibraryTestCase(t, Bp2buildTestCase{ + Description: "limited java_sdk_library for api surfaces, none of the api scopes is set", + Filesystem: map[string]string{ + "build/soong/scripts/gen-java-current-api-files.sh": "", + "api/current.txt": "", + "api/system-current.txt": "", + "api/test-current.txt": "", + "api/removed.txt": "", + "api/system-removed.txt": "", + "api/test-removed.txt": "", + }, + Blueprint: `java_sdk_library { + name: "java-sdk-lib", + srcs: ["a.java"], +}`, + ExpectedBazelTargets: []string{ + MakeBazelTarget("java_sdk_library", "java-sdk-lib", AttrNameToString{ + "public": `"api/current.txt"`, + "system": `"api/system-current.txt"`, + "test": `"api/test-current.txt"`, + }), + }, + }) +} diff --git a/java/sdk_library.go b/java/sdk_library.go index a2295f4a6..d50628080 100644 --- a/java/sdk_library.go +++ b/java/sdk_library.go @@ -28,6 +28,7 @@ import ( "github.com/google/blueprint/proptools" "android/soong/android" + "android/soong/bazel" "android/soong/dexpreopt" ) @@ -546,14 +547,14 @@ type sdkLibraryProperties struct { // The properties specific to the module-lib api scope // - // Unless explicitly specified by using test.enabled the module-lib api scope is - // disabled by default. + // Unless explicitly specified by using module_lib.enabled the module_lib api + // scope is disabled by default. Module_lib ApiScopeProperties // The properties specific to the system-server api scope // - // Unless explicitly specified by using test.enabled the module-lib api scope is - // disabled by default. + // Unless explicitly specified by using system_server.enabled the + // system_server api scope is disabled by default. System_server ApiScopeProperties // Determines if the stubs are preferred over the implementation library @@ -1163,6 +1164,8 @@ type SdkLibraryDependency interface { type SdkLibrary struct { Library + android.BazelModuleBase + sdkLibraryProperties sdkLibraryProperties // Map from api scope to the scope specific property structure. @@ -2081,9 +2084,48 @@ func SdkLibraryFactory() android.Module { module.CreateInternalModules(ctx) } }) + android.InitBazelModule(module) return module } +type bazelSdkLibraryAttributes struct { + Public bazel.StringAttribute + System bazel.StringAttribute + Test bazel.StringAttribute + Module_lib bazel.StringAttribute + System_server bazel.StringAttribute +} + +// java_sdk_library bp2build converter +func (module *SdkLibrary) ConvertWithBp2build(ctx android.TopDownMutatorContext) { + if ctx.ModuleType() != "java_sdk_library" { + return + } + + nameToAttr := make(map[string]bazel.StringAttribute) + + for _, scope := range module.getGeneratedApiScopes(ctx) { + apiSurfaceFile := path.Join(module.getApiDir(), scope.apiFilePrefix+"current.txt") + var scopeStringAttribute bazel.StringAttribute + scopeStringAttribute.SetValue(apiSurfaceFile) + nameToAttr[scope.name] = scopeStringAttribute + } + + attrs := bazelSdkLibraryAttributes{ + Public: nameToAttr["public"], + System: nameToAttr["system"], + Test: nameToAttr["test"], + Module_lib: nameToAttr["module-lib"], + System_server: nameToAttr["system-server"], + } + props := bazel.BazelTargetModuleProperties{ + Rule_class: "java_sdk_library", + Bzl_load_location: "//build/bazel/rules/java:sdk_library.bzl", + } + + ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, &attrs) +} + // // SDK library prebuilts //