construct_context.py is a script that runs at Ninja stage and constructs class loader context arguments for dex2oat. Previously it accepted lists of library paths and concatenated them into one class loader context string. The script also handled the dependency of "android.hidl.manager" on "android.hidl.base" that is not tracked by the build system and must be handled in a special way. Now that class loader context representation is going to change from flat lists to trees, passing paths to individual libraries as script arguments is no longer possible, because a list of paths cannot represent a class loader context tree. Passing the trees in a serialized form is also inconvenient, because the script would have to parse them, which would complicate it a lot. Therefore this patch ports all the concatenation and "android.hidl.base" handling to Soong. It is not possible to port the remaining script to Soong because Soong has no information about the targetSdkVersiion of the dexpreopted apps (it is in the manifest and sometimes in an APK, and has to be extracted at Ninja time). Test: construct_context_test.py Test: new subtests in TestUsesLibs Test: lunch aosp_cf_x86_phone-userdebug && m Bug: 132357300 Change-Id: Icdb03cf00d1e27e4cff3844b89bfaec4de502dd7
776 lines
29 KiB
Go
776 lines
29 KiB
Go
// Copyright 2018 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.
|
|
|
|
// The dexpreopt package converts a global dexpreopt config and a module dexpreopt config into rules to perform
|
|
// dexpreopting.
|
|
//
|
|
// It is used in two places; in the dexpeopt_gen binary for modules defined in Make, and directly linked into Soong.
|
|
//
|
|
// For Make modules it is built into the dexpreopt_gen binary, which is executed as a Make rule using global config and
|
|
// module config specified in JSON files. The binary writes out two shell scripts, only updating them if they have
|
|
// changed. One script takes an APK or JAR as an input and produces a zip file containing any outputs of preopting,
|
|
// in the location they should be on the device. The Make build rules will unzip the zip file into $(PRODUCT_OUT) when
|
|
// installing the APK, which will install the preopt outputs into $(PRODUCT_OUT)/system or $(PRODUCT_OUT)/system_other
|
|
// as necessary. The zip file may be empty if preopting was disabled for any reason.
|
|
//
|
|
// The intermediate shell scripts allow changes to this package or to the global config to regenerate the shell scripts
|
|
// but only require re-executing preopting if the script has changed.
|
|
//
|
|
// For Soong modules this package is linked directly into Soong and run from the java package. It generates the same
|
|
// commands as for make, using athe same global config JSON file used by make, but using a module config structure
|
|
// provided by Soong. The generated commands are then converted into Soong rule and written directly to the ninja file,
|
|
// with no extra shell scripts involved.
|
|
package dexpreopt
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"android/soong/android"
|
|
|
|
"github.com/google/blueprint/pathtools"
|
|
)
|
|
|
|
const SystemPartition = "/system/"
|
|
const SystemOtherPartition = "/system_other/"
|
|
|
|
var DexpreoptRunningInSoong = false
|
|
|
|
// GenerateDexpreoptRule generates a set of commands that will preopt a module based on a GlobalConfig and a
|
|
// ModuleConfig. The produced files and their install locations will be available through rule.Installs().
|
|
func GenerateDexpreoptRule(ctx android.PathContext, globalSoong *GlobalSoongConfig,
|
|
global *GlobalConfig, module *ModuleConfig) (rule *android.RuleBuilder, err error) {
|
|
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
if _, ok := r.(runtime.Error); ok {
|
|
panic(r)
|
|
} else if e, ok := r.(error); ok {
|
|
err = e
|
|
rule = nil
|
|
} else {
|
|
panic(r)
|
|
}
|
|
}
|
|
}()
|
|
|
|
rule = android.NewRuleBuilder()
|
|
|
|
generateProfile := module.ProfileClassListing.Valid() && !global.DisableGenerateProfile
|
|
generateBootProfile := module.ProfileBootListing.Valid() && !global.DisableGenerateProfile
|
|
|
|
var profile android.WritablePath
|
|
if generateProfile {
|
|
profile = profileCommand(ctx, globalSoong, global, module, rule)
|
|
}
|
|
if generateBootProfile {
|
|
bootProfileCommand(ctx, globalSoong, global, module, rule)
|
|
}
|
|
|
|
if !dexpreoptDisabled(ctx, global, module) {
|
|
if clc := genClassLoaderContext(ctx, global, module); clc != nil {
|
|
appImage := (generateProfile || module.ForceCreateAppImage || global.DefaultAppImages) &&
|
|
!module.NoCreateAppImage
|
|
|
|
generateDM := shouldGenerateDM(module, global)
|
|
|
|
for archIdx, _ := range module.Archs {
|
|
dexpreoptCommand(ctx, globalSoong, global, module, rule, archIdx, *clc, profile, appImage, generateDM)
|
|
}
|
|
}
|
|
}
|
|
|
|
return rule, nil
|
|
}
|
|
|
|
func dexpreoptDisabled(ctx android.PathContext, global *GlobalConfig, module *ModuleConfig) bool {
|
|
if contains(global.DisablePreoptModules, module.Name) {
|
|
return true
|
|
}
|
|
|
|
// Don't preopt individual boot jars, they will be preopted together.
|
|
if global.BootJars.ContainsJar(module.Name) {
|
|
return true
|
|
}
|
|
|
|
// Don't preopt system server jars that are updatable.
|
|
if global.UpdatableSystemServerJars.ContainsJar(module.Name) {
|
|
return true
|
|
}
|
|
|
|
// If OnlyPreoptBootImageAndSystemServer=true and module is not in boot class path skip
|
|
// Also preopt system server jars since selinux prevents system server from loading anything from
|
|
// /data. If we don't do this they will need to be extracted which is not favorable for RAM usage
|
|
// or performance. If PreoptExtractedApk is true, we ignore the only preopt boot image options.
|
|
if global.OnlyPreoptBootImageAndSystemServer && !global.BootJars.ContainsJar(module.Name) &&
|
|
!contains(global.SystemServerJars, module.Name) && !module.PreoptExtractedApk {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func profileCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig,
|
|
module *ModuleConfig, rule *android.RuleBuilder) android.WritablePath {
|
|
|
|
profilePath := module.BuildPath.InSameDir(ctx, "profile.prof")
|
|
profileInstalledPath := module.DexLocation + ".prof"
|
|
|
|
if !module.ProfileIsTextListing {
|
|
rule.Command().FlagWithOutput("touch ", profilePath)
|
|
}
|
|
|
|
cmd := rule.Command().
|
|
Text(`ANDROID_LOG_TAGS="*:e"`).
|
|
Tool(globalSoong.Profman)
|
|
|
|
if module.ProfileIsTextListing {
|
|
// The profile is a test listing of classes (used for framework jars).
|
|
// We need to generate the actual binary profile before being able to compile.
|
|
cmd.FlagWithInput("--create-profile-from=", module.ProfileClassListing.Path())
|
|
} else {
|
|
// The profile is binary profile (used for apps). Run it through profman to
|
|
// ensure the profile keys match the apk.
|
|
cmd.
|
|
Flag("--copy-and-update-profile-key").
|
|
FlagWithInput("--profile-file=", module.ProfileClassListing.Path())
|
|
}
|
|
|
|
cmd.
|
|
FlagWithInput("--apk=", module.DexPath).
|
|
Flag("--dex-location="+module.DexLocation).
|
|
FlagWithOutput("--reference-profile-file=", profilePath)
|
|
|
|
if !module.ProfileIsTextListing {
|
|
cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath))
|
|
}
|
|
rule.Install(profilePath, profileInstalledPath)
|
|
|
|
return profilePath
|
|
}
|
|
|
|
func bootProfileCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig,
|
|
module *ModuleConfig, rule *android.RuleBuilder) android.WritablePath {
|
|
|
|
profilePath := module.BuildPath.InSameDir(ctx, "profile.bprof")
|
|
profileInstalledPath := module.DexLocation + ".bprof"
|
|
|
|
if !module.ProfileIsTextListing {
|
|
rule.Command().FlagWithOutput("touch ", profilePath)
|
|
}
|
|
|
|
cmd := rule.Command().
|
|
Text(`ANDROID_LOG_TAGS="*:e"`).
|
|
Tool(globalSoong.Profman)
|
|
|
|
// The profile is a test listing of methods.
|
|
// We need to generate the actual binary profile.
|
|
cmd.FlagWithInput("--create-profile-from=", module.ProfileBootListing.Path())
|
|
|
|
cmd.
|
|
Flag("--generate-boot-profile").
|
|
FlagWithInput("--apk=", module.DexPath).
|
|
Flag("--dex-location="+module.DexLocation).
|
|
FlagWithOutput("--reference-profile-file=", profilePath)
|
|
|
|
if !module.ProfileIsTextListing {
|
|
cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath))
|
|
}
|
|
rule.Install(profilePath, profileInstalledPath)
|
|
|
|
return profilePath
|
|
}
|
|
|
|
type classLoaderContext struct {
|
|
// Library names
|
|
Names []string
|
|
|
|
// The class loader context using paths in the build.
|
|
Host android.Paths
|
|
|
|
// The class loader context using paths as they will be on the device.
|
|
Target []string
|
|
}
|
|
|
|
// A map of class loader contexts for each SDK version.
|
|
// A map entry for "any" version contains libraries that are unconditionally added to class loader
|
|
// context. Map entries for existing versions contains libraries that were in the default classpath
|
|
// until that API version, and should be added to class loader context if and only if the
|
|
// targetSdkVersion in the manifest or APK is less than that API version.
|
|
type classLoaderContextMap map[int]*classLoaderContext
|
|
|
|
const AnySdkVersion int = 9999 // should go last in class loader context
|
|
|
|
func (m classLoaderContextMap) getValue(sdkVer int) *classLoaderContext {
|
|
if _, ok := m[sdkVer]; !ok {
|
|
m[sdkVer] = &classLoaderContext{}
|
|
}
|
|
return m[sdkVer]
|
|
}
|
|
|
|
func (clc *classLoaderContext) addLib(lib string, hostPath android.Path, targetPath string) {
|
|
clc.Names = append(clc.Names, lib)
|
|
clc.Host = append(clc.Host, hostPath)
|
|
clc.Target = append(clc.Target, targetPath)
|
|
}
|
|
|
|
func (m classLoaderContextMap) addLibs(ctx android.PathContext, sdkVer int, module *ModuleConfig, libs ...string) bool {
|
|
clc := m.getValue(sdkVer)
|
|
for _, lib := range libs {
|
|
if p, ok := module.LibraryPaths[lib]; ok && p.Host != nil && p.Device != UnknownInstallLibraryPath {
|
|
clc.addLib(lib, p.Host, p.Device)
|
|
} else {
|
|
if sdkVer == AnySdkVersion {
|
|
// Fail the build if dexpreopt doesn't know paths to one of the <uses-library>
|
|
// dependencies. In the future we may need to relax this and just disable dexpreopt.
|
|
android.ReportPathErrorf(ctx, "dexpreopt cannot find path for <uses-library> '%s'", lib)
|
|
} else {
|
|
// No error for compatibility libraries, as Soong doesn't know if they are needed
|
|
// (this depends on the targetSdkVersion in the manifest).
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (m classLoaderContextMap) usesLibs() []string {
|
|
if clc, ok := m[AnySdkVersion]; ok {
|
|
return clc.Names
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m classLoaderContextMap) addSystemServerLibs(sdkVer int, ctx android.PathContext, module *ModuleConfig, libs ...string) {
|
|
clc := m.getValue(sdkVer)
|
|
for _, lib := range libs {
|
|
clc.addLib(lib, SystemServerDexJarHostPath(ctx, lib), filepath.Join("/system/framework", lib+".jar"))
|
|
}
|
|
}
|
|
|
|
// genClassLoaderContext generates host and target class loader context to be passed to the dex2oat
|
|
// command for the dexpreopted module. There are three possible cases:
|
|
//
|
|
// 1. System server jars. They have a special class loader context that includes other system
|
|
// server jars.
|
|
//
|
|
// 2. Library jars or APKs which have precise list of their <uses-library> libs. Their class loader
|
|
// context includes build and on-device paths to these libs. In some cases it may happen that
|
|
// the path to a <uses-library> is unknown (e.g. the dexpreopted module may depend on stubs
|
|
// library, whose implementation library is missing from the build altogether). In such case
|
|
// dexpreopting with the <uses-library> is impossible, and dexpreopting without it is pointless,
|
|
// as the runtime classpath won't match and the dexpreopted code will be discarded. Therefore in
|
|
// such cases the function returns nil, which disables dexpreopt.
|
|
//
|
|
// 3. All other library jars or APKs for which the exact <uses-library> list is unknown. They use
|
|
// the unsafe &-classpath workaround that means empty class loader context and absence of runtime
|
|
// check that the class loader context provided by the PackageManager agrees with the stored
|
|
// class loader context recorded in the .odex file.
|
|
//
|
|
func genClassLoaderContext(ctx android.PathContext, global *GlobalConfig, module *ModuleConfig) *classLoaderContextMap {
|
|
classLoaderContexts := make(classLoaderContextMap)
|
|
systemServerJars := NonUpdatableSystemServerJars(ctx, global)
|
|
|
|
if jarIndex := android.IndexList(module.Name, systemServerJars); jarIndex >= 0 {
|
|
// System server jars should be dexpreopted together: class loader context of each jar
|
|
// should include all preceding jars on the system server classpath.
|
|
classLoaderContexts.addSystemServerLibs(AnySdkVersion, ctx, module, systemServerJars[:jarIndex]...)
|
|
|
|
} else if module.EnforceUsesLibraries {
|
|
// Unconditional class loader context.
|
|
usesLibs := append(copyOf(module.UsesLibraries), module.OptionalUsesLibraries...)
|
|
if !classLoaderContexts.addLibs(ctx, AnySdkVersion, module, usesLibs...) {
|
|
return nil
|
|
}
|
|
|
|
// Conditional class loader context for API version < 28.
|
|
const httpLegacy = "org.apache.http.legacy"
|
|
if !classLoaderContexts.addLibs(ctx, 28, module, httpLegacy) {
|
|
return nil
|
|
}
|
|
|
|
// Conditional class loader context for API version < 29.
|
|
usesLibs29 := []string{
|
|
"android.hidl.base-V1.0-java",
|
|
"android.hidl.manager-V1.0-java",
|
|
}
|
|
if !classLoaderContexts.addLibs(ctx, 29, module, usesLibs29...) {
|
|
return nil
|
|
}
|
|
|
|
// Conditional class loader context for API version < 30.
|
|
if !classLoaderContexts.addLibs(ctx, 30, module, OptionalCompatUsesLibs30...) {
|
|
return nil
|
|
}
|
|
|
|
} else {
|
|
// Pass special class loader context to skip the classpath and collision check.
|
|
// This will get removed once LOCAL_USES_LIBRARIES is enforced.
|
|
// Right now LOCAL_USES_LIBRARIES is opt in, for the case where it's not specified we still default
|
|
// to the &.
|
|
}
|
|
|
|
fixConditionalClassLoaderContext(classLoaderContexts)
|
|
|
|
return &classLoaderContexts
|
|
}
|
|
|
|
// Find build and install paths to "android.hidl.base". The library must be present in conditional
|
|
// class loader context for SDK version 29, because it's one of the compatibility libraries.
|
|
func findHidlBasePaths(ctx android.PathContext, clcMap classLoaderContextMap) (android.Path, string) {
|
|
var hostPath android.Path
|
|
targetPath := UnknownInstallLibraryPath
|
|
|
|
if clc, ok := clcMap[29]; ok {
|
|
for i, lib := range clc.Names {
|
|
if lib == AndroidHidlBase {
|
|
hostPath = clc.Host[i]
|
|
targetPath = clc.Target[i]
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fail if the library paths were not found. This may happen if the function is called at the
|
|
// wrong time (either before the compatibility libraries were added to context, or after they
|
|
// have been removed for some reason).
|
|
if hostPath == nil {
|
|
android.ReportPathErrorf(ctx, "dexpreopt cannot find build path to '%s'", AndroidHidlBase)
|
|
} else if targetPath == UnknownInstallLibraryPath {
|
|
android.ReportPathErrorf(ctx, "dexpreopt cannot find install path to '%s'", AndroidHidlBase)
|
|
}
|
|
|
|
return hostPath, targetPath
|
|
}
|
|
|
|
// Now that the full unconditional context is known, reconstruct conditional context.
|
|
// Apply filters for individual libraries, mirroring what the PackageManager does when it
|
|
// constructs class loader context on device.
|
|
//
|
|
// TODO(b/132357300):
|
|
// - move handling of android.hidl.manager -> android.hidl.base dependency here
|
|
// - remove android.hidl.manager and android.hidl.base unless the app is a system app.
|
|
//
|
|
func fixConditionalClassLoaderContext(clcMap classLoaderContextMap) {
|
|
usesLibs := clcMap.usesLibs()
|
|
|
|
for sdkVer, clc := range clcMap {
|
|
if sdkVer == AnySdkVersion {
|
|
continue
|
|
}
|
|
clcMap[sdkVer] = &classLoaderContext{}
|
|
for i, lib := range clc.Names {
|
|
if android.InList(lib, usesLibs) {
|
|
// skip compatibility libraries that are already included in unconditional context
|
|
} else if lib == AndroidTestMock && !android.InList("android.test.runner", usesLibs) {
|
|
// android.test.mock is only needed as a compatibility library (in conditional class
|
|
// loader context) if android.test.runner is used, otherwise skip it
|
|
} else {
|
|
clcMap[sdkVer].addLib(lib, clc.Host[i], clc.Target[i])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Return the class loader context as a string and a slice of build paths for all dependencies.
|
|
func computeClassLoaderContext(ctx android.PathContext, clcMap classLoaderContextMap) (clcStr string, paths android.Paths) {
|
|
hidlBaseHostPath, hidlBaseTargetPath := findHidlBasePaths(ctx, clcMap)
|
|
|
|
for _, ver := range android.SortedIntKeys(clcMap) {
|
|
clc := clcMap.getValue(ver)
|
|
|
|
clcLen := len(clc.Names)
|
|
if clcLen != len(clc.Host) || clcLen != len(clc.Target) {
|
|
android.ReportPathErrorf(ctx, "ill-formed class loader context")
|
|
}
|
|
|
|
var hostClc, targetClc []string
|
|
var hostPaths android.Paths
|
|
|
|
for i := 0; i < clcLen; i++ {
|
|
hostStr := "PCL[" + clc.Host[i].String() + "]"
|
|
targetStr := "PCL[" + clc.Target[i] + "]"
|
|
|
|
// Add dependency of android.hidl.manager on android.hidl.base (it is not tracked as
|
|
// a regular dependency by the build system, so it needs special handling).
|
|
if clc.Names[i] == AndroidHidlManager {
|
|
hostStr += "{PCL[" + hidlBaseHostPath.String() + "]}"
|
|
targetStr += "{PCL[" + hidlBaseTargetPath + "]}"
|
|
hostPaths = append(hostPaths, hidlBaseHostPath)
|
|
}
|
|
|
|
hostClc = append(hostClc, hostStr)
|
|
targetClc = append(targetClc, targetStr)
|
|
hostPaths = append(hostPaths, clc.Host[i])
|
|
}
|
|
|
|
if hostPaths != nil {
|
|
sdkVerStr := fmt.Sprintf("%d", ver)
|
|
if ver == AnySdkVersion {
|
|
sdkVerStr = "any" // a special keyword that means any SDK version
|
|
}
|
|
clcStr += fmt.Sprintf(" --host-context-for-sdk %s %s", sdkVerStr, strings.Join(hostClc, "#"))
|
|
clcStr += fmt.Sprintf(" --target-context-for-sdk %s %s", sdkVerStr, strings.Join(targetClc, "#"))
|
|
paths = append(paths, hostPaths...)
|
|
}
|
|
}
|
|
|
|
return clcStr, paths
|
|
}
|
|
|
|
func dexpreoptCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig,
|
|
module *ModuleConfig, rule *android.RuleBuilder, archIdx int, classLoaderContexts classLoaderContextMap,
|
|
profile android.WritablePath, appImage bool, generateDM bool) {
|
|
|
|
arch := module.Archs[archIdx]
|
|
|
|
// HACK: make soname in Soong-generated .odex files match Make.
|
|
base := filepath.Base(module.DexLocation)
|
|
if filepath.Ext(base) == ".jar" {
|
|
base = "javalib.jar"
|
|
} else if filepath.Ext(base) == ".apk" {
|
|
base = "package.apk"
|
|
}
|
|
|
|
toOdexPath := func(path string) string {
|
|
return filepath.Join(
|
|
filepath.Dir(path),
|
|
"oat",
|
|
arch.String(),
|
|
pathtools.ReplaceExtension(filepath.Base(path), "odex"))
|
|
}
|
|
|
|
odexPath := module.BuildPath.InSameDir(ctx, "oat", arch.String(), pathtools.ReplaceExtension(base, "odex"))
|
|
odexInstallPath := toOdexPath(module.DexLocation)
|
|
if odexOnSystemOther(module, global) {
|
|
odexInstallPath = filepath.Join(SystemOtherPartition, odexInstallPath)
|
|
}
|
|
|
|
vdexPath := odexPath.ReplaceExtension(ctx, "vdex")
|
|
vdexInstallPath := pathtools.ReplaceExtension(odexInstallPath, "vdex")
|
|
|
|
invocationPath := odexPath.ReplaceExtension(ctx, "invocation")
|
|
|
|
systemServerJars := NonUpdatableSystemServerJars(ctx, global)
|
|
|
|
rule.Command().FlagWithArg("mkdir -p ", filepath.Dir(odexPath.String()))
|
|
rule.Command().FlagWithOutput("rm -f ", odexPath)
|
|
|
|
if jarIndex := android.IndexList(module.Name, systemServerJars); jarIndex >= 0 {
|
|
// Copy the system server jar to a predefined location where dex2oat will find it.
|
|
dexPathHost := SystemServerDexJarHostPath(ctx, module.Name)
|
|
rule.Command().Text("mkdir -p").Flag(filepath.Dir(dexPathHost.String()))
|
|
rule.Command().Text("cp -f").Input(module.DexPath).Output(dexPathHost)
|
|
|
|
checkSystemServerOrder(ctx, jarIndex)
|
|
|
|
clc := classLoaderContexts[AnySdkVersion]
|
|
rule.Command().
|
|
Text("class_loader_context_arg=--class-loader-context=PCL[" + strings.Join(clc.Host.Strings(), ":") + "]").
|
|
Implicits(clc.Host).
|
|
Text("stored_class_loader_context_arg=--stored-class-loader-context=PCL[" + strings.Join(clc.Target, ":") + "]")
|
|
} else if module.EnforceUsesLibraries {
|
|
// Generate command that saves target SDK version in a shell variable.
|
|
if module.ManifestPath != nil {
|
|
rule.Command().Text(`target_sdk_version="$(`).
|
|
Tool(globalSoong.ManifestCheck).
|
|
Flag("--extract-target-sdk-version").
|
|
Input(module.ManifestPath).
|
|
Text(`)"`)
|
|
} else {
|
|
// No manifest to extract targetSdkVersion from, hope that DexJar is an APK
|
|
rule.Command().Text(`target_sdk_version="$(`).
|
|
Tool(globalSoong.Aapt).
|
|
Flag("dump badging").
|
|
Input(module.DexPath).
|
|
Text(`| grep "targetSdkVersion" | sed -n "s/targetSdkVersion:'\(.*\)'/\1/p"`).
|
|
Text(`)"`)
|
|
}
|
|
|
|
// Generate command that saves host and target class loader context in shell variables.
|
|
clc, paths := computeClassLoaderContext(ctx, classLoaderContexts)
|
|
cmd := rule.Command().
|
|
Text(`eval "$(`).Tool(globalSoong.ConstructContext).
|
|
Text(` --target-sdk-version ${target_sdk_version}`).
|
|
Text(clc).Implicits(paths)
|
|
cmd.Text(`)"`)
|
|
} else {
|
|
// Pass special class loader context to skip the classpath and collision check.
|
|
// This will get removed once LOCAL_USES_LIBRARIES is enforced.
|
|
// Right now LOCAL_USES_LIBRARIES is opt in, for the case where it's not specified we still default
|
|
// to the &.
|
|
rule.Command().
|
|
Text(`class_loader_context_arg=--class-loader-context=\&`).
|
|
Text(`stored_class_loader_context_arg=""`)
|
|
}
|
|
|
|
// Devices that do not have a product partition use a symlink from /product to /system/product.
|
|
// Because on-device dexopt will see dex locations starting with /product, we change the paths
|
|
// to mimic this behavior.
|
|
dexLocationArg := module.DexLocation
|
|
if strings.HasPrefix(dexLocationArg, "/system/product/") {
|
|
dexLocationArg = strings.TrimPrefix(dexLocationArg, "/system")
|
|
}
|
|
|
|
cmd := rule.Command().
|
|
Text(`ANDROID_LOG_TAGS="*:e"`).
|
|
Tool(globalSoong.Dex2oat).
|
|
Flag("--avoid-storing-invocation").
|
|
FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
|
|
Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatXms).
|
|
Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatXmx).
|
|
Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", module.PreoptBootClassPathDexFiles, ":").
|
|
Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", module.PreoptBootClassPathDexLocations, ":").
|
|
Flag("${class_loader_context_arg}").
|
|
Flag("${stored_class_loader_context_arg}").
|
|
FlagWithArg("--boot-image=", strings.Join(module.DexPreoptImageLocations, ":")).Implicits(module.DexPreoptImagesDeps[archIdx].Paths()).
|
|
FlagWithInput("--dex-file=", module.DexPath).
|
|
FlagWithArg("--dex-location=", dexLocationArg).
|
|
FlagWithOutput("--oat-file=", odexPath).ImplicitOutput(vdexPath).
|
|
// Pass an empty directory, dex2oat shouldn't be reading arbitrary files
|
|
FlagWithArg("--android-root=", global.EmptyDirectory).
|
|
FlagWithArg("--instruction-set=", arch.String()).
|
|
FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]).
|
|
FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]).
|
|
Flag("--no-generate-debug-info").
|
|
Flag("--generate-build-id").
|
|
Flag("--abort-on-hard-verifier-error").
|
|
Flag("--force-determinism").
|
|
FlagWithArg("--no-inline-from=", "core-oj.jar")
|
|
|
|
var preoptFlags []string
|
|
if len(module.PreoptFlags) > 0 {
|
|
preoptFlags = module.PreoptFlags
|
|
} else if len(global.PreoptFlags) > 0 {
|
|
preoptFlags = global.PreoptFlags
|
|
}
|
|
|
|
if len(preoptFlags) > 0 {
|
|
cmd.Text(strings.Join(preoptFlags, " "))
|
|
}
|
|
|
|
if module.UncompressedDex {
|
|
cmd.FlagWithArg("--copy-dex-files=", "false")
|
|
}
|
|
|
|
if !android.PrefixInList(preoptFlags, "--compiler-filter=") {
|
|
var compilerFilter string
|
|
if contains(global.SystemServerJars, module.Name) {
|
|
// Jars of system server, use the product option if it is set, speed otherwise.
|
|
if global.SystemServerCompilerFilter != "" {
|
|
compilerFilter = global.SystemServerCompilerFilter
|
|
} else {
|
|
compilerFilter = "speed"
|
|
}
|
|
} else if contains(global.SpeedApps, module.Name) || contains(global.SystemServerApps, module.Name) {
|
|
// Apps loaded into system server, and apps the product default to being compiled with the
|
|
// 'speed' compiler filter.
|
|
compilerFilter = "speed"
|
|
} else if profile != nil {
|
|
// For non system server jars, use speed-profile when we have a profile.
|
|
compilerFilter = "speed-profile"
|
|
} else if global.DefaultCompilerFilter != "" {
|
|
compilerFilter = global.DefaultCompilerFilter
|
|
} else {
|
|
compilerFilter = "quicken"
|
|
}
|
|
cmd.FlagWithArg("--compiler-filter=", compilerFilter)
|
|
}
|
|
|
|
if generateDM {
|
|
cmd.FlagWithArg("--copy-dex-files=", "false")
|
|
dmPath := module.BuildPath.InSameDir(ctx, "generated.dm")
|
|
dmInstalledPath := pathtools.ReplaceExtension(module.DexLocation, "dm")
|
|
tmpPath := module.BuildPath.InSameDir(ctx, "primary.vdex")
|
|
rule.Command().Text("cp -f").Input(vdexPath).Output(tmpPath)
|
|
rule.Command().Tool(globalSoong.SoongZip).
|
|
FlagWithArg("-L", "9").
|
|
FlagWithOutput("-o", dmPath).
|
|
Flag("-j").
|
|
Input(tmpPath)
|
|
rule.Install(dmPath, dmInstalledPath)
|
|
}
|
|
|
|
// By default, emit debug info.
|
|
debugInfo := true
|
|
if global.NoDebugInfo {
|
|
// If the global setting suppresses mini-debug-info, disable it.
|
|
debugInfo = false
|
|
}
|
|
|
|
// PRODUCT_SYSTEM_SERVER_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO.
|
|
// PRODUCT_OTHER_JAVA_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO.
|
|
if contains(global.SystemServerJars, module.Name) {
|
|
if global.AlwaysSystemServerDebugInfo {
|
|
debugInfo = true
|
|
} else if global.NeverSystemServerDebugInfo {
|
|
debugInfo = false
|
|
}
|
|
} else {
|
|
if global.AlwaysOtherDebugInfo {
|
|
debugInfo = true
|
|
} else if global.NeverOtherDebugInfo {
|
|
debugInfo = false
|
|
}
|
|
}
|
|
|
|
// Never enable on eng.
|
|
if global.IsEng {
|
|
debugInfo = false
|
|
}
|
|
|
|
if debugInfo {
|
|
cmd.Flag("--generate-mini-debug-info")
|
|
} else {
|
|
cmd.Flag("--no-generate-mini-debug-info")
|
|
}
|
|
|
|
// Set the compiler reason to 'prebuilt' to identify the oat files produced
|
|
// during the build, as opposed to compiled on the device.
|
|
cmd.FlagWithArg("--compilation-reason=", "prebuilt")
|
|
|
|
if appImage {
|
|
appImagePath := odexPath.ReplaceExtension(ctx, "art")
|
|
appImageInstallPath := pathtools.ReplaceExtension(odexInstallPath, "art")
|
|
cmd.FlagWithOutput("--app-image-file=", appImagePath).
|
|
FlagWithArg("--image-format=", "lz4")
|
|
if !global.DontResolveStartupStrings {
|
|
cmd.FlagWithArg("--resolve-startup-const-strings=", "true")
|
|
}
|
|
rule.Install(appImagePath, appImageInstallPath)
|
|
}
|
|
|
|
if profile != nil {
|
|
cmd.FlagWithInput("--profile-file=", profile)
|
|
}
|
|
|
|
rule.Install(odexPath, odexInstallPath)
|
|
rule.Install(vdexPath, vdexInstallPath)
|
|
}
|
|
|
|
func shouldGenerateDM(module *ModuleConfig, global *GlobalConfig) bool {
|
|
// Generating DM files only makes sense for verify, avoid doing for non verify compiler filter APKs.
|
|
// No reason to use a dm file if the dex is already uncompressed.
|
|
return global.GenerateDMFiles && !module.UncompressedDex &&
|
|
contains(module.PreoptFlags, "--compiler-filter=verify")
|
|
}
|
|
|
|
func OdexOnSystemOtherByName(name string, dexLocation string, global *GlobalConfig) bool {
|
|
if !global.HasSystemOther {
|
|
return false
|
|
}
|
|
|
|
if global.SanitizeLite {
|
|
return false
|
|
}
|
|
|
|
if contains(global.SpeedApps, name) || contains(global.SystemServerApps, name) {
|
|
return false
|
|
}
|
|
|
|
for _, f := range global.PatternsOnSystemOther {
|
|
if makefileMatch(filepath.Join(SystemPartition, f), dexLocation) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func odexOnSystemOther(module *ModuleConfig, global *GlobalConfig) bool {
|
|
return OdexOnSystemOtherByName(module.Name, module.DexLocation, global)
|
|
}
|
|
|
|
// PathToLocation converts .../system/framework/arm64/boot.art to .../system/framework/boot.art
|
|
func PathToLocation(path android.Path, arch android.ArchType) string {
|
|
pathArch := filepath.Base(filepath.Dir(path.String()))
|
|
if pathArch != arch.String() {
|
|
panic(fmt.Errorf("last directory in %q must be %q", path, arch.String()))
|
|
}
|
|
return filepath.Join(filepath.Dir(filepath.Dir(path.String())), filepath.Base(path.String()))
|
|
}
|
|
|
|
func makefileMatch(pattern, s string) bool {
|
|
percent := strings.IndexByte(pattern, '%')
|
|
switch percent {
|
|
case -1:
|
|
return pattern == s
|
|
case len(pattern) - 1:
|
|
return strings.HasPrefix(s, pattern[:len(pattern)-1])
|
|
default:
|
|
panic(fmt.Errorf("unsupported makefile pattern %q", pattern))
|
|
}
|
|
}
|
|
|
|
var nonUpdatableSystemServerJarsKey = android.NewOnceKey("nonUpdatableSystemServerJars")
|
|
|
|
// TODO: eliminate the superficial global config parameter by moving global config definition
|
|
// from java subpackage to dexpreopt.
|
|
func NonUpdatableSystemServerJars(ctx android.PathContext, global *GlobalConfig) []string {
|
|
return ctx.Config().Once(nonUpdatableSystemServerJarsKey, func() interface{} {
|
|
return android.RemoveListFromList(global.SystemServerJars, global.UpdatableSystemServerJars.CopyOfJars())
|
|
}).([]string)
|
|
}
|
|
|
|
// A predefined location for the system server dex jars. This is needed in order to generate
|
|
// class loader context for dex2oat, as the path to the jar in the Soong module may be unknown
|
|
// at that time (Soong processes the jars in dependency order, which may be different from the
|
|
// the system server classpath order).
|
|
func SystemServerDexJarHostPath(ctx android.PathContext, jar string) android.OutputPath {
|
|
if DexpreoptRunningInSoong {
|
|
// Soong module, just use the default output directory $OUT/soong.
|
|
return android.PathForOutput(ctx, "system_server_dexjars", jar+".jar")
|
|
} else {
|
|
// Make module, default output directory is $OUT (passed via the "null config" created
|
|
// by dexpreopt_gen). Append Soong subdirectory to match Soong module paths.
|
|
return android.PathForOutput(ctx, "soong", "system_server_dexjars", jar+".jar")
|
|
}
|
|
}
|
|
|
|
// Check the order of jars on the system server classpath and give a warning/error if a jar precedes
|
|
// one of its dependencies. This is not an error, but a missed optimization, as dexpreopt won't
|
|
// have the dependency jar in the class loader context, and it won't be able to resolve any
|
|
// references to its classes and methods.
|
|
func checkSystemServerOrder(ctx android.PathContext, jarIndex int) {
|
|
mctx, isModule := ctx.(android.ModuleContext)
|
|
if isModule {
|
|
config := GetGlobalConfig(ctx)
|
|
jars := NonUpdatableSystemServerJars(ctx, config)
|
|
mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
|
|
depIndex := android.IndexList(dep.Name(), jars)
|
|
if jarIndex < depIndex && !config.BrokenSuboptimalOrderOfSystemServerJars {
|
|
jar := jars[jarIndex]
|
|
dep := jars[depIndex]
|
|
mctx.ModuleErrorf("non-optimal order of jars on the system server classpath:"+
|
|
" '%s' precedes its dependency '%s', so dexpreopt is unable to resolve any"+
|
|
" references from '%s' to '%s'.\n", jar, dep, jar, dep)
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
}
|
|
|
|
func contains(l []string, s string) bool {
|
|
for _, e := range l {
|
|
if e == s {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
var copyOf = android.CopyOf
|