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 (
|
2019-02-28 17:13:20 +01:00
|
|
|
"strings"
|
|
|
|
|
2019-01-17 00:15:52 +01:00
|
|
|
"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-01 20:01:34 +01:00
|
|
|
// True if the module containing this structure contributes to the hiddenapi information.
|
|
|
|
active bool
|
|
|
|
|
|
|
|
// True if the module only contains additional annotations and so does not require hiddenapi
|
|
|
|
// information to be encoded in its dex file and should not be used to generate the
|
|
|
|
// hiddenAPISingletonPathsStruct.stubFlags file.
|
|
|
|
annotationsOnly 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
|
|
|
|
|
|
|
|
// The path to the CSV file that 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.
|
|
|
|
flagsCSVPath android.Path
|
|
|
|
|
|
|
|
// The path to the CSV file that contains mappings from Java signature to the value of properties
|
|
|
|
// specified on UnsupportedAppUsage annotations in the source.
|
|
|
|
//
|
|
|
|
// Like the flagsCSVPath 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.
|
2019-01-31 23:12:44 +01:00
|
|
|
metadataCSVPath android.Path
|
2021-01-29 13:53:15 +01:00
|
|
|
|
|
|
|
// The path to the CSV file that 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.
|
|
|
|
indexCSVPath android.Path
|
2019-01-31 23:12:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hiddenAPI) flagsCSV() android.Path {
|
|
|
|
return h.flagsCSVPath
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hiddenAPI) metadataCSV() android.Path {
|
|
|
|
return h.metadataCSVPath
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hiddenAPI) bootDexJar() android.Path {
|
|
|
|
return h.bootDexJarPath
|
|
|
|
}
|
|
|
|
|
2020-02-19 17:39:59 +01:00
|
|
|
func (h *hiddenAPI) indexCSV() android.Path {
|
|
|
|
return h.indexCSVPath
|
|
|
|
}
|
|
|
|
|
2019-01-31 23:12:44 +01:00
|
|
|
type hiddenAPIIntf interface {
|
2020-02-19 17:39:59 +01:00
|
|
|
bootDexJar() android.Path
|
2019-01-31 23:12:44 +01:00
|
|
|
flagsCSV() android.Path
|
2020-02-19 17:39:59 +01:00
|
|
|
indexCSV() android.Path
|
2019-01-31 23:12:44 +01:00
|
|
|
metadataCSV() android.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ hiddenAPIIntf = (*hiddenAPI)(nil)
|
|
|
|
|
2021-02-01 20:01:34 +01:00
|
|
|
// Initialize the hiddenapi structure
|
|
|
|
func (h *hiddenAPI) initHiddenAPI(ctx android.BaseModuleContext, name string) {
|
|
|
|
// If hiddenapi processing is disabled treat this as inactive.
|
|
|
|
if ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modules whose names are of the format <x>-hiddenapi provide hiddenapi information for the boot
|
|
|
|
// jar module <x>. Otherwise, the module provides information for itself. Either way extract the
|
|
|
|
// name of the boot jar module.
|
|
|
|
bootJarName := strings.TrimSuffix(name, "-hiddenapi")
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
h.active = inList(bootJarName, ctx.Config().BootJars())
|
|
|
|
|
|
|
|
// If this module has a suffix of -hiddenapi then it only provides additional annotation
|
|
|
|
// information for a module on the boot jars list.
|
|
|
|
h.annotationsOnly = strings.HasSuffix(name, "-hiddenapi")
|
|
|
|
}
|
|
|
|
|
2021-02-03 21:06:33 +01:00
|
|
|
// hiddenAPIExtractAndEncode is called by any module that could contribute to the hiddenapi
|
|
|
|
// processing.
|
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
|
|
|
|
// jar list.
|
|
|
|
//
|
|
|
|
// Otherwise, it generates ninja rules to do the following:
|
2021-02-03 21:06:33 +01:00
|
|
|
// 1. Extract information needed for hiddenapi processing from the module and output it into CSV
|
|
|
|
// files.
|
2021-02-01 20:01:34 +01:00
|
|
|
// 2. Conditionally adds the supplied dex file to the list of files used to generate the
|
|
|
|
// hiddenAPISingletonPathsStruct.stubsFlag file.
|
|
|
|
// 3. Conditionally 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, otherwise simply returns the supplied
|
|
|
|
// dex jar.
|
2021-02-03 21:06:33 +01:00
|
|
|
func (h *hiddenAPI) hiddenAPIExtractAndEncode(ctx android.ModuleContext, name string, primary bool, dexJar android.OutputPath,
|
2021-02-02 14:38:13 +01:00
|
|
|
implementationJar android.Path, uncompressDex bool) android.OutputPath {
|
2021-02-01 20:01:34 +01:00
|
|
|
|
|
|
|
if !h.active {
|
|
|
|
return dexJar
|
|
|
|
}
|
|
|
|
|
2021-02-03 21:06:33 +01:00
|
|
|
h.hiddenAPIExtractInformation(ctx, dexJar, implementationJar, primary)
|
2021-02-01 20:01:34 +01:00
|
|
|
|
|
|
|
if !h.annotationsOnly {
|
|
|
|
hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", name+".jar").OutputPath
|
|
|
|
|
|
|
|
// Create a copy of the dex jar which has been encoded with hiddenapi flags.
|
|
|
|
hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexJar, uncompressDex)
|
|
|
|
|
|
|
|
// Use the encoded dex jar from here onwards.
|
|
|
|
dexJar = hiddenAPIJar
|
2019-01-31 23:12:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return dexJar
|
|
|
|
}
|
|
|
|
|
2021-02-03 21:06:33 +01:00
|
|
|
// hiddenAPIExtractInformation generates ninja rules to extract the information from the classes
|
|
|
|
// jar, and outputs it to the appropriate module specific CSV file.
|
|
|
|
//
|
|
|
|
// It also makes the dex jar available for use when generating the
|
|
|
|
// hiddenAPISingletonPathsStruct.stubFlags.
|
2021-02-03 21:06:33 +01:00
|
|
|
func (h *hiddenAPI) hiddenAPIExtractInformation(ctx android.ModuleContext, dexJar, classesJar android.Path, primary bool) {
|
|
|
|
if !h.active {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// More than one library with the same classes may need to be encoded but only one should be
|
|
|
|
// used as a source of information for hidden API processing otherwise it will result in
|
|
|
|
// duplicate entries in the files.
|
|
|
|
if !primary {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-31 23:12:44 +01:00
|
|
|
stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags
|
2019-01-17 00:15:52 +01:00
|
|
|
|
2021-01-27 16:11:42 +01:00
|
|
|
flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
|
2019-01-17 00:15:52 +01:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: hiddenAPIGenerateCSVRule,
|
|
|
|
Description: "hiddenapi flags",
|
|
|
|
Input: classesJar,
|
|
|
|
Output: flagsCSV,
|
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
|
|
|
},
|
|
|
|
})
|
2020-02-19 17:39:59 +01:00
|
|
|
h.flagsCSVPath = flagsCSV
|
2019-01-17 00:15:52 +01:00
|
|
|
|
2021-01-27 16:11:42 +01:00
|
|
|
metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
|
2019-01-17 00:15:52 +01:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: hiddenAPIGenerateCSVRule,
|
|
|
|
Description: "hiddenapi metadata",
|
|
|
|
Input: classesJar,
|
|
|
|
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
|
|
|
},
|
|
|
|
})
|
2020-02-19 17:39:59 +01:00
|
|
|
h.metadataCSVPath = metadataCSV
|
|
|
|
|
2021-01-27 16:11:42 +01:00
|
|
|
indexCSV := android.PathForModuleOut(ctx, "hiddenapi", "index.csv")
|
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").
|
2020-02-19 17:39:59 +01:00
|
|
|
FlagWithInput("--zip_input=", classesJar).
|
|
|
|
FlagWithOutput("--output=", indexCSV)
|
2020-11-17 02:32:30 +01:00
|
|
|
rule.Build("merged-hiddenapi-index", "Merged Hidden API index")
|
2020-02-19 17:39:59 +01:00
|
|
|
h.indexCSVPath = indexCSV
|
2021-02-03 21:06:33 +01:00
|
|
|
|
|
|
|
// Save the unencoded dex jar so it can be used when generating the
|
|
|
|
// hiddenAPISingletonPathsStruct.stubFlags file.
|
|
|
|
h.bootDexJarPath = dexJar
|
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" &&
|
|
|
|
${config.MergeZipsCmd} -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
|
|
|
|
2019-01-31 23:12:44 +01:00
|
|
|
func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.Path,
|
2019-01-19 07:03:02 +01:00
|
|
|
uncompressDex bool) {
|
2019-01-17 00:15:52 +01:00
|
|
|
|
2019-01-31 23:12:44 +01:00
|
|
|
flagsCSV := hiddenAPISingletonPaths(ctx).flags
|
2019-01-17 00:15:52 +01:00
|
|
|
|
2019-01-19 07:03:02 +01:00
|
|
|
// The encode dex rule requires unzipping and rezipping the classes.dex files, ensure that if it was uncompressed
|
|
|
|
// in the input it stays uncompressed in the output.
|
|
|
|
soongZipFlags := ""
|
2019-01-23 22:04:05 +01:00
|
|
|
hiddenapiFlags := ""
|
2019-01-22 00:20:23 +01:00
|
|
|
tmpOutput := output
|
|
|
|
tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex")
|
2019-01-19 07:03:02 +01:00
|
|
|
if uncompressDex {
|
|
|
|
soongZipFlags = "-L 0"
|
2019-01-22 00:20:23 +01:00
|
|
|
tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar")
|
|
|
|
tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned")
|
2019-01-19 07:03:02 +01:00
|
|
|
}
|
2020-02-21 08:04:53 +01:00
|
|
|
|
|
|
|
enforceHiddenApiFlagsToAllMembers := true
|
2019-01-23 22:04:05 +01:00
|
|
|
// If frameworks/base doesn't exist we must be building with the 'master-art' manifest.
|
|
|
|
// Disable assertion that all methods/fields have hidden API flags assigned.
|
|
|
|
if !ctx.Config().FrameworksBaseDirExists(ctx) {
|
2020-02-21 08:04:53 +01:00
|
|
|
enforceHiddenApiFlagsToAllMembers = false
|
|
|
|
}
|
|
|
|
// 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.
|
2020-05-19 22:07:52 +02:00
|
|
|
if j, ok := ctx.Module().(interface {
|
|
|
|
shouldInstrument(android.BaseModuleContext) bool
|
|
|
|
}); ok && j.shouldInstrument(ctx) {
|
2020-02-21 08:04:53 +01:00
|
|
|
enforceHiddenApiFlagsToAllMembers = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !enforceHiddenApiFlagsToAllMembers {
|
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,
|
2019-01-22 00:20:23 +01:00
|
|
|
Output: tmpOutput,
|
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 {
|
|
|
|
TransformZipAlign(ctx, output, tmpOutput)
|
|
|
|
}
|
2019-01-17 00:15:52 +01:00
|
|
|
}
|