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 | // The dexpreopt package converts a global dexpreopt config and a module dexpreopt config into rules to perform |
Nicolas Geoffray | c1bf724 | 2019-10-18 14:51:38 +0100 | [diff] [blame] | 16 | // dexpreopting. |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 17 | // |
| 18 | // It is used in two places; in the dexpeopt_gen binary for modules defined in Make, and directly linked into Soong. |
| 19 | // |
| 20 | // For Make modules it is built into the dexpreopt_gen binary, which is executed as a Make rule using global config and |
| 21 | // module config specified in JSON files. The binary writes out two shell scripts, only updating them if they have |
| 22 | // changed. One script takes an APK or JAR as an input and produces a zip file containing any outputs of preopting, |
| 23 | // in the location they should be on the device. The Make build rules will unzip the zip file into $(PRODUCT_OUT) when |
| 24 | // installing the APK, which will install the preopt outputs into $(PRODUCT_OUT)/system or $(PRODUCT_OUT)/system_other |
Nicolas Geoffray | c1bf724 | 2019-10-18 14:51:38 +0100 | [diff] [blame] | 25 | // as necessary. The zip file may be empty if preopting was disabled for any reason. |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 26 | // |
| 27 | // The intermediate shell scripts allow changes to this package or to the global config to regenerate the shell scripts |
| 28 | // but only require re-executing preopting if the script has changed. |
| 29 | // |
| 30 | // For Soong modules this package is linked directly into Soong and run from the java package. It generates the same |
| 31 | // commands as for make, using athe same global config JSON file used by make, but using a module config structure |
| 32 | // provided by Soong. The generated commands are then converted into Soong rule and written directly to the ninja file, |
| 33 | // with no extra shell scripts involved. |
| 34 | package dexpreopt |
| 35 | |
| 36 | import ( |
| 37 | "fmt" |
| 38 | "path/filepath" |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 39 | "runtime" |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 40 | "strings" |
| 41 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 42 | "android/soong/android" |
| 43 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 44 | "github.com/google/blueprint/pathtools" |
| 45 | ) |
| 46 | |
| 47 | const SystemPartition = "/system/" |
| 48 | const SystemOtherPartition = "/system_other/" |
| 49 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 50 | // GenerateDexpreoptRule generates a set of commands that will preopt a module based on a GlobalConfig and a |
| 51 | // ModuleConfig. The produced files and their install locations will be available through rule.Installs(). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 52 | func GenerateDexpreoptRule(ctx android.PathContext, |
| 53 | global GlobalConfig, module ModuleConfig) (rule *android.RuleBuilder, err error) { |
| 54 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 55 | defer func() { |
| 56 | if r := recover(); r != nil { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 57 | if _, ok := r.(runtime.Error); ok { |
| 58 | panic(r) |
| 59 | } else if e, ok := r.(error); ok { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 60 | err = e |
| 61 | rule = nil |
| 62 | } else { |
| 63 | panic(r) |
| 64 | } |
| 65 | } |
| 66 | }() |
| 67 | |
Colin Cross | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 68 | rule = android.NewRuleBuilder() |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 69 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 70 | generateProfile := module.ProfileClassListing.Valid() && !global.DisableGenerateProfile |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 71 | generateBootProfile := module.ProfileBootListing.Valid() && !global.DisableGenerateProfile |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 72 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 73 | var profile android.WritablePath |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 74 | if generateProfile { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 75 | profile = profileCommand(ctx, global, module, rule) |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 76 | } |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 77 | if generateBootProfile { |
| 78 | bootProfileCommand(ctx, global, module, rule) |
| 79 | } |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 80 | |
| 81 | if !dexpreoptDisabled(global, module) { |
| 82 | // Don't preopt individual boot jars, they will be preopted together. |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 83 | if !contains(global.BootJars, module.Name) { |
| 84 | appImage := (generateProfile || module.ForceCreateAppImage || global.DefaultAppImages) && |
| 85 | !module.NoCreateAppImage |
| 86 | |
| 87 | generateDM := shouldGenerateDM(module, global) |
| 88 | |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 89 | for i, arch := range module.Archs { |
| 90 | image := module.DexPreoptImages[i] |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 91 | imageDeps := module.DexPreoptImagesDeps[i] |
| 92 | dexpreoptCommand(ctx, global, module, rule, arch, profile, image, imageDeps, appImage, generateDM) |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return rule, nil |
| 98 | } |
| 99 | |
| 100 | func dexpreoptDisabled(global GlobalConfig, module ModuleConfig) bool { |
| 101 | if contains(global.DisablePreoptModules, module.Name) { |
| 102 | return true |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Ulyana Trafimovich | f2cb7e9 | 2019-11-27 12:26:49 +0000 | [diff] [blame] | 105 | // Don't preopt system server jars that are updatable. |
| 106 | for _, p := range global.UpdatableSystemServerJars { |
| 107 | if _, jar := SplitApexJarPair(p); jar == module.Name { |
| 108 | return true |
| 109 | } |
| 110 | } |
| 111 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 112 | // If OnlyPreoptBootImageAndSystemServer=true and module is not in boot class path skip |
| 113 | // Also preopt system server jars since selinux prevents system server from loading anything from |
| 114 | // /data. If we don't do this they will need to be extracted which is not favorable for RAM usage |
| 115 | // or performance. If PreoptExtractedApk is true, we ignore the only preopt boot image options. |
| 116 | if global.OnlyPreoptBootImageAndSystemServer && !contains(global.BootJars, module.Name) && |
| 117 | !contains(global.SystemServerJars, module.Name) && !module.PreoptExtractedApk { |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 118 | return true |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 121 | return false |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 124 | func profileCommand(ctx android.PathContext, global GlobalConfig, module ModuleConfig, |
| 125 | rule *android.RuleBuilder) android.WritablePath { |
| 126 | |
| 127 | profilePath := module.BuildPath.InSameDir(ctx, "profile.prof") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 128 | profileInstalledPath := module.DexLocation + ".prof" |
| 129 | |
| 130 | if !module.ProfileIsTextListing { |
| 131 | rule.Command().FlagWithOutput("touch ", profilePath) |
| 132 | } |
| 133 | |
| 134 | cmd := rule.Command(). |
| 135 | Text(`ANDROID_LOG_TAGS="*:e"`). |
| 136 | Tool(global.Tools.Profman) |
| 137 | |
| 138 | if module.ProfileIsTextListing { |
| 139 | // The profile is a test listing of classes (used for framework jars). |
| 140 | // We need to generate the actual binary profile before being able to compile. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 141 | cmd.FlagWithInput("--create-profile-from=", module.ProfileClassListing.Path()) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 142 | } else { |
| 143 | // The profile is binary profile (used for apps). Run it through profman to |
| 144 | // ensure the profile keys match the apk. |
| 145 | cmd. |
| 146 | Flag("--copy-and-update-profile-key"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 147 | FlagWithInput("--profile-file=", module.ProfileClassListing.Path()) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | cmd. |
| 151 | FlagWithInput("--apk=", module.DexPath). |
| 152 | Flag("--dex-location="+module.DexLocation). |
| 153 | FlagWithOutput("--reference-profile-file=", profilePath) |
| 154 | |
| 155 | if !module.ProfileIsTextListing { |
| 156 | cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath)) |
| 157 | } |
| 158 | rule.Install(profilePath, profileInstalledPath) |
| 159 | |
| 160 | return profilePath |
| 161 | } |
| 162 | |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 163 | func bootProfileCommand(ctx android.PathContext, global GlobalConfig, module ModuleConfig, |
| 164 | rule *android.RuleBuilder) android.WritablePath { |
| 165 | |
| 166 | profilePath := module.BuildPath.InSameDir(ctx, "profile.bprof") |
| 167 | profileInstalledPath := module.DexLocation + ".bprof" |
| 168 | |
| 169 | if !module.ProfileIsTextListing { |
| 170 | rule.Command().FlagWithOutput("touch ", profilePath) |
| 171 | } |
| 172 | |
| 173 | cmd := rule.Command(). |
| 174 | Text(`ANDROID_LOG_TAGS="*:e"`). |
| 175 | Tool(global.Tools.Profman) |
| 176 | |
| 177 | // The profile is a test listing of methods. |
| 178 | // We need to generate the actual binary profile. |
| 179 | cmd.FlagWithInput("--create-profile-from=", module.ProfileBootListing.Path()) |
| 180 | |
| 181 | cmd. |
| 182 | Flag("--generate-boot-profile"). |
| 183 | FlagWithInput("--apk=", module.DexPath). |
| 184 | Flag("--dex-location="+module.DexLocation). |
| 185 | FlagWithOutput("--reference-profile-file=", profilePath) |
| 186 | |
| 187 | if !module.ProfileIsTextListing { |
| 188 | cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath)) |
| 189 | } |
| 190 | rule.Install(profilePath, profileInstalledPath) |
| 191 | |
| 192 | return profilePath |
| 193 | } |
| 194 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 195 | func dexpreoptCommand(ctx android.PathContext, global GlobalConfig, module ModuleConfig, rule *android.RuleBuilder, |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 196 | arch android.ArchType, profile, bootImage android.Path, bootImageDeps android.Paths, appImage, generateDM bool) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 197 | |
| 198 | // HACK: make soname in Soong-generated .odex files match Make. |
| 199 | base := filepath.Base(module.DexLocation) |
| 200 | if filepath.Ext(base) == ".jar" { |
| 201 | base = "javalib.jar" |
| 202 | } else if filepath.Ext(base) == ".apk" { |
| 203 | base = "package.apk" |
| 204 | } |
| 205 | |
| 206 | toOdexPath := func(path string) string { |
| 207 | return filepath.Join( |
| 208 | filepath.Dir(path), |
| 209 | "oat", |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 210 | arch.String(), |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 211 | pathtools.ReplaceExtension(filepath.Base(path), "odex")) |
| 212 | } |
| 213 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 214 | odexPath := module.BuildPath.InSameDir(ctx, "oat", arch.String(), pathtools.ReplaceExtension(base, "odex")) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 215 | odexInstallPath := toOdexPath(module.DexLocation) |
| 216 | if odexOnSystemOther(module, global) { |
Anton Hansson | 43ab0bc | 2019-10-03 14:18:45 +0100 | [diff] [blame] | 217 | odexInstallPath = filepath.Join(SystemOtherPartition, odexInstallPath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 220 | vdexPath := odexPath.ReplaceExtension(ctx, "vdex") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 221 | vdexInstallPath := pathtools.ReplaceExtension(odexInstallPath, "vdex") |
| 222 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 223 | invocationPath := odexPath.ReplaceExtension(ctx, "invocation") |
Alex Light | 5de4196 | 2018-12-18 15:16:26 -0800 | [diff] [blame] | 224 | |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 225 | // bootImage is .../dex_bootjars/system/framework/arm64/boot.art, but dex2oat wants |
| 226 | // .../dex_bootjars/system/framework/boot.art on the command line |
| 227 | var bootImageLocation string |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 228 | if bootImage != nil { |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 229 | bootImageLocation = PathToLocation(bootImage, arch) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 232 | // The class loader context using paths in the build |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 233 | var classLoaderContextHost android.Paths |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 234 | |
| 235 | // The class loader context using paths as they will be on the device |
| 236 | var classLoaderContextTarget []string |
| 237 | |
| 238 | // Extra paths that will be appended to the class loader if the APK manifest has targetSdkVersion < 28 |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 239 | var conditionalClassLoaderContextHost28 android.Paths |
Nicolas Geoffray | 05aa7d2 | 2018-12-18 14:15:12 +0000 | [diff] [blame] | 240 | var conditionalClassLoaderContextTarget28 []string |
| 241 | |
| 242 | // Extra paths that will be appended to the class loader if the APK manifest has targetSdkVersion < 29 |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 243 | var conditionalClassLoaderContextHost29 android.Paths |
Nicolas Geoffray | 05aa7d2 | 2018-12-18 14:15:12 +0000 | [diff] [blame] | 244 | var conditionalClassLoaderContextTarget29 []string |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 245 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 246 | var classLoaderContextHostString string |
| 247 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 248 | if module.EnforceUsesLibraries { |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 249 | usesLibs := append(copyOf(module.UsesLibraries), module.PresentOptionalUsesLibraries...) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 250 | |
| 251 | // Create class loader context for dex2oat from uses libraries and filtered optional libraries |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 252 | for _, l := range usesLibs { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 253 | |
| 254 | classLoaderContextHost = append(classLoaderContextHost, |
| 255 | pathForLibrary(module, l)) |
| 256 | classLoaderContextTarget = append(classLoaderContextTarget, |
| 257 | filepath.Join("/system/framework", l+".jar")) |
| 258 | } |
| 259 | |
| 260 | const httpLegacy = "org.apache.http.legacy" |
| 261 | const httpLegacyImpl = "org.apache.http.legacy.impl" |
| 262 | |
Colin Cross | 38b9685 | 2019-05-22 10:21:09 -0700 | [diff] [blame] | 263 | // org.apache.http.legacy contains classes that were in the default classpath until API 28. If the |
| 264 | // targetSdkVersion in the manifest or APK is < 28, and the module does not explicitly depend on |
| 265 | // org.apache.http.legacy, then implicitly add the classes to the classpath for dexpreopt. One the |
| 266 | // device the classes will be in a file called org.apache.http.legacy.impl.jar. |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 267 | module.LibraryPaths[httpLegacyImpl] = module.LibraryPaths[httpLegacy] |
| 268 | |
| 269 | if !contains(module.UsesLibraries, httpLegacy) && !contains(module.PresentOptionalUsesLibraries, httpLegacy) { |
Nicolas Geoffray | 05aa7d2 | 2018-12-18 14:15:12 +0000 | [diff] [blame] | 270 | conditionalClassLoaderContextHost28 = append(conditionalClassLoaderContextHost28, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 271 | pathForLibrary(module, httpLegacyImpl)) |
Nicolas Geoffray | 05aa7d2 | 2018-12-18 14:15:12 +0000 | [diff] [blame] | 272 | conditionalClassLoaderContextTarget28 = append(conditionalClassLoaderContextTarget28, |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 273 | filepath.Join("/system/framework", httpLegacyImpl+".jar")) |
| 274 | } |
Nicolas Geoffray | 05aa7d2 | 2018-12-18 14:15:12 +0000 | [diff] [blame] | 275 | |
| 276 | const hidlBase = "android.hidl.base-V1.0-java" |
| 277 | const hidlManager = "android.hidl.manager-V1.0-java" |
| 278 | |
Colin Cross | 38b9685 | 2019-05-22 10:21:09 -0700 | [diff] [blame] | 279 | // android.hidl.base-V1.0-java and android.hidl.manager-V1.0 contain classes that were in the default |
| 280 | // classpath until API 29. If the targetSdkVersion in the manifest or APK is < 29 then implicitly add |
| 281 | // the classes to the classpath for dexpreopt. |
Nicolas Geoffray | 05aa7d2 | 2018-12-18 14:15:12 +0000 | [diff] [blame] | 282 | conditionalClassLoaderContextHost29 = append(conditionalClassLoaderContextHost29, |
Alex Light | 5de4196 | 2018-12-18 15:16:26 -0800 | [diff] [blame] | 283 | pathForLibrary(module, hidlManager)) |
Nicolas Geoffray | 05aa7d2 | 2018-12-18 14:15:12 +0000 | [diff] [blame] | 284 | conditionalClassLoaderContextTarget29 = append(conditionalClassLoaderContextTarget29, |
| 285 | filepath.Join("/system/framework", hidlManager+".jar")) |
| 286 | conditionalClassLoaderContextHost29 = append(conditionalClassLoaderContextHost29, |
Alex Light | 5de4196 | 2018-12-18 15:16:26 -0800 | [diff] [blame] | 287 | pathForLibrary(module, hidlBase)) |
Nicolas Geoffray | 05aa7d2 | 2018-12-18 14:15:12 +0000 | [diff] [blame] | 288 | conditionalClassLoaderContextTarget29 = append(conditionalClassLoaderContextTarget29, |
| 289 | filepath.Join("/system/framework", hidlBase+".jar")) |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 290 | |
| 291 | classLoaderContextHostString = strings.Join(classLoaderContextHost.Strings(), ":") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 292 | } else { |
| 293 | // Pass special class loader context to skip the classpath and collision check. |
| 294 | // This will get removed once LOCAL_USES_LIBRARIES is enforced. |
| 295 | // Right now LOCAL_USES_LIBRARIES is opt in, for the case where it's not specified we still default |
| 296 | // to the &. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 297 | classLoaderContextHostString = `\&` |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 300 | rule.Command().FlagWithArg("mkdir -p ", filepath.Dir(odexPath.String())) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 301 | rule.Command().FlagWithOutput("rm -f ", odexPath) |
| 302 | // Set values in the environment of the rule. These may be modified by construct_context.sh. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 303 | rule.Command().FlagWithArg("class_loader_context_arg=--class-loader-context=", classLoaderContextHostString) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 304 | rule.Command().Text(`stored_class_loader_context_arg=""`) |
| 305 | |
| 306 | if module.EnforceUsesLibraries { |
Colin Cross | 38b9685 | 2019-05-22 10:21:09 -0700 | [diff] [blame] | 307 | if module.ManifestPath != nil { |
| 308 | rule.Command().Text(`target_sdk_version="$(`). |
| 309 | Tool(global.Tools.ManifestCheck). |
| 310 | Flag("--extract-target-sdk-version"). |
| 311 | Input(module.ManifestPath). |
| 312 | Text(`)"`) |
| 313 | } else { |
| 314 | // No manifest to extract targetSdkVersion from, hope that DexJar is an APK |
| 315 | rule.Command().Text(`target_sdk_version="$(`). |
| 316 | Tool(global.Tools.Aapt). |
| 317 | Flag("dump badging"). |
| 318 | Input(module.DexPath). |
| 319 | Text(`| grep "targetSdkVersion" | sed -n "s/targetSdkVersion:'\(.*\)'/\1/p"`). |
| 320 | Text(`)"`) |
| 321 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 322 | rule.Command().Textf(`dex_preopt_host_libraries="%s"`, |
| 323 | strings.Join(classLoaderContextHost.Strings(), " ")). |
| 324 | Implicits(classLoaderContextHost) |
| 325 | rule.Command().Textf(`dex_preopt_target_libraries="%s"`, |
| 326 | strings.Join(classLoaderContextTarget, " ")) |
| 327 | rule.Command().Textf(`conditional_host_libs_28="%s"`, |
| 328 | strings.Join(conditionalClassLoaderContextHost28.Strings(), " ")). |
| 329 | Implicits(conditionalClassLoaderContextHost28) |
| 330 | rule.Command().Textf(`conditional_target_libs_28="%s"`, |
| 331 | strings.Join(conditionalClassLoaderContextTarget28, " ")) |
| 332 | rule.Command().Textf(`conditional_host_libs_29="%s"`, |
| 333 | strings.Join(conditionalClassLoaderContextHost29.Strings(), " ")). |
| 334 | Implicits(conditionalClassLoaderContextHost29) |
| 335 | rule.Command().Textf(`conditional_target_libs_29="%s"`, |
| 336 | strings.Join(conditionalClassLoaderContextTarget29, " ")) |
Colin Cross | 38b9685 | 2019-05-22 10:21:09 -0700 | [diff] [blame] | 337 | rule.Command().Text("source").Tool(global.Tools.ConstructContext).Input(module.DexPath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Nicolas Geoffray | 2464ef4 | 2019-03-05 14:07:07 +0000 | [diff] [blame] | 340 | // Devices that do not have a product partition use a symlink from /product to /system/product. |
| 341 | // Because on-device dexopt will see dex locations starting with /product, we change the paths |
| 342 | // to mimic this behavior. |
| 343 | dexLocationArg := module.DexLocation |
| 344 | if strings.HasPrefix(dexLocationArg, "/system/product/") { |
| 345 | dexLocationArg = strings.TrimPrefix(dexLocationArg, "/system") |
| 346 | } |
| 347 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 348 | cmd := rule.Command(). |
| 349 | Text(`ANDROID_LOG_TAGS="*:e"`). |
| 350 | Tool(global.Tools.Dex2oat). |
| 351 | Flag("--avoid-storing-invocation"). |
Alex Light | 5de4196 | 2018-12-18 15:16:26 -0800 | [diff] [blame] | 352 | FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 353 | Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatXms). |
| 354 | Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatXmx). |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 355 | Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", module.PreoptBootClassPathDexFiles, ":"). |
| 356 | Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", module.PreoptBootClassPathDexLocations, ":"). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 357 | Flag("${class_loader_context_arg}"). |
| 358 | Flag("${stored_class_loader_context_arg}"). |
Dan Willemsen | 0f41678 | 2019-06-13 21:44:53 +0000 | [diff] [blame] | 359 | FlagWithArg("--boot-image=", bootImageLocation).Implicits(bootImageDeps). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 360 | FlagWithInput("--dex-file=", module.DexPath). |
Nicolas Geoffray | 2464ef4 | 2019-03-05 14:07:07 +0000 | [diff] [blame] | 361 | FlagWithArg("--dex-location=", dexLocationArg). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 362 | FlagWithOutput("--oat-file=", odexPath).ImplicitOutput(vdexPath). |
| 363 | // Pass an empty directory, dex2oat shouldn't be reading arbitrary files |
| 364 | FlagWithArg("--android-root=", global.EmptyDirectory). |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 365 | FlagWithArg("--instruction-set=", arch.String()). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 366 | FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]). |
| 367 | FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]). |
| 368 | Flag("--no-generate-debug-info"). |
| 369 | Flag("--generate-build-id"). |
| 370 | Flag("--abort-on-hard-verifier-error"). |
| 371 | Flag("--force-determinism"). |
| 372 | FlagWithArg("--no-inline-from=", "core-oj.jar") |
| 373 | |
| 374 | var preoptFlags []string |
| 375 | if len(module.PreoptFlags) > 0 { |
| 376 | preoptFlags = module.PreoptFlags |
| 377 | } else if len(global.PreoptFlags) > 0 { |
| 378 | preoptFlags = global.PreoptFlags |
| 379 | } |
| 380 | |
| 381 | if len(preoptFlags) > 0 { |
| 382 | cmd.Text(strings.Join(preoptFlags, " ")) |
| 383 | } |
| 384 | |
| 385 | if module.UncompressedDex { |
| 386 | cmd.FlagWithArg("--copy-dex-files=", "false") |
| 387 | } |
| 388 | |
| 389 | if !anyHavePrefix(preoptFlags, "--compiler-filter=") { |
| 390 | var compilerFilter string |
| 391 | if contains(global.SystemServerJars, module.Name) { |
| 392 | // Jars of system server, use the product option if it is set, speed otherwise. |
| 393 | if global.SystemServerCompilerFilter != "" { |
| 394 | compilerFilter = global.SystemServerCompilerFilter |
| 395 | } else { |
| 396 | compilerFilter = "speed" |
| 397 | } |
| 398 | } else if contains(global.SpeedApps, module.Name) || contains(global.SystemServerApps, module.Name) { |
| 399 | // Apps loaded into system server, and apps the product default to being compiled with the |
| 400 | // 'speed' compiler filter. |
| 401 | compilerFilter = "speed" |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 402 | } else if profile != nil { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 403 | // For non system server jars, use speed-profile when we have a profile. |
| 404 | compilerFilter = "speed-profile" |
| 405 | } else if global.DefaultCompilerFilter != "" { |
| 406 | compilerFilter = global.DefaultCompilerFilter |
| 407 | } else { |
| 408 | compilerFilter = "quicken" |
| 409 | } |
| 410 | cmd.FlagWithArg("--compiler-filter=", compilerFilter) |
| 411 | } |
| 412 | |
| 413 | if generateDM { |
| 414 | cmd.FlagWithArg("--copy-dex-files=", "false") |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 415 | dmPath := module.BuildPath.InSameDir(ctx, "generated.dm") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 416 | dmInstalledPath := pathtools.ReplaceExtension(module.DexLocation, "dm") |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 417 | tmpPath := module.BuildPath.InSameDir(ctx, "primary.vdex") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 418 | rule.Command().Text("cp -f").Input(vdexPath).Output(tmpPath) |
| 419 | rule.Command().Tool(global.Tools.SoongZip). |
| 420 | FlagWithArg("-L", "9"). |
| 421 | FlagWithOutput("-o", dmPath). |
| 422 | Flag("-j"). |
| 423 | Input(tmpPath) |
| 424 | rule.Install(dmPath, dmInstalledPath) |
| 425 | } |
| 426 | |
| 427 | // By default, emit debug info. |
| 428 | debugInfo := true |
| 429 | if global.NoDebugInfo { |
| 430 | // If the global setting suppresses mini-debug-info, disable it. |
| 431 | debugInfo = false |
| 432 | } |
| 433 | |
| 434 | // PRODUCT_SYSTEM_SERVER_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO. |
| 435 | // PRODUCT_OTHER_JAVA_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO. |
| 436 | if contains(global.SystemServerJars, module.Name) { |
| 437 | if global.AlwaysSystemServerDebugInfo { |
| 438 | debugInfo = true |
| 439 | } else if global.NeverSystemServerDebugInfo { |
| 440 | debugInfo = false |
| 441 | } |
| 442 | } else { |
| 443 | if global.AlwaysOtherDebugInfo { |
| 444 | debugInfo = true |
| 445 | } else if global.NeverOtherDebugInfo { |
| 446 | debugInfo = false |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | // Never enable on eng. |
| 451 | if global.IsEng { |
| 452 | debugInfo = false |
| 453 | } |
| 454 | |
| 455 | if debugInfo { |
| 456 | cmd.Flag("--generate-mini-debug-info") |
| 457 | } else { |
| 458 | cmd.Flag("--no-generate-mini-debug-info") |
| 459 | } |
| 460 | |
| 461 | // Set the compiler reason to 'prebuilt' to identify the oat files produced |
| 462 | // during the build, as opposed to compiled on the device. |
| 463 | cmd.FlagWithArg("--compilation-reason=", "prebuilt") |
| 464 | |
| 465 | if appImage { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 466 | appImagePath := odexPath.ReplaceExtension(ctx, "art") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 467 | appImageInstallPath := pathtools.ReplaceExtension(odexInstallPath, "art") |
| 468 | cmd.FlagWithOutput("--app-image-file=", appImagePath). |
| 469 | FlagWithArg("--image-format=", "lz4") |
Mathieu Chartier | 3f7ddbb | 2019-04-29 09:33:50 -0700 | [diff] [blame] | 470 | if !global.DontResolveStartupStrings { |
| 471 | cmd.FlagWithArg("--resolve-startup-const-strings=", "true") |
| 472 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 473 | rule.Install(appImagePath, appImageInstallPath) |
| 474 | } |
| 475 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 476 | if profile != nil { |
| 477 | cmd.FlagWithInput("--profile-file=", profile) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | rule.Install(odexPath, odexInstallPath) |
| 481 | rule.Install(vdexPath, vdexInstallPath) |
| 482 | } |
| 483 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 484 | func shouldGenerateDM(module ModuleConfig, global GlobalConfig) bool { |
| 485 | // Generating DM files only makes sense for verify, avoid doing for non verify compiler filter APKs. |
| 486 | // No reason to use a dm file if the dex is already uncompressed. |
| 487 | return global.GenerateDMFiles && !module.UncompressedDex && |
| 488 | contains(module.PreoptFlags, "--compiler-filter=verify") |
| 489 | } |
| 490 | |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 491 | func OdexOnSystemOtherByName(name string, dexLocation string, global GlobalConfig) bool { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 492 | if !global.HasSystemOther { |
| 493 | return false |
| 494 | } |
| 495 | |
| 496 | if global.SanitizeLite { |
| 497 | return false |
| 498 | } |
| 499 | |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 500 | if contains(global.SpeedApps, name) || contains(global.SystemServerApps, name) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 501 | return false |
| 502 | } |
| 503 | |
| 504 | for _, f := range global.PatternsOnSystemOther { |
Anton Hansson | 12b8d42 | 2019-10-02 18:14:02 +0100 | [diff] [blame] | 505 | // See comment of SYSTEM_OTHER_ODEX_FILTER for details on the matching. |
| 506 | if makefileMatch("/"+f, dexLocation) || makefileMatch(filepath.Join(SystemPartition, f), dexLocation) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 507 | return true |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | return false |
| 512 | } |
| 513 | |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 514 | func odexOnSystemOther(module ModuleConfig, global GlobalConfig) bool { |
| 515 | return OdexOnSystemOtherByName(module.Name, module.DexLocation, global) |
| 516 | } |
| 517 | |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 518 | // PathToLocation converts .../system/framework/arm64/boot.art to .../system/framework/boot.art |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 519 | func PathToLocation(path android.Path, arch android.ArchType) string { |
| 520 | pathArch := filepath.Base(filepath.Dir(path.String())) |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 521 | if pathArch != arch.String() { |
| 522 | panic(fmt.Errorf("last directory in %q must be %q", path, arch.String())) |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 523 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 524 | return filepath.Join(filepath.Dir(filepath.Dir(path.String())), filepath.Base(path.String())) |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 525 | } |
| 526 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 527 | func pathForLibrary(module ModuleConfig, lib string) android.Path { |
| 528 | path, ok := module.LibraryPaths[lib] |
| 529 | if !ok { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 530 | panic(fmt.Errorf("unknown library path for %q", lib)) |
| 531 | } |
| 532 | return path |
| 533 | } |
| 534 | |
| 535 | func makefileMatch(pattern, s string) bool { |
| 536 | percent := strings.IndexByte(pattern, '%') |
| 537 | switch percent { |
| 538 | case -1: |
| 539 | return pattern == s |
| 540 | case len(pattern) - 1: |
| 541 | return strings.HasPrefix(s, pattern[:len(pattern)-1]) |
| 542 | default: |
| 543 | panic(fmt.Errorf("unsupported makefile pattern %q", pattern)) |
| 544 | } |
| 545 | } |
| 546 | |
Ulyana Trafimovich | f2cb7e9 | 2019-11-27 12:26:49 +0000 | [diff] [blame] | 547 | // Expected format for apexJarValue = <apex name>:<jar name> |
| 548 | func SplitApexJarPair(apexJarValue string) (string, string) { |
| 549 | var apexJarPair []string = strings.SplitN(apexJarValue, ":", 2) |
| 550 | if apexJarPair == nil || len(apexJarPair) != 2 { |
| 551 | panic(fmt.Errorf("malformed apexJarValue: %q, expected format: <apex>:<jar>", |
| 552 | apexJarValue)) |
| 553 | } |
| 554 | return apexJarPair[0], apexJarPair[1] |
| 555 | } |
| 556 | |
Roshan Pius | ccc26ef | 2019-11-27 09:37:46 -0800 | [diff] [blame^] | 557 | // Expected format for apexJarValue = <apex name>:<jar name> |
| 558 | func GetJarLocationFromApexJarPair(apexJarValue string) (string) { |
| 559 | apex, jar := SplitApexJarPair(apexJarValue) |
| 560 | return filepath.Join("/apex", apex, "javalib", jar + ".jar") |
| 561 | } |
| 562 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 563 | func contains(l []string, s string) bool { |
| 564 | for _, e := range l { |
| 565 | if e == s { |
| 566 | return true |
| 567 | } |
| 568 | } |
| 569 | return false |
| 570 | } |
| 571 | |
| 572 | // remove all elements in a from b, returning a new slice |
| 573 | func filterOut(a []string, b []string) []string { |
| 574 | var ret []string |
| 575 | for _, x := range b { |
| 576 | if !contains(a, x) { |
| 577 | ret = append(ret, x) |
| 578 | } |
| 579 | } |
| 580 | return ret |
| 581 | } |
| 582 | |
| 583 | func replace(l []string, from, to string) { |
| 584 | for i := range l { |
| 585 | if l[i] == from { |
| 586 | l[i] = to |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | |
Colin Cross | 454c087 | 2019-02-15 23:03:34 -0800 | [diff] [blame] | 591 | var copyOf = android.CopyOf |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 592 | |
| 593 | func anyHavePrefix(l []string, prefix string) bool { |
| 594 | for _, x := range l { |
| 595 | if strings.HasPrefix(x, prefix) { |
| 596 | return true |
| 597 | } |
| 598 | } |
| 599 | return false |
| 600 | } |