Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 18 | "path/filepath" |
| 19 | "strings" |
| 20 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 21 | "android/soong/android" |
| 22 | "android/soong/dexpreopt" |
| 23 | ) |
| 24 | |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 25 | type DexpreopterInterface interface { |
Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 26 | IsInstallable() bool // Structs that embed dexpreopter must implement this. |
| 27 | dexpreoptDisabled(ctx android.BaseModuleContext) bool |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 28 | DexpreoptBuiltInstalledForApex() []dexpreopterInstall |
| 29 | AndroidMkEntriesForApex() []android.AndroidMkEntries |
Jiakai Zhang | 3317ce7 | 2023-02-08 01:19:19 +0800 | [diff] [blame] | 30 | ProfilePathOnHost() android.Path |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | type dexpreopterInstall struct { |
| 34 | // A unique name to distinguish an output from others for the same java library module. Usually in |
| 35 | // the form of `<arch>-<encoded-path>.odex/vdex/art`. |
| 36 | name string |
| 37 | |
| 38 | // The name of the input java module. |
| 39 | moduleName string |
| 40 | |
| 41 | // The path to the dexpreopt output on host. |
| 42 | outputPathOnHost android.Path |
| 43 | |
| 44 | // The directory on the device for the output to install to. |
| 45 | installDirOnDevice android.InstallPath |
| 46 | |
| 47 | // The basename (the last segment of the path) for the output to install as. |
| 48 | installFileOnDevice string |
| 49 | } |
| 50 | |
| 51 | // The full module name of the output in the makefile. |
| 52 | func (install *dexpreopterInstall) FullModuleName() string { |
| 53 | return install.moduleName + install.SubModuleName() |
| 54 | } |
| 55 | |
| 56 | // The sub-module name of the output in the makefile (the name excluding the java module name). |
| 57 | func (install *dexpreopterInstall) SubModuleName() string { |
| 58 | return "-dexpreopt-" + install.name |
Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Jiakai Zhang | 6decef9 | 2022-01-12 17:56:19 +0000 | [diff] [blame] | 61 | // Returns Make entries for installing the file. |
| 62 | // |
| 63 | // This function uses a value receiver rather than a pointer receiver to ensure that the object is |
| 64 | // safe to use in `android.AndroidMkExtraEntriesFunc`. |
| 65 | func (install dexpreopterInstall) ToMakeEntries() android.AndroidMkEntries { |
| 66 | return android.AndroidMkEntries{ |
| 67 | Class: "ETC", |
| 68 | SubName: install.SubModuleName(), |
| 69 | OutputFile: android.OptionalPathForPath(install.outputPathOnHost), |
| 70 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 71 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 72 | entries.SetString("LOCAL_MODULE_PATH", install.installDirOnDevice.String()) |
| 73 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", install.installFileOnDevice) |
| 74 | entries.SetString("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", "false") |
| 75 | }, |
| 76 | }, |
| 77 | } |
| 78 | } |
| 79 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 80 | type dexpreopter struct { |
Jiakai Zhang | 9c4dc19 | 2023-02-09 00:09:24 +0800 | [diff] [blame^] | 81 | dexpreoptProperties DexpreoptProperties |
| 82 | importDexpreoptProperties ImportDexpreoptProperties |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 83 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 84 | installPath android.InstallPath |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 85 | uncompressedDex bool |
| 86 | isSDKLibrary bool |
Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 87 | isApp bool |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 88 | isTest bool |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 89 | isPresignedPrebuilt bool |
Colin Cross | fa9bfcd | 2021-11-10 16:42:38 -0800 | [diff] [blame] | 90 | preventInstall bool |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 91 | |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 92 | manifestFile android.Path |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 93 | statusFile android.WritablePath |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 94 | enforceUsesLibs bool |
| 95 | classLoaderContexts dexpreopt.ClassLoaderContextMap |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 96 | |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 97 | // See the `dexpreopt` function for details. |
| 98 | builtInstalled string |
| 99 | builtInstalledForApex []dexpreopterInstall |
Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 100 | |
Jeongik Cha | c624667 | 2021-04-08 00:00:19 +0900 | [diff] [blame] | 101 | // The config is used for two purposes: |
| 102 | // - Passing dexpreopt information about libraries from Soong to Make. This is needed when |
| 103 | // a <uses-library> is defined in Android.bp, but used in Android.mk (see dex_preopt_config_merger.py). |
| 104 | // Note that dexpreopt.config might be needed even if dexpreopt is disabled for the library itself. |
| 105 | // - Dexpreopt post-processing (using dexpreopt artifacts from a prebuilt system image to incrementally |
| 106 | // dexpreopt another partition). |
Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 107 | configPath android.WritablePath |
Jiakai Zhang | 3317ce7 | 2023-02-08 01:19:19 +0800 | [diff] [blame] | 108 | |
| 109 | // The path to the profile on host. |
| 110 | profilePathOnHost android.Path |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | type DexpreoptProperties struct { |
| 114 | Dex_preopt struct { |
Nicolas Geoffray | c1bf724 | 2019-10-18 14:51:38 +0100 | [diff] [blame] | 115 | // If false, prevent dexpreopting. Defaults to true. |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 116 | Enabled *bool |
| 117 | |
| 118 | // If true, generate an app image (.art file) for this module. |
| 119 | App_image *bool |
| 120 | |
| 121 | // If true, use a checked-in profile to guide optimization. Defaults to false unless |
| 122 | // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR |
| 123 | // that matches the name of this module, in which case it is defaulted to true. |
| 124 | Profile_guided *bool |
| 125 | |
| 126 | // If set, provides the path to profile relative to the Android.bp file. If not set, |
| 127 | // defaults to searching for a file that matches the name of this module in the default |
| 128 | // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found. |
Colin Cross | de4e4e6 | 2019-04-26 10:52:32 -0700 | [diff] [blame] | 129 | Profile *string `android:"path"` |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 130 | } |
Jiakai Zhang | 9c4dc19 | 2023-02-09 00:09:24 +0800 | [diff] [blame^] | 131 | |
| 132 | Dex_preopt_result struct { |
| 133 | // True if profile-guided optimization is actually enabled. |
| 134 | Profile_guided bool |
| 135 | } `blueprint:"mutated"` |
| 136 | } |
| 137 | |
| 138 | type ImportDexpreoptProperties struct { |
| 139 | Dex_preopt struct { |
| 140 | // If true, use the profile in the prebuilt APEX to guide optimization. Defaults to false. |
| 141 | Profile_guided *bool |
| 142 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Ulya Trafimovich | 6cf2c0c | 2020-04-24 12:15:20 +0100 | [diff] [blame] | 145 | func init() { |
| 146 | dexpreopt.DexpreoptRunningInSoong = true |
| 147 | } |
| 148 | |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 149 | func isApexVariant(ctx android.BaseModuleContext) bool { |
| 150 | apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo) |
| 151 | return !apexInfo.IsForPlatform() |
| 152 | } |
| 153 | |
Jiakai Zhang | 28bc9a8 | 2021-12-20 15:08:57 +0000 | [diff] [blame] | 154 | func forPrebuiltApex(ctx android.BaseModuleContext) bool { |
| 155 | apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo) |
| 156 | return apexInfo.ForPrebuiltApex |
| 157 | } |
| 158 | |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 159 | func moduleName(ctx android.BaseModuleContext) string { |
| 160 | // Remove the "prebuilt_" prefix if the module is from a prebuilt because the prefix is not |
| 161 | // expected by dexpreopter. |
| 162 | return android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName()) |
| 163 | } |
| 164 | |
Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 165 | func (d *dexpreopter) dexpreoptDisabled(ctx android.BaseModuleContext) bool { |
Colin Cross | 38310bb | 2021-12-01 10:34:14 -0800 | [diff] [blame] | 166 | if !ctx.Device() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 167 | return true |
| 168 | } |
| 169 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 170 | if d.isTest { |
| 171 | return true |
| 172 | } |
| 173 | |
| 174 | if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) { |
| 175 | return true |
| 176 | } |
| 177 | |
Jiakai Zhang | 28bc9a8 | 2021-12-20 15:08:57 +0000 | [diff] [blame] | 178 | // If the module is from a prebuilt APEX, it shouldn't be installable, but it can still be |
| 179 | // dexpreopted. |
| 180 | if !ctx.Module().(DexpreopterInterface).IsInstallable() && !forPrebuiltApex(ctx) { |
Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 181 | return true |
| 182 | } |
| 183 | |
Colin Cross | 38310bb | 2021-12-01 10:34:14 -0800 | [diff] [blame] | 184 | if !android.IsModulePreferred(ctx.Module()) { |
| 185 | return true |
| 186 | } |
| 187 | |
| 188 | global := dexpreopt.GetGlobalConfig(ctx) |
| 189 | |
| 190 | if global.DisablePreopt { |
| 191 | return true |
| 192 | } |
| 193 | |
| 194 | if inList(moduleName(ctx), global.DisablePreoptModules) { |
Colin Cross | dc2da91 | 2019-01-05 22:13:05 -0800 | [diff] [blame] | 195 | return true |
| 196 | } |
| 197 | |
Jiakai Zhang | 389a647 | 2021-12-14 18:54:06 +0000 | [diff] [blame] | 198 | isApexSystemServerJar := global.AllApexSystemServerJars(ctx).ContainsJar(moduleName(ctx)) |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 199 | if isApexVariant(ctx) { |
Jiakai Zhang | 3317ce7 | 2023-02-08 01:19:19 +0800 | [diff] [blame] | 200 | // Don't preopt APEX variant module unless the module is an APEX system server jar. |
| 201 | if !isApexSystemServerJar { |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 202 | return true |
| 203 | } |
| 204 | } else { |
| 205 | // Don't preopt the platform variant of an APEX system server jar to avoid conflicts. |
Jiakai Zhang | 389a647 | 2021-12-14 18:54:06 +0000 | [diff] [blame] | 206 | if isApexSystemServerJar { |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 207 | return true |
| 208 | } |
Yo Chiang | dbdf8f9 | 2020-01-09 19:00:27 +0800 | [diff] [blame] | 209 | } |
| 210 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 211 | // TODO: contains no java code |
| 212 | |
| 213 | return false |
| 214 | } |
| 215 | |
Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 216 | func dexpreoptToolDepsMutator(ctx android.BottomUpMutatorContext) { |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 217 | if d, ok := ctx.Module().(DexpreopterInterface); !ok || d.dexpreoptDisabled(ctx) { |
Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 218 | return |
| 219 | } |
| 220 | dexpreopt.RegisterToolDeps(ctx) |
| 221 | } |
| 222 | |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 223 | func (d *dexpreopter) odexOnSystemOther(ctx android.ModuleContext, installPath android.InstallPath) bool { |
| 224 | return dexpreopt.OdexOnSystemOtherByName(moduleName(ctx), android.InstallPathToOnDevicePath(ctx, installPath), dexpreopt.GetGlobalConfig(ctx)) |
| 225 | } |
| 226 | |
| 227 | // Returns the install path of the dex jar of a module. |
| 228 | // |
| 229 | // Do not rely on `ApexInfo.ApexVariationName` because it can be something like "apex1000", rather |
| 230 | // than the `name` in the path `/apex/<name>` as suggested in its comment. |
| 231 | // |
| 232 | // This function is on a best-effort basis. It cannot handle the case where an APEX jar is not a |
| 233 | // system server jar, which is fine because we currently only preopt system server jars for APEXes. |
| 234 | func (d *dexpreopter) getInstallPath( |
| 235 | ctx android.ModuleContext, defaultInstallPath android.InstallPath) android.InstallPath { |
| 236 | global := dexpreopt.GetGlobalConfig(ctx) |
Jiakai Zhang | 389a647 | 2021-12-14 18:54:06 +0000 | [diff] [blame] | 237 | if global.AllApexSystemServerJars(ctx).ContainsJar(moduleName(ctx)) { |
| 238 | dexLocation := dexpreopt.GetSystemServerDexLocation(ctx, global, moduleName(ctx)) |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 239 | return android.PathForModuleInPartitionInstall(ctx, "", strings.TrimPrefix(dexLocation, "/")) |
| 240 | } |
| 241 | if !d.dexpreoptDisabled(ctx) && isApexVariant(ctx) && |
| 242 | filepath.Base(defaultInstallPath.PartitionDir()) != "apex" { |
| 243 | ctx.ModuleErrorf("unable to get the install path of the dex jar for dexpreopt") |
| 244 | } |
| 245 | return defaultInstallPath |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Paul Duffin | 612e610 | 2021-02-02 13:38:13 +0000 | [diff] [blame] | 248 | func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.WritablePath) { |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 249 | global := dexpreopt.GetGlobalConfig(ctx) |
| 250 | |
Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 251 | // TODO(b/148690468): The check on d.installPath is to bail out in cases where |
| 252 | // the dexpreopter struct hasn't been fully initialized before we're called, |
| 253 | // e.g. in aar.go. This keeps the behaviour that dexpreopting is effectively |
| 254 | // disabled, even if installable is true. |
Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 255 | if d.installPath.Base() == "." { |
| 256 | return |
| 257 | } |
| 258 | |
| 259 | dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath) |
| 260 | |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 261 | providesUsesLib := moduleName(ctx) |
Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 262 | if ulib, ok := ctx.Module().(ProvidesUsesLib); ok { |
| 263 | name := ulib.ProvidesUsesLib() |
| 264 | if name != nil { |
| 265 | providesUsesLib = *name |
| 266 | } |
| 267 | } |
| 268 | |
Jeongik Cha | 4b073cd | 2021-06-08 11:35:00 +0900 | [diff] [blame] | 269 | // If it is test, make config files regardless of its dexpreopt setting. |
Jeongik Cha | c624667 | 2021-04-08 00:00:19 +0900 | [diff] [blame] | 270 | // The config files are required for apps defined in make which depend on the lib. |
Jeongik Cha | 4b073cd | 2021-06-08 11:35:00 +0900 | [diff] [blame] | 271 | if d.isTest && d.dexpreoptDisabled(ctx) { |
Jaewoong Jung | 4b97a56 | 2020-12-17 09:43:28 -0800 | [diff] [blame] | 272 | return |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Jiakai Zhang | 389a647 | 2021-12-14 18:54:06 +0000 | [diff] [blame] | 275 | isSystemServerJar := global.AllSystemServerJars(ctx).ContainsJar(moduleName(ctx)) |
Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 276 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 277 | bootImage := defaultBootImageConfig(ctx) |
Jiakai Zhang | 02669e8 | 2021-09-11 03:44:06 +0000 | [diff] [blame] | 278 | dexFiles, dexLocations := bcpForDexpreopt(ctx, global.PreoptWithUpdatableBcp) |
Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 279 | |
David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 280 | targets := ctx.MultiTargets() |
| 281 | if len(targets) == 0 { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 282 | // assume this is a java library, dexpreopt for all arches for now |
| 283 | for _, target := range ctx.Config().Targets[android.Android] { |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 284 | if target.NativeBridge == android.NativeBridgeDisabled { |
David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 285 | targets = append(targets, target) |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 286 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 287 | } |
Jiakai Zhang | 2fbc355 | 2022-11-28 15:38:23 +0000 | [diff] [blame] | 288 | if isSystemServerJar && moduleName(ctx) != "com.android.location.provider" { |
| 289 | // If the module is a system server jar, only preopt for the primary arch because the jar can |
| 290 | // only be loaded by system server. "com.android.location.provider" is a special case because |
| 291 | // it's also used by apps as a shared library. |
David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 292 | targets = targets[:1] |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 293 | } |
| 294 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 295 | |
David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 296 | var archs []android.ArchType |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 297 | var images android.Paths |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 298 | var imagesDeps []android.OutputPaths |
David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 299 | for _, target := range targets { |
| 300 | archs = append(archs, target.Arch.ArchType) |
| 301 | variant := bootImage.getVariant(target) |
Jeongik Cha | a596909 | 2021-05-07 18:53:21 +0900 | [diff] [blame] | 302 | images = append(images, variant.imagePathOnHost) |
David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 303 | imagesDeps = append(imagesDeps, variant.imagesDeps) |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 304 | } |
David Srbecky | ab99498 | 2020-03-30 17:24:13 +0100 | [diff] [blame] | 305 | // The image locations for all Android variants are identical. |
Jeongik Cha | 4dda75e | 2021-04-27 23:56:44 +0900 | [diff] [blame] | 306 | hostImageLocations, deviceImageLocations := bootImage.getAnyAndroidVariant().imageLocations() |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 307 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 308 | var profileClassListing android.OptionalPath |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 309 | var profileBootListing android.OptionalPath |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 310 | profileIsTextListing := false |
| 311 | if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) { |
| 312 | // If dex_preopt.profile_guided is not set, default it based on the existence of the |
| 313 | // dexprepot.profile option or the profile class listing. |
| 314 | if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" { |
| 315 | profileClassListing = android.OptionalPathForPath( |
| 316 | android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile))) |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 317 | profileBootListing = android.ExistentPathForSource(ctx, |
| 318 | ctx.ModuleDir(), String(d.dexpreoptProperties.Dex_preopt.Profile)+"-boot") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 319 | profileIsTextListing = true |
Dan Willemsen | 78d51b0 | 2020-06-24 16:33:31 -0700 | [diff] [blame] | 320 | } else if global.ProfileDir != "" { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 321 | profileClassListing = android.ExistentPathForSource(ctx, |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 322 | global.ProfileDir, moduleName(ctx)+".prof") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
Jiakai Zhang | 9c4dc19 | 2023-02-09 00:09:24 +0800 | [diff] [blame^] | 326 | d.dexpreoptProperties.Dex_preopt_result.Profile_guided = profileClassListing.Valid() |
| 327 | |
Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 328 | // Full dexpreopt config, used to create dexpreopt build rules. |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 329 | dexpreoptConfig := &dexpreopt.ModuleConfig{ |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 330 | Name: moduleName(ctx), |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 331 | DexLocation: dexLocation, |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 332 | BuildPath: android.PathForModuleOut(ctx, "dexpreopt", moduleName(ctx)+".jar").OutputPath, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 333 | DexPath: dexJarFile, |
Jeongik Cha | 33a3a81 | 2021-04-15 09:12:49 +0900 | [diff] [blame] | 334 | ManifestPath: android.OptionalPathForPath(d.manifestFile), |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 335 | UncompressedDex: d.uncompressedDex, |
| 336 | HasApkLibraries: false, |
| 337 | PreoptFlags: nil, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 338 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 339 | ProfileClassListing: profileClassListing, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 340 | ProfileIsTextListing: profileIsTextListing, |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 341 | ProfileBootListing: profileBootListing, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 342 | |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 343 | EnforceUsesLibrariesStatusFile: dexpreopt.UsesLibrariesStatusFile(ctx), |
| 344 | EnforceUsesLibraries: d.enforceUsesLibs, |
| 345 | ProvidesUsesLibrary: providesUsesLib, |
| 346 | ClassLoaderContexts: d.classLoaderContexts, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 347 | |
Jeongik Cha | 4dda75e | 2021-04-27 23:56:44 +0900 | [diff] [blame] | 348 | Archs: archs, |
| 349 | DexPreoptImagesDeps: imagesDeps, |
| 350 | DexPreoptImageLocationsOnHost: hostImageLocations, |
| 351 | DexPreoptImageLocationsOnDevice: deviceImageLocations, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 352 | |
Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 353 | PreoptBootClassPathDexFiles: dexFiles.Paths(), |
Vladimir Marko | 40139d6 | 2020-02-06 15:14:29 +0000 | [diff] [blame] | 354 | PreoptBootClassPathDexLocations: dexLocations, |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 355 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 356 | PreoptExtractedApk: false, |
| 357 | |
| 358 | NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true), |
| 359 | ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false), |
| 360 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 361 | PresignedPrebuilt: d.isPresignedPrebuilt, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 362 | } |
| 363 | |
Jeongik Cha | c624667 | 2021-04-08 00:00:19 +0900 | [diff] [blame] | 364 | d.configPath = android.PathForModuleOut(ctx, "dexpreopt", "dexpreopt.config") |
| 365 | dexpreopt.WriteModuleConfig(ctx, dexpreoptConfig, d.configPath) |
| 366 | |
| 367 | if d.dexpreoptDisabled(ctx) { |
| 368 | return |
| 369 | } |
| 370 | |
| 371 | globalSoong := dexpreopt.GetGlobalSoongConfig(ctx) |
| 372 | |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 373 | dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, globalSoong, global, dexpreoptConfig) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 374 | if err != nil { |
| 375 | ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error()) |
Jaewoong Jung | 4b97a56 | 2020-12-17 09:43:28 -0800 | [diff] [blame] | 376 | return |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 379 | dexpreoptRule.Build("dexpreopt", "dexpreopt") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 380 | |
Jiakai Zhang | 389a647 | 2021-12-14 18:54:06 +0000 | [diff] [blame] | 381 | isApexSystemServerJar := global.AllApexSystemServerJars(ctx).ContainsJar(moduleName(ctx)) |
| 382 | |
Colin Cross | 1d0eb7a | 2021-11-03 14:08:20 -0700 | [diff] [blame] | 383 | for _, install := range dexpreoptRule.Installs() { |
| 384 | // Remove the "/" prefix because the path should be relative to $ANDROID_PRODUCT_OUT. |
| 385 | installDir := strings.TrimPrefix(filepath.Dir(install.To), "/") |
| 386 | installBase := filepath.Base(install.To) |
| 387 | arch := filepath.Base(installDir) |
| 388 | installPath := android.PathForModuleInPartitionInstall(ctx, "", installDir) |
Jiakai Zhang | 3317ce7 | 2023-02-08 01:19:19 +0800 | [diff] [blame] | 389 | isProfile := strings.HasSuffix(installBase, ".prof") |
| 390 | |
| 391 | if isProfile { |
| 392 | d.profilePathOnHost = install.From |
| 393 | } |
Colin Cross | 1d0eb7a | 2021-11-03 14:08:20 -0700 | [diff] [blame] | 394 | |
Jiakai Zhang | 389a647 | 2021-12-14 18:54:06 +0000 | [diff] [blame] | 395 | if isApexSystemServerJar { |
Jiakai Zhang | 3317ce7 | 2023-02-08 01:19:19 +0800 | [diff] [blame] | 396 | // Profiles are handled separately because they are installed into the APEX. |
| 397 | if !isProfile { |
| 398 | // APEX variants of java libraries are hidden from Make, so their dexpreopt |
| 399 | // outputs need special handling. Currently, for APEX variants of java |
| 400 | // libraries, only those in the system server classpath are handled here. |
| 401 | // Preopting of boot classpath jars in the ART APEX are handled in |
| 402 | // java/dexpreopt_bootjars.go, and other APEX jars are not preopted. |
| 403 | // The installs will be handled by Make as sub-modules of the java library. |
| 404 | d.builtInstalledForApex = append(d.builtInstalledForApex, dexpreopterInstall{ |
| 405 | name: arch + "-" + installBase, |
| 406 | moduleName: moduleName(ctx), |
| 407 | outputPathOnHost: install.From, |
| 408 | installDirOnDevice: installPath, |
| 409 | installFileOnDevice: installBase, |
| 410 | }) |
| 411 | } |
Colin Cross | fa9bfcd | 2021-11-10 16:42:38 -0800 | [diff] [blame] | 412 | } else if !d.preventInstall { |
Colin Cross | 1d0eb7a | 2021-11-03 14:08:20 -0700 | [diff] [blame] | 413 | ctx.InstallFile(installPath, installBase, install.From) |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 414 | } |
Colin Cross | 1d0eb7a | 2021-11-03 14:08:20 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Jiakai Zhang | 389a647 | 2021-12-14 18:54:06 +0000 | [diff] [blame] | 417 | if !isApexSystemServerJar { |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 418 | d.builtInstalled = dexpreoptRule.Installs().String() |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | func (d *dexpreopter) DexpreoptBuiltInstalledForApex() []dexpreopterInstall { |
| 423 | return d.builtInstalledForApex |
| 424 | } |
| 425 | |
| 426 | func (d *dexpreopter) AndroidMkEntriesForApex() []android.AndroidMkEntries { |
| 427 | var entries []android.AndroidMkEntries |
| 428 | for _, install := range d.builtInstalledForApex { |
Jiakai Zhang | 6decef9 | 2022-01-12 17:56:19 +0000 | [diff] [blame] | 429 | entries = append(entries, install.ToMakeEntries()) |
Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 430 | } |
| 431 | return entries |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 432 | } |
Jiakai Zhang | 3317ce7 | 2023-02-08 01:19:19 +0800 | [diff] [blame] | 433 | |
| 434 | func (d *dexpreopter) ProfilePathOnHost() android.Path { |
| 435 | return d.profilePathOnHost |
| 436 | } |