blob: b4cf012af8c0524997f55a11d2fb6667e8cc4b56 [file] [log] [blame]
Colin Cross43f08db2018-11-12 10:13:39 -08001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Colin Cross43f08db2018-11-12 10:13:39 -080018 "android/soong/android"
19 "android/soong/dexpreopt"
20)
21
Martin Stjernholm6d415272020-01-31 17:10:36 +000022type dexpreopterInterface interface {
23 IsInstallable() bool // Structs that embed dexpreopter must implement this.
24 dexpreoptDisabled(ctx android.BaseModuleContext) bool
25}
26
Colin Cross43f08db2018-11-12 10:13:39 -080027type dexpreopter struct {
28 dexpreoptProperties DexpreoptProperties
29
Colin Cross70dda7e2019-10-01 22:05:35 -070030 installPath android.InstallPath
Jaewoong Jungccbb3932019-04-15 09:48:31 -070031 uncompressedDex bool
32 isSDKLibrary bool
Ulya Trafimovich76b08522021-01-14 17:52:43 +000033 isApp bool
Jaewoong Jungccbb3932019-04-15 09:48:31 -070034 isTest bool
Jaewoong Jungccbb3932019-04-15 09:48:31 -070035 isPresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -080036
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +000037 manifestFile android.Path
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +000038 statusFile android.WritablePath
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +000039 enforceUsesLibs bool
40 classLoaderContexts dexpreopt.ClassLoaderContextMap
Colin Cross50ddcc42019-05-16 12:28:22 -070041
Colin Crossdeabb942019-02-11 14:11:09 -080042 builtInstalled string
Ulya Trafimovich76b08522021-01-14 17:52:43 +000043
44 // A path to a dexpreopt.config file generated by Soong for libraries that may be used as a
45 // <uses-library> by Make modules. The path is passed to Make via LOCAL_SOONG_DEXPREOPT_CONFIG
46 // variable. If the path is nil, no config is generated (which is the case for apps and tests).
47 configPath android.WritablePath
Colin Cross43f08db2018-11-12 10:13:39 -080048}
49
50type DexpreoptProperties struct {
51 Dex_preopt struct {
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010052 // If false, prevent dexpreopting. Defaults to true.
Colin Cross43f08db2018-11-12 10:13:39 -080053 Enabled *bool
54
55 // If true, generate an app image (.art file) for this module.
56 App_image *bool
57
58 // If true, use a checked-in profile to guide optimization. Defaults to false unless
59 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
60 // that matches the name of this module, in which case it is defaulted to true.
61 Profile_guided *bool
62
63 // If set, provides the path to profile relative to the Android.bp file. If not set,
64 // defaults to searching for a file that matches the name of this module in the default
65 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
Colin Crossde4e4e62019-04-26 10:52:32 -070066 Profile *string `android:"path"`
Colin Cross43f08db2018-11-12 10:13:39 -080067 }
68}
69
Ulya Trafimovich6cf2c0c2020-04-24 12:15:20 +010070func init() {
71 dexpreopt.DexpreoptRunningInSoong = true
72}
73
Martin Stjernholm6d415272020-01-31 17:10:36 +000074func (d *dexpreopter) dexpreoptDisabled(ctx android.BaseModuleContext) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000075 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross69f59a32019-02-15 10:39:37 -080076
77 if global.DisablePreopt {
Colin Cross800fe132019-02-11 14:21:24 -080078 return true
79 }
80
Colin Cross69f59a32019-02-15 10:39:37 -080081 if inList(ctx.ModuleName(), global.DisablePreoptModules) {
Colin Cross43f08db2018-11-12 10:13:39 -080082 return true
83 }
84
Colin Cross43f08db2018-11-12 10:13:39 -080085 if d.isTest {
86 return true
87 }
88
89 if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) {
90 return true
91 }
92
Martin Stjernholm6d415272020-01-31 17:10:36 +000093 if !ctx.Module().(dexpreopterInterface).IsInstallable() {
94 return true
95 }
96
97 if ctx.Host() {
Colin Crossdc2da912019-01-05 22:13:05 -080098 return true
99 }
100
Yo Chiangdbdf8f92020-01-09 19:00:27 +0800101 // Don't preopt APEX variant module
Colin Cross56a83212020-09-15 18:30:11 -0700102 if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() {
Yo Chiangdbdf8f92020-01-09 19:00:27 +0800103 return true
104 }
105
Colin Cross43f08db2018-11-12 10:13:39 -0800106 // TODO: contains no java code
107
108 return false
109}
110
Martin Stjernholm6d415272020-01-31 17:10:36 +0000111func dexpreoptToolDepsMutator(ctx android.BottomUpMutatorContext) {
112 if d, ok := ctx.Module().(dexpreopterInterface); !ok || d.dexpreoptDisabled(ctx) {
113 return
114 }
115 dexpreopt.RegisterToolDeps(ctx)
116}
117
Colin Cross70dda7e2019-10-01 22:05:35 -0700118func odexOnSystemOther(ctx android.ModuleContext, installPath android.InstallPath) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000119 return dexpreopt.OdexOnSystemOtherByName(ctx.ModuleName(), android.InstallPathToOnDevicePath(ctx, installPath), dexpreopt.GetGlobalConfig(ctx))
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000120}
121
Paul Duffin612e6102021-02-02 13:38:13 +0000122func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.WritablePath) {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000123 // TODO(b/148690468): The check on d.installPath is to bail out in cases where
124 // the dexpreopter struct hasn't been fully initialized before we're called,
125 // e.g. in aar.go. This keeps the behaviour that dexpreopting is effectively
126 // disabled, even if installable is true.
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000127 if d.installPath.Base() == "." {
128 return
129 }
130
131 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
132
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000133 providesUsesLib := ctx.ModuleName()
134 if ulib, ok := ctx.Module().(ProvidesUsesLib); ok {
135 name := ulib.ProvidesUsesLib()
136 if name != nil {
137 providesUsesLib = *name
138 }
139 }
140
141 if !d.isApp && !d.isTest {
142 // Slim dexpreopt config is serialized to dexpreopt.config files and used by
143 // dex_preopt_config_merger.py to get information about <uses-library> dependencies.
144 // Note that it might be needed even if dexpreopt is disabled for this module.
145 slimDexpreoptConfig := &dexpreopt.ModuleConfig{
146 Name: ctx.ModuleName(),
147 DexLocation: dexLocation,
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000148 EnforceUsesLibraries: d.enforceUsesLibs,
149 ProvidesUsesLibrary: providesUsesLib,
150 ClassLoaderContexts: d.classLoaderContexts,
151 // The rest of the fields are not needed.
152 }
153 d.configPath = android.PathForModuleOut(ctx, "dexpreopt", "dexpreopt.config")
154 dexpreopt.WriteSlimModuleConfigForMake(ctx, slimDexpreoptConfig, d.configPath)
155 }
156
157 if d.dexpreoptDisabled(ctx) {
Jaewoong Jung4b97a562020-12-17 09:43:28 -0800158 return
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000159 }
160
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000161 globalSoong := dexpreopt.GetGlobalSoongConfig(ctx)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000162 global := dexpreopt.GetGlobalConfig(ctx)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000163
164 isSystemServerJar := inList(ctx.ModuleName(), global.SystemServerJars)
165
Colin Cross44df5812019-02-15 23:06:46 -0800166 bootImage := defaultBootImageConfig(ctx)
Vladimir Marko40139d62020-02-06 15:14:29 +0000167 if global.UseArtImage {
168 bootImage = artBootImageConfig(ctx)
169 }
Colin Cross43f08db2018-11-12 10:13:39 -0800170
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000171 // System server jars are an exception: they are dexpreopted without updatable bootclasspath.
172 dexFiles, dexLocations := bcpForDexpreopt(ctx, global.PreoptWithUpdatableBcp && !isSystemServerJar)
173
David Srbeckyc177ebe2020-02-18 20:43:06 +0000174 targets := ctx.MultiTargets()
175 if len(targets) == 0 {
Colin Cross43f08db2018-11-12 10:13:39 -0800176 // assume this is a java library, dexpreopt for all arches for now
177 for _, target := range ctx.Config().Targets[android.Android] {
dimitry1f33e402019-03-26 12:39:31 +0100178 if target.NativeBridge == android.NativeBridgeDisabled {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000179 targets = append(targets, target)
dimitry1f33e402019-03-26 12:39:31 +0100180 }
Colin Cross43f08db2018-11-12 10:13:39 -0800181 }
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000182 if isSystemServerJar && !d.isSDKLibrary {
Colin Cross43f08db2018-11-12 10:13:39 -0800183 // If the module is not an SDK library and it's a system server jar, only preopt the primary arch.
David Srbeckyc177ebe2020-02-18 20:43:06 +0000184 targets = targets[:1]
Colin Cross43f08db2018-11-12 10:13:39 -0800185 }
186 }
Colin Cross43f08db2018-11-12 10:13:39 -0800187
David Srbeckyc177ebe2020-02-18 20:43:06 +0000188 var archs []android.ArchType
Colin Cross69f59a32019-02-15 10:39:37 -0800189 var images android.Paths
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000190 var imagesDeps []android.OutputPaths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000191 for _, target := range targets {
192 archs = append(archs, target.Arch.ArchType)
193 variant := bootImage.getVariant(target)
194 images = append(images, variant.images)
195 imagesDeps = append(imagesDeps, variant.imagesDeps)
Colin Crossc7e40aa2019-02-08 21:37:00 -0800196 }
David Srbeckyab994982020-03-30 17:24:13 +0100197 // The image locations for all Android variants are identical.
198 imageLocations := bootImage.getAnyAndroidVariant().imageLocations()
Colin Crossc7e40aa2019-02-08 21:37:00 -0800199
Colin Cross43f08db2018-11-12 10:13:39 -0800200 var profileClassListing android.OptionalPath
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100201 var profileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800202 profileIsTextListing := false
203 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
204 // If dex_preopt.profile_guided is not set, default it based on the existence of the
205 // dexprepot.profile option or the profile class listing.
206 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
207 profileClassListing = android.OptionalPathForPath(
208 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100209 profileBootListing = android.ExistentPathForSource(ctx,
210 ctx.ModuleDir(), String(d.dexpreoptProperties.Dex_preopt.Profile)+"-boot")
Colin Cross43f08db2018-11-12 10:13:39 -0800211 profileIsTextListing = true
Dan Willemsen78d51b02020-06-24 16:33:31 -0700212 } else if global.ProfileDir != "" {
Colin Cross43f08db2018-11-12 10:13:39 -0800213 profileClassListing = android.ExistentPathForSource(ctx,
Colin Cross44df5812019-02-15 23:06:46 -0800214 global.ProfileDir, ctx.ModuleName()+".prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800215 }
216 }
217
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000218 // Full dexpreopt config, used to create dexpreopt build rules.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000219 dexpreoptConfig := &dexpreopt.ModuleConfig{
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800220 Name: ctx.ModuleName(),
221 DexLocation: dexLocation,
Ulya Trafimovichc0f64792021-02-04 10:04:39 +0000222 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath,
Colin Cross69f59a32019-02-15 10:39:37 -0800223 DexPath: dexJarFile,
Colin Cross50ddcc42019-05-16 12:28:22 -0700224 ManifestPath: d.manifestFile,
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800225 UncompressedDex: d.uncompressedDex,
226 HasApkLibraries: false,
227 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800228
Colin Cross69f59a32019-02-15 10:39:37 -0800229 ProfileClassListing: profileClassListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800230 ProfileIsTextListing: profileIsTextListing,
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100231 ProfileBootListing: profileBootListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800232
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +0000233 EnforceUsesLibrariesStatusFile: dexpreopt.UsesLibrariesStatusFile(ctx),
234 EnforceUsesLibraries: d.enforceUsesLibs,
235 ProvidesUsesLibrary: providesUsesLib,
236 ClassLoaderContexts: d.classLoaderContexts,
Colin Cross43f08db2018-11-12 10:13:39 -0800237
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000238 Archs: archs,
239 DexPreoptImages: images,
240 DexPreoptImagesDeps: imagesDeps,
David Srbecky1aacc6c2020-03-26 11:10:45 +0000241 DexPreoptImageLocations: imageLocations,
Colin Cross43f08db2018-11-12 10:13:39 -0800242
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000243 PreoptBootClassPathDexFiles: dexFiles.Paths(),
Vladimir Marko40139d62020-02-06 15:14:29 +0000244 PreoptBootClassPathDexLocations: dexLocations,
Colin Cross800fe132019-02-11 14:21:24 -0800245
Colin Cross43f08db2018-11-12 10:13:39 -0800246 PreoptExtractedApk: false,
247
248 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
249 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
250
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700251 PresignedPrebuilt: d.isPresignedPrebuilt,
Colin Cross43f08db2018-11-12 10:13:39 -0800252 }
253
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000254 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, globalSoong, global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800255 if err != nil {
256 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
Jaewoong Jung4b97a562020-12-17 09:43:28 -0800257 return
Colin Cross43f08db2018-11-12 10:13:39 -0800258 }
259
Colin Crossf1a035e2020-11-16 17:32:30 -0800260 dexpreoptRule.Build("dexpreopt", "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800261
Colin Crossdeabb942019-02-11 14:11:09 -0800262 d.builtInstalled = dexpreoptRule.Installs().String()
Colin Cross43f08db2018-11-12 10:13:39 -0800263}