| 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 | 
|  | 33 | isTest              bool | 
| Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 34 | isPresignedPrebuilt bool | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 35 |  | 
| Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 36 | manifestFile     android.Path | 
|  | 37 | usesLibs         []string | 
|  | 38 | optionalUsesLibs []string | 
|  | 39 | enforceUsesLibs  bool | 
|  | 40 | libraryPaths     map[string]android.Path | 
|  | 41 |  | 
| Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 42 | builtInstalled string | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 43 | } | 
|  | 44 |  | 
|  | 45 | type DexpreoptProperties struct { | 
|  | 46 | Dex_preopt struct { | 
| Nicolas Geoffray | c1bf724 | 2019-10-18 14:51:38 +0100 | [diff] [blame] | 47 | // If false, prevent dexpreopting.  Defaults to true. | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 48 | Enabled *bool | 
|  | 49 |  | 
|  | 50 | // If true, generate an app image (.art file) for this module. | 
|  | 51 | App_image *bool | 
|  | 52 |  | 
|  | 53 | // If true, use a checked-in profile to guide optimization.  Defaults to false unless | 
|  | 54 | // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR | 
|  | 55 | // that matches the name of this module, in which case it is defaulted to true. | 
|  | 56 | Profile_guided *bool | 
|  | 57 |  | 
|  | 58 | // If set, provides the path to profile relative to the Android.bp file.  If not set, | 
|  | 59 | // defaults to searching for a file that matches the name of this module in the default | 
|  | 60 | // 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] | 61 | Profile *string `android:"path"` | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 62 | } | 
|  | 63 | } | 
|  | 64 |  | 
| Ulya Trafimovich | de962bd | 2020-04-24 12:15:20 +0100 | [diff] [blame] | 65 | func init() { | 
|  | 66 | dexpreopt.DexpreoptRunningInSoong = true | 
|  | 67 | } | 
|  | 68 |  | 
| Martin Stjernholm | 6d41527 | 2020-01-31 17:10:36 +0000 | [diff] [blame] | 69 | func (d *dexpreopter) dexpreoptDisabled(ctx android.BaseModuleContext) bool { | 
| Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 70 | global := dexpreopt.GetGlobalConfig(ctx) | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 71 |  | 
|  | 72 | if global.DisablePreopt { | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 73 | return true | 
|  | 74 | } | 
|  | 75 |  | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 76 | if inList(ctx.ModuleName(), global.DisablePreoptModules) { | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 77 | return true | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | if ctx.Config().UnbundledBuild() { | 
|  | 81 | return true | 
|  | 82 | } | 
|  | 83 |  | 
|  | 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 | 
|  | 101 | if am, ok := ctx.Module().(android.ApexModule); ok && !am.IsForPlatform() { | 
|  | 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 |  | 
|  | 121 | func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.ModuleOutPath) 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. | 
|  | 126 | if d.dexpreoptDisabled(ctx) || d.installPath.Base() == "." { | 
| Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 127 | return dexJarFile | 
|  | 128 | } | 
|  | 129 |  | 
| Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 130 | globalSoong := dexpreopt.GetGlobalSoongConfig(ctx) | 
| Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 131 | global := dexpreopt.GetGlobalConfig(ctx) | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 132 | bootImage := defaultBootImageConfig(ctx) | 
| Vladimir Marko | 40139d6 | 2020-02-06 15:14:29 +0000 | [diff] [blame] | 133 | dexFiles := bootImage.dexPathsDeps.Paths() | 
|  | 134 | dexLocations := bootImage.dexLocationsDeps | 
|  | 135 | if global.UseArtImage { | 
|  | 136 | bootImage = artBootImageConfig(ctx) | 
|  | 137 | } | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 138 |  | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 139 | targets := ctx.MultiTargets() | 
|  | 140 | if len(targets) == 0 { | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 141 | // assume this is a java library, dexpreopt for all arches for now | 
|  | 142 | for _, target := range ctx.Config().Targets[android.Android] { | 
| dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 143 | if target.NativeBridge == android.NativeBridgeDisabled { | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 144 | targets = append(targets, target) | 
| dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 145 | } | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 146 | } | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 147 | if inList(ctx.ModuleName(), global.SystemServerJars) && !d.isSDKLibrary { | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 148 | // If the module is not an SDK library and it's a system server jar, only preopt the primary arch. | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 149 | targets = targets[:1] | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 150 | } | 
|  | 151 | } | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 152 |  | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 153 | var archs []android.ArchType | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 154 | var images android.Paths | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 155 | var imagesDeps []android.OutputPaths | 
| David Srbecky | 163bda6 | 2020-02-18 20:43:06 +0000 | [diff] [blame] | 156 | for _, target := range targets { | 
|  | 157 | archs = append(archs, target.Arch.ArchType) | 
|  | 158 | variant := bootImage.getVariant(target) | 
|  | 159 | images = append(images, variant.images) | 
|  | 160 | imagesDeps = append(imagesDeps, variant.imagesDeps) | 
| Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 161 | } | 
|  | 162 |  | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 163 | dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath) | 
|  | 164 |  | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 165 | var profileClassListing android.OptionalPath | 
| Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 166 | var profileBootListing android.OptionalPath | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 167 | profileIsTextListing := false | 
|  | 168 | if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) { | 
|  | 169 | // If dex_preopt.profile_guided is not set, default it based on the existence of the | 
|  | 170 | // dexprepot.profile option or the profile class listing. | 
|  | 171 | if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" { | 
|  | 172 | profileClassListing = android.OptionalPathForPath( | 
|  | 173 | android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile))) | 
| Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 174 | profileBootListing = android.ExistentPathForSource(ctx, | 
|  | 175 | ctx.ModuleDir(), String(d.dexpreoptProperties.Dex_preopt.Profile)+"-boot") | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 176 | profileIsTextListing = true | 
|  | 177 | } else { | 
|  | 178 | profileClassListing = android.ExistentPathForSource(ctx, | 
| Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 179 | global.ProfileDir, ctx.ModuleName()+".prof") | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 180 | } | 
|  | 181 | } | 
|  | 182 |  | 
| Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 183 | dexpreoptConfig := &dexpreopt.ModuleConfig{ | 
| Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 184 | Name:            ctx.ModuleName(), | 
|  | 185 | DexLocation:     dexLocation, | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 186 | BuildPath:       android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath, | 
|  | 187 | DexPath:         dexJarFile, | 
| Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 188 | ManifestPath:    d.manifestFile, | 
| Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 189 | UncompressedDex: d.uncompressedDex, | 
|  | 190 | HasApkLibraries: false, | 
|  | 191 | PreoptFlags:     nil, | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 192 |  | 
| Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 193 | ProfileClassListing:  profileClassListing, | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 194 | ProfileIsTextListing: profileIsTextListing, | 
| Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 195 | ProfileBootListing:   profileBootListing, | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 196 |  | 
| Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 197 | EnforceUsesLibraries:         d.enforceUsesLibs, | 
|  | 198 | PresentOptionalUsesLibraries: d.optionalUsesLibs, | 
|  | 199 | UsesLibraries:                d.usesLibs, | 
|  | 200 | LibraryPaths:                 d.libraryPaths, | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 201 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 202 | Archs:                   archs, | 
|  | 203 | DexPreoptImages:         images, | 
|  | 204 | DexPreoptImagesDeps:     imagesDeps, | 
|  | 205 | DexPreoptImageLocations: bootImage.imageLocations, | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 206 |  | 
| Vladimir Marko | 40139d6 | 2020-02-06 15:14:29 +0000 | [diff] [blame] | 207 | PreoptBootClassPathDexFiles:     dexFiles, | 
|  | 208 | PreoptBootClassPathDexLocations: dexLocations, | 
| Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 209 |  | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 210 | PreoptExtractedApk: false, | 
|  | 211 |  | 
|  | 212 | NoCreateAppImage:    !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true), | 
|  | 213 | ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false), | 
|  | 214 |  | 
| Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 215 | PresignedPrebuilt: d.isPresignedPrebuilt, | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 216 | } | 
|  | 217 |  | 
| Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 218 | dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, globalSoong, global, dexpreoptConfig) | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 219 | if err != nil { | 
|  | 220 | ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error()) | 
|  | 221 | return dexJarFile | 
|  | 222 | } | 
|  | 223 |  | 
| Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 224 | dexpreoptRule.Build(pctx, ctx, "dexpreopt", "dexpreopt") | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 225 |  | 
| Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 226 | d.builtInstalled = dexpreoptRule.Installs().String() | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 227 |  | 
| Nicolas Geoffray | c1bf724 | 2019-10-18 14:51:38 +0100 | [diff] [blame] | 228 | return dexJarFile | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 229 | } |