2019-01-17 00:15:52 +01:00
|
|
|
// Copyright 2019 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 (
|
|
|
|
"github.com/google/blueprint"
|
|
|
|
|
|
|
|
"android/soong/android"
|
|
|
|
)
|
|
|
|
|
|
|
|
var hiddenAPIGenerateCSVRule = pctx.AndroidStaticRule("hiddenAPIGenerateCSV", blueprint.RuleParams{
|
2020-08-12 17:48:23 +02:00
|
|
|
Command: "${config.Class2NonSdkList} --stub-api-flags ${stubAPIFlags} $in $outFlag $out",
|
|
|
|
CommandDeps: []string{"${config.Class2NonSdkList}"},
|
2019-01-18 17:30:03 +01:00
|
|
|
}, "outFlag", "stubAPIFlags")
|
2019-01-17 00:15:52 +01:00
|
|
|
|
2019-01-31 23:12:44 +01:00
|
|
|
type hiddenAPI struct {
|
2021-02-09 15:34:25 +01:00
|
|
|
// True if the module containing this structure contributes to the hiddenapi information or has
|
|
|
|
// that information encoded within it.
|
2021-02-01 20:01:34 +01:00
|
|
|
active bool
|
|
|
|
|
2021-01-29 13:53:15 +01:00
|
|
|
// The path to the dex jar that is in the boot class path. If this is nil then the associated
|
|
|
|
// module is not a boot jar, but could be one of the <x>-hiddenapi modules that provide additional
|
|
|
|
// annotations for the <x> boot dex jar but which do not actually provide a boot dex jar
|
|
|
|
// themselves.
|
2021-02-01 20:01:34 +01:00
|
|
|
//
|
|
|
|
// This must be the path to the unencoded dex jar as the encoded dex jar indirectly depends on
|
|
|
|
// this file so using the encoded dex jar here would result in a cycle in the ninja rules.
|
2021-01-29 13:53:15 +01:00
|
|
|
bootDexJarPath android.Path
|
|
|
|
|
2021-04-22 17:43:06 +02:00
|
|
|
// The paths to the classes jars that contain classes and class members annotated with
|
|
|
|
// the UnsupportedAppUsage annotation that need to be extracted as part of the hidden API
|
|
|
|
// processing.
|
|
|
|
classesJarPaths android.Paths
|
2021-05-14 16:52:25 +02:00
|
|
|
|
|
|
|
// The compressed state of the dex file being encoded. This is used to ensure that the encoded
|
|
|
|
// dex file has the same state.
|
|
|
|
uncompressDexState *bool
|
2019-01-31 23:12:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hiddenAPI) bootDexJar() android.Path {
|
|
|
|
return h.bootDexJarPath
|
|
|
|
}
|
|
|
|
|
2021-04-22 17:43:06 +02:00
|
|
|
func (h *hiddenAPI) classesJars() android.Paths {
|
|
|
|
return h.classesJarPaths
|
|
|
|
}
|
|
|
|
|
2021-05-14 16:52:25 +02:00
|
|
|
func (h *hiddenAPI) uncompressDex() *bool {
|
|
|
|
return h.uncompressDexState
|
|
|
|
}
|
|
|
|
|
2021-05-14 11:38:00 +02:00
|
|
|
// hiddenAPIModule is the interface a module that embeds the hiddenAPI structure must implement.
|
|
|
|
type hiddenAPIModule interface {
|
|
|
|
android.Module
|
|
|
|
hiddenAPIIntf
|
|
|
|
}
|
|
|
|
|
2019-01-31 23:12:44 +01:00
|
|
|
type hiddenAPIIntf interface {
|
2020-02-19 17:39:59 +01:00
|
|
|
bootDexJar() android.Path
|
2021-04-22 17:43:06 +02:00
|
|
|
classesJars() android.Paths
|
2021-05-14 16:52:25 +02:00
|
|
|
uncompressDex() *bool
|
2019-01-31 23:12:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ hiddenAPIIntf = (*hiddenAPI)(nil)
|
|
|
|
|
2021-02-01 20:01:34 +01:00
|
|
|
// Initialize the hiddenapi structure
|
2021-05-14 16:52:25 +02:00
|
|
|
//
|
|
|
|
// uncompressedDexState should be nil when the module is a prebuilt and so does not require hidden
|
|
|
|
// API encoding.
|
|
|
|
func (h *hiddenAPI) initHiddenAPI(ctx android.ModuleContext, dexJar, classesJar android.Path, uncompressedDexState *bool) {
|
2021-05-14 15:18:47 +02:00
|
|
|
|
|
|
|
// Save the classes jars even if this is not active as they may be used by modular hidden API
|
|
|
|
// processing.
|
|
|
|
classesJars := android.Paths{classesJar}
|
|
|
|
ctx.VisitDirectDepsWithTag(hiddenApiAnnotationsTag, func(dep android.Module) {
|
|
|
|
javaInfo := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
|
|
|
|
classesJars = append(classesJars, javaInfo.ImplementationJars...)
|
|
|
|
})
|
|
|
|
h.classesJarPaths = classesJars
|
|
|
|
|
|
|
|
// Save the unencoded dex jar so it can be used when generating the
|
|
|
|
// hiddenAPISingletonPathsStruct.stubFlags file.
|
|
|
|
h.bootDexJarPath = dexJar
|
|
|
|
|
2021-05-14 16:52:25 +02:00
|
|
|
h.uncompressDexState = uncompressedDexState
|
|
|
|
|
2021-02-01 20:01:34 +01:00
|
|
|
// If hiddenapi processing is disabled treat this as inactive.
|
|
|
|
if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:18:47 +02:00
|
|
|
// The context module must implement hiddenAPIModule.
|
|
|
|
module := ctx.Module().(hiddenAPIModule)
|
|
|
|
|
2021-05-14 08:52:42 +02:00
|
|
|
// If the frameworks/base directories does not exist and no prebuilt hidden API flag files have
|
|
|
|
// been configured then it is not possible to do hidden API encoding.
|
|
|
|
if !ctx.Config().FrameworksBaseDirExists(ctx) && ctx.Config().PrebuiltHiddenApiDir(ctx) == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-01 20:01:34 +01:00
|
|
|
// It is important that hiddenapi information is only gathered for/from modules that are actually
|
|
|
|
// on the boot jars list because the runtime only enforces access to the hidden API for the
|
|
|
|
// bootclassloader. If information is gathered for modules not on the list then that will cause
|
|
|
|
// failures in the CtsHiddenApiBlocklist... tests.
|
2021-02-12 16:42:46 +01:00
|
|
|
h.active = isModuleInBootClassPath(ctx, module)
|
2021-02-01 20:01:34 +01:00
|
|
|
}
|
|
|
|
|
2021-02-12 16:42:46 +01:00
|
|
|
func isModuleInBootClassPath(ctx android.BaseModuleContext, module android.Module) bool {
|
|
|
|
// Get the configured non-updatable and updatable boot jars.
|
|
|
|
nonUpdatableBootJars := ctx.Config().NonUpdatableBootJars()
|
|
|
|
updatableBootJars := ctx.Config().UpdatableBootJars()
|
|
|
|
active := isModuleInConfiguredList(ctx, module, nonUpdatableBootJars) ||
|
|
|
|
isModuleInConfiguredList(ctx, module, updatableBootJars)
|
|
|
|
return active
|
|
|
|
}
|
|
|
|
|
2021-05-14 14:04:04 +02:00
|
|
|
// hiddenAPIEncodeDex is called by any module that needs to encode dex files.
|
2021-02-01 20:01:34 +01:00
|
|
|
//
|
|
|
|
// It ignores any module that has not had initHiddenApi() called on it and which is not in the boot
|
2021-05-14 14:04:04 +02:00
|
|
|
// jar list. In that case it simply returns the supplied dex jar path.
|
2021-02-01 20:01:34 +01:00
|
|
|
//
|
2021-05-14 14:04:04 +02:00
|
|
|
// Otherwise, it creates a copy of the supplied dex file into which it has encoded the hiddenapi
|
|
|
|
// flags and returns this instead of the supplied dex jar.
|
2021-05-14 16:52:25 +02:00
|
|
|
func (h *hiddenAPI) hiddenAPIEncodeDex(ctx android.ModuleContext, dexJar android.OutputPath) android.OutputPath {
|
2021-05-14 02:13:55 +02:00
|
|
|
|
2021-02-01 20:01:34 +01:00
|
|
|
if !h.active {
|
|
|
|
return dexJar
|
|
|
|
}
|
|
|
|
|
2021-05-14 16:52:25 +02:00
|
|
|
// A nil uncompressDexState prevents the dex file from being encoded.
|
|
|
|
if h.uncompressDexState == nil {
|
|
|
|
ctx.ModuleErrorf("cannot encode dex file %s when uncompressDexState is nil", dexJar)
|
|
|
|
}
|
|
|
|
uncompressDex := *h.uncompressDexState
|
|
|
|
|
2021-02-12 16:42:20 +01:00
|
|
|
// Create a copy of the dex jar which has been encoded with hiddenapi flags.
|
2021-05-17 00:32:52 +02:00
|
|
|
flagsCSV := hiddenAPISingletonPaths(ctx).flags
|
|
|
|
outputDir := android.PathForModuleOut(ctx, "hiddenapi").OutputPath
|
|
|
|
encodedDex := hiddenAPIEncodeDex(ctx, dexJar, flagsCSV, uncompressDex, outputDir)
|
2021-02-01 20:01:34 +01:00
|
|
|
|
2021-02-12 16:42:20 +01:00
|
|
|
// Use the encoded dex jar from here onwards.
|
2021-05-17 00:32:52 +02:00
|
|
|
return encodedDex
|
2019-01-31 23:12:44 +01:00
|
|
|
}
|
|
|
|
|
2021-05-14 10:58:48 +02:00
|
|
|
// buildRuleToGenerateAnnotationFlags builds a ninja rule to generate the annotation-flags.csv file
|
|
|
|
// from the classes jars and stub-flags.csv files.
|
2021-05-14 14:04:04 +02:00
|
|
|
//
|
|
|
|
// The annotation-flags.csv file contains mappings from Java signature to various flags derived from
|
|
|
|
// annotations in the source, e.g. whether it is public or the sdk version above which it can no
|
|
|
|
// longer be used.
|
|
|
|
//
|
|
|
|
// It is created by the Class2NonSdkList tool which processes the .class files in the class
|
|
|
|
// implementation jar looking for UnsupportedAppUsage and CovariantReturnType annotations. The
|
|
|
|
// tool also consumes the hiddenAPISingletonPathsStruct.stubFlags file in order to perform
|
|
|
|
// consistency checks on the information in the annotations and to filter out bridge methods
|
|
|
|
// that are already part of the public API.
|
2021-05-14 10:58:48 +02:00
|
|
|
func buildRuleToGenerateAnnotationFlags(ctx android.ModuleContext, desc string, classesJars android.Paths, stubFlagsCSV android.Path, outputPath android.WritablePath) {
|
2019-01-17 00:15:52 +01:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: hiddenAPIGenerateCSVRule,
|
2021-05-14 10:58:48 +02:00
|
|
|
Description: desc,
|
2021-02-12 12:46:42 +01:00
|
|
|
Inputs: classesJars,
|
2021-05-14 10:58:48 +02:00
|
|
|
Output: outputPath,
|
2019-01-18 17:30:03 +01:00
|
|
|
Implicit: stubFlagsCSV,
|
2019-01-17 00:15:52 +01:00
|
|
|
Args: map[string]string{
|
2019-01-18 17:30:03 +01:00
|
|
|
"outFlag": "--write-flags-csv",
|
|
|
|
"stubAPIFlags": stubFlagsCSV.String(),
|
2019-01-17 00:15:52 +01:00
|
|
|
},
|
|
|
|
})
|
2021-05-14 10:58:48 +02:00
|
|
|
}
|
2019-01-17 00:15:52 +01:00
|
|
|
|
2021-05-14 10:58:48 +02:00
|
|
|
// buildRuleToGenerateMetadata builds a ninja rule to generate the metadata.csv file from
|
|
|
|
// the classes jars and stub-flags.csv files.
|
2021-05-14 14:04:04 +02:00
|
|
|
//
|
|
|
|
// The metadata.csv file contains mappings from Java signature to the value of properties specified
|
|
|
|
// on UnsupportedAppUsage annotations in the source.
|
|
|
|
//
|
|
|
|
// Like the annotation-flags.csv file this is also created by the Class2NonSdkList in the same way.
|
|
|
|
// Although the two files could potentially be created in a single invocation of the
|
|
|
|
// Class2NonSdkList at the moment they are created using their own invocation, with the behavior
|
|
|
|
// being determined by the property that is used.
|
2021-05-14 10:58:48 +02:00
|
|
|
func buildRuleToGenerateMetadata(ctx android.ModuleContext, desc string, classesJars android.Paths, stubFlagsCSV android.Path, metadataCSV android.WritablePath) {
|
2019-01-17 00:15:52 +01:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: hiddenAPIGenerateCSVRule,
|
2021-05-14 10:58:48 +02:00
|
|
|
Description: desc,
|
2021-02-12 12:46:42 +01:00
|
|
|
Inputs: classesJars,
|
2019-01-17 00:15:52 +01:00
|
|
|
Output: metadataCSV,
|
2019-01-18 17:30:03 +01:00
|
|
|
Implicit: stubFlagsCSV,
|
2019-01-17 00:15:52 +01:00
|
|
|
Args: map[string]string{
|
2019-01-18 17:30:03 +01:00
|
|
|
"outFlag": "--write-metadata-csv",
|
|
|
|
"stubAPIFlags": stubFlagsCSV.String(),
|
2019-01-17 00:15:52 +01:00
|
|
|
},
|
|
|
|
})
|
2021-05-14 10:58:48 +02:00
|
|
|
}
|
2020-02-19 17:39:59 +01:00
|
|
|
|
2021-05-14 14:04:04 +02:00
|
|
|
// buildRuleToGenerateIndex builds a ninja rule to generate the index.csv file from the classes
|
2021-05-14 10:58:48 +02:00
|
|
|
// jars.
|
2021-05-14 14:04:04 +02:00
|
|
|
//
|
|
|
|
// The index.csv file contains mappings from Java signature to source location information.
|
|
|
|
//
|
|
|
|
// It is created by the merge_csv tool which processes the class implementation jar, extracting
|
|
|
|
// all the files ending in .uau (which are CSV files) and merges them together. The .uau files are
|
|
|
|
// created by the unsupported app usage annotation processor during compilation of the class
|
|
|
|
// implementation jar.
|
2021-05-14 10:58:48 +02:00
|
|
|
func buildRuleToGenerateIndex(ctx android.ModuleContext, desc string, classesJars android.Paths, indexCSV android.WritablePath) {
|
2020-11-17 02:32:30 +01:00
|
|
|
rule := android.NewRuleBuilder(pctx, ctx)
|
2020-02-19 17:39:59 +01:00
|
|
|
rule.Command().
|
2020-11-17 02:32:30 +01:00
|
|
|
BuiltTool("merge_csv").
|
2021-02-12 12:46:42 +01:00
|
|
|
Flag("--zip_input").
|
2021-02-16 17:57:06 +01:00
|
|
|
Flag("--key_field signature").
|
2021-05-14 11:38:00 +02:00
|
|
|
FlagWithArg("--header=", "signature,file,startline,startcol,endline,endcol,properties").
|
2021-02-12 12:46:42 +01:00
|
|
|
FlagWithOutput("--output=", indexCSV).
|
|
|
|
Inputs(classesJars)
|
2021-05-14 10:58:48 +02:00
|
|
|
rule.Build(desc, desc)
|
2019-01-17 00:15:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
|
2020-02-19 17:39:59 +01:00
|
|
|
Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output &&
|
2020-07-12 07:30:45 +02:00
|
|
|
unzip -qoDD $in 'classes*.dex' -d $tmpDir/dex-input &&
|
2020-02-19 17:39:59 +01:00
|
|
|
for INPUT_DEX in $$(find $tmpDir/dex-input -maxdepth 1 -name 'classes*.dex' | sort); do
|
|
|
|
echo "--input-dex=$${INPUT_DEX}";
|
|
|
|
echo "--output-dex=$tmpDir/dex-output/$$(basename $${INPUT_DEX})";
|
|
|
|
done | xargs ${config.HiddenAPI} encode --api-flags=$flagsCsv $hiddenapiFlags &&
|
|
|
|
${config.SoongZipCmd} $soongZipFlags -o $tmpDir/dex.jar -C $tmpDir/dex-output -f "$tmpDir/dex-output/classes*.dex" &&
|
2021-05-16 06:21:16 +02:00
|
|
|
${config.MergeZipsCmd} -j -D -zipToNotStrip $tmpDir/dex.jar -stripFile "classes*.dex" -stripFile "**/*.uau" $out $tmpDir/dex.jar $in`,
|
2019-01-17 00:15:52 +01:00
|
|
|
CommandDeps: []string{
|
|
|
|
"${config.HiddenAPI}",
|
|
|
|
"${config.SoongZipCmd}",
|
|
|
|
"${config.MergeZipsCmd}",
|
|
|
|
},
|
2019-01-23 22:04:05 +01:00
|
|
|
}, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
|
2019-01-19 07:03:02 +01:00
|
|
|
|
2021-05-17 00:32:52 +02:00
|
|
|
// hiddenAPIEncodeDex generates the build rule that will encode the supplied dex jar and place the
|
|
|
|
// encoded dex jar in a file of the same name in the output directory.
|
|
|
|
//
|
|
|
|
// The encode dex rule requires unzipping, encoding and rezipping the classes.dex files along with
|
|
|
|
// all the resources from the input jar. It also ensures that if it was uncompressed in the input
|
|
|
|
// it stays uncompressed in the output.
|
|
|
|
func hiddenAPIEncodeDex(ctx android.ModuleContext, dexInput, flagsCSV android.Path, uncompressDex bool, outputDir android.OutputPath) android.OutputPath {
|
2019-01-17 00:15:52 +01:00
|
|
|
|
2021-05-17 00:32:52 +02:00
|
|
|
// The output file has the same name as the input file and is in the output directory.
|
|
|
|
output := outputDir.Join(ctx, dexInput.Base())
|
|
|
|
|
|
|
|
// Create a jar specific temporary directory in which to do the work just in case this is called
|
|
|
|
// with the same output directory for multiple modules.
|
|
|
|
tmpDir := outputDir.Join(ctx, dexInput.Base()+"-tmp")
|
2019-01-17 00:15:52 +01:00
|
|
|
|
2021-05-17 00:32:52 +02:00
|
|
|
// If the input is uncompressed then generate the output of the encode rule to an intermediate
|
|
|
|
// file as the final output will need further processing after encoding.
|
2019-01-19 07:03:02 +01:00
|
|
|
soongZipFlags := ""
|
2021-05-17 00:32:52 +02:00
|
|
|
encodeRuleOutput := output
|
2019-01-19 07:03:02 +01:00
|
|
|
if uncompressDex {
|
|
|
|
soongZipFlags = "-L 0"
|
2021-05-17 00:32:52 +02:00
|
|
|
encodeRuleOutput = outputDir.Join(ctx, "unaligned", dexInput.Base())
|
2019-01-19 07:03:02 +01:00
|
|
|
}
|
2020-02-21 08:04:53 +01:00
|
|
|
|
|
|
|
// b/149353192: when a module is instrumented, jacoco adds synthetic members
|
|
|
|
// $jacocoData and $jacocoInit. Since they don't exist when building the hidden API flags,
|
|
|
|
// don't complain when we don't find hidden API flags for the synthetic members.
|
2021-05-17 00:32:52 +02:00
|
|
|
hiddenapiFlags := ""
|
2020-05-19 22:07:52 +02:00
|
|
|
if j, ok := ctx.Module().(interface {
|
|
|
|
shouldInstrument(android.BaseModuleContext) bool
|
|
|
|
}); ok && j.shouldInstrument(ctx) {
|
2019-01-23 22:04:05 +01:00
|
|
|
hiddenapiFlags = "--no-force-assign-all"
|
|
|
|
}
|
2019-01-19 07:03:02 +01:00
|
|
|
|
2019-01-17 00:15:52 +01:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: hiddenAPIEncodeDexRule,
|
|
|
|
Description: "hiddenapi encode dex",
|
|
|
|
Input: dexInput,
|
2021-05-17 00:32:52 +02:00
|
|
|
Output: encodeRuleOutput,
|
2019-01-31 23:12:44 +01:00
|
|
|
Implicit: flagsCSV,
|
2019-01-17 00:15:52 +01:00
|
|
|
Args: map[string]string{
|
2019-01-31 23:12:44 +01:00
|
|
|
"flagsCsv": flagsCSV.String(),
|
2019-01-23 22:04:05 +01:00
|
|
|
"tmpDir": tmpDir.String(),
|
|
|
|
"soongZipFlags": soongZipFlags,
|
|
|
|
"hiddenapiFlags": hiddenapiFlags,
|
2019-01-17 00:15:52 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2019-01-22 00:20:23 +01:00
|
|
|
if uncompressDex {
|
2021-05-17 00:32:52 +02:00
|
|
|
TransformZipAlign(ctx, output, encodeRuleOutput)
|
2019-01-22 00:20:23 +01:00
|
|
|
}
|
2021-05-17 00:32:52 +02:00
|
|
|
|
|
|
|
return output
|
2019-01-17 00:15:52 +01:00
|
|
|
}
|
2021-02-12 12:46:42 +01:00
|
|
|
|
|
|
|
type hiddenApiAnnotationsDependencyTag struct {
|
|
|
|
blueprint.BaseDependencyTag
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tag used to mark dependencies on java_library instances that contains Java source files whose
|
|
|
|
// sole purpose is to provide additional hiddenapi annotations.
|
|
|
|
var hiddenApiAnnotationsTag hiddenApiAnnotationsDependencyTag
|
|
|
|
|
|
|
|
// Mark this tag so dependencies that use it are excluded from APEX contents.
|
|
|
|
func (t hiddenApiAnnotationsDependencyTag) ExcludeFromApexContents() {}
|
|
|
|
|
|
|
|
var _ android.ExcludeFromApexContentsTag = hiddenApiAnnotationsTag
|