platform_build_soong/java/generated_java_library.go
Jihoon Kang 3921f0b356 Collect aconfig_declarations of the dependent java_aconfig_library modules
droidstubs module require aconfig_declarations modules to be specified
when the module depends on the java_aconfig_library module in order to
generate the "exportable" stubs. This adds burden to the droidstubs or
java_sdk_library module owners, as module should specify both the
java_aconfig_library and aconfig_declarations modules to genreate the
"exportable" stubs, although the necessary information from the
aconfig_declarations module can be provided from the
java_aconfig_library modules.

In order to resolve such burden, this change enables the intermediate
cache files from the associated aconfig_declarations module of a
java_aconfig_library module to be propagated to its reverse dependencies
without having the specify the aconfig_declarations modules in the
droidstubs or java_sdk_library modules definitions.

This does not mean that the intermediate cache files of every transitive
dependencies of the java_sdk_library or the droidstubs will be passed to
aconfig to retrieve the state of the aconfig flags.

Specifically, only the java_aconfig_library modules or the java_library
modules that have static dependency on java_aconfig_library modules that
are passed to droidstubs via `libs` or `srcs` (using
":module_name{.tag}" syntax) will provide the intermediate cache files
to generate the "exportable" stubs. For java_sdk_library, all modules
listed as `libs`, `static_libs`, `stub_only_libs`, and `apiScope.libs`
are passed as `libs` and all modules listed as `api_srcs` and `srcs` are
passed as `srcs` to the droidstubs modules dynamically generated in
java_sdk_library module per api scope, thus these properties will be
affected.

Note that the test is being added in the apex package. This is because
trying to register the codegen package build components in the java
package leads to circular dependency between the codegen and the java
package, as codegen package imports the java package.

Test: m nothing --no-skip-soong-tests
Bug: 329284345
Change-Id: I7953ab64776f6947808321ce8a3598154501bcfe
2024-03-29 01:11:32 +00:00

116 lines
5 KiB
Go

// 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 java
import (
"android/soong/android"
)
type GeneratedJavaLibraryModule struct {
Library
callbacks GeneratedJavaLibraryCallbacks
moduleName string
// true if we've already called DepsMutator. Can't call AddLibrary or AddSharedLibrary
// after DepsMutator.
depsMutatorDone bool
}
type GeneratedJavaLibraryCallbacks interface {
// Called from inside DepsMutator, gives a chance to AddDependencies
DepsMutator(module *GeneratedJavaLibraryModule, ctx android.BottomUpMutatorContext)
// Called from inside GenerateAndroidBuildActions. Add the build rules to
// make the srcjar, and return the path to it.
GenerateSourceJarBuildActions(module *GeneratedJavaLibraryModule, ctx android.ModuleContext) (android.Path, android.Path)
}
// GeneratedJavaLibraryModuleFactory provides a utility for modules that are generated
// source code, including ones outside the java package to build jar files
// from that generated source.
//
// To use GeneratedJavaLibraryModule, call GeneratedJavaLibraryModuleFactory with
// a callback interface and a properties object to add to the module.
//
// These modules will have some properties blocked, and it will be an error if
// modules attempt to set them. See the list of property names in GeneratedAndroidBuildActions
// for the list of those properties.
func GeneratedJavaLibraryModuleFactory(moduleName string, callbacks GeneratedJavaLibraryCallbacks, properties interface{}) android.Module {
module := &GeneratedJavaLibraryModule{
callbacks: callbacks,
moduleName: moduleName,
}
module.addHostAndDeviceProperties()
module.initModuleAndImport(module)
android.InitApexModule(module)
InitJavaModule(module, android.HostAndDeviceSupported)
if properties != nil {
module.AddProperties(properties)
}
return module
}
// Add a java shared library as a dependency, as if they had said `libs: [ "name" ]`
func (module *GeneratedJavaLibraryModule) AddSharedLibrary(name string) {
if module.depsMutatorDone {
panic("GeneratedJavaLibraryModule.AddLibrary called after DepsMutator")
}
module.Library.properties.Libs = append(module.Library.properties.Libs, name)
}
// Add a java shared library as a dependency, as if they had said `libs: [ "name" ]`
func (module *GeneratedJavaLibraryModule) AddStaticLibrary(name string) {
if module.depsMutatorDone {
panic("GeneratedJavaLibraryModule.AddStaticLibrary called after DepsMutator")
}
module.Library.properties.Static_libs = append(module.Library.properties.Static_libs, name)
}
func (module *GeneratedJavaLibraryModule) DepsMutator(ctx android.BottomUpMutatorContext) {
module.callbacks.DepsMutator(module, ctx)
module.depsMutatorDone = true
module.Library.DepsMutator(ctx)
}
func checkPropertyEmpty(ctx android.ModuleContext, module *GeneratedJavaLibraryModule, name string, value []string) {
if len(value) != 0 {
ctx.PropertyErrorf(name, "%s not allowed on %s", name, module.moduleName)
}
}
func (module *GeneratedJavaLibraryModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
// These modules are all-generated, so disallow these properties to keep it simple.
// No additional sources
checkPropertyEmpty(ctx, module, "srcs", module.Library.properties.Srcs)
checkPropertyEmpty(ctx, module, "common_srcs", module.Library.properties.Common_srcs)
checkPropertyEmpty(ctx, module, "exclude_srcs", module.Library.properties.Exclude_srcs)
checkPropertyEmpty(ctx, module, "java_resource_dirs", module.Library.properties.Java_resource_dirs)
checkPropertyEmpty(ctx, module, "exclude_java_resource_dirs", module.Library.properties.Exclude_java_resource_dirs)
// Restrict these for no good reason other than to limit the surface area. If there's a
// good use case put them back.
checkPropertyEmpty(ctx, module, "plugins", module.Library.properties.Plugins)
checkPropertyEmpty(ctx, module, "exported_plugins", module.Library.properties.Exported_plugins)
srcJarPath, cacheOutputPath := module.callbacks.GenerateSourceJarBuildActions(module, ctx)
module.Library.properties.Generated_srcjars = append(module.Library.properties.Generated_srcjars, srcJarPath)
module.Library.properties.Aconfig_Cache_files = append(module.Library.properties.Aconfig_Cache_files, cacheOutputPath)
module.Library.GenerateAndroidBuildActions(ctx)
}
// Add a rule to the jarjar renaming rules. See RepackageProviderData.
func (module *GeneratedJavaLibraryModule) AddJarJarRenameRule(original string, renamed string) {
module.addJarJarRenameRule(original, renamed)
}