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 | |
| 22 | type dexpreopter struct { |
| 23 | dexpreoptProperties DexpreoptProperties |
| 24 | |
Colin Cross | 2fc72f6 | 2018-12-21 12:59:54 -0800 | [diff] [blame] | 25 | installPath android.OutputPath |
| 26 | uncompressedDex bool |
| 27 | isSDKLibrary bool |
| 28 | isTest bool |
| 29 | isInstallable bool |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 30 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 31 | builtInstalled string |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | type DexpreoptProperties struct { |
| 35 | Dex_preopt struct { |
| 36 | // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to |
| 37 | // true. |
| 38 | Enabled *bool |
| 39 | |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 40 | // If true, never strip the dex files from the final jar when dexpreopting. Defaults to false. |
| 41 | No_stripping *bool |
| 42 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 43 | // If true, generate an app image (.art file) for this module. |
| 44 | App_image *bool |
| 45 | |
| 46 | // If true, use a checked-in profile to guide optimization. Defaults to false unless |
| 47 | // a matching profile is set or a profile is found in PRODUCT_DEX_PREOPT_PROFILE_DIR |
| 48 | // that matches the name of this module, in which case it is defaulted to true. |
| 49 | Profile_guided *bool |
| 50 | |
| 51 | // If set, provides the path to profile relative to the Android.bp file. If not set, |
| 52 | // defaults to searching for a file that matches the name of this module in the default |
| 53 | // profile location set by PRODUCT_DEX_PREOPT_PROFILE_DIR, or empty if not found. |
| 54 | Profile *string |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func (d *dexpreopter) dexpreoptDisabled(ctx android.ModuleContext) bool { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 59 | global := dexpreoptGlobalConfig(ctx) |
| 60 | |
| 61 | if global.DisablePreopt { |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 62 | return true |
| 63 | } |
| 64 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 65 | if inList(ctx.ModuleName(), global.DisablePreoptModules) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 66 | return true |
| 67 | } |
| 68 | |
| 69 | if ctx.Config().UnbundledBuild() { |
| 70 | return true |
| 71 | } |
| 72 | |
| 73 | if d.isTest { |
| 74 | return true |
| 75 | } |
| 76 | |
| 77 | if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) { |
| 78 | return true |
| 79 | } |
| 80 | |
Colin Cross | dc2da91 | 2019-01-05 22:13:05 -0800 | [diff] [blame] | 81 | if !d.isInstallable { |
| 82 | return true |
| 83 | } |
| 84 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 85 | // TODO: contains no java code |
| 86 | |
| 87 | return false |
| 88 | } |
| 89 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 90 | var dexpreoptGlobalConfigKey = android.NewOnceKey("DexpreoptGlobalConfig") |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 91 | var dexpreoptTestGlobalConfigKey = android.NewOnceKey("TestDexpreoptGlobalConfig") |
| 92 | |
| 93 | func setDexpreoptGlobalConfig(config android.Config, globalConfig dexpreopt.GlobalConfig) { |
| 94 | config.Once(dexpreoptTestGlobalConfigKey, func() interface{} { return globalConfig }) |
| 95 | } |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 96 | |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 97 | func dexpreoptGlobalConfig(ctx android.PathContext) dexpreopt.GlobalConfig { |
| 98 | return ctx.Config().Once(dexpreoptGlobalConfigKey, func() interface{} { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 99 | if f := ctx.Config().DexpreoptGlobalConfig(); f != "" { |
| 100 | ctx.AddNinjaFileDeps(f) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 101 | globalConfig, err := dexpreopt.LoadGlobalConfig(ctx, f) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 102 | if err != nil { |
| 103 | panic(err) |
| 104 | } |
| 105 | return globalConfig |
| 106 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 107 | |
| 108 | // No global config filename set, see if there is a test config set |
| 109 | return ctx.Config().Once(dexpreoptTestGlobalConfigKey, func() interface{} { |
| 110 | // Nope, return a config with preopting disabled |
| 111 | return dexpreopt.GlobalConfig{ |
| 112 | DisablePreopt: true, |
| 113 | } |
| 114 | }) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 115 | }).(dexpreopt.GlobalConfig) |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | func odexOnSystemOther(ctx android.ModuleContext, installPath android.OutputPath) bool { |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 119 | return dexpreopt.OdexOnSystemOtherByName(ctx.ModuleName(), android.InstallPathToOnDevicePath(ctx, installPath), dexpreoptGlobalConfig(ctx)) |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.ModuleOutPath) android.ModuleOutPath { |
| 123 | if d.dexpreoptDisabled(ctx) { |
| 124 | return dexJarFile |
| 125 | } |
| 126 | |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 127 | info := dexpreoptBootJarsInfo(ctx) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 128 | |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 129 | var archs []android.ArchType |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 130 | for _, a := range ctx.MultiTargets() { |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 131 | archs = append(archs, a.Arch.ArchType) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 132 | } |
| 133 | if len(archs) == 0 { |
| 134 | // assume this is a java library, dexpreopt for all arches for now |
| 135 | for _, target := range ctx.Config().Targets[android.Android] { |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 136 | archs = append(archs, target.Arch.ArchType) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 137 | } |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 138 | if inList(ctx.ModuleName(), info.global.SystemServerJars) && !d.isSDKLibrary { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 139 | // If the module is not an SDK library and it's a system server jar, only preopt the primary arch. |
| 140 | archs = archs[:1] |
| 141 | } |
| 142 | } |
| 143 | if ctx.Config().SecondArchIsTranslated() { |
| 144 | // Only preopt primary arch for translated arch since there is only an image there. |
| 145 | archs = archs[:1] |
| 146 | } |
| 147 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 148 | var images android.Paths |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 149 | for _, arch := range archs { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 150 | images = append(images, info.images[arch]) |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 153 | dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath) |
| 154 | |
| 155 | strippedDexJarFile := android.PathForModuleOut(ctx, "dexpreopt", dexJarFile.Base()) |
| 156 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 157 | var profileClassListing android.OptionalPath |
| 158 | profileIsTextListing := false |
| 159 | if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) { |
| 160 | // If dex_preopt.profile_guided is not set, default it based on the existence of the |
| 161 | // dexprepot.profile option or the profile class listing. |
| 162 | if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" { |
| 163 | profileClassListing = android.OptionalPathForPath( |
| 164 | android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile))) |
| 165 | profileIsTextListing = true |
| 166 | } else { |
| 167 | profileClassListing = android.ExistentPathForSource(ctx, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 168 | info.global.ProfileDir, ctx.ModuleName()+".prof") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 172 | dexpreoptConfig := dexpreopt.ModuleConfig{ |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 173 | Name: ctx.ModuleName(), |
| 174 | DexLocation: dexLocation, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 175 | BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath, |
| 176 | DexPath: dexJarFile, |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 177 | UncompressedDex: d.uncompressedDex, |
| 178 | HasApkLibraries: false, |
| 179 | PreoptFlags: nil, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 180 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 181 | ProfileClassListing: profileClassListing, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 182 | ProfileIsTextListing: profileIsTextListing, |
| 183 | |
| 184 | EnforceUsesLibraries: false, |
| 185 | OptionalUsesLibraries: nil, |
| 186 | UsesLibraries: nil, |
| 187 | LibraryPaths: nil, |
| 188 | |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 189 | Archs: archs, |
| 190 | DexPreoptImages: images, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 191 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 192 | PreoptBootClassPathDexFiles: info.preoptBootDex.Paths(), |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 193 | PreoptBootClassPathDexLocations: info.preoptBootLocations, |
| 194 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 195 | PreoptExtractedApk: false, |
| 196 | |
| 197 | NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true), |
| 198 | ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false), |
| 199 | |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 200 | NoStripping: Bool(d.dexpreoptProperties.Dex_preopt.No_stripping), |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 201 | StripInputPath: dexJarFile, |
| 202 | StripOutputPath: strippedDexJarFile.OutputPath, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame^] | 205 | dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, info.global, dexpreoptConfig) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 206 | if err != nil { |
| 207 | ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error()) |
| 208 | return dexJarFile |
| 209 | } |
| 210 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 211 | dexpreoptRule.Build(pctx, ctx, "dexpreopt", "dexpreopt") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 212 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 213 | d.builtInstalled = dexpreoptRule.Installs().String() |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 214 | |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 215 | stripRule, err := dexpreopt.GenerateStripRule(info.global, dexpreoptConfig) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 216 | if err != nil { |
| 217 | ctx.ModuleErrorf("error generating dexpreopt strip rule: %s", err.Error()) |
| 218 | return dexJarFile |
| 219 | } |
| 220 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 221 | stripRule.Build(pctx, ctx, "dexpreopt_strip", "dexpreopt strip") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 222 | |
| 223 | return strippedDexJarFile |
| 224 | } |