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

Commit c4518304 authored by Zi Wang's avatar Zi Wang Committed by Gerrit Code Review
Browse files

Merge "Add limited bp2build converter of java_sdk_library"

parents 7ec899ac b2179e39
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -686,12 +686,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
Original line number Diff line number Diff line
// 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
Original line number Diff line number Diff line
@@ -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
//