blob: cdd42ed1adc744fb7ad6aec87f219c8c6499af0a [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 (
Jiakai Zhangca9bc982021-09-09 08:09:41 +000018 "path/filepath"
19 "strings"
20
Colin Cross43f08db2018-11-12 10:13:39 -080021 "android/soong/android"
22 "android/soong/dexpreopt"
23)
24
Jiakai Zhangca9bc982021-09-09 08:09:41 +000025type DexpreopterInterface interface {
Martin Stjernholm6d415272020-01-31 17:10:36 +000026 IsInstallable() bool // Structs that embed dexpreopter must implement this.
27 dexpreoptDisabled(ctx android.BaseModuleContext) bool
Jiakai Zhangca9bc982021-09-09 08:09:41 +000028 DexpreoptBuiltInstalledForApex() []dexpreopterInstall
29 AndroidMkEntriesForApex() []android.AndroidMkEntries
30}
31
32type dexpreopterInstall struct {
33 // A unique name to distinguish an output from others for the same java library module. Usually in
34 // the form of `<arch>-<encoded-path>.odex/vdex/art`.
35 name string
36
37 // The name of the input java module.
38 moduleName string
39
40 // The path to the dexpreopt output on host.
41 outputPathOnHost android.Path
42
43 // The directory on the device for the output to install to.
44 installDirOnDevice android.InstallPath
45
46 // The basename (the last segment of the path) for the output to install as.
47 installFileOnDevice string
48}
49
50// The full module name of the output in the makefile.
51func (install *dexpreopterInstall) FullModuleName() string {
52 return install.moduleName + install.SubModuleName()
53}
54
55// The sub-module name of the output in the makefile (the name excluding the java module name).
56func (install *dexpreopterInstall) SubModuleName() string {
57 return "-dexpreopt-" + install.name
Martin Stjernholm6d415272020-01-31 17:10:36 +000058}
59
Colin Cross43f08db2018-11-12 10:13:39 -080060type dexpreopter struct {
61 dexpreoptProperties DexpreoptProperties
62
Colin Cross70dda7e2019-10-01 22:05:35 -070063 installPath android.InstallPath
Jaewoong Jungccbb3932019-04-15 09:48:31 -070064 uncompressedDex bool
65 isSDKLibrary bool
Ulya Trafimovich76b08522021-01-14 17:52:43 +000066 isApp bool
Jaewoong Jungccbb3932019-04-15 09:48:31 -070067 isTest bool
Jaewoong Jungccbb3932019-04-15 09:48:31 -070068 isPresignedPrebuilt bool
Colin Cross43f08db2018-11-12 10:13:39 -080069
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +000070 manifestFile android.Path
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +000071 statusFile android.WritablePath
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +000072 enforceUsesLibs bool
73 classLoaderContexts dexpreopt.ClassLoaderContextMap
Colin Cross50ddcc42019-05-16 12:28:22 -070074
Jiakai Zhangca9bc982021-09-09 08:09:41 +000075 // See the `dexpreopt` function for details.
76 builtInstalled string
77 builtInstalledForApex []dexpreopterInstall
Ulya Trafimovich76b08522021-01-14 17:52:43 +000078
Jeongik Chac6246672021-04-08 00:00:19 +090079 // The config is used for two purposes:
80 // - Passing dexpreopt information about libraries from Soong to Make. This is needed when
81 // a <uses-library> is defined in Android.bp, but used in Android.mk (see dex_preopt_config_merger.py).
82 // Note that dexpreopt.config might be needed even if dexpreopt is disabled for the library itself.
83 // - Dexpreopt post-processing (using dexpreopt artifacts from a prebuilt system image to incrementally
84 // dexpreopt another partition).
Ulya Trafimovich76b08522021-01-14 17:52:43 +000085 configPath android.WritablePath
Colin Cross43f08db2018-11-12 10:13:39 -080086}
87
88type DexpreoptProperties struct {
89 Dex_preopt struct {
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +010090 // If false, prevent dexpreopting. Defaults to true.
Colin Cross43f08db2018-11-12 10:13:39 -080091 Enabled *bool
92
93 // If true, generate an app image (.art file) for this module.
94 App_image *bool
95
96 // If true, use a checked-in profile to guide optimization. Defaults to false unless
97 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
98 // that matches the name of this module, in which case it is defaulted to true.
99 Profile_guided *bool
100
101 // If set, provides the path to profile relative to the Android.bp file. If not set,
102 // defaults to searching for a file that matches the name of this module in the default
103 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
Colin Crossde4e4e62019-04-26 10:52:32 -0700104 Profile *string `android:"path"`
Colin Cross43f08db2018-11-12 10:13:39 -0800105 }
106}
107
Ulya Trafimovich6cf2c0c2020-04-24 12:15:20 +0100108func init() {
109 dexpreopt.DexpreoptRunningInSoong = true
110}
111
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000112func isApexVariant(ctx android.BaseModuleContext) bool {
113 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
114 return !apexInfo.IsForPlatform()
115}
116
117func moduleName(ctx android.BaseModuleContext) string {
118 // Remove the "prebuilt_" prefix if the module is from a prebuilt because the prefix is not
119 // expected by dexpreopter.
120 return android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName())
121}
122
Martin Stjernholm6d415272020-01-31 17:10:36 +0000123func (d *dexpreopter) dexpreoptDisabled(ctx android.BaseModuleContext) bool {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000124 global := dexpreopt.GetGlobalConfig(ctx)
Colin Cross69f59a32019-02-15 10:39:37 -0800125
126 if global.DisablePreopt {
Colin Cross800fe132019-02-11 14:21:24 -0800127 return true
128 }
129
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000130 if inList(moduleName(ctx), global.DisablePreoptModules) {
Colin Cross43f08db2018-11-12 10:13:39 -0800131 return true
132 }
133
Colin Cross43f08db2018-11-12 10:13:39 -0800134 if d.isTest {
135 return true
136 }
137
138 if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) {
139 return true
140 }
141
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000142 if !ctx.Module().(DexpreopterInterface).IsInstallable() {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000143 return true
144 }
145
146 if ctx.Host() {
Colin Crossdc2da912019-01-05 22:13:05 -0800147 return true
148 }
149
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000150 if isApexVariant(ctx) {
151 // Don't preopt APEX variant module unless the module is an APEX system server jar and we are
152 // building the entire system image.
153 if !global.ApexSystemServerJars.ContainsJar(moduleName(ctx)) || ctx.Config().UnbundledBuild() {
154 return true
155 }
156 } else {
157 // Don't preopt the platform variant of an APEX system server jar to avoid conflicts.
158 if global.ApexSystemServerJars.ContainsJar(moduleName(ctx)) {
159 return true
160 }
Yo Chiangdbdf8f92020-01-09 19:00:27 +0800161 }
162
Colin Cross43f08db2018-11-12 10:13:39 -0800163 // TODO: contains no java code
164
165 return false
166}
167
Martin Stjernholm6d415272020-01-31 17:10:36 +0000168func dexpreoptToolDepsMutator(ctx android.BottomUpMutatorContext) {
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000169 if d, ok := ctx.Module().(DexpreopterInterface); !ok || d.dexpreoptDisabled(ctx) {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000170 return
171 }
172 dexpreopt.RegisterToolDeps(ctx)
173}
174
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000175func (d *dexpreopter) odexOnSystemOther(ctx android.ModuleContext, installPath android.InstallPath) bool {
176 return dexpreopt.OdexOnSystemOtherByName(moduleName(ctx), android.InstallPathToOnDevicePath(ctx, installPath), dexpreopt.GetGlobalConfig(ctx))
177}
178
179// Returns the install path of the dex jar of a module.
180//
181// Do not rely on `ApexInfo.ApexVariationName` because it can be something like "apex1000", rather
182// than the `name` in the path `/apex/<name>` as suggested in its comment.
183//
184// This function is on a best-effort basis. It cannot handle the case where an APEX jar is not a
185// system server jar, which is fine because we currently only preopt system server jars for APEXes.
186func (d *dexpreopter) getInstallPath(
187 ctx android.ModuleContext, defaultInstallPath android.InstallPath) android.InstallPath {
188 global := dexpreopt.GetGlobalConfig(ctx)
189 if global.ApexSystemServerJars.ContainsJar(moduleName(ctx)) {
190 dexLocation := dexpreopt.GetSystemServerDexLocation(global, moduleName(ctx))
191 return android.PathForModuleInPartitionInstall(ctx, "", strings.TrimPrefix(dexLocation, "/"))
192 }
193 if !d.dexpreoptDisabled(ctx) && isApexVariant(ctx) &&
194 filepath.Base(defaultInstallPath.PartitionDir()) != "apex" {
195 ctx.ModuleErrorf("unable to get the install path of the dex jar for dexpreopt")
196 }
197 return defaultInstallPath
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000198}
199
Paul Duffin612e6102021-02-02 13:38:13 +0000200func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.WritablePath) {
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000201 global := dexpreopt.GetGlobalConfig(ctx)
202
Martin Stjernholm6d415272020-01-31 17:10:36 +0000203 // TODO(b/148690468): The check on d.installPath is to bail out in cases where
204 // the dexpreopter struct hasn't been fully initialized before we're called,
205 // e.g. in aar.go. This keeps the behaviour that dexpreopting is effectively
206 // disabled, even if installable is true.
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000207 if d.installPath.Base() == "." {
208 return
209 }
210
211 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
212
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000213 providesUsesLib := moduleName(ctx)
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000214 if ulib, ok := ctx.Module().(ProvidesUsesLib); ok {
215 name := ulib.ProvidesUsesLib()
216 if name != nil {
217 providesUsesLib = *name
218 }
219 }
220
Jeongik Cha4b073cd2021-06-08 11:35:00 +0900221 // If it is test, make config files regardless of its dexpreopt setting.
Jeongik Chac6246672021-04-08 00:00:19 +0900222 // The config files are required for apps defined in make which depend on the lib.
Jeongik Cha4b073cd2021-06-08 11:35:00 +0900223 if d.isTest && d.dexpreoptDisabled(ctx) {
Jaewoong Jung4b97a562020-12-17 09:43:28 -0800224 return
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000225 }
226
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000227 isSystemServerJar := global.SystemServerJars.ContainsJar(moduleName(ctx)) ||
228 global.ApexSystemServerJars.ContainsJar(moduleName(ctx))
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000229
Colin Cross44df5812019-02-15 23:06:46 -0800230 bootImage := defaultBootImageConfig(ctx)
Vladimir Marko40139d62020-02-06 15:14:29 +0000231 if global.UseArtImage {
232 bootImage = artBootImageConfig(ctx)
233 }
Colin Cross43f08db2018-11-12 10:13:39 -0800234
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000235 // System server jars are an exception: they are dexpreopted without updatable bootclasspath.
236 dexFiles, dexLocations := bcpForDexpreopt(ctx, global.PreoptWithUpdatableBcp && !isSystemServerJar)
237
David Srbeckyc177ebe2020-02-18 20:43:06 +0000238 targets := ctx.MultiTargets()
239 if len(targets) == 0 {
Colin Cross43f08db2018-11-12 10:13:39 -0800240 // assume this is a java library, dexpreopt for all arches for now
241 for _, target := range ctx.Config().Targets[android.Android] {
dimitry1f33e402019-03-26 12:39:31 +0100242 if target.NativeBridge == android.NativeBridgeDisabled {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000243 targets = append(targets, target)
dimitry1f33e402019-03-26 12:39:31 +0100244 }
Colin Cross43f08db2018-11-12 10:13:39 -0800245 }
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000246 if isSystemServerJar && !d.isSDKLibrary {
Colin Cross43f08db2018-11-12 10:13:39 -0800247 // 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 +0000248 targets = targets[:1]
Colin Cross43f08db2018-11-12 10:13:39 -0800249 }
250 }
Colin Cross43f08db2018-11-12 10:13:39 -0800251
David Srbeckyc177ebe2020-02-18 20:43:06 +0000252 var archs []android.ArchType
Colin Cross69f59a32019-02-15 10:39:37 -0800253 var images android.Paths
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000254 var imagesDeps []android.OutputPaths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000255 for _, target := range targets {
256 archs = append(archs, target.Arch.ArchType)
257 variant := bootImage.getVariant(target)
Jeongik Chaa5969092021-05-07 18:53:21 +0900258 images = append(images, variant.imagePathOnHost)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000259 imagesDeps = append(imagesDeps, variant.imagesDeps)
Colin Crossc7e40aa2019-02-08 21:37:00 -0800260 }
David Srbeckyab994982020-03-30 17:24:13 +0100261 // The image locations for all Android variants are identical.
Jeongik Cha4dda75e2021-04-27 23:56:44 +0900262 hostImageLocations, deviceImageLocations := bootImage.getAnyAndroidVariant().imageLocations()
Colin Crossc7e40aa2019-02-08 21:37:00 -0800263
Colin Cross43f08db2018-11-12 10:13:39 -0800264 var profileClassListing android.OptionalPath
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100265 var profileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800266 profileIsTextListing := false
267 if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) {
268 // If dex_preopt.profile_guided is not set, default it based on the existence of the
269 // dexprepot.profile option or the profile class listing.
270 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
271 profileClassListing = android.OptionalPathForPath(
272 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100273 profileBootListing = android.ExistentPathForSource(ctx,
274 ctx.ModuleDir(), String(d.dexpreoptProperties.Dex_preopt.Profile)+"-boot")
Colin Cross43f08db2018-11-12 10:13:39 -0800275 profileIsTextListing = true
Dan Willemsen78d51b02020-06-24 16:33:31 -0700276 } else if global.ProfileDir != "" {
Colin Cross43f08db2018-11-12 10:13:39 -0800277 profileClassListing = android.ExistentPathForSource(ctx,
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000278 global.ProfileDir, moduleName(ctx)+".prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800279 }
280 }
281
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000282 // Full dexpreopt config, used to create dexpreopt build rules.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000283 dexpreoptConfig := &dexpreopt.ModuleConfig{
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000284 Name: moduleName(ctx),
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800285 DexLocation: dexLocation,
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000286 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", moduleName(ctx)+".jar").OutputPath,
Colin Cross69f59a32019-02-15 10:39:37 -0800287 DexPath: dexJarFile,
Jeongik Cha33a3a812021-04-15 09:12:49 +0900288 ManifestPath: android.OptionalPathForPath(d.manifestFile),
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800289 UncompressedDex: d.uncompressedDex,
290 HasApkLibraries: false,
291 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800292
Colin Cross69f59a32019-02-15 10:39:37 -0800293 ProfileClassListing: profileClassListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800294 ProfileIsTextListing: profileIsTextListing,
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100295 ProfileBootListing: profileBootListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800296
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +0000297 EnforceUsesLibrariesStatusFile: dexpreopt.UsesLibrariesStatusFile(ctx),
298 EnforceUsesLibraries: d.enforceUsesLibs,
299 ProvidesUsesLibrary: providesUsesLib,
300 ClassLoaderContexts: d.classLoaderContexts,
Colin Cross43f08db2018-11-12 10:13:39 -0800301
Jeongik Cha4dda75e2021-04-27 23:56:44 +0900302 Archs: archs,
303 DexPreoptImagesDeps: imagesDeps,
304 DexPreoptImageLocationsOnHost: hostImageLocations,
305 DexPreoptImageLocationsOnDevice: deviceImageLocations,
Colin Cross43f08db2018-11-12 10:13:39 -0800306
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000307 PreoptBootClassPathDexFiles: dexFiles.Paths(),
Vladimir Marko40139d62020-02-06 15:14:29 +0000308 PreoptBootClassPathDexLocations: dexLocations,
Colin Cross800fe132019-02-11 14:21:24 -0800309
Colin Cross43f08db2018-11-12 10:13:39 -0800310 PreoptExtractedApk: false,
311
312 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
313 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
314
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700315 PresignedPrebuilt: d.isPresignedPrebuilt,
Colin Cross43f08db2018-11-12 10:13:39 -0800316 }
317
Jeongik Chac6246672021-04-08 00:00:19 +0900318 d.configPath = android.PathForModuleOut(ctx, "dexpreopt", "dexpreopt.config")
319 dexpreopt.WriteModuleConfig(ctx, dexpreoptConfig, d.configPath)
320
321 if d.dexpreoptDisabled(ctx) {
322 return
323 }
324
325 globalSoong := dexpreopt.GetGlobalSoongConfig(ctx)
326
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000327 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, globalSoong, global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800328 if err != nil {
329 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
Jaewoong Jung4b97a562020-12-17 09:43:28 -0800330 return
Colin Cross43f08db2018-11-12 10:13:39 -0800331 }
332
Colin Crossf1a035e2020-11-16 17:32:30 -0800333 dexpreoptRule.Build("dexpreopt", "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800334
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000335 if global.ApexSystemServerJars.ContainsJar(moduleName(ctx)) {
336 // APEX variants of java libraries are hidden from Make, so their dexpreopt outputs need special
337 // handling. Currently, for APEX variants of java libraries, only those in the system server
338 // classpath are handled here. Preopting of boot classpath jars in the ART APEX are handled in
339 // java/dexpreopt_bootjars.go, and other APEX jars are not preopted.
340 for _, install := range dexpreoptRule.Installs() {
341 // Remove the "/" prefix because the path should be relative to $ANDROID_PRODUCT_OUT.
342 installDir := strings.TrimPrefix(filepath.Dir(install.To), "/")
343 installBase := filepath.Base(install.To)
344 arch := filepath.Base(installDir)
345 installPath := android.PathForModuleInPartitionInstall(ctx, "", installDir)
346 // The installs will be handled by Make as sub-modules of the java library.
347 d.builtInstalledForApex = append(d.builtInstalledForApex, dexpreopterInstall{
348 name: arch + "-" + installBase,
349 moduleName: moduleName(ctx),
350 outputPathOnHost: install.From,
351 installDirOnDevice: installPath,
352 installFileOnDevice: installBase,
353 })
354 }
355 } else {
356 // The installs will be handled by Make as LOCAL_SOONG_BUILT_INSTALLED of the java library
357 // module.
358 d.builtInstalled = dexpreoptRule.Installs().String()
359 }
360}
361
362func (d *dexpreopter) DexpreoptBuiltInstalledForApex() []dexpreopterInstall {
363 return d.builtInstalledForApex
364}
365
366func (d *dexpreopter) AndroidMkEntriesForApex() []android.AndroidMkEntries {
367 var entries []android.AndroidMkEntries
368 for _, install := range d.builtInstalledForApex {
369 install := install
370 entries = append(entries, android.AndroidMkEntries{
371 Class: "ETC",
372 SubName: install.SubModuleName(),
373 OutputFile: android.OptionalPathForPath(install.outputPathOnHost),
374 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
375 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
376 entries.SetString("LOCAL_MODULE_PATH", install.installDirOnDevice.ToMakePath().String())
377 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", install.installFileOnDevice)
378 entries.SetString("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", "false")
379 },
380 },
381 })
382 }
383 return entries
Colin Cross43f08db2018-11-12 10:13:39 -0800384}