blob: 7a39fa1d701fe349dc243175ea0b941723ce2dfa [file] [log] [blame]
Colin Cross43f08db2018-11-12 10:13:39 -08001// 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 Geoffrayc1bf7242019-10-18 14:51:38 +010016// dexpreopting.
Colin Cross43f08db2018-11-12 10:13:39 -080017//
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 Geoffrayc1bf7242019-10-18 14:51:38 +010025// as necessary. The zip file may be empty if preopting was disabled for any reason.
Colin Cross43f08db2018-11-12 10:13:39 -080026//
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.
34package dexpreopt
35
36import (
37 "fmt"
38 "path/filepath"
Colin Cross69f59a32019-02-15 10:39:37 -080039 "runtime"
Colin Cross43f08db2018-11-12 10:13:39 -080040 "strings"
41
Colin Crossfeec25b2019-01-30 17:32:39 -080042 "android/soong/android"
43
Colin Cross43f08db2018-11-12 10:13:39 -080044 "github.com/google/blueprint/pathtools"
45)
46
47const SystemPartition = "/system/"
48const SystemOtherPartition = "/system_other/"
49
Ulya Trafimovich6cf2c0c2020-04-24 12:15:20 +010050var DexpreoptRunningInSoong = false
51
Colin Cross43f08db2018-11-12 10:13:39 -080052// 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 Crossf1a035e2020-11-16 17:32:30 -080054func GenerateDexpreoptRule(ctx android.BuilderContext, globalSoong *GlobalSoongConfig,
Spandan Das5ae65ee2024-04-16 22:03:26 +000055 global *GlobalConfig, module *ModuleConfig, productPackages android.Path, copyApexSystemServerJarDex bool) (
Jiakai Zhanga4496782023-05-17 16:57:30 +010056 rule *android.RuleBuilder, err error) {
Colin Cross69f59a32019-02-15 10:39:37 -080057
Colin Cross43f08db2018-11-12 10:13:39 -080058 defer func() {
59 if r := recover(); r != nil {
Colin Cross69f59a32019-02-15 10:39:37 -080060 if _, ok := r.(runtime.Error); ok {
61 panic(r)
62 } else if e, ok := r.(error); ok {
Colin Cross43f08db2018-11-12 10:13:39 -080063 err = e
64 rule = nil
65 } else {
66 panic(r)
67 }
68 }
69 }()
70
Colin Crossf1a035e2020-11-16 17:32:30 -080071 rule = android.NewRuleBuilder(pctx, ctx)
Colin Cross43f08db2018-11-12 10:13:39 -080072
Colin Cross69f59a32019-02-15 10:39:37 -080073 generateProfile := module.ProfileClassListing.Valid() && !global.DisableGenerateProfile
Nicolas Geoffraye7102422019-07-24 13:19:29 +010074 generateBootProfile := module.ProfileBootListing.Valid() && !global.DisableGenerateProfile
Colin Cross43f08db2018-11-12 10:13:39 -080075
Colin Cross69f59a32019-02-15 10:39:37 -080076 var profile android.WritablePath
Colin Crosscbed6572019-01-08 17:38:37 -080077 if generateProfile {
Martin Stjernholm75a48d82020-01-10 20:32:59 +000078 profile = profileCommand(ctx, globalSoong, global, module, rule)
Colin Crosscbed6572019-01-08 17:38:37 -080079 }
Nicolas Geoffraye7102422019-07-24 13:19:29 +010080 if generateBootProfile {
Martin Stjernholm75a48d82020-01-10 20:32:59 +000081 bootProfileCommand(ctx, globalSoong, global, module, rule)
Nicolas Geoffraye7102422019-07-24 13:19:29 +010082 }
Colin Crosscbed6572019-01-08 17:38:37 -080083
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +000084 if !dexpreoptDisabled(ctx, global, module) {
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +000085 if valid, err := validateClassLoaderContext(module.ClassLoaderContexts); err != nil {
Ulya Trafimovich69612672020-10-20 17:41:54 +010086 android.ReportPathErrorf(ctx, err.Error())
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +000087 } else if valid {
88 fixClassLoaderContext(module.ClassLoaderContexts)
89
Colin Crosscbed6572019-01-08 17:38:37 -080090 appImage := (generateProfile || module.ForceCreateAppImage || global.DefaultAppImages) &&
91 !module.NoCreateAppImage
92
93 generateDM := shouldGenerateDM(module, global)
94
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000095 for archIdx, _ := range module.Archs {
Jiakai Zhanga4496782023-05-17 16:57:30 +010096 dexpreoptCommand(ctx, globalSoong, global, module, rule, archIdx, profile, appImage,
Spandan Das5ae65ee2024-04-16 22:03:26 +000097 generateDM, productPackages, copyApexSystemServerJarDex)
Colin Crosscbed6572019-01-08 17:38:37 -080098 }
99 }
100 }
101
102 return rule, nil
103}
104
Jiakai Zhangcf61e3c2023-05-08 16:28:38 +0000105// 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 Stjernholm8d80cee2020-01-31 17:44:54 +0000109func dexpreoptDisabled(ctx android.PathContext, global *GlobalConfig, module *ModuleConfig) bool {
Jiakai Zhang3317ce72023-02-08 01:19:19 +0800110 if ctx.Config().UnbundledBuild() {
111 return true
112 }
113
Jiakai Zhangcf61e3c2023-05-08 16:28:38 +0000114 if global.DisablePreopt {
115 return true
116 }
117
Colin Crosscbed6572019-01-08 17:38:37 -0800118 if contains(global.DisablePreoptModules, module.Name) {
119 return true
Colin Cross43f08db2018-11-12 10:13:39 -0800120 }
121
Ulya Trafimovichfc24ad32020-08-19 16:32:54 +0100122 // Don't preopt individual boot jars, they will be preopted together.
123 if global.BootJars.ContainsJar(module.Name) {
124 return true
125 }
126
Jiakai Zhangdb935532023-11-28 13:38:23 +0000127 if global.OnlyPreoptArtBootImage {
Colin Crosscbed6572019-01-08 17:38:37 -0800128 return true
Colin Cross43f08db2018-11-12 10:13:39 -0800129 }
130
Colin Crosscbed6572019-01-08 17:38:37 -0800131 return false
Colin Cross43f08db2018-11-12 10:13:39 -0800132}
133
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000134func profileCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig,
135 module *ModuleConfig, rule *android.RuleBuilder) android.WritablePath {
Colin Cross69f59a32019-02-15 10:39:37 -0800136
137 profilePath := module.BuildPath.InSameDir(ctx, "profile.prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800138 profileInstalledPath := module.DexLocation + ".prof"
139
140 if !module.ProfileIsTextListing {
Vladimir Marko982e3842021-04-29 09:05:18 +0100141 rule.Command().Text("rm -f").Output(profilePath)
142 rule.Command().Text("touch").Output(profilePath)
Colin Cross43f08db2018-11-12 10:13:39 -0800143 }
144
145 cmd := rule.Command().
146 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000147 Tool(globalSoong.Profman)
Colin Cross43f08db2018-11-12 10:13:39 -0800148
149 if module.ProfileIsTextListing {
150 // The profile is a test listing of classes (used for framework jars).
151 // We need to generate the actual binary profile before being able to compile.
Colin Cross69f59a32019-02-15 10:39:37 -0800152 cmd.FlagWithInput("--create-profile-from=", module.ProfileClassListing.Path())
Colin Cross43f08db2018-11-12 10:13:39 -0800153 } else {
154 // The profile is binary profile (used for apps). Run it through profman to
155 // ensure the profile keys match the apk.
156 cmd.
157 Flag("--copy-and-update-profile-key").
Colin Cross69f59a32019-02-15 10:39:37 -0800158 FlagWithInput("--profile-file=", module.ProfileClassListing.Path())
Colin Cross43f08db2018-11-12 10:13:39 -0800159 }
160
161 cmd.
Vladimir Marko230bd422021-04-23 15:18:33 +0100162 Flag("--output-profile-type=app").
Colin Cross43f08db2018-11-12 10:13:39 -0800163 FlagWithInput("--apk=", module.DexPath).
164 Flag("--dex-location="+module.DexLocation).
165 FlagWithOutput("--reference-profile-file=", profilePath)
166
167 if !module.ProfileIsTextListing {
168 cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath))
169 }
170 rule.Install(profilePath, profileInstalledPath)
171
172 return profilePath
173}
174
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000175func bootProfileCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig,
176 module *ModuleConfig, rule *android.RuleBuilder) android.WritablePath {
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100177
178 profilePath := module.BuildPath.InSameDir(ctx, "profile.bprof")
179 profileInstalledPath := module.DexLocation + ".bprof"
180
181 if !module.ProfileIsTextListing {
Vladimir Marko982e3842021-04-29 09:05:18 +0100182 rule.Command().Text("rm -f").Output(profilePath)
183 rule.Command().Text("touch").Output(profilePath)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100184 }
185
186 cmd := rule.Command().
187 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000188 Tool(globalSoong.Profman)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100189
190 // The profile is a test listing of methods.
191 // We need to generate the actual binary profile.
192 cmd.FlagWithInput("--create-profile-from=", module.ProfileBootListing.Path())
193
194 cmd.
Vladimir Marko230bd422021-04-23 15:18:33 +0100195 Flag("--output-profile-type=bprof").
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100196 FlagWithInput("--apk=", module.DexPath).
197 Flag("--dex-location="+module.DexLocation).
198 FlagWithOutput("--reference-profile-file=", profilePath)
199
200 if !module.ProfileIsTextListing {
201 cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath))
202 }
203 rule.Install(profilePath, profileInstalledPath)
204
205 return profilePath
206}
207
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000208// Returns the dex location of a system server java library.
Jiakai Zhang389a6472021-12-14 18:54:06 +0000209func GetSystemServerDexLocation(ctx android.PathContext, global *GlobalConfig, lib string) string {
210 if apex := global.AllApexSystemServerJars(ctx).ApexOfJar(lib); apex != "" {
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000211 return fmt.Sprintf("/apex/%s/javalib/%s.jar", apex, lib)
212 }
liulvping76d56ee2022-05-07 14:40:13 +0800213
214 if apex := global.AllPlatformSystemServerJars(ctx).ApexOfJar(lib); apex == "system_ext" {
215 return fmt.Sprintf("/system_ext/framework/%s.jar", lib)
216 }
217
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000218 return fmt.Sprintf("/system/framework/%s.jar", lib)
219}
220
Jiakai Zhang0a0a2fb2021-09-30 09:38:19 +0000221// Returns the location to the odex file for the dex file at `path`.
Spandan Das950deca2024-10-01 18:35:23 +0000222func ToOdexPath(path string, arch android.ArchType, partition string) string {
Jiakai Zhang0a0a2fb2021-09-30 09:38:19 +0000223 if strings.HasPrefix(path, "/apex/") {
Spandan Das950deca2024-10-01 18:35:23 +0000224 return filepath.Join(partition, "framework/oat", arch.String(),
Jiakai Zhang0a0a2fb2021-09-30 09:38:19 +0000225 strings.ReplaceAll(path[1:], "/", "@")+"@classes.odex")
226 }
227
228 return filepath.Join(filepath.Dir(path), "oat", arch.String(),
229 pathtools.ReplaceExtension(filepath.Base(path), "odex"))
230}
231
Jiakai Zhanga4496782023-05-17 16:57:30 +0100232func dexpreoptCommand(ctx android.BuilderContext, globalSoong *GlobalSoongConfig,
233 global *GlobalConfig, module *ModuleConfig, rule *android.RuleBuilder, archIdx int,
Spandan Das5ae65ee2024-04-16 22:03:26 +0000234 profile android.WritablePath, appImage bool, generateDM bool, productPackages android.Path, copyApexSystemServerJarDex bool) {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000235
236 arch := module.Archs[archIdx]
Colin Cross43f08db2018-11-12 10:13:39 -0800237
238 // HACK: make soname in Soong-generated .odex files match Make.
239 base := filepath.Base(module.DexLocation)
240 if filepath.Ext(base) == ".jar" {
241 base = "javalib.jar"
242 } else if filepath.Ext(base) == ".apk" {
243 base = "package.apk"
244 }
245
Colin Cross69f59a32019-02-15 10:39:37 -0800246 odexPath := module.BuildPath.InSameDir(ctx, "oat", arch.String(), pathtools.ReplaceExtension(base, "odex"))
Jared Duke9c17ad62024-05-22 20:48:45 +0000247 odexSymbolsPath := odexPath.ReplaceExtension(ctx, "symbols.odex")
Spandan Das950deca2024-10-01 18:35:23 +0000248 odexInstallPath := ToOdexPath(module.DexLocation, arch, module.ApexPartition)
Colin Cross43f08db2018-11-12 10:13:39 -0800249 if odexOnSystemOther(module, global) {
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100250 odexInstallPath = filepath.Join(SystemOtherPartition, odexInstallPath)
Colin Cross43f08db2018-11-12 10:13:39 -0800251 }
252
Colin Cross69f59a32019-02-15 10:39:37 -0800253 vdexPath := odexPath.ReplaceExtension(ctx, "vdex")
Colin Cross43f08db2018-11-12 10:13:39 -0800254 vdexInstallPath := pathtools.ReplaceExtension(odexInstallPath, "vdex")
255
Colin Cross69f59a32019-02-15 10:39:37 -0800256 invocationPath := odexPath.ReplaceExtension(ctx, "invocation")
Alex Light5de41962018-12-18 15:16:26 -0800257
Jiakai Zhang389a6472021-12-14 18:54:06 +0000258 systemServerJars := global.AllSystemServerJars(ctx)
259 systemServerClasspathJars := global.AllSystemServerClasspathJars(ctx)
Ulya Trafimovichdacc6c52020-03-11 11:59:34 +0000260
Ulya Trafimovichc4dac262020-06-30 11:25:49 +0100261 rule.Command().FlagWithArg("mkdir -p ", filepath.Dir(odexPath.String()))
Jared Duke9c17ad62024-05-22 20:48:45 +0000262 rule.Command().FlagWithOutput("rm -f ", odexPath).
263 FlagWithArg("rm -f ", odexSymbolsPath.String())
Ulya Trafimovichc9af5382020-05-29 15:35:06 +0100264
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000265 if jarIndex := systemServerJars.IndexOfJar(module.Name); jarIndex >= 0 {
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000266 // System server jars should be dexpreopted together: class loader context of each jar
267 // should include all preceding jars on the system server classpath.
268
269 var clcHost android.Paths
270 var clcTarget []string
Jiakai Zhang389a6472021-12-14 18:54:06 +0000271 endIndex := systemServerClasspathJars.IndexOfJar(module.Name)
272 if endIndex < 0 {
273 // The jar is a standalone one. Use the full classpath as the class loader context.
274 endIndex = systemServerClasspathJars.Len()
275 }
276 for i := 0; i < endIndex; i++ {
277 lib := systemServerClasspathJars.Jar(i)
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000278 clcHost = append(clcHost, SystemServerDexJarHostPath(ctx, lib))
Jiakai Zhang389a6472021-12-14 18:54:06 +0000279 clcTarget = append(clcTarget, GetSystemServerDexLocation(ctx, global, lib))
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000280 }
281
Spandan Das5ae65ee2024-04-16 22:03:26 +0000282 if DexpreoptRunningInSoong && copyApexSystemServerJarDex {
Spandan Dasb2fd4ff2024-01-25 04:25:38 +0000283 // Copy the system server jar to a predefined location where dex2oat will find it.
284 dexPathHost := SystemServerDexJarHostPath(ctx, module.Name)
285 rule.Command().Text("mkdir -p").Flag(filepath.Dir(dexPathHost.String()))
286 rule.Command().Text("cp -f").Input(module.DexPath).Output(dexPathHost)
287 } else {
288 // For Make modules the copy rule is generated in the makefiles, not in dexpreopt.sh.
289 // This is necessary to expose the rule to Ninja, otherwise it has rules that depend on
290 // the jar (namely, dexpreopt commands for all subsequent system server jars that have
291 // this one in their class loader context), but no rule that creates it (because Ninja
292 // cannot see the rule in the generated dexpreopt.sh script).
293 }
294
Jiakai Zhang389a6472021-12-14 18:54:06 +0000295 clcHostString := "PCL[" + strings.Join(clcHost.Strings(), ":") + "]"
296 clcTargetString := "PCL[" + strings.Join(clcTarget, ":") + "]"
297
298 if systemServerClasspathJars.ContainsJar(module.Name) {
299 checkSystemServerOrder(ctx, jarIndex)
300 } else {
301 // Standalone jars are loaded by separate class loaders with SYSTEMSERVERCLASSPATH as the
302 // parent.
303 clcHostString = "PCL[];" + clcHostString
304 clcTargetString = "PCL[];" + clcTargetString
305 }
Ulya Trafimovichc4dac262020-06-30 11:25:49 +0100306
Ulya Trafimovichc4dac262020-06-30 11:25:49 +0100307 rule.Command().
Jiakai Zhang389a6472021-12-14 18:54:06 +0000308 Text(`class_loader_context_arg=--class-loader-context="` + clcHostString + `"`).
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000309 Implicits(clcHost).
Jiakai Zhang389a6472021-12-14 18:54:06 +0000310 Text(`stored_class_loader_context_arg=--stored-class-loader-context="` + clcTargetString + `"`)
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +0000311
Ulya Trafimovichbd6b0762021-03-12 12:12:12 +0000312 } else {
313 // There are three categories of Java modules handled here:
314 //
315 // - Modules that have passed verify_uses_libraries check. They are AOT-compiled and
316 // expected to be loaded on device without CLC mismatch errors.
317 //
318 // - Modules that have failed the check in relaxed mode, so it didn't cause a build error.
319 // They are dexpreopted with "verify" filter and not AOT-compiled.
320 // TODO(b/132357300): ensure that CLC mismatch errors are ignored with "verify" filter.
321 //
322 // - Modules that didn't run the check. They are AOT-compiled, but it's unknown if they
323 // will have CLC mismatch errors on device (the check is disabled by default).
324 //
325 // TODO(b/132357300): enable the check by default and eliminate the last category, so that
326 // no time/space is wasted on AOT-compiling modules that will fail CLC check on device.
327
328 var manifestOrApk android.Path
Jeongik Cha33a3a812021-04-15 09:12:49 +0900329 if module.ManifestPath.Valid() {
Ulya Trafimovichbd6b0762021-03-12 12:12:12 +0000330 // Ok, there is an XML manifest.
Jeongik Cha33a3a812021-04-15 09:12:49 +0900331 manifestOrApk = module.ManifestPath.Path()
Ulya Trafimovichbd6b0762021-03-12 12:12:12 +0000332 } else if filepath.Ext(base) == ".apk" {
333 // Ok, there is is an APK with the manifest inside.
Ulya Trafimovich0aba2522021-03-03 16:38:37 +0000334 manifestOrApk = module.DexPath
Colin Cross38b96852019-05-22 10:21:09 -0700335 }
Ulya Trafimovichbd6b0762021-03-12 12:12:12 +0000336
337 // Generate command that saves target SDK version in a shell variable.
338 if manifestOrApk == nil {
339 // There is neither an XML manifest nor APK => nowhere to extract targetSdkVersion from.
340 // Set the latest ("any") version: then construct_context will not add any compatibility
341 // libraries (if this is incorrect, there will be a CLC mismatch and dexopt on device).
342 rule.Command().Textf(`target_sdk_version=%d`, AnySdkVersion)
343 } else {
344 rule.Command().Text(`target_sdk_version="$(`).
345 Tool(globalSoong.ManifestCheck).
346 Flag("--extract-target-sdk-version").
347 Input(manifestOrApk).
Ulya Trafimovichdd622952021-03-25 12:52:49 +0000348 FlagWithInput("--aapt ", globalSoong.Aapt).
Ulya Trafimovichbd6b0762021-03-12 12:12:12 +0000349 Text(`)"`)
350 }
Ulya Trafimovichc4dac262020-06-30 11:25:49 +0100351
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100352 // Generate command that saves host and target class loader context in shell variables.
Jiakai Zhang51b2a8b2023-06-26 16:47:38 +0100353 _, paths := ComputeClassLoaderContextDependencies(module.ClassLoaderContexts)
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +0000354 rule.Command().
Ulya Trafimovichbd6b0762021-03-12 12:12:12 +0000355 Text(`eval "$(`).Tool(globalSoong.ConstructContext).
Ulya Trafimovich8130c482020-10-07 15:17:13 +0100356 Text(` --target-sdk-version ${target_sdk_version}`).
Jiakai Zhanga4496782023-05-17 16:57:30 +0100357 FlagWithArg("--context-json=", module.ClassLoaderContexts.DumpForFlag()).
358 FlagWithInput("--product-packages=", productPackages).
359 Implicits(paths).
Ulya Trafimovichbd6b0762021-03-12 12:12:12 +0000360 Text(`)"`)
Colin Cross43f08db2018-11-12 10:13:39 -0800361 }
362
Nicolas Geoffray2464ef42019-03-05 14:07:07 +0000363 // Devices that do not have a product partition use a symlink from /product to /system/product.
364 // Because on-device dexopt will see dex locations starting with /product, we change the paths
365 // to mimic this behavior.
366 dexLocationArg := module.DexLocation
367 if strings.HasPrefix(dexLocationArg, "/system/product/") {
368 dexLocationArg = strings.TrimPrefix(dexLocationArg, "/system")
369 }
370
Colin Cross43f08db2018-11-12 10:13:39 -0800371 cmd := rule.Command().
372 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000373 Tool(globalSoong.Dex2oat).
Colin Cross43f08db2018-11-12 10:13:39 -0800374 Flag("--avoid-storing-invocation").
Alex Light5de41962018-12-18 15:16:26 -0800375 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross43f08db2018-11-12 10:13:39 -0800376 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatXms).
377 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatXmx).
Colin Cross800fe132019-02-11 14:21:24 -0800378 Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", module.PreoptBootClassPathDexFiles, ":").
379 Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", module.PreoptBootClassPathDexLocations, ":").
Colin Cross43f08db2018-11-12 10:13:39 -0800380 Flag("${class_loader_context_arg}").
Ulya Trafimovichdacc6c52020-03-11 11:59:34 +0000381 Flag("${stored_class_loader_context_arg}").
Jeongik Chaa5969092021-05-07 18:53:21 +0900382 FlagWithArg("--boot-image=", strings.Join(module.DexPreoptImageLocationsOnHost, ":")).Implicits(module.DexPreoptImagesDeps[archIdx].Paths()).
Colin Cross43f08db2018-11-12 10:13:39 -0800383 FlagWithInput("--dex-file=", module.DexPath).
Nicolas Geoffray2464ef42019-03-05 14:07:07 +0000384 FlagWithArg("--dex-location=", dexLocationArg).
Colin Cross43f08db2018-11-12 10:13:39 -0800385 FlagWithOutput("--oat-file=", odexPath).ImplicitOutput(vdexPath).
386 // Pass an empty directory, dex2oat shouldn't be reading arbitrary files
387 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross74ba9622019-02-11 15:11:14 -0800388 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross43f08db2018-11-12 10:13:39 -0800389 FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]).
390 FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]).
Jared Duke9c17ad62024-05-22 20:48:45 +0000391 FlagWithOutput("--oat-symbols=", odexSymbolsPath).
392 Flag("--generate-debug-info").
393 Flag("--strip").
Colin Cross43f08db2018-11-12 10:13:39 -0800394 Flag("--generate-build-id").
395 Flag("--abort-on-hard-verifier-error").
396 Flag("--force-determinism").
Jiakai Zhang7d292222024-01-18 17:27:42 +0000397 FlagWithArg("--no-inline-from=", "core-oj.jar").
398 Text("$(cat").Input(globalSoong.UffdGcFlag).Text(")")
Colin Cross43f08db2018-11-12 10:13:39 -0800399
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 Jung3aff5782020-02-11 07:54:35 -0800415 if !android.PrefixInList(preoptFlags, "--compiler-filter=") {
Colin Cross43f08db2018-11-12 10:13:39 -0800416 var compilerFilter string
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000417 if systemServerJars.ContainsJar(module.Name) {
Colin Cross43f08db2018-11-12 10:13:39 -0800418 if global.SystemServerCompilerFilter != "" {
Ulya Trafimovich6fad1d02022-06-30 13:21:35 +0100419 // Use the product option if it is set.
Colin Cross43f08db2018-11-12 10:13:39 -0800420 compilerFilter = global.SystemServerCompilerFilter
Ulya Trafimovich6fad1d02022-06-30 13:21:35 +0100421 } else if profile != nil {
422 // Use "speed-profile" for system server jars that have a profile.
423 compilerFilter = "speed-profile"
Colin Cross43f08db2018-11-12 10:13:39 -0800424 } else {
Ulya Trafimovich6fad1d02022-06-30 13:21:35 +0100425 // Use "speed" for system server jars that do not have a profile.
Colin Cross43f08db2018-11-12 10:13:39 -0800426 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 Cross69f59a32019-02-15 10:39:37 -0800432 } else if profile != nil {
Colin Cross43f08db2018-11-12 10:13:39 -0800433 // 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 Trafimovich8c35fcf2021-02-17 16:23:28 +0000440 if module.EnforceUsesLibraries {
441 // If the verify_uses_libraries check failed (in this case status file contains a
Ulya Trafimovich4a13acb2021-03-02 12:25:02 +0000442 // non-empty error message), then use "verify" compiler filter to avoid compiling any
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +0000443 // 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 Trafimovich4a13acb2021-03-02 12:25:02 +0000446 Text(" ; then echo verify ; else echo " + compilerFilter + " ; fi)")
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +0000447 } else {
448 cmd.FlagWithArg("--compiler-filter=", compilerFilter)
449 }
Colin Cross43f08db2018-11-12 10:13:39 -0800450 }
451
452 if generateDM {
453 cmd.FlagWithArg("--copy-dex-files=", "false")
Colin Cross69f59a32019-02-15 10:39:37 -0800454 dmPath := module.BuildPath.InSameDir(ctx, "generated.dm")
Colin Cross43f08db2018-11-12 10:13:39 -0800455 dmInstalledPath := pathtools.ReplaceExtension(module.DexLocation, "dm")
Colin Cross69f59a32019-02-15 10:39:37 -0800456 tmpPath := module.BuildPath.InSameDir(ctx, "primary.vdex")
Colin Cross43f08db2018-11-12 10:13:39 -0800457 rule.Command().Text("cp -f").Input(vdexPath).Output(tmpPath)
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000458 rule.Command().Tool(globalSoong.SoongZip).
Colin Cross43f08db2018-11-12 10:13:39 -0800459 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 Zhang519c5c82021-09-16 06:15:39 +0000475 if systemServerJars.ContainsJar(module.Name) {
Colin Cross43f08db2018-11-12 10:13:39 -0800476 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 Cross43f08db2018-11-12 10:13:39 -0800489 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 Cross69f59a32019-02-15 10:39:37 -0800500 appImagePath := odexPath.ReplaceExtension(ctx, "art")
Colin Cross43f08db2018-11-12 10:13:39 -0800501 appImageInstallPath := pathtools.ReplaceExtension(odexInstallPath, "art")
502 cmd.FlagWithOutput("--app-image-file=", appImagePath).
503 FlagWithArg("--image-format=", "lz4")
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700504 if !global.DontResolveStartupStrings {
505 cmd.FlagWithArg("--resolve-startup-const-strings=", "true")
506 }
Colin Cross43f08db2018-11-12 10:13:39 -0800507 rule.Install(appImagePath, appImageInstallPath)
508 }
509
Colin Cross69f59a32019-02-15 10:39:37 -0800510 if profile != nil {
511 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross43f08db2018-11-12 10:13:39 -0800512 }
513
514 rule.Install(odexPath, odexInstallPath)
515 rule.Install(vdexPath, vdexInstallPath)
516}
517
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000518func shouldGenerateDM(module *ModuleConfig, global *GlobalConfig) bool {
Colin Cross43f08db2018-11-12 10:13:39 -0800519 // Generating DM files only makes sense for verify, avoid doing for non verify compiler filter APKs.
520 // No reason to use a dm file if the dex is already uncompressed.
521 return global.GenerateDMFiles && !module.UncompressedDex &&
522 contains(module.PreoptFlags, "--compiler-filter=verify")
523}
524
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000525func OdexOnSystemOtherByName(name string, dexLocation string, global *GlobalConfig) bool {
Colin Cross43f08db2018-11-12 10:13:39 -0800526 if !global.HasSystemOther {
527 return false
528 }
529
530 if global.SanitizeLite {
531 return false
532 }
533
Spandan Das780a0cf2024-06-27 02:33:36 +0000534 if contains(global.SystemServerApps, name) {
Colin Cross43f08db2018-11-12 10:13:39 -0800535 return false
536 }
537
538 for _, f := range global.PatternsOnSystemOther {
Colin Cross8a49a3d2024-05-20 12:22:27 -0700539 if makefileMatch("/"+f, dexLocation) || makefileMatch(filepath.Join(SystemPartition, f), dexLocation) {
Colin Cross43f08db2018-11-12 10:13:39 -0800540 return true
541 }
542 }
543
544 return false
545}
546
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000547func odexOnSystemOther(module *ModuleConfig, global *GlobalConfig) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000548 return OdexOnSystemOtherByName(module.Name, module.DexLocation, global)
549}
550
Colin Crossc7e40aa2019-02-08 21:37:00 -0800551// PathToLocation converts .../system/framework/arm64/boot.art to .../system/framework/boot.art
Colin Cross69f59a32019-02-15 10:39:37 -0800552func PathToLocation(path android.Path, arch android.ArchType) string {
Jeongik Cha4dda75e2021-04-27 23:56:44 +0900553 return PathStringToLocation(path.String(), arch)
554}
555
556// PathStringToLocation converts .../system/framework/arm64/boot.art to .../system/framework/boot.art
557func PathStringToLocation(path string, arch android.ArchType) string {
558 pathArch := filepath.Base(filepath.Dir(path))
Colin Cross74ba9622019-02-11 15:11:14 -0800559 if pathArch != arch.String() {
560 panic(fmt.Errorf("last directory in %q must be %q", path, arch.String()))
Colin Crossc7e40aa2019-02-08 21:37:00 -0800561 }
Jeongik Cha4dda75e2021-04-27 23:56:44 +0900562 return filepath.Join(filepath.Dir(filepath.Dir(path)), filepath.Base(path))
Colin Crossc7e40aa2019-02-08 21:37:00 -0800563}
564
Colin Cross43f08db2018-11-12 10:13:39 -0800565func makefileMatch(pattern, s string) bool {
566 percent := strings.IndexByte(pattern, '%')
567 switch percent {
568 case -1:
569 return pattern == s
570 case len(pattern) - 1:
571 return strings.HasPrefix(s, pattern[:len(pattern)-1])
572 default:
573 panic(fmt.Errorf("unsupported makefile pattern %q", pattern))
574 }
575}
576
Ulya Trafimovichdacc6c52020-03-11 11:59:34 +0000577// A predefined location for the system server dex jars. This is needed in order to generate
578// class loader context for dex2oat, as the path to the jar in the Soong module may be unknown
579// at that time (Soong processes the jars in dependency order, which may be different from the
580// the system server classpath order).
581func SystemServerDexJarHostPath(ctx android.PathContext, jar string) android.OutputPath {
Ulya Trafimovich6cf2c0c2020-04-24 12:15:20 +0100582 if DexpreoptRunningInSoong {
583 // Soong module, just use the default output directory $OUT/soong.
584 return android.PathForOutput(ctx, "system_server_dexjars", jar+".jar")
585 } else {
586 // Make module, default output directory is $OUT (passed via the "null config" created
587 // by dexpreopt_gen). Append Soong subdirectory to match Soong module paths.
588 return android.PathForOutput(ctx, "soong", "system_server_dexjars", jar+".jar")
589 }
Ulya Trafimovichdacc6c52020-03-11 11:59:34 +0000590}
591
Ulya Trafimovichcd3203f2020-03-27 11:30:00 +0000592// Check the order of jars on the system server classpath and give a warning/error if a jar precedes
593// one of its dependencies. This is not an error, but a missed optimization, as dexpreopt won't
594// have the dependency jar in the class loader context, and it won't be able to resolve any
595// references to its classes and methods.
596func checkSystemServerOrder(ctx android.PathContext, jarIndex int) {
597 mctx, isModule := ctx.(android.ModuleContext)
598 if isModule {
599 config := GetGlobalConfig(ctx)
Jiakai Zhang389a6472021-12-14 18:54:06 +0000600 jars := config.AllSystemServerClasspathJars(ctx)
Ulya Trafimovichcd3203f2020-03-27 11:30:00 +0000601 mctx.WalkDeps(func(dep android.Module, parent android.Module) bool {
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000602 depIndex := jars.IndexOfJar(dep.Name())
Ulya Trafimovichcd3203f2020-03-27 11:30:00 +0000603 if jarIndex < depIndex && !config.BrokenSuboptimalOrderOfSystemServerJars {
Jiakai Zhang519c5c82021-09-16 06:15:39 +0000604 jar := jars.Jar(jarIndex)
605 dep := jars.Jar(depIndex)
Ulya Trafimovichcd3203f2020-03-27 11:30:00 +0000606 mctx.ModuleErrorf("non-optimal order of jars on the system server classpath:"+
607 " '%s' precedes its dependency '%s', so dexpreopt is unable to resolve any"+
608 " references from '%s' to '%s'.\n", jar, dep, jar, dep)
609 }
610 return true
611 })
612 }
613}
614
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +0000615// Returns path to a file containing the reult of verify_uses_libraries check (empty if the check
616// has succeeded, or an error message if it failed).
617func UsesLibrariesStatusFile(ctx android.ModuleContext) android.WritablePath {
618 return android.PathForModuleOut(ctx, "enforce_uses_libraries.status")
619}
620
Colin Cross43f08db2018-11-12 10:13:39 -0800621func contains(l []string, s string) bool {
622 for _, e := range l {
623 if e == s {
624 return true
625 }
626 }
627 return false
628}