Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1 | // Copyright 2018 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // The dexpreopt package converts a global dexpreopt config and a module dexpreopt config into rules to perform |
Nicolas Geoffray | c1bf724 | 2019-10-18 14:51:38 +0100 | [diff] [blame] | 16 | // dexpreopting. |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 17 | // |
| 18 | // It is used in two places; in the dexpeopt_gen binary for modules defined in Make, and directly linked into Soong. |
| 19 | // |
| 20 | // For Make modules it is built into the dexpreopt_gen binary, which is executed as a Make rule using global config and |
| 21 | // module config specified in JSON files. The binary writes out two shell scripts, only updating them if they have |
| 22 | // changed. One script takes an APK or JAR as an input and produces a zip file containing any outputs of preopting, |
| 23 | // in the location they should be on the device. The Make build rules will unzip the zip file into $(PRODUCT_OUT) when |
| 24 | // installing the APK, which will install the preopt outputs into $(PRODUCT_OUT)/system or $(PRODUCT_OUT)/system_other |
Nicolas Geoffray | c1bf724 | 2019-10-18 14:51:38 +0100 | [diff] [blame] | 25 | // as necessary. The zip file may be empty if preopting was disabled for any reason. |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 26 | // |
| 27 | // The intermediate shell scripts allow changes to this package or to the global config to regenerate the shell scripts |
| 28 | // but only require re-executing preopting if the script has changed. |
| 29 | // |
| 30 | // For Soong modules this package is linked directly into Soong and run from the java package. It generates the same |
| 31 | // commands as for make, using athe same global config JSON file used by make, but using a module config structure |
| 32 | // provided by Soong. The generated commands are then converted into Soong rule and written directly to the ninja file, |
| 33 | // with no extra shell scripts involved. |
| 34 | package dexpreopt |
| 35 | |
| 36 | import ( |
| 37 | "fmt" |
| 38 | "path/filepath" |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 39 | "runtime" |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 40 | "strings" |
| 41 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 42 | "android/soong/android" |
| 43 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 44 | "github.com/google/blueprint/pathtools" |
| 45 | ) |
| 46 | |
| 47 | const SystemPartition = "/system/" |
| 48 | const SystemOtherPartition = "/system_other/" |
| 49 | |
Ulya Trafimovich | 6cf2c0c | 2020-04-24 12:15:20 +0100 | [diff] [blame] | 50 | var DexpreoptRunningInSoong = false |
| 51 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 52 | // GenerateDexpreoptRule generates a set of commands that will preopt a module based on a GlobalConfig and a |
| 53 | // ModuleConfig. The produced files and their install locations will be available through rule.Installs(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 54 | func GenerateDexpreoptRule(ctx android.BuilderContext, globalSoong *GlobalSoongConfig, |
Jiakai Zhang | a449678 | 2023-05-17 16:57:30 +0100 | [diff] [blame] | 55 | global *GlobalConfig, module *ModuleConfig, productPackages android.Path) ( |
| 56 | 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 | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 71 | rule = android.NewRuleBuilder(pctx, ctx) |
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) { |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 85 | if valid, err := validateClassLoaderContext(module.ClassLoaderContexts); err != nil { |
Ulya Trafimovich | 6961267 | 2020-10-20 17:41:54 +0100 | [diff] [blame] | 86 | android.ReportPathErrorf(ctx, err.Error()) |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 87 | } else if valid { |
| 88 | fixClassLoaderContext(module.ClassLoaderContexts) |
| 89 | |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 90 | appImage := (generateProfile || module.ForceCreateAppImage || global.DefaultAppImages) && |
| 91 | !module.NoCreateAppImage |
| 92 | |
| 93 | generateDM := shouldGenerateDM(module, global) |
| 94 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 95 | for archIdx, _ := range module.Archs { |
Jiakai Zhang | a449678 | 2023-05-17 16:57:30 +0100 | [diff] [blame] | 96 | dexpreoptCommand(ctx, globalSoong, global, module, rule, archIdx, profile, appImage, |
| 97 | generateDM, productPackages) |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return rule, nil |
| 103 | } |
| 104 | |
Jiakai Zhang | cf61e3c | 2023-05-08 16:28:38 +0000 | [diff] [blame] | 105 | // If dexpreopt is applicable to the module, returns whether dexpreopt is disabled. Otherwise, the |
| 106 | // behavior is undefined. |
| 107 | // When it returns true, dexpreopt artifacts will not be generated, but profile will still be |
| 108 | // generated if profile-guided compilation is requested. |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 109 | func dexpreoptDisabled(ctx android.PathContext, global *GlobalConfig, module *ModuleConfig) bool { |
Jiakai Zhang | 3317ce7 | 2023-02-08 01:19:19 +0800 | [diff] [blame] | 110 | if ctx.Config().UnbundledBuild() { |
| 111 | return true |
| 112 | } |
| 113 | |
Jiakai Zhang | cf61e3c | 2023-05-08 16:28:38 +0000 | [diff] [blame] | 114 | if global.DisablePreopt { |
| 115 | return true |
| 116 | } |
| 117 | |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 118 | if contains(global.DisablePreoptModules, module.Name) { |
| 119 | return true |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 122 | // Don't preopt individual boot jars, they will be preopted together. |
| 123 | if global.BootJars.ContainsJar(module.Name) { |
| 124 | return true |
| 125 | } |
| 126 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 127 | // If OnlyPreoptBootImageAndSystemServer=true and module is not in boot class path skip |
| 128 | // Also preopt system server jars since selinux prevents system server from loading anything from |
| 129 | // /data. If we don't do this they will need to be extracted which is not favorable for RAM usage |
| 130 | // 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] | 131 | if global.OnlyPreoptBootImageAndSystemServer && !global.BootJars.ContainsJar(module.Name) && |
Jiakai Zhang | 389a647 | 2021-12-14 18:54:06 +0000 | [diff] [blame] | 132 | !global.AllSystemServerJars(ctx).ContainsJar(module.Name) && !module.PreoptExtractedApk { |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 133 | return true |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Colin Cross | cbed657 | 2019-01-08 17:38:37 -0800 | [diff] [blame] | 136 | return false |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 139 | func profileCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig, |
| 140 | module *ModuleConfig, rule *android.RuleBuilder) android.WritablePath { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 141 | |
| 142 | profilePath := module.BuildPath.InSameDir(ctx, "profile.prof") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 143 | profileInstalledPath := module.DexLocation + ".prof" |
| 144 | |
| 145 | if !module.ProfileIsTextListing { |
Vladimir Marko | 982e384 | 2021-04-29 09:05:18 +0100 | [diff] [blame] | 146 | rule.Command().Text("rm -f").Output(profilePath) |
| 147 | rule.Command().Text("touch").Output(profilePath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | cmd := rule.Command(). |
| 151 | Text(`ANDROID_LOG_TAGS="*:e"`). |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 152 | Tool(globalSoong.Profman) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 153 | |
| 154 | if module.ProfileIsTextListing { |
| 155 | // The profile is a test listing of classes (used for framework jars). |
| 156 | // 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] | 157 | cmd.FlagWithInput("--create-profile-from=", module.ProfileClassListing.Path()) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 158 | } else { |
| 159 | // The profile is binary profile (used for apps). Run it through profman to |
| 160 | // ensure the profile keys match the apk. |
| 161 | cmd. |
| 162 | Flag("--copy-and-update-profile-key"). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 163 | FlagWithInput("--profile-file=", module.ProfileClassListing.Path()) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | cmd. |
Vladimir Marko | 230bd42 | 2021-04-23 15:18:33 +0100 | [diff] [blame] | 167 | Flag("--output-profile-type=app"). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 168 | FlagWithInput("--apk=", module.DexPath). |
| 169 | Flag("--dex-location="+module.DexLocation). |
| 170 | FlagWithOutput("--reference-profile-file=", profilePath) |
| 171 | |
| 172 | if !module.ProfileIsTextListing { |
| 173 | cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath)) |
| 174 | } |
| 175 | rule.Install(profilePath, profileInstalledPath) |
| 176 | |
| 177 | return profilePath |
| 178 | } |
| 179 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 180 | func bootProfileCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig, |
| 181 | module *ModuleConfig, rule *android.RuleBuilder) android.WritablePath { |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 182 | |
| 183 | profilePath := module.BuildPath.InSameDir(ctx, "profile.bprof") |
| 184 | profileInstalledPath := module.DexLocation + ".bprof" |
| 185 | |
| 186 | if !module.ProfileIsTextListing { |
Vladimir Marko | 982e384 | 2021-04-29 09:05:18 +0100 | [diff] [blame] | 187 | rule.Command().Text("rm -f").Output(profilePath) |
| 188 | rule.Command().Text("touch").Output(profilePath) |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | cmd := rule.Command(). |
| 192 | Text(`ANDROID_LOG_TAGS="*:e"`). |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 193 | Tool(globalSoong.Profman) |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 194 | |
| 195 | // The profile is a test listing of methods. |
| 196 | // We need to generate the actual binary profile. |
| 197 | cmd.FlagWithInput("--create-profile-from=", module.ProfileBootListing.Path()) |
| 198 | |
| 199 | cmd. |
Vladimir Marko | 230bd42 | 2021-04-23 15:18:33 +0100 | [diff] [blame] | 200 | Flag("--output-profile-type=bprof"). |
Nicolas Geoffray | e710242 | 2019-07-24 13:19:29 +0100 | [diff] [blame] | 201 | FlagWithInput("--apk=", module.DexPath). |
| 202 | Flag("--dex-location="+module.DexLocation). |
| 203 | FlagWithOutput("--reference-profile-file=", profilePath) |
| 204 | |
| 205 | if !module.ProfileIsTextListing { |
| 206 | cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath)) |
| 207 | } |
| 208 | rule.Install(profilePath, profileInstalledPath) |
| 209 | |
| 210 | return profilePath |
| 211 | } |
| 212 | |
Jiakai Zhang | 519c5c8 | 2021-09-16 06:15:39 +0000 | [diff] [blame] | 213 | // Returns the dex location of a system server java library. |
Jiakai Zhang | 389a647 | 2021-12-14 18:54:06 +0000 | [diff] [blame] | 214 | func GetSystemServerDexLocation(ctx android.PathContext, global *GlobalConfig, lib string) string { |
| 215 | if apex := global.AllApexSystemServerJars(ctx).ApexOfJar(lib); apex != "" { |
Jiakai Zhang | 519c5c8 | 2021-09-16 06:15:39 +0000 | [diff] [blame] | 216 | return fmt.Sprintf("/apex/%s/javalib/%s.jar", apex, lib) |
| 217 | } |
liulvping | 76d56ee | 2022-05-07 14:40:13 +0800 | [diff] [blame] | 218 | |
| 219 | if apex := global.AllPlatformSystemServerJars(ctx).ApexOfJar(lib); apex == "system_ext" { |
| 220 | return fmt.Sprintf("/system_ext/framework/%s.jar", lib) |
| 221 | } |
| 222 | |
Jiakai Zhang | 519c5c8 | 2021-09-16 06:15:39 +0000 | [diff] [blame] | 223 | return fmt.Sprintf("/system/framework/%s.jar", lib) |
| 224 | } |
| 225 | |
Jiakai Zhang | 0a0a2fb | 2021-09-30 09:38:19 +0000 | [diff] [blame] | 226 | // Returns the location to the odex file for the dex file at `path`. |
| 227 | func ToOdexPath(path string, arch android.ArchType) string { |
| 228 | if strings.HasPrefix(path, "/apex/") { |
| 229 | return filepath.Join("/system/framework/oat", arch.String(), |
| 230 | strings.ReplaceAll(path[1:], "/", "@")+"@classes.odex") |
| 231 | } |
| 232 | |
| 233 | return filepath.Join(filepath.Dir(path), "oat", arch.String(), |
| 234 | pathtools.ReplaceExtension(filepath.Base(path), "odex")) |
| 235 | } |
| 236 | |
Jiakai Zhang | a449678 | 2023-05-17 16:57:30 +0100 | [diff] [blame] | 237 | func dexpreoptCommand(ctx android.BuilderContext, globalSoong *GlobalSoongConfig, |
| 238 | global *GlobalConfig, module *ModuleConfig, rule *android.RuleBuilder, archIdx int, |
| 239 | profile android.WritablePath, appImage bool, generateDM bool, productPackages android.Path) { |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 240 | |
| 241 | arch := module.Archs[archIdx] |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 242 | |
| 243 | // HACK: make soname in Soong-generated .odex files match Make. |
| 244 | base := filepath.Base(module.DexLocation) |
| 245 | if filepath.Ext(base) == ".jar" { |
| 246 | base = "javalib.jar" |
| 247 | } else if filepath.Ext(base) == ".apk" { |
| 248 | base = "package.apk" |
| 249 | } |
| 250 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 251 | odexPath := module.BuildPath.InSameDir(ctx, "oat", arch.String(), pathtools.ReplaceExtension(base, "odex")) |
Jiakai Zhang | 0a0a2fb | 2021-09-30 09:38:19 +0000 | [diff] [blame] | 252 | odexInstallPath := ToOdexPath(module.DexLocation, arch) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 253 | if odexOnSystemOther(module, global) { |
Anton Hansson | 43ab0bc | 2019-10-03 14:18:45 +0100 | [diff] [blame] | 254 | odexInstallPath = filepath.Join(SystemOtherPartition, odexInstallPath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 257 | vdexPath := odexPath.ReplaceExtension(ctx, "vdex") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 258 | vdexInstallPath := pathtools.ReplaceExtension(odexInstallPath, "vdex") |
| 259 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 260 | invocationPath := odexPath.ReplaceExtension(ctx, "invocation") |
Alex Light | 5de4196 | 2018-12-18 15:16:26 -0800 | [diff] [blame] | 261 | |
Jiakai Zhang | 389a647 | 2021-12-14 18:54:06 +0000 | [diff] [blame] | 262 | systemServerJars := global.AllSystemServerJars(ctx) |
| 263 | systemServerClasspathJars := global.AllSystemServerClasspathJars(ctx) |
Ulya Trafimovich | dacc6c5 | 2020-03-11 11:59:34 +0000 | [diff] [blame] | 264 | |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 265 | rule.Command().FlagWithArg("mkdir -p ", filepath.Dir(odexPath.String())) |
| 266 | rule.Command().FlagWithOutput("rm -f ", odexPath) |
Ulya Trafimovich | c9af538 | 2020-05-29 15:35:06 +0100 | [diff] [blame] | 267 | |
Jiakai Zhang | 519c5c8 | 2021-09-16 06:15:39 +0000 | [diff] [blame] | 268 | if jarIndex := systemServerJars.IndexOfJar(module.Name); jarIndex >= 0 { |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 269 | // System server jars should be dexpreopted together: class loader context of each jar |
| 270 | // should include all preceding jars on the system server classpath. |
| 271 | |
| 272 | var clcHost android.Paths |
| 273 | var clcTarget []string |
Jiakai Zhang | 389a647 | 2021-12-14 18:54:06 +0000 | [diff] [blame] | 274 | endIndex := systemServerClasspathJars.IndexOfJar(module.Name) |
| 275 | if endIndex < 0 { |
| 276 | // The jar is a standalone one. Use the full classpath as the class loader context. |
| 277 | endIndex = systemServerClasspathJars.Len() |
| 278 | } |
| 279 | for i := 0; i < endIndex; i++ { |
| 280 | lib := systemServerClasspathJars.Jar(i) |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 281 | clcHost = append(clcHost, SystemServerDexJarHostPath(ctx, lib)) |
Jiakai Zhang | 389a647 | 2021-12-14 18:54:06 +0000 | [diff] [blame] | 282 | clcTarget = append(clcTarget, GetSystemServerDexLocation(ctx, global, lib)) |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Ulya Trafimovich | 29e95d9 | 2021-09-21 12:59:39 +0100 | [diff] [blame] | 285 | if DexpreoptRunningInSoong { |
| 286 | // Copy the system server jar to a predefined location where dex2oat will find it. |
| 287 | dexPathHost := SystemServerDexJarHostPath(ctx, module.Name) |
| 288 | rule.Command().Text("mkdir -p").Flag(filepath.Dir(dexPathHost.String())) |
| 289 | rule.Command().Text("cp -f").Input(module.DexPath).Output(dexPathHost) |
| 290 | } else { |
| 291 | // For Make modules the copy rule is generated in the makefiles, not in dexpreopt.sh. |
| 292 | // This is necessary to expose the rule to Ninja, otherwise it has rules that depend on |
| 293 | // the jar (namely, dexpreopt commands for all subsequent system server jars that have |
| 294 | // this one in their class loader context), but no rule that creates it (because Ninja |
| 295 | // cannot see the rule in the generated dexpreopt.sh script). |
| 296 | } |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 297 | |
Jiakai Zhang | 389a647 | 2021-12-14 18:54:06 +0000 | [diff] [blame] | 298 | clcHostString := "PCL[" + strings.Join(clcHost.Strings(), ":") + "]" |
| 299 | clcTargetString := "PCL[" + strings.Join(clcTarget, ":") + "]" |
| 300 | |
| 301 | if systemServerClasspathJars.ContainsJar(module.Name) { |
| 302 | checkSystemServerOrder(ctx, jarIndex) |
| 303 | } else { |
| 304 | // Standalone jars are loaded by separate class loaders with SYSTEMSERVERCLASSPATH as the |
| 305 | // parent. |
| 306 | clcHostString = "PCL[];" + clcHostString |
| 307 | clcTargetString = "PCL[];" + clcTargetString |
| 308 | } |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 309 | |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 310 | rule.Command(). |
Jiakai Zhang | 389a647 | 2021-12-14 18:54:06 +0000 | [diff] [blame] | 311 | Text(`class_loader_context_arg=--class-loader-context="` + clcHostString + `"`). |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 312 | Implicits(clcHost). |
Jiakai Zhang | 389a647 | 2021-12-14 18:54:06 +0000 | [diff] [blame] | 313 | Text(`stored_class_loader_context_arg=--stored-class-loader-context="` + clcTargetString + `"`) |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 314 | |
Ulya Trafimovich | bd6b076 | 2021-03-12 12:12:12 +0000 | [diff] [blame] | 315 | } else { |
| 316 | // There are three categories of Java modules handled here: |
| 317 | // |
| 318 | // - Modules that have passed verify_uses_libraries check. They are AOT-compiled and |
| 319 | // expected to be loaded on device without CLC mismatch errors. |
| 320 | // |
| 321 | // - Modules that have failed the check in relaxed mode, so it didn't cause a build error. |
| 322 | // They are dexpreopted with "verify" filter and not AOT-compiled. |
| 323 | // TODO(b/132357300): ensure that CLC mismatch errors are ignored with "verify" filter. |
| 324 | // |
| 325 | // - Modules that didn't run the check. They are AOT-compiled, but it's unknown if they |
| 326 | // will have CLC mismatch errors on device (the check is disabled by default). |
| 327 | // |
| 328 | // TODO(b/132357300): enable the check by default and eliminate the last category, so that |
| 329 | // no time/space is wasted on AOT-compiling modules that will fail CLC check on device. |
| 330 | |
| 331 | var manifestOrApk android.Path |
Jeongik Cha | 33a3a81 | 2021-04-15 09:12:49 +0900 | [diff] [blame] | 332 | if module.ManifestPath.Valid() { |
Ulya Trafimovich | bd6b076 | 2021-03-12 12:12:12 +0000 | [diff] [blame] | 333 | // Ok, there is an XML manifest. |
Jeongik Cha | 33a3a81 | 2021-04-15 09:12:49 +0900 | [diff] [blame] | 334 | manifestOrApk = module.ManifestPath.Path() |
Ulya Trafimovich | bd6b076 | 2021-03-12 12:12:12 +0000 | [diff] [blame] | 335 | } else if filepath.Ext(base) == ".apk" { |
| 336 | // Ok, there is is an APK with the manifest inside. |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 337 | manifestOrApk = module.DexPath |
Colin Cross | 38b9685 | 2019-05-22 10:21:09 -0700 | [diff] [blame] | 338 | } |
Ulya Trafimovich | bd6b076 | 2021-03-12 12:12:12 +0000 | [diff] [blame] | 339 | |
| 340 | // Generate command that saves target SDK version in a shell variable. |
| 341 | if manifestOrApk == nil { |
| 342 | // There is neither an XML manifest nor APK => nowhere to extract targetSdkVersion from. |
| 343 | // Set the latest ("any") version: then construct_context will not add any compatibility |
| 344 | // libraries (if this is incorrect, there will be a CLC mismatch and dexopt on device). |
| 345 | rule.Command().Textf(`target_sdk_version=%d`, AnySdkVersion) |
| 346 | } else { |
| 347 | rule.Command().Text(`target_sdk_version="$(`). |
| 348 | Tool(globalSoong.ManifestCheck). |
| 349 | Flag("--extract-target-sdk-version"). |
| 350 | Input(manifestOrApk). |
Ulya Trafimovich | dd62295 | 2021-03-25 12:52:49 +0000 | [diff] [blame] | 351 | FlagWithInput("--aapt ", globalSoong.Aapt). |
Ulya Trafimovich | bd6b076 | 2021-03-12 12:12:12 +0000 | [diff] [blame] | 352 | Text(`)"`) |
| 353 | } |
Ulya Trafimovich | c4dac26 | 2020-06-30 11:25:49 +0100 | [diff] [blame] | 354 | |
Ulya Trafimovich | 5f364b6 | 2020-06-30 12:39:01 +0100 | [diff] [blame] | 355 | // Generate command that saves host and target class loader context in shell variables. |
Jiakai Zhang | 51b2a8b | 2023-06-26 16:47:38 +0100 | [diff] [blame] | 356 | _, paths := ComputeClassLoaderContextDependencies(module.ClassLoaderContexts) |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 357 | rule.Command(). |
Ulya Trafimovich | bd6b076 | 2021-03-12 12:12:12 +0000 | [diff] [blame] | 358 | Text(`eval "$(`).Tool(globalSoong.ConstructContext). |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 359 | Text(` --target-sdk-version ${target_sdk_version}`). |
Jiakai Zhang | a449678 | 2023-05-17 16:57:30 +0100 | [diff] [blame] | 360 | FlagWithArg("--context-json=", module.ClassLoaderContexts.DumpForFlag()). |
| 361 | FlagWithInput("--product-packages=", productPackages). |
| 362 | Implicits(paths). |
Ulya Trafimovich | bd6b076 | 2021-03-12 12:12:12 +0000 | [diff] [blame] | 363 | Text(`)"`) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Nicolas Geoffray | 2464ef4 | 2019-03-05 14:07:07 +0000 | [diff] [blame] | 366 | // Devices that do not have a product partition use a symlink from /product to /system/product. |
| 367 | // Because on-device dexopt will see dex locations starting with /product, we change the paths |
| 368 | // to mimic this behavior. |
| 369 | dexLocationArg := module.DexLocation |
| 370 | if strings.HasPrefix(dexLocationArg, "/system/product/") { |
| 371 | dexLocationArg = strings.TrimPrefix(dexLocationArg, "/system") |
| 372 | } |
| 373 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 374 | cmd := rule.Command(). |
| 375 | Text(`ANDROID_LOG_TAGS="*:e"`). |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 376 | Tool(globalSoong.Dex2oat). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 377 | Flag("--avoid-storing-invocation"). |
Alex Light | 5de4196 | 2018-12-18 15:16:26 -0800 | [diff] [blame] | 378 | FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 379 | Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatXms). |
| 380 | Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatXmx). |
Colin Cross | 800fe13 | 2019-02-11 14:21:24 -0800 | [diff] [blame] | 381 | Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", module.PreoptBootClassPathDexFiles, ":"). |
| 382 | Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", module.PreoptBootClassPathDexLocations, ":"). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 383 | Flag("${class_loader_context_arg}"). |
Ulya Trafimovich | dacc6c5 | 2020-03-11 11:59:34 +0000 | [diff] [blame] | 384 | Flag("${stored_class_loader_context_arg}"). |
Jeongik Cha | a596909 | 2021-05-07 18:53:21 +0900 | [diff] [blame] | 385 | FlagWithArg("--boot-image=", strings.Join(module.DexPreoptImageLocationsOnHost, ":")).Implicits(module.DexPreoptImagesDeps[archIdx].Paths()). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 386 | FlagWithInput("--dex-file=", module.DexPath). |
Nicolas Geoffray | 2464ef4 | 2019-03-05 14:07:07 +0000 | [diff] [blame] | 387 | FlagWithArg("--dex-location=", dexLocationArg). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 388 | FlagWithOutput("--oat-file=", odexPath).ImplicitOutput(vdexPath). |
| 389 | // Pass an empty directory, dex2oat shouldn't be reading arbitrary files |
| 390 | FlagWithArg("--android-root=", global.EmptyDirectory). |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 391 | FlagWithArg("--instruction-set=", arch.String()). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 392 | FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]). |
| 393 | FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]). |
| 394 | Flag("--no-generate-debug-info"). |
| 395 | Flag("--generate-build-id"). |
| 396 | Flag("--abort-on-hard-verifier-error"). |
| 397 | Flag("--force-determinism"). |
| 398 | FlagWithArg("--no-inline-from=", "core-oj.jar") |
| 399 | |
| 400 | var preoptFlags []string |
| 401 | if len(module.PreoptFlags) > 0 { |
| 402 | preoptFlags = module.PreoptFlags |
| 403 | } else if len(global.PreoptFlags) > 0 { |
| 404 | preoptFlags = global.PreoptFlags |
| 405 | } |
| 406 | |
| 407 | if len(preoptFlags) > 0 { |
| 408 | cmd.Text(strings.Join(preoptFlags, " ")) |
| 409 | } |
| 410 | |
| 411 | if module.UncompressedDex { |
| 412 | cmd.FlagWithArg("--copy-dex-files=", "false") |
| 413 | } |
| 414 | |
Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 415 | if !android.PrefixInList(preoptFlags, "--compiler-filter=") { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 416 | var compilerFilter string |
Jiakai Zhang | 519c5c8 | 2021-09-16 06:15:39 +0000 | [diff] [blame] | 417 | if systemServerJars.ContainsJar(module.Name) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 418 | if global.SystemServerCompilerFilter != "" { |
Ulya Trafimovich | 6fad1d0 | 2022-06-30 13:21:35 +0100 | [diff] [blame] | 419 | // Use the product option if it is set. |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 420 | compilerFilter = global.SystemServerCompilerFilter |
Ulya Trafimovich | 6fad1d0 | 2022-06-30 13:21:35 +0100 | [diff] [blame] | 421 | } else if profile != nil { |
| 422 | // Use "speed-profile" for system server jars that have a profile. |
| 423 | compilerFilter = "speed-profile" |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 424 | } else { |
Ulya Trafimovich | 6fad1d0 | 2022-06-30 13:21:35 +0100 | [diff] [blame] | 425 | // Use "speed" for system server jars that do not have a profile. |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 426 | compilerFilter = "speed" |
| 427 | } |
| 428 | } else if contains(global.SpeedApps, module.Name) || contains(global.SystemServerApps, module.Name) { |
| 429 | // Apps loaded into system server, and apps the product default to being compiled with the |
| 430 | // 'speed' compiler filter. |
| 431 | compilerFilter = "speed" |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 432 | } else if profile != nil { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 433 | // For non system server jars, use speed-profile when we have a profile. |
| 434 | compilerFilter = "speed-profile" |
| 435 | } else if global.DefaultCompilerFilter != "" { |
| 436 | compilerFilter = global.DefaultCompilerFilter |
| 437 | } else { |
| 438 | compilerFilter = "quicken" |
| 439 | } |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 440 | if module.EnforceUsesLibraries { |
| 441 | // If the verify_uses_libraries check failed (in this case status file contains a |
Ulya Trafimovich | 4a13acb | 2021-03-02 12:25:02 +0000 | [diff] [blame] | 442 | // non-empty error message), then use "verify" compiler filter to avoid compiling any |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 443 | // code (it would be rejected on device because of a class loader context mismatch). |
| 444 | cmd.Text("--compiler-filter=$(if test -s "). |
| 445 | Input(module.EnforceUsesLibrariesStatusFile). |
Ulya Trafimovich | 4a13acb | 2021-03-02 12:25:02 +0000 | [diff] [blame] | 446 | Text(" ; then echo verify ; else echo " + compilerFilter + " ; fi)") |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 447 | } else { |
| 448 | cmd.FlagWithArg("--compiler-filter=", compilerFilter) |
| 449 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | if generateDM { |
| 453 | cmd.FlagWithArg("--copy-dex-files=", "false") |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 454 | dmPath := module.BuildPath.InSameDir(ctx, "generated.dm") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 455 | dmInstalledPath := pathtools.ReplaceExtension(module.DexLocation, "dm") |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 456 | tmpPath := module.BuildPath.InSameDir(ctx, "primary.vdex") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 457 | rule.Command().Text("cp -f").Input(vdexPath).Output(tmpPath) |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 458 | rule.Command().Tool(globalSoong.SoongZip). |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 459 | FlagWithArg("-L", "9"). |
| 460 | FlagWithOutput("-o", dmPath). |
| 461 | Flag("-j"). |
| 462 | Input(tmpPath) |
| 463 | rule.Install(dmPath, dmInstalledPath) |
| 464 | } |
| 465 | |
| 466 | // By default, emit debug info. |
| 467 | debugInfo := true |
| 468 | if global.NoDebugInfo { |
| 469 | // If the global setting suppresses mini-debug-info, disable it. |
| 470 | debugInfo = false |
| 471 | } |
| 472 | |
| 473 | // PRODUCT_SYSTEM_SERVER_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO. |
| 474 | // PRODUCT_OTHER_JAVA_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO. |
Jiakai Zhang | 519c5c8 | 2021-09-16 06:15:39 +0000 | [diff] [blame] | 475 | if systemServerJars.ContainsJar(module.Name) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 476 | if global.AlwaysSystemServerDebugInfo { |
| 477 | debugInfo = true |
| 478 | } else if global.NeverSystemServerDebugInfo { |
| 479 | debugInfo = false |
| 480 | } |
| 481 | } else { |
| 482 | if global.AlwaysOtherDebugInfo { |
| 483 | debugInfo = true |
| 484 | } else if global.NeverOtherDebugInfo { |
| 485 | debugInfo = false |
| 486 | } |
| 487 | } |
| 488 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 489 | if debugInfo { |
| 490 | cmd.Flag("--generate-mini-debug-info") |
| 491 | } else { |
| 492 | cmd.Flag("--no-generate-mini-debug-info") |
| 493 | } |
| 494 | |
| 495 | // Set the compiler reason to 'prebuilt' to identify the oat files produced |
| 496 | // during the build, as opposed to compiled on the device. |
| 497 | cmd.FlagWithArg("--compilation-reason=", "prebuilt") |
| 498 | |
| 499 | if appImage { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 500 | appImagePath := odexPath.ReplaceExtension(ctx, "art") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 501 | appImageInstallPath := pathtools.ReplaceExtension(odexInstallPath, "art") |
| 502 | cmd.FlagWithOutput("--app-image-file=", appImagePath). |
| 503 | FlagWithArg("--image-format=", "lz4") |
Mathieu Chartier | 3f7ddbb | 2019-04-29 09:33:50 -0700 | [diff] [blame] | 504 | if !global.DontResolveStartupStrings { |
| 505 | cmd.FlagWithArg("--resolve-startup-const-strings=", "true") |
| 506 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 507 | rule.Install(appImagePath, appImageInstallPath) |
| 508 | } |
| 509 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 510 | if profile != nil { |
| 511 | cmd.FlagWithInput("--profile-file=", profile) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Jiakai Zhang | 616be06 | 2022-11-16 11:50:59 +0000 | [diff] [blame] | 514 | if global.EnableUffdGc { |
| 515 | cmd.Flag("--runtime-arg").Flag("-Xgc:CMC") |
| 516 | } |
| 517 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 518 | rule.Install(odexPath, odexInstallPath) |
| 519 | rule.Install(vdexPath, vdexInstallPath) |
| 520 | } |
| 521 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 522 | func shouldGenerateDM(module *ModuleConfig, global *GlobalConfig) bool { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 523 | // Generating DM files only makes sense for verify, avoid doing for non verify compiler filter APKs. |
| 524 | // No reason to use a dm file if the dex is already uncompressed. |
| 525 | return global.GenerateDMFiles && !module.UncompressedDex && |
| 526 | contains(module.PreoptFlags, "--compiler-filter=verify") |
| 527 | } |
| 528 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 529 | func OdexOnSystemOtherByName(name string, dexLocation string, global *GlobalConfig) bool { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 530 | if !global.HasSystemOther { |
| 531 | return false |
| 532 | } |
| 533 | |
| 534 | if global.SanitizeLite { |
| 535 | return false |
| 536 | } |
| 537 | |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 538 | if contains(global.SpeedApps, name) || contains(global.SystemServerApps, name) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 539 | return false |
| 540 | } |
| 541 | |
| 542 | for _, f := range global.PatternsOnSystemOther { |
Anton Hansson | da4d9d9 | 2020-09-15 09:28:55 +0000 | [diff] [blame] | 543 | if makefileMatch(filepath.Join(SystemPartition, f), dexLocation) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 544 | return true |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | return false |
| 549 | } |
| 550 | |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 551 | func odexOnSystemOther(module *ModuleConfig, global *GlobalConfig) bool { |
Nicolas Geoffray | fa6e9ec | 2019-02-12 13:12:16 +0000 | [diff] [blame] | 552 | return OdexOnSystemOtherByName(module.Name, module.DexLocation, global) |
| 553 | } |
| 554 | |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 555 | // PathToLocation converts .../system/framework/arm64/boot.art to .../system/framework/boot.art |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 556 | func PathToLocation(path android.Path, arch android.ArchType) string { |
Jeongik Cha | 4dda75e | 2021-04-27 23:56:44 +0900 | [diff] [blame] | 557 | return PathStringToLocation(path.String(), arch) |
| 558 | } |
| 559 | |
| 560 | // PathStringToLocation converts .../system/framework/arm64/boot.art to .../system/framework/boot.art |
| 561 | func PathStringToLocation(path string, arch android.ArchType) string { |
| 562 | pathArch := filepath.Base(filepath.Dir(path)) |
Colin Cross | 74ba962 | 2019-02-11 15:11:14 -0800 | [diff] [blame] | 563 | if pathArch != arch.String() { |
| 564 | 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] | 565 | } |
Jeongik Cha | 4dda75e | 2021-04-27 23:56:44 +0900 | [diff] [blame] | 566 | return filepath.Join(filepath.Dir(filepath.Dir(path)), filepath.Base(path)) |
Colin Cross | c7e40aa | 2019-02-08 21:37:00 -0800 | [diff] [blame] | 567 | } |
| 568 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 569 | func makefileMatch(pattern, s string) bool { |
| 570 | percent := strings.IndexByte(pattern, '%') |
| 571 | switch percent { |
| 572 | case -1: |
| 573 | return pattern == s |
| 574 | case len(pattern) - 1: |
| 575 | return strings.HasPrefix(s, pattern[:len(pattern)-1]) |
| 576 | default: |
| 577 | panic(fmt.Errorf("unsupported makefile pattern %q", pattern)) |
| 578 | } |
| 579 | } |
| 580 | |
Ulya Trafimovich | dacc6c5 | 2020-03-11 11:59:34 +0000 | [diff] [blame] | 581 | // A predefined location for the system server dex jars. This is needed in order to generate |
| 582 | // class loader context for dex2oat, as the path to the jar in the Soong module may be unknown |
| 583 | // at that time (Soong processes the jars in dependency order, which may be different from the |
| 584 | // the system server classpath order). |
| 585 | func SystemServerDexJarHostPath(ctx android.PathContext, jar string) android.OutputPath { |
Ulya Trafimovich | 6cf2c0c | 2020-04-24 12:15:20 +0100 | [diff] [blame] | 586 | if DexpreoptRunningInSoong { |
| 587 | // Soong module, just use the default output directory $OUT/soong. |
| 588 | return android.PathForOutput(ctx, "system_server_dexjars", jar+".jar") |
| 589 | } else { |
| 590 | // Make module, default output directory is $OUT (passed via the "null config" created |
| 591 | // by dexpreopt_gen). Append Soong subdirectory to match Soong module paths. |
| 592 | return android.PathForOutput(ctx, "soong", "system_server_dexjars", jar+".jar") |
| 593 | } |
Ulya Trafimovich | dacc6c5 | 2020-03-11 11:59:34 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Ulya Trafimovich | cd3203f | 2020-03-27 11:30:00 +0000 | [diff] [blame] | 596 | // Check the order of jars on the system server classpath and give a warning/error if a jar precedes |
| 597 | // one of its dependencies. This is not an error, but a missed optimization, as dexpreopt won't |
| 598 | // have the dependency jar in the class loader context, and it won't be able to resolve any |
| 599 | // references to its classes and methods. |
| 600 | func checkSystemServerOrder(ctx android.PathContext, jarIndex int) { |
| 601 | mctx, isModule := ctx.(android.ModuleContext) |
| 602 | if isModule { |
| 603 | config := GetGlobalConfig(ctx) |
Jiakai Zhang | 389a647 | 2021-12-14 18:54:06 +0000 | [diff] [blame] | 604 | jars := config.AllSystemServerClasspathJars(ctx) |
Ulya Trafimovich | cd3203f | 2020-03-27 11:30:00 +0000 | [diff] [blame] | 605 | mctx.WalkDeps(func(dep android.Module, parent android.Module) bool { |
Jiakai Zhang | 519c5c8 | 2021-09-16 06:15:39 +0000 | [diff] [blame] | 606 | depIndex := jars.IndexOfJar(dep.Name()) |
Ulya Trafimovich | cd3203f | 2020-03-27 11:30:00 +0000 | [diff] [blame] | 607 | if jarIndex < depIndex && !config.BrokenSuboptimalOrderOfSystemServerJars { |
Jiakai Zhang | 519c5c8 | 2021-09-16 06:15:39 +0000 | [diff] [blame] | 608 | jar := jars.Jar(jarIndex) |
| 609 | dep := jars.Jar(depIndex) |
Ulya Trafimovich | cd3203f | 2020-03-27 11:30:00 +0000 | [diff] [blame] | 610 | mctx.ModuleErrorf("non-optimal order of jars on the system server classpath:"+ |
| 611 | " '%s' precedes its dependency '%s', so dexpreopt is unable to resolve any"+ |
| 612 | " references from '%s' to '%s'.\n", jar, dep, jar, dep) |
| 613 | } |
| 614 | return true |
| 615 | }) |
| 616 | } |
| 617 | } |
| 618 | |
Ulya Trafimovich | 8c35fcf | 2021-02-17 16:23:28 +0000 | [diff] [blame] | 619 | // Returns path to a file containing the reult of verify_uses_libraries check (empty if the check |
| 620 | // has succeeded, or an error message if it failed). |
| 621 | func UsesLibrariesStatusFile(ctx android.ModuleContext) android.WritablePath { |
| 622 | return android.PathForModuleOut(ctx, "enforce_uses_libraries.status") |
| 623 | } |
| 624 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 625 | func contains(l []string, s string) bool { |
| 626 | for _, e := range l { |
| 627 | if e == s { |
| 628 | return true |
| 629 | } |
| 630 | } |
| 631 | return false |
| 632 | } |