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