Switch java_sdk_library to use SetDefaultableHook()

Previously, java_sdk_library used AddLoadHook() to register a hook that
when called would create its child modules. That meant the
java_sdk_library properties that were used to create the child modules,
e.g. sdk_version could not be defaulted because the modules are created
before the defaults are applied.

This change switches java_sdk_library to use the new
SetDefaultableHook() mechanism to register the hook instead of the
AddLoadHook() mechanism.

It also prevents the child modules from being created if the module has
been disabled.

Bug: 155295806
Test: m checkapi
Change-Id: Ic6f90eb4449338e549878f64e8119e286b9aa549
This commit is contained in:
Paul Duffin
2020-04-29 16:47:28 +01:00
parent 955ade6475
commit f022920bde

View File

@@ -382,7 +382,7 @@ func (module *SdkLibrary) apiDistPath(apiScope *apiScope) string {
} }
// Get the sdk version for use when compiling the stubs library. // Get the sdk version for use when compiling the stubs library.
func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) string { func (module *SdkLibrary) sdkVersionForStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) string {
sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library)) sdkDep := decodeSdkDep(mctx, sdkContext(&module.Library))
if sdkDep.hasStandardLibs() { if sdkDep.hasStandardLibs() {
// If building against a standard sdk then use the sdk version appropriate for the scope. // If building against a standard sdk then use the sdk version appropriate for the scope.
@@ -402,7 +402,7 @@ func (module *SdkLibrary) latestRemovedApiFilegroupName(apiScope *apiScope) stri
} }
// Creates a static java library that has API stubs // Creates a static java library that has API stubs
func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiScope *apiScope) { func (module *SdkLibrary) createStubsLibrary(mctx android.DefaultableHookContext, apiScope *apiScope) {
props := struct { props := struct {
Name *string Name *string
Srcs []string Srcs []string
@@ -473,7 +473,7 @@ func (module *SdkLibrary) createStubsLibrary(mctx android.LoadHookContext, apiSc
// Creates a droidstubs module that creates stubs source files from the given full source // Creates a droidstubs module that creates stubs source files from the given full source
// files // files
func (module *SdkLibrary) createStubsSources(mctx android.LoadHookContext, apiScope *apiScope) { func (module *SdkLibrary) createStubsSources(mctx android.DefaultableHookContext, apiScope *apiScope) {
props := struct { props := struct {
Name *string Name *string
Srcs []string Srcs []string
@@ -592,7 +592,7 @@ func (module *SdkLibrary) DepIsInSameApex(mctx android.BaseModuleContext, dep an
} }
// Creates the xml file that publicizes the runtime library // Creates the xml file that publicizes the runtime library
func (module *SdkLibrary) createXmlFile(mctx android.LoadHookContext) { func (module *SdkLibrary) createXmlFile(mctx android.DefaultableHookContext) {
props := struct { props := struct {
Name *string Name *string
Lib_name *string Lib_name *string
@@ -713,7 +713,12 @@ func (module *SdkLibrary) getApiDir() string {
// For a java_sdk_library module, create internal modules for stubs, docs, // For a java_sdk_library module, create internal modules for stubs, docs,
// runtime libs and xml file. If requested, the stubs and docs are created twice // runtime libs and xml file. If requested, the stubs and docs are created twice
// once for public API level and once for system API level // once for public API level and once for system API level
func (module *SdkLibrary) CreateInternalModules(mctx android.LoadHookContext) { func (module *SdkLibrary) CreateInternalModules(mctx android.DefaultableHookContext) {
// If the module has been disabled then don't create any child modules.
if !module.Enabled() {
return
}
if len(module.Library.Module.properties.Srcs) == 0 { if len(module.Library.Module.properties.Srcs) == 0 {
mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs") mctx.PropertyErrorf("srcs", "java_sdk_library must specify srcs")
return return
@@ -799,7 +804,7 @@ func SdkLibraryFactory() android.Module {
module.InitSdkLibraryProperties() module.InitSdkLibraryProperties()
android.InitApexModule(module) android.InitApexModule(module)
InitJavaModule(module, android.HostAndDeviceSupported) InitJavaModule(module, android.HostAndDeviceSupported)
android.AddLoadHook(module, func(ctx android.LoadHookContext) { module.CreateInternalModules(ctx) }) module.SetDefaultableHook(func(ctx android.DefaultableHookContext) { module.CreateInternalModules(ctx) })
return module return module
} }