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 ( |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 18 | "android/soong/android" |
| 19 | "android/soong/dexpreopt" |
| 20 | ) |
| 21 | |
Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 22 | type dexpreopterInterface interface { |
| 23 | IsInstallable() bool // Structs that embed dexpreopter must implement this. |
| 24 | dexpreoptDisabled(ctx android.BaseModuleContext) bool |
| 25 | } |
| 26 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 27 | type dexpreopter struct { |
| 28 | dexpreoptProperties DexpreoptProperties |
| 29 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 30 | installPath android.InstallPath |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 31 | uncompressedDex bool |
| 32 | isSDKLibrary bool |
Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 33 | isApp bool |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 34 | isTest bool |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 35 | isPresignedPrebuilt bool |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 36 | |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 37 | manifestFile android.Path |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 38 | statusFile android.WritablePath |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 39 | enforceUsesLibs bool |
| 40 | classLoaderContexts dexpreopt.ClassLoaderContextMap |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 41 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 42 | builtInstalled string |
Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 43 | |
| 44 | // A path to a dexpreopt.config file generated by Soong for libraries that may be used as a |
| 45 | // <uses-library> by Make modules. The path is passed to Make via LOCAL_SOONG_DEXPREOPT_CONFIG |
| 46 | // variable. If the path is nil, no config is generated (which is the case for apps and tests). |
| 47 | configPath android.WritablePath |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | type DexpreoptProperties struct { |
| 51 | Dex_preopt struct { |
Nicolas Geoffray | c1bf724 | 2019-10-18 14:51:38 +0100 | [diff] [blame] | 52 | // If false, prevent dexpreopting. Defaults to true. |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 53 | Enabled *bool |
| 54 | |
| 55 | // If true, generate an app image (.art file) for this module. |
| 56 | App_image *bool |
| 57 | |
| 58 | // If true, use a checked-in profile to guide optimization. Defaults to false unless |
| 59 | // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR |
| 60 | // that matches the name of this module, in which case it is defaulted to true. |
| 61 | Profile_guided *bool |
| 62 | |
| 63 | // If set, provides the path to profile relative to the Android.bp file. If not set, |
| 64 | // defaults to searching for a file that matches the name of this module in the default |
| 65 | // 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] | 66 | Profile *string `android:"path"` |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Ulya Trafimovich | 6cf2c0c | 2020-04-24 12:15:20 +0100 | [diff] [blame] | 70 | func init() { |
| 71 | dexpreopt.DexpreoptRunningInSoong = true |
| 72 | } |
| 73 | |
Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 74 | func (d *dexpreopter) dexpreoptDisabled(ctx android.BaseModuleContext) bool { |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 75 | global := dexpreopt.GetGlobalConfig(ctx) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 76 | |
| 77 | if global.DisablePreopt { |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 78 | return true |
| 79 | } |
| 80 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 81 | if inList(ctx.ModuleName(), global.DisablePreoptModules) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 82 | return true |
| 83 | } |
| 84 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 85 | if d.isTest { |
| 86 | return true |
| 87 | } |
| 88 | |
| 89 | if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) { |
| 90 | return true |
| 91 | } |
| 92 | |
Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 93 | if !ctx.Module().(dexpreopterInterface).IsInstallable() { |
| 94 | return true |
| 95 | } |
| 96 | |
| 97 | if ctx.Host() { |
Colin Cross | dc2da91 | 2019-01-05 22:13:05 -0800 | [diff] [blame] | 98 | return true |
| 99 | } |
| 100 | |
Yo Chiang | dbdf8f9 | 2020-01-09 19:00:27 +0800 | [diff] [blame] | 101 | // Don't preopt APEX variant module |
Colin Cross | 56a8321 | 2020-09-15 18:30:11 -0700 | [diff] [blame] | 102 | if apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo); !apexInfo.IsForPlatform() { |
Yo Chiang | dbdf8f9 | 2020-01-09 19:00:27 +0800 | [diff] [blame] | 103 | return true |
| 104 | } |
| 105 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 106 | // TODO: contains no java code |
| 107 | |
| 108 | return false |
| 109 | } |
| 110 | |
Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 111 | func dexpreoptToolDepsMutator(ctx android.BottomUpMutatorContext) { |
| 112 | if d, ok := ctx.Module().(dexpreopterInterface); !ok || d.dexpreoptDisabled(ctx) { |
| 113 | return |
| 114 | } |
| 115 | dexpreopt.RegisterToolDeps(ctx) |
| 116 | } |
| 117 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 118 | func odexOnSystemOther(ctx android.ModuleContext, installPath android.InstallPath) bool { |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 119 | return dexpreopt.OdexOnSystemOtherByName(ctx.ModuleName(), android.InstallPathToOnDevicePath(ctx, installPath), dexpreopt.GetGlobalConfig(ctx)) |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Paul Duffin | 612e610 | 2021-02-02 13:38:13 +0000 | [diff] [blame] | 122 | func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.WritablePath) { |
Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 123 | // TODO(b/148690468): The check on d.installPath is to bail out in cases where |
| 124 | // the dexpreopter struct hasn't been fully initialized before we're called, |
| 125 | // e.g. in aar.go. This keeps the behaviour that dexpreopting is effectively |
| 126 | // disabled, even if installable is true. |
Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 127 | if d.installPath.Base() == "." { |
| 128 | return |
| 129 | } |
| 130 | |
| 131 | dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath) |
| 132 | |
Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 133 | providesUsesLib := ctx.ModuleName() |
| 134 | if ulib, ok := ctx.Module().(ProvidesUsesLib); ok { |
| 135 | name := ulib.ProvidesUsesLib() |
| 136 | if name != nil { |
| 137 | providesUsesLib = *name |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if !d.isApp && !d.isTest { |
| 142 | // Slim dexpreopt config is serialized to dexpreopt.config files and used by |
| 143 | // dex_preopt_config_merger.py to get information about <uses-library> dependencies. |
| 144 | // Note that it might be needed even if dexpreopt is disabled for this module. |
| 145 | slimDexpreoptConfig := &dexpreopt.ModuleConfig{ |
| 146 | Name: ctx.ModuleName(), |
| 147 | DexLocation: dexLocation, |
Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 148 | EnforceUsesLibraries: d.enforceUsesLibs, |
| 149 | ProvidesUsesLibrary: providesUsesLib, |
| 150 | ClassLoaderContexts: d.classLoaderContexts, |
| 151 | // The rest of the fields are not needed. |
| 152 | } |
| 153 | d.configPath = android.PathForModuleOut(ctx, "dexpreopt", "dexpreopt.config") |
| 154 | dexpreopt.WriteSlimModuleConfigForMake(ctx, slimDexpreoptConfig, d.configPath) |
| 155 | } |
| 156 | |
| 157 | if d.dexpreoptDisabled(ctx) { |
Jaewoong Jung | 4b97a56 | 2020-12-17 09:43:28 -0800 | [diff] [blame] | 158 | return |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 161 | globalSoong := dexpreopt.GetGlobalSoongConfig(ctx) |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 162 | global := dexpreopt.GetGlobalConfig(ctx) |
Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 163 | |
| 164 | isSystemServerJar := inList(ctx.ModuleName(), global.SystemServerJars) |
| 165 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 166 | bootImage := defaultBootImageConfig(ctx) |
Vladimir Marko | 40139d6 | 2020-02-06 15:14:29 +0000 | [diff] [blame] | 167 | if global.UseArtImage { |
| 168 | bootImage = artBootImageConfig(ctx) |
| 169 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 170 | |
Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 171 | // System server jars are an exception: they are dexpreopted without updatable bootclasspath. |
| 172 | dexFiles, dexLocations := bcpForDexpreopt(ctx, global.PreoptWithUpdatableBcp && !isSystemServerJar) |
| 173 | |
David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 174 | targets := ctx.MultiTargets() |
| 175 | if len(targets) == 0 { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 176 | // assume this is a java library, dexpreopt for all arches for now |
| 177 | for _, target := range ctx.Config().Targets[android.Android] { |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 178 | if target.NativeBridge == android.NativeBridgeDisabled { |
David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 179 | targets = append(targets, target) |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 180 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 181 | } |
Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 182 | if isSystemServerJar && !d.isSDKLibrary { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 183 | // If the module is not an SDK library and it's a system server jar, only preopt the primary arch. |
David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 184 | targets = targets[:1] |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 187 | |
David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 188 | var archs []android.ArchType |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 189 | var images android.Paths |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 190 | var imagesDeps []android.OutputPaths |
David Srbecky | c177ebe | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 191 | for _, target := range targets { |
| 192 | archs = append(archs, target.Arch.ArchType) |
| 193 | variant := bootImage.getVariant(target) |
| 194 | images = append(images, variant.images) |
| 195 | imagesDeps = append(imagesDeps, variant.imagesDeps) |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 196 | } |
David Srbecky | ab99498 | 2020-03-30 17:24:13 +0100 | [diff] [blame] | 197 | // The image locations for all Android variants are identical. |
| 198 | imageLocations := bootImage.getAnyAndroidVariant().imageLocations() |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 199 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 200 | var profileClassListing android.OptionalPath |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 201 | var profileBootListing android.OptionalPath |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 202 | profileIsTextListing := false |
| 203 | if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) { |
| 204 | // If dex_preopt.profile_guided is not set, default it based on the existence of the |
| 205 | // dexprepot.profile option or the profile class listing. |
| 206 | if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" { |
| 207 | profileClassListing = android.OptionalPathForPath( |
| 208 | android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile))) |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 209 | profileBootListing = android.ExistentPathForSource(ctx, |
| 210 | ctx.ModuleDir(), String(d.dexpreoptProperties.Dex_preopt.Profile)+"-boot") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 211 | profileIsTextListing = true |
Dan Willemsen | 78d51b0 | 2020-06-24 16:33:31 -0700 | [diff] [blame] | 212 | } else if global.ProfileDir != "" { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 213 | profileClassListing = android.ExistentPathForSource(ctx, |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 214 | global.ProfileDir, ctx.ModuleName()+".prof") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Ulya Trafimovich | 76b0852 | 2021-01-14 17:52:43 +0000 | [diff] [blame] | 218 | // Full dexpreopt config, used to create dexpreopt build rules. |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 219 | dexpreoptConfig := &dexpreopt.ModuleConfig{ |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 220 | Name: ctx.ModuleName(), |
| 221 | DexLocation: dexLocation, |
Ulya Trafimovich | c0f6479 | 2021-02-04 10:04:39 +0000 | [diff] [blame] | 222 | BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 223 | DexPath: dexJarFile, |
Jeongik Cha | 33a3a81 | 2021-04-15 09:12:49 +0900 | [diff] [blame^] | 224 | ManifestPath: android.OptionalPathForPath(d.manifestFile), |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 225 | UncompressedDex: d.uncompressedDex, |
| 226 | HasApkLibraries: false, |
| 227 | PreoptFlags: nil, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 228 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 229 | ProfileClassListing: profileClassListing, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 230 | ProfileIsTextListing: profileIsTextListing, |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 231 | ProfileBootListing: profileBootListing, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 232 | |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 233 | EnforceUsesLibrariesStatusFile: dexpreopt.UsesLibrariesStatusFile(ctx), |
| 234 | EnforceUsesLibraries: d.enforceUsesLibs, |
| 235 | ProvidesUsesLibrary: providesUsesLib, |
| 236 | ClassLoaderContexts: d.classLoaderContexts, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 237 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 238 | Archs: archs, |
| 239 | DexPreoptImages: images, |
| 240 | DexPreoptImagesDeps: imagesDeps, |
David Srbecky | 1aacc6c | 2020-03-26 11:10:45 +0000 | [diff] [blame] | 241 | DexPreoptImageLocations: imageLocations, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 242 | |
Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 243 | PreoptBootClassPathDexFiles: dexFiles.Paths(), |
Vladimir Marko | 40139d6 | 2020-02-06 15:14:29 +0000 | [diff] [blame] | 244 | PreoptBootClassPathDexLocations: dexLocations, |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 245 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 246 | PreoptExtractedApk: false, |
| 247 | |
| 248 | NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true), |
| 249 | ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false), |
| 250 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 251 | PresignedPrebuilt: d.isPresignedPrebuilt, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 254 | dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, globalSoong, global, dexpreoptConfig) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 255 | if err != nil { |
| 256 | ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error()) |
Jaewoong Jung | 4b97a56 | 2020-12-17 09:43:28 -0800 | [diff] [blame] | 257 | return |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 260 | dexpreoptRule.Build("dexpreopt", "dexpreopt") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 261 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 262 | d.builtInstalled = dexpreoptRule.Installs().String() |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 263 | } |