blob: b21cfc96844a4359232d7f4b0725aeca120c6002 [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
Spandan Das3dbda182024-05-20 22:23:10 +000022 "github.com/google/blueprint/proptools"
23
Colin Cross43f08db2018-11-12 10:13:39 -080024 "android/soong/android"
25 "android/soong/dexpreopt"
26)
27
Jiakai Zhangca9bc982021-09-09 08:09:41 +000028type DexpreopterInterface interface {
Jiakai Zhang81e46812023-02-08 21:56:07 +080029 // True if the java module is to be dexed and installed on devices.
30 // Structs that embed dexpreopter must implement this.
31 IsInstallable() bool
32
33 // True if dexpreopt is disabled for the java module.
Spandan Dase21a8d42024-01-23 23:56:29 +000034 dexpreoptDisabled(ctx android.BaseModuleContext, libraryName string) bool
Jiakai Zhang81e46812023-02-08 21:56:07 +080035
36 // If the java module is to be installed into an APEX, this list contains information about the
37 // dexpreopt outputs to be installed on devices. Note that these dexpreopt outputs are installed
38 // outside of the APEX.
Colin Cross388c6612025-01-28 14:00:12 -080039 ApexSystemServerDexpreoptInstalls() []DexpreopterInstall
Jiakai Zhang81e46812023-02-08 21:56:07 +080040
Colin Cross388c6612025-01-28 14:00:12 -080041 // ApexSystemServerDexJars returns the list of dex jars if this is an apex system server jar.
42 ApexSystemServerDexJars() android.Paths
Jiakai Zhang81e46812023-02-08 21:56:07 +080043
44 // See `dexpreopter.outputProfilePathOnHost`.
45 OutputProfilePathOnHost() android.Path
Jiakai Zhangca9bc982021-09-09 08:09:41 +000046}
47
Colin Cross388c6612025-01-28 14:00:12 -080048type DexpreopterInstall struct {
Jiakai Zhangca9bc982021-09-09 08:09:41 +000049 // The path to the dexpreopt output on host.
Colin Cross388c6612025-01-28 14:00:12 -080050 OutputPathOnHost android.Path
Jiakai Zhangca9bc982021-09-09 08:09:41 +000051
52 // The directory on the device for the output to install to.
Colin Cross388c6612025-01-28 14:00:12 -080053 InstallDirOnDevice android.InstallPath
Jiakai Zhangca9bc982021-09-09 08:09:41 +000054
55 // The basename (the last segment of the path) for the output to install as.
Colin Cross388c6612025-01-28 14:00:12 -080056 InstallFileOnDevice string
Jihoon Kangd4063812025-01-24 00:25:30 +000057}
58
Spandan Das2069c3f2023-12-06 19:40:24 +000059type Dexpreopter struct {
60 dexpreopter
61}
62
Colin Cross43f08db2018-11-12 10:13:39 -080063type dexpreopter struct {
Jiakai Zhang9c4dc192023-02-09 00:09:24 +080064 dexpreoptProperties DexpreoptProperties
65 importDexpreoptProperties ImportDexpreoptProperties
Colin Cross43f08db2018-11-12 10:13:39 -080066
Spandan Das0727ba72024-02-13 16:37:43 +000067 // If true, the dexpreopt rules will not be generated
68 // Unlike Dex_preopt.Enabled which is user-facing,
69 // shouldDisableDexpreopt is a mutated propery.
70 shouldDisableDexpreopt bool
71
Colin Cross70dda7e2019-10-01 22:05:35 -070072 installPath android.InstallPath
Jaewoong Jungccbb3932019-04-15 09:48:31 -070073 uncompressedDex bool
74 isSDKLibrary bool
Ulya Trafimovich76b08522021-01-14 17:52:43 +000075 isApp bool
Jaewoong Jungccbb3932019-04-15 09:48:31 -070076 isTest bool
Jaewoong Jungccbb3932019-04-15 09:48:31 -070077 isPresignedPrebuilt bool
Colin Crossfa9bfcd2021-11-10 16:42:38 -080078 preventInstall bool
Colin Cross43f08db2018-11-12 10:13:39 -080079
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +000080 manifestFile android.Path
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +000081 statusFile android.WritablePath
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +000082 enforceUsesLibs bool
83 classLoaderContexts dexpreopt.ClassLoaderContextMap
Colin Cross50ddcc42019-05-16 12:28:22 -070084
Jiakai Zhangca9bc982021-09-09 08:09:41 +000085 // See the `dexpreopt` function for details.
Colin Cross388c6612025-01-28 14:00:12 -080086 builtInstalled string
87 apexSystemServerDexpreoptInstalls []DexpreopterInstall
88 apexSystemServerDexJars android.Paths
Ulya Trafimovich76b08522021-01-14 17:52:43 +000089
Jeongik Chac6246672021-04-08 00:00:19 +090090 // The config is used for two purposes:
91 // - Passing dexpreopt information about libraries from Soong to Make. This is needed when
92 // a <uses-library> is defined in Android.bp, but used in Android.mk (see dex_preopt_config_merger.py).
93 // Note that dexpreopt.config might be needed even if dexpreopt is disabled for the library itself.
94 // - Dexpreopt post-processing (using dexpreopt artifacts from a prebuilt system image to incrementally
95 // dexpreopt another partition).
Ulya Trafimovich76b08522021-01-14 17:52:43 +000096 configPath android.WritablePath
Jiakai Zhang3317ce72023-02-08 01:19:19 +080097
Jiakai Zhang81e46812023-02-08 21:56:07 +080098 // The path to the profile on host that dexpreopter generates. This is used as the input for
99 // dex2oat.
100 outputProfilePathOnHost android.Path
101
102 // The path to the profile that dexpreopter accepts. It must be in the binary format. If this is
103 // set, it overrides the profile settings in `dexpreoptProperties`.
104 inputProfilePathOnHost android.Path
Spandan Das3dbda182024-05-20 22:23:10 +0000105
106 // The path to the profile that matches the dex optimized by r8/d8. It is in text format. If this is
107 // set, it will be converted to a binary profile which will be subsequently used for dexpreopt.
108 rewrittenProfile android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800109}
110
111type DexpreoptProperties struct {
112 Dex_preopt struct {
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +0100113 // If false, prevent dexpreopting. Defaults to true.
Cole Fausteb032462024-09-19 11:12:54 -0700114 Enabled proptools.Configurable[bool] `android:"replace_instead_of_append"`
Colin Cross43f08db2018-11-12 10:13:39 -0800115
116 // If true, generate an app image (.art file) for this module.
Cole Fausteb032462024-09-19 11:12:54 -0700117 App_image proptools.Configurable[bool] `android:"replace_instead_of_append"`
Colin Cross43f08db2018-11-12 10:13:39 -0800118
119 // If true, use a checked-in profile to guide optimization. Defaults to false unless
120 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
121 // that matches the name of this module, in which case it is defaulted to true.
Cole Fausteb032462024-09-19 11:12:54 -0700122 Profile_guided proptools.Configurable[bool] `android:"replace_instead_of_append"`
Colin Cross43f08db2018-11-12 10:13:39 -0800123
124 // If set, provides the path to profile relative to the Android.bp file. If not set,
125 // defaults to searching for a file that matches the name of this module in the default
126 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
Cole Fausteb032462024-09-19 11:12:54 -0700127 Profile proptools.Configurable[string] `android:"path,replace_instead_of_append"`
Spandan Das3dbda182024-05-20 22:23:10 +0000128
129 // If set to true, r8/d8 will use `profile` as input to generate a new profile that matches
130 // the optimized dex.
131 // The new profile will be subsequently used as the profile to dexpreopt the dex file.
Cole Fausteb032462024-09-19 11:12:54 -0700132 Enable_profile_rewriting proptools.Configurable[bool] `android:"replace_instead_of_append"`
Colin Cross43f08db2018-11-12 10:13:39 -0800133 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +0800134
135 Dex_preopt_result struct {
136 // True if profile-guided optimization is actually enabled.
137 Profile_guided bool
138 } `blueprint:"mutated"`
139}
140
141type ImportDexpreoptProperties struct {
142 Dex_preopt struct {
143 // If true, use the profile in the prebuilt APEX to guide optimization. Defaults to false.
144 Profile_guided *bool
145 }
Colin Cross43f08db2018-11-12 10:13:39 -0800146}
147
Ulya Trafimovich6cf2c0c2020-04-24 12:15:20 +0100148func init() {
149 dexpreopt.DexpreoptRunningInSoong = true
150}
151
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000152func isApexVariant(ctx android.BaseModuleContext) bool {
Colin Crossff694a82023-12-13 15:54:49 -0800153 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000154 return !apexInfo.IsForPlatform()
155}
156
Jiakai Zhang28bc9a82021-12-20 15:08:57 +0000157func forPrebuiltApex(ctx android.BaseModuleContext) bool {
Colin Crossff694a82023-12-13 15:54:49 -0800158 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Jiakai Zhang28bc9a82021-12-20 15:08:57 +0000159 return apexInfo.ForPrebuiltApex
160}
161
Spandan Dasa8afdcb2024-02-29 06:40:16 +0000162// For apex variant of modules, this returns true on the source variant if the prebuilt apex
163// has been selected using apex_contributions.
164// The prebuilt apex will be responsible for generating the dexpreopt rules of the deapexed java lib.
165func disableSourceApexVariant(ctx android.BaseModuleContext) bool {
166 if !isApexVariant(ctx) {
167 return false // platform variant
168 }
169 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
170 psi := android.PrebuiltSelectionInfoMap{}
Yu Liu7eebf8b2025-01-17 00:23:57 +0000171 ctx.VisitDirectDepsProxy(func(am android.ModuleProxy) {
Jihoon Kanga3a05462024-04-05 00:36:44 +0000172 if prebuiltSelectionInfo, ok := android.OtherModuleProvider(ctx, am, android.PrebuiltSelectionInfoProvider); ok {
173 psi = prebuiltSelectionInfo
174 }
Spandan Dasa8afdcb2024-02-29 06:40:16 +0000175 })
Spandan Das003452f2024-09-06 00:56:25 +0000176
Spandan Dasa8afdcb2024-02-29 06:40:16 +0000177 // Find the apex variant for this module
Colin Crossaf4c8562024-11-20 15:07:58 -0800178 apexVariants := []string{}
Spandan Dased7a0302024-08-26 18:06:25 +0000179 if apexInfo.BaseApexName != "" {
Colin Crossaf4c8562024-11-20 15:07:58 -0800180 apexVariants = append(apexVariants, apexInfo.BaseApexName)
Spandan Das003452f2024-09-06 00:56:25 +0000181 }
182 if apexInfo.ApexAvailableName != "" {
Colin Crossaf4c8562024-11-20 15:07:58 -0800183 apexVariants = append(apexVariants, apexInfo.ApexAvailableName)
Spandan Dased7a0302024-08-26 18:06:25 +0000184 }
Spandan Dasa8afdcb2024-02-29 06:40:16 +0000185 disableSource := false
186 // find the selected apexes
Colin Crossaf4c8562024-11-20 15:07:58 -0800187 for _, apexVariant := range apexVariants {
Spandan Dased7a0302024-08-26 18:06:25 +0000188 if len(psi.GetSelectedModulesForApiDomain(apexVariant)) > 0 {
189 // If the apex_contribution for this api domain is non-empty, disable the source variant
190 disableSource = true
Spandan Dasa8afdcb2024-02-29 06:40:16 +0000191 }
192 }
193 return disableSource
194}
195
Jiakai Zhangcf61e3c2023-05-08 16:28:38 +0000196// Returns whether dexpreopt is applicable to the module.
197// When it returns true, neither profile nor dexpreopt artifacts will be generated.
Spandan Dase21a8d42024-01-23 23:56:29 +0000198func (d *dexpreopter) dexpreoptDisabled(ctx android.BaseModuleContext, libName string) bool {
Colin Cross38310bb2021-12-01 10:34:14 -0800199 if !ctx.Device() {
Colin Cross43f08db2018-11-12 10:13:39 -0800200 return true
201 }
202
Colin Cross43f08db2018-11-12 10:13:39 -0800203 if d.isTest {
204 return true
205 }
206
Cole Fausteb032462024-09-19 11:12:54 -0700207 if !d.dexpreoptProperties.Dex_preopt.Enabled.GetOrDefault(ctx, true) {
Colin Cross43f08db2018-11-12 10:13:39 -0800208 return true
209 }
210
Spandan Das0727ba72024-02-13 16:37:43 +0000211 if d.shouldDisableDexpreopt {
212 return true
213 }
214
Jiakai Zhang28bc9a82021-12-20 15:08:57 +0000215 // If the module is from a prebuilt APEX, it shouldn't be installable, but it can still be
216 // dexpreopted.
217 if !ctx.Module().(DexpreopterInterface).IsInstallable() && !forPrebuiltApex(ctx) {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000218 return true
219 }
220
Colin Cross38310bb2021-12-01 10:34:14 -0800221 if !android.IsModulePreferred(ctx.Module()) {
222 return true
223 }
224
Spandan Dase21a8d42024-01-23 23:56:29 +0000225 if _, isApex := android.ModuleProvider(ctx, android.ApexBundleInfoProvider); isApex {
226 // dexpreopt rules for system server jars can be generated in the ModuleCtx of prebuilt apexes
227 return false
228 }
229
Colin Cross38310bb2021-12-01 10:34:14 -0800230 global := dexpreopt.GetGlobalConfig(ctx)
231
Spandan Dase21a8d42024-01-23 23:56:29 +0000232 // Use the libName argument to determine if the library being dexpreopt'd is a system server jar
233 // ctx.ModuleName() is not safe. In case of prebuilt apexes, the dexpreopt rules of system server jars
234 // are created in the ctx object of the top-level prebuilt apex.
235 isApexSystemServerJar := global.AllApexSystemServerJars(ctx).ContainsJar(libName)
236
237 if _, isApex := android.ModuleProvider(ctx, android.ApexBundleInfoProvider); isApex || isApexVariant(ctx) {
238 // dexpreopt rules for system server jars can be generated in the ModuleCtx of prebuilt apexes
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800239 if !isApexSystemServerJar {
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000240 return true
241 }
242 } else {
243 // Don't preopt the platform variant of an APEX system server jar to avoid conflicts.
Jiakai Zhang389a6472021-12-14 18:54:06 +0000244 if isApexSystemServerJar {
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000245 return true
246 }
Yo Chiangdbdf8f92020-01-09 19:00:27 +0800247 }
248
Colin Cross43f08db2018-11-12 10:13:39 -0800249 // TODO: contains no java code
250
251 return false
252}
253
Martin Stjernholm6d415272020-01-31 17:10:36 +0000254func dexpreoptToolDepsMutator(ctx android.BottomUpMutatorContext) {
Spandan Dase21a8d42024-01-23 23:56:29 +0000255 if _, isApex := android.ModuleProvider(ctx, android.ApexBundleInfoProvider); isApex && dexpreopt.IsDex2oatNeeded(ctx) {
256 // prebuilt apexes can genererate rules to dexpreopt deapexed jars
257 // Add a dex2oat dep aggressively on _every_ apex module
258 dexpreopt.RegisterToolDeps(ctx)
259 return
260 }
261 if d, ok := ctx.Module().(DexpreopterInterface); !ok || d.dexpreoptDisabled(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName())) || !dexpreopt.IsDex2oatNeeded(ctx) {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000262 return
263 }
264 dexpreopt.RegisterToolDeps(ctx)
265}
266
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000267// Returns the install path of the dex jar of a module.
268//
269// Do not rely on `ApexInfo.ApexVariationName` because it can be something like "apex1000", rather
270// than the `name` in the path `/apex/<name>` as suggested in its comment.
271//
272// This function is on a best-effort basis. It cannot handle the case where an APEX jar is not a
273// system server jar, which is fine because we currently only preopt system server jars for APEXes.
274func (d *dexpreopter) getInstallPath(
Spandan Dase21a8d42024-01-23 23:56:29 +0000275 ctx android.ModuleContext, libName string, defaultInstallPath android.InstallPath) android.InstallPath {
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000276 global := dexpreopt.GetGlobalConfig(ctx)
Spandan Dase21a8d42024-01-23 23:56:29 +0000277 if global.AllApexSystemServerJars(ctx).ContainsJar(libName) {
278 dexLocation := dexpreopt.GetSystemServerDexLocation(ctx, global, libName)
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000279 return android.PathForModuleInPartitionInstall(ctx, "", strings.TrimPrefix(dexLocation, "/"))
280 }
Spandan Dase21a8d42024-01-23 23:56:29 +0000281 if !d.dexpreoptDisabled(ctx, libName) && isApexVariant(ctx) &&
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000282 filepath.Base(defaultInstallPath.PartitionDir()) != "apex" {
283 ctx.ModuleErrorf("unable to get the install path of the dex jar for dexpreopt")
284 }
285 return defaultInstallPath
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000286}
287
Spandan Das2069c3f2023-12-06 19:40:24 +0000288// DexpreoptPrebuiltApexSystemServerJars generates the dexpreopt artifacts from a jar file that has been deapexed from a prebuilt apex
289func (d *Dexpreopter) DexpreoptPrebuiltApexSystemServerJars(ctx android.ModuleContext, libraryName string, di *android.DeapexerInfo) {
290 // A single prebuilt apex can have multiple apex system jars
291 // initialize the output path for this dex jar
292 dc := dexpreopt.GetGlobalConfig(ctx)
293 d.installPath = android.PathForModuleInPartitionInstall(ctx, "", strings.TrimPrefix(dexpreopt.GetSystemServerDexLocation(ctx, dc, libraryName), "/"))
294 // generate the rules for creating the .odex and .vdex files for this system server jar
Spandan Das5be63332023-12-13 00:06:32 +0000295 dexJarFile := di.PrebuiltExportPath(ApexRootRelativePathToJavaLib(libraryName))
Spandan Das419b3c62025-01-13 21:46:01 +0000296 if dexJarFile == nil {
297 ctx.ModuleErrorf(
298 `Could not find library %s in prebuilt apex %s.
299Please make sure that the value of PRODUCT_APEX_(SYSTEM_SERVER|STANDALONE_SYSTEM_SERVER)_JARS is correct`, libraryName, ctx.ModuleName())
300 }
Spandan Das2ea84dd2024-01-25 22:12:50 +0000301 d.inputProfilePathOnHost = nil // reset: TODO(spandandas): Make dexpreopter stateless
302 if android.InList(libraryName, di.GetDexpreoptProfileGuidedExportedModuleNames()) {
303 // Set the profile path to guide optimization
304 prof := di.PrebuiltExportPath(ApexRootRelativePathToJavaLib(libraryName) + ".prof")
305 if prof == nil {
306 ctx.ModuleErrorf("Could not find a .prof file in this prebuilt apex")
307 }
308 d.inputProfilePathOnHost = prof
309 }
310
Spandan Dase21a8d42024-01-23 23:56:29 +0000311 d.dexpreopt(ctx, libraryName, dexJarFile)
Spandan Das2069c3f2023-12-06 19:40:24 +0000312}
313
Colin Cross7707b242024-07-26 12:02:36 -0700314func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, libName string, dexJarFile android.Path) {
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000315 global := dexpreopt.GetGlobalConfig(ctx)
316
Martin Stjernholm6d415272020-01-31 17:10:36 +0000317 // TODO(b/148690468): The check on d.installPath is to bail out in cases where
318 // the dexpreopter struct hasn't been fully initialized before we're called,
319 // e.g. in aar.go. This keeps the behaviour that dexpreopting is effectively
320 // disabled, even if installable is true.
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000321 if d.installPath.Base() == "." {
322 return
323 }
324
325 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
326
Spandan Dase21a8d42024-01-23 23:56:29 +0000327 providesUsesLib := libName
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000328 if ulib, ok := ctx.Module().(ProvidesUsesLib); ok {
329 name := ulib.ProvidesUsesLib()
330 if name != nil {
331 providesUsesLib = *name
332 }
333 }
334
Jeongik Cha4b073cd2021-06-08 11:35:00 +0900335 // If it is test, make config files regardless of its dexpreopt setting.
Jeongik Chac6246672021-04-08 00:00:19 +0900336 // The config files are required for apps defined in make which depend on the lib.
Spandan Dase21a8d42024-01-23 23:56:29 +0000337 if d.isTest && d.dexpreoptDisabled(ctx, libName) {
Jaewoong Jung4b97a562020-12-17 09:43:28 -0800338 return
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000339 }
340
Spandan Dase21a8d42024-01-23 23:56:29 +0000341 isSystemServerJar := global.AllSystemServerJars(ctx).ContainsJar(libName)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000342
Colin Cross44df5812019-02-15 23:06:46 -0800343 bootImage := defaultBootImageConfig(ctx)
Jiakai Zhangb8796202023-03-06 19:16:48 +0000344 // When `global.PreoptWithUpdatableBcp` is true, `bcpForDexpreopt` below includes the mainline
345 // boot jars into bootclasspath, so we should include the mainline boot image as well because it's
346 // generated from those jars.
347 if global.PreoptWithUpdatableBcp {
348 bootImage = mainlineBootImageConfig(ctx)
349 }
Jiakai Zhang02669e82021-09-11 03:44:06 +0000350 dexFiles, dexLocations := bcpForDexpreopt(ctx, global.PreoptWithUpdatableBcp)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000351
David Srbeckyc177ebe2020-02-18 20:43:06 +0000352 targets := ctx.MultiTargets()
353 if len(targets) == 0 {
Colin Cross43f08db2018-11-12 10:13:39 -0800354 // assume this is a java library, dexpreopt for all arches for now
355 for _, target := range ctx.Config().Targets[android.Android] {
dimitry1f33e402019-03-26 12:39:31 +0100356 if target.NativeBridge == android.NativeBridgeDisabled {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000357 targets = append(targets, target)
dimitry1f33e402019-03-26 12:39:31 +0100358 }
Colin Cross43f08db2018-11-12 10:13:39 -0800359 }
Spandan Dase21a8d42024-01-23 23:56:29 +0000360 if isSystemServerJar && libName != "com.android.location.provider" {
Jiakai Zhang2fbc3552022-11-28 15:38:23 +0000361 // If the module is a system server jar, only preopt for the primary arch because the jar can
362 // only be loaded by system server. "com.android.location.provider" is a special case because
363 // it's also used by apps as a shared library.
David Srbeckyc177ebe2020-02-18 20:43:06 +0000364 targets = targets[:1]
Colin Cross43f08db2018-11-12 10:13:39 -0800365 }
366 }
Colin Cross43f08db2018-11-12 10:13:39 -0800367
David Srbeckyc177ebe2020-02-18 20:43:06 +0000368 var archs []android.ArchType
Colin Cross69f59a32019-02-15 10:39:37 -0800369 var images android.Paths
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000370 var imagesDeps []android.OutputPaths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000371 for _, target := range targets {
372 archs = append(archs, target.Arch.ArchType)
373 variant := bootImage.getVariant(target)
Jeongik Chaa5969092021-05-07 18:53:21 +0900374 images = append(images, variant.imagePathOnHost)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000375 imagesDeps = append(imagesDeps, variant.imagesDeps)
Colin Crossc7e40aa2019-02-08 21:37:00 -0800376 }
David Srbeckyab994982020-03-30 17:24:13 +0100377 // The image locations for all Android variants are identical.
Jeongik Cha4dda75e2021-04-27 23:56:44 +0900378 hostImageLocations, deviceImageLocations := bootImage.getAnyAndroidVariant().imageLocations()
Colin Crossc7e40aa2019-02-08 21:37:00 -0800379
Colin Cross43f08db2018-11-12 10:13:39 -0800380 var profileClassListing android.OptionalPath
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100381 var profileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800382 profileIsTextListing := false
Spandan Das2ea84dd2024-01-25 22:12:50 +0000383
Jiakai Zhang81e46812023-02-08 21:56:07 +0800384 if d.inputProfilePathOnHost != nil {
385 profileClassListing = android.OptionalPathForPath(d.inputProfilePathOnHost)
Cole Fausteb032462024-09-19 11:12:54 -0700386 } else if d.dexpreoptProperties.Dex_preopt.Profile_guided.GetOrDefault(ctx, true) && !forPrebuiltApex(ctx) {
Spandan Das3dbda182024-05-20 22:23:10 +0000387 // If enable_profile_rewriting is set, use the rewritten profile instead of the checked-in profile
Cole Fausteb032462024-09-19 11:12:54 -0700388 if d.EnableProfileRewriting(ctx) {
Spandan Das3dbda182024-05-20 22:23:10 +0000389 profileClassListing = android.OptionalPathForPath(d.GetRewrittenProfile())
390 profileIsTextListing = true
Cole Fausteb032462024-09-19 11:12:54 -0700391 } else if profile := d.GetProfile(ctx); profile != "" {
Spandan Das3dbda182024-05-20 22:23:10 +0000392 // If dex_preopt.profile_guided is not set, default it based on the existence of the
393 // dexprepot.profile option or the profile class listing.
Colin Cross43f08db2018-11-12 10:13:39 -0800394 profileClassListing = android.OptionalPathForPath(
Spandan Das3dbda182024-05-20 22:23:10 +0000395 android.PathForModuleSrc(ctx, profile))
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100396 profileBootListing = android.ExistentPathForSource(ctx,
Spandan Das3dbda182024-05-20 22:23:10 +0000397 ctx.ModuleDir(), profile+"-boot")
Colin Cross43f08db2018-11-12 10:13:39 -0800398 profileIsTextListing = true
Dan Willemsen78d51b02020-06-24 16:33:31 -0700399 } else if global.ProfileDir != "" {
Colin Cross43f08db2018-11-12 10:13:39 -0800400 profileClassListing = android.ExistentPathForSource(ctx,
Spandan Dase21a8d42024-01-23 23:56:29 +0000401 global.ProfileDir, libName+".prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800402 }
403 }
404
Jiakai Zhang9c4dc192023-02-09 00:09:24 +0800405 d.dexpreoptProperties.Dex_preopt_result.Profile_guided = profileClassListing.Valid()
406
Spandan Das2069c3f2023-12-06 19:40:24 +0000407 // A single apex can have multiple system server jars
408 // Use the dexJar to create a unique scope for each
409 dexJarStem := strings.TrimSuffix(dexJarFile.Base(), dexJarFile.Ext())
410
Cole Fausteb032462024-09-19 11:12:54 -0700411 appImage := d.dexpreoptProperties.Dex_preopt.App_image.Get(ctx)
412
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000413 // Full dexpreopt config, used to create dexpreopt build rules.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000414 dexpreoptConfig := &dexpreopt.ModuleConfig{
Spandan Dase21a8d42024-01-23 23:56:29 +0000415 Name: libName,
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800416 DexLocation: dexLocation,
Spandan Dase21a8d42024-01-23 23:56:29 +0000417 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", dexJarStem, libName+".jar").OutputPath,
Colin Cross69f59a32019-02-15 10:39:37 -0800418 DexPath: dexJarFile,
Jeongik Cha33a3a812021-04-15 09:12:49 +0900419 ManifestPath: android.OptionalPathForPath(d.manifestFile),
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800420 UncompressedDex: d.uncompressedDex,
421 HasApkLibraries: false,
422 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800423
Colin Cross69f59a32019-02-15 10:39:37 -0800424 ProfileClassListing: profileClassListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800425 ProfileIsTextListing: profileIsTextListing,
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100426 ProfileBootListing: profileBootListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800427
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +0000428 EnforceUsesLibrariesStatusFile: dexpreopt.UsesLibrariesStatusFile(ctx),
429 EnforceUsesLibraries: d.enforceUsesLibs,
430 ProvidesUsesLibrary: providesUsesLib,
431 ClassLoaderContexts: d.classLoaderContexts,
Colin Cross43f08db2018-11-12 10:13:39 -0800432
Jeongik Cha4dda75e2021-04-27 23:56:44 +0900433 Archs: archs,
434 DexPreoptImagesDeps: imagesDeps,
435 DexPreoptImageLocationsOnHost: hostImageLocations,
436 DexPreoptImageLocationsOnDevice: deviceImageLocations,
Colin Cross43f08db2018-11-12 10:13:39 -0800437
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000438 PreoptBootClassPathDexFiles: dexFiles.Paths(),
Vladimir Marko40139d62020-02-06 15:14:29 +0000439 PreoptBootClassPathDexLocations: dexLocations,
Colin Cross800fe132019-02-11 14:21:24 -0800440
Cole Fausteb032462024-09-19 11:12:54 -0700441 NoCreateAppImage: !appImage.GetOrDefault(true),
442 ForceCreateAppImage: appImage.GetOrDefault(false),
Colin Cross43f08db2018-11-12 10:13:39 -0800443
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700444 PresignedPrebuilt: d.isPresignedPrebuilt,
Colin Cross43f08db2018-11-12 10:13:39 -0800445 }
446
Spandan Das950deca2024-10-01 18:35:23 +0000447 if ctx.Config().InstallApexSystemServerDexpreoptSamePartition() {
448 dexpreoptConfig.ApexPartition = android.PathForModuleInstall(ctx).Partition()
449 } else {
450 dexpreoptConfig.ApexPartition = "system"
451 }
452
Spandan Das2069c3f2023-12-06 19:40:24 +0000453 d.configPath = android.PathForModuleOut(ctx, "dexpreopt", dexJarStem, "dexpreopt.config")
Jeongik Chac6246672021-04-08 00:00:19 +0900454 dexpreopt.WriteModuleConfig(ctx, dexpreoptConfig, d.configPath)
Colin Crossa6182ab2024-08-21 10:47:44 -0700455 ctx.CheckbuildFile(d.configPath)
Jeongik Chac6246672021-04-08 00:00:19 +0900456
Spandan Dase21a8d42024-01-23 23:56:29 +0000457 if d.dexpreoptDisabled(ctx, libName) {
Jeongik Chac6246672021-04-08 00:00:19 +0900458 return
459 }
460
461 globalSoong := dexpreopt.GetGlobalSoongConfig(ctx)
462
Jiakai Zhang51b2a8b2023-06-26 16:47:38 +0100463 // The root "product_packages.txt" is generated by `build/make/core/Makefile`. It contains a list
464 // of all packages that are installed on the device. We use `grep` to filter the list by the app's
465 // dependencies to create a per-app list, and use `rsync --checksum` to prevent the file's mtime
466 // from being changed if the contents don't change. This avoids unnecessary dexpreopt reruns.
Jiakai Zhanga4496782023-05-17 16:57:30 +0100467 productPackages := android.PathForModuleInPartitionInstall(ctx, "", "product_packages.txt")
Spandan Das2069c3f2023-12-06 19:40:24 +0000468 appProductPackages := android.PathForModuleOut(ctx, "dexpreopt", dexJarStem, "product_packages.txt")
Jiakai Zhang51b2a8b2023-06-26 16:47:38 +0100469 appProductPackagesStaging := appProductPackages.ReplaceExtension(ctx, "txt.tmp")
470 clcNames, _ := dexpreopt.ComputeClassLoaderContextDependencies(dexpreoptConfig.ClassLoaderContexts)
471 sort.Strings(clcNames) // The order needs to be deterministic.
472 productPackagesRule := android.NewRuleBuilder(pctx, ctx)
473 if len(clcNames) > 0 {
474 productPackagesRule.Command().
475 Text("grep -F -x").
476 FlagForEachArg("-e ", clcNames).
477 Input(productPackages).
478 FlagWithOutput("> ", appProductPackagesStaging).
479 Text("|| true")
480 } else {
481 productPackagesRule.Command().
482 Text("rm -f").Output(appProductPackagesStaging).
483 Text("&&").
484 Text("touch").Output(appProductPackagesStaging)
485 }
486 productPackagesRule.Command().
487 Text("rsync --checksum").
488 Input(appProductPackagesStaging).
489 Output(appProductPackages)
Spandan Das2069c3f2023-12-06 19:40:24 +0000490 productPackagesRule.Restat().Build("product_packages."+dexJarStem, "dexpreopt product_packages")
Jiakai Zhanga4496782023-05-17 16:57:30 +0100491
492 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(
Colin Cross388c6612025-01-28 14:00:12 -0800493 ctx, globalSoong, global, dexpreoptConfig, appProductPackages)
Colin Cross43f08db2018-11-12 10:13:39 -0800494 if err != nil {
495 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
Jaewoong Jung4b97a562020-12-17 09:43:28 -0800496 return
Colin Cross43f08db2018-11-12 10:13:39 -0800497 }
498
Spandan Das2069c3f2023-12-06 19:40:24 +0000499 dexpreoptRule.Build("dexpreopt"+"."+dexJarStem, "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800500
Spandan Das2069c3f2023-12-06 19:40:24 +0000501 // The current ctx might be of a deapexer module created by a prebuilt apex
502 // Use the path of the dex file to determine the library name
503 isApexSystemServerJar := global.AllApexSystemServerJars(ctx).ContainsJar(dexJarStem)
Jiakai Zhang389a6472021-12-14 18:54:06 +0000504
Justin Yun613bdc52024-06-12 21:32:10 +0900505 dexpreoptPartition := d.installPath.Partition()
506 // dexpreoptPartition is set to empty for dexpreopts of system APEX and system_other.
507 // In case of system APEX, however, we can set it to "system" manually.
508 // TODO(b/346662300): Let dexpreopter generate the installPath for dexpreopt files instead of
509 // using the dex location to generate the installPath.
510 if isApexSystemServerJar {
Spandan Das906222c2024-10-17 18:29:54 +0000511 dexpreoptPartition = dexpreoptConfig.ApexPartition
Justin Yun613bdc52024-06-12 21:32:10 +0900512 }
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700513 for _, install := range dexpreoptRule.Installs() {
514 // Remove the "/" prefix because the path should be relative to $ANDROID_PRODUCT_OUT.
515 installDir := strings.TrimPrefix(filepath.Dir(install.To), "/")
Justin Yun613bdc52024-06-12 21:32:10 +0900516 partition := dexpreoptPartition
Justin Yun22c8aca2024-06-05 20:25:03 +0900517 if strings.HasPrefix(installDir, partition+"/") {
518 installDir = strings.TrimPrefix(installDir, partition+"/")
519 } else {
520 // If the partition for the installDir is different from the install partition, set the
521 // partition empty to install the dexpreopt files to the desired partition.
522 // TODO(b/346439786): Define and use the dexpreopt module type to avoid this mismatch.
523 partition = ""
524 }
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700525 installBase := filepath.Base(install.To)
Justin Yun22c8aca2024-06-05 20:25:03 +0900526 installPath := android.PathForModuleInPartitionInstall(ctx, partition, installDir)
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800527 isProfile := strings.HasSuffix(installBase, ".prof")
528
529 if isProfile {
Jiakai Zhang81e46812023-02-08 21:56:07 +0800530 d.outputProfilePathOnHost = install.From
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800531 }
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700532
Jiakai Zhang389a6472021-12-14 18:54:06 +0000533 if isApexSystemServerJar {
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800534 // Profiles are handled separately because they are installed into the APEX.
535 if !isProfile {
536 // APEX variants of java libraries are hidden from Make, so their dexpreopt
537 // outputs need special handling. Currently, for APEX variants of java
538 // libraries, only those in the system server classpath are handled here.
539 // Preopting of boot classpath jars in the ART APEX are handled in
540 // java/dexpreopt_bootjars.go, and other APEX jars are not preopted.
Colin Cross388c6612025-01-28 14:00:12 -0800541 // The installs will be handled the apex module that includes this library.
542 di := DexpreopterInstall{
543 OutputPathOnHost: install.From,
544 InstallDirOnDevice: installPath,
545 InstallFileOnDevice: installBase,
Spandan Das746161d2024-08-21 22:47:53 +0000546 }
Colin Cross388c6612025-01-28 14:00:12 -0800547 d.apexSystemServerDexpreoptInstalls = append(d.apexSystemServerDexpreoptInstalls, di)
Spandan Das746161d2024-08-21 22:47:53 +0000548
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800549 }
Colin Crossfa9bfcd2021-11-10 16:42:38 -0800550 } else if !d.preventInstall {
Colin Crossa6182ab2024-08-21 10:47:44 -0700551 // Install without adding to checkbuild to match behavior of previous Make-based checkbuild rules
552 ctx.InstallFileWithoutCheckbuild(installPath, installBase, install.From)
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000553 }
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700554 }
555
Colin Cross388c6612025-01-28 14:00:12 -0800556 if isApexSystemServerJar {
557 // Store the dex jar location for system server jars in apexes, the apex will copy the file into
558 // a known location for dex2oat.
559 d.apexSystemServerDexJars = append(d.apexSystemServerDexJars, dexJarFile)
560 } else if isSystemServerJar && !d.preventInstall {
561 // Copy the dex jar into a known location for dex2oat for non-apex system server jars.
562 android.CopyFileRule(ctx, dexJarFile, android.PathForOutput(ctx, dexpreopt.SystemServerDexjarsDir, dexJarFile.Base()))
563 }
564
Jiakai Zhang389a6472021-12-14 18:54:06 +0000565 if !isApexSystemServerJar {
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000566 d.builtInstalled = dexpreoptRule.Installs().String()
567 }
Colin Cross6029aed2025-02-18 14:55:07 -0800568
569 if isSystemServerJar {
570 checkSystemServerOrder(ctx, libName)
571 }
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000572}
573
Justin Yun22c8aca2024-06-05 20:25:03 +0900574func getModuleInstallPathInfo(ctx android.ModuleContext, fullInstallPath string) (android.InstallPath, string, string) {
575 installPath := android.PathForModuleInstall(ctx)
576 installDir, installBase := filepath.Split(strings.TrimPrefix(fullInstallPath, "/"))
577
578 if !strings.HasPrefix(installDir, installPath.Partition()+"/") {
579 // Return empty filename if the install partition is not for the target image.
580 return installPath, "", ""
581 }
582 relDir, err := filepath.Rel(installPath.Partition(), installDir)
583 if err != nil {
584 panic(err)
585 }
586 return installPath, relDir, installBase
587}
588
Spandan Das29207b52024-07-30 23:28:17 +0000589// installFile will install the file if `install` path and the target install partition are the same.
590func installFile(ctx android.ModuleContext, install android.RuleBuilderInstall) {
Justin Yun22c8aca2024-06-05 20:25:03 +0900591 installPath, relDir, name := getModuleInstallPathInfo(ctx, install.To)
592 // Empty name means the install partition is not for the target image.
593 // For the system image, files for "apex" and "system_other" are skipped here.
594 // The skipped "apex" files are for testing only, for example,
595 // "/apex/art_boot_images/javalib/x86/boot.vdex".
596 // TODO(b/320196894): Files for "system_other" are skipped because soong creates the system
597 // image only for now.
598 if name != "" {
Spandan Das29207b52024-07-30 23:28:17 +0000599 ctx.InstallFile(installPath.Join(ctx, relDir), name, install.From)
Justin Yun22c8aca2024-06-05 20:25:03 +0900600 }
601}
602
Colin Cross388c6612025-01-28 14:00:12 -0800603func (d *dexpreopter) ApexSystemServerDexpreoptInstalls() []DexpreopterInstall {
604 return d.apexSystemServerDexpreoptInstalls
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000605}
606
Colin Cross388c6612025-01-28 14:00:12 -0800607func (d *dexpreopter) ApexSystemServerDexJars() android.Paths {
608 return d.apexSystemServerDexJars
Jihoon Kangd4063812025-01-24 00:25:30 +0000609}
610
Jiakai Zhang81e46812023-02-08 21:56:07 +0800611func (d *dexpreopter) OutputProfilePathOnHost() android.Path {
612 return d.outputProfilePathOnHost
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800613}
Spandan Das0727ba72024-02-13 16:37:43 +0000614
615func (d *dexpreopter) disableDexpreopt() {
616 d.shouldDisableDexpreopt = true
617}
Spandan Das3dbda182024-05-20 22:23:10 +0000618
Cole Fausteb032462024-09-19 11:12:54 -0700619func (d *dexpreopter) EnableProfileRewriting(ctx android.BaseModuleContext) bool {
620 return d.dexpreoptProperties.Dex_preopt.Enable_profile_rewriting.GetOrDefault(ctx, false)
Spandan Das3dbda182024-05-20 22:23:10 +0000621}
622
Cole Fausteb032462024-09-19 11:12:54 -0700623func (d *dexpreopter) GetProfile(ctx android.BaseModuleContext) string {
624 return d.dexpreoptProperties.Dex_preopt.Profile.GetOrDefault(ctx, "")
Spandan Das3dbda182024-05-20 22:23:10 +0000625}
626
Cole Fausteb032462024-09-19 11:12:54 -0700627func (d *dexpreopter) GetProfileGuided(ctx android.BaseModuleContext) bool {
628 return d.dexpreoptProperties.Dex_preopt.Profile_guided.GetOrDefault(ctx, false)
Spandan Das3dbda182024-05-20 22:23:10 +0000629}
630
631func (d *dexpreopter) GetRewrittenProfile() android.Path {
632 return d.rewrittenProfile
633}
634
635func (d *dexpreopter) SetRewrittenProfile(p android.Path) {
636 d.rewrittenProfile = p
637}
Colin Cross6029aed2025-02-18 14:55:07 -0800638
639// Check the order of jars on the system server classpath and give a warning/error if a jar precedes
640// one of its dependencies. This is not an error, but a missed optimization, as dexpreopt won't
641// have the dependency jar in the class loader context, and it won't be able to resolve any
642// references to its classes and methods.
643func checkSystemServerOrder(ctx android.ModuleContext, libName string) {
644 config := dexpreopt.GetGlobalConfig(ctx)
645 jars := config.AllSystemServerClasspathJars(ctx)
646 jarIndex := config.AllSystemServerJars(ctx).IndexOfJar(libName)
647 ctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
648 tag := ctx.OtherModuleDependencyTag(dep)
649 // Ideally this should only be walking relevant dependencies, but to maintain existing behavior
650 // for now just exclude any known irrelevant dependencies that would lead to incorrect errors.
651 if _, ok := tag.(bootclasspathDependencyTag); ok {
652 return false
653 }
654 depIndex := jars.IndexOfJar(dep.Name())
655 if jarIndex < depIndex && !config.BrokenSuboptimalOrderOfSystemServerJars {
656 jar := jars.Jar(jarIndex)
657 dep := jars.Jar(depIndex)
658 ctx.ModuleErrorf("non-optimal order of jars on the system server classpath:"+
659 " '%s' precedes its dependency '%s', so dexpreopt is unable to resolve any"+
660 " references from '%s' to '%s'.\n", jar, dep, jar, dep)
661 }
662 return true
663 })
664}