blob: 0ffedf6c407151fd7670d3e467e8a94d8c0814ed [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 {
Jiakai Zhang81e46812023-02-08 21:56:07 +080026 // True if the java module is to be dexed and installed on devices.
27 // Structs that embed dexpreopter must implement this.
28 IsInstallable() bool
29
30 // True if dexpreopt is disabled for the java module.
Martin Stjernholm6d415272020-01-31 17:10:36 +000031 dexpreoptDisabled(ctx android.BaseModuleContext) bool
Jiakai Zhang81e46812023-02-08 21:56:07 +080032
33 // If the java module is to be installed into an APEX, this list contains information about the
34 // dexpreopt outputs to be installed on devices. Note that these dexpreopt outputs are installed
35 // outside of the APEX.
Jiakai Zhangca9bc982021-09-09 08:09:41 +000036 DexpreoptBuiltInstalledForApex() []dexpreopterInstall
Jiakai Zhang81e46812023-02-08 21:56:07 +080037
38 // The Make entries to install the dexpreopt outputs. Derived from
39 // `DexpreoptBuiltInstalledForApex`.
Jiakai Zhangca9bc982021-09-09 08:09:41 +000040 AndroidMkEntriesForApex() []android.AndroidMkEntries
Jiakai Zhang81e46812023-02-08 21:56:07 +080041
42 // See `dexpreopter.outputProfilePathOnHost`.
43 OutputProfilePathOnHost() android.Path
Jiakai Zhangca9bc982021-09-09 08:09:41 +000044}
45
46type dexpreopterInstall struct {
47 // A unique name to distinguish an output from others for the same java library module. Usually in
48 // the form of `<arch>-<encoded-path>.odex/vdex/art`.
49 name string
50
51 // The name of the input java module.
52 moduleName string
53
54 // The path to the dexpreopt output on host.
55 outputPathOnHost android.Path
56
57 // The directory on the device for the output to install to.
58 installDirOnDevice android.InstallPath
59
60 // The basename (the last segment of the path) for the output to install as.
61 installFileOnDevice string
62}
63
64// The full module name of the output in the makefile.
65func (install *dexpreopterInstall) FullModuleName() string {
66 return install.moduleName + install.SubModuleName()
67}
68
69// The sub-module name of the output in the makefile (the name excluding the java module name).
70func (install *dexpreopterInstall) SubModuleName() string {
71 return "-dexpreopt-" + install.name
Martin Stjernholm6d415272020-01-31 17:10:36 +000072}
73
Jiakai Zhang6decef92022-01-12 17:56:19 +000074// Returns Make entries for installing the file.
75//
76// This function uses a value receiver rather than a pointer receiver to ensure that the object is
77// safe to use in `android.AndroidMkExtraEntriesFunc`.
78func (install dexpreopterInstall) ToMakeEntries() android.AndroidMkEntries {
79 return android.AndroidMkEntries{
80 Class: "ETC",
81 SubName: install.SubModuleName(),
82 OutputFile: android.OptionalPathForPath(install.outputPathOnHost),
83 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
84 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
85 entries.SetString("LOCAL_MODULE_PATH", install.installDirOnDevice.String())
86 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", install.installFileOnDevice)
87 entries.SetString("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", "false")
88 },
89 },
90 }
91}
92
Colin Cross43f08db2018-11-12 10:13:39 -080093type dexpreopter struct {
Jiakai Zhang9c4dc192023-02-09 00:09:24 +080094 dexpreoptProperties DexpreoptProperties
95 importDexpreoptProperties ImportDexpreoptProperties
Colin Cross43f08db2018-11-12 10:13:39 -080096
Colin Cross70dda7e2019-10-01 22:05:35 -070097 installPath android.InstallPath
Jaewoong Jungccbb3932019-04-15 09:48:31 -070098 uncompressedDex bool
99 isSDKLibrary bool
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000100 isApp bool
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700101 isTest bool
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700102 isPresignedPrebuilt bool
Colin Crossfa9bfcd2021-11-10 16:42:38 -0800103 preventInstall bool
Colin Cross43f08db2018-11-12 10:13:39 -0800104
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000105 manifestFile android.Path
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +0000106 statusFile android.WritablePath
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000107 enforceUsesLibs bool
108 classLoaderContexts dexpreopt.ClassLoaderContextMap
Colin Cross50ddcc42019-05-16 12:28:22 -0700109
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000110 // See the `dexpreopt` function for details.
111 builtInstalled string
112 builtInstalledForApex []dexpreopterInstall
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000113
Jeongik Chac6246672021-04-08 00:00:19 +0900114 // The config is used for two purposes:
115 // - Passing dexpreopt information about libraries from Soong to Make. This is needed when
116 // a <uses-library> is defined in Android.bp, but used in Android.mk (see dex_preopt_config_merger.py).
117 // Note that dexpreopt.config might be needed even if dexpreopt is disabled for the library itself.
118 // - Dexpreopt post-processing (using dexpreopt artifacts from a prebuilt system image to incrementally
119 // dexpreopt another partition).
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000120 configPath android.WritablePath
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800121
Jiakai Zhang81e46812023-02-08 21:56:07 +0800122 // The path to the profile on host that dexpreopter generates. This is used as the input for
123 // dex2oat.
124 outputProfilePathOnHost android.Path
125
126 // The path to the profile that dexpreopter accepts. It must be in the binary format. If this is
127 // set, it overrides the profile settings in `dexpreoptProperties`.
128 inputProfilePathOnHost android.Path
Colin Cross43f08db2018-11-12 10:13:39 -0800129}
130
131type DexpreoptProperties struct {
132 Dex_preopt struct {
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +0100133 // If false, prevent dexpreopting. Defaults to true.
Colin Cross43f08db2018-11-12 10:13:39 -0800134 Enabled *bool
135
136 // If true, generate an app image (.art file) for this module.
137 App_image *bool
138
139 // If true, use a checked-in profile to guide optimization. Defaults to false unless
140 // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR
141 // that matches the name of this module, in which case it is defaulted to true.
142 Profile_guided *bool
143
144 // If set, provides the path to profile relative to the Android.bp file. If not set,
145 // defaults to searching for a file that matches the name of this module in the default
146 // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found.
Colin Crossde4e4e62019-04-26 10:52:32 -0700147 Profile *string `android:"path"`
Colin Cross43f08db2018-11-12 10:13:39 -0800148 }
Jiakai Zhang9c4dc192023-02-09 00:09:24 +0800149
150 Dex_preopt_result struct {
151 // True if profile-guided optimization is actually enabled.
152 Profile_guided bool
153 } `blueprint:"mutated"`
154}
155
156type ImportDexpreoptProperties struct {
157 Dex_preopt struct {
158 // If true, use the profile in the prebuilt APEX to guide optimization. Defaults to false.
159 Profile_guided *bool
160 }
Colin Cross43f08db2018-11-12 10:13:39 -0800161}
162
Ulya Trafimovich6cf2c0c2020-04-24 12:15:20 +0100163func init() {
164 dexpreopt.DexpreoptRunningInSoong = true
165}
166
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000167func isApexVariant(ctx android.BaseModuleContext) bool {
168 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
169 return !apexInfo.IsForPlatform()
170}
171
Jiakai Zhang28bc9a82021-12-20 15:08:57 +0000172func forPrebuiltApex(ctx android.BaseModuleContext) bool {
173 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
174 return apexInfo.ForPrebuiltApex
175}
176
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000177func moduleName(ctx android.BaseModuleContext) string {
178 // Remove the "prebuilt_" prefix if the module is from a prebuilt because the prefix is not
179 // expected by dexpreopter.
180 return android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName())
181}
182
Martin Stjernholm6d415272020-01-31 17:10:36 +0000183func (d *dexpreopter) dexpreoptDisabled(ctx android.BaseModuleContext) bool {
Colin Cross38310bb2021-12-01 10:34:14 -0800184 if !ctx.Device() {
Colin Cross43f08db2018-11-12 10:13:39 -0800185 return true
186 }
187
Colin Cross43f08db2018-11-12 10:13:39 -0800188 if d.isTest {
189 return true
190 }
191
192 if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) {
193 return true
194 }
195
Jiakai Zhang28bc9a82021-12-20 15:08:57 +0000196 // If the module is from a prebuilt APEX, it shouldn't be installable, but it can still be
197 // dexpreopted.
198 if !ctx.Module().(DexpreopterInterface).IsInstallable() && !forPrebuiltApex(ctx) {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000199 return true
200 }
201
Colin Cross38310bb2021-12-01 10:34:14 -0800202 if !android.IsModulePreferred(ctx.Module()) {
203 return true
204 }
205
206 global := dexpreopt.GetGlobalConfig(ctx)
207
208 if global.DisablePreopt {
209 return true
210 }
211
212 if inList(moduleName(ctx), global.DisablePreoptModules) {
Colin Crossdc2da912019-01-05 22:13:05 -0800213 return true
214 }
215
Jiakai Zhang389a6472021-12-14 18:54:06 +0000216 isApexSystemServerJar := global.AllApexSystemServerJars(ctx).ContainsJar(moduleName(ctx))
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000217 if isApexVariant(ctx) {
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800218 // Don't preopt APEX variant module unless the module is an APEX system server jar.
219 if !isApexSystemServerJar {
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000220 return true
221 }
222 } else {
223 // Don't preopt the platform variant of an APEX system server jar to avoid conflicts.
Jiakai Zhang389a6472021-12-14 18:54:06 +0000224 if isApexSystemServerJar {
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000225 return true
226 }
Yo Chiangdbdf8f92020-01-09 19:00:27 +0800227 }
228
Colin Cross43f08db2018-11-12 10:13:39 -0800229 // TODO: contains no java code
230
231 return false
232}
233
Martin Stjernholm6d415272020-01-31 17:10:36 +0000234func dexpreoptToolDepsMutator(ctx android.BottomUpMutatorContext) {
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000235 if d, ok := ctx.Module().(DexpreopterInterface); !ok || d.dexpreoptDisabled(ctx) {
Martin Stjernholm6d415272020-01-31 17:10:36 +0000236 return
237 }
238 dexpreopt.RegisterToolDeps(ctx)
239}
240
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000241func (d *dexpreopter) odexOnSystemOther(ctx android.ModuleContext, installPath android.InstallPath) bool {
242 return dexpreopt.OdexOnSystemOtherByName(moduleName(ctx), android.InstallPathToOnDevicePath(ctx, installPath), dexpreopt.GetGlobalConfig(ctx))
243}
244
245// Returns the install path of the dex jar of a module.
246//
247// Do not rely on `ApexInfo.ApexVariationName` because it can be something like "apex1000", rather
248// than the `name` in the path `/apex/<name>` as suggested in its comment.
249//
250// This function is on a best-effort basis. It cannot handle the case where an APEX jar is not a
251// system server jar, which is fine because we currently only preopt system server jars for APEXes.
252func (d *dexpreopter) getInstallPath(
253 ctx android.ModuleContext, defaultInstallPath android.InstallPath) android.InstallPath {
254 global := dexpreopt.GetGlobalConfig(ctx)
Jiakai Zhang389a6472021-12-14 18:54:06 +0000255 if global.AllApexSystemServerJars(ctx).ContainsJar(moduleName(ctx)) {
256 dexLocation := dexpreopt.GetSystemServerDexLocation(ctx, global, moduleName(ctx))
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000257 return android.PathForModuleInPartitionInstall(ctx, "", strings.TrimPrefix(dexLocation, "/"))
258 }
259 if !d.dexpreoptDisabled(ctx) && isApexVariant(ctx) &&
260 filepath.Base(defaultInstallPath.PartitionDir()) != "apex" {
261 ctx.ModuleErrorf("unable to get the install path of the dex jar for dexpreopt")
262 }
263 return defaultInstallPath
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000264}
265
Paul Duffin612e6102021-02-02 13:38:13 +0000266func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.WritablePath) {
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000267 global := dexpreopt.GetGlobalConfig(ctx)
268
Martin Stjernholm6d415272020-01-31 17:10:36 +0000269 // TODO(b/148690468): The check on d.installPath is to bail out in cases where
270 // the dexpreopter struct hasn't been fully initialized before we're called,
271 // e.g. in aar.go. This keeps the behaviour that dexpreopting is effectively
272 // disabled, even if installable is true.
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000273 if d.installPath.Base() == "." {
274 return
275 }
276
277 dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath)
278
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000279 providesUsesLib := moduleName(ctx)
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000280 if ulib, ok := ctx.Module().(ProvidesUsesLib); ok {
281 name := ulib.ProvidesUsesLib()
282 if name != nil {
283 providesUsesLib = *name
284 }
285 }
286
Jeongik Cha4b073cd2021-06-08 11:35:00 +0900287 // If it is test, make config files regardless of its dexpreopt setting.
Jeongik Chac6246672021-04-08 00:00:19 +0900288 // The config files are required for apps defined in make which depend on the lib.
Jeongik Cha4b073cd2021-06-08 11:35:00 +0900289 if d.isTest && d.dexpreoptDisabled(ctx) {
Jaewoong Jung4b97a562020-12-17 09:43:28 -0800290 return
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000291 }
292
Jiakai Zhang389a6472021-12-14 18:54:06 +0000293 isSystemServerJar := global.AllSystemServerJars(ctx).ContainsJar(moduleName(ctx))
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000294
Colin Cross44df5812019-02-15 23:06:46 -0800295 bootImage := defaultBootImageConfig(ctx)
Jiakai Zhangb8796202023-03-06 19:16:48 +0000296 // When `global.PreoptWithUpdatableBcp` is true, `bcpForDexpreopt` below includes the mainline
297 // boot jars into bootclasspath, so we should include the mainline boot image as well because it's
298 // generated from those jars.
299 if global.PreoptWithUpdatableBcp {
300 bootImage = mainlineBootImageConfig(ctx)
301 }
Jiakai Zhang02669e82021-09-11 03:44:06 +0000302 dexFiles, dexLocations := bcpForDexpreopt(ctx, global.PreoptWithUpdatableBcp)
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000303
David Srbeckyc177ebe2020-02-18 20:43:06 +0000304 targets := ctx.MultiTargets()
305 if len(targets) == 0 {
Colin Cross43f08db2018-11-12 10:13:39 -0800306 // assume this is a java library, dexpreopt for all arches for now
307 for _, target := range ctx.Config().Targets[android.Android] {
dimitry1f33e402019-03-26 12:39:31 +0100308 if target.NativeBridge == android.NativeBridgeDisabled {
David Srbeckyc177ebe2020-02-18 20:43:06 +0000309 targets = append(targets, target)
dimitry1f33e402019-03-26 12:39:31 +0100310 }
Colin Cross43f08db2018-11-12 10:13:39 -0800311 }
Jiakai Zhang2fbc3552022-11-28 15:38:23 +0000312 if isSystemServerJar && moduleName(ctx) != "com.android.location.provider" {
313 // If the module is a system server jar, only preopt for the primary arch because the jar can
314 // only be loaded by system server. "com.android.location.provider" is a special case because
315 // it's also used by apps as a shared library.
David Srbeckyc177ebe2020-02-18 20:43:06 +0000316 targets = targets[:1]
Colin Cross43f08db2018-11-12 10:13:39 -0800317 }
318 }
Colin Cross43f08db2018-11-12 10:13:39 -0800319
David Srbeckyc177ebe2020-02-18 20:43:06 +0000320 var archs []android.ArchType
Colin Cross69f59a32019-02-15 10:39:37 -0800321 var images android.Paths
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000322 var imagesDeps []android.OutputPaths
David Srbeckyc177ebe2020-02-18 20:43:06 +0000323 for _, target := range targets {
324 archs = append(archs, target.Arch.ArchType)
325 variant := bootImage.getVariant(target)
Jeongik Chaa5969092021-05-07 18:53:21 +0900326 images = append(images, variant.imagePathOnHost)
David Srbeckyc177ebe2020-02-18 20:43:06 +0000327 imagesDeps = append(imagesDeps, variant.imagesDeps)
Colin Crossc7e40aa2019-02-08 21:37:00 -0800328 }
David Srbeckyab994982020-03-30 17:24:13 +0100329 // The image locations for all Android variants are identical.
Jeongik Cha4dda75e2021-04-27 23:56:44 +0900330 hostImageLocations, deviceImageLocations := bootImage.getAnyAndroidVariant().imageLocations()
Colin Crossc7e40aa2019-02-08 21:37:00 -0800331
Colin Cross43f08db2018-11-12 10:13:39 -0800332 var profileClassListing android.OptionalPath
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100333 var profileBootListing android.OptionalPath
Colin Cross43f08db2018-11-12 10:13:39 -0800334 profileIsTextListing := false
Jiakai Zhang81e46812023-02-08 21:56:07 +0800335 if d.inputProfilePathOnHost != nil {
336 profileClassListing = android.OptionalPathForPath(d.inputProfilePathOnHost)
337 } else if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) && !forPrebuiltApex(ctx) {
Colin Cross43f08db2018-11-12 10:13:39 -0800338 // If dex_preopt.profile_guided is not set, default it based on the existence of the
339 // dexprepot.profile option or the profile class listing.
340 if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" {
341 profileClassListing = android.OptionalPathForPath(
342 android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile)))
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100343 profileBootListing = android.ExistentPathForSource(ctx,
344 ctx.ModuleDir(), String(d.dexpreoptProperties.Dex_preopt.Profile)+"-boot")
Colin Cross43f08db2018-11-12 10:13:39 -0800345 profileIsTextListing = true
Dan Willemsen78d51b02020-06-24 16:33:31 -0700346 } else if global.ProfileDir != "" {
Colin Cross43f08db2018-11-12 10:13:39 -0800347 profileClassListing = android.ExistentPathForSource(ctx,
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000348 global.ProfileDir, moduleName(ctx)+".prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800349 }
350 }
351
Jiakai Zhang9c4dc192023-02-09 00:09:24 +0800352 d.dexpreoptProperties.Dex_preopt_result.Profile_guided = profileClassListing.Valid()
353
Ulya Trafimovich76b08522021-01-14 17:52:43 +0000354 // Full dexpreopt config, used to create dexpreopt build rules.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000355 dexpreoptConfig := &dexpreopt.ModuleConfig{
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000356 Name: moduleName(ctx),
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800357 DexLocation: dexLocation,
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000358 BuildPath: android.PathForModuleOut(ctx, "dexpreopt", moduleName(ctx)+".jar").OutputPath,
Colin Cross69f59a32019-02-15 10:39:37 -0800359 DexPath: dexJarFile,
Jeongik Cha33a3a812021-04-15 09:12:49 +0900360 ManifestPath: android.OptionalPathForPath(d.manifestFile),
Victor Hsiehd181c8b2019-01-29 13:00:33 -0800361 UncompressedDex: d.uncompressedDex,
362 HasApkLibraries: false,
363 PreoptFlags: nil,
Colin Cross43f08db2018-11-12 10:13:39 -0800364
Colin Cross69f59a32019-02-15 10:39:37 -0800365 ProfileClassListing: profileClassListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800366 ProfileIsTextListing: profileIsTextListing,
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100367 ProfileBootListing: profileBootListing,
Colin Cross43f08db2018-11-12 10:13:39 -0800368
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +0000369 EnforceUsesLibrariesStatusFile: dexpreopt.UsesLibrariesStatusFile(ctx),
370 EnforceUsesLibraries: d.enforceUsesLibs,
371 ProvidesUsesLibrary: providesUsesLib,
372 ClassLoaderContexts: d.classLoaderContexts,
Colin Cross43f08db2018-11-12 10:13:39 -0800373
Jeongik Cha4dda75e2021-04-27 23:56:44 +0900374 Archs: archs,
375 DexPreoptImagesDeps: imagesDeps,
376 DexPreoptImageLocationsOnHost: hostImageLocations,
377 DexPreoptImageLocationsOnDevice: deviceImageLocations,
Colin Cross43f08db2018-11-12 10:13:39 -0800378
Ulya Trafimovich9023b022021-03-22 16:02:28 +0000379 PreoptBootClassPathDexFiles: dexFiles.Paths(),
Vladimir Marko40139d62020-02-06 15:14:29 +0000380 PreoptBootClassPathDexLocations: dexLocations,
Colin Cross800fe132019-02-11 14:21:24 -0800381
Colin Cross43f08db2018-11-12 10:13:39 -0800382 PreoptExtractedApk: false,
383
384 NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true),
385 ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false),
386
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700387 PresignedPrebuilt: d.isPresignedPrebuilt,
Colin Cross43f08db2018-11-12 10:13:39 -0800388 }
389
Jeongik Chac6246672021-04-08 00:00:19 +0900390 d.configPath = android.PathForModuleOut(ctx, "dexpreopt", "dexpreopt.config")
391 dexpreopt.WriteModuleConfig(ctx, dexpreoptConfig, d.configPath)
392
393 if d.dexpreoptDisabled(ctx) {
394 return
395 }
396
397 globalSoong := dexpreopt.GetGlobalSoongConfig(ctx)
398
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000399 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, globalSoong, global, dexpreoptConfig)
Colin Cross43f08db2018-11-12 10:13:39 -0800400 if err != nil {
401 ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error())
Jaewoong Jung4b97a562020-12-17 09:43:28 -0800402 return
Colin Cross43f08db2018-11-12 10:13:39 -0800403 }
404
Colin Crossf1a035e2020-11-16 17:32:30 -0800405 dexpreoptRule.Build("dexpreopt", "dexpreopt")
Colin Cross43f08db2018-11-12 10:13:39 -0800406
Jiakai Zhang389a6472021-12-14 18:54:06 +0000407 isApexSystemServerJar := global.AllApexSystemServerJars(ctx).ContainsJar(moduleName(ctx))
408
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700409 for _, install := range dexpreoptRule.Installs() {
410 // Remove the "/" prefix because the path should be relative to $ANDROID_PRODUCT_OUT.
411 installDir := strings.TrimPrefix(filepath.Dir(install.To), "/")
412 installBase := filepath.Base(install.To)
413 arch := filepath.Base(installDir)
414 installPath := android.PathForModuleInPartitionInstall(ctx, "", installDir)
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800415 isProfile := strings.HasSuffix(installBase, ".prof")
416
417 if isProfile {
Jiakai Zhang81e46812023-02-08 21:56:07 +0800418 d.outputProfilePathOnHost = install.From
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800419 }
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700420
Jiakai Zhang389a6472021-12-14 18:54:06 +0000421 if isApexSystemServerJar {
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800422 // Profiles are handled separately because they are installed into the APEX.
423 if !isProfile {
424 // APEX variants of java libraries are hidden from Make, so their dexpreopt
425 // outputs need special handling. Currently, for APEX variants of java
426 // libraries, only those in the system server classpath are handled here.
427 // Preopting of boot classpath jars in the ART APEX are handled in
428 // java/dexpreopt_bootjars.go, and other APEX jars are not preopted.
429 // The installs will be handled by Make as sub-modules of the java library.
430 d.builtInstalledForApex = append(d.builtInstalledForApex, dexpreopterInstall{
431 name: arch + "-" + installBase,
432 moduleName: moduleName(ctx),
433 outputPathOnHost: install.From,
434 installDirOnDevice: installPath,
435 installFileOnDevice: installBase,
436 })
437 }
Colin Crossfa9bfcd2021-11-10 16:42:38 -0800438 } else if !d.preventInstall {
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700439 ctx.InstallFile(installPath, installBase, install.From)
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000440 }
Colin Cross1d0eb7a2021-11-03 14:08:20 -0700441 }
442
Jiakai Zhang389a6472021-12-14 18:54:06 +0000443 if !isApexSystemServerJar {
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000444 d.builtInstalled = dexpreoptRule.Installs().String()
445 }
446}
447
448func (d *dexpreopter) DexpreoptBuiltInstalledForApex() []dexpreopterInstall {
449 return d.builtInstalledForApex
450}
451
452func (d *dexpreopter) AndroidMkEntriesForApex() []android.AndroidMkEntries {
453 var entries []android.AndroidMkEntries
454 for _, install := range d.builtInstalledForApex {
Jiakai Zhang6decef92022-01-12 17:56:19 +0000455 entries = append(entries, install.ToMakeEntries())
Jiakai Zhangca9bc982021-09-09 08:09:41 +0000456 }
457 return entries
Colin Cross43f08db2018-11-12 10:13:39 -0800458}
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800459
Jiakai Zhang81e46812023-02-08 21:56:07 +0800460func (d *dexpreopter) OutputProfilePathOnHost() android.Path {
461 return d.outputProfilePathOnHost
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800462}