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