2019-01-17 07:15:52 +08: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-03-01 00:13:20 +08:00
|
|
|
"strings"
|
|
|
|
|
2019-01-17 07:15:52 +08:00
|
|
|
"github.com/google/blueprint"
|
|
|
|
|
|
|
|
"android/soong/android"
|
|
|
|
)
|
|
|
|
|
|
|
|
var hiddenAPIGenerateCSVRule = pctx.AndroidStaticRule("hiddenAPIGenerateCSV", blueprint.RuleParams{
|
2020-08-12 23:48:23 +08:00
|
|
|
Command: "${config.Class2NonSdkList} --stub-api-flags ${stubAPIFlags} $in $outFlag $out",
|
|
|
|
CommandDeps: []string{"${config.Class2NonSdkList}"},
|
2019-01-19 00:30:03 +08:00
|
|
|
}, "outFlag", "stubAPIFlags")
|
2019-01-17 07:15:52 +08:00
|
|
|
|
2019-02-01 06:12:44 +08:00
|
|
|
type hiddenAPI struct {
|
2020-02-20 00:39:59 +08:00
|
|
|
bootDexJarPath android.Path
|
2019-02-01 06:12:44 +08:00
|
|
|
flagsCSVPath android.Path
|
2020-02-20 00:39:59 +08:00
|
|
|
indexCSVPath android.Path
|
2019-02-01 06:12:44 +08:00
|
|
|
metadataCSVPath android.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
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-20 00:39:59 +08:00
|
|
|
func (h *hiddenAPI) indexCSV() android.Path {
|
|
|
|
return h.indexCSVPath
|
|
|
|
}
|
|
|
|
|
2019-02-01 06:12:44 +08:00
|
|
|
type hiddenAPIIntf interface {
|
2020-02-20 00:39:59 +08:00
|
|
|
bootDexJar() android.Path
|
2019-02-01 06:12:44 +08:00
|
|
|
flagsCSV() android.Path
|
2020-02-20 00:39:59 +08:00
|
|
|
indexCSV() android.Path
|
2019-02-01 06:12:44 +08:00
|
|
|
metadataCSV() android.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ hiddenAPIIntf = (*hiddenAPI)(nil)
|
|
|
|
|
2020-06-24 23:22:38 +08:00
|
|
|
func (h *hiddenAPI) hiddenAPI(ctx android.ModuleContext, name string, primary bool, dexJar android.ModuleOutPath,
|
2020-02-20 00:39:59 +08:00
|
|
|
implementationJar android.Path, uncompressDex bool) android.ModuleOutPath {
|
2019-02-01 06:12:44 +08:00
|
|
|
if !ctx.Config().IsEnvTrue("UNSAFE_DISABLE_HIDDENAPI_FLAGS") {
|
2019-03-01 00:13:20 +08:00
|
|
|
|
|
|
|
// 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")
|
|
|
|
|
|
|
|
// If this module is on the boot jars list (or providing information for a module
|
|
|
|
// on the list) then extract the hiddenapi information from it, and if necessary
|
|
|
|
// encode that information in the generated dex file.
|
|
|
|
//
|
|
|
|
// It is important that hiddenapi information is only gathered for/from modules on
|
|
|
|
// 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 CtsHiddenApiBlacklist...
|
|
|
|
// tests.
|
|
|
|
if inList(bootJarName, ctx.Config().BootJars()) {
|
2019-02-01 06:12:44 +08:00
|
|
|
// Derive the greylist from classes jar.
|
|
|
|
flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
|
|
|
|
metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
|
2020-02-20 00:39:59 +08:00
|
|
|
indexCSV := android.PathForModuleOut(ctx, "hiddenapi", "index.csv")
|
|
|
|
h.hiddenAPIGenerateCSV(ctx, flagsCSV, metadataCSV, indexCSV, implementationJar)
|
2019-03-01 00:13:20 +08:00
|
|
|
|
|
|
|
// If this module is actually on the boot jars list and not providing
|
|
|
|
// hiddenapi information for a module on the boot jars list then encode
|
|
|
|
// the gathered information in the generated dex file.
|
|
|
|
if name == bootJarName {
|
|
|
|
hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", name+".jar")
|
2020-06-24 23:22:38 +08:00
|
|
|
|
|
|
|
// More than one library with the same classes can be encoded but only one can
|
|
|
|
// be added to the global set of flags, otherwise it will result in duplicate
|
|
|
|
// classes which is an error. Therefore, only add the dex jar of one of them
|
|
|
|
// to the global set of flags.
|
|
|
|
if primary {
|
|
|
|
h.bootDexJarPath = dexJar
|
|
|
|
}
|
2019-03-01 00:13:20 +08:00
|
|
|
hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexJar, uncompressDex)
|
|
|
|
dexJar = hiddenAPIJar
|
|
|
|
}
|
2019-02-01 06:12:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dexJar
|
|
|
|
}
|
|
|
|
|
2020-02-20 00:39:59 +08:00
|
|
|
func (h *hiddenAPI) hiddenAPIGenerateCSV(ctx android.ModuleContext, flagsCSV, metadataCSV, indexCSV android.WritablePath, classesJar android.Path) {
|
2019-02-01 06:12:44 +08:00
|
|
|
stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags
|
2019-01-17 07:15:52 +08:00
|
|
|
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: hiddenAPIGenerateCSVRule,
|
|
|
|
Description: "hiddenapi flags",
|
|
|
|
Input: classesJar,
|
|
|
|
Output: flagsCSV,
|
2019-01-19 00:30:03 +08:00
|
|
|
Implicit: stubFlagsCSV,
|
2019-01-17 07:15:52 +08:00
|
|
|
Args: map[string]string{
|
2019-01-19 00:30:03 +08:00
|
|
|
"outFlag": "--write-flags-csv",
|
|
|
|
"stubAPIFlags": stubFlagsCSV.String(),
|
2019-01-17 07:15:52 +08:00
|
|
|
},
|
|
|
|
})
|
2020-02-20 00:39:59 +08:00
|
|
|
h.flagsCSVPath = flagsCSV
|
2019-01-17 07:15:52 +08:00
|
|
|
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: hiddenAPIGenerateCSVRule,
|
|
|
|
Description: "hiddenapi metadata",
|
|
|
|
Input: classesJar,
|
|
|
|
Output: metadataCSV,
|
2019-01-19 00:30:03 +08:00
|
|
|
Implicit: stubFlagsCSV,
|
2019-01-17 07:15:52 +08:00
|
|
|
Args: map[string]string{
|
2019-01-19 00:30:03 +08:00
|
|
|
"outFlag": "--write-metadata-csv",
|
|
|
|
"stubAPIFlags": stubFlagsCSV.String(),
|
2019-01-17 07:15:52 +08:00
|
|
|
},
|
|
|
|
})
|
2020-02-20 00:39:59 +08:00
|
|
|
h.metadataCSVPath = metadataCSV
|
|
|
|
|
|
|
|
rule := android.NewRuleBuilder()
|
|
|
|
rule.Command().
|
|
|
|
BuiltTool(ctx, "merge_csv").
|
|
|
|
FlagWithInput("--zip_input=", classesJar).
|
|
|
|
FlagWithOutput("--output=", indexCSV)
|
|
|
|
rule.Build(pctx, ctx, "merged-hiddenapi-index", "Merged Hidden API index")
|
|
|
|
h.indexCSVPath = indexCSV
|
2019-01-17 07:15:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{
|
2020-02-20 00:39:59 +08:00
|
|
|
Command: `rm -rf $tmpDir && mkdir -p $tmpDir && mkdir $tmpDir/dex-input && mkdir $tmpDir/dex-output &&
|
2020-07-12 13:30:45 +08:00
|
|
|
unzip -qoDD $in 'classes*.dex' -d $tmpDir/dex-input &&
|
2020-02-20 00:39:59 +08: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 07:15:52 +08:00
|
|
|
CommandDeps: []string{
|
|
|
|
"${config.HiddenAPI}",
|
|
|
|
"${config.SoongZipCmd}",
|
|
|
|
"${config.MergeZipsCmd}",
|
|
|
|
},
|
2019-01-24 05:04:05 +08:00
|
|
|
}, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
|
2019-01-19 14:03:02 +08:00
|
|
|
|
2019-02-01 06:12:44 +08:00
|
|
|
func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.Path,
|
2019-01-19 14:03:02 +08:00
|
|
|
uncompressDex bool) {
|
2019-01-17 07:15:52 +08:00
|
|
|
|
2019-02-01 06:12:44 +08:00
|
|
|
flagsCSV := hiddenAPISingletonPaths(ctx).flags
|
2019-01-17 07:15:52 +08:00
|
|
|
|
2019-01-19 14:03:02 +08: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-24 05:04:05 +08:00
|
|
|
hiddenapiFlags := ""
|
2019-01-22 07:20:23 +08:00
|
|
|
tmpOutput := output
|
|
|
|
tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex")
|
2019-01-19 14:03:02 +08:00
|
|
|
if uncompressDex {
|
|
|
|
soongZipFlags = "-L 0"
|
2019-01-22 07:20:23 +08:00
|
|
|
tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar")
|
|
|
|
tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned")
|
2019-01-19 14:03:02 +08:00
|
|
|
}
|
2020-02-21 15:04:53 +08:00
|
|
|
|
|
|
|
enforceHiddenApiFlagsToAllMembers := true
|
2019-01-24 05:04:05 +08: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 15:04:53 +08: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-20 04:07:52 +08:00
|
|
|
if j, ok := ctx.Module().(interface {
|
|
|
|
shouldInstrument(android.BaseModuleContext) bool
|
|
|
|
}); ok && j.shouldInstrument(ctx) {
|
2020-02-21 15:04:53 +08:00
|
|
|
enforceHiddenApiFlagsToAllMembers = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !enforceHiddenApiFlagsToAllMembers {
|
2019-01-24 05:04:05 +08:00
|
|
|
hiddenapiFlags = "--no-force-assign-all"
|
|
|
|
}
|
2019-01-19 14:03:02 +08:00
|
|
|
|
2019-01-17 07:15:52 +08:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: hiddenAPIEncodeDexRule,
|
|
|
|
Description: "hiddenapi encode dex",
|
|
|
|
Input: dexInput,
|
2019-01-22 07:20:23 +08:00
|
|
|
Output: tmpOutput,
|
2019-02-01 06:12:44 +08:00
|
|
|
Implicit: flagsCSV,
|
2019-01-17 07:15:52 +08:00
|
|
|
Args: map[string]string{
|
2019-02-01 06:12:44 +08:00
|
|
|
"flagsCsv": flagsCSV.String(),
|
2019-01-24 05:04:05 +08:00
|
|
|
"tmpDir": tmpDir.String(),
|
|
|
|
"soongZipFlags": soongZipFlags,
|
|
|
|
"hiddenapiFlags": hiddenapiFlags,
|
2019-01-17 07:15:52 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2019-01-22 07:20:23 +08:00
|
|
|
if uncompressDex {
|
|
|
|
TransformZipAlign(ctx, output, tmpOutput)
|
|
|
|
}
|
2019-01-17 07:15:52 +08:00
|
|
|
}
|