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 | |
Ulya Trafimovich | 6cf2c0c | 2020-04-24 12:15:20 +0100 | [diff] [blame] | 50 | var DexpreoptRunningInSoong = false |
| 51 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 52 | // GenerateDexpreoptRule generates a set of commands that will preopt a module based on a GlobalConfig and a |
| 53 | // ModuleConfig. The produced files and their install locations will be available through rule.Installs(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 54 | func GenerateDexpreoptRule(ctx android.BuilderContext, globalSoong *GlobalSoongConfig, |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 55 | global *GlobalConfig, module *ModuleConfig) (rule *android.RuleBuilder, err error) { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 56 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 57 | defer func() { |
| 58 | if r := recover(); r != nil { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 59 | if _, ok := r.(runtime.Error); ok { |
| 60 | panic(r) |
| 61 | } else if e, ok := r.(error); ok { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 62 | err = e |
| 63 | rule = nil |
| 64 | } else { |
| 65 | panic(r) |
| 66 | } |
| 67 | } |
| 68 | }() |
| 69 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 70 | rule = android.NewRuleBuilder(pctx, ctx) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 71 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 72 | generateProfile := module.ProfileClassListing.Valid() && !global.DisableGenerateProfile |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 73 | generateBootProfile := module.ProfileBootListing.Valid() && !global.DisableGenerateProfile |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 74 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 75 | var profile android.WritablePath |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 76 | if generateProfile { |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 77 | profile = profileCommand(ctx, globalSoong, global, module, rule) |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 78 | } |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 79 | if generateBootProfile { |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 80 | bootProfileCommand(ctx, globalSoong, global, module, rule) |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 81 | } |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 82 | |
Ulya Trafimovich | f3ff010 | 2019-12-03 15:39:23 +0000 | [diff] [blame] | 83 | if !dexpreoptDisabled(ctx, global, module) { |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 84 | if valid, err := validateClassLoaderContext(module.ClassLoaderContexts); err != nil { |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 85 | android.ReportPathErrorf(ctx, err.Error()) |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 86 | } else if valid { |
| 87 | fixClassLoaderContext(module.ClassLoaderContexts) |
| 88 | |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 89 | appImage := (generateProfile || module.ForceCreateAppImage || global.DefaultAppImages) && |
| 90 | !module.NoCreateAppImage |
| 91 | |
| 92 | generateDM := shouldGenerateDM(module, global) |
| 93 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 94 | for archIdx, _ := range module.Archs { |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 95 | dexpreoptCommand(ctx, globalSoong, global, module, rule, archIdx, profile, appImage, generateDM) |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return rule, nil |
| 101 | } |
| 102 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 103 | func dexpreoptDisabled(ctx android.PathContext, global *GlobalConfig, module *ModuleConfig) bool { |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 104 | if contains(global.DisablePreoptModules, module.Name) { |
| 105 | return true |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 108 | // Don't preopt individual boot jars, they will be preopted together. |
| 109 | if global.BootJars.ContainsJar(module.Name) { |
| 110 | return true |
| 111 | } |
| 112 | |
Ulyana Trafimovich | f2cb7e9 | 2019-11-27 12:26:49 +0000 | [diff] [blame] | 113 | // Don't preopt system server jars that are updatable. |
Ulya Trafimovich | 249386a | 2020-07-01 14:31:13 +0100 | [diff] [blame] | 114 | if global.UpdatableSystemServerJars.ContainsJar(module.Name) { |
| 115 | return true |
Ulyana Trafimovich | f2cb7e9 | 2019-11-27 12:26:49 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 118 | // If OnlyPreoptBootImageAndSystemServer=true and module is not in boot class path skip |
| 119 | // Also preopt system server jars since selinux prevents system server from loading anything from |
| 120 | // /data. If we don't do this they will need to be extracted which is not favorable for RAM usage |
| 121 | // or performance. If PreoptExtractedApk is true, we ignore the only preopt boot image options. |
Ulya Trafimovich | 249386a | 2020-07-01 14:31:13 +0100 | [diff] [blame] | 122 | if global.OnlyPreoptBootImageAndSystemServer && !global.BootJars.ContainsJar(module.Name) && |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 123 | !contains(global.SystemServerJars, module.Name) && !module.PreoptExtractedApk { |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 124 | return true |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 127 | return false |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 130 | func profileCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig, |
| 131 | module *ModuleConfig, rule *android.RuleBuilder) android.WritablePath { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 132 | |
| 133 | profilePath := module.BuildPath.InSameDir(ctx, "profile.prof") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 134 | profileInstalledPath := module.DexLocation + ".prof" |
| 135 | |
| 136 | if !module.ProfileIsTextListing { |
| 137 | rule.Command().FlagWithOutput("touch ", profilePath) |
| 138 | } |
| 139 | |
| 140 | cmd := rule.Command(). |
| 141 | Text(`ANDROID_LOG_TAGS="*:e"`). |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 142 | Tool(globalSoong.Profman) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 143 | |
| 144 | if module.ProfileIsTextListing { |
| 145 | // The profile is a test listing of classes (used for framework jars). |
| 146 | // 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] | 147 | cmd.FlagWithInput("--create-profile-from=", module.ProfileClassListing.Path()) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 148 | } else { |
| 149 | // The profile is binary profile (used for apps). Run it through profman to |
| 150 | // ensure the profile keys match the apk. |
| 151 | cmd. |
| 152 | Flag("--copy-and-update-profile-key"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 153 | FlagWithInput("--profile-file=", module.ProfileClassListing.Path()) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | cmd. |
| 157 | FlagWithInput("--apk=", module.DexPath). |
| 158 | Flag("--dex-location="+module.DexLocation). |
| 159 | FlagWithOutput("--reference-profile-file=", profilePath) |
| 160 | |
| 161 | if !module.ProfileIsTextListing { |
| 162 | cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath)) |
| 163 | } |
| 164 | rule.Install(profilePath, profileInstalledPath) |
| 165 | |
| 166 | return profilePath |
| 167 | } |
| 168 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 169 | func bootProfileCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig, |
| 170 | module *ModuleConfig, rule *android.RuleBuilder) android.WritablePath { |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 171 | |
| 172 | profilePath := module.BuildPath.InSameDir(ctx, "profile.bprof") |
| 173 | profileInstalledPath := module.DexLocation + ".bprof" |
| 174 | |
| 175 | if !module.ProfileIsTextListing { |
| 176 | rule.Command().FlagWithOutput("touch ", profilePath) |
| 177 | } |
| 178 | |
| 179 | cmd := rule.Command(). |
| 180 | Text(`ANDROID_LOG_TAGS="*:e"`). |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 181 | Tool(globalSoong.Profman) |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 182 | |
| 183 | // The profile is a test listing of methods. |
| 184 | // We need to generate the actual binary profile. |
| 185 | cmd.FlagWithInput("--create-profile-from=", module.ProfileBootListing.Path()) |
| 186 | |
| 187 | cmd. |
| 188 | Flag("--generate-boot-profile"). |
| 189 | FlagWithInput("--apk=", module.DexPath). |
| 190 | Flag("--dex-location="+module.DexLocation). |
| 191 | FlagWithOutput("--reference-profile-file=", profilePath) |
| 192 | |
| 193 | if !module.ProfileIsTextListing { |
| 194 | cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath)) |
| 195 | } |
| 196 | rule.Install(profilePath, profileInstalledPath) |
| 197 | |
| 198 | return profilePath |
| 199 | } |
| 200 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 201 | func dexpreoptCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig, |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 202 | module *ModuleConfig, rule *android.RuleBuilder, archIdx int, profile android.WritablePath, |
| 203 | appImage bool, generateDM bool) { |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 204 | |
| 205 | arch := module.Archs[archIdx] |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 206 | |
| 207 | // HACK: make soname in Soong-generated .odex files match Make. |
| 208 | base := filepath.Base(module.DexLocation) |
| 209 | if filepath.Ext(base) == ".jar" { |
| 210 | base = "javalib.jar" |
| 211 | } else if filepath.Ext(base) == ".apk" { |
| 212 | base = "package.apk" |
| 213 | } |
| 214 | |
| 215 | toOdexPath := func(path string) string { |
| 216 | return filepath.Join( |
| 217 | filepath.Dir(path), |
| 218 | "oat", |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 219 | arch.String(), |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 220 | pathtools.ReplaceExtension(filepath.Base(path), "odex")) |
| 221 | } |
| 222 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 223 | odexPath := module.BuildPath.InSameDir(ctx, "oat", arch.String(), pathtools.ReplaceExtension(base, "odex")) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 224 | odexInstallPath := toOdexPath(module.DexLocation) |
| 225 | if odexOnSystemOther(module, global) { |
Anton Hansson | 43ab0bc | 2019-10-03 14:18:45 +0100 | [diff] [blame] | 226 | odexInstallPath = filepath.Join(SystemOtherPartition, odexInstallPath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 229 | vdexPath := odexPath.ReplaceExtension(ctx, "vdex") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 230 | vdexInstallPath := pathtools.ReplaceExtension(odexInstallPath, "vdex") |
| 231 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 232 | invocationPath := odexPath.ReplaceExtension(ctx, "invocation") |
Alex Light | 5de4196 | 2018-12-18 15:16:26 -0800 | [diff] [blame] | 233 | |
Ulya Trafimovich | dacc6c5 | 2020-03-11 11:59:34 +0000 | [diff] [blame] | 234 | systemServerJars := NonUpdatableSystemServerJars(ctx, global) |
| 235 | |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 236 | rule.Command().FlagWithArg("mkdir -p ", filepath.Dir(odexPath.String())) |
| 237 | rule.Command().FlagWithOutput("rm -f ", odexPath) |
Ulya Trafimovich | c9af538 | 2020-05-29 15:35:06 +0100 | [diff] [blame] | 238 | |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 239 | if jarIndex := android.IndexList(module.Name, systemServerJars); jarIndex >= 0 { |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 240 | // System server jars should be dexpreopted together: class loader context of each jar |
| 241 | // should include all preceding jars on the system server classpath. |
| 242 | |
| 243 | var clcHost android.Paths |
| 244 | var clcTarget []string |
| 245 | for _, lib := range systemServerJars[:jarIndex] { |
| 246 | clcHost = append(clcHost, SystemServerDexJarHostPath(ctx, lib)) |
| 247 | clcTarget = append(clcTarget, filepath.Join("/system/framework", lib+".jar")) |
| 248 | } |
| 249 | |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 250 | // Copy the system server jar to a predefined location where dex2oat will find it. |
| 251 | dexPathHost := SystemServerDexJarHostPath(ctx, module.Name) |
| 252 | rule.Command().Text("mkdir -p").Flag(filepath.Dir(dexPathHost.String())) |
| 253 | rule.Command().Text("cp -f").Input(module.DexPath).Output(dexPathHost) |
| 254 | |
| 255 | checkSystemServerOrder(ctx, jarIndex) |
| 256 | |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 257 | rule.Command(). |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 258 | Text("class_loader_context_arg=--class-loader-context=PCL[" + strings.Join(clcHost.Strings(), ":") + "]"). |
| 259 | Implicits(clcHost). |
| 260 | Text("stored_class_loader_context_arg=--stored-class-loader-context=PCL[" + strings.Join(clcTarget, ":") + "]") |
| 261 | |
Ulya Trafimovich | bd6b076 | 2021-03-12 12:12:12 +0000 | [diff] [blame] | 262 | } else { |
| 263 | // There are three categories of Java modules handled here: |
| 264 | // |
| 265 | // - Modules that have passed verify_uses_libraries check. They are AOT-compiled and |
| 266 | // expected to be loaded on device without CLC mismatch errors. |
| 267 | // |
| 268 | // - Modules that have failed the check in relaxed mode, so it didn't cause a build error. |
| 269 | // They are dexpreopted with "verify" filter and not AOT-compiled. |
| 270 | // TODO(b/132357300): ensure that CLC mismatch errors are ignored with "verify" filter. |
| 271 | // |
| 272 | // - Modules that didn't run the check. They are AOT-compiled, but it's unknown if they |
| 273 | // will have CLC mismatch errors on device (the check is disabled by default). |
| 274 | // |
| 275 | // TODO(b/132357300): enable the check by default and eliminate the last category, so that |
| 276 | // no time/space is wasted on AOT-compiling modules that will fail CLC check on device. |
| 277 | |
| 278 | var manifestOrApk android.Path |
Jeongik Cha | 33a3a81 | 2021-04-15 09:12:49 +0900 | [diff] [blame^] | 279 | if module.ManifestPath.Valid() { |
Ulya Trafimovich | bd6b076 | 2021-03-12 12:12:12 +0000 | [diff] [blame] | 280 | // Ok, there is an XML manifest. |
Jeongik Cha | 33a3a81 | 2021-04-15 09:12:49 +0900 | [diff] [blame^] | 281 | manifestOrApk = module.ManifestPath.Path() |
Ulya Trafimovich | bd6b076 | 2021-03-12 12:12:12 +0000 | [diff] [blame] | 282 | } else if filepath.Ext(base) == ".apk" { |
| 283 | // Ok, there is is an APK with the manifest inside. |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 284 | manifestOrApk = module.DexPath |
Colin Cross | 38b9685 | 2019-05-22 10:21:09 -0700 | [diff] [blame] | 285 | } |
Ulya Trafimovich | bd6b076 | 2021-03-12 12:12:12 +0000 | [diff] [blame] | 286 | |
| 287 | // Generate command that saves target SDK version in a shell variable. |
| 288 | if manifestOrApk == nil { |
| 289 | // There is neither an XML manifest nor APK => nowhere to extract targetSdkVersion from. |
| 290 | // Set the latest ("any") version: then construct_context will not add any compatibility |
| 291 | // libraries (if this is incorrect, there will be a CLC mismatch and dexopt on device). |
| 292 | rule.Command().Textf(`target_sdk_version=%d`, AnySdkVersion) |
| 293 | } else { |
| 294 | rule.Command().Text(`target_sdk_version="$(`). |
| 295 | Tool(globalSoong.ManifestCheck). |
| 296 | Flag("--extract-target-sdk-version"). |
| 297 | Input(manifestOrApk). |
Ulya Trafimovich | dd62295 | 2021-03-25 12:52:49 +0000 | [diff] [blame] | 298 | FlagWithInput("--aapt ", globalSoong.Aapt). |
Ulya Trafimovich | bd6b076 | 2021-03-12 12:12:12 +0000 | [diff] [blame] | 299 | Text(`)"`) |
| 300 | } |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 301 | |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 302 | // Generate command that saves host and target class loader context in shell variables. |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 303 | clc, paths := ComputeClassLoaderContext(module.ClassLoaderContexts) |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 304 | rule.Command(). |
Ulya Trafimovich | bd6b076 | 2021-03-12 12:12:12 +0000 | [diff] [blame] | 305 | Text(`eval "$(`).Tool(globalSoong.ConstructContext). |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 306 | Text(` --target-sdk-version ${target_sdk_version}`). |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 307 | Text(clc).Implicits(paths). |
Ulya Trafimovich | bd6b076 | 2021-03-12 12:12:12 +0000 | [diff] [blame] | 308 | Text(`)"`) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Nicolas Geoffray | 2464ef4 | 2019-03-05 14:07:07 +0000 | [diff] [blame] | 311 | // Devices that do not have a product partition use a symlink from /product to /system/product. |
| 312 | // Because on-device dexopt will see dex locations starting with /product, we change the paths |
| 313 | // to mimic this behavior. |
| 314 | dexLocationArg := module.DexLocation |
| 315 | if strings.HasPrefix(dexLocationArg, "/system/product/") { |
| 316 | dexLocationArg = strings.TrimPrefix(dexLocationArg, "/system") |
| 317 | } |
| 318 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 319 | cmd := rule.Command(). |
| 320 | Text(`ANDROID_LOG_TAGS="*:e"`). |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 321 | Tool(globalSoong.Dex2oat). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 322 | Flag("--avoid-storing-invocation"). |
Alex Light | 5de4196 | 2018-12-18 15:16:26 -0800 | [diff] [blame] | 323 | FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 324 | Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatXms). |
| 325 | Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatXmx). |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 326 | Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", module.PreoptBootClassPathDexFiles, ":"). |
| 327 | Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", module.PreoptBootClassPathDexLocations, ":"). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 328 | Flag("${class_loader_context_arg}"). |
Ulya Trafimovich | dacc6c5 | 2020-03-11 11:59:34 +0000 | [diff] [blame] | 329 | Flag("${stored_class_loader_context_arg}"). |
Ulya Trafimovich | 3391a1e | 2020-01-03 17:33:17 +0000 | [diff] [blame] | 330 | FlagWithArg("--boot-image=", strings.Join(module.DexPreoptImageLocations, ":")).Implicits(module.DexPreoptImagesDeps[archIdx].Paths()). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 331 | FlagWithInput("--dex-file=", module.DexPath). |
Nicolas Geoffray | 2464ef4 | 2019-03-05 14:07:07 +0000 | [diff] [blame] | 332 | FlagWithArg("--dex-location=", dexLocationArg). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 333 | FlagWithOutput("--oat-file=", odexPath).ImplicitOutput(vdexPath). |
| 334 | // Pass an empty directory, dex2oat shouldn't be reading arbitrary files |
| 335 | FlagWithArg("--android-root=", global.EmptyDirectory). |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 336 | FlagWithArg("--instruction-set=", arch.String()). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 337 | FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]). |
| 338 | FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]). |
| 339 | Flag("--no-generate-debug-info"). |
| 340 | Flag("--generate-build-id"). |
| 341 | Flag("--abort-on-hard-verifier-error"). |
| 342 | Flag("--force-determinism"). |
| 343 | FlagWithArg("--no-inline-from=", "core-oj.jar") |
| 344 | |
| 345 | var preoptFlags []string |
| 346 | if len(module.PreoptFlags) > 0 { |
| 347 | preoptFlags = module.PreoptFlags |
| 348 | } else if len(global.PreoptFlags) > 0 { |
| 349 | preoptFlags = global.PreoptFlags |
| 350 | } |
| 351 | |
| 352 | if len(preoptFlags) > 0 { |
| 353 | cmd.Text(strings.Join(preoptFlags, " ")) |
| 354 | } |
| 355 | |
| 356 | if module.UncompressedDex { |
| 357 | cmd.FlagWithArg("--copy-dex-files=", "false") |
| 358 | } |
| 359 | |
Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 360 | if !android.PrefixInList(preoptFlags, "--compiler-filter=") { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 361 | var compilerFilter string |
| 362 | if contains(global.SystemServerJars, module.Name) { |
| 363 | // Jars of system server, use the product option if it is set, speed otherwise. |
| 364 | if global.SystemServerCompilerFilter != "" { |
| 365 | compilerFilter = global.SystemServerCompilerFilter |
| 366 | } else { |
| 367 | compilerFilter = "speed" |
| 368 | } |
| 369 | } else if contains(global.SpeedApps, module.Name) || contains(global.SystemServerApps, module.Name) { |
| 370 | // Apps loaded into system server, and apps the product default to being compiled with the |
| 371 | // 'speed' compiler filter. |
| 372 | compilerFilter = "speed" |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 373 | } else if profile != nil { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 374 | // For non system server jars, use speed-profile when we have a profile. |
| 375 | compilerFilter = "speed-profile" |
| 376 | } else if global.DefaultCompilerFilter != "" { |
| 377 | compilerFilter = global.DefaultCompilerFilter |
| 378 | } else { |
| 379 | compilerFilter = "quicken" |
| 380 | } |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 381 | if module.EnforceUsesLibraries { |
| 382 | // If the verify_uses_libraries check failed (in this case status file contains a |
Ulya Trafimovich | 4a13acb | 2021-03-02 12:25:02 +0000 | [diff] [blame] | 383 | // non-empty error message), then use "verify" compiler filter to avoid compiling any |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 384 | // code (it would be rejected on device because of a class loader context mismatch). |
| 385 | cmd.Text("--compiler-filter=$(if test -s "). |
| 386 | Input(module.EnforceUsesLibrariesStatusFile). |
Ulya Trafimovich | 4a13acb | 2021-03-02 12:25:02 +0000 | [diff] [blame] | 387 | Text(" ; then echo verify ; else echo " + compilerFilter + " ; fi)") |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 388 | } else { |
| 389 | cmd.FlagWithArg("--compiler-filter=", compilerFilter) |
| 390 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | if generateDM { |
| 394 | cmd.FlagWithArg("--copy-dex-files=", "false") |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 395 | dmPath := module.BuildPath.InSameDir(ctx, "generated.dm") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 396 | dmInstalledPath := pathtools.ReplaceExtension(module.DexLocation, "dm") |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 397 | tmpPath := module.BuildPath.InSameDir(ctx, "primary.vdex") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 398 | rule.Command().Text("cp -f").Input(vdexPath).Output(tmpPath) |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 399 | rule.Command().Tool(globalSoong.SoongZip). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 400 | FlagWithArg("-L", "9"). |
| 401 | FlagWithOutput("-o", dmPath). |
| 402 | Flag("-j"). |
| 403 | Input(tmpPath) |
| 404 | rule.Install(dmPath, dmInstalledPath) |
| 405 | } |
| 406 | |
| 407 | // By default, emit debug info. |
| 408 | debugInfo := true |
| 409 | if global.NoDebugInfo { |
| 410 | // If the global setting suppresses mini-debug-info, disable it. |
| 411 | debugInfo = false |
| 412 | } |
| 413 | |
| 414 | // PRODUCT_SYSTEM_SERVER_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO. |
| 415 | // PRODUCT_OTHER_JAVA_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO. |
| 416 | if contains(global.SystemServerJars, module.Name) { |
| 417 | if global.AlwaysSystemServerDebugInfo { |
| 418 | debugInfo = true |
| 419 | } else if global.NeverSystemServerDebugInfo { |
| 420 | debugInfo = false |
| 421 | } |
| 422 | } else { |
| 423 | if global.AlwaysOtherDebugInfo { |
| 424 | debugInfo = true |
| 425 | } else if global.NeverOtherDebugInfo { |
| 426 | debugInfo = false |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | // Never enable on eng. |
| 431 | if global.IsEng { |
| 432 | debugInfo = false |
| 433 | } |
| 434 | |
| 435 | if debugInfo { |
| 436 | cmd.Flag("--generate-mini-debug-info") |
| 437 | } else { |
| 438 | cmd.Flag("--no-generate-mini-debug-info") |
| 439 | } |
| 440 | |
| 441 | // Set the compiler reason to 'prebuilt' to identify the oat files produced |
| 442 | // during the build, as opposed to compiled on the device. |
| 443 | cmd.FlagWithArg("--compilation-reason=", "prebuilt") |
| 444 | |
| 445 | if appImage { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 446 | appImagePath := odexPath.ReplaceExtension(ctx, "art") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 447 | appImageInstallPath := pathtools.ReplaceExtension(odexInstallPath, "art") |
| 448 | cmd.FlagWithOutput("--app-image-file=", appImagePath). |
| 449 | FlagWithArg("--image-format=", "lz4") |
Mathieu Chartier | 3f7ddbb | 2019-04-29 09:33:50 -0700 | [diff] [blame] | 450 | if !global.DontResolveStartupStrings { |
| 451 | cmd.FlagWithArg("--resolve-startup-const-strings=", "true") |
| 452 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 453 | rule.Install(appImagePath, appImageInstallPath) |
| 454 | } |
| 455 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 456 | if profile != nil { |
| 457 | cmd.FlagWithInput("--profile-file=", profile) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | rule.Install(odexPath, odexInstallPath) |
| 461 | rule.Install(vdexPath, vdexInstallPath) |
| 462 | } |
| 463 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 464 | func shouldGenerateDM(module *ModuleConfig, global *GlobalConfig) bool { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 465 | // Generating DM files only makes sense for verify, avoid doing for non verify compiler filter APKs. |
| 466 | // No reason to use a dm file if the dex is already uncompressed. |
| 467 | return global.GenerateDMFiles && !module.UncompressedDex && |
| 468 | contains(module.PreoptFlags, "--compiler-filter=verify") |
| 469 | } |
| 470 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 471 | func OdexOnSystemOtherByName(name string, dexLocation string, global *GlobalConfig) bool { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 472 | if !global.HasSystemOther { |
| 473 | return false |
| 474 | } |
| 475 | |
| 476 | if global.SanitizeLite { |
| 477 | return false |
| 478 | } |
| 479 | |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 480 | if contains(global.SpeedApps, name) || contains(global.SystemServerApps, name) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 481 | return false |
| 482 | } |
| 483 | |
| 484 | for _, f := range global.PatternsOnSystemOther { |
Anton Hansson | da4d9d9 | 2020-09-15 09:28:55 +0000 | [diff] [blame] | 485 | if makefileMatch(filepath.Join(SystemPartition, f), dexLocation) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 486 | return true |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | return false |
| 491 | } |
| 492 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 493 | func odexOnSystemOther(module *ModuleConfig, global *GlobalConfig) bool { |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 494 | return OdexOnSystemOtherByName(module.Name, module.DexLocation, global) |
| 495 | } |
| 496 | |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 497 | // PathToLocation converts .../system/framework/arm64/boot.art to .../system/framework/boot.art |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 498 | func PathToLocation(path android.Path, arch android.ArchType) string { |
| 499 | pathArch := filepath.Base(filepath.Dir(path.String())) |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 500 | if pathArch != arch.String() { |
| 501 | 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] | 502 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 503 | 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] | 504 | } |
| 505 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 506 | func makefileMatch(pattern, s string) bool { |
| 507 | percent := strings.IndexByte(pattern, '%') |
| 508 | switch percent { |
| 509 | case -1: |
| 510 | return pattern == s |
| 511 | case len(pattern) - 1: |
| 512 | return strings.HasPrefix(s, pattern[:len(pattern)-1]) |
| 513 | default: |
| 514 | panic(fmt.Errorf("unsupported makefile pattern %q", pattern)) |
| 515 | } |
| 516 | } |
| 517 | |
Ulya Trafimovich | f3ff010 | 2019-12-03 15:39:23 +0000 | [diff] [blame] | 518 | var nonUpdatableSystemServerJarsKey = android.NewOnceKey("nonUpdatableSystemServerJars") |
| 519 | |
| 520 | // TODO: eliminate the superficial global config parameter by moving global config definition |
| 521 | // from java subpackage to dexpreopt. |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 522 | func NonUpdatableSystemServerJars(ctx android.PathContext, global *GlobalConfig) []string { |
Ulya Trafimovich | f3ff010 | 2019-12-03 15:39:23 +0000 | [diff] [blame] | 523 | return ctx.Config().Once(nonUpdatableSystemServerJarsKey, func() interface{} { |
Ulya Trafimovich | 249386a | 2020-07-01 14:31:13 +0100 | [diff] [blame] | 524 | return android.RemoveListFromList(global.SystemServerJars, global.UpdatableSystemServerJars.CopyOfJars()) |
Ulya Trafimovich | f3ff010 | 2019-12-03 15:39:23 +0000 | [diff] [blame] | 525 | }).([]string) |
| 526 | } |
| 527 | |
Ulya Trafimovich | dacc6c5 | 2020-03-11 11:59:34 +0000 | [diff] [blame] | 528 | // A predefined location for the system server dex jars. This is needed in order to generate |
| 529 | // class loader context for dex2oat, as the path to the jar in the Soong module may be unknown |
| 530 | // at that time (Soong processes the jars in dependency order, which may be different from the |
| 531 | // the system server classpath order). |
| 532 | func SystemServerDexJarHostPath(ctx android.PathContext, jar string) android.OutputPath { |
Ulya Trafimovich | 6cf2c0c | 2020-04-24 12:15:20 +0100 | [diff] [blame] | 533 | if DexpreoptRunningInSoong { |
| 534 | // Soong module, just use the default output directory $OUT/soong. |
| 535 | return android.PathForOutput(ctx, "system_server_dexjars", jar+".jar") |
| 536 | } else { |
| 537 | // Make module, default output directory is $OUT (passed via the "null config" created |
| 538 | // by dexpreopt_gen). Append Soong subdirectory to match Soong module paths. |
| 539 | return android.PathForOutput(ctx, "soong", "system_server_dexjars", jar+".jar") |
| 540 | } |
Ulya Trafimovich | dacc6c5 | 2020-03-11 11:59:34 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Ulya Trafimovich | cd3203f | 2020-03-27 11:30:00 +0000 | [diff] [blame] | 543 | // Check the order of jars on the system server classpath and give a warning/error if a jar precedes |
| 544 | // one of its dependencies. This is not an error, but a missed optimization, as dexpreopt won't |
| 545 | // have the dependency jar in the class loader context, and it won't be able to resolve any |
| 546 | // references to its classes and methods. |
| 547 | func checkSystemServerOrder(ctx android.PathContext, jarIndex int) { |
| 548 | mctx, isModule := ctx.(android.ModuleContext) |
| 549 | if isModule { |
| 550 | config := GetGlobalConfig(ctx) |
| 551 | jars := NonUpdatableSystemServerJars(ctx, config) |
| 552 | mctx.WalkDeps(func(dep android.Module, parent android.Module) bool { |
| 553 | depIndex := android.IndexList(dep.Name(), jars) |
| 554 | if jarIndex < depIndex && !config.BrokenSuboptimalOrderOfSystemServerJars { |
| 555 | jar := jars[jarIndex] |
| 556 | dep := jars[depIndex] |
| 557 | mctx.ModuleErrorf("non-optimal order of jars on the system server classpath:"+ |
| 558 | " '%s' precedes its dependency '%s', so dexpreopt is unable to resolve any"+ |
| 559 | " references from '%s' to '%s'.\n", jar, dep, jar, dep) |
| 560 | } |
| 561 | return true |
| 562 | }) |
| 563 | } |
| 564 | } |
| 565 | |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 566 | // Returns path to a file containing the reult of verify_uses_libraries check (empty if the check |
| 567 | // has succeeded, or an error message if it failed). |
| 568 | func UsesLibrariesStatusFile(ctx android.ModuleContext) android.WritablePath { |
| 569 | return android.PathForModuleOut(ctx, "enforce_uses_libraries.status") |
| 570 | } |
| 571 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 572 | func contains(l []string, s string) bool { |
| 573 | for _, e := range l { |
| 574 | if e == s { |
| 575 | return true |
| 576 | } |
| 577 | } |
| 578 | return false |
| 579 | } |
| 580 | |
Colin Cross | 454c087 | 2019-02-15 23:03:34 -0800 | [diff] [blame] | 581 | var copyOf = android.CopyOf |