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(). |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 54 | func GenerateDexpreoptRule(ctx android.PathContext, globalSoong *GlobalSoongConfig, |
| 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 | 758290d | 2019-02-01 16:42:32 -0800 | [diff] [blame] | 70 | rule = android.NewRuleBuilder() |
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 | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 84 | if clc := genClassLoaderContext(ctx, global, module); clc != nil { |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 85 | appImage := (generateProfile || module.ForceCreateAppImage || global.DefaultAppImages) && |
| 86 | !module.NoCreateAppImage |
| 87 | |
| 88 | generateDM := shouldGenerateDM(module, global) |
| 89 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 90 | for archIdx, _ := range module.Archs { |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 91 | dexpreoptCommand(ctx, globalSoong, global, module, rule, archIdx, *clc, profile, appImage, generateDM) |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return rule, nil |
| 97 | } |
| 98 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 99 | func dexpreoptDisabled(ctx android.PathContext, global *GlobalConfig, module *ModuleConfig) bool { |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 100 | if contains(global.DisablePreoptModules, module.Name) { |
| 101 | return true |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 104 | // Don't preopt individual boot jars, they will be preopted together. |
| 105 | if global.BootJars.ContainsJar(module.Name) { |
| 106 | return true |
| 107 | } |
| 108 | |
Ulyana Trafimovich | f2cb7e9 | 2019-11-27 12:26:49 +0000 | [diff] [blame] | 109 | // Don't preopt system server jars that are updatable. |
Ulya Trafimovich | 249386a | 2020-07-01 14:31:13 +0100 | [diff] [blame] | 110 | if global.UpdatableSystemServerJars.ContainsJar(module.Name) { |
| 111 | return true |
Ulyana Trafimovich | f2cb7e9 | 2019-11-27 12:26:49 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 114 | // If OnlyPreoptBootImageAndSystemServer=true and module is not in boot class path skip |
| 115 | // Also preopt system server jars since selinux prevents system server from loading anything from |
| 116 | // /data. If we don't do this they will need to be extracted which is not favorable for RAM usage |
| 117 | // 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] | 118 | if global.OnlyPreoptBootImageAndSystemServer && !global.BootJars.ContainsJar(module.Name) && |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 119 | !contains(global.SystemServerJars, module.Name) && !module.PreoptExtractedApk { |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 120 | return true |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 123 | return false |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 126 | func profileCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig, |
| 127 | module *ModuleConfig, rule *android.RuleBuilder) android.WritablePath { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 128 | |
| 129 | profilePath := module.BuildPath.InSameDir(ctx, "profile.prof") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 130 | profileInstalledPath := module.DexLocation + ".prof" |
| 131 | |
| 132 | if !module.ProfileIsTextListing { |
| 133 | rule.Command().FlagWithOutput("touch ", profilePath) |
| 134 | } |
| 135 | |
| 136 | cmd := rule.Command(). |
| 137 | Text(`ANDROID_LOG_TAGS="*:e"`). |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 138 | Tool(globalSoong.Profman) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 139 | |
| 140 | if module.ProfileIsTextListing { |
| 141 | // The profile is a test listing of classes (used for framework jars). |
| 142 | // 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] | 143 | cmd.FlagWithInput("--create-profile-from=", module.ProfileClassListing.Path()) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 144 | } else { |
| 145 | // The profile is binary profile (used for apps). Run it through profman to |
| 146 | // ensure the profile keys match the apk. |
| 147 | cmd. |
| 148 | Flag("--copy-and-update-profile-key"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 149 | FlagWithInput("--profile-file=", module.ProfileClassListing.Path()) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | cmd. |
| 153 | FlagWithInput("--apk=", module.DexPath). |
| 154 | Flag("--dex-location="+module.DexLocation). |
| 155 | FlagWithOutput("--reference-profile-file=", profilePath) |
| 156 | |
| 157 | if !module.ProfileIsTextListing { |
| 158 | cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath)) |
| 159 | } |
| 160 | rule.Install(profilePath, profileInstalledPath) |
| 161 | |
| 162 | return profilePath |
| 163 | } |
| 164 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 165 | func bootProfileCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig, |
| 166 | module *ModuleConfig, rule *android.RuleBuilder) android.WritablePath { |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 167 | |
| 168 | profilePath := module.BuildPath.InSameDir(ctx, "profile.bprof") |
| 169 | profileInstalledPath := module.DexLocation + ".bprof" |
| 170 | |
| 171 | if !module.ProfileIsTextListing { |
| 172 | rule.Command().FlagWithOutput("touch ", profilePath) |
| 173 | } |
| 174 | |
| 175 | cmd := rule.Command(). |
| 176 | Text(`ANDROID_LOG_TAGS="*:e"`). |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 177 | Tool(globalSoong.Profman) |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 178 | |
| 179 | // The profile is a test listing of methods. |
| 180 | // We need to generate the actual binary profile. |
| 181 | cmd.FlagWithInput("--create-profile-from=", module.ProfileBootListing.Path()) |
| 182 | |
| 183 | cmd. |
| 184 | Flag("--generate-boot-profile"). |
| 185 | FlagWithInput("--apk=", module.DexPath). |
| 186 | Flag("--dex-location="+module.DexLocation). |
| 187 | FlagWithOutput("--reference-profile-file=", profilePath) |
| 188 | |
| 189 | if !module.ProfileIsTextListing { |
| 190 | cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath)) |
| 191 | } |
| 192 | rule.Install(profilePath, profileInstalledPath) |
| 193 | |
| 194 | return profilePath |
| 195 | } |
| 196 | |
Ulya Trafimovich | 696c59d | 2020-06-01 16:10:56 +0100 | [diff] [blame] | 197 | type classLoaderContext struct { |
| 198 | // The class loader context using paths in the build. |
| 199 | Host android.Paths |
| 200 | |
| 201 | // The class loader context using paths as they will be on the device. |
| 202 | Target []string |
| 203 | } |
| 204 | |
| 205 | // A map of class loader contexts for each SDK version. |
| 206 | // A map entry for "any" version contains libraries that are unconditionally added to class loader |
| 207 | // context. Map entries for existing versions contains libraries that were in the default classpath |
| 208 | // until that API version, and should be added to class loader context if and only if the |
| 209 | // targetSdkVersion in the manifest or APK is less than that API version. |
| 210 | type classLoaderContextMap map[int]*classLoaderContext |
| 211 | |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 212 | const anySdkVersion int = 9999 // should go last in class loader context |
Ulya Trafimovich | 696c59d | 2020-06-01 16:10:56 +0100 | [diff] [blame] | 213 | |
Ulya Trafimovich | 696c59d | 2020-06-01 16:10:56 +0100 | [diff] [blame] | 214 | func (m classLoaderContextMap) getValue(sdkVer int) *classLoaderContext { |
| 215 | if _, ok := m[sdkVer]; !ok { |
| 216 | m[sdkVer] = &classLoaderContext{} |
| 217 | } |
| 218 | return m[sdkVer] |
| 219 | } |
| 220 | |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 221 | func (m classLoaderContextMap) addLibs(sdkVer int, module *ModuleConfig, libs ...string) bool { |
Ulya Trafimovich | 696c59d | 2020-06-01 16:10:56 +0100 | [diff] [blame] | 222 | clc := m.getValue(sdkVer) |
| 223 | for _, lib := range libs { |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 224 | if p := pathForLibrary(module, lib); p != nil { |
| 225 | clc.Host = append(clc.Host, p.Host) |
| 226 | clc.Target = append(clc.Target, p.Device) |
| 227 | } else { |
| 228 | return false |
| 229 | } |
Ulya Trafimovich | 696c59d | 2020-06-01 16:10:56 +0100 | [diff] [blame] | 230 | } |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 231 | return true |
Ulya Trafimovich | 696c59d | 2020-06-01 16:10:56 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | func (m classLoaderContextMap) addSystemServerLibs(sdkVer int, ctx android.PathContext, module *ModuleConfig, libs ...string) { |
| 235 | clc := m.getValue(sdkVer) |
| 236 | for _, lib := range libs { |
| 237 | clc.Host = append(clc.Host, SystemServerDexJarHostPath(ctx, lib)) |
| 238 | clc.Target = append(clc.Target, filepath.Join("/system/framework", lib+".jar")) |
| 239 | } |
| 240 | } |
| 241 | |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 242 | // genClassLoaderContext generates host and target class loader context to be passed to the dex2oat |
| 243 | // command for the dexpreopted module. There are three possible cases: |
| 244 | // |
| 245 | // 1. System server jars. They have a special class loader context that includes other system |
| 246 | // server jars. |
| 247 | // |
| 248 | // 2. Library jars or APKs which have precise list of their <uses-library> libs. Their class loader |
| 249 | // context includes build and on-device paths to these libs. In some cases it may happen that |
| 250 | // the path to a <uses-library> is unknown (e.g. the dexpreopted module may depend on stubs |
| 251 | // library, whose implementation library is missing from the build altogether). In such case |
| 252 | // dexpreopting with the <uses-library> is impossible, and dexpreopting without it is pointless, |
| 253 | // as the runtime classpath won't match and the dexpreopted code will be discarded. Therefore in |
| 254 | // such cases the function returns nil, which disables dexpreopt. |
| 255 | // |
| 256 | // 2. All other library jars or APKs for which the exact <uses-library> list is unknown. They use |
| 257 | // the unsafe &-classpath workaround that means empty class loader context and absence of runtime |
| 258 | // check that the class loader context provided by the PackageManager agrees with the stored |
| 259 | // class loader context recorded in the .odex file. |
| 260 | // |
| 261 | func genClassLoaderContext(ctx android.PathContext, global *GlobalConfig, module *ModuleConfig) *classLoaderContextMap { |
| 262 | classLoaderContexts := make(classLoaderContextMap) |
| 263 | systemServerJars := NonUpdatableSystemServerJars(ctx, global) |
| 264 | |
| 265 | if jarIndex := android.IndexList(module.Name, systemServerJars); jarIndex >= 0 { |
| 266 | // System server jars should be dexpreopted together: class loader context of each jar |
| 267 | // should include all preceding jars on the system server classpath. |
| 268 | classLoaderContexts.addSystemServerLibs(anySdkVersion, ctx, module, systemServerJars[:jarIndex]...) |
| 269 | |
| 270 | } else if module.EnforceUsesLibraries { |
| 271 | // Unconditional class loader context. |
| 272 | usesLibs := append(copyOf(module.UsesLibraries), module.OptionalUsesLibraries...) |
| 273 | if !classLoaderContexts.addLibs(anySdkVersion, module, usesLibs...) { |
| 274 | return nil |
| 275 | } |
| 276 | |
| 277 | // Conditional class loader context for API version < 28. |
| 278 | const httpLegacy = "org.apache.http.legacy" |
| 279 | if !contains(usesLibs, httpLegacy) { |
| 280 | if !classLoaderContexts.addLibs(28, module, httpLegacy) { |
| 281 | return nil |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // Conditional class loader context for API version < 29. |
| 286 | usesLibs29 := []string{ |
| 287 | "android.hidl.base-V1.0-java", |
| 288 | "android.hidl.manager-V1.0-java", |
| 289 | } |
| 290 | if !classLoaderContexts.addLibs(29, module, usesLibs29...) { |
| 291 | return nil |
| 292 | } |
| 293 | |
| 294 | // Conditional class loader context for API version < 30. |
| 295 | const testBase = "android.test.base" |
| 296 | if !contains(usesLibs, testBase) { |
| 297 | if !classLoaderContexts.addLibs(30, module, testBase) { |
| 298 | return nil |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | } else { |
| 303 | // Pass special class loader context to skip the classpath and collision check. |
| 304 | // This will get removed once LOCAL_USES_LIBRARIES is enforced. |
| 305 | // Right now LOCAL_USES_LIBRARIES is opt in, for the case where it's not specified we still default |
| 306 | // to the &. |
| 307 | } |
| 308 | |
| 309 | return &classLoaderContexts |
| 310 | } |
| 311 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 312 | func dexpreoptCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig, |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 313 | module *ModuleConfig, rule *android.RuleBuilder, archIdx int, classLoaderContexts classLoaderContextMap, |
| 314 | profile android.WritablePath, appImage bool, generateDM bool) { |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 315 | |
| 316 | arch := module.Archs[archIdx] |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 317 | |
| 318 | // HACK: make soname in Soong-generated .odex files match Make. |
| 319 | base := filepath.Base(module.DexLocation) |
| 320 | if filepath.Ext(base) == ".jar" { |
| 321 | base = "javalib.jar" |
| 322 | } else if filepath.Ext(base) == ".apk" { |
| 323 | base = "package.apk" |
| 324 | } |
| 325 | |
| 326 | toOdexPath := func(path string) string { |
| 327 | return filepath.Join( |
| 328 | filepath.Dir(path), |
| 329 | "oat", |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 330 | arch.String(), |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 331 | pathtools.ReplaceExtension(filepath.Base(path), "odex")) |
| 332 | } |
| 333 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 334 | odexPath := module.BuildPath.InSameDir(ctx, "oat", arch.String(), pathtools.ReplaceExtension(base, "odex")) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 335 | odexInstallPath := toOdexPath(module.DexLocation) |
| 336 | if odexOnSystemOther(module, global) { |
Anton Hansson | 43ab0bc | 2019-10-03 14:18:45 +0100 | [diff] [blame] | 337 | odexInstallPath = filepath.Join(SystemOtherPartition, odexInstallPath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 340 | vdexPath := odexPath.ReplaceExtension(ctx, "vdex") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 341 | vdexInstallPath := pathtools.ReplaceExtension(odexInstallPath, "vdex") |
| 342 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 343 | invocationPath := odexPath.ReplaceExtension(ctx, "invocation") |
Alex Light | 5de4196 | 2018-12-18 15:16:26 -0800 | [diff] [blame] | 344 | |
Ulya Trafimovich | dacc6c5 | 2020-03-11 11:59:34 +0000 | [diff] [blame] | 345 | systemServerJars := NonUpdatableSystemServerJars(ctx, global) |
| 346 | |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 347 | rule.Command().FlagWithArg("mkdir -p ", filepath.Dir(odexPath.String())) |
| 348 | rule.Command().FlagWithOutput("rm -f ", odexPath) |
Ulya Trafimovich | c9af538 | 2020-05-29 15:35:06 +0100 | [diff] [blame] | 349 | |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 350 | if jarIndex := android.IndexList(module.Name, systemServerJars); jarIndex >= 0 { |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 351 | // Copy the system server jar to a predefined location where dex2oat will find it. |
| 352 | dexPathHost := SystemServerDexJarHostPath(ctx, module.Name) |
| 353 | rule.Command().Text("mkdir -p").Flag(filepath.Dir(dexPathHost.String())) |
| 354 | rule.Command().Text("cp -f").Input(module.DexPath).Output(dexPathHost) |
| 355 | |
| 356 | checkSystemServerOrder(ctx, jarIndex) |
| 357 | |
| 358 | clc := classLoaderContexts[anySdkVersion] |
| 359 | rule.Command(). |
| 360 | Text("class_loader_context_arg=--class-loader-context=PCL[" + strings.Join(clc.Host.Strings(), ":") + "]"). |
| 361 | Implicits(clc.Host). |
| 362 | Text("stored_class_loader_context_arg=--stored-class-loader-context=PCL[" + strings.Join(clc.Target, ":") + "]") |
| 363 | } else if module.EnforceUsesLibraries { |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 364 | // Generate command that saves target SDK version in a shell variable. |
Colin Cross | 38b9685 | 2019-05-22 10:21:09 -0700 | [diff] [blame] | 365 | if module.ManifestPath != nil { |
| 366 | rule.Command().Text(`target_sdk_version="$(`). |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 367 | Tool(globalSoong.ManifestCheck). |
Colin Cross | 38b9685 | 2019-05-22 10:21:09 -0700 | [diff] [blame] | 368 | Flag("--extract-target-sdk-version"). |
| 369 | Input(module.ManifestPath). |
| 370 | Text(`)"`) |
| 371 | } else { |
| 372 | // No manifest to extract targetSdkVersion from, hope that DexJar is an APK |
| 373 | rule.Command().Text(`target_sdk_version="$(`). |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 374 | Tool(globalSoong.Aapt). |
Colin Cross | 38b9685 | 2019-05-22 10:21:09 -0700 | [diff] [blame] | 375 | Flag("dump badging"). |
| 376 | Input(module.DexPath). |
| 377 | Text(`| grep "targetSdkVersion" | sed -n "s/targetSdkVersion:'\(.*\)'/\1/p"`). |
| 378 | Text(`)"`) |
| 379 | } |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 380 | |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 381 | // Generate command that saves host and target class loader context in shell variables. |
| 382 | cmd := rule.Command(). |
| 383 | Text(`eval "$(`).Tool(globalSoong.ConstructContext). |
| 384 | Text(` --target-sdk-version ${target_sdk_version}`) |
Ulya Trafimovich | b8063c6 | 2020-08-20 11:33:12 +0100 | [diff] [blame] | 385 | for _, ver := range android.SortedIntKeys(classLoaderContexts) { |
Ulya Trafimovich | 696c59d | 2020-06-01 16:10:56 +0100 | [diff] [blame] | 386 | clc := classLoaderContexts.getValue(ver) |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 387 | verString := fmt.Sprintf("%d", ver) |
Ulya Trafimovich | 696c59d | 2020-06-01 16:10:56 +0100 | [diff] [blame] | 388 | if ver == anySdkVersion { |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 389 | verString = "any" // a special keyword that means any SDK version |
Ulya Trafimovich | 696c59d | 2020-06-01 16:10:56 +0100 | [diff] [blame] | 390 | } |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 391 | cmd.Textf(`--host-classpath-for-sdk %s %s`, verString, strings.Join(clc.Host.Strings(), ":")). |
| 392 | Implicits(clc.Host). |
| 393 | Textf(`--target-classpath-for-sdk %s %s`, verString, strings.Join(clc.Target, ":")) |
Ulya Trafimovich | 696c59d | 2020-06-01 16:10:56 +0100 | [diff] [blame] | 394 | } |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 395 | cmd.Text(`)"`) |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 396 | } else { |
| 397 | // Pass special class loader context to skip the classpath and collision check. |
| 398 | // This will get removed once LOCAL_USES_LIBRARIES is enforced. |
| 399 | // Right now LOCAL_USES_LIBRARIES is opt in, for the case where it's not specified we still default |
| 400 | // to the &. |
| 401 | rule.Command(). |
| 402 | Text(`class_loader_context_arg=--class-loader-context=\&`). |
| 403 | Text(`stored_class_loader_context_arg=""`) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Nicolas Geoffray | 2464ef4 | 2019-03-05 14:07:07 +0000 | [diff] [blame] | 406 | // Devices that do not have a product partition use a symlink from /product to /system/product. |
| 407 | // Because on-device dexopt will see dex locations starting with /product, we change the paths |
| 408 | // to mimic this behavior. |
| 409 | dexLocationArg := module.DexLocation |
| 410 | if strings.HasPrefix(dexLocationArg, "/system/product/") { |
| 411 | dexLocationArg = strings.TrimPrefix(dexLocationArg, "/system") |
| 412 | } |
| 413 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 414 | cmd := rule.Command(). |
| 415 | Text(`ANDROID_LOG_TAGS="*:e"`). |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 416 | Tool(globalSoong.Dex2oat). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 417 | Flag("--avoid-storing-invocation"). |
Alex Light | 5de4196 | 2018-12-18 15:16:26 -0800 | [diff] [blame] | 418 | FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 419 | Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatXms). |
| 420 | Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatXmx). |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 421 | Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", module.PreoptBootClassPathDexFiles, ":"). |
| 422 | Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", module.PreoptBootClassPathDexLocations, ":"). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 423 | Flag("${class_loader_context_arg}"). |
Ulya Trafimovich | dacc6c5 | 2020-03-11 11:59:34 +0000 | [diff] [blame] | 424 | Flag("${stored_class_loader_context_arg}"). |
Ulya Trafimovich | 3391a1e | 2020-01-03 17:33:17 +0000 | [diff] [blame] | 425 | FlagWithArg("--boot-image=", strings.Join(module.DexPreoptImageLocations, ":")).Implicits(module.DexPreoptImagesDeps[archIdx].Paths()). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 426 | FlagWithInput("--dex-file=", module.DexPath). |
Nicolas Geoffray | 2464ef4 | 2019-03-05 14:07:07 +0000 | [diff] [blame] | 427 | FlagWithArg("--dex-location=", dexLocationArg). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 428 | FlagWithOutput("--oat-file=", odexPath).ImplicitOutput(vdexPath). |
| 429 | // Pass an empty directory, dex2oat shouldn't be reading arbitrary files |
| 430 | FlagWithArg("--android-root=", global.EmptyDirectory). |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 431 | FlagWithArg("--instruction-set=", arch.String()). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 432 | FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]). |
| 433 | FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]). |
| 434 | Flag("--no-generate-debug-info"). |
| 435 | Flag("--generate-build-id"). |
| 436 | Flag("--abort-on-hard-verifier-error"). |
| 437 | Flag("--force-determinism"). |
| 438 | FlagWithArg("--no-inline-from=", "core-oj.jar") |
| 439 | |
| 440 | var preoptFlags []string |
| 441 | if len(module.PreoptFlags) > 0 { |
| 442 | preoptFlags = module.PreoptFlags |
| 443 | } else if len(global.PreoptFlags) > 0 { |
| 444 | preoptFlags = global.PreoptFlags |
| 445 | } |
| 446 | |
| 447 | if len(preoptFlags) > 0 { |
| 448 | cmd.Text(strings.Join(preoptFlags, " ")) |
| 449 | } |
| 450 | |
| 451 | if module.UncompressedDex { |
| 452 | cmd.FlagWithArg("--copy-dex-files=", "false") |
| 453 | } |
| 454 | |
Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 455 | if !android.PrefixInList(preoptFlags, "--compiler-filter=") { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 456 | var compilerFilter string |
| 457 | if contains(global.SystemServerJars, module.Name) { |
| 458 | // Jars of system server, use the product option if it is set, speed otherwise. |
| 459 | if global.SystemServerCompilerFilter != "" { |
| 460 | compilerFilter = global.SystemServerCompilerFilter |
| 461 | } else { |
| 462 | compilerFilter = "speed" |
| 463 | } |
| 464 | } else if contains(global.SpeedApps, module.Name) || contains(global.SystemServerApps, module.Name) { |
| 465 | // Apps loaded into system server, and apps the product default to being compiled with the |
| 466 | // 'speed' compiler filter. |
| 467 | compilerFilter = "speed" |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 468 | } else if profile != nil { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 469 | // For non system server jars, use speed-profile when we have a profile. |
| 470 | compilerFilter = "speed-profile" |
| 471 | } else if global.DefaultCompilerFilter != "" { |
| 472 | compilerFilter = global.DefaultCompilerFilter |
| 473 | } else { |
| 474 | compilerFilter = "quicken" |
| 475 | } |
| 476 | cmd.FlagWithArg("--compiler-filter=", compilerFilter) |
| 477 | } |
| 478 | |
| 479 | if generateDM { |
| 480 | cmd.FlagWithArg("--copy-dex-files=", "false") |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 481 | dmPath := module.BuildPath.InSameDir(ctx, "generated.dm") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 482 | dmInstalledPath := pathtools.ReplaceExtension(module.DexLocation, "dm") |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 483 | tmpPath := module.BuildPath.InSameDir(ctx, "primary.vdex") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 484 | rule.Command().Text("cp -f").Input(vdexPath).Output(tmpPath) |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 485 | rule.Command().Tool(globalSoong.SoongZip). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 486 | FlagWithArg("-L", "9"). |
| 487 | FlagWithOutput("-o", dmPath). |
| 488 | Flag("-j"). |
| 489 | Input(tmpPath) |
| 490 | rule.Install(dmPath, dmInstalledPath) |
| 491 | } |
| 492 | |
| 493 | // By default, emit debug info. |
| 494 | debugInfo := true |
| 495 | if global.NoDebugInfo { |
| 496 | // If the global setting suppresses mini-debug-info, disable it. |
| 497 | debugInfo = false |
| 498 | } |
| 499 | |
| 500 | // PRODUCT_SYSTEM_SERVER_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO. |
| 501 | // PRODUCT_OTHER_JAVA_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO. |
| 502 | if contains(global.SystemServerJars, module.Name) { |
| 503 | if global.AlwaysSystemServerDebugInfo { |
| 504 | debugInfo = true |
| 505 | } else if global.NeverSystemServerDebugInfo { |
| 506 | debugInfo = false |
| 507 | } |
| 508 | } else { |
| 509 | if global.AlwaysOtherDebugInfo { |
| 510 | debugInfo = true |
| 511 | } else if global.NeverOtherDebugInfo { |
| 512 | debugInfo = false |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | // Never enable on eng. |
| 517 | if global.IsEng { |
| 518 | debugInfo = false |
| 519 | } |
| 520 | |
| 521 | if debugInfo { |
| 522 | cmd.Flag("--generate-mini-debug-info") |
| 523 | } else { |
| 524 | cmd.Flag("--no-generate-mini-debug-info") |
| 525 | } |
| 526 | |
| 527 | // Set the compiler reason to 'prebuilt' to identify the oat files produced |
| 528 | // during the build, as opposed to compiled on the device. |
| 529 | cmd.FlagWithArg("--compilation-reason=", "prebuilt") |
| 530 | |
| 531 | if appImage { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 532 | appImagePath := odexPath.ReplaceExtension(ctx, "art") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 533 | appImageInstallPath := pathtools.ReplaceExtension(odexInstallPath, "art") |
| 534 | cmd.FlagWithOutput("--app-image-file=", appImagePath). |
| 535 | FlagWithArg("--image-format=", "lz4") |
Mathieu Chartier | 3f7ddbb | 2019-04-29 09:33:50 -0700 | [diff] [blame] | 536 | if !global.DontResolveStartupStrings { |
| 537 | cmd.FlagWithArg("--resolve-startup-const-strings=", "true") |
| 538 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 539 | rule.Install(appImagePath, appImageInstallPath) |
| 540 | } |
| 541 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 542 | if profile != nil { |
| 543 | cmd.FlagWithInput("--profile-file=", profile) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | rule.Install(odexPath, odexInstallPath) |
| 547 | rule.Install(vdexPath, vdexInstallPath) |
| 548 | } |
| 549 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 550 | func shouldGenerateDM(module *ModuleConfig, global *GlobalConfig) bool { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 551 | // Generating DM files only makes sense for verify, avoid doing for non verify compiler filter APKs. |
| 552 | // No reason to use a dm file if the dex is already uncompressed. |
| 553 | return global.GenerateDMFiles && !module.UncompressedDex && |
| 554 | contains(module.PreoptFlags, "--compiler-filter=verify") |
| 555 | } |
| 556 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 557 | func OdexOnSystemOtherByName(name string, dexLocation string, global *GlobalConfig) bool { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 558 | if !global.HasSystemOther { |
| 559 | return false |
| 560 | } |
| 561 | |
| 562 | if global.SanitizeLite { |
| 563 | return false |
| 564 | } |
| 565 | |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 566 | if contains(global.SpeedApps, name) || contains(global.SystemServerApps, name) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 567 | return false |
| 568 | } |
| 569 | |
| 570 | for _, f := range global.PatternsOnSystemOther { |
Anton Hansson | da4d9d9 | 2020-09-15 09:28:55 +0000 | [diff] [blame^] | 571 | if makefileMatch(filepath.Join(SystemPartition, f), dexLocation) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 572 | return true |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | return false |
| 577 | } |
| 578 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 579 | func odexOnSystemOther(module *ModuleConfig, global *GlobalConfig) bool { |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 580 | return OdexOnSystemOtherByName(module.Name, module.DexLocation, global) |
| 581 | } |
| 582 | |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 583 | // PathToLocation converts .../system/framework/arm64/boot.art to .../system/framework/boot.art |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 584 | func PathToLocation(path android.Path, arch android.ArchType) string { |
| 585 | pathArch := filepath.Base(filepath.Dir(path.String())) |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 586 | if pathArch != arch.String() { |
| 587 | 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] | 588 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 589 | 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] | 590 | } |
| 591 | |
Ulya Trafimovich | d4bcea4 | 2020-06-03 14:57:22 +0100 | [diff] [blame] | 592 | func pathForLibrary(module *ModuleConfig, lib string) *LibraryPath { |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 593 | if path, ok := module.LibraryPaths[lib]; ok && path.Host != nil && path.Device != "error" { |
| 594 | return path |
| 595 | } else { |
| 596 | return nil |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 597 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | func makefileMatch(pattern, s string) bool { |
| 601 | percent := strings.IndexByte(pattern, '%') |
| 602 | switch percent { |
| 603 | case -1: |
| 604 | return pattern == s |
| 605 | case len(pattern) - 1: |
| 606 | return strings.HasPrefix(s, pattern[:len(pattern)-1]) |
| 607 | default: |
| 608 | panic(fmt.Errorf("unsupported makefile pattern %q", pattern)) |
| 609 | } |
| 610 | } |
| 611 | |
Ulya Trafimovich | f3ff010 | 2019-12-03 15:39:23 +0000 | [diff] [blame] | 612 | var nonUpdatableSystemServerJarsKey = android.NewOnceKey("nonUpdatableSystemServerJars") |
| 613 | |
| 614 | // TODO: eliminate the superficial global config parameter by moving global config definition |
| 615 | // from java subpackage to dexpreopt. |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 616 | func NonUpdatableSystemServerJars(ctx android.PathContext, global *GlobalConfig) []string { |
Ulya Trafimovich | f3ff010 | 2019-12-03 15:39:23 +0000 | [diff] [blame] | 617 | return ctx.Config().Once(nonUpdatableSystemServerJarsKey, func() interface{} { |
Ulya Trafimovich | 249386a | 2020-07-01 14:31:13 +0100 | [diff] [blame] | 618 | return android.RemoveListFromList(global.SystemServerJars, global.UpdatableSystemServerJars.CopyOfJars()) |
Ulya Trafimovich | f3ff010 | 2019-12-03 15:39:23 +0000 | [diff] [blame] | 619 | }).([]string) |
| 620 | } |
| 621 | |
Ulya Trafimovich | dacc6c5 | 2020-03-11 11:59:34 +0000 | [diff] [blame] | 622 | // A predefined location for the system server dex jars. This is needed in order to generate |
| 623 | // class loader context for dex2oat, as the path to the jar in the Soong module may be unknown |
| 624 | // at that time (Soong processes the jars in dependency order, which may be different from the |
| 625 | // the system server classpath order). |
| 626 | func SystemServerDexJarHostPath(ctx android.PathContext, jar string) android.OutputPath { |
Ulya Trafimovich | 6cf2c0c | 2020-04-24 12:15:20 +0100 | [diff] [blame] | 627 | if DexpreoptRunningInSoong { |
| 628 | // Soong module, just use the default output directory $OUT/soong. |
| 629 | return android.PathForOutput(ctx, "system_server_dexjars", jar+".jar") |
| 630 | } else { |
| 631 | // Make module, default output directory is $OUT (passed via the "null config" created |
| 632 | // by dexpreopt_gen). Append Soong subdirectory to match Soong module paths. |
| 633 | return android.PathForOutput(ctx, "soong", "system_server_dexjars", jar+".jar") |
| 634 | } |
Ulya Trafimovich | dacc6c5 | 2020-03-11 11:59:34 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Ulya Trafimovich | cd3203f | 2020-03-27 11:30:00 +0000 | [diff] [blame] | 637 | // Check the order of jars on the system server classpath and give a warning/error if a jar precedes |
| 638 | // one of its dependencies. This is not an error, but a missed optimization, as dexpreopt won't |
| 639 | // have the dependency jar in the class loader context, and it won't be able to resolve any |
| 640 | // references to its classes and methods. |
| 641 | func checkSystemServerOrder(ctx android.PathContext, jarIndex int) { |
| 642 | mctx, isModule := ctx.(android.ModuleContext) |
| 643 | if isModule { |
| 644 | config := GetGlobalConfig(ctx) |
| 645 | jars := NonUpdatableSystemServerJars(ctx, config) |
| 646 | mctx.WalkDeps(func(dep android.Module, parent android.Module) bool { |
| 647 | depIndex := android.IndexList(dep.Name(), jars) |
| 648 | if jarIndex < depIndex && !config.BrokenSuboptimalOrderOfSystemServerJars { |
| 649 | jar := jars[jarIndex] |
| 650 | dep := jars[depIndex] |
| 651 | mctx.ModuleErrorf("non-optimal order of jars on the system server classpath:"+ |
| 652 | " '%s' precedes its dependency '%s', so dexpreopt is unable to resolve any"+ |
| 653 | " references from '%s' to '%s'.\n", jar, dep, jar, dep) |
| 654 | } |
| 655 | return true |
| 656 | }) |
| 657 | } |
| 658 | } |
| 659 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 660 | func contains(l []string, s string) bool { |
| 661 | for _, e := range l { |
| 662 | if e == s { |
| 663 | return true |
| 664 | } |
| 665 | } |
| 666 | return false |
| 667 | } |
| 668 | |
Colin Cross | 454c087 | 2019-02-15 23:03:34 -0800 | [diff] [blame] | 669 | var copyOf = android.CopyOf |