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 { |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 198 | // Library names |
| 199 | Names []string |
| 200 | |
Ulya Trafimovich | 696c59d | 2020-06-01 16:10:56 +0100 | [diff] [blame] | 201 | // The class loader context using paths in the build. |
| 202 | Host android.Paths |
| 203 | |
| 204 | // The class loader context using paths as they will be on the device. |
| 205 | Target []string |
| 206 | } |
| 207 | |
| 208 | // A map of class loader contexts for each SDK version. |
| 209 | // A map entry for "any" version contains libraries that are unconditionally added to class loader |
| 210 | // context. Map entries for existing versions contains libraries that were in the default classpath |
| 211 | // until that API version, and should be added to class loader context if and only if the |
| 212 | // targetSdkVersion in the manifest or APK is less than that API version. |
| 213 | type classLoaderContextMap map[int]*classLoaderContext |
| 214 | |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 215 | const AnySdkVersion int = 9999 // should go last in class loader context |
Ulya Trafimovich | 696c59d | 2020-06-01 16:10:56 +0100 | [diff] [blame] | 216 | |
Ulya Trafimovich | 696c59d | 2020-06-01 16:10:56 +0100 | [diff] [blame] | 217 | func (m classLoaderContextMap) getValue(sdkVer int) *classLoaderContext { |
| 218 | if _, ok := m[sdkVer]; !ok { |
| 219 | m[sdkVer] = &classLoaderContext{} |
| 220 | } |
| 221 | return m[sdkVer] |
| 222 | } |
| 223 | |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 224 | func (clc *classLoaderContext) addLib(lib string, hostPath android.Path, targetPath string) { |
| 225 | clc.Names = append(clc.Names, lib) |
| 226 | clc.Host = append(clc.Host, hostPath) |
| 227 | clc.Target = append(clc.Target, targetPath) |
| 228 | } |
| 229 | |
Ulya Trafimovich | a54d33b | 2020-09-23 16:55:42 +0100 | [diff] [blame] | 230 | func (m classLoaderContextMap) addLibs(ctx android.PathContext, sdkVer int, module *ModuleConfig, libs ...string) bool { |
Ulya Trafimovich | 696c59d | 2020-06-01 16:10:56 +0100 | [diff] [blame] | 231 | clc := m.getValue(sdkVer) |
| 232 | for _, lib := range libs { |
Ulya Trafimovich | a54d33b | 2020-09-23 16:55:42 +0100 | [diff] [blame] | 233 | if p, ok := module.LibraryPaths[lib]; ok && p.Host != nil && p.Device != UnknownInstallLibraryPath { |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 234 | clc.addLib(lib, p.Host, p.Device) |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 235 | } else { |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 236 | if sdkVer == AnySdkVersion { |
Ulya Trafimovich | a54d33b | 2020-09-23 16:55:42 +0100 | [diff] [blame] | 237 | // Fail the build if dexpreopt doesn't know paths to one of the <uses-library> |
| 238 | // dependencies. In the future we may need to relax this and just disable dexpreopt. |
| 239 | android.ReportPathErrorf(ctx, "dexpreopt cannot find path for <uses-library> '%s'", lib) |
| 240 | } else { |
| 241 | // No error for compatibility libraries, as Soong doesn't know if they are needed |
| 242 | // (this depends on the targetSdkVersion in the manifest). |
| 243 | } |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 244 | return false |
| 245 | } |
Ulya Trafimovich | 696c59d | 2020-06-01 16:10:56 +0100 | [diff] [blame] | 246 | } |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 247 | return true |
Ulya Trafimovich | 696c59d | 2020-06-01 16:10:56 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 250 | func (m classLoaderContextMap) usesLibs() []string { |
| 251 | if clc, ok := m[AnySdkVersion]; ok { |
| 252 | return clc.Names |
| 253 | } |
| 254 | return nil |
| 255 | } |
| 256 | |
Ulya Trafimovich | 696c59d | 2020-06-01 16:10:56 +0100 | [diff] [blame] | 257 | func (m classLoaderContextMap) addSystemServerLibs(sdkVer int, ctx android.PathContext, module *ModuleConfig, libs ...string) { |
| 258 | clc := m.getValue(sdkVer) |
| 259 | for _, lib := range libs { |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 260 | clc.addLib(lib, SystemServerDexJarHostPath(ctx, lib), filepath.Join("/system/framework", lib+".jar")) |
Ulya Trafimovich | 696c59d | 2020-06-01 16:10:56 +0100 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 264 | // genClassLoaderContext generates host and target class loader context to be passed to the dex2oat |
| 265 | // command for the dexpreopted module. There are three possible cases: |
| 266 | // |
| 267 | // 1. System server jars. They have a special class loader context that includes other system |
| 268 | // server jars. |
| 269 | // |
| 270 | // 2. Library jars or APKs which have precise list of their <uses-library> libs. Their class loader |
| 271 | // context includes build and on-device paths to these libs. In some cases it may happen that |
| 272 | // the path to a <uses-library> is unknown (e.g. the dexpreopted module may depend on stubs |
| 273 | // library, whose implementation library is missing from the build altogether). In such case |
| 274 | // dexpreopting with the <uses-library> is impossible, and dexpreopting without it is pointless, |
| 275 | // as the runtime classpath won't match and the dexpreopted code will be discarded. Therefore in |
| 276 | // such cases the function returns nil, which disables dexpreopt. |
| 277 | // |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 278 | // 3. All other library jars or APKs for which the exact <uses-library> list is unknown. They use |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 279 | // the unsafe &-classpath workaround that means empty class loader context and absence of runtime |
| 280 | // check that the class loader context provided by the PackageManager agrees with the stored |
| 281 | // class loader context recorded in the .odex file. |
| 282 | // |
| 283 | func genClassLoaderContext(ctx android.PathContext, global *GlobalConfig, module *ModuleConfig) *classLoaderContextMap { |
| 284 | classLoaderContexts := make(classLoaderContextMap) |
| 285 | systemServerJars := NonUpdatableSystemServerJars(ctx, global) |
| 286 | |
| 287 | if jarIndex := android.IndexList(module.Name, systemServerJars); jarIndex >= 0 { |
| 288 | // System server jars should be dexpreopted together: class loader context of each jar |
| 289 | // should include all preceding jars on the system server classpath. |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 290 | classLoaderContexts.addSystemServerLibs(AnySdkVersion, ctx, module, systemServerJars[:jarIndex]...) |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 291 | |
| 292 | } else if module.EnforceUsesLibraries { |
| 293 | // Unconditional class loader context. |
| 294 | usesLibs := append(copyOf(module.UsesLibraries), module.OptionalUsesLibraries...) |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 295 | if !classLoaderContexts.addLibs(ctx, AnySdkVersion, module, usesLibs...) { |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 296 | return nil |
| 297 | } |
| 298 | |
| 299 | // Conditional class loader context for API version < 28. |
| 300 | const httpLegacy = "org.apache.http.legacy" |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 301 | if !classLoaderContexts.addLibs(ctx, 28, module, httpLegacy) { |
| 302 | return nil |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | // Conditional class loader context for API version < 29. |
| 306 | usesLibs29 := []string{ |
| 307 | "android.hidl.base-V1.0-java", |
| 308 | "android.hidl.manager-V1.0-java", |
| 309 | } |
Ulya Trafimovich | a54d33b | 2020-09-23 16:55:42 +0100 | [diff] [blame] | 310 | if !classLoaderContexts.addLibs(ctx, 29, module, usesLibs29...) { |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 311 | return nil |
| 312 | } |
| 313 | |
| 314 | // Conditional class loader context for API version < 30. |
Ulya Trafimovich | 46b3d5b | 2020-10-21 13:20:55 +0100 | [diff] [blame] | 315 | if !classLoaderContexts.addLibs(ctx, 30, module, OptionalCompatUsesLibs30...) { |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 316 | return nil |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | } else { |
| 320 | // Pass special class loader context to skip the classpath and collision check. |
| 321 | // This will get removed once LOCAL_USES_LIBRARIES is enforced. |
| 322 | // Right now LOCAL_USES_LIBRARIES is opt in, for the case where it's not specified we still default |
| 323 | // to the &. |
| 324 | } |
| 325 | |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 326 | fixConditionalClassLoaderContext(classLoaderContexts) |
| 327 | |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 328 | return &classLoaderContexts |
| 329 | } |
| 330 | |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame^] | 331 | // Find build and install paths to "android.hidl.base". The library must be present in conditional |
| 332 | // class loader context for SDK version 29, because it's one of the compatibility libraries. |
| 333 | func findHidlBasePaths(ctx android.PathContext, clcMap classLoaderContextMap) (android.Path, string) { |
| 334 | var hostPath android.Path |
| 335 | targetPath := UnknownInstallLibraryPath |
| 336 | |
| 337 | if clc, ok := clcMap[29]; ok { |
| 338 | for i, lib := range clc.Names { |
| 339 | if lib == AndroidHidlBase { |
| 340 | hostPath = clc.Host[i] |
| 341 | targetPath = clc.Target[i] |
| 342 | break |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // Fail if the library paths were not found. This may happen if the function is called at the |
| 348 | // wrong time (either before the compatibility libraries were added to context, or after they |
| 349 | // have been removed for some reason). |
| 350 | if hostPath == nil { |
| 351 | android.ReportPathErrorf(ctx, "dexpreopt cannot find build path to '%s'", AndroidHidlBase) |
| 352 | } else if targetPath == UnknownInstallLibraryPath { |
| 353 | android.ReportPathErrorf(ctx, "dexpreopt cannot find install path to '%s'", AndroidHidlBase) |
| 354 | } |
| 355 | |
| 356 | return hostPath, targetPath |
| 357 | } |
| 358 | |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 359 | // Now that the full unconditional context is known, reconstruct conditional context. |
| 360 | // Apply filters for individual libraries, mirroring what the PackageManager does when it |
| 361 | // constructs class loader context on device. |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame^] | 362 | // |
| 363 | // TODO(b/132357300): |
| 364 | // - move handling of android.hidl.manager -> android.hidl.base dependency here |
| 365 | // - remove android.hidl.manager and android.hidl.base unless the app is a system app. |
| 366 | // |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 367 | func fixConditionalClassLoaderContext(clcMap classLoaderContextMap) { |
| 368 | usesLibs := clcMap.usesLibs() |
| 369 | |
| 370 | for sdkVer, clc := range clcMap { |
| 371 | if sdkVer == AnySdkVersion { |
| 372 | continue |
| 373 | } |
| 374 | clcMap[sdkVer] = &classLoaderContext{} |
| 375 | for i, lib := range clc.Names { |
| 376 | if android.InList(lib, usesLibs) { |
| 377 | // skip compatibility libraries that are already included in unconditional context |
Ulya Trafimovich | 46b3d5b | 2020-10-21 13:20:55 +0100 | [diff] [blame] | 378 | } else if lib == AndroidTestMock && !android.InList("android.test.runner", usesLibs) { |
| 379 | // android.test.mock is only needed as a compatibility library (in conditional class |
| 380 | // loader context) if android.test.runner is used, otherwise skip it |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 381 | } else { |
| 382 | clcMap[sdkVer].addLib(lib, clc.Host[i], clc.Target[i]) |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame^] | 388 | // Return the class loader context as a string and a slice of build paths for all dependencies. |
| 389 | func computeClassLoaderContext(ctx android.PathContext, clcMap classLoaderContextMap) (clcStr string, paths android.Paths) { |
| 390 | hidlBaseHostPath, hidlBaseTargetPath := findHidlBasePaths(ctx, clcMap) |
| 391 | |
| 392 | for _, ver := range android.SortedIntKeys(clcMap) { |
| 393 | clc := clcMap.getValue(ver) |
| 394 | |
| 395 | clcLen := len(clc.Names) |
| 396 | if clcLen != len(clc.Host) || clcLen != len(clc.Target) { |
| 397 | android.ReportPathErrorf(ctx, "ill-formed class loader context") |
| 398 | } |
| 399 | |
| 400 | var hostClc, targetClc []string |
| 401 | var hostPaths android.Paths |
| 402 | |
| 403 | for i := 0; i < clcLen; i++ { |
| 404 | hostStr := "PCL[" + clc.Host[i].String() + "]" |
| 405 | targetStr := "PCL[" + clc.Target[i] + "]" |
| 406 | |
| 407 | // Add dependency of android.hidl.manager on android.hidl.base (it is not tracked as |
| 408 | // a regular dependency by the build system, so it needs special handling). |
| 409 | if clc.Names[i] == AndroidHidlManager { |
| 410 | hostStr += "{PCL[" + hidlBaseHostPath.String() + "]}" |
| 411 | targetStr += "{PCL[" + hidlBaseTargetPath + "]}" |
| 412 | hostPaths = append(hostPaths, hidlBaseHostPath) |
| 413 | } |
| 414 | |
| 415 | hostClc = append(hostClc, hostStr) |
| 416 | targetClc = append(targetClc, targetStr) |
| 417 | hostPaths = append(hostPaths, clc.Host[i]) |
| 418 | } |
| 419 | |
| 420 | if hostPaths != nil { |
| 421 | sdkVerStr := fmt.Sprintf("%d", ver) |
| 422 | if ver == AnySdkVersion { |
| 423 | sdkVerStr = "any" // a special keyword that means any SDK version |
| 424 | } |
| 425 | clcStr += fmt.Sprintf(" --host-context-for-sdk %s %s", sdkVerStr, strings.Join(hostClc, "#")) |
| 426 | clcStr += fmt.Sprintf(" --target-context-for-sdk %s %s", sdkVerStr, strings.Join(targetClc, "#")) |
| 427 | paths = append(paths, hostPaths...) |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | return clcStr, paths |
| 432 | } |
| 433 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 434 | func dexpreoptCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig, |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 435 | module *ModuleConfig, rule *android.RuleBuilder, archIdx int, classLoaderContexts classLoaderContextMap, |
| 436 | profile android.WritablePath, appImage bool, generateDM bool) { |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 437 | |
| 438 | arch := module.Archs[archIdx] |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 439 | |
| 440 | // HACK: make soname in Soong-generated .odex files match Make. |
| 441 | base := filepath.Base(module.DexLocation) |
| 442 | if filepath.Ext(base) == ".jar" { |
| 443 | base = "javalib.jar" |
| 444 | } else if filepath.Ext(base) == ".apk" { |
| 445 | base = "package.apk" |
| 446 | } |
| 447 | |
| 448 | toOdexPath := func(path string) string { |
| 449 | return filepath.Join( |
| 450 | filepath.Dir(path), |
| 451 | "oat", |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 452 | arch.String(), |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 453 | pathtools.ReplaceExtension(filepath.Base(path), "odex")) |
| 454 | } |
| 455 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 456 | odexPath := module.BuildPath.InSameDir(ctx, "oat", arch.String(), pathtools.ReplaceExtension(base, "odex")) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 457 | odexInstallPath := toOdexPath(module.DexLocation) |
| 458 | if odexOnSystemOther(module, global) { |
Anton Hansson | 43ab0bc | 2019-10-03 14:18:45 +0100 | [diff] [blame] | 459 | odexInstallPath = filepath.Join(SystemOtherPartition, odexInstallPath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 462 | vdexPath := odexPath.ReplaceExtension(ctx, "vdex") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 463 | vdexInstallPath := pathtools.ReplaceExtension(odexInstallPath, "vdex") |
| 464 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 465 | invocationPath := odexPath.ReplaceExtension(ctx, "invocation") |
Alex Light | 5de4196 | 2018-12-18 15:16:26 -0800 | [diff] [blame] | 466 | |
Ulya Trafimovich | dacc6c5 | 2020-03-11 11:59:34 +0000 | [diff] [blame] | 467 | systemServerJars := NonUpdatableSystemServerJars(ctx, global) |
| 468 | |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 469 | rule.Command().FlagWithArg("mkdir -p ", filepath.Dir(odexPath.String())) |
| 470 | rule.Command().FlagWithOutput("rm -f ", odexPath) |
Ulya Trafimovich | c9af538 | 2020-05-29 15:35:06 +0100 | [diff] [blame] | 471 | |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 472 | if jarIndex := android.IndexList(module.Name, systemServerJars); jarIndex >= 0 { |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 473 | // Copy the system server jar to a predefined location where dex2oat will find it. |
| 474 | dexPathHost := SystemServerDexJarHostPath(ctx, module.Name) |
| 475 | rule.Command().Text("mkdir -p").Flag(filepath.Dir(dexPathHost.String())) |
| 476 | rule.Command().Text("cp -f").Input(module.DexPath).Output(dexPathHost) |
| 477 | |
| 478 | checkSystemServerOrder(ctx, jarIndex) |
| 479 | |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 480 | clc := classLoaderContexts[AnySdkVersion] |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 481 | rule.Command(). |
| 482 | Text("class_loader_context_arg=--class-loader-context=PCL[" + strings.Join(clc.Host.Strings(), ":") + "]"). |
| 483 | Implicits(clc.Host). |
| 484 | Text("stored_class_loader_context_arg=--stored-class-loader-context=PCL[" + strings.Join(clc.Target, ":") + "]") |
| 485 | } else if module.EnforceUsesLibraries { |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 486 | // Generate command that saves target SDK version in a shell variable. |
Colin Cross | 38b9685 | 2019-05-22 10:21:09 -0700 | [diff] [blame] | 487 | if module.ManifestPath != nil { |
| 488 | rule.Command().Text(`target_sdk_version="$(`). |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 489 | Tool(globalSoong.ManifestCheck). |
Colin Cross | 38b9685 | 2019-05-22 10:21:09 -0700 | [diff] [blame] | 490 | Flag("--extract-target-sdk-version"). |
| 491 | Input(module.ManifestPath). |
| 492 | Text(`)"`) |
| 493 | } else { |
| 494 | // No manifest to extract targetSdkVersion from, hope that DexJar is an APK |
| 495 | rule.Command().Text(`target_sdk_version="$(`). |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 496 | Tool(globalSoong.Aapt). |
Colin Cross | 38b9685 | 2019-05-22 10:21:09 -0700 | [diff] [blame] | 497 | Flag("dump badging"). |
| 498 | Input(module.DexPath). |
| 499 | Text(`| grep "targetSdkVersion" | sed -n "s/targetSdkVersion:'\(.*\)'/\1/p"`). |
| 500 | Text(`)"`) |
| 501 | } |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 502 | |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 503 | // Generate command that saves host and target class loader context in shell variables. |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame^] | 504 | clc, paths := computeClassLoaderContext(ctx, classLoaderContexts) |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 505 | cmd := rule.Command(). |
| 506 | Text(`eval "$(`).Tool(globalSoong.ConstructContext). |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame^] | 507 | Text(` --target-sdk-version ${target_sdk_version}`). |
| 508 | Text(clc).Implicits(paths) |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 509 | cmd.Text(`)"`) |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 510 | } else { |
| 511 | // Pass special class loader context to skip the classpath and collision check. |
| 512 | // This will get removed once LOCAL_USES_LIBRARIES is enforced. |
| 513 | // Right now LOCAL_USES_LIBRARIES is opt in, for the case where it's not specified we still default |
| 514 | // to the &. |
| 515 | rule.Command(). |
| 516 | Text(`class_loader_context_arg=--class-loader-context=\&`). |
| 517 | Text(`stored_class_loader_context_arg=""`) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Nicolas Geoffray | 2464ef4 | 2019-03-05 14:07:07 +0000 | [diff] [blame] | 520 | // Devices that do not have a product partition use a symlink from /product to /system/product. |
| 521 | // Because on-device dexopt will see dex locations starting with /product, we change the paths |
| 522 | // to mimic this behavior. |
| 523 | dexLocationArg := module.DexLocation |
| 524 | if strings.HasPrefix(dexLocationArg, "/system/product/") { |
| 525 | dexLocationArg = strings.TrimPrefix(dexLocationArg, "/system") |
| 526 | } |
| 527 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 528 | cmd := rule.Command(). |
| 529 | Text(`ANDROID_LOG_TAGS="*:e"`). |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 530 | Tool(globalSoong.Dex2oat). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 531 | Flag("--avoid-storing-invocation"). |
Alex Light | 5de4196 | 2018-12-18 15:16:26 -0800 | [diff] [blame] | 532 | FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 533 | Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatXms). |
| 534 | Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatXmx). |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 535 | Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", module.PreoptBootClassPathDexFiles, ":"). |
| 536 | Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", module.PreoptBootClassPathDexLocations, ":"). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 537 | Flag("${class_loader_context_arg}"). |
Ulya Trafimovich | dacc6c5 | 2020-03-11 11:59:34 +0000 | [diff] [blame] | 538 | Flag("${stored_class_loader_context_arg}"). |
Ulya Trafimovich | 3391a1e | 2020-01-03 17:33:17 +0000 | [diff] [blame] | 539 | FlagWithArg("--boot-image=", strings.Join(module.DexPreoptImageLocations, ":")).Implicits(module.DexPreoptImagesDeps[archIdx].Paths()). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 540 | FlagWithInput("--dex-file=", module.DexPath). |
Nicolas Geoffray | 2464ef4 | 2019-03-05 14:07:07 +0000 | [diff] [blame] | 541 | FlagWithArg("--dex-location=", dexLocationArg). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 542 | FlagWithOutput("--oat-file=", odexPath).ImplicitOutput(vdexPath). |
| 543 | // Pass an empty directory, dex2oat shouldn't be reading arbitrary files |
| 544 | FlagWithArg("--android-root=", global.EmptyDirectory). |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 545 | FlagWithArg("--instruction-set=", arch.String()). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 546 | FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]). |
| 547 | FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]). |
| 548 | Flag("--no-generate-debug-info"). |
| 549 | Flag("--generate-build-id"). |
| 550 | Flag("--abort-on-hard-verifier-error"). |
| 551 | Flag("--force-determinism"). |
| 552 | FlagWithArg("--no-inline-from=", "core-oj.jar") |
| 553 | |
| 554 | var preoptFlags []string |
| 555 | if len(module.PreoptFlags) > 0 { |
| 556 | preoptFlags = module.PreoptFlags |
| 557 | } else if len(global.PreoptFlags) > 0 { |
| 558 | preoptFlags = global.PreoptFlags |
| 559 | } |
| 560 | |
| 561 | if len(preoptFlags) > 0 { |
| 562 | cmd.Text(strings.Join(preoptFlags, " ")) |
| 563 | } |
| 564 | |
| 565 | if module.UncompressedDex { |
| 566 | cmd.FlagWithArg("--copy-dex-files=", "false") |
| 567 | } |
| 568 | |
Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 569 | if !android.PrefixInList(preoptFlags, "--compiler-filter=") { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 570 | var compilerFilter string |
| 571 | if contains(global.SystemServerJars, module.Name) { |
| 572 | // Jars of system server, use the product option if it is set, speed otherwise. |
| 573 | if global.SystemServerCompilerFilter != "" { |
| 574 | compilerFilter = global.SystemServerCompilerFilter |
| 575 | } else { |
| 576 | compilerFilter = "speed" |
| 577 | } |
| 578 | } else if contains(global.SpeedApps, module.Name) || contains(global.SystemServerApps, module.Name) { |
| 579 | // Apps loaded into system server, and apps the product default to being compiled with the |
| 580 | // 'speed' compiler filter. |
| 581 | compilerFilter = "speed" |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 582 | } else if profile != nil { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 583 | // For non system server jars, use speed-profile when we have a profile. |
| 584 | compilerFilter = "speed-profile" |
| 585 | } else if global.DefaultCompilerFilter != "" { |
| 586 | compilerFilter = global.DefaultCompilerFilter |
| 587 | } else { |
| 588 | compilerFilter = "quicken" |
| 589 | } |
| 590 | cmd.FlagWithArg("--compiler-filter=", compilerFilter) |
| 591 | } |
| 592 | |
| 593 | if generateDM { |
| 594 | cmd.FlagWithArg("--copy-dex-files=", "false") |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 595 | dmPath := module.BuildPath.InSameDir(ctx, "generated.dm") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 596 | dmInstalledPath := pathtools.ReplaceExtension(module.DexLocation, "dm") |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 597 | tmpPath := module.BuildPath.InSameDir(ctx, "primary.vdex") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 598 | rule.Command().Text("cp -f").Input(vdexPath).Output(tmpPath) |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 599 | rule.Command().Tool(globalSoong.SoongZip). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 600 | FlagWithArg("-L", "9"). |
| 601 | FlagWithOutput("-o", dmPath). |
| 602 | Flag("-j"). |
| 603 | Input(tmpPath) |
| 604 | rule.Install(dmPath, dmInstalledPath) |
| 605 | } |
| 606 | |
| 607 | // By default, emit debug info. |
| 608 | debugInfo := true |
| 609 | if global.NoDebugInfo { |
| 610 | // If the global setting suppresses mini-debug-info, disable it. |
| 611 | debugInfo = false |
| 612 | } |
| 613 | |
| 614 | // PRODUCT_SYSTEM_SERVER_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO. |
| 615 | // PRODUCT_OTHER_JAVA_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO. |
| 616 | if contains(global.SystemServerJars, module.Name) { |
| 617 | if global.AlwaysSystemServerDebugInfo { |
| 618 | debugInfo = true |
| 619 | } else if global.NeverSystemServerDebugInfo { |
| 620 | debugInfo = false |
| 621 | } |
| 622 | } else { |
| 623 | if global.AlwaysOtherDebugInfo { |
| 624 | debugInfo = true |
| 625 | } else if global.NeverOtherDebugInfo { |
| 626 | debugInfo = false |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | // Never enable on eng. |
| 631 | if global.IsEng { |
| 632 | debugInfo = false |
| 633 | } |
| 634 | |
| 635 | if debugInfo { |
| 636 | cmd.Flag("--generate-mini-debug-info") |
| 637 | } else { |
| 638 | cmd.Flag("--no-generate-mini-debug-info") |
| 639 | } |
| 640 | |
| 641 | // Set the compiler reason to 'prebuilt' to identify the oat files produced |
| 642 | // during the build, as opposed to compiled on the device. |
| 643 | cmd.FlagWithArg("--compilation-reason=", "prebuilt") |
| 644 | |
| 645 | if appImage { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 646 | appImagePath := odexPath.ReplaceExtension(ctx, "art") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 647 | appImageInstallPath := pathtools.ReplaceExtension(odexInstallPath, "art") |
| 648 | cmd.FlagWithOutput("--app-image-file=", appImagePath). |
| 649 | FlagWithArg("--image-format=", "lz4") |
Mathieu Chartier | 3f7ddbb | 2019-04-29 09:33:50 -0700 | [diff] [blame] | 650 | if !global.DontResolveStartupStrings { |
| 651 | cmd.FlagWithArg("--resolve-startup-const-strings=", "true") |
| 652 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 653 | rule.Install(appImagePath, appImageInstallPath) |
| 654 | } |
| 655 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 656 | if profile != nil { |
| 657 | cmd.FlagWithInput("--profile-file=", profile) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | rule.Install(odexPath, odexInstallPath) |
| 661 | rule.Install(vdexPath, vdexInstallPath) |
| 662 | } |
| 663 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 664 | func shouldGenerateDM(module *ModuleConfig, global *GlobalConfig) bool { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 665 | // Generating DM files only makes sense for verify, avoid doing for non verify compiler filter APKs. |
| 666 | // No reason to use a dm file if the dex is already uncompressed. |
| 667 | return global.GenerateDMFiles && !module.UncompressedDex && |
| 668 | contains(module.PreoptFlags, "--compiler-filter=verify") |
| 669 | } |
| 670 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 671 | func OdexOnSystemOtherByName(name string, dexLocation string, global *GlobalConfig) bool { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 672 | if !global.HasSystemOther { |
| 673 | return false |
| 674 | } |
| 675 | |
| 676 | if global.SanitizeLite { |
| 677 | return false |
| 678 | } |
| 679 | |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 680 | if contains(global.SpeedApps, name) || contains(global.SystemServerApps, name) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 681 | return false |
| 682 | } |
| 683 | |
| 684 | for _, f := range global.PatternsOnSystemOther { |
Anton Hansson | da4d9d9 | 2020-09-15 09:28:55 +0000 | [diff] [blame] | 685 | if makefileMatch(filepath.Join(SystemPartition, f), dexLocation) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 686 | return true |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | return false |
| 691 | } |
| 692 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 693 | func odexOnSystemOther(module *ModuleConfig, global *GlobalConfig) bool { |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 694 | return OdexOnSystemOtherByName(module.Name, module.DexLocation, global) |
| 695 | } |
| 696 | |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 697 | // PathToLocation converts .../system/framework/arm64/boot.art to .../system/framework/boot.art |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 698 | func PathToLocation(path android.Path, arch android.ArchType) string { |
| 699 | pathArch := filepath.Base(filepath.Dir(path.String())) |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 700 | if pathArch != arch.String() { |
| 701 | 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] | 702 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 703 | 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] | 704 | } |
| 705 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 706 | func makefileMatch(pattern, s string) bool { |
| 707 | percent := strings.IndexByte(pattern, '%') |
| 708 | switch percent { |
| 709 | case -1: |
| 710 | return pattern == s |
| 711 | case len(pattern) - 1: |
| 712 | return strings.HasPrefix(s, pattern[:len(pattern)-1]) |
| 713 | default: |
| 714 | panic(fmt.Errorf("unsupported makefile pattern %q", pattern)) |
| 715 | } |
| 716 | } |
| 717 | |
Ulya Trafimovich | f3ff010 | 2019-12-03 15:39:23 +0000 | [diff] [blame] | 718 | var nonUpdatableSystemServerJarsKey = android.NewOnceKey("nonUpdatableSystemServerJars") |
| 719 | |
| 720 | // TODO: eliminate the superficial global config parameter by moving global config definition |
| 721 | // from java subpackage to dexpreopt. |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 722 | func NonUpdatableSystemServerJars(ctx android.PathContext, global *GlobalConfig) []string { |
Ulya Trafimovich | f3ff010 | 2019-12-03 15:39:23 +0000 | [diff] [blame] | 723 | return ctx.Config().Once(nonUpdatableSystemServerJarsKey, func() interface{} { |
Ulya Trafimovich | 249386a | 2020-07-01 14:31:13 +0100 | [diff] [blame] | 724 | return android.RemoveListFromList(global.SystemServerJars, global.UpdatableSystemServerJars.CopyOfJars()) |
Ulya Trafimovich | f3ff010 | 2019-12-03 15:39:23 +0000 | [diff] [blame] | 725 | }).([]string) |
| 726 | } |
| 727 | |
Ulya Trafimovich | dacc6c5 | 2020-03-11 11:59:34 +0000 | [diff] [blame] | 728 | // A predefined location for the system server dex jars. This is needed in order to generate |
| 729 | // class loader context for dex2oat, as the path to the jar in the Soong module may be unknown |
| 730 | // at that time (Soong processes the jars in dependency order, which may be different from the |
| 731 | // the system server classpath order). |
| 732 | func SystemServerDexJarHostPath(ctx android.PathContext, jar string) android.OutputPath { |
Ulya Trafimovich | 6cf2c0c | 2020-04-24 12:15:20 +0100 | [diff] [blame] | 733 | if DexpreoptRunningInSoong { |
| 734 | // Soong module, just use the default output directory $OUT/soong. |
| 735 | return android.PathForOutput(ctx, "system_server_dexjars", jar+".jar") |
| 736 | } else { |
| 737 | // Make module, default output directory is $OUT (passed via the "null config" created |
| 738 | // by dexpreopt_gen). Append Soong subdirectory to match Soong module paths. |
| 739 | return android.PathForOutput(ctx, "soong", "system_server_dexjars", jar+".jar") |
| 740 | } |
Ulya Trafimovich | dacc6c5 | 2020-03-11 11:59:34 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Ulya Trafimovich | cd3203f | 2020-03-27 11:30:00 +0000 | [diff] [blame] | 743 | // Check the order of jars on the system server classpath and give a warning/error if a jar precedes |
| 744 | // one of its dependencies. This is not an error, but a missed optimization, as dexpreopt won't |
| 745 | // have the dependency jar in the class loader context, and it won't be able to resolve any |
| 746 | // references to its classes and methods. |
| 747 | func checkSystemServerOrder(ctx android.PathContext, jarIndex int) { |
| 748 | mctx, isModule := ctx.(android.ModuleContext) |
| 749 | if isModule { |
| 750 | config := GetGlobalConfig(ctx) |
| 751 | jars := NonUpdatableSystemServerJars(ctx, config) |
| 752 | mctx.WalkDeps(func(dep android.Module, parent android.Module) bool { |
| 753 | depIndex := android.IndexList(dep.Name(), jars) |
| 754 | if jarIndex < depIndex && !config.BrokenSuboptimalOrderOfSystemServerJars { |
| 755 | jar := jars[jarIndex] |
| 756 | dep := jars[depIndex] |
| 757 | mctx.ModuleErrorf("non-optimal order of jars on the system server classpath:"+ |
| 758 | " '%s' precedes its dependency '%s', so dexpreopt is unable to resolve any"+ |
| 759 | " references from '%s' to '%s'.\n", jar, dep, jar, dep) |
| 760 | } |
| 761 | return true |
| 762 | }) |
| 763 | } |
| 764 | } |
| 765 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 766 | func contains(l []string, s string) bool { |
| 767 | for _, e := range l { |
| 768 | if e == s { |
| 769 | return true |
| 770 | } |
| 771 | } |
| 772 | return false |
| 773 | } |
| 774 | |
Colin Cross | 454c087 | 2019-02-15 23:03:34 -0800 | [diff] [blame] | 775 | var copyOf = android.CopyOf |