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