blob: 9db9b1b4643d3d703c9b1b96a8247190b32665fc [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"
Jiakai Zhang51b2a8b2023-06-26 16:47:38 +010019 "sort"
Jiakai Zhangca9bc982021-09-09 08:09:41 +000020 "strings"
21
Colin Cross43f08db2018-11-12 10:13:39 -080022 "android/soong/android"
23 "android/soong/dexpreopt"
24)
25
Jiakai Zhangca9bc982021-09-09 08:09:41 +000026type DexpreopterInterface interface {
Jiakai Zhang81e46812023-02-08 21:56:07 +080027 // True if the java module is to be dexed and installed on devices.
28 // Structs that embed dexpreopter must implement this.
29 IsInstallable() bool
30
31 // True if dexpreopt is disabled for the java module.
Spandan Dase21a8d42024-01-23 23:56:29 +000032 dexpreoptDisabled(ctx android.BaseModuleContext, libraryName string) bool
Jiakai Zhang81e46812023-02-08 21:56:07 +080033
34 // If the java module is to be installed into an APEX, this list contains information about the
35 // dexpreopt outputs to be installed on devices. Note that these dexpreopt outputs are installed
36 // outside of the APEX.
Jiakai Zhangca9bc982021-09-09 08:09:41 +000037 DexpreoptBuiltInstalledForApex() []dexpreopterInstall
Jiakai Zhang81e46812023-02-08 21:56:07 +080038
39 // The Make entries to install the dexpreopt outputs. Derived from
40 // `DexpreoptBuiltInstalledForApex`.
Jiakai Zhangca9bc982021-09-09 08:09:41 +000041 AndroidMkEntriesForApex() []android.AndroidMkEntries
Jiakai Zhang81e46812023-02-08 21:56:07 +080042
43 // See `dexpreopter.outputProfilePathOnHost`.
44 OutputProfilePathOnHost() android.Path
Jiakai Zhangca9bc982021-09-09 08:09:41 +000045}
46
47type dexpreopterInstall struct {
48 // A unique name to distinguish an output from others for the same java library module. Usually in
49 // the form of `<arch>-<encoded-path>.odex/vdex/art`.
50 name string
51
52 // The name of the input java module.
53 moduleName string
54
55 // The path to the dexpreopt output on host.
56 outputPathOnHost android.Path
57
58 // The directory on the device for the output to install to.
59 installDirOnDevice android.InstallPath
60
61 // The basename (the last segment of the path) for the output to install as.
62 installFileOnDevice string
63}
64
65// The full module name of the output in the makefile.
66func (install *dexpreopterInstall) FullModuleName() string {
67 return install.moduleName + install.SubModuleName()
68}
69
70// The sub-module name of the output in the makefile (the name excluding the java module name).
71func (install *dexpreopterInstall) SubModuleName() string {
72 return "-dexpreopt-" + install.name
Martin Stjernholm6d415272020-01-31 17:10:36 +000073}
74
Jiakai Zhang6decef92022-01-12 17:56:19 +000075// Returns Make entries for installing the file.
76//
77// This function uses a value receiver rather than a pointer receiver to ensure that the object is
78// safe to use in `android.AndroidMkExtraEntriesFunc`.
79func (install dexpreopterInstall) ToMakeEntries() android.AndroidMkEntries {
80 return android.AndroidMkEntries{
81 Class: "ETC",
Jiakai Zhang6decef92022-01-12 17:56:19 +000082 OutputFile: android.OptionalPathForPath(install.outputPathOnHost),
83 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
84 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Spandan Das2069c3f2023-12-06 19:40:24 +000085 entries.SetString("LOCAL_MODULE", install.FullModuleName())
Jiakai Zhang6decef92022-01-12 17:56:19 +000086 entries.SetString("LOCAL_MODULE_PATH", install.installDirOnDevice.String())
87 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", install.installFileOnDevice)
88 entries.SetString("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", "false")
Spandan Das2069c3f2023-12-06 19:40:24 +000089 // Unset LOCAL_SOONG_INSTALLED_MODULE so that this does not default to the primary .apex file
90 // Without this, installation of the dexpreopt artifacts get skipped
91 entries.SetString("LOCAL_SOONG_INSTALLED_MODULE", "")
Jiakai Zhang6decef92022-01-12 17:56:19 +000092 },
93 },
94 }
95}
96
Spandan Das2069c3f2023-12-06 19:40:24 +000097type Dexpreopter struct {
98 dexpreopter
99}
100
Colin Cross43f08db2018-11-12 10:13:39 -0800101type dexpreopter struct {
Jiakai Zhang9c4dc192023-02-09 00:09:24 +0800102 dexpreoptProperties DexpreoptProperties
103 importDexpreoptProperties ImportDexpreoptProperties
Colin Cross43f08db2018-11-12 10:13:39 -0800104
Colin Cross70dda7e2019-10-01 22:05:35 -0700105 installPath android.InstallPath
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700106 uncompressedDex bool
107 isSDKLibrary bool
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000108 isApp bool
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700109 isTest bool
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700110 isPresignedPrebuilt bool
Colin Crossfa9bfcd2021-11-10 16:42:38 -0800111 preventInstall bool
Colin Cross43f08db2018-11-12 10:13:39 -0800112
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000113 manifestFile android.Path
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +0000114 statusFile android.WritablePath
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000115 enforceUsesLibs bool
116 classLoaderContexts dexpreopt.ClassLoaderContextMap
Colin Cross50ddcc42019-05-16 12:28:22 -0700117
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000118 // See the `dexpreopt` function for details.
119 builtInstalled string
120 builtInstalledForApex []dexpreopterInstall
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000121
Jeongik Chac6246672021-04-08 00:00:19 +0900122 // The config is used for two purposes:
123 // - Passing dexpreopt information about libraries from Soong to Make. This is needed when
124 // a <uses-library> is defined in Android.bp, but used in Android.mk (see dex_preopt_config_merger.py).
125 // Note that dexpreopt.config might be needed even if dexpreopt is disabled for the library itself.
126 // - Dexpreopt post-processing (using dexpreopt artifacts from a prebuilt system image to incrementally
127 // dexpreopt another partition).
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000128 configPath android.WritablePath
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800129
Jiakai Zhang81e46812023-02-08 21:56:07 +0800130 // The path to the profile on host that dexpreopter generates. This is used as the input for
131 // dex2oat.
132 outputProfilePathOnHost android.Path
133
134 // The path to the profile that dexpreopter accepts. It must be in the binary format. If this is
135 // set, it overrides the profile settings in `dexpreoptProperties`.
136 inputProfilePathOnHost android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800137}
138
139type DexpreoptProperties struct {
140 Dex_preopt struct {
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +0100141 // If false, prevent dexpreopting. Defaults to true.
Colin Cross43f08db2018-11-12 10:13:39 -0800142 Enabled *bool
143
144 // If true, generate an app image (.art file) for this module.
145 App_image *bool
146
147 // If true, use a checked-in profile to guide optimization. Defaults to false unless
148 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
149 // that matches the name of this module, in which case it is defaulted to true.
150 Profile_guided *bool
151
152 // If set, provides the path to profile relative to the Android.bp file. If not set,
153 // defaults to searching for a file that matches the name of this module in the default
154 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
Colin Crossde4e4e62019-04-26 10:52:32 -0700155 Profile *string `android:"path"`
Colin Cross43f08db2018-11-12 10:13:39 -0800156 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +0800157
158 Dex_preopt_result struct {
159 // True if profile-guided optimization is actually enabled.
160 Profile_guided bool
161 } `blueprint:"mutated"`
162}
163
164type ImportDexpreoptProperties struct {
165 Dex_preopt struct {
166 // If true, use the profile in the prebuilt APEX to guide optimization. Defaults to false.
167 Profile_guided *bool
168 }
Colin Cross43f08db2018-11-12 10:13:39 -0800169}
170
Ulya Trafimovich6cf2c0c2020-04-24 12:15:20 +0100171func init() {
172 dexpreopt.DexpreoptRunningInSoong = true
173}
174
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000175func isApexVariant(ctx android.BaseModuleContext) bool {
Colin Crossff694a82023-12-13 15:54:49 -0800176 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000177 return !apexInfo.IsForPlatform()
178}
179
Jiakai Zhang28bc9a82021-12-20 15:08:57 +0000180func forPrebuiltApex(ctx android.BaseModuleContext) bool {
Colin Crossff694a82023-12-13 15:54:49 -0800181 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Jiakai Zhang28bc9a82021-12-20 15:08:57 +0000182 return apexInfo.ForPrebuiltApex
183}
184
Jiakai Zhangcf61e3c2023-05-08 16:28:38 +0000185// Returns whether dexpreopt is applicable to the module.
186// When it returns true, neither profile nor dexpreopt artifacts will be generated.
Spandan Dase21a8d42024-01-23 23:56:29 +0000187func (d *dexpreopter) dexpreoptDisabled(ctx android.BaseModuleContext, libName string) bool {
Colin Cross38310bb2021-12-01 10:34:14 -0800188 if !ctx.Device() {
Colin Cross43f08db2018-11-12 10:13:39 -0800189 return true
190 }
191
Colin Cross43f08db2018-11-12 10:13:39 -0800192 if d.isTest {
193 return true
194 }
195
196 if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) {
197 return true
198 }
199
Jiakai Zhang28bc9a82021-12-20 15:08:57 +0000200 // If the module is from a prebuilt APEX, it shouldn't be installable, but it can still be
201 // dexpreopted.
202 if !ctx.Module().(DexpreopterInterface).IsInstallable() && !forPrebuiltApex(ctx) {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000203 return true
204 }
205
Colin Cross38310bb2021-12-01 10:34:14 -0800206 if !android.IsModulePreferred(ctx.Module()) {
207 return true
208 }
209
Spandan Dase21a8d42024-01-23 23:56:29 +0000210 if _, isApex := android.ModuleProvider(ctx, android.ApexBundleInfoProvider); isApex {
211 // dexpreopt rules for system server jars can be generated in the ModuleCtx of prebuilt apexes
212 return false
213 }
214
Colin Cross38310bb2021-12-01 10:34:14 -0800215 global := dexpreopt.GetGlobalConfig(ctx)
216
Spandan Dase21a8d42024-01-23 23:56:29 +0000217 // Use the libName argument to determine if the library being dexpreopt'd is a system server jar
218 // ctx.ModuleName() is not safe. In case of prebuilt apexes, the dexpreopt rules of system server jars
219 // are created in the ctx object of the top-level prebuilt apex.
220 isApexSystemServerJar := global.AllApexSystemServerJars(ctx).ContainsJar(libName)
221
222 if _, isApex := android.ModuleProvider(ctx, android.ApexBundleInfoProvider); isApex || isApexVariant(ctx) {
223 // dexpreopt rules for system server jars can be generated in the ModuleCtx of prebuilt apexes
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800224 if !isApexSystemServerJar {
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000225 return true
226 }
227 } else {
228 // Don't preopt the platform variant of an APEX system server jar to avoid conflicts.
Jiakai Zhang389a6472021-12-14 18:54:06 +0000229 if isApexSystemServerJar {
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000230 return true
231 }
Yo Chiangdbdf8f92020-01-09 19:00:27 +0800232 }
233
Colin Cross43f08db2018-11-12 10:13:39 -0800234 // TODO: contains no java code
235
236 return false
237}
238
Martin Stjernholm6d415272020-01-31 17:10:36 +0000239func dexpreoptToolDepsMutator(ctx android.BottomUpMutatorContext) {
Spandan Dase21a8d42024-01-23 23:56:29 +0000240 if _, isApex := android.ModuleProvider(ctx, android.ApexBundleInfoProvider); isApex && dexpreopt.IsDex2oatNeeded(ctx) {
241 // prebuilt apexes can genererate rules to dexpreopt deapexed jars
242 // Add a dex2oat dep aggressively on _every_ apex module
243 dexpreopt.RegisterToolDeps(ctx)
244 return
245 }
246 if d, ok := ctx.Module().(DexpreopterInterface); !ok || d.dexpreoptDisabled(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName())) || !dexpreopt.IsDex2oatNeeded(ctx) {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000247 return
248 }
249 dexpreopt.RegisterToolDeps(ctx)
250}
251
Spandan Dase21a8d42024-01-23 23:56:29 +0000252func (d *dexpreopter) odexOnSystemOther(ctx android.ModuleContext, libName string, installPath android.InstallPath) bool {
253 return dexpreopt.OdexOnSystemOtherByName(libName, android.InstallPathToOnDevicePath(ctx, installPath), dexpreopt.GetGlobalConfig(ctx))
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000254}
255
256// Returns the install path of the dex jar of a module.
257//
258// Do not rely on `ApexInfo.ApexVariationName` because it can be something like "apex1000", rather
259// than the `name` in the path `/apex/<name>` as suggested in its comment.
260//
261// This function is on a best-effort basis. It cannot handle the case where an APEX jar is not a
262// system server jar, which is fine because we currently only preopt system server jars for APEXes.
263func (d *dexpreopter) getInstallPath(
Spandan Dase21a8d42024-01-23 23:56:29 +0000264 ctx android.ModuleContext, libName string, defaultInstallPath android.InstallPath) android.InstallPath {
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000265 global := dexpreopt.GetGlobalConfig(ctx)
Spandan Dase21a8d42024-01-23 23:56:29 +0000266 if global.AllApexSystemServerJars(ctx).ContainsJar(libName) {
267 dexLocation := dexpreopt.GetSystemServerDexLocation(ctx, global, libName)
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000268 return android.PathForModuleInPartitionInstall(ctx, "", strings.TrimPrefix(dexLocation, "/"))
269 }
Spandan Dase21a8d42024-01-23 23:56:29 +0000270 if !d.dexpreoptDisabled(ctx, libName) && isApexVariant(ctx) &&
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000271 filepath.Base(defaultInstallPath.PartitionDir()) != "apex" {
272 ctx.ModuleErrorf("unable to get the install path of the dex jar for dexpreopt")
273 }
274 return defaultInstallPath
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000275}
276
Spandan Das2069c3f2023-12-06 19:40:24 +0000277// DexpreoptPrebuiltApexSystemServerJars generates the dexpreopt artifacts from a jar file that has been deapexed from a prebuilt apex
278func (d *Dexpreopter) DexpreoptPrebuiltApexSystemServerJars(ctx android.ModuleContext, libraryName string, di *android.DeapexerInfo) {
279 // A single prebuilt apex can have multiple apex system jars
280 // initialize the output path for this dex jar
281 dc := dexpreopt.GetGlobalConfig(ctx)
282 d.installPath = android.PathForModuleInPartitionInstall(ctx, "", strings.TrimPrefix(dexpreopt.GetSystemServerDexLocation(ctx, dc, libraryName), "/"))
283 // generate the rules for creating the .odex and .vdex files for this system server jar
Spandan Das5be63332023-12-13 00:06:32 +0000284 dexJarFile := di.PrebuiltExportPath(ApexRootRelativePathToJavaLib(libraryName))
Spandan Das2ea84dd2024-01-25 22:12:50 +0000285
286 d.inputProfilePathOnHost = nil // reset: TODO(spandandas): Make dexpreopter stateless
287 if android.InList(libraryName, di.GetDexpreoptProfileGuidedExportedModuleNames()) {
288 // Set the profile path to guide optimization
289 prof := di.PrebuiltExportPath(ApexRootRelativePathToJavaLib(libraryName) + ".prof")
290 if prof == nil {
291 ctx.ModuleErrorf("Could not find a .prof file in this prebuilt apex")
292 }
293 d.inputProfilePathOnHost = prof
294 }
295
Spandan Dase21a8d42024-01-23 23:56:29 +0000296 d.dexpreopt(ctx, libraryName, dexJarFile)
Spandan Das2069c3f2023-12-06 19:40:24 +0000297}
298
Spandan Dase21a8d42024-01-23 23:56:29 +0000299func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, libName string, dexJarFile android.WritablePath) {
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000300 global := dexpreopt.GetGlobalConfig(ctx)
301
Martin Stjernholm6d415272020-01-31 17:10:36 +0000302 // TODO(b/148690468): The check on d.installPath is to bail out in cases where
303 // the dexpreopter struct hasn't been fully initialized before we're called,
304 // e.g. in aar.go. This keeps the behaviour that dexpreopting is effectively
305 // disabled, even if installable is true.
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000306 if d.installPath.Base() == "." {
307 return
308 }
309
310 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
311
Spandan Dase21a8d42024-01-23 23:56:29 +0000312 providesUsesLib := libName
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000313 if ulib, ok := ctx.Module().(ProvidesUsesLib); ok {
314 name := ulib.ProvidesUsesLib()
315 if name != nil {
316 providesUsesLib = *name
317 }
318 }
319
Jeongik Cha4b073cd2021-06-08 11:35:00 +0900320 // If it is test, make config files regardless of its dexpreopt setting.
Jeongik Chac6246672021-04-08 00:00:19 +0900321 // The config files are required for apps defined in make which depend on the lib.
Spandan Dase21a8d42024-01-23 23:56:29 +0000322 if d.isTest && d.dexpreoptDisabled(ctx, libName) {
Jaewoong Jung4b97a562020-12-17 09:43:28 -0800323 return
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000324 }
325
Spandan Dase21a8d42024-01-23 23:56:29 +0000326 isSystemServerJar := global.AllSystemServerJars(ctx).ContainsJar(libName)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000327
Colin Cross44df5812019-02-15 23:06:46 -0800328 bootImage := defaultBootImageConfig(ctx)
Jiakai Zhangb8796202023-03-06 19:16:48 +0000329 // When `global.PreoptWithUpdatableBcp` is true, `bcpForDexpreopt` below includes the mainline
330 // boot jars into bootclasspath, so we should include the mainline boot image as well because it's
331 // generated from those jars.
332 if global.PreoptWithUpdatableBcp {
333 bootImage = mainlineBootImageConfig(ctx)
334 }
Jiakai Zhang02669e82021-09-11 03:44:06 +0000335 dexFiles, dexLocations := bcpForDexpreopt(ctx, global.PreoptWithUpdatableBcp)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000336
David Srbeckyc177ebe2020-02-18 20:43:06 +0000337 targets := ctx.MultiTargets()
338 if len(targets) == 0 {
Colin Cross43f08db2018-11-12 10:13:39 -0800339 // assume this is a java library, dexpreopt for all arches for now
340 for _, target := range ctx.Config().Targets[android.Android] {
dimitry1f33e402019-03-26 12:39:31 +0100341 if target.NativeBridge == android.NativeBridgeDisabled {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000342 targets = append(targets, target)
dimitry1f33e402019-03-26 12:39:31 +0100343 }
Colin Cross43f08db2018-11-12 10:13:39 -0800344 }
Spandan Dase21a8d42024-01-23 23:56:29 +0000345 if isSystemServerJar && libName != "com.android.location.provider" {
Jiakai Zhang2fbc3552022-11-28 15:38:23 +0000346 // If the module is a system server jar, only preopt for the primary arch because the jar can
347 // only be loaded by system server. "com.android.location.provider" is a special case because
348 // it's also used by apps as a shared library.
David Srbeckyc177ebe2020-02-18 20:43:06 +0000349 targets = targets[:1]
Colin Cross43f08db2018-11-12 10:13:39 -0800350 }
351 }
Colin Cross43f08db2018-11-12 10:13:39 -0800352
David Srbeckyc177ebe2020-02-18 20:43:06 +0000353 var archs []android.ArchType
Colin Cross69f59a32019-02-15 10:39:37 -0800354 var images android.Paths
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000355 var imagesDeps []android.OutputPaths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000356 for _, target := range targets {
357 archs = append(archs, target.Arch.ArchType)
358 variant := bootImage.getVariant(target)
Jeongik Chaa5969092021-05-07 18:53:21 +0900359 images = append(images, variant.imagePathOnHost)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000360 imagesDeps = append(imagesDeps, variant.imagesDeps)
Colin Crossc7e40aa2019-02-08 21:37:00 -0800361 }
David Srbeckyab994982020-03-30 17:24:13 +0100362 // The image locations for all Android variants are identical.
Jeongik Cha4dda75e2021-04-27 23:56:44 +0900363 hostImageLocations, deviceImageLocations := bootImage.getAnyAndroidVariant().imageLocations()
Colin Crossc7e40aa2019-02-08 21:37:00 -0800364
Colin Cross43f08db2018-11-12 10:13:39 -0800365 var profileClassListing android.OptionalPath
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100366 var profileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800367 profileIsTextListing := false
Spandan Das2ea84dd2024-01-25 22:12:50 +0000368
Jiakai Zhang81e46812023-02-08 21:56:07 +0800369 if d.inputProfilePathOnHost != nil {
370 profileClassListing = android.OptionalPathForPath(d.inputProfilePathOnHost)
371 } else if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) && !forPrebuiltApex(ctx) {
Colin Cross43f08db2018-11-12 10:13:39 -0800372 // If dex_preopt.profile_guided is not set, default it based on the existence of the
373 // dexprepot.profile option or the profile class listing.
374 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
375 profileClassListing = android.OptionalPathForPath(
376 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100377 profileBootListing = android.ExistentPathForSource(ctx,
378 ctx.ModuleDir(), String(d.dexpreoptProperties.Dex_preopt.Profile)+"-boot")
Colin Cross43f08db2018-11-12 10:13:39 -0800379 profileIsTextListing = true
Dan Willemsen78d51b02020-06-24 16:33:31 -0700380 } else if global.ProfileDir != "" {
Colin Cross43f08db2018-11-12 10:13:39 -0800381 profileClassListing = android.ExistentPathForSource(ctx,
Spandan Dase21a8d42024-01-23 23:56:29 +0000382 global.ProfileDir, libName+".prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800383 }
384 }
385
Jiakai Zhang9c4dc192023-02-09 00:09:24 +0800386 d.dexpreoptProperties.Dex_preopt_result.Profile_guided = profileClassListing.Valid()
387
Spandan Das2069c3f2023-12-06 19:40:24 +0000388 // A single apex can have multiple system server jars
389 // Use the dexJar to create a unique scope for each
390 dexJarStem := strings.TrimSuffix(dexJarFile.Base(), dexJarFile.Ext())
391
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000392 // Full dexpreopt config, used to create dexpreopt build rules.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000393 dexpreoptConfig := &dexpreopt.ModuleConfig{
Spandan Dase21a8d42024-01-23 23:56:29 +0000394 Name: libName,
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800395 DexLocation: dexLocation,
Spandan Dase21a8d42024-01-23 23:56:29 +0000396 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", dexJarStem, libName+".jar").OutputPath,
Colin Cross69f59a32019-02-15 10:39:37 -0800397 DexPath: dexJarFile,
Jeongik Cha33a3a812021-04-15 09:12:49 +0900398 ManifestPath: android.OptionalPathForPath(d.manifestFile),
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800399 UncompressedDex: d.uncompressedDex,
400 HasApkLibraries: false,
401 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800402
Colin Cross69f59a32019-02-15 10:39:37 -0800403 ProfileClassListing: profileClassListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800404 ProfileIsTextListing: profileIsTextListing,
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100405 ProfileBootListing: profileBootListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800406
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +0000407 EnforceUsesLibrariesStatusFile: dexpreopt.UsesLibrariesStatusFile(ctx),
408 EnforceUsesLibraries: d.enforceUsesLibs,
409 ProvidesUsesLibrary: providesUsesLib,
410 ClassLoaderContexts: d.classLoaderContexts,
Colin Cross43f08db2018-11-12 10:13:39 -0800411
Jeongik Cha4dda75e2021-04-27 23:56:44 +0900412 Archs: archs,
413 DexPreoptImagesDeps: imagesDeps,
414 DexPreoptImageLocationsOnHost: hostImageLocations,
415 DexPreoptImageLocationsOnDevice: deviceImageLocations,
Colin Cross43f08db2018-11-12 10:13:39 -0800416
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000417 PreoptBootClassPathDexFiles: dexFiles.Paths(),
Vladimir Marko40139d62020-02-06 15:14:29 +0000418 PreoptBootClassPathDexLocations: dexLocations,
Colin Cross800fe132019-02-11 14:21:24 -0800419
Colin Cross43f08db2018-11-12 10:13:39 -0800420 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
421 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
422
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700423 PresignedPrebuilt: d.isPresignedPrebuilt,
Colin Cross43f08db2018-11-12 10:13:39 -0800424 }
425
Spandan Das2069c3f2023-12-06 19:40:24 +0000426 d.configPath = android.PathForModuleOut(ctx, "dexpreopt", dexJarStem, "dexpreopt.config")
Jeongik Chac6246672021-04-08 00:00:19 +0900427 dexpreopt.WriteModuleConfig(ctx, dexpreoptConfig, d.configPath)
428
Spandan Dase21a8d42024-01-23 23:56:29 +0000429 if d.dexpreoptDisabled(ctx, libName) {
Jeongik Chac6246672021-04-08 00:00:19 +0900430 return
431 }
432
433 globalSoong := dexpreopt.GetGlobalSoongConfig(ctx)
434
Jiakai Zhang51b2a8b2023-06-26 16:47:38 +0100435 // The root "product_packages.txt" is generated by `build/make/core/Makefile`. It contains a list
436 // of all packages that are installed on the device. We use `grep` to filter the list by the app's
437 // dependencies to create a per-app list, and use `rsync --checksum` to prevent the file's mtime
438 // from being changed if the contents don't change. This avoids unnecessary dexpreopt reruns.
Jiakai Zhanga4496782023-05-17 16:57:30 +0100439 productPackages := android.PathForModuleInPartitionInstall(ctx, "", "product_packages.txt")
Spandan Das2069c3f2023-12-06 19:40:24 +0000440 appProductPackages := android.PathForModuleOut(ctx, "dexpreopt", dexJarStem, "product_packages.txt")
Jiakai Zhang51b2a8b2023-06-26 16:47:38 +0100441 appProductPackagesStaging := appProductPackages.ReplaceExtension(ctx, "txt.tmp")
442 clcNames, _ := dexpreopt.ComputeClassLoaderContextDependencies(dexpreoptConfig.ClassLoaderContexts)
443 sort.Strings(clcNames) // The order needs to be deterministic.
444 productPackagesRule := android.NewRuleBuilder(pctx, ctx)
445 if len(clcNames) > 0 {
446 productPackagesRule.Command().
447 Text("grep -F -x").
448 FlagForEachArg("-e ", clcNames).
449 Input(productPackages).
450 FlagWithOutput("> ", appProductPackagesStaging).
451 Text("|| true")
452 } else {
453 productPackagesRule.Command().
454 Text("rm -f").Output(appProductPackagesStaging).
455 Text("&&").
456 Text("touch").Output(appProductPackagesStaging)
457 }
458 productPackagesRule.Command().
459 Text("rsync --checksum").
460 Input(appProductPackagesStaging).
461 Output(appProductPackages)
Spandan Das2069c3f2023-12-06 19:40:24 +0000462 productPackagesRule.Restat().Build("product_packages."+dexJarStem, "dexpreopt product_packages")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100463
464 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(
Jiakai Zhang51b2a8b2023-06-26 16:47:38 +0100465 ctx, globalSoong, global, dexpreoptConfig, appProductPackages)
Colin Cross43f08db2018-11-12 10:13:39 -0800466 if err != nil {
467 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
Jaewoong Jung4b97a562020-12-17 09:43:28 -0800468 return
Colin Cross43f08db2018-11-12 10:13:39 -0800469 }
470
Spandan Das2069c3f2023-12-06 19:40:24 +0000471 dexpreoptRule.Build("dexpreopt"+"."+dexJarStem, "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800472
Spandan Das2069c3f2023-12-06 19:40:24 +0000473 // The current ctx might be of a deapexer module created by a prebuilt apex
474 // Use the path of the dex file to determine the library name
475 isApexSystemServerJar := global.AllApexSystemServerJars(ctx).ContainsJar(dexJarStem)
Jiakai Zhang389a6472021-12-14 18:54:06 +0000476
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700477 for _, install := range dexpreoptRule.Installs() {
478 // Remove the "/" prefix because the path should be relative to $ANDROID_PRODUCT_OUT.
479 installDir := strings.TrimPrefix(filepath.Dir(install.To), "/")
480 installBase := filepath.Base(install.To)
481 arch := filepath.Base(installDir)
482 installPath := android.PathForModuleInPartitionInstall(ctx, "", installDir)
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800483 isProfile := strings.HasSuffix(installBase, ".prof")
484
485 if isProfile {
Jiakai Zhang81e46812023-02-08 21:56:07 +0800486 d.outputProfilePathOnHost = install.From
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800487 }
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700488
Jiakai Zhang389a6472021-12-14 18:54:06 +0000489 if isApexSystemServerJar {
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800490 // Profiles are handled separately because they are installed into the APEX.
491 if !isProfile {
492 // APEX variants of java libraries are hidden from Make, so their dexpreopt
493 // outputs need special handling. Currently, for APEX variants of java
494 // libraries, only those in the system server classpath are handled here.
495 // Preopting of boot classpath jars in the ART APEX are handled in
496 // java/dexpreopt_bootjars.go, and other APEX jars are not preopted.
497 // The installs will be handled by Make as sub-modules of the java library.
498 d.builtInstalledForApex = append(d.builtInstalledForApex, dexpreopterInstall{
499 name: arch + "-" + installBase,
Spandan Dase21a8d42024-01-23 23:56:29 +0000500 moduleName: libName,
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800501 outputPathOnHost: install.From,
502 installDirOnDevice: installPath,
503 installFileOnDevice: installBase,
504 })
505 }
Colin Crossfa9bfcd2021-11-10 16:42:38 -0800506 } else if !d.preventInstall {
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700507 ctx.InstallFile(installPath, installBase, install.From)
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000508 }
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700509 }
510
Jiakai Zhang389a6472021-12-14 18:54:06 +0000511 if !isApexSystemServerJar {
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000512 d.builtInstalled = dexpreoptRule.Installs().String()
513 }
514}
515
516func (d *dexpreopter) DexpreoptBuiltInstalledForApex() []dexpreopterInstall {
517 return d.builtInstalledForApex
518}
519
520func (d *dexpreopter) AndroidMkEntriesForApex() []android.AndroidMkEntries {
521 var entries []android.AndroidMkEntries
522 for _, install := range d.builtInstalledForApex {
Jiakai Zhang6decef92022-01-12 17:56:19 +0000523 entries = append(entries, install.ToMakeEntries())
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000524 }
525 return entries
Colin Cross43f08db2018-11-12 10:13:39 -0800526}
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800527
Jiakai Zhang81e46812023-02-08 21:56:07 +0800528func (d *dexpreopter) OutputProfilePathOnHost() android.Path {
529 return d.outputProfilePathOnHost
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800530}