| 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" | 
| Jiakai Zhang | 51b2a8b | 2023-06-26 16:47:38 +0100 | [diff] [blame] | 19 | "sort" | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 20 | "strings" | 
|  | 21 |  | 
| Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" | 
|  | 23 |  | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 24 | "android/soong/android" | 
|  | 25 | "android/soong/dexpreopt" | 
|  | 26 | ) | 
|  | 27 |  | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 28 | type DexpreopterInterface interface { | 
| Jiakai Zhang | 81e4681 | 2023-02-08 21:56:07 +0800 | [diff] [blame] | 29 | // 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 Das | e21a8d4 | 2024-01-23 23:56:29 +0000 | [diff] [blame] | 34 | dexpreoptDisabled(ctx android.BaseModuleContext, libraryName string) bool | 
| Jiakai Zhang | 81e4681 | 2023-02-08 21:56:07 +0800 | [diff] [blame] | 35 |  | 
|  | 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. | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 39 | DexpreoptBuiltInstalledForApex() []dexpreopterInstall | 
| Jiakai Zhang | 81e4681 | 2023-02-08 21:56:07 +0800 | [diff] [blame] | 40 |  | 
|  | 41 | // The Make entries to install the dexpreopt outputs. Derived from | 
|  | 42 | // `DexpreoptBuiltInstalledForApex`. | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 43 | AndroidMkEntriesForApex() []android.AndroidMkEntries | 
| Jiakai Zhang | 81e4681 | 2023-02-08 21:56:07 +0800 | [diff] [blame] | 44 |  | 
|  | 45 | // See `dexpreopter.outputProfilePathOnHost`. | 
|  | 46 | OutputProfilePathOnHost() android.Path | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 47 | } | 
|  | 48 |  | 
|  | 49 | type dexpreopterInstall struct { | 
|  | 50 | // A unique name to distinguish an output from others for the same java library module. Usually in | 
|  | 51 | // the form of `<arch>-<encoded-path>.odex/vdex/art`. | 
|  | 52 | name string | 
|  | 53 |  | 
|  | 54 | // The name of the input java module. | 
|  | 55 | moduleName string | 
|  | 56 |  | 
|  | 57 | // The path to the dexpreopt output on host. | 
|  | 58 | outputPathOnHost android.Path | 
|  | 59 |  | 
|  | 60 | // The directory on the device for the output to install to. | 
|  | 61 | installDirOnDevice android.InstallPath | 
|  | 62 |  | 
|  | 63 | // The basename (the last segment of the path) for the output to install as. | 
|  | 64 | installFileOnDevice string | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | // The full module name of the output in the makefile. | 
|  | 68 | func (install *dexpreopterInstall) FullModuleName() string { | 
|  | 69 | return install.moduleName + install.SubModuleName() | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | // The sub-module name of the output in the makefile (the name excluding the java module name). | 
|  | 73 | func (install *dexpreopterInstall) SubModuleName() string { | 
|  | 74 | return "-dexpreopt-" + install.name | 
| Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
| Jiakai Zhang | 6decef9 | 2022-01-12 17:56:19 +0000 | [diff] [blame] | 77 | // Returns Make entries for installing the file. | 
|  | 78 | // | 
|  | 79 | // This function uses a value receiver rather than a pointer receiver to ensure that the object is | 
|  | 80 | // safe to use in `android.AndroidMkExtraEntriesFunc`. | 
|  | 81 | func (install dexpreopterInstall) ToMakeEntries() android.AndroidMkEntries { | 
|  | 82 | return android.AndroidMkEntries{ | 
|  | 83 | Class:      "ETC", | 
| Jiakai Zhang | 6decef9 | 2022-01-12 17:56:19 +0000 | [diff] [blame] | 84 | OutputFile: android.OptionalPathForPath(install.outputPathOnHost), | 
|  | 85 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ | 
|  | 86 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { | 
| Spandan Das | 2069c3f | 2023-12-06 19:40:24 +0000 | [diff] [blame] | 87 | entries.SetString("LOCAL_MODULE", install.FullModuleName()) | 
| Jiakai Zhang | 6decef9 | 2022-01-12 17:56:19 +0000 | [diff] [blame] | 88 | entries.SetString("LOCAL_MODULE_PATH", install.installDirOnDevice.String()) | 
|  | 89 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", install.installFileOnDevice) | 
|  | 90 | entries.SetString("LOCAL_NOT_AVAILABLE_FOR_PLATFORM", "false") | 
|  | 91 | }, | 
|  | 92 | }, | 
|  | 93 | } | 
|  | 94 | } | 
|  | 95 |  | 
| Spandan Das | 2069c3f | 2023-12-06 19:40:24 +0000 | [diff] [blame] | 96 | type Dexpreopter struct { | 
|  | 97 | dexpreopter | 
|  | 98 | } | 
|  | 99 |  | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 100 | type dexpreopter struct { | 
| Jiakai Zhang | 9c4dc19 | 2023-02-09 00:09:24 +0800 | [diff] [blame] | 101 | dexpreoptProperties       DexpreoptProperties | 
|  | 102 | importDexpreoptProperties ImportDexpreoptProperties | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 103 |  | 
| Spandan Das | 0727ba7 | 2024-02-13 16:37:43 +0000 | [diff] [blame] | 104 | // If true, the dexpreopt rules will not be generated | 
|  | 105 | // Unlike Dex_preopt.Enabled which is user-facing, | 
|  | 106 | // shouldDisableDexpreopt is a mutated propery. | 
|  | 107 | shouldDisableDexpreopt bool | 
|  | 108 |  | 
| Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 109 | installPath         android.InstallPath | 
| Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 110 | uncompressedDex     bool | 
|  | 111 | isSDKLibrary        bool | 
| Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 112 | isApp               bool | 
| Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 113 | isTest              bool | 
| Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 114 | isPresignedPrebuilt bool | 
| Colin Cross | fa9bfcd | 2021-11-10 16:42:38 -0800 | [diff] [blame] | 115 | preventInstall      bool | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 116 |  | 
| Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 117 | manifestFile        android.Path | 
| Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 118 | statusFile          android.WritablePath | 
| Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 119 | enforceUsesLibs     bool | 
|  | 120 | classLoaderContexts dexpreopt.ClassLoaderContextMap | 
| Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 121 |  | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 122 | // See the `dexpreopt` function for details. | 
|  | 123 | builtInstalled        string | 
|  | 124 | builtInstalledForApex []dexpreopterInstall | 
| Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 125 |  | 
| Jeongik Cha | c624667 | 2021-04-08 00:00:19 +0900 | [diff] [blame] | 126 | // The config is used for two purposes: | 
|  | 127 | // - Passing dexpreopt information about libraries from Soong to Make. This is needed when | 
|  | 128 | //   a <uses-library> is defined in Android.bp, but used in Android.mk (see dex_preopt_config_merger.py). | 
|  | 129 | //   Note that dexpreopt.config might be needed even if dexpreopt is disabled for the library itself. | 
|  | 130 | // - Dexpreopt post-processing (using dexpreopt artifacts from a prebuilt system image to incrementally | 
|  | 131 | //   dexpreopt another partition). | 
| Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 132 | configPath android.WritablePath | 
| Jiakai Zhang | 3317ce7 | 2023-02-08 01:19:19 +0800 | [diff] [blame] | 133 |  | 
| Jiakai Zhang | 81e4681 | 2023-02-08 21:56:07 +0800 | [diff] [blame] | 134 | // The path to the profile on host that dexpreopter generates. This is used as the input for | 
|  | 135 | // dex2oat. | 
|  | 136 | outputProfilePathOnHost android.Path | 
|  | 137 |  | 
|  | 138 | // The path to the profile that dexpreopter accepts. It must be in the binary format. If this is | 
|  | 139 | // set, it overrides the profile settings in `dexpreoptProperties`. | 
|  | 140 | inputProfilePathOnHost android.Path | 
| Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 141 |  | 
|  | 142 | // The path to the profile that matches the dex optimized by r8/d8. It is in text format. If this is | 
|  | 143 | // set, it will be converted to a binary profile which will be subsequently used for dexpreopt. | 
|  | 144 | rewrittenProfile android.Path | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 145 | } | 
|  | 146 |  | 
|  | 147 | type DexpreoptProperties struct { | 
|  | 148 | Dex_preopt struct { | 
| Nicolas Geoffray | c1bf724 | 2019-10-18 14:51:38 +0100 | [diff] [blame] | 149 | // If false, prevent dexpreopting.  Defaults to true. | 
| Cole Faust | eb03246 | 2024-09-19 11:12:54 -0700 | [diff] [blame] | 150 | Enabled proptools.Configurable[bool] `android:"replace_instead_of_append"` | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 151 |  | 
|  | 152 | // If true, generate an app image (.art file) for this module. | 
| Cole Faust | eb03246 | 2024-09-19 11:12:54 -0700 | [diff] [blame] | 153 | App_image proptools.Configurable[bool] `android:"replace_instead_of_append"` | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 154 |  | 
|  | 155 | // If true, use a checked-in profile to guide optimization.  Defaults to false unless | 
|  | 156 | // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR | 
|  | 157 | // that matches the name of this module, in which case it is defaulted to true. | 
| Cole Faust | eb03246 | 2024-09-19 11:12:54 -0700 | [diff] [blame] | 158 | Profile_guided proptools.Configurable[bool] `android:"replace_instead_of_append"` | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 159 |  | 
|  | 160 | // If set, provides the path to profile relative to the Android.bp file.  If not set, | 
|  | 161 | // defaults to searching for a file that matches the name of this module in the default | 
|  | 162 | // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found. | 
| Cole Faust | eb03246 | 2024-09-19 11:12:54 -0700 | [diff] [blame] | 163 | Profile proptools.Configurable[string] `android:"path,replace_instead_of_append"` | 
| Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 164 |  | 
|  | 165 | // If set to true, r8/d8 will use `profile` as input to generate a new profile that matches | 
|  | 166 | // the optimized dex. | 
|  | 167 | // The new profile will be subsequently used as the profile to dexpreopt the dex file. | 
| Cole Faust | eb03246 | 2024-09-19 11:12:54 -0700 | [diff] [blame] | 168 | Enable_profile_rewriting proptools.Configurable[bool] `android:"replace_instead_of_append"` | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 169 | } | 
| Jiakai Zhang | 9c4dc19 | 2023-02-09 00:09:24 +0800 | [diff] [blame] | 170 |  | 
|  | 171 | Dex_preopt_result struct { | 
|  | 172 | // True if profile-guided optimization is actually enabled. | 
|  | 173 | Profile_guided bool | 
|  | 174 | } `blueprint:"mutated"` | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | type ImportDexpreoptProperties struct { | 
|  | 178 | Dex_preopt struct { | 
|  | 179 | // If true, use the profile in the prebuilt APEX to guide optimization. Defaults to false. | 
|  | 180 | Profile_guided *bool | 
|  | 181 | } | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 182 | } | 
|  | 183 |  | 
| Ulya Trafimovich | 6cf2c0c | 2020-04-24 12:15:20 +0100 | [diff] [blame] | 184 | func init() { | 
|  | 185 | dexpreopt.DexpreoptRunningInSoong = true | 
|  | 186 | } | 
|  | 187 |  | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 188 | func isApexVariant(ctx android.BaseModuleContext) bool { | 
| Colin Cross | ff694a8 | 2023-12-13 15:54:49 -0800 | [diff] [blame] | 189 | apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider) | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 190 | return !apexInfo.IsForPlatform() | 
|  | 191 | } | 
|  | 192 |  | 
| Jiakai Zhang | 28bc9a8 | 2021-12-20 15:08:57 +0000 | [diff] [blame] | 193 | func forPrebuiltApex(ctx android.BaseModuleContext) bool { | 
| Colin Cross | ff694a8 | 2023-12-13 15:54:49 -0800 | [diff] [blame] | 194 | apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider) | 
| Jiakai Zhang | 28bc9a8 | 2021-12-20 15:08:57 +0000 | [diff] [blame] | 195 | return apexInfo.ForPrebuiltApex | 
|  | 196 | } | 
|  | 197 |  | 
| Spandan Das | a8afdcb | 2024-02-29 06:40:16 +0000 | [diff] [blame] | 198 | // For apex variant of modules, this returns true on the source variant if the prebuilt apex | 
|  | 199 | // has been selected using apex_contributions. | 
|  | 200 | // The prebuilt apex will be responsible for generating the dexpreopt rules of the deapexed java lib. | 
|  | 201 | func disableSourceApexVariant(ctx android.BaseModuleContext) bool { | 
|  | 202 | if !isApexVariant(ctx) { | 
|  | 203 | return false // platform variant | 
|  | 204 | } | 
|  | 205 | apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider) | 
|  | 206 | psi := android.PrebuiltSelectionInfoMap{} | 
| Jihoon Kang | a3a0546 | 2024-04-05 00:36:44 +0000 | [diff] [blame] | 207 | ctx.VisitDirectDeps(func(am android.Module) { | 
|  | 208 | if prebuiltSelectionInfo, ok := android.OtherModuleProvider(ctx, am, android.PrebuiltSelectionInfoProvider); ok { | 
|  | 209 | psi = prebuiltSelectionInfo | 
|  | 210 | } | 
| Spandan Das | a8afdcb | 2024-02-29 06:40:16 +0000 | [diff] [blame] | 211 | }) | 
| Spandan Das | 003452f | 2024-09-06 00:56:25 +0000 | [diff] [blame] | 212 |  | 
| Spandan Das | a8afdcb | 2024-02-29 06:40:16 +0000 | [diff] [blame] | 213 | // Find the apex variant for this module | 
| Spandan Das | 003452f | 2024-09-06 00:56:25 +0000 | [diff] [blame] | 214 | apexVariantsWithoutTestApexes := []string{} | 
| Spandan Das | ed7a030 | 2024-08-26 18:06:25 +0000 | [diff] [blame] | 215 | if apexInfo.BaseApexName != "" { | 
|  | 216 | // This is a transitive dependency of an override_apex | 
| Spandan Das | 003452f | 2024-09-06 00:56:25 +0000 | [diff] [blame] | 217 | apexVariantsWithoutTestApexes = append(apexVariantsWithoutTestApexes, apexInfo.BaseApexName) | 
| Spandan Das | ed7a030 | 2024-08-26 18:06:25 +0000 | [diff] [blame] | 218 | } else { | 
| Spandan Das | 003452f | 2024-09-06 00:56:25 +0000 | [diff] [blame] | 219 | _, variants, _ := android.ListSetDifference(apexInfo.InApexVariants, apexInfo.TestApexes) | 
|  | 220 | apexVariantsWithoutTestApexes = append(apexVariantsWithoutTestApexes, variants...) | 
|  | 221 | } | 
|  | 222 | if apexInfo.ApexAvailableName != "" { | 
|  | 223 | apexVariantsWithoutTestApexes = append(apexVariantsWithoutTestApexes, apexInfo.ApexAvailableName) | 
| Spandan Das | ed7a030 | 2024-08-26 18:06:25 +0000 | [diff] [blame] | 224 | } | 
| Spandan Das | a8afdcb | 2024-02-29 06:40:16 +0000 | [diff] [blame] | 225 | disableSource := false | 
|  | 226 | // find the selected apexes | 
|  | 227 | for _, apexVariant := range apexVariantsWithoutTestApexes { | 
| Spandan Das | ed7a030 | 2024-08-26 18:06:25 +0000 | [diff] [blame] | 228 | if len(psi.GetSelectedModulesForApiDomain(apexVariant)) > 0 { | 
|  | 229 | // If the apex_contribution for this api domain is non-empty, disable the source variant | 
|  | 230 | disableSource = true | 
| Spandan Das | a8afdcb | 2024-02-29 06:40:16 +0000 | [diff] [blame] | 231 | } | 
|  | 232 | } | 
|  | 233 | return disableSource | 
|  | 234 | } | 
|  | 235 |  | 
| Jiakai Zhang | cf61e3c | 2023-05-08 16:28:38 +0000 | [diff] [blame] | 236 | // Returns whether dexpreopt is applicable to the module. | 
|  | 237 | // When it returns true, neither profile nor dexpreopt artifacts will be generated. | 
| Spandan Das | e21a8d4 | 2024-01-23 23:56:29 +0000 | [diff] [blame] | 238 | func (d *dexpreopter) dexpreoptDisabled(ctx android.BaseModuleContext, libName string) bool { | 
| Colin Cross | 38310bb | 2021-12-01 10:34:14 -0800 | [diff] [blame] | 239 | if !ctx.Device() { | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 240 | return true | 
|  | 241 | } | 
|  | 242 |  | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 243 | if d.isTest { | 
|  | 244 | return true | 
|  | 245 | } | 
|  | 246 |  | 
| Cole Faust | eb03246 | 2024-09-19 11:12:54 -0700 | [diff] [blame] | 247 | if !d.dexpreoptProperties.Dex_preopt.Enabled.GetOrDefault(ctx, true) { | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 248 | return true | 
|  | 249 | } | 
|  | 250 |  | 
| Spandan Das | 0727ba7 | 2024-02-13 16:37:43 +0000 | [diff] [blame] | 251 | if d.shouldDisableDexpreopt { | 
|  | 252 | return true | 
|  | 253 | } | 
|  | 254 |  | 
| Jiakai Zhang | 28bc9a8 | 2021-12-20 15:08:57 +0000 | [diff] [blame] | 255 | // If the module is from a prebuilt APEX, it shouldn't be installable, but it can still be | 
|  | 256 | // dexpreopted. | 
|  | 257 | if !ctx.Module().(DexpreopterInterface).IsInstallable() && !forPrebuiltApex(ctx) { | 
| Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 258 | return true | 
|  | 259 | } | 
|  | 260 |  | 
| Colin Cross | 38310bb | 2021-12-01 10:34:14 -0800 | [diff] [blame] | 261 | if !android.IsModulePreferred(ctx.Module()) { | 
|  | 262 | return true | 
|  | 263 | } | 
|  | 264 |  | 
| Spandan Das | e21a8d4 | 2024-01-23 23:56:29 +0000 | [diff] [blame] | 265 | if _, isApex := android.ModuleProvider(ctx, android.ApexBundleInfoProvider); isApex { | 
|  | 266 | // dexpreopt rules for system server jars can be generated in the ModuleCtx of prebuilt apexes | 
|  | 267 | return false | 
|  | 268 | } | 
|  | 269 |  | 
| Colin Cross | 38310bb | 2021-12-01 10:34:14 -0800 | [diff] [blame] | 270 | global := dexpreopt.GetGlobalConfig(ctx) | 
|  | 271 |  | 
| Spandan Das | e21a8d4 | 2024-01-23 23:56:29 +0000 | [diff] [blame] | 272 | // Use the libName argument to determine if the library being dexpreopt'd is a system server jar | 
|  | 273 | // ctx.ModuleName() is not safe. In case of prebuilt apexes, the dexpreopt rules of system server jars | 
|  | 274 | // are created in the ctx object of the top-level prebuilt apex. | 
|  | 275 | isApexSystemServerJar := global.AllApexSystemServerJars(ctx).ContainsJar(libName) | 
|  | 276 |  | 
|  | 277 | if _, isApex := android.ModuleProvider(ctx, android.ApexBundleInfoProvider); isApex || isApexVariant(ctx) { | 
|  | 278 | // dexpreopt rules for system server jars can be generated in the ModuleCtx of prebuilt apexes | 
| Jiakai Zhang | 3317ce7 | 2023-02-08 01:19:19 +0800 | [diff] [blame] | 279 | if !isApexSystemServerJar { | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 280 | return true | 
|  | 281 | } | 
| Spandan Das | 50801e2 | 2024-05-13 18:29:45 +0000 | [diff] [blame] | 282 | ai, _ := android.ModuleProvider(ctx, android.ApexInfoProvider) | 
|  | 283 | allApexInfos := []android.ApexInfo{} | 
|  | 284 | if allApexInfosProvider, ok := android.ModuleProvider(ctx, android.AllApexInfoProvider); ok { | 
|  | 285 | allApexInfos = allApexInfosProvider.ApexInfos | 
|  | 286 | } | 
|  | 287 | if len(allApexInfos) > 0 && !ai.MinSdkVersion.EqualTo(allApexInfos[0].MinSdkVersion) { | 
|  | 288 | // Apex system server jars are dexpreopted and installed on to the system image. | 
|  | 289 | // Since we can have BigAndroid and Go variants of system server jar providing apexes, | 
|  | 290 | // and these two variants can have different min_sdk_versions, hide one of the apex variants | 
|  | 291 | // from make to prevent collisions. | 
|  | 292 | // | 
|  | 293 | // Unlike cc, min_sdk_version does not have an effect on the build actions of java libraries. | 
|  | 294 | ctx.Module().MakeUninstallable() | 
|  | 295 | } | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 296 | } else { | 
|  | 297 | // 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] | 298 | if isApexSystemServerJar { | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 299 | return true | 
|  | 300 | } | 
| Yo Chiang | dbdf8f9 | 2020-01-09 19:00:27 +0800 | [diff] [blame] | 301 | } | 
|  | 302 |  | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 303 | // TODO: contains no java code | 
|  | 304 |  | 
|  | 305 | return false | 
|  | 306 | } | 
|  | 307 |  | 
| Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 308 | func dexpreoptToolDepsMutator(ctx android.BottomUpMutatorContext) { | 
| Spandan Das | e21a8d4 | 2024-01-23 23:56:29 +0000 | [diff] [blame] | 309 | if _, isApex := android.ModuleProvider(ctx, android.ApexBundleInfoProvider); isApex && dexpreopt.IsDex2oatNeeded(ctx) { | 
|  | 310 | // prebuilt apexes can genererate rules to dexpreopt deapexed jars | 
|  | 311 | // Add a dex2oat dep aggressively on _every_ apex module | 
|  | 312 | dexpreopt.RegisterToolDeps(ctx) | 
|  | 313 | return | 
|  | 314 | } | 
|  | 315 | if d, ok := ctx.Module().(DexpreopterInterface); !ok || d.dexpreoptDisabled(ctx, android.RemoveOptionalPrebuiltPrefix(ctx.ModuleName())) || !dexpreopt.IsDex2oatNeeded(ctx) { | 
| Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 316 | return | 
|  | 317 | } | 
|  | 318 | dexpreopt.RegisterToolDeps(ctx) | 
|  | 319 | } | 
|  | 320 |  | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 321 | // Returns the install path of the dex jar of a module. | 
|  | 322 | // | 
|  | 323 | // Do not rely on `ApexInfo.ApexVariationName` because it can be something like "apex1000", rather | 
|  | 324 | // than the `name` in the path `/apex/<name>` as suggested in its comment. | 
|  | 325 | // | 
|  | 326 | // This function is on a best-effort basis. It cannot handle the case where an APEX jar is not a | 
|  | 327 | // system server jar, which is fine because we currently only preopt system server jars for APEXes. | 
|  | 328 | func (d *dexpreopter) getInstallPath( | 
| Spandan Das | e21a8d4 | 2024-01-23 23:56:29 +0000 | [diff] [blame] | 329 | ctx android.ModuleContext, libName string, defaultInstallPath android.InstallPath) android.InstallPath { | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 330 | global := dexpreopt.GetGlobalConfig(ctx) | 
| Spandan Das | e21a8d4 | 2024-01-23 23:56:29 +0000 | [diff] [blame] | 331 | if global.AllApexSystemServerJars(ctx).ContainsJar(libName) { | 
|  | 332 | dexLocation := dexpreopt.GetSystemServerDexLocation(ctx, global, libName) | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 333 | return android.PathForModuleInPartitionInstall(ctx, "", strings.TrimPrefix(dexLocation, "/")) | 
|  | 334 | } | 
| Spandan Das | e21a8d4 | 2024-01-23 23:56:29 +0000 | [diff] [blame] | 335 | if !d.dexpreoptDisabled(ctx, libName) && isApexVariant(ctx) && | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 336 | filepath.Base(defaultInstallPath.PartitionDir()) != "apex" { | 
|  | 337 | ctx.ModuleErrorf("unable to get the install path of the dex jar for dexpreopt") | 
|  | 338 | } | 
|  | 339 | return defaultInstallPath | 
| Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 340 | } | 
|  | 341 |  | 
| Spandan Das | 2069c3f | 2023-12-06 19:40:24 +0000 | [diff] [blame] | 342 | // DexpreoptPrebuiltApexSystemServerJars generates the dexpreopt artifacts from a jar file that has been deapexed from a prebuilt apex | 
|  | 343 | func (d *Dexpreopter) DexpreoptPrebuiltApexSystemServerJars(ctx android.ModuleContext, libraryName string, di *android.DeapexerInfo) { | 
|  | 344 | // A single prebuilt apex can have multiple apex system jars | 
|  | 345 | // initialize the output path for this dex jar | 
|  | 346 | dc := dexpreopt.GetGlobalConfig(ctx) | 
|  | 347 | d.installPath = android.PathForModuleInPartitionInstall(ctx, "", strings.TrimPrefix(dexpreopt.GetSystemServerDexLocation(ctx, dc, libraryName), "/")) | 
|  | 348 | // generate the rules for creating the .odex and .vdex files for this system server jar | 
| Spandan Das | 5be6333 | 2023-12-13 00:06:32 +0000 | [diff] [blame] | 349 | dexJarFile := di.PrebuiltExportPath(ApexRootRelativePathToJavaLib(libraryName)) | 
| Spandan Das | 2ea84dd | 2024-01-25 22:12:50 +0000 | [diff] [blame] | 350 |  | 
|  | 351 | d.inputProfilePathOnHost = nil // reset: TODO(spandandas): Make dexpreopter stateless | 
|  | 352 | if android.InList(libraryName, di.GetDexpreoptProfileGuidedExportedModuleNames()) { | 
|  | 353 | // Set the profile path to guide optimization | 
|  | 354 | prof := di.PrebuiltExportPath(ApexRootRelativePathToJavaLib(libraryName) + ".prof") | 
|  | 355 | if prof == nil { | 
|  | 356 | ctx.ModuleErrorf("Could not find a .prof file in this prebuilt apex") | 
|  | 357 | } | 
|  | 358 | d.inputProfilePathOnHost = prof | 
|  | 359 | } | 
|  | 360 |  | 
| Spandan Das | e21a8d4 | 2024-01-23 23:56:29 +0000 | [diff] [blame] | 361 | d.dexpreopt(ctx, libraryName, dexJarFile) | 
| Spandan Das | 2069c3f | 2023-12-06 19:40:24 +0000 | [diff] [blame] | 362 | } | 
|  | 363 |  | 
| Colin Cross | 7707b24 | 2024-07-26 12:02:36 -0700 | [diff] [blame] | 364 | func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, libName string, dexJarFile android.Path) { | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 365 | global := dexpreopt.GetGlobalConfig(ctx) | 
|  | 366 |  | 
| Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 367 | // TODO(b/148690468): The check on d.installPath is to bail out in cases where | 
|  | 368 | // the dexpreopter struct hasn't been fully initialized before we're called, | 
|  | 369 | // e.g. in aar.go. This keeps the behaviour that dexpreopting is effectively | 
|  | 370 | // disabled, even if installable is true. | 
| Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 371 | if d.installPath.Base() == "." { | 
|  | 372 | return | 
|  | 373 | } | 
|  | 374 |  | 
|  | 375 | dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath) | 
|  | 376 |  | 
| Spandan Das | e21a8d4 | 2024-01-23 23:56:29 +0000 | [diff] [blame] | 377 | providesUsesLib := libName | 
| Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 378 | if ulib, ok := ctx.Module().(ProvidesUsesLib); ok { | 
|  | 379 | name := ulib.ProvidesUsesLib() | 
|  | 380 | if name != nil { | 
|  | 381 | providesUsesLib = *name | 
|  | 382 | } | 
|  | 383 | } | 
|  | 384 |  | 
| Jeongik Cha | 4b073cd | 2021-06-08 11:35:00 +0900 | [diff] [blame] | 385 | // If it is test, make config files regardless of its dexpreopt setting. | 
| Jeongik Cha | c624667 | 2021-04-08 00:00:19 +0900 | [diff] [blame] | 386 | // The config files are required for apps defined in make which depend on the lib. | 
| Spandan Das | e21a8d4 | 2024-01-23 23:56:29 +0000 | [diff] [blame] | 387 | if d.isTest && d.dexpreoptDisabled(ctx, libName) { | 
| Jaewoong Jung | 4b97a56 | 2020-12-17 09:43:28 -0800 | [diff] [blame] | 388 | return | 
| Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 389 | } | 
|  | 390 |  | 
| Spandan Das | e21a8d4 | 2024-01-23 23:56:29 +0000 | [diff] [blame] | 391 | isSystemServerJar := global.AllSystemServerJars(ctx).ContainsJar(libName) | 
| Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 392 |  | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 393 | bootImage := defaultBootImageConfig(ctx) | 
| Jiakai Zhang | b879620 | 2023-03-06 19:16:48 +0000 | [diff] [blame] | 394 | // When `global.PreoptWithUpdatableBcp` is true, `bcpForDexpreopt` below includes the mainline | 
|  | 395 | // boot jars into bootclasspath, so we should include the mainline boot image as well because it's | 
|  | 396 | // generated from those jars. | 
|  | 397 | if global.PreoptWithUpdatableBcp { | 
|  | 398 | bootImage = mainlineBootImageConfig(ctx) | 
|  | 399 | } | 
| Jiakai Zhang | 02669e8 | 2021-09-11 03:44:06 +0000 | [diff] [blame] | 400 | dexFiles, dexLocations := bcpForDexpreopt(ctx, global.PreoptWithUpdatableBcp) | 
| Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 401 |  | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 402 | targets := ctx.MultiTargets() | 
|  | 403 | if len(targets) == 0 { | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 404 | // assume this is a java library, dexpreopt for all arches for now | 
|  | 405 | for _, target := range ctx.Config().Targets[android.Android] { | 
| dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 406 | if target.NativeBridge == android.NativeBridgeDisabled { | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 407 | targets = append(targets, target) | 
| dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 408 | } | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 409 | } | 
| Spandan Das | e21a8d4 | 2024-01-23 23:56:29 +0000 | [diff] [blame] | 410 | if isSystemServerJar && libName != "com.android.location.provider" { | 
| Jiakai Zhang | 2fbc355 | 2022-11-28 15:38:23 +0000 | [diff] [blame] | 411 | // If the module is a system server jar, only preopt for the primary arch because the jar can | 
|  | 412 | // only be loaded by system server. "com.android.location.provider" is a special case because | 
|  | 413 | // it's also used by apps as a shared library. | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 414 | targets = targets[:1] | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 415 | } | 
|  | 416 | } | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 417 |  | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 418 | var archs []android.ArchType | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 419 | var images android.Paths | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 420 | var imagesDeps []android.OutputPaths | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 421 | for _, target := range targets { | 
|  | 422 | archs = append(archs, target.Arch.ArchType) | 
|  | 423 | variant := bootImage.getVariant(target) | 
| Jeongik Cha | a596909 | 2021-05-07 18:53:21 +0900 | [diff] [blame] | 424 | images = append(images, variant.imagePathOnHost) | 
| David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 425 | imagesDeps = append(imagesDeps, variant.imagesDeps) | 
| Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 426 | } | 
| David Srbecky | ab99498 | 2020-03-30 17:24:13 +0100 | [diff] [blame] | 427 | // The image locations for all Android variants are identical. | 
| Jeongik Cha | 4dda75e | 2021-04-27 23:56:44 +0900 | [diff] [blame] | 428 | hostImageLocations, deviceImageLocations := bootImage.getAnyAndroidVariant().imageLocations() | 
| Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 429 |  | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 430 | var profileClassListing android.OptionalPath | 
| Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 431 | var profileBootListing android.OptionalPath | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 432 | profileIsTextListing := false | 
| Spandan Das | 2ea84dd | 2024-01-25 22:12:50 +0000 | [diff] [blame] | 433 |  | 
| Jiakai Zhang | 81e4681 | 2023-02-08 21:56:07 +0800 | [diff] [blame] | 434 | if d.inputProfilePathOnHost != nil { | 
|  | 435 | profileClassListing = android.OptionalPathForPath(d.inputProfilePathOnHost) | 
| Cole Faust | eb03246 | 2024-09-19 11:12:54 -0700 | [diff] [blame] | 436 | } else if d.dexpreoptProperties.Dex_preopt.Profile_guided.GetOrDefault(ctx, true) && !forPrebuiltApex(ctx) { | 
| Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 437 | // If enable_profile_rewriting is set, use the rewritten profile instead of the checked-in profile | 
| Cole Faust | eb03246 | 2024-09-19 11:12:54 -0700 | [diff] [blame] | 438 | if d.EnableProfileRewriting(ctx) { | 
| Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 439 | profileClassListing = android.OptionalPathForPath(d.GetRewrittenProfile()) | 
|  | 440 | profileIsTextListing = true | 
| Cole Faust | eb03246 | 2024-09-19 11:12:54 -0700 | [diff] [blame] | 441 | } else if profile := d.GetProfile(ctx); profile != "" { | 
| Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 442 | // If dex_preopt.profile_guided is not set, default it based on the existence of the | 
|  | 443 | // dexprepot.profile option or the profile class listing. | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 444 | profileClassListing = android.OptionalPathForPath( | 
| Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 445 | android.PathForModuleSrc(ctx, profile)) | 
| Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 446 | profileBootListing = android.ExistentPathForSource(ctx, | 
| Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 447 | ctx.ModuleDir(), profile+"-boot") | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 448 | profileIsTextListing = true | 
| Dan Willemsen | 78d51b0 | 2020-06-24 16:33:31 -0700 | [diff] [blame] | 449 | } else if global.ProfileDir != "" { | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 450 | profileClassListing = android.ExistentPathForSource(ctx, | 
| Spandan Das | e21a8d4 | 2024-01-23 23:56:29 +0000 | [diff] [blame] | 451 | global.ProfileDir, libName+".prof") | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 452 | } | 
|  | 453 | } | 
|  | 454 |  | 
| Jiakai Zhang | 9c4dc19 | 2023-02-09 00:09:24 +0800 | [diff] [blame] | 455 | d.dexpreoptProperties.Dex_preopt_result.Profile_guided = profileClassListing.Valid() | 
|  | 456 |  | 
| Spandan Das | 2069c3f | 2023-12-06 19:40:24 +0000 | [diff] [blame] | 457 | // A single apex can have multiple system server jars | 
|  | 458 | // Use the dexJar to create a unique scope for each | 
|  | 459 | dexJarStem := strings.TrimSuffix(dexJarFile.Base(), dexJarFile.Ext()) | 
|  | 460 |  | 
| Cole Faust | eb03246 | 2024-09-19 11:12:54 -0700 | [diff] [blame] | 461 | appImage := d.dexpreoptProperties.Dex_preopt.App_image.Get(ctx) | 
|  | 462 |  | 
| Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 463 | // Full dexpreopt config, used to create dexpreopt build rules. | 
| Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 464 | dexpreoptConfig := &dexpreopt.ModuleConfig{ | 
| Spandan Das | e21a8d4 | 2024-01-23 23:56:29 +0000 | [diff] [blame] | 465 | Name:            libName, | 
| Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 466 | DexLocation:     dexLocation, | 
| Spandan Das | e21a8d4 | 2024-01-23 23:56:29 +0000 | [diff] [blame] | 467 | BuildPath:       android.PathForModuleOut(ctx, "dexpreopt", dexJarStem, libName+".jar").OutputPath, | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 468 | DexPath:         dexJarFile, | 
| Jeongik Cha | 33a3a81 | 2021-04-15 09:12:49 +0900 | [diff] [blame] | 469 | ManifestPath:    android.OptionalPathForPath(d.manifestFile), | 
| Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 470 | UncompressedDex: d.uncompressedDex, | 
|  | 471 | HasApkLibraries: false, | 
|  | 472 | PreoptFlags:     nil, | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 473 |  | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 474 | ProfileClassListing:  profileClassListing, | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 475 | ProfileIsTextListing: profileIsTextListing, | 
| Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 476 | ProfileBootListing:   profileBootListing, | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 477 |  | 
| Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 478 | EnforceUsesLibrariesStatusFile: dexpreopt.UsesLibrariesStatusFile(ctx), | 
|  | 479 | EnforceUsesLibraries:           d.enforceUsesLibs, | 
|  | 480 | ProvidesUsesLibrary:            providesUsesLib, | 
|  | 481 | ClassLoaderContexts:            d.classLoaderContexts, | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 482 |  | 
| Jeongik Cha | 4dda75e | 2021-04-27 23:56:44 +0900 | [diff] [blame] | 483 | Archs:                           archs, | 
|  | 484 | DexPreoptImagesDeps:             imagesDeps, | 
|  | 485 | DexPreoptImageLocationsOnHost:   hostImageLocations, | 
|  | 486 | DexPreoptImageLocationsOnDevice: deviceImageLocations, | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 487 |  | 
| Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 488 | PreoptBootClassPathDexFiles:     dexFiles.Paths(), | 
| Vladimir Marko | 40139d6 | 2020-02-06 15:14:29 +0000 | [diff] [blame] | 489 | PreoptBootClassPathDexLocations: dexLocations, | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 490 |  | 
| Cole Faust | eb03246 | 2024-09-19 11:12:54 -0700 | [diff] [blame] | 491 | NoCreateAppImage:    !appImage.GetOrDefault(true), | 
|  | 492 | ForceCreateAppImage: appImage.GetOrDefault(false), | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 493 |  | 
| Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 494 | PresignedPrebuilt: d.isPresignedPrebuilt, | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 495 | } | 
|  | 496 |  | 
| Spandan Das | 950deca | 2024-10-01 18:35:23 +0000 | [diff] [blame] | 497 | if ctx.Config().InstallApexSystemServerDexpreoptSamePartition() { | 
|  | 498 | dexpreoptConfig.ApexPartition = android.PathForModuleInstall(ctx).Partition() | 
|  | 499 | } else { | 
|  | 500 | dexpreoptConfig.ApexPartition = "system" | 
|  | 501 | } | 
|  | 502 |  | 
| Spandan Das | 2069c3f | 2023-12-06 19:40:24 +0000 | [diff] [blame] | 503 | d.configPath = android.PathForModuleOut(ctx, "dexpreopt", dexJarStem, "dexpreopt.config") | 
| Jeongik Cha | c624667 | 2021-04-08 00:00:19 +0900 | [diff] [blame] | 504 | dexpreopt.WriteModuleConfig(ctx, dexpreoptConfig, d.configPath) | 
| Colin Cross | a6182ab | 2024-08-21 10:47:44 -0700 | [diff] [blame] | 505 | ctx.CheckbuildFile(d.configPath) | 
| Jeongik Cha | c624667 | 2021-04-08 00:00:19 +0900 | [diff] [blame] | 506 |  | 
| Spandan Das | e21a8d4 | 2024-01-23 23:56:29 +0000 | [diff] [blame] | 507 | if d.dexpreoptDisabled(ctx, libName) { | 
| Jeongik Cha | c624667 | 2021-04-08 00:00:19 +0900 | [diff] [blame] | 508 | return | 
|  | 509 | } | 
|  | 510 |  | 
|  | 511 | globalSoong := dexpreopt.GetGlobalSoongConfig(ctx) | 
|  | 512 |  | 
| Jiakai Zhang | 51b2a8b | 2023-06-26 16:47:38 +0100 | [diff] [blame] | 513 | // The root "product_packages.txt" is generated by `build/make/core/Makefile`. It contains a list | 
|  | 514 | // of all packages that are installed on the device. We use `grep` to filter the list by the app's | 
|  | 515 | // dependencies to create a per-app list, and use `rsync --checksum` to prevent the file's mtime | 
|  | 516 | // from being changed if the contents don't change. This avoids unnecessary dexpreopt reruns. | 
| Jiakai Zhang | a449678 | 2023-05-17 16:57:30 +0100 | [diff] [blame] | 517 | productPackages := android.PathForModuleInPartitionInstall(ctx, "", "product_packages.txt") | 
| Spandan Das | 2069c3f | 2023-12-06 19:40:24 +0000 | [diff] [blame] | 518 | appProductPackages := android.PathForModuleOut(ctx, "dexpreopt", dexJarStem, "product_packages.txt") | 
| Jiakai Zhang | 51b2a8b | 2023-06-26 16:47:38 +0100 | [diff] [blame] | 519 | appProductPackagesStaging := appProductPackages.ReplaceExtension(ctx, "txt.tmp") | 
|  | 520 | clcNames, _ := dexpreopt.ComputeClassLoaderContextDependencies(dexpreoptConfig.ClassLoaderContexts) | 
|  | 521 | sort.Strings(clcNames) // The order needs to be deterministic. | 
|  | 522 | productPackagesRule := android.NewRuleBuilder(pctx, ctx) | 
|  | 523 | if len(clcNames) > 0 { | 
|  | 524 | productPackagesRule.Command(). | 
|  | 525 | Text("grep -F -x"). | 
|  | 526 | FlagForEachArg("-e ", clcNames). | 
|  | 527 | Input(productPackages). | 
|  | 528 | FlagWithOutput("> ", appProductPackagesStaging). | 
|  | 529 | Text("|| true") | 
|  | 530 | } else { | 
|  | 531 | productPackagesRule.Command(). | 
|  | 532 | Text("rm -f").Output(appProductPackagesStaging). | 
|  | 533 | Text("&&"). | 
|  | 534 | Text("touch").Output(appProductPackagesStaging) | 
|  | 535 | } | 
|  | 536 | productPackagesRule.Command(). | 
|  | 537 | Text("rsync --checksum"). | 
|  | 538 | Input(appProductPackagesStaging). | 
|  | 539 | Output(appProductPackages) | 
| Spandan Das | 2069c3f | 2023-12-06 19:40:24 +0000 | [diff] [blame] | 540 | productPackagesRule.Restat().Build("product_packages."+dexJarStem, "dexpreopt product_packages") | 
| Jiakai Zhang | a449678 | 2023-05-17 16:57:30 +0100 | [diff] [blame] | 541 |  | 
| Spandan Das | 5ae65ee | 2024-04-16 22:03:26 +0000 | [diff] [blame] | 542 | // Prebuilts are active, do not copy the dexpreopt'd source javalib to out/soong/system_server_dexjars | 
|  | 543 | // The javalib from the deapexed prebuilt will be copied to this location. | 
|  | 544 | // TODO (b/331665856): Implement a principled solution for this. | 
| Spandan Das | 50801e2 | 2024-05-13 18:29:45 +0000 | [diff] [blame] | 545 | copyApexSystemServerJarDex := !disableSourceApexVariant(ctx) && !ctx.Module().IsHideFromMake() | 
| Jiakai Zhang | a449678 | 2023-05-17 16:57:30 +0100 | [diff] [blame] | 546 | dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule( | 
| Spandan Das | 5ae65ee | 2024-04-16 22:03:26 +0000 | [diff] [blame] | 547 | ctx, globalSoong, global, dexpreoptConfig, appProductPackages, copyApexSystemServerJarDex) | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 548 | if err != nil { | 
|  | 549 | ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error()) | 
| Jaewoong Jung | 4b97a56 | 2020-12-17 09:43:28 -0800 | [diff] [blame] | 550 | return | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 551 | } | 
|  | 552 |  | 
| Spandan Das | 2069c3f | 2023-12-06 19:40:24 +0000 | [diff] [blame] | 553 | dexpreoptRule.Build("dexpreopt"+"."+dexJarStem, "dexpreopt") | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 554 |  | 
| Spandan Das | 2069c3f | 2023-12-06 19:40:24 +0000 | [diff] [blame] | 555 | // The current ctx might be of a deapexer module created by a prebuilt apex | 
|  | 556 | // Use the path of the dex file to determine the library name | 
|  | 557 | isApexSystemServerJar := global.AllApexSystemServerJars(ctx).ContainsJar(dexJarStem) | 
| Jiakai Zhang | 389a647 | 2021-12-14 18:54:06 +0000 | [diff] [blame] | 558 |  | 
| Justin Yun | 613bdc5 | 2024-06-12 21:32:10 +0900 | [diff] [blame] | 559 | dexpreoptPartition := d.installPath.Partition() | 
|  | 560 | // dexpreoptPartition is set to empty for dexpreopts of system APEX and system_other. | 
|  | 561 | // In case of system APEX, however, we can set it to "system" manually. | 
|  | 562 | // TODO(b/346662300): Let dexpreopter generate the installPath for dexpreopt files instead of | 
|  | 563 | // using the dex location to generate the installPath. | 
|  | 564 | if isApexSystemServerJar { | 
| Spandan Das | 906222c | 2024-10-17 18:29:54 +0000 | [diff] [blame] | 565 | dexpreoptPartition = dexpreoptConfig.ApexPartition | 
| Justin Yun | 613bdc5 | 2024-06-12 21:32:10 +0900 | [diff] [blame] | 566 | } | 
| Colin Cross | 1d0eb7a | 2021-11-03 14:08:20 -0700 | [diff] [blame] | 567 | for _, install := range dexpreoptRule.Installs() { | 
|  | 568 | // Remove the "/" prefix because the path should be relative to $ANDROID_PRODUCT_OUT. | 
|  | 569 | installDir := strings.TrimPrefix(filepath.Dir(install.To), "/") | 
| Justin Yun | 613bdc5 | 2024-06-12 21:32:10 +0900 | [diff] [blame] | 570 | partition := dexpreoptPartition | 
| Justin Yun | 22c8aca | 2024-06-05 20:25:03 +0900 | [diff] [blame] | 571 | if strings.HasPrefix(installDir, partition+"/") { | 
|  | 572 | installDir = strings.TrimPrefix(installDir, partition+"/") | 
|  | 573 | } else { | 
|  | 574 | // If the partition for the installDir is different from the install partition, set the | 
|  | 575 | // partition empty to install the dexpreopt files to the desired partition. | 
|  | 576 | // TODO(b/346439786): Define and use the dexpreopt module type to avoid this mismatch. | 
|  | 577 | partition = "" | 
|  | 578 | } | 
| Colin Cross | 1d0eb7a | 2021-11-03 14:08:20 -0700 | [diff] [blame] | 579 | installBase := filepath.Base(install.To) | 
|  | 580 | arch := filepath.Base(installDir) | 
| Justin Yun | 22c8aca | 2024-06-05 20:25:03 +0900 | [diff] [blame] | 581 | installPath := android.PathForModuleInPartitionInstall(ctx, partition, installDir) | 
| Jiakai Zhang | 3317ce7 | 2023-02-08 01:19:19 +0800 | [diff] [blame] | 582 | isProfile := strings.HasSuffix(installBase, ".prof") | 
|  | 583 |  | 
|  | 584 | if isProfile { | 
| Jiakai Zhang | 81e4681 | 2023-02-08 21:56:07 +0800 | [diff] [blame] | 585 | d.outputProfilePathOnHost = install.From | 
| Jiakai Zhang | 3317ce7 | 2023-02-08 01:19:19 +0800 | [diff] [blame] | 586 | } | 
| Colin Cross | 1d0eb7a | 2021-11-03 14:08:20 -0700 | [diff] [blame] | 587 |  | 
| Jiakai Zhang | 389a647 | 2021-12-14 18:54:06 +0000 | [diff] [blame] | 588 | if isApexSystemServerJar { | 
| Jiakai Zhang | 3317ce7 | 2023-02-08 01:19:19 +0800 | [diff] [blame] | 589 | // Profiles are handled separately because they are installed into the APEX. | 
|  | 590 | if !isProfile { | 
|  | 591 | // APEX variants of java libraries are hidden from Make, so their dexpreopt | 
|  | 592 | // outputs need special handling. Currently, for APEX variants of java | 
|  | 593 | // libraries, only those in the system server classpath are handled here. | 
|  | 594 | // Preopting of boot classpath jars in the ART APEX are handled in | 
|  | 595 | // java/dexpreopt_bootjars.go, and other APEX jars are not preopted. | 
|  | 596 | // The installs will be handled by Make as sub-modules of the java library. | 
| Spandan Das | 746161d | 2024-08-21 22:47:53 +0000 | [diff] [blame] | 597 | di := dexpreopterInstall{ | 
| Jiakai Zhang | 3317ce7 | 2023-02-08 01:19:19 +0800 | [diff] [blame] | 598 | name:                arch + "-" + installBase, | 
| Spandan Das | e21a8d4 | 2024-01-23 23:56:29 +0000 | [diff] [blame] | 599 | moduleName:          libName, | 
| Jiakai Zhang | 3317ce7 | 2023-02-08 01:19:19 +0800 | [diff] [blame] | 600 | outputPathOnHost:    install.From, | 
|  | 601 | installDirOnDevice:  installPath, | 
|  | 602 | installFileOnDevice: installBase, | 
| Spandan Das | 746161d | 2024-08-21 22:47:53 +0000 | [diff] [blame] | 603 | } | 
|  | 604 | ctx.InstallFile(di.installDirOnDevice, di.installFileOnDevice, di.outputPathOnHost) | 
|  | 605 | d.builtInstalledForApex = append(d.builtInstalledForApex, di) | 
|  | 606 |  | 
| Jiakai Zhang | 3317ce7 | 2023-02-08 01:19:19 +0800 | [diff] [blame] | 607 | } | 
| Colin Cross | fa9bfcd | 2021-11-10 16:42:38 -0800 | [diff] [blame] | 608 | } else if !d.preventInstall { | 
| Colin Cross | a6182ab | 2024-08-21 10:47:44 -0700 | [diff] [blame] | 609 | // Install without adding to checkbuild to match behavior of previous Make-based checkbuild rules | 
|  | 610 | ctx.InstallFileWithoutCheckbuild(installPath, installBase, install.From) | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 611 | } | 
| Colin Cross | 1d0eb7a | 2021-11-03 14:08:20 -0700 | [diff] [blame] | 612 | } | 
|  | 613 |  | 
| Jiakai Zhang | 389a647 | 2021-12-14 18:54:06 +0000 | [diff] [blame] | 614 | if !isApexSystemServerJar { | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 615 | d.builtInstalled = dexpreoptRule.Installs().String() | 
|  | 616 | } | 
|  | 617 | } | 
|  | 618 |  | 
| Justin Yun | 22c8aca | 2024-06-05 20:25:03 +0900 | [diff] [blame] | 619 | func getModuleInstallPathInfo(ctx android.ModuleContext, fullInstallPath string) (android.InstallPath, string, string) { | 
|  | 620 | installPath := android.PathForModuleInstall(ctx) | 
|  | 621 | installDir, installBase := filepath.Split(strings.TrimPrefix(fullInstallPath, "/")) | 
|  | 622 |  | 
|  | 623 | if !strings.HasPrefix(installDir, installPath.Partition()+"/") { | 
|  | 624 | // Return empty filename if the install partition is not for the target image. | 
|  | 625 | return installPath, "", "" | 
|  | 626 | } | 
|  | 627 | relDir, err := filepath.Rel(installPath.Partition(), installDir) | 
|  | 628 | if err != nil { | 
|  | 629 | panic(err) | 
|  | 630 | } | 
|  | 631 | return installPath, relDir, installBase | 
|  | 632 | } | 
|  | 633 |  | 
| Spandan Das | 29207b5 | 2024-07-30 23:28:17 +0000 | [diff] [blame] | 634 | // installFile will install the file if `install` path and the target install partition are the same. | 
|  | 635 | func installFile(ctx android.ModuleContext, install android.RuleBuilderInstall) { | 
| Justin Yun | 22c8aca | 2024-06-05 20:25:03 +0900 | [diff] [blame] | 636 | installPath, relDir, name := getModuleInstallPathInfo(ctx, install.To) | 
|  | 637 | // Empty name means the install partition is not for the target image. | 
|  | 638 | // For the system image, files for "apex" and "system_other" are skipped here. | 
|  | 639 | // The skipped "apex" files are for testing only, for example, | 
|  | 640 | // "/apex/art_boot_images/javalib/x86/boot.vdex". | 
|  | 641 | // TODO(b/320196894): Files for "system_other" are skipped because soong creates the system | 
|  | 642 | // image only for now. | 
|  | 643 | if name != "" { | 
| Spandan Das | 29207b5 | 2024-07-30 23:28:17 +0000 | [diff] [blame] | 644 | ctx.InstallFile(installPath.Join(ctx, relDir), name, install.From) | 
| Justin Yun | 22c8aca | 2024-06-05 20:25:03 +0900 | [diff] [blame] | 645 | } | 
|  | 646 | } | 
|  | 647 |  | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 648 | func (d *dexpreopter) DexpreoptBuiltInstalledForApex() []dexpreopterInstall { | 
|  | 649 | return d.builtInstalledForApex | 
|  | 650 | } | 
|  | 651 |  | 
|  | 652 | func (d *dexpreopter) AndroidMkEntriesForApex() []android.AndroidMkEntries { | 
|  | 653 | var entries []android.AndroidMkEntries | 
|  | 654 | for _, install := range d.builtInstalledForApex { | 
| Jiakai Zhang | 6decef9 | 2022-01-12 17:56:19 +0000 | [diff] [blame] | 655 | entries = append(entries, install.ToMakeEntries()) | 
| Jiakai Zhang | ca9bc98 | 2021-09-09 08:09:41 +0000 | [diff] [blame] | 656 | } | 
|  | 657 | return entries | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 658 | } | 
| Jiakai Zhang | 3317ce7 | 2023-02-08 01:19:19 +0800 | [diff] [blame] | 659 |  | 
| Jiakai Zhang | 81e4681 | 2023-02-08 21:56:07 +0800 | [diff] [blame] | 660 | func (d *dexpreopter) OutputProfilePathOnHost() android.Path { | 
|  | 661 | return d.outputProfilePathOnHost | 
| Jiakai Zhang | 3317ce7 | 2023-02-08 01:19:19 +0800 | [diff] [blame] | 662 | } | 
| Spandan Das | 0727ba7 | 2024-02-13 16:37:43 +0000 | [diff] [blame] | 663 |  | 
|  | 664 | func (d *dexpreopter) disableDexpreopt() { | 
|  | 665 | d.shouldDisableDexpreopt = true | 
|  | 666 | } | 
| Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 667 |  | 
| Cole Faust | eb03246 | 2024-09-19 11:12:54 -0700 | [diff] [blame] | 668 | func (d *dexpreopter) EnableProfileRewriting(ctx android.BaseModuleContext) bool { | 
|  | 669 | return d.dexpreoptProperties.Dex_preopt.Enable_profile_rewriting.GetOrDefault(ctx, false) | 
| Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 670 | } | 
|  | 671 |  | 
| Cole Faust | eb03246 | 2024-09-19 11:12:54 -0700 | [diff] [blame] | 672 | func (d *dexpreopter) GetProfile(ctx android.BaseModuleContext) string { | 
|  | 673 | return d.dexpreoptProperties.Dex_preopt.Profile.GetOrDefault(ctx, "") | 
| Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 674 | } | 
|  | 675 |  | 
| Cole Faust | eb03246 | 2024-09-19 11:12:54 -0700 | [diff] [blame] | 676 | func (d *dexpreopter) GetProfileGuided(ctx android.BaseModuleContext) bool { | 
|  | 677 | return d.dexpreoptProperties.Dex_preopt.Profile_guided.GetOrDefault(ctx, false) | 
| Spandan Das | 3dbda18 | 2024-05-20 22:23:10 +0000 | [diff] [blame] | 678 | } | 
|  | 679 |  | 
|  | 680 | func (d *dexpreopter) GetRewrittenProfile() android.Path { | 
|  | 681 | return d.rewrittenProfile | 
|  | 682 | } | 
|  | 683 |  | 
|  | 684 | func (d *dexpreopter) SetRewrittenProfile(p android.Path) { | 
|  | 685 | d.rewrittenProfile = p | 
|  | 686 | } |