platform_build_soong/java/bootclasspath.go
Paul Duffin 7487a7abb6 Workaround to make AlwaysUsePrebuiltSdks() work with platform_bootclasspath
The AlwaysUsePrebuiltSdks() causes all java_sdk_library_import modules
to be preferred over the source, i.e. as if they had prefer: true set.
That interacts badly with the work that is being done to integrate the
bootclasspath_fragment/platform_bootclasspath modules into the build.

It would work fine once that integration has been completed but in the
interim it causes problems. e.g. it does not cause a problem in AOSP
because those java_sdk_library_import modules that are affected have
already been integrated into the build properly.

Unfortunately, internally that is not the case because there are
java_sdk_library/java_sdk_library_import modules that still need to
be updated.

Before the java_sdk_library_import can be safely preferred each
java_sdk_library/java_sdk_library_import module that contributes to the
bootclasspath must:
* Be in the contents of matching bootclasspath_fragment and
  prebuilt_bootclasspath_fragment modules.
* Have an apex and one of a prebuilt_apex/apex_set that contains the
  dex implementation jar and lists the prebuilt_bootclasspath_fragment
  name in its exported_bootclasspath_fragments property.

Safely preferred in this context means that the whole build will
continue to work rather than the current situation which is that only
some of the build will work and some will fail if an attempt is
actually made to build it.

Unfortunately, many java_sdk_library_import modules are missing:
* The prebuilt_bootclasspath_fragment.
* The exported_bootclasspath_fragments property on the
  prebuilt_apex/apex_set that contains them.

Together these cause the following symptoms:
1. The java_sdk_library_import does not have a dex implementation jar.
2. The java_sdk_library_import does not have a myapex variant.

These workarounds will avoid Soong reporting build failures. However,
the build will still fail if an attempt is made to build anything
produced by the platform-bootclasspath, e.g. hidden API processing or
a system image.

Bug: 188505921
Bug: 179354495
Test: m TARGET_BUILD_APPS=Calendar
Change-Id: I3226e21cd6a7f9e4d6bbe94e54129ac5e1d4c679
Merged-In: I3226e21cd6a7f9e4d6bbe94e54129ac5e1d4c679
2021-05-19 11:42:20 +00:00

246 lines
10 KiB
Go

// Copyright 2021 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"
"github.com/google/blueprint"
"github.com/google/blueprint/proptools"
)
// Contains code that is common to both platform_bootclasspath and bootclasspath_fragment.
func init() {
registerBootclasspathBuildComponents(android.InitRegistrationContext)
}
func registerBootclasspathBuildComponents(ctx android.RegistrationContext) {
ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) {
ctx.BottomUp("bootclasspath_deps", bootclasspathDepsMutator).Parallel()
})
}
// BootclasspathDepsMutator is the interface that a module must implement if it wants to add
// dependencies onto APEX specific variants of bootclasspath fragments or bootclasspath contents.
type BootclasspathDepsMutator interface {
// BootclasspathDepsMutator implementations should add dependencies using
// addDependencyOntoApexModulePair and addDependencyOntoApexVariants.
BootclasspathDepsMutator(ctx android.BottomUpMutatorContext)
}
// bootclasspathDepsMutator is called during the final deps phase after all APEX variants have
// been created so can add dependencies onto specific APEX variants of modules.
func bootclasspathDepsMutator(ctx android.BottomUpMutatorContext) {
m := ctx.Module()
if p, ok := m.(BootclasspathDepsMutator); ok {
p.BootclasspathDepsMutator(ctx)
}
}
// addDependencyOntoApexVariants adds dependencies onto the appropriate apex specific variants of
// the module as specified in the ApexVariantReference list.
func addDependencyOntoApexVariants(ctx android.BottomUpMutatorContext, propertyName string, refs []ApexVariantReference, tag blueprint.DependencyTag) {
for i, ref := range refs {
apex := proptools.StringDefault(ref.Apex, "platform")
if ref.Module == nil {
ctx.PropertyErrorf(propertyName, "missing module name at position %d", i)
continue
}
name := proptools.String(ref.Module)
addDependencyOntoApexModulePair(ctx, apex, name, tag)
}
}
// addDependencyOntoApexModulePair adds a dependency onto the specified APEX specific variant or the
// specified module.
//
// If apex="platform" or "system_ext" then this adds a dependency onto the platform variant of the
// module. This adds dependencies onto the prebuilt and source modules with the specified name,
// depending on which ones are available. Visiting must use isActiveModule to select the preferred
// module when both source and prebuilt modules are available.
//
// Use gatherApexModulePairDepsWithTag to retrieve the dependencies.
func addDependencyOntoApexModulePair(ctx android.BottomUpMutatorContext, apex string, name string, tag blueprint.DependencyTag) {
var variations []blueprint.Variation
if apex != "platform" && apex != "system_ext" {
// Pick the correct apex variant.
variations = []blueprint.Variation{
{Mutator: "apex", Variation: apex},
}
}
addedDep := false
if ctx.OtherModuleDependencyVariantExists(variations, name) {
ctx.AddFarVariationDependencies(variations, tag, name)
addedDep = true
}
// Add a dependency on the prebuilt module if it exists.
prebuiltName := android.PrebuiltNameFromSource(name)
if ctx.OtherModuleDependencyVariantExists(variations, prebuiltName) {
ctx.AddVariationDependencies(variations, tag, prebuiltName)
addedDep = true
} else if ctx.Config().AlwaysUsePrebuiltSdks() && len(variations) > 0 {
// TODO(b/179354495): Remove this code path once the Android build has been fully migrated to
// use bootclasspath_fragment properly.
// Some prebuilt java_sdk_library modules do not yet have an APEX variations so try and add a
// dependency on the non-APEX variant.
if ctx.OtherModuleDependencyVariantExists(nil, prebuiltName) {
ctx.AddVariationDependencies(nil, tag, prebuiltName)
addedDep = true
}
}
// If no appropriate variant existing for this, so no dependency could be added, then it is an
// error, unless missing dependencies are allowed. The simplest way to handle that is to add a
// dependency that will not be satisfied and the default behavior will handle it.
if !addedDep {
// Add dependency on the unprefixed (i.e. source or renamed prebuilt) module which we know does
// not exist. The resulting error message will contain useful information about the available
// variants.
reportMissingVariationDependency(ctx, variations, name)
// Add dependency on the missing prefixed prebuilt variant too if a module with that name exists
// so that information about its available variants will be reported too.
if ctx.OtherModuleExists(prebuiltName) {
reportMissingVariationDependency(ctx, variations, prebuiltName)
}
}
}
// reportMissingVariationDependency intentionally adds a dependency on a missing variation in order
// to generate an appropriate error message with information about the available variations.
func reportMissingVariationDependency(ctx android.BottomUpMutatorContext, variations []blueprint.Variation, name string) {
ctx.AddFarVariationDependencies(variations, nil, name)
}
// gatherApexModulePairDepsWithTag returns the list of dependencies with the supplied tag that was
// added by addDependencyOntoApexModulePair.
func gatherApexModulePairDepsWithTag(ctx android.BaseModuleContext, tag blueprint.DependencyTag) []android.Module {
var modules []android.Module
ctx.VisitDirectDepsIf(isActiveModule, func(module android.Module) {
t := ctx.OtherModuleDependencyTag(module)
if t == tag {
modules = append(modules, module)
}
})
return modules
}
// ApexVariantReference specifies a particular apex variant of a module.
type ApexVariantReference struct {
// The name of the module apex variant, i.e. the apex containing the module variant.
//
// If this is not specified then it defaults to "platform" which will cause a dependency to be
// added to the module's platform variant.
//
// A value of system_ext should be used for any module that will be part of the system_ext
// partition.
Apex *string
// The name of the module.
Module *string
}
// BootclasspathFragmentsDepsProperties contains properties related to dependencies onto fragments.
type BootclasspathFragmentsDepsProperties struct {
// The names of the bootclasspath_fragment modules that form part of this module.
Fragments []ApexVariantReference
}
// addDependenciesOntoFragments adds dependencies to the fragments specified in this properties
// structure.
func (p *BootclasspathFragmentsDepsProperties) addDependenciesOntoFragments(ctx android.BottomUpMutatorContext) {
addDependencyOntoApexVariants(ctx, "fragments", p.Fragments, bootclasspathFragmentDepTag)
}
// bootclasspathDependencyTag defines dependencies from/to bootclasspath_fragment,
// prebuilt_bootclasspath_fragment and platform_bootclasspath onto either source or prebuilt
// modules.
type bootclasspathDependencyTag struct {
blueprint.BaseDependencyTag
name string
}
func (t bootclasspathDependencyTag) ExcludeFromVisibilityEnforcement() {
}
// Dependencies that use the bootclasspathDependencyTag instances are only added after all the
// visibility checking has been done so this has no functional effect. However, it does make it
// clear that visibility is not being enforced on these tags.
var _ android.ExcludeFromVisibilityEnforcementTag = bootclasspathDependencyTag{}
// The tag used for dependencies onto bootclasspath_fragments.
var bootclasspathFragmentDepTag = bootclasspathDependencyTag{name: "fragment"}
// BootclasspathNestedAPIProperties defines properties related to the API provided by parts of the
// bootclasspath that are nested within the main BootclasspathAPIProperties.
type BootclasspathNestedAPIProperties struct {
// java_library or preferably, java_sdk_library modules providing stub classes that define the
// APIs provided by this bootclasspath_fragment.
Stub_libs []string
}
// BootclasspathAPIProperties defines properties for defining the API provided by parts of the
// bootclasspath.
type BootclasspathAPIProperties struct {
// Api properties provide information about the APIs provided by the bootclasspath_fragment.
// Properties in this section apply to public, system and test api scopes. They DO NOT apply to
// core_platform as that is a special, ART specific scope, that does not follow the pattern and so
// has its own section. It is in the process of being deprecated and replaced by the system scope
// but this will remain for the foreseeable future to maintain backwards compatibility.
//
// Every bootclasspath_fragment must specify at least one stubs_lib in this section and must
// specify stubs for all the APIs provided by its contents. Failure to do so will lead to those
// methods being inaccessible to other parts of Android, including but not limited to
// applications.
Api BootclasspathNestedAPIProperties
// Properties related to the core platform API surface.
//
// This must only be used by the following modules:
// * ART
// * Conscrypt
// * I18N
//
// The bootclasspath_fragments for each of the above modules must specify at least one stubs_lib
// and must specify stubs for all the APIs provided by its contents. Failure to do so will lead to
// those methods being inaccessible to the other modules in the list.
Core_platform_api BootclasspathNestedAPIProperties
}
// sdkKindToStubLibs calculates the stub library modules for each relevant android.SdkKind from the
// Stub_libs properties.
func (p BootclasspathAPIProperties) sdkKindToStubLibs() map[android.SdkKind][]string {
m := map[android.SdkKind][]string{}
for _, kind := range []android.SdkKind{android.SdkPublic, android.SdkSystem, android.SdkTest} {
m[kind] = p.Api.Stub_libs
}
m[android.SdkCorePlatform] = p.Core_platform_api.Stub_libs
return m
}
// bootclasspathApiInfo contains paths resolved from BootclasspathAPIProperties
type bootclasspathApiInfo struct {
// stubJarsByKind maps from the android.SdkKind to the paths containing dex stub jars for each
// kind.
stubJarsByKind map[android.SdkKind]android.Paths
}
var bootclasspathApiInfoProvider = blueprint.NewProvider(bootclasspathApiInfo{})