blob: de696da10b6c69a40dce72bfe18f38cea5e273ea [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().
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000054func GenerateDexpreoptRule(ctx android.PathContext, globalSoong *GlobalSoongConfig,
55 global *GlobalConfig, module *ModuleConfig) (rule *android.RuleBuilder, err error) {
Colin Cross69f59a32019-02-15 10:39:37 -080056
Colin Cross43f08db2018-11-12 10:13:39 -080057 defer func() {
58 if r := recover(); r != nil {
Colin Cross69f59a32019-02-15 10:39:37 -080059 if _, ok := r.(runtime.Error); ok {
60 panic(r)
61 } else if e, ok := r.(error); ok {
Colin Cross43f08db2018-11-12 10:13:39 -080062 err = e
63 rule = nil
64 } else {
65 panic(r)
66 }
67 }
68 }()
69
Colin Cross758290d2019-02-01 16:42:32 -080070 rule = android.NewRuleBuilder()
Colin Cross43f08db2018-11-12 10:13:39 -080071
Colin Cross69f59a32019-02-15 10:39:37 -080072 generateProfile := module.ProfileClassListing.Valid() && !global.DisableGenerateProfile
Nicolas Geoffraye7102422019-07-24 13:19:29 +010073 generateBootProfile := module.ProfileBootListing.Valid() && !global.DisableGenerateProfile
Colin Cross43f08db2018-11-12 10:13:39 -080074
Colin Cross69f59a32019-02-15 10:39:37 -080075 var profile android.WritablePath
Colin Crosscbed6572019-01-08 17:38:37 -080076 if generateProfile {
Martin Stjernholm75a48d82020-01-10 20:32:59 +000077 profile = profileCommand(ctx, globalSoong, global, module, rule)
Colin Crosscbed6572019-01-08 17:38:37 -080078 }
Nicolas Geoffraye7102422019-07-24 13:19:29 +010079 if generateBootProfile {
Martin Stjernholm75a48d82020-01-10 20:32:59 +000080 bootProfileCommand(ctx, globalSoong, global, module, rule)
Nicolas Geoffraye7102422019-07-24 13:19:29 +010081 }
Colin Crosscbed6572019-01-08 17:38:37 -080082
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +000083 if !dexpreoptDisabled(ctx, global, module) {
Colin Crosscbed6572019-01-08 17:38:37 -080084 // Don't preopt individual boot jars, they will be preopted together.
Ulya Trafimovich8640ab92020-05-11 18:06:15 +010085 if !contains(android.GetJarsFromApexJarPairs(ctx, global.BootJars), module.Name) {
Colin Crosscbed6572019-01-08 17:38:37 -080086 appImage := (generateProfile || module.ForceCreateAppImage || global.DefaultAppImages) &&
87 !module.NoCreateAppImage
88
89 generateDM := shouldGenerateDM(module, global)
90
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000091 for archIdx, _ := range module.Archs {
Martin Stjernholm75a48d82020-01-10 20:32:59 +000092 dexpreoptCommand(ctx, globalSoong, global, module, rule, archIdx, profile, appImage, generateDM)
Colin Crosscbed6572019-01-08 17:38:37 -080093 }
94 }
95 }
96
97 return rule, nil
98}
99
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000100func dexpreoptDisabled(ctx android.PathContext, global *GlobalConfig, module *ModuleConfig) bool {
Colin Crosscbed6572019-01-08 17:38:37 -0800101 if contains(global.DisablePreoptModules, module.Name) {
102 return true
Colin Cross43f08db2018-11-12 10:13:39 -0800103 }
104
Ulyana Trafimovichf2cb7e92019-11-27 12:26:49 +0000105 // Don't preopt system server jars that are updatable.
106 for _, p := range global.UpdatableSystemServerJars {
Ulya Trafimovich8640ab92020-05-11 18:06:15 +0100107 if _, jar := android.SplitApexJarPair(ctx, p); jar == module.Name {
Ulyana Trafimovichf2cb7e92019-11-27 12:26:49 +0000108 return true
109 }
110 }
111
Colin Cross43f08db2018-11-12 10:13:39 -0800112 // If OnlyPreoptBootImageAndSystemServer=true and module is not in boot class path skip
113 // Also preopt system server jars since selinux prevents system server from loading anything from
114 // /data. If we don't do this they will need to be extracted which is not favorable for RAM usage
115 // or performance. If PreoptExtractedApk is true, we ignore the only preopt boot image options.
Ulya Trafimovich8640ab92020-05-11 18:06:15 +0100116 if global.OnlyPreoptBootImageAndSystemServer && !contains(android.GetJarsFromApexJarPairs(ctx, global.BootJars), module.Name) &&
Colin Cross43f08db2018-11-12 10:13:39 -0800117 !contains(global.SystemServerJars, module.Name) && !module.PreoptExtractedApk {
Colin Crosscbed6572019-01-08 17:38:37 -0800118 return true
Colin Cross43f08db2018-11-12 10:13:39 -0800119 }
120
Colin Crosscbed6572019-01-08 17:38:37 -0800121 return false
Colin Cross43f08db2018-11-12 10:13:39 -0800122}
123
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000124func profileCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig,
125 module *ModuleConfig, rule *android.RuleBuilder) android.WritablePath {
Colin Cross69f59a32019-02-15 10:39:37 -0800126
127 profilePath := module.BuildPath.InSameDir(ctx, "profile.prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800128 profileInstalledPath := module.DexLocation + ".prof"
129
130 if !module.ProfileIsTextListing {
131 rule.Command().FlagWithOutput("touch ", profilePath)
132 }
133
134 cmd := rule.Command().
135 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000136 Tool(globalSoong.Profman)
Colin Cross43f08db2018-11-12 10:13:39 -0800137
138 if module.ProfileIsTextListing {
139 // The profile is a test listing of classes (used for framework jars).
140 // We need to generate the actual binary profile before being able to compile.
Colin Cross69f59a32019-02-15 10:39:37 -0800141 cmd.FlagWithInput("--create-profile-from=", module.ProfileClassListing.Path())
Colin Cross43f08db2018-11-12 10:13:39 -0800142 } else {
143 // The profile is binary profile (used for apps). Run it through profman to
144 // ensure the profile keys match the apk.
145 cmd.
146 Flag("--copy-and-update-profile-key").
Colin Cross69f59a32019-02-15 10:39:37 -0800147 FlagWithInput("--profile-file=", module.ProfileClassListing.Path())
Colin Cross43f08db2018-11-12 10:13:39 -0800148 }
149
150 cmd.
151 FlagWithInput("--apk=", module.DexPath).
152 Flag("--dex-location="+module.DexLocation).
153 FlagWithOutput("--reference-profile-file=", profilePath)
154
155 if !module.ProfileIsTextListing {
156 cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath))
157 }
158 rule.Install(profilePath, profileInstalledPath)
159
160 return profilePath
161}
162
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000163func bootProfileCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig,
164 module *ModuleConfig, rule *android.RuleBuilder) android.WritablePath {
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100165
166 profilePath := module.BuildPath.InSameDir(ctx, "profile.bprof")
167 profileInstalledPath := module.DexLocation + ".bprof"
168
169 if !module.ProfileIsTextListing {
170 rule.Command().FlagWithOutput("touch ", profilePath)
171 }
172
173 cmd := rule.Command().
174 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000175 Tool(globalSoong.Profman)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100176
177 // The profile is a test listing of methods.
178 // We need to generate the actual binary profile.
179 cmd.FlagWithInput("--create-profile-from=", module.ProfileBootListing.Path())
180
181 cmd.
182 Flag("--generate-boot-profile").
183 FlagWithInput("--apk=", module.DexPath).
184 Flag("--dex-location="+module.DexLocation).
185 FlagWithOutput("--reference-profile-file=", profilePath)
186
187 if !module.ProfileIsTextListing {
188 cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath))
189 }
190 rule.Install(profilePath, profileInstalledPath)
191
192 return profilePath
193}
194
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000195func dexpreoptCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig,
196 module *ModuleConfig, rule *android.RuleBuilder, archIdx int, profile android.WritablePath,
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000197 appImage bool, generateDM bool) {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000198
199 arch := module.Archs[archIdx]
Colin Cross43f08db2018-11-12 10:13:39 -0800200
201 // HACK: make soname in Soong-generated .odex files match Make.
202 base := filepath.Base(module.DexLocation)
203 if filepath.Ext(base) == ".jar" {
204 base = "javalib.jar"
205 } else if filepath.Ext(base) == ".apk" {
206 base = "package.apk"
207 }
208
209 toOdexPath := func(path string) string {
210 return filepath.Join(
211 filepath.Dir(path),
212 "oat",
Colin Cross74ba9622019-02-11 15:11:14 -0800213 arch.String(),
Colin Cross43f08db2018-11-12 10:13:39 -0800214 pathtools.ReplaceExtension(filepath.Base(path), "odex"))
215 }
216
Colin Cross69f59a32019-02-15 10:39:37 -0800217 odexPath := module.BuildPath.InSameDir(ctx, "oat", arch.String(), pathtools.ReplaceExtension(base, "odex"))
Colin Cross43f08db2018-11-12 10:13:39 -0800218 odexInstallPath := toOdexPath(module.DexLocation)
219 if odexOnSystemOther(module, global) {
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100220 odexInstallPath = filepath.Join(SystemOtherPartition, odexInstallPath)
Colin Cross43f08db2018-11-12 10:13:39 -0800221 }
222
Colin Cross69f59a32019-02-15 10:39:37 -0800223 vdexPath := odexPath.ReplaceExtension(ctx, "vdex")
Colin Cross43f08db2018-11-12 10:13:39 -0800224 vdexInstallPath := pathtools.ReplaceExtension(odexInstallPath, "vdex")
225
Colin Cross69f59a32019-02-15 10:39:37 -0800226 invocationPath := odexPath.ReplaceExtension(ctx, "invocation")
Alex Light5de41962018-12-18 15:16:26 -0800227
Ulya Trafimovichdacc6c52020-03-11 11:59:34 +0000228 systemServerJars := NonUpdatableSystemServerJars(ctx, global)
229
Colin Cross43f08db2018-11-12 10:13:39 -0800230 // The class loader context using paths in the build
Colin Cross69f59a32019-02-15 10:39:37 -0800231 var classLoaderContextHost android.Paths
Colin Cross43f08db2018-11-12 10:13:39 -0800232
233 // The class loader context using paths as they will be on the device
234 var classLoaderContextTarget []string
235
236 // Extra paths that will be appended to the class loader if the APK manifest has targetSdkVersion < 28
Colin Cross69f59a32019-02-15 10:39:37 -0800237 var conditionalClassLoaderContextHost28 android.Paths
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000238 var conditionalClassLoaderContextTarget28 []string
239
240 // Extra paths that will be appended to the class loader if the APK manifest has targetSdkVersion < 29
Colin Cross69f59a32019-02-15 10:39:37 -0800241 var conditionalClassLoaderContextHost29 android.Paths
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000242 var conditionalClassLoaderContextTarget29 []string
Colin Cross43f08db2018-11-12 10:13:39 -0800243
Ulya Trafimovichc9af5382020-05-29 15:35:06 +0100244 // Extra paths that will be appended to the class loader if the APK manifest has targetSdkVersion < 30
245 var conditionalClassLoaderContextHost30 android.Paths
246 var conditionalClassLoaderContextTarget30 []string
247
Ulya Trafimovichdacc6c52020-03-11 11:59:34 +0000248 // A flag indicating if the '&' class loader context is used.
249 unknownClassLoaderContext := false
Colin Cross69f59a32019-02-15 10:39:37 -0800250
Colin Cross43f08db2018-11-12 10:13:39 -0800251 if module.EnforceUsesLibraries {
Colin Cross50ddcc42019-05-16 12:28:22 -0700252 usesLibs := append(copyOf(module.UsesLibraries), module.PresentOptionalUsesLibraries...)
Colin Cross43f08db2018-11-12 10:13:39 -0800253
254 // Create class loader context for dex2oat from uses libraries and filtered optional libraries
Colin Cross50ddcc42019-05-16 12:28:22 -0700255 for _, l := range usesLibs {
Colin Cross43f08db2018-11-12 10:13:39 -0800256
257 classLoaderContextHost = append(classLoaderContextHost,
258 pathForLibrary(module, l))
259 classLoaderContextTarget = append(classLoaderContextTarget,
260 filepath.Join("/system/framework", l+".jar"))
261 }
262
Ulya Trafimovichdf00dde2020-05-29 14:55:02 +0100263 // org.apache.http.legacy contains classes that were in the default classpath until API 28.
264 // If the targetSdkVersion in the manifest or APK is < 28, and the module does not explicitly
265 // depend on org.apache.http.legacy, then implicitly add it to the classpath for dexpreopt.
Colin Cross43f08db2018-11-12 10:13:39 -0800266 const httpLegacy = "org.apache.http.legacy"
Ulya Trafimovichdf00dde2020-05-29 14:55:02 +0100267 if !contains(usesLibs, httpLegacy) {
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000268 conditionalClassLoaderContextHost28 = append(conditionalClassLoaderContextHost28,
Ulya Trafimovichdf00dde2020-05-29 14:55:02 +0100269 pathForLibrary(module, httpLegacy))
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000270 conditionalClassLoaderContextTarget28 = append(conditionalClassLoaderContextTarget28,
Ulya Trafimovichdf00dde2020-05-29 14:55:02 +0100271 filepath.Join("/system/framework", httpLegacy+".jar"))
Colin Cross43f08db2018-11-12 10:13:39 -0800272 }
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000273
Colin Cross38b96852019-05-22 10:21:09 -0700274 // android.hidl.base-V1.0-java and android.hidl.manager-V1.0 contain classes that were in the default
275 // classpath until API 29. If the targetSdkVersion in the manifest or APK is < 29 then implicitly add
276 // the classes to the classpath for dexpreopt.
Ulya Trafimovichdf00dde2020-05-29 14:55:02 +0100277 const hidlBase = "android.hidl.base-V1.0-java"
278 const hidlManager = "android.hidl.manager-V1.0-java"
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000279 conditionalClassLoaderContextHost29 = append(conditionalClassLoaderContextHost29,
Alex Light5de41962018-12-18 15:16:26 -0800280 pathForLibrary(module, hidlManager))
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000281 conditionalClassLoaderContextTarget29 = append(conditionalClassLoaderContextTarget29,
282 filepath.Join("/system/framework", hidlManager+".jar"))
283 conditionalClassLoaderContextHost29 = append(conditionalClassLoaderContextHost29,
Alex Light5de41962018-12-18 15:16:26 -0800284 pathForLibrary(module, hidlBase))
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000285 conditionalClassLoaderContextTarget29 = append(conditionalClassLoaderContextTarget29,
286 filepath.Join("/system/framework", hidlBase+".jar"))
Ulya Trafimovichc9af5382020-05-29 15:35:06 +0100287
288 // android.test.base contains classes that were in the default classpath until API 30.
289 // If the targetSdkVersion in the manifest or APK is < 30 then implicitly add it to the
290 // classpath for dexpreopt.
291 const testBase = "android.test.base"
292 if !contains(usesLibs, testBase) {
293 conditionalClassLoaderContextHost30 = append(conditionalClassLoaderContextHost30,
294 pathForLibrary(module, testBase))
295 conditionalClassLoaderContextTarget30 = append(conditionalClassLoaderContextTarget30,
296 filepath.Join("/system/framework", testBase+".jar"))
297 }
Ulya Trafimovichdacc6c52020-03-11 11:59:34 +0000298 } else if jarIndex := android.IndexList(module.Name, systemServerJars); jarIndex >= 0 {
299 // System server jars should be dexpreopted together: class loader context of each jar
300 // should include all preceding jars on the system server classpath.
301 for _, otherJar := range systemServerJars[:jarIndex] {
302 classLoaderContextHost = append(classLoaderContextHost, SystemServerDexJarHostPath(ctx, otherJar))
303 classLoaderContextTarget = append(classLoaderContextTarget, "/system/framework/"+otherJar+".jar")
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000304 }
305
Ulya Trafimovichdacc6c52020-03-11 11:59:34 +0000306 // Copy the system server jar to a predefined location where dex2oat will find it.
307 dexPathHost := SystemServerDexJarHostPath(ctx, module.Name)
308 rule.Command().Text("mkdir -p").Flag(filepath.Dir(dexPathHost.String()))
309 rule.Command().Text("cp -f").Input(module.DexPath).Output(dexPathHost)
Colin Cross43f08db2018-11-12 10:13:39 -0800310 } else {
311 // Pass special class loader context to skip the classpath and collision check.
312 // This will get removed once LOCAL_USES_LIBRARIES is enforced.
313 // Right now LOCAL_USES_LIBRARIES is opt in, for the case where it's not specified we still default
314 // to the &.
Ulya Trafimovichdacc6c52020-03-11 11:59:34 +0000315 unknownClassLoaderContext = true
Colin Cross43f08db2018-11-12 10:13:39 -0800316 }
317
Colin Cross69f59a32019-02-15 10:39:37 -0800318 rule.Command().FlagWithArg("mkdir -p ", filepath.Dir(odexPath.String()))
Colin Cross43f08db2018-11-12 10:13:39 -0800319 rule.Command().FlagWithOutput("rm -f ", odexPath)
320 // Set values in the environment of the rule. These may be modified by construct_context.sh.
Ulya Trafimovichdacc6c52020-03-11 11:59:34 +0000321 if unknownClassLoaderContext {
322 rule.Command().
323 Text(`class_loader_context_arg=--class-loader-context=\&`).
324 Text(`stored_class_loader_context_arg=""`)
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000325 } else {
Ulya Trafimovichdacc6c52020-03-11 11:59:34 +0000326 rule.Command().
327 Text("class_loader_context_arg=--class-loader-context=PCL[" + strings.Join(classLoaderContextHost.Strings(), ":") + "]").
328 Implicits(classLoaderContextHost).
329 Text("stored_class_loader_context_arg=--stored-class-loader-context=PCL[" + strings.Join(classLoaderContextTarget, ":") + "]")
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000330 }
Colin Cross43f08db2018-11-12 10:13:39 -0800331
332 if module.EnforceUsesLibraries {
Colin Cross38b96852019-05-22 10:21:09 -0700333 if module.ManifestPath != nil {
334 rule.Command().Text(`target_sdk_version="$(`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000335 Tool(globalSoong.ManifestCheck).
Colin Cross38b96852019-05-22 10:21:09 -0700336 Flag("--extract-target-sdk-version").
337 Input(module.ManifestPath).
338 Text(`)"`)
339 } else {
340 // No manifest to extract targetSdkVersion from, hope that DexJar is an APK
341 rule.Command().Text(`target_sdk_version="$(`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000342 Tool(globalSoong.Aapt).
Colin Cross38b96852019-05-22 10:21:09 -0700343 Flag("dump badging").
344 Input(module.DexPath).
345 Text(`| grep "targetSdkVersion" | sed -n "s/targetSdkVersion:'\(.*\)'/\1/p"`).
346 Text(`)"`)
347 }
Colin Cross69f59a32019-02-15 10:39:37 -0800348 rule.Command().Textf(`dex_preopt_host_libraries="%s"`,
349 strings.Join(classLoaderContextHost.Strings(), " ")).
350 Implicits(classLoaderContextHost)
351 rule.Command().Textf(`dex_preopt_target_libraries="%s"`,
352 strings.Join(classLoaderContextTarget, " "))
353 rule.Command().Textf(`conditional_host_libs_28="%s"`,
354 strings.Join(conditionalClassLoaderContextHost28.Strings(), " ")).
355 Implicits(conditionalClassLoaderContextHost28)
356 rule.Command().Textf(`conditional_target_libs_28="%s"`,
357 strings.Join(conditionalClassLoaderContextTarget28, " "))
358 rule.Command().Textf(`conditional_host_libs_29="%s"`,
359 strings.Join(conditionalClassLoaderContextHost29.Strings(), " ")).
360 Implicits(conditionalClassLoaderContextHost29)
361 rule.Command().Textf(`conditional_target_libs_29="%s"`,
362 strings.Join(conditionalClassLoaderContextTarget29, " "))
Ulya Trafimovichc9af5382020-05-29 15:35:06 +0100363 rule.Command().Textf(`conditional_host_libs_30="%s"`,
364 strings.Join(conditionalClassLoaderContextHost30.Strings(), " ")).
365 Implicits(conditionalClassLoaderContextHost30)
366 rule.Command().Textf(`conditional_target_libs_30="%s"`,
367 strings.Join(conditionalClassLoaderContextTarget30, " "))
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000368 rule.Command().Text("source").Tool(globalSoong.ConstructContext).Input(module.DexPath)
Colin Cross43f08db2018-11-12 10:13:39 -0800369 }
370
Nicolas Geoffray2464ef42019-03-05 14:07:07 +0000371 // Devices that do not have a product partition use a symlink from /product to /system/product.
372 // Because on-device dexopt will see dex locations starting with /product, we change the paths
373 // to mimic this behavior.
374 dexLocationArg := module.DexLocation
375 if strings.HasPrefix(dexLocationArg, "/system/product/") {
376 dexLocationArg = strings.TrimPrefix(dexLocationArg, "/system")
377 }
378
Colin Cross43f08db2018-11-12 10:13:39 -0800379 cmd := rule.Command().
380 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000381 Tool(globalSoong.Dex2oat).
Colin Cross43f08db2018-11-12 10:13:39 -0800382 Flag("--avoid-storing-invocation").
Alex Light5de41962018-12-18 15:16:26 -0800383 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross43f08db2018-11-12 10:13:39 -0800384 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatXms).
385 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatXmx).
Colin Cross800fe132019-02-11 14:21:24 -0800386 Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", module.PreoptBootClassPathDexFiles, ":").
387 Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", module.PreoptBootClassPathDexLocations, ":").
Colin Cross43f08db2018-11-12 10:13:39 -0800388 Flag("${class_loader_context_arg}").
Ulya Trafimovichdacc6c52020-03-11 11:59:34 +0000389 Flag("${stored_class_loader_context_arg}").
Ulya Trafimovich3391a1e2020-01-03 17:33:17 +0000390 FlagWithArg("--boot-image=", strings.Join(module.DexPreoptImageLocations, ":")).Implicits(module.DexPreoptImagesDeps[archIdx].Paths()).
Colin Cross43f08db2018-11-12 10:13:39 -0800391 FlagWithInput("--dex-file=", module.DexPath).
Nicolas Geoffray2464ef42019-03-05 14:07:07 +0000392 FlagWithArg("--dex-location=", dexLocationArg).
Colin Cross43f08db2018-11-12 10:13:39 -0800393 FlagWithOutput("--oat-file=", odexPath).ImplicitOutput(vdexPath).
394 // Pass an empty directory, dex2oat shouldn't be reading arbitrary files
395 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross74ba9622019-02-11 15:11:14 -0800396 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross43f08db2018-11-12 10:13:39 -0800397 FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]).
398 FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]).
399 Flag("--no-generate-debug-info").
400 Flag("--generate-build-id").
401 Flag("--abort-on-hard-verifier-error").
402 Flag("--force-determinism").
403 FlagWithArg("--no-inline-from=", "core-oj.jar")
404
405 var preoptFlags []string
406 if len(module.PreoptFlags) > 0 {
407 preoptFlags = module.PreoptFlags
408 } else if len(global.PreoptFlags) > 0 {
409 preoptFlags = global.PreoptFlags
410 }
411
412 if len(preoptFlags) > 0 {
413 cmd.Text(strings.Join(preoptFlags, " "))
414 }
415
416 if module.UncompressedDex {
417 cmd.FlagWithArg("--copy-dex-files=", "false")
418 }
419
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800420 if !android.PrefixInList(preoptFlags, "--compiler-filter=") {
Colin Cross43f08db2018-11-12 10:13:39 -0800421 var compilerFilter string
422 if contains(global.SystemServerJars, module.Name) {
423 // Jars of system server, use the product option if it is set, speed otherwise.
424 if global.SystemServerCompilerFilter != "" {
425 compilerFilter = global.SystemServerCompilerFilter
426 } else {
427 compilerFilter = "speed"
428 }
429 } else if contains(global.SpeedApps, module.Name) || contains(global.SystemServerApps, module.Name) {
430 // Apps loaded into system server, and apps the product default to being compiled with the
431 // 'speed' compiler filter.
432 compilerFilter = "speed"
Colin Cross69f59a32019-02-15 10:39:37 -0800433 } else if profile != nil {
Colin Cross43f08db2018-11-12 10:13:39 -0800434 // For non system server jars, use speed-profile when we have a profile.
435 compilerFilter = "speed-profile"
436 } else if global.DefaultCompilerFilter != "" {
437 compilerFilter = global.DefaultCompilerFilter
438 } else {
439 compilerFilter = "quicken"
440 }
441 cmd.FlagWithArg("--compiler-filter=", compilerFilter)
442 }
443
444 if generateDM {
445 cmd.FlagWithArg("--copy-dex-files=", "false")
Colin Cross69f59a32019-02-15 10:39:37 -0800446 dmPath := module.BuildPath.InSameDir(ctx, "generated.dm")
Colin Cross43f08db2018-11-12 10:13:39 -0800447 dmInstalledPath := pathtools.ReplaceExtension(module.DexLocation, "dm")
Colin Cross69f59a32019-02-15 10:39:37 -0800448 tmpPath := module.BuildPath.InSameDir(ctx, "primary.vdex")
Colin Cross43f08db2018-11-12 10:13:39 -0800449 rule.Command().Text("cp -f").Input(vdexPath).Output(tmpPath)
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000450 rule.Command().Tool(globalSoong.SoongZip).
Colin Cross43f08db2018-11-12 10:13:39 -0800451 FlagWithArg("-L", "9").
452 FlagWithOutput("-o", dmPath).
453 Flag("-j").
454 Input(tmpPath)
455 rule.Install(dmPath, dmInstalledPath)
456 }
457
458 // By default, emit debug info.
459 debugInfo := true
460 if global.NoDebugInfo {
461 // If the global setting suppresses mini-debug-info, disable it.
462 debugInfo = false
463 }
464
465 // PRODUCT_SYSTEM_SERVER_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO.
466 // PRODUCT_OTHER_JAVA_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO.
467 if contains(global.SystemServerJars, module.Name) {
468 if global.AlwaysSystemServerDebugInfo {
469 debugInfo = true
470 } else if global.NeverSystemServerDebugInfo {
471 debugInfo = false
472 }
473 } else {
474 if global.AlwaysOtherDebugInfo {
475 debugInfo = true
476 } else if global.NeverOtherDebugInfo {
477 debugInfo = false
478 }
479 }
480
481 // Never enable on eng.
482 if global.IsEng {
483 debugInfo = false
484 }
485
486 if debugInfo {
487 cmd.Flag("--generate-mini-debug-info")
488 } else {
489 cmd.Flag("--no-generate-mini-debug-info")
490 }
491
492 // Set the compiler reason to 'prebuilt' to identify the oat files produced
493 // during the build, as opposed to compiled on the device.
494 cmd.FlagWithArg("--compilation-reason=", "prebuilt")
495
496 if appImage {
Colin Cross69f59a32019-02-15 10:39:37 -0800497 appImagePath := odexPath.ReplaceExtension(ctx, "art")
Colin Cross43f08db2018-11-12 10:13:39 -0800498 appImageInstallPath := pathtools.ReplaceExtension(odexInstallPath, "art")
499 cmd.FlagWithOutput("--app-image-file=", appImagePath).
500 FlagWithArg("--image-format=", "lz4")
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700501 if !global.DontResolveStartupStrings {
502 cmd.FlagWithArg("--resolve-startup-const-strings=", "true")
503 }
Colin Cross43f08db2018-11-12 10:13:39 -0800504 rule.Install(appImagePath, appImageInstallPath)
505 }
506
Colin Cross69f59a32019-02-15 10:39:37 -0800507 if profile != nil {
508 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross43f08db2018-11-12 10:13:39 -0800509 }
510
511 rule.Install(odexPath, odexInstallPath)
512 rule.Install(vdexPath, vdexInstallPath)
513}
514
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000515func shouldGenerateDM(module *ModuleConfig, global *GlobalConfig) bool {
Colin Cross43f08db2018-11-12 10:13:39 -0800516 // Generating DM files only makes sense for verify, avoid doing for non verify compiler filter APKs.
517 // No reason to use a dm file if the dex is already uncompressed.
518 return global.GenerateDMFiles && !module.UncompressedDex &&
519 contains(module.PreoptFlags, "--compiler-filter=verify")
520}
521
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000522func OdexOnSystemOtherByName(name string, dexLocation string, global *GlobalConfig) bool {
Colin Cross43f08db2018-11-12 10:13:39 -0800523 if !global.HasSystemOther {
524 return false
525 }
526
527 if global.SanitizeLite {
528 return false
529 }
530
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000531 if contains(global.SpeedApps, name) || contains(global.SystemServerApps, name) {
Colin Cross43f08db2018-11-12 10:13:39 -0800532 return false
533 }
534
535 for _, f := range global.PatternsOnSystemOther {
Anton Hanssond57bd3c2019-10-14 16:53:02 +0100536 if makefileMatch(filepath.Join(SystemPartition, f), dexLocation) {
Colin Cross43f08db2018-11-12 10:13:39 -0800537 return true
538 }
539 }
540
541 return false
542}
543
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000544func odexOnSystemOther(module *ModuleConfig, global *GlobalConfig) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000545 return OdexOnSystemOtherByName(module.Name, module.DexLocation, global)
546}
547
Colin Crossc7e40aa2019-02-08 21:37:00 -0800548// PathToLocation converts .../system/framework/arm64/boot.art to .../system/framework/boot.art
Colin Cross69f59a32019-02-15 10:39:37 -0800549func PathToLocation(path android.Path, arch android.ArchType) string {
550 pathArch := filepath.Base(filepath.Dir(path.String()))
Colin Cross74ba9622019-02-11 15:11:14 -0800551 if pathArch != arch.String() {
552 panic(fmt.Errorf("last directory in %q must be %q", path, arch.String()))
Colin Crossc7e40aa2019-02-08 21:37:00 -0800553 }
Colin Cross69f59a32019-02-15 10:39:37 -0800554 return filepath.Join(filepath.Dir(filepath.Dir(path.String())), filepath.Base(path.String()))
Colin Crossc7e40aa2019-02-08 21:37:00 -0800555}
556
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000557func pathForLibrary(module *ModuleConfig, lib string) android.Path {
Colin Cross69f59a32019-02-15 10:39:37 -0800558 path, ok := module.LibraryPaths[lib]
559 if !ok {
Colin Cross43f08db2018-11-12 10:13:39 -0800560 panic(fmt.Errorf("unknown library path for %q", lib))
561 }
562 return path
563}
564
565func 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
Ulyana Trafimovichf2cb7e92019-11-27 12:26:49 +0000577// Expected format for apexJarValue = <apex name>:<jar name>
Ulya Trafimovich8640ab92020-05-11 18:06:15 +0100578func GetJarLocationFromApexJarPair(ctx android.PathContext, apexJarValue string) string {
579 apex, jar := android.SplitApexJarPair(ctx, apexJarValue)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000580 return filepath.Join("/apex", apex, "javalib", jar+".jar")
Roshan Piusccc26ef2019-11-27 09:37:46 -0800581}
582
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000583var nonUpdatableSystemServerJarsKey = android.NewOnceKey("nonUpdatableSystemServerJars")
584
585// TODO: eliminate the superficial global config parameter by moving global config definition
586// from java subpackage to dexpreopt.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000587func NonUpdatableSystemServerJars(ctx android.PathContext, global *GlobalConfig) []string {
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000588 return ctx.Config().Once(nonUpdatableSystemServerJarsKey, func() interface{} {
589 return android.RemoveListFromList(global.SystemServerJars,
Ulya Trafimovich8640ab92020-05-11 18:06:15 +0100590 android.GetJarsFromApexJarPairs(ctx, global.UpdatableSystemServerJars))
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000591 }).([]string)
592}
593
Ulya Trafimovichdacc6c52020-03-11 11:59:34 +0000594// A predefined location for the system server dex jars. This is needed in order to generate
595// class loader context for dex2oat, as the path to the jar in the Soong module may be unknown
596// at that time (Soong processes the jars in dependency order, which may be different from the
597// the system server classpath order).
598func SystemServerDexJarHostPath(ctx android.PathContext, jar string) android.OutputPath {
Ulya Trafimovich6cf2c0c2020-04-24 12:15:20 +0100599 if DexpreoptRunningInSoong {
600 // Soong module, just use the default output directory $OUT/soong.
601 return android.PathForOutput(ctx, "system_server_dexjars", jar+".jar")
602 } else {
603 // Make module, default output directory is $OUT (passed via the "null config" created
604 // by dexpreopt_gen). Append Soong subdirectory to match Soong module paths.
605 return android.PathForOutput(ctx, "soong", "system_server_dexjars", jar+".jar")
606 }
Ulya Trafimovichdacc6c52020-03-11 11:59:34 +0000607}
608
Colin Cross43f08db2018-11-12 10:13:39 -0800609func contains(l []string, s string) bool {
610 for _, e := range l {
611 if e == s {
612 return true
613 }
614 }
615 return false
616}
617
Colin Cross454c0872019-02-15 23:03:34 -0800618var copyOf = android.CopyOf