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 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 25 | installPath android.OutputPath |
| 26 | uncompressedDex bool |
| 27 | isSDKLibrary bool |
| 28 | isTest bool |
| 29 | isInstallable bool |
| 30 | isPresignedPrebuilt bool |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 31 | |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 32 | manifestFile android.Path |
| 33 | usesLibs []string |
| 34 | optionalUsesLibs []string |
| 35 | enforceUsesLibs bool |
| 36 | libraryPaths map[string]android.Path |
| 37 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 38 | builtInstalled string |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | type DexpreoptProperties struct { |
| 42 | Dex_preopt struct { |
| 43 | // If false, prevent dexpreopting and stripping the dex file from the final jar. Defaults to |
| 44 | // true. |
| 45 | Enabled *bool |
| 46 | |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 47 | // If true, never strip the dex files from the final jar when dexpreopting. Defaults to false. |
| 48 | No_stripping *bool |
| 49 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 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 | |
| 65 | func (d *dexpreopter) dexpreoptDisabled(ctx android.ModuleContext) bool { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 66 | global := dexpreoptGlobalConfig(ctx) |
| 67 | |
| 68 | if global.DisablePreopt { |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 69 | return true |
| 70 | } |
| 71 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 72 | if inList(ctx.ModuleName(), global.DisablePreoptModules) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 73 | return true |
| 74 | } |
| 75 | |
| 76 | if ctx.Config().UnbundledBuild() { |
| 77 | return true |
| 78 | } |
| 79 | |
| 80 | if d.isTest { |
| 81 | return true |
| 82 | } |
| 83 | |
| 84 | if !BoolDefault(d.dexpreoptProperties.Dex_preopt.Enabled, true) { |
| 85 | return true |
| 86 | } |
| 87 | |
Colin Cross | dc2da91 | 2019-01-05 22:13:05 -0800 | [diff] [blame] | 88 | if !d.isInstallable { |
| 89 | return true |
| 90 | } |
| 91 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 92 | // TODO: contains no java code |
| 93 | |
| 94 | return false |
| 95 | } |
| 96 | |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 97 | func odexOnSystemOther(ctx android.ModuleContext, installPath android.OutputPath) bool { |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 98 | return dexpreopt.OdexOnSystemOtherByName(ctx.ModuleName(), android.InstallPathToOnDevicePath(ctx, installPath), dexpreoptGlobalConfig(ctx)) |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | func (d *dexpreopter) dexpreopt(ctx android.ModuleContext, dexJarFile android.ModuleOutPath) android.ModuleOutPath { |
| 102 | if d.dexpreoptDisabled(ctx) { |
| 103 | return dexJarFile |
| 104 | } |
| 105 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 106 | global := dexpreoptGlobalConfig(ctx) |
| 107 | bootImage := defaultBootImageConfig(ctx) |
Nicolas Geoffray | 06758a7 | 2019-04-08 17:19:15 +0100 | [diff] [blame] | 108 | defaultBootImage := bootImage |
Nicolas Geoffray | 25c0e03 | 2019-04-04 18:45:20 +0100 | [diff] [blame] | 109 | if global.UseApexImage { |
| 110 | bootImage = apexBootImageConfig(ctx) |
| 111 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 112 | |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 113 | var archs []android.ArchType |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 114 | for _, a := range ctx.MultiTargets() { |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 115 | archs = append(archs, a.Arch.ArchType) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 116 | } |
| 117 | if len(archs) == 0 { |
| 118 | // assume this is a java library, dexpreopt for all arches for now |
| 119 | for _, target := range ctx.Config().Targets[android.Android] { |
dimitry | 1f33e40 | 2019-03-26 12:39:31 +0100 | [diff] [blame] | 120 | if target.NativeBridge == android.NativeBridgeDisabled { |
| 121 | archs = append(archs, target.Arch.ArchType) |
| 122 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 123 | } |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 124 | if inList(ctx.ModuleName(), global.SystemServerJars) && !d.isSDKLibrary { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 125 | // If the module is not an SDK library and it's a system server jar, only preopt the primary arch. |
| 126 | archs = archs[:1] |
| 127 | } |
| 128 | } |
| 129 | if ctx.Config().SecondArchIsTranslated() { |
| 130 | // Only preopt primary arch for translated arch since there is only an image there. |
| 131 | archs = archs[:1] |
| 132 | } |
| 133 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 134 | var images android.Paths |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 135 | var imagesDeps []android.Paths |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 136 | for _, arch := range archs { |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 137 | images = append(images, bootImage.images[arch]) |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 138 | imagesDeps = append(imagesDeps, bootImage.imagesDeps[arch]) |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 141 | dexLocation := android.InstallPathToOnDevicePath(ctx, d.installPath) |
| 142 | |
| 143 | strippedDexJarFile := android.PathForModuleOut(ctx, "dexpreopt", dexJarFile.Base()) |
| 144 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 145 | var profileClassListing android.OptionalPath |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame^] | 146 | var profileBootListing android.OptionalPath |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 147 | profileIsTextListing := false |
| 148 | if BoolDefault(d.dexpreoptProperties.Dex_preopt.Profile_guided, true) { |
| 149 | // If dex_preopt.profile_guided is not set, default it based on the existence of the |
| 150 | // dexprepot.profile option or the profile class listing. |
| 151 | if String(d.dexpreoptProperties.Dex_preopt.Profile) != "" { |
| 152 | profileClassListing = android.OptionalPathForPath( |
| 153 | android.PathForModuleSrc(ctx, String(d.dexpreoptProperties.Dex_preopt.Profile))) |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame^] | 154 | profileBootListing = android.ExistentPathForSource(ctx, |
| 155 | ctx.ModuleDir(), String(d.dexpreoptProperties.Dex_preopt.Profile)+"-boot") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 156 | profileIsTextListing = true |
| 157 | } else { |
| 158 | profileClassListing = android.ExistentPathForSource(ctx, |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 159 | global.ProfileDir, ctx.ModuleName()+".prof") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 163 | dexpreoptConfig := dexpreopt.ModuleConfig{ |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 164 | Name: ctx.ModuleName(), |
| 165 | DexLocation: dexLocation, |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 166 | BuildPath: android.PathForModuleOut(ctx, "dexpreopt", ctx.ModuleName()+".jar").OutputPath, |
| 167 | DexPath: dexJarFile, |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 168 | ManifestPath: d.manifestFile, |
Victor Hsieh | d181c8b | 2019-01-29 13:00:33 -0800 | [diff] [blame] | 169 | UncompressedDex: d.uncompressedDex, |
| 170 | HasApkLibraries: false, |
| 171 | PreoptFlags: nil, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 172 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 173 | ProfileClassListing: profileClassListing, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 174 | ProfileIsTextListing: profileIsTextListing, |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame^] | 175 | ProfileBootListing: profileBootListing, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 176 | |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 177 | EnforceUsesLibraries: d.enforceUsesLibs, |
| 178 | PresentOptionalUsesLibraries: d.optionalUsesLibs, |
| 179 | UsesLibraries: d.usesLibs, |
| 180 | LibraryPaths: d.libraryPaths, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 181 | |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 182 | Archs: archs, |
| 183 | DexPreoptImages: images, |
| 184 | DexPreoptImagesDeps: imagesDeps, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 185 | |
Nicolas Geoffray | 06758a7 | 2019-04-08 17:19:15 +0100 | [diff] [blame] | 186 | // We use the dex paths and dex locations of the default boot image, as it |
| 187 | // contains the full dexpreopt boot classpath. Other images may just contain a subset of |
| 188 | // the dexpreopt boot classpath. |
| 189 | PreoptBootClassPathDexFiles: defaultBootImage.dexPaths.Paths(), |
| 190 | PreoptBootClassPathDexLocations: defaultBootImage.dexLocations, |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 191 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 192 | PreoptExtractedApk: false, |
| 193 | |
| 194 | NoCreateAppImage: !BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, true), |
| 195 | ForceCreateAppImage: BoolDefault(d.dexpreoptProperties.Dex_preopt.App_image, false), |
| 196 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 197 | PresignedPrebuilt: d.isPresignedPrebuilt, |
| 198 | |
Colin Cross | 8c6d250 | 2019-01-09 21:09:14 -0800 | [diff] [blame] | 199 | NoStripping: Bool(d.dexpreoptProperties.Dex_preopt.No_stripping), |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 200 | StripInputPath: dexJarFile, |
| 201 | StripOutputPath: strippedDexJarFile.OutputPath, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 204 | dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, global, dexpreoptConfig) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 205 | if err != nil { |
| 206 | ctx.ModuleErrorf("error generating dexpreopt rule: %s", err.Error()) |
| 207 | return dexJarFile |
| 208 | } |
| 209 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 210 | dexpreoptRule.Build(pctx, ctx, "dexpreopt", "dexpreopt") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 211 | |
Colin Cross | deabb94 | 2019-02-11 14:11:09 -0800 | [diff] [blame] | 212 | d.builtInstalled = dexpreoptRule.Installs().String() |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 213 | |
Colin Cross | 44df581 | 2019-02-15 23:06:46 -0800 | [diff] [blame] | 214 | stripRule, err := dexpreopt.GenerateStripRule(global, dexpreoptConfig) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 215 | if err != nil { |
| 216 | ctx.ModuleErrorf("error generating dexpreopt strip rule: %s", err.Error()) |
| 217 | return dexJarFile |
| 218 | } |
| 219 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 220 | stripRule.Build(pctx, ctx, "dexpreopt_strip", "dexpreopt strip") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 221 | |
| 222 | return strippedDexJarFile |
| 223 | } |