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