Bladeren bron

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
Zi Wang 1 jaar geleden
bovenliggende
commit
b2179e397a
3 gewijzigde bestanden met toevoegingen van 196 en 5 verwijderingen
  1. 2 1
      android/allowlists/allowlists.go
  2. 148 0
      bp2build/java_sdk_library_conversion_test.go
  3. 46 4
      java/sdk_library.go

+ 2 - 1
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

+ 148 - 0
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"`,
+			}),
+		},
+	})
+}

+ 46 - 4
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
 //