platform_build_soong/java/platform_bootclasspath.go
Paul Duffin 2fef136885 Generate hidden API flags for a bootclasspath_fragment
This change adds support for generating the hidden API flags for the
contents of a bootclasspath_fragment. Currently, it will only work for
the art-bootclasspath-fragment as it has no support for creating
dependencies between bootclasspath_fragment modules which will be
needed for handling any other bootclasspath_fragment.

The hidden API flag generation added by this change is completely
separate to the normal hidden API processing and is not as yet encoded
in dex jars so will have no effect on the runtime.

The generated files are provided for use by other modules and copied
into the sdk snapshot. That is needed to allow the build to verify that
the hidden API flags generated by the individual bootclasspath_fragment
modules are consistent with the flags generated for the whole
bootclasspath, whether building from source or prebuilts.

Bug: 179354495
Test: m art-module-sdk
      m out/soong/.intermediates/art/build/boot/art-bootclasspath-fragment/android_common_apex10000/modular-hiddenapi/all-flags.csv
      m out/soong/hiddenapi/hiddenapi-flags.csv
      - test that the former file is a subset of the latter and that
        where they overlap they are identical.
Change-Id: Ie27303e2960953db1b7abe95510e3bca4411b09a
2021-05-14 01:48:51 +01:00

466 lines
18 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 (
"fmt"
"android/soong/android"
"android/soong/dexpreopt"
)
func init() {
registerPlatformBootclasspathBuildComponents(android.InitRegistrationContext)
}
func registerPlatformBootclasspathBuildComponents(ctx android.RegistrationContext) {
ctx.RegisterSingletonModuleType("platform_bootclasspath", platformBootclasspathFactory)
}
// The tags used for the dependencies between the platform bootclasspath and any configured boot
// jars.
var (
platformBootclasspathArtBootJarDepTag = bootclasspathDependencyTag{name: "art-boot-jar"}
platformBootclasspathNonUpdatableBootJarDepTag = bootclasspathDependencyTag{name: "non-updatable-boot-jar"}
platformBootclasspathUpdatableBootJarDepTag = bootclasspathDependencyTag{name: "updatable-boot-jar"}
)
type platformBootclasspathModule struct {
android.SingletonModuleBase
ClasspathFragmentBase
properties platformBootclasspathProperties
// The apex:module pairs obtained from the configured modules.
//
// Currently only for testing.
configuredModules []android.Module
// The apex:module pairs obtained from the fragments.
//
// Currently only for testing.
fragments []android.Module
// Path to the monolithic hiddenapi-flags.csv file.
hiddenAPIFlagsCSV android.OutputPath
// Path to the monolithic hiddenapi-index.csv file.
hiddenAPIIndexCSV android.OutputPath
// Path to the monolithic hiddenapi-unsupported.csv file.
hiddenAPIMetadataCSV android.OutputPath
}
type platformBootclasspathProperties struct {
BootclasspathFragmentsDepsProperties
Hidden_api HiddenAPIFlagFileProperties
}
func platformBootclasspathFactory() android.SingletonModule {
m := &platformBootclasspathModule{}
m.AddProperties(&m.properties)
// TODO(satayev): split apex jars into separate configs.
initClasspathFragment(m, BOOTCLASSPATH)
android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon)
return m
}
var _ android.OutputFileProducer = (*platformBootclasspathModule)(nil)
func (b *platformBootclasspathModule) AndroidMkEntries() (entries []android.AndroidMkEntries) {
entries = append(entries, android.AndroidMkEntries{
Class: "FAKE",
// Need at least one output file in order for this to take effect.
OutputFile: android.OptionalPathForPath(b.hiddenAPIFlagsCSV),
Include: "$(BUILD_PHONY_PACKAGE)",
})
entries = append(entries, b.classpathFragmentBase().androidMkEntries()...)
return
}
// Make the hidden API files available from the platform-bootclasspath module.
func (b *platformBootclasspathModule) OutputFiles(tag string) (android.Paths, error) {
switch tag {
case "hiddenapi-flags.csv":
return android.Paths{b.hiddenAPIFlagsCSV}, nil
case "hiddenapi-index.csv":
return android.Paths{b.hiddenAPIIndexCSV}, nil
case "hiddenapi-metadata.csv":
return android.Paths{b.hiddenAPIMetadataCSV}, nil
}
return nil, fmt.Errorf("unknown tag %s", tag)
}
func (b *platformBootclasspathModule) DepsMutator(ctx android.BottomUpMutatorContext) {
b.hiddenAPIDepsMutator(ctx)
if SkipDexpreoptBootJars(ctx) {
return
}
// Add a dependency onto the dex2oat tool which is needed for creating the boot image. The
// path is retrieved from the dependency by GetGlobalSoongConfig(ctx).
dexpreopt.RegisterToolDeps(ctx)
}
func (b *platformBootclasspathModule) hiddenAPIDepsMutator(ctx android.BottomUpMutatorContext) {
if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
return
}
// Add dependencies onto the stub lib modules.
sdkKindToStubLibModules := hiddenAPIComputeMonolithicStubLibModules(ctx.Config())
hiddenAPIAddStubLibDependencies(ctx, sdkKindToStubLibModules)
}
func (b *platformBootclasspathModule) BootclasspathDepsMutator(ctx android.BottomUpMutatorContext) {
// Add dependencies on all the modules configured in the "art" boot image.
artImageConfig := genBootImageConfigs(ctx)[artBootImageName]
addDependenciesOntoBootImageModules(ctx, artImageConfig.modules, platformBootclasspathArtBootJarDepTag)
// Add dependencies on all the non-updatable module configured in the "boot" boot image. That does
// not include modules configured in the "art" boot image.
bootImageConfig := b.getImageConfig(ctx)
addDependenciesOntoBootImageModules(ctx, bootImageConfig.modules, platformBootclasspathNonUpdatableBootJarDepTag)
// Add dependencies on all the updatable modules.
updatableModules := dexpreopt.GetGlobalConfig(ctx).UpdatableBootJars
addDependenciesOntoBootImageModules(ctx, updatableModules, platformBootclasspathUpdatableBootJarDepTag)
// Add dependencies on all the fragments.
b.properties.BootclasspathFragmentsDepsProperties.addDependenciesOntoFragments(ctx)
}
func addDependenciesOntoBootImageModules(ctx android.BottomUpMutatorContext, modules android.ConfiguredJarList, tag bootclasspathDependencyTag) {
for i := 0; i < modules.Len(); i++ {
apex := modules.Apex(i)
name := modules.Jar(i)
addDependencyOntoApexModulePair(ctx, apex, name, tag)
}
}
// GenerateSingletonBuildActions does nothing and must never do anything.
//
// This module only implements android.SingletonModule so that it can implement
// android.SingletonMakeVarsProvider.
func (b *platformBootclasspathModule) GenerateSingletonBuildActions(android.SingletonContext) {
// Keep empty
}
func (d *platformBootclasspathModule) MakeVars(ctx android.MakeVarsContext) {
d.generateHiddenApiMakeVars(ctx)
}
func (b *platformBootclasspathModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
// Gather all the dependencies from the art, updatable and non-updatable boot jars.
artModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathArtBootJarDepTag)
nonUpdatableModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathNonUpdatableBootJarDepTag)
updatableModules := gatherApexModulePairDepsWithTag(ctx, platformBootclasspathUpdatableBootJarDepTag)
// Concatenate them all, in order as they would appear on the bootclasspath.
var allModules []android.Module
allModules = append(allModules, artModules...)
allModules = append(allModules, nonUpdatableModules...)
allModules = append(allModules, updatableModules...)
b.configuredModules = allModules
// Gather all the fragments dependencies.
b.fragments = gatherApexModulePairDepsWithTag(ctx, bootclasspathFragmentDepTag)
// Check the configuration of the boot modules.
// ART modules are checked by the art-bootclasspath-fragment.
b.checkNonUpdatableModules(ctx, nonUpdatableModules)
b.checkUpdatableModules(ctx, updatableModules)
b.generateClasspathProtoBuildActions(ctx)
b.generateHiddenAPIBuildActions(ctx, b.configuredModules, b.fragments)
// Nothing to do if skipping the dexpreopt of boot image jars.
if SkipDexpreoptBootJars(ctx) {
return
}
b.generateBootImageBuildActions(ctx, nonUpdatableModules, updatableModules)
}
// Generate classpaths.proto config
func (b *platformBootclasspathModule) generateClasspathProtoBuildActions(ctx android.ModuleContext) {
// ART and platform boot jars must have a corresponding entry in DEX2OATBOOTCLASSPATH
classpathJars := configuredJarListToClasspathJars(ctx, b.ClasspathFragmentToConfiguredJarList(ctx), BOOTCLASSPATH, DEX2OATBOOTCLASSPATH)
// TODO(satayev): remove updatable boot jars once each apex has its own fragment
global := dexpreopt.GetGlobalConfig(ctx)
classpathJars = append(classpathJars, configuredJarListToClasspathJars(ctx, global.UpdatableBootJars, BOOTCLASSPATH)...)
b.classpathFragmentBase().generateClasspathProtoBuildActions(ctx, classpathJars)
}
func (b *platformBootclasspathModule) ClasspathFragmentToConfiguredJarList(ctx android.ModuleContext) android.ConfiguredJarList {
global := dexpreopt.GetGlobalConfig(ctx)
// TODO(satayev): split ART apex jars into their own classpathFragment
return global.BootJars
}
// checkNonUpdatableModules ensures that the non-updatable modules supplied are not part of an
// updatable module.
func (b *platformBootclasspathModule) checkNonUpdatableModules(ctx android.ModuleContext, modules []android.Module) {
for _, m := range modules {
apexInfo := ctx.OtherModuleProvider(m, android.ApexInfoProvider).(android.ApexInfo)
fromUpdatableApex := apexInfo.Updatable
if fromUpdatableApex {
// error: this jar is part of an updatable apex
ctx.ModuleErrorf("module %q from updatable apexes %q is not allowed in the framework boot image", ctx.OtherModuleName(m), apexInfo.InApexes)
} else {
// ok: this jar is part of the platform or a non-updatable apex
}
}
}
// checkUpdatableModules ensures that the updatable modules supplied are not from the platform.
func (b *platformBootclasspathModule) checkUpdatableModules(ctx android.ModuleContext, modules []android.Module) {
for _, m := range modules {
apexInfo := ctx.OtherModuleProvider(m, android.ApexInfoProvider).(android.ApexInfo)
fromUpdatableApex := apexInfo.Updatable
if fromUpdatableApex {
// ok: this jar is part of an updatable apex
} else {
name := ctx.OtherModuleName(m)
if apexInfo.IsForPlatform() {
// error: this jar is part of the platform
ctx.ModuleErrorf("module %q from platform is not allowed in the updatable boot jars list", name)
} else {
// TODO(b/177892522): Treat this as an error.
// Cannot do that at the moment because framework-wifi and framework-tethering are in the
// PRODUCT_UPDATABLE_BOOT_JARS but not marked as updatable in AOSP.
}
}
}
}
func (b *platformBootclasspathModule) getImageConfig(ctx android.EarlyModuleContext) *bootImageConfig {
return defaultBootImageConfig(ctx)
}
// hiddenAPISupportingModule encapsulates the information provided by any module that contributes to
// the hidden API processing.
type hiddenAPISupportingModule struct {
module android.Module
bootDexJar android.Path
flagsCSV android.Path
indexCSV android.Path
metadataCSV android.Path
}
// generateHiddenAPIBuildActions generates all the hidden API related build rules.
func (b *platformBootclasspathModule) generateHiddenAPIBuildActions(ctx android.ModuleContext, modules []android.Module, fragments []android.Module) {
// Save the paths to the monolithic files for retrieval via OutputFiles().
b.hiddenAPIFlagsCSV = hiddenAPISingletonPaths(ctx).flags
b.hiddenAPIIndexCSV = hiddenAPISingletonPaths(ctx).index
b.hiddenAPIMetadataCSV = hiddenAPISingletonPaths(ctx).metadata
// Don't run any hiddenapi rules if UNSAFE_DISABLE_HIDDENAPI_FLAGS=true. This is a performance
// optimization that can be used to reduce the incremental build time but as its name suggests it
// can be unsafe to use, e.g. when the changes affect anything that goes on the bootclasspath.
if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
paths := android.OutputPaths{b.hiddenAPIFlagsCSV, b.hiddenAPIIndexCSV, b.hiddenAPIMetadataCSV}
for _, path := range paths {
ctx.Build(pctx, android.BuildParams{
Rule: android.Touch,
Output: path,
})
}
return
}
// nilPathHandler will check the supplied path and if it is nil then it will either immediately
// report an error, or it will defer the error reporting until it is actually used, depending
// whether missing dependencies are allowed.
var nilPathHandler func(path android.Path, name string, module android.Module) android.Path
if ctx.Config().AllowMissingDependencies() {
nilPathHandler = func(path android.Path, name string, module android.Module) android.Path {
if path == nil {
outputPath := android.PathForModuleOut(ctx, "missing", module.Name(), name)
path = outputPath
// Create an error rule that pretends to create the output file but will actually fail if it
// is run.
ctx.Build(pctx, android.BuildParams{
Rule: android.ErrorRule,
Output: outputPath,
Args: map[string]string{
"error": fmt.Sprintf("missing hidden API file: %s for %s", name, module),
},
})
}
return path
}
} else {
nilPathHandler = func(path android.Path, name string, module android.Module) android.Path {
if path == nil {
ctx.ModuleErrorf("module %s does not provide a %s file", module, name)
}
return path
}
}
hiddenAPISupportingModules := []hiddenAPISupportingModule{}
for _, module := range modules {
if h, ok := module.(hiddenAPIIntf); ok {
hiddenAPISupportingModule := hiddenAPISupportingModule{
module: module,
bootDexJar: nilPathHandler(h.bootDexJar(), "bootDexJar", module),
flagsCSV: nilPathHandler(h.flagsCSV(), "flagsCSV", module),
indexCSV: nilPathHandler(h.indexCSV(), "indexCSV", module),
metadataCSV: nilPathHandler(h.metadataCSV(), "metadataCSV", module),
}
// If any errors were reported when trying to populate the hiddenAPISupportingModule struct
// then don't add it to the list.
if ctx.Failed() {
continue
}
hiddenAPISupportingModules = append(hiddenAPISupportingModules, hiddenAPISupportingModule)
} else if _, ok := module.(*DexImport); ok {
// Ignore this for the purposes of hidden API processing
} else {
ctx.ModuleErrorf("module %s of type %s does not support hidden API processing", module, ctx.OtherModuleType(module))
}
}
moduleSpecificFlagsPaths := android.Paths{}
for _, module := range hiddenAPISupportingModules {
moduleSpecificFlagsPaths = append(moduleSpecificFlagsPaths, module.flagsCSV)
}
flagFileInfo := b.properties.Hidden_api.hiddenAPIFlagFileInfo(ctx)
for _, fragment := range fragments {
if ctx.OtherModuleHasProvider(fragment, hiddenAPIFlagFileInfoProvider) {
info := ctx.OtherModuleProvider(fragment, hiddenAPIFlagFileInfoProvider).(hiddenAPIFlagFileInfo)
flagFileInfo.append(info)
}
}
// Store the information for testing.
ctx.SetProvider(hiddenAPIFlagFileInfoProvider, flagFileInfo)
outputPath := hiddenAPISingletonPaths(ctx).flags
baseFlagsPath := hiddenAPISingletonPaths(ctx).stubFlags
buildRuleToGenerateHiddenApiFlags(ctx, "hiddenAPIFlagsFile", "hiddenapi flags", outputPath, baseFlagsPath, moduleSpecificFlagsPaths, &flagFileInfo)
b.generateHiddenAPIStubFlagsRules(ctx, hiddenAPISupportingModules)
b.generateHiddenAPIIndexRules(ctx, hiddenAPISupportingModules)
b.generatedHiddenAPIMetadataRules(ctx, hiddenAPISupportingModules)
}
func (b *platformBootclasspathModule) generateHiddenAPIStubFlagsRules(ctx android.ModuleContext, modules []hiddenAPISupportingModule) {
bootDexJars := android.Paths{}
for _, module := range modules {
bootDexJars = append(bootDexJars, module.bootDexJar)
}
sdkKindToStubPaths := hiddenAPIGatherStubLibDexJarPaths(ctx)
outputPath := hiddenAPISingletonPaths(ctx).stubFlags
rule := ruleToGenerateHiddenAPIStubFlagsFile(ctx, outputPath, bootDexJars, sdkKindToStubPaths)
rule.Build("platform-bootclasspath-monolithic-hiddenapi-stub-flags", "monolithic hidden API stub flags")
}
func (b *platformBootclasspathModule) generateHiddenAPIIndexRules(ctx android.ModuleContext, modules []hiddenAPISupportingModule) {
indexes := android.Paths{}
for _, module := range modules {
indexes = append(indexes, module.indexCSV)
}
rule := android.NewRuleBuilder(pctx, ctx)
rule.Command().
BuiltTool("merge_csv").
Flag("--key_field signature").
FlagWithArg("--header=", "signature,file,startline,startcol,endline,endcol,properties").
FlagWithOutput("--output=", hiddenAPISingletonPaths(ctx).index).
Inputs(indexes)
rule.Build("platform-bootclasspath-monolithic-hiddenapi-index", "monolithic hidden API index")
}
func (b *platformBootclasspathModule) generatedHiddenAPIMetadataRules(ctx android.ModuleContext, modules []hiddenAPISupportingModule) {
metadataCSVFiles := android.Paths{}
for _, module := range modules {
metadataCSVFiles = append(metadataCSVFiles, module.metadataCSV)
}
rule := android.NewRuleBuilder(pctx, ctx)
outputPath := hiddenAPISingletonPaths(ctx).metadata
rule.Command().
BuiltTool("merge_csv").
Flag("--key_field signature").
FlagWithOutput("--output=", outputPath).
Inputs(metadataCSVFiles)
rule.Build("platform-bootclasspath-monolithic-hiddenapi-metadata", "monolithic hidden API metadata")
}
// generateHiddenApiMakeVars generates make variables needed by hidden API related make rules, e.g.
// veridex and run-appcompat.
func (b *platformBootclasspathModule) generateHiddenApiMakeVars(ctx android.MakeVarsContext) {
if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
return
}
// INTERNAL_PLATFORM_HIDDENAPI_FLAGS is used by Make rules in art/ and cts/.
ctx.Strict("INTERNAL_PLATFORM_HIDDENAPI_FLAGS", b.hiddenAPIFlagsCSV.String())
}
// generateBootImageBuildActions generates ninja rules related to the boot image creation.
func (b *platformBootclasspathModule) generateBootImageBuildActions(ctx android.ModuleContext, nonUpdatableModules, updatableModules []android.Module) {
// Force the GlobalSoongConfig to be created and cached for use by the dex_bootjars
// GenerateSingletonBuildActions method as it cannot create it for itself.
dexpreopt.GetGlobalSoongConfig(ctx)
imageConfig := b.getImageConfig(ctx)
if imageConfig == nil {
return
}
global := dexpreopt.GetGlobalConfig(ctx)
if !shouldBuildBootImages(ctx.Config(), global) {
return
}
// Generate the framework profile rule
bootFrameworkProfileRule(ctx, imageConfig)
// Generate the updatable bootclasspath packages rule.
generateUpdatableBcpPackagesRule(ctx, imageConfig, updatableModules)
// Copy non-updatable module dex jars to their predefined locations.
copyBootJarsToPredefinedLocations(ctx, nonUpdatableModules, imageConfig.modules, imageConfig.dexPaths)
// Copy updatable module dex jars to their predefined locations.
config := GetUpdatableBootConfig(ctx)
copyBootJarsToPredefinedLocations(ctx, updatableModules, config.modules, config.dexPaths)
// Build a profile for the image config and then use that to build the boot image.
profile := bootImageProfileRule(ctx, imageConfig)
buildBootImage(ctx, imageConfig, profile)
dumpOatRules(ctx, imageConfig)
}