2018-11-12 19:13:39 +01:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
package java
|
|
|
|
|
|
|
|
import (
|
|
|
|
"android/soong/android"
|
|
|
|
"android/soong/dexpreopt"
|
|
|
|
)
|
|
|
|
|
2020-01-31 18:10:36 +01:00
|
|
|
type dexpreopterInterface interface {
|
|
|
|
IsInstallable() bool // Structs that embed dexpreopter must implement this.
|
|
|
|
dexpreoptDisabled(ctx android.BaseModuleContext) bool
|
|
|
|
}
|
|
|
|
|
2018-11-12 19:13:39 +01:00
|
|
|
type dexpreopter struct {
|
|
|
|
dexpreoptProperties DexpreoptProperties
|
|
|
|
|
2019-10-02 07:05:35 +02:00
|
|
|
installPath android.InstallPath
|
2019-04-15 18:48:31 +02:00
|
|
|
uncompressedDex bool
|
|
|
|
isSDKLibrary bool
|
2021-01-14 18:52:43 +01:00
|
|
|
isApp bool
|
2019-04-15 18:48:31 +02:00
|
|
|
isTest bool
|
|
|
|
isPresignedPrebuilt bool
|
2018-11-12 19:13:39 +01:00
|
|
|
|
2020-11-03 16:15:46 +01:00
|
|
|
manifestFile android.Path
|
Add non-fatal mode for verify_uses_libraries check.
The new mode is enabled with environment variable
RELAX_USES_LIBRARY_CHECK. If the variable is set to true, then a
verify_uses_libraries check failure does not fail the build, instead it
sets a special compiler filter "extract" for dexpreopt, which means that
the DEX file will be extracted, but it won't be compiled to native code.
Class loader context will be set to empty in this case (not &, as it is
going to be deprecated soon).
If the variable RELAX_USES_LIBRARY_CHECK is unset or set to something
other than "true", then the old behaviour of the verify_uses_libraries
check is preserved.
The intended use case for this flag is to have a smoother migration path
for the Java modules that need to add <uses-library> information in
the build files. The flag allows to quickly silence build errors. This
flag should be used with caution and only as a temporary measure, as it
masks real errors and affects performance.
verify_uses_libraries check is reworked so that it writes the error
message to a status file (which is used instead of the former timestamp
file). Currently the stored error message is not used, but it may be
used later to produce a warning. Dexpreopt command checks if the status
file exists and is nonempty; if that is the case, then compiler filter
is set to "extract".
Bug: 132357300
Test: Manually add some mismatch between the libraries in the Android.bp
and Android.mk files for dexpreopted apps, build with
RELAX_USES_LIBRARY_CHECK=true and obsserve that the build doesn't
fail and they are compiled with compiler-filter "extract".
Unset RELAX_USES_LIBRARY_CHECK and observe that the build fails.
Change-Id: Ibb5d993a25b1df1d2e70b7d5aafc6997f9d64e67
2021-02-17 17:23:28 +01:00
|
|
|
statusFile android.WritablePath
|
2020-11-03 16:15:46 +01:00
|
|
|
enforceUsesLibs bool
|
|
|
|
classLoaderContexts dexpreopt.ClassLoaderContextMap
|
2019-05-16 21:28:22 +02:00
|
|
|
|
2019-02-11 23:11:09 +01:00
|
|
|
builtInstalled string
|
2021-01-14 18:52:43 +01:00
|
|
|
|
|
|
|
// A path to a dexpreopt.config file generated by Soong for libraries that may be used as a
|
|
|
|
// <uses-library> by Make modules. The path is passed to Make via LOCAL_SOONG_DEXPREOPT_CONFIG
|
|
|
|
// variable. If the path is nil, no config is generated (which is the case for apps and tests).
|
|
|
|
configPath android.WritablePath
|
2018-11-12 19:13:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type DexpreoptProperties struct {
|
|
|
|
Dex_preopt struct {
|
2019-10-18 15:51:38 +02:00
|
|
|
// If false, prevent dexpreopting. Defaults to true.
|
2018-11-12 19:13:39 +01:00
|
|
|
Enabled *bool
|
|
|
|
|
|
|
|
// If true, generate an app image (.art file) for this module.
|
|
|
|
App_image *bool
|
|
|
|
|
|
|
|
// If true, use a checked-in profile to guide optimization. Defaults to false unless
|
|
|
|
// a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
|
|
|
|
// that matches the name of this module, in which case it is defaulted to true.
|
|
|
|
Profile_guided *bool
|
|
|
|
|
|
|
|
// If set, provides the path to profile relative to the Android.bp file. If not set,
|
|
|
|
// defaults to searching for a file that matches the name of this module in the default
|
|
|
|
// profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
|
2019-04-26 19:52:32 +02:00
|
|
|
Profile *string `android:"path"`
|
2018-11-12 19:13:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 13:15:20 +02:00
|
|
|
func init() {
|
|
|
|
dexpreopt.DexpreoptRunningInSoong = true
|
|
|
|
}
|
|
|
|
|
2020-01-31 18:10:36 +01:00
|
|
|
func (d *dexpreopter) dexpreoptDisabled(ctx android.BaseModuleContext) bool {
|
2020-01-20 19:12:23 +01:00
|
|
|
global := dexpreopt.GetGlobalConfig(ctx)
|
2019-02-15 19:39:37 +01:00
|
|
|
|
|
|
|
if global.DisablePreopt {
|
2019-02-11 23:21:24 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-02-15 19:39:37 +01:00
|
|
|
if inList(ctx.ModuleName(), global.DisablePreoptModules) {
|
2018-11-12 19:13:39 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.isTest {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-01-31 18:10:36 +01:00
|
|
|
if !ctx.Module().(dexpreopterInterface).IsInstallable() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Host() {
|
2019-01-06 07:13:05 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-01-09 12:00:27 +01:00
|
|
|
// Don't preopt APEX variant module
|
2020-09-16 03:30:11 +02:00
|
|
|
if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
|
2020-01-09 12:00:27 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-11-12 19:13:39 +01:00
|
|
|
// TODO: contains no java code
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-31 18:10:36 +01:00
|
|
|
func dexpreoptToolDepsMutator(ctx android.BottomUpMutatorContext) {
|
|
|
|
if d, ok := ctx.Module().(dexpreopterInterface); !ok || d.dexpreoptDisabled(ctx) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dexpreopt.RegisterToolDeps(ctx)
|
|
|
|
}
|
|
|
|
|
2019-10-02 07:05:35 +02:00
|
|
|
func odexOnSystemOther(ctx android.ModuleContext, installPath android.InstallPath) bool {
|
2020-01-20 19:12:23 +01:00
|
|
|
return dexpreopt.OdexOnSystemOtherByName(ctx.ModuleName(), android.InstallPathToOnDevicePath(ctx, installPath), dexpreopt.GetGlobalConfig(ctx))
|
2019-02-12 14:12:16 +01:00
|
|
|
}
|
|
|
|
|
2021-02-02 14:38:13 +01:00
|
|
|
func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.WritablePath) {
|
2020-01-31 18:10:36 +01:00
|
|
|
// TODO(b/148690468): The check on d.installPath is to bail out in cases where
|
|
|
|
// the dexpreopter struct hasn't been fully initialized before we're called,
|
|
|
|
// e.g. in aar.go. This keeps the behaviour that dexpreopting is effectively
|
|
|
|
// disabled, even if installable is true.
|
2021-01-14 18:52:43 +01:00
|
|
|
if d.installPath.Base() == "." {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
|
|
|
|
|
|
|
|
providesUsesLib := ctx.ModuleName()
|
|
|
|
if ulib, ok := ctx.Module().(ProvidesUsesLib); ok {
|
|
|
|
name := ulib.ProvidesUsesLib()
|
|
|
|
if name != nil {
|
|
|
|
providesUsesLib = *name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !d.isApp && !d.isTest {
|
|
|
|
// Slim dexpreopt config is serialized to dexpreopt.config files and used by
|
|
|
|
// dex_preopt_config_merger.py to get information about <uses-library> dependencies.
|
|
|
|
// Note that it might be needed even if dexpreopt is disabled for this module.
|
|
|
|
slimDexpreoptConfig := &dexpreopt.ModuleConfig{
|
|
|
|
Name: ctx.ModuleName(),
|
|
|
|
DexLocation: dexLocation,
|
|
|
|
EnforceUsesLibraries: d.enforceUsesLibs,
|
|
|
|
ProvidesUsesLibrary: providesUsesLib,
|
|
|
|
ClassLoaderContexts: d.classLoaderContexts,
|
|
|
|
// The rest of the fields are not needed.
|
|
|
|
}
|
|
|
|
d.configPath = android.PathForModuleOut(ctx, "dexpreopt", "dexpreopt.config")
|
|
|
|
dexpreopt.WriteSlimModuleConfigForMake(ctx, slimDexpreoptConfig, d.configPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.dexpreoptDisabled(ctx) {
|
2020-12-17 18:43:28 +01:00
|
|
|
return
|
2019-02-12 14:12:16 +01:00
|
|
|
}
|
|
|
|
|
2020-01-10 21:32:59 +01:00
|
|
|
globalSoong := dexpreopt.GetGlobalSoongConfig(ctx)
|
2020-01-20 19:12:23 +01:00
|
|
|
global := dexpreopt.GetGlobalConfig(ctx)
|
2019-02-16 08:06:46 +01:00
|
|
|
bootImage := defaultBootImageConfig(ctx)
|
2020-02-06 16:14:29 +01:00
|
|
|
dexFiles := bootImage.dexPathsDeps.Paths()
|
2020-03-30 18:24:13 +02:00
|
|
|
// The dex locations for all Android variants are identical.
|
|
|
|
dexLocations := bootImage.getAnyAndroidVariant().dexLocationsDeps
|
2020-02-06 16:14:29 +01:00
|
|
|
if global.UseArtImage {
|
|
|
|
bootImage = artBootImageConfig(ctx)
|
|
|
|
}
|
2018-11-12 19:13:39 +01:00
|
|
|
|
2020-02-18 21:43:06 +01:00
|
|
|
targets := ctx.MultiTargets()
|
|
|
|
if len(targets) == 0 {
|
2018-11-12 19:13:39 +01:00
|
|
|
// assume this is a java library, dexpreopt for all arches for now
|
|
|
|
for _, target := range ctx.Config().Targets[android.Android] {
|
2019-03-26 12:39:31 +01:00
|
|
|
if target.NativeBridge == android.NativeBridgeDisabled {
|
2020-02-18 21:43:06 +01:00
|
|
|
targets = append(targets, target)
|
2019-03-26 12:39:31 +01:00
|
|
|
}
|
2018-11-12 19:13:39 +01:00
|
|
|
}
|
2019-02-16 08:06:46 +01:00
|
|
|
if inList(ctx.ModuleName(), global.SystemServerJars) && !d.isSDKLibrary {
|
2018-11-12 19:13:39 +01:00
|
|
|
// If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
|
2020-02-18 21:43:06 +01:00
|
|
|
targets = targets[:1]
|
2018-11-12 19:13:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 21:43:06 +01:00
|
|
|
var archs []android.ArchType
|
2019-02-15 19:39:37 +01:00
|
|
|
var images android.Paths
|
2019-11-08 11:54:21 +01:00
|
|
|
var imagesDeps []android.OutputPaths
|
2020-02-18 21:43:06 +01:00
|
|
|
for _, target := range targets {
|
|
|
|
archs = append(archs, target.Arch.ArchType)
|
|
|
|
variant := bootImage.getVariant(target)
|
|
|
|
images = append(images, variant.images)
|
|
|
|
imagesDeps = append(imagesDeps, variant.imagesDeps)
|
2019-02-09 06:37:00 +01:00
|
|
|
}
|
2020-03-30 18:24:13 +02:00
|
|
|
// The image locations for all Android variants are identical.
|
|
|
|
imageLocations := bootImage.getAnyAndroidVariant().imageLocations()
|
2019-02-09 06:37:00 +01:00
|
|
|
|
2018-11-12 19:13:39 +01:00
|
|
|
var profileClassListing android.OptionalPath
|
2019-07-24 14:19:29 +02:00
|
|
|
var profileBootListing android.OptionalPath
|
2018-11-12 19:13:39 +01:00
|
|
|
profileIsTextListing := false
|
|
|
|
if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
|
|
|
|
// If dex_preopt.profile_guided is not set, default it based on the existence of the
|
|
|
|
// dexprepot.profile option or the profile class listing.
|
|
|
|
if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
|
|
|
|
profileClassListing = android.OptionalPathForPath(
|
|
|
|
android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
|
2019-07-24 14:19:29 +02:00
|
|
|
profileBootListing = android.ExistentPathForSource(ctx,
|
|
|
|
ctx.ModuleDir(), String(d.dexpreoptProperties.Dex_preopt.Profile)+"-boot")
|
2018-11-12 19:13:39 +01:00
|
|
|
profileIsTextListing = true
|
2020-06-25 01:33:31 +02:00
|
|
|
} else if global.ProfileDir != "" {
|
2018-11-12 19:13:39 +01:00
|
|
|
profileClassListing = android.ExistentPathForSource(ctx,
|
2019-02-16 08:06:46 +01:00
|
|
|
global.ProfileDir, ctx.ModuleName()+".prof")
|
2018-11-12 19:13:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 18:52:43 +01:00
|
|
|
// Full dexpreopt config, used to create dexpreopt build rules.
|
2020-01-31 18:44:54 +01:00
|
|
|
dexpreoptConfig := &dexpreopt.ModuleConfig{
|
2019-01-29 22:00:33 +01:00
|
|
|
Name: ctx.ModuleName(),
|
|
|
|
DexLocation: dexLocation,
|
2021-02-04 11:04:39 +01:00
|
|
|
BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath,
|
2019-02-15 19:39:37 +01:00
|
|
|
DexPath: dexJarFile,
|
2019-05-16 21:28:22 +02:00
|
|
|
ManifestPath: d.manifestFile,
|
2019-01-29 22:00:33 +01:00
|
|
|
UncompressedDex: d.uncompressedDex,
|
|
|
|
HasApkLibraries: false,
|
|
|
|
PreoptFlags: nil,
|
2018-11-12 19:13:39 +01:00
|
|
|
|
2019-02-15 19:39:37 +01:00
|
|
|
ProfileClassListing: profileClassListing,
|
2018-11-12 19:13:39 +01:00
|
|
|
ProfileIsTextListing: profileIsTextListing,
|
2019-07-24 14:19:29 +02:00
|
|
|
ProfileBootListing: profileBootListing,
|
2018-11-12 19:13:39 +01:00
|
|
|
|
Add non-fatal mode for verify_uses_libraries check.
The new mode is enabled with environment variable
RELAX_USES_LIBRARY_CHECK. If the variable is set to true, then a
verify_uses_libraries check failure does not fail the build, instead it
sets a special compiler filter "extract" for dexpreopt, which means that
the DEX file will be extracted, but it won't be compiled to native code.
Class loader context will be set to empty in this case (not &, as it is
going to be deprecated soon).
If the variable RELAX_USES_LIBRARY_CHECK is unset or set to something
other than "true", then the old behaviour of the verify_uses_libraries
check is preserved.
The intended use case for this flag is to have a smoother migration path
for the Java modules that need to add <uses-library> information in
the build files. The flag allows to quickly silence build errors. This
flag should be used with caution and only as a temporary measure, as it
masks real errors and affects performance.
verify_uses_libraries check is reworked so that it writes the error
message to a status file (which is used instead of the former timestamp
file). Currently the stored error message is not used, but it may be
used later to produce a warning. Dexpreopt command checks if the status
file exists and is nonempty; if that is the case, then compiler filter
is set to "extract".
Bug: 132357300
Test: Manually add some mismatch between the libraries in the Android.bp
and Android.mk files for dexpreopted apps, build with
RELAX_USES_LIBRARY_CHECK=true and obsserve that the build doesn't
fail and they are compiled with compiler-filter "extract".
Unset RELAX_USES_LIBRARY_CHECK and observe that the build fails.
Change-Id: Ibb5d993a25b1df1d2e70b7d5aafc6997f9d64e67
2021-02-17 17:23:28 +01:00
|
|
|
EnforceUsesLibrariesStatusFile: dexpreopt.UsesLibrariesStatusFile(ctx),
|
|
|
|
EnforceUsesLibraries: d.enforceUsesLibs,
|
|
|
|
ProvidesUsesLibrary: providesUsesLib,
|
|
|
|
ClassLoaderContexts: d.classLoaderContexts,
|
2018-11-12 19:13:39 +01:00
|
|
|
|
2019-11-08 11:54:21 +01:00
|
|
|
Archs: archs,
|
|
|
|
DexPreoptImages: images,
|
|
|
|
DexPreoptImagesDeps: imagesDeps,
|
2020-03-26 12:10:45 +01:00
|
|
|
DexPreoptImageLocations: imageLocations,
|
2018-11-12 19:13:39 +01:00
|
|
|
|
2020-02-06 16:14:29 +01:00
|
|
|
PreoptBootClassPathDexFiles: dexFiles,
|
|
|
|
PreoptBootClassPathDexLocations: dexLocations,
|
2019-02-11 23:21:24 +01:00
|
|
|
|
2018-11-12 19:13:39 +01:00
|
|
|
PreoptExtractedApk: false,
|
|
|
|
|
|
|
|
NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
|
|
|
|
ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
|
|
|
|
|
2019-04-15 18:48:31 +02:00
|
|
|
PresignedPrebuilt: d.isPresignedPrebuilt,
|
2018-11-12 19:13:39 +01:00
|
|
|
}
|
|
|
|
|
2020-01-10 21:32:59 +01:00
|
|
|
dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, globalSoong, global, dexpreoptConfig)
|
2018-11-12 19:13:39 +01:00
|
|
|
if err != nil {
|
|
|
|
ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
|
2020-12-17 18:43:28 +01:00
|
|
|
return
|
2018-11-12 19:13:39 +01:00
|
|
|
}
|
|
|
|
|
2020-11-17 02:32:30 +01:00
|
|
|
dexpreoptRule.Build("dexpreopt", "dexpreopt")
|
2018-11-12 19:13:39 +01:00
|
|
|
|
2019-02-11 23:11:09 +01:00
|
|
|
d.builtInstalled = dexpreoptRule.Installs().String()
|
2018-11-12 19:13:39 +01:00
|
|
|
}
|