blob: 9b0e7a5d892e81895cfc9c940d92064869ed759a [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
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +000044 "github.com/google/blueprint"
Colin Cross43f08db2018-11-12 10:13:39 -080045 "github.com/google/blueprint/pathtools"
46)
47
48const SystemPartition = "/system/"
49const SystemOtherPartition = "/system_other/"
50
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +000051type dependencyTag struct {
52 blueprint.BaseDependencyTag
53 name string
54}
55
56var SystemServerDepTag = dependencyTag{name: "system-server-dep"}
57var SystemServerForcedDepTag = dependencyTag{name: "system-server-forced-dep"}
58
Colin Cross43f08db2018-11-12 10:13:39 -080059// GenerateDexpreoptRule generates a set of commands that will preopt a module based on a GlobalConfig and a
60// ModuleConfig. The produced files and their install locations will be available through rule.Installs().
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000061func GenerateDexpreoptRule(ctx android.PathContext, globalSoong *GlobalSoongConfig,
62 global *GlobalConfig, module *ModuleConfig) (rule *android.RuleBuilder, err error) {
Colin Cross69f59a32019-02-15 10:39:37 -080063
Colin Cross43f08db2018-11-12 10:13:39 -080064 defer func() {
65 if r := recover(); r != nil {
Colin Cross69f59a32019-02-15 10:39:37 -080066 if _, ok := r.(runtime.Error); ok {
67 panic(r)
68 } else if e, ok := r.(error); ok {
Colin Cross43f08db2018-11-12 10:13:39 -080069 err = e
70 rule = nil
71 } else {
72 panic(r)
73 }
74 }
75 }()
76
Colin Cross758290d2019-02-01 16:42:32 -080077 rule = android.NewRuleBuilder()
Colin Cross43f08db2018-11-12 10:13:39 -080078
Colin Cross69f59a32019-02-15 10:39:37 -080079 generateProfile := module.ProfileClassListing.Valid() && !global.DisableGenerateProfile
Nicolas Geoffraye7102422019-07-24 13:19:29 +010080 generateBootProfile := module.ProfileBootListing.Valid() && !global.DisableGenerateProfile
Colin Cross43f08db2018-11-12 10:13:39 -080081
Colin Cross69f59a32019-02-15 10:39:37 -080082 var profile android.WritablePath
Colin Crosscbed6572019-01-08 17:38:37 -080083 if generateProfile {
Martin Stjernholm75a48d82020-01-10 20:32:59 +000084 profile = profileCommand(ctx, globalSoong, global, module, rule)
Colin Crosscbed6572019-01-08 17:38:37 -080085 }
Nicolas Geoffraye7102422019-07-24 13:19:29 +010086 if generateBootProfile {
Martin Stjernholm75a48d82020-01-10 20:32:59 +000087 bootProfileCommand(ctx, globalSoong, global, module, rule)
Nicolas Geoffraye7102422019-07-24 13:19:29 +010088 }
Colin Crosscbed6572019-01-08 17:38:37 -080089
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +000090 if !dexpreoptDisabled(ctx, global, module) {
Colin Crosscbed6572019-01-08 17:38:37 -080091 // Don't preopt individual boot jars, they will be preopted together.
Colin Crosscbed6572019-01-08 17:38:37 -080092 if !contains(global.BootJars, module.Name) {
93 appImage := (generateProfile || module.ForceCreateAppImage || global.DefaultAppImages) &&
94 !module.NoCreateAppImage
95
96 generateDM := shouldGenerateDM(module, global)
97
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000098 for archIdx, _ := range module.Archs {
Martin Stjernholm75a48d82020-01-10 20:32:59 +000099 dexpreoptCommand(ctx, globalSoong, global, module, rule, archIdx, profile, appImage, generateDM)
Colin Crosscbed6572019-01-08 17:38:37 -0800100 }
101 }
102 }
103
104 return rule, nil
105}
106
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000107func dexpreoptDisabled(ctx android.PathContext, global *GlobalConfig, module *ModuleConfig) bool {
Colin Crosscbed6572019-01-08 17:38:37 -0800108 if contains(global.DisablePreoptModules, module.Name) {
109 return true
Colin Cross43f08db2018-11-12 10:13:39 -0800110 }
111
Ulyana Trafimovichf2cb7e92019-11-27 12:26:49 +0000112 // Don't preopt system server jars that are updatable.
113 for _, p := range global.UpdatableSystemServerJars {
Ulya Trafimovich4cdada22020-02-10 15:29:28 +0000114 if _, jar := android.SplitApexJarPair(p); jar == module.Name {
Ulyana Trafimovichf2cb7e92019-11-27 12:26:49 +0000115 return true
116 }
117 }
118
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000119 // Don't preopt system server jars that are not Soong modules.
120 if android.InList(module.Name, NonUpdatableSystemServerJars(ctx, global)) {
121 if _, ok := ctx.(android.ModuleContext); !ok {
122 return true
123 }
124 }
125
Colin Cross43f08db2018-11-12 10:13:39 -0800126 // If OnlyPreoptBootImageAndSystemServer=true and module is not in boot class path skip
127 // Also preopt system server jars since selinux prevents system server from loading anything from
128 // /data. If we don't do this they will need to be extracted which is not favorable for RAM usage
129 // or performance. If PreoptExtractedApk is true, we ignore the only preopt boot image options.
130 if global.OnlyPreoptBootImageAndSystemServer && !contains(global.BootJars, module.Name) &&
131 !contains(global.SystemServerJars, module.Name) && !module.PreoptExtractedApk {
Colin Crosscbed6572019-01-08 17:38:37 -0800132 return true
Colin Cross43f08db2018-11-12 10:13:39 -0800133 }
134
Colin Crosscbed6572019-01-08 17:38:37 -0800135 return false
Colin Cross43f08db2018-11-12 10:13:39 -0800136}
137
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000138func profileCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig,
139 module *ModuleConfig, rule *android.RuleBuilder) android.WritablePath {
Colin Cross69f59a32019-02-15 10:39:37 -0800140
141 profilePath := module.BuildPath.InSameDir(ctx, "profile.prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800142 profileInstalledPath := module.DexLocation + ".prof"
143
144 if !module.ProfileIsTextListing {
145 rule.Command().FlagWithOutput("touch ", profilePath)
146 }
147
148 cmd := rule.Command().
149 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000150 Tool(globalSoong.Profman)
Colin Cross43f08db2018-11-12 10:13:39 -0800151
152 if module.ProfileIsTextListing {
153 // The profile is a test listing of classes (used for framework jars).
154 // We need to generate the actual binary profile before being able to compile.
Colin Cross69f59a32019-02-15 10:39:37 -0800155 cmd.FlagWithInput("--create-profile-from=", module.ProfileClassListing.Path())
Colin Cross43f08db2018-11-12 10:13:39 -0800156 } else {
157 // The profile is binary profile (used for apps). Run it through profman to
158 // ensure the profile keys match the apk.
159 cmd.
160 Flag("--copy-and-update-profile-key").
Colin Cross69f59a32019-02-15 10:39:37 -0800161 FlagWithInput("--profile-file=", module.ProfileClassListing.Path())
Colin Cross43f08db2018-11-12 10:13:39 -0800162 }
163
164 cmd.
165 FlagWithInput("--apk=", module.DexPath).
166 Flag("--dex-location="+module.DexLocation).
167 FlagWithOutput("--reference-profile-file=", profilePath)
168
169 if !module.ProfileIsTextListing {
170 cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath))
171 }
172 rule.Install(profilePath, profileInstalledPath)
173
174 return profilePath
175}
176
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000177func bootProfileCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig,
178 module *ModuleConfig, rule *android.RuleBuilder) android.WritablePath {
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100179
180 profilePath := module.BuildPath.InSameDir(ctx, "profile.bprof")
181 profileInstalledPath := module.DexLocation + ".bprof"
182
183 if !module.ProfileIsTextListing {
184 rule.Command().FlagWithOutput("touch ", profilePath)
185 }
186
187 cmd := rule.Command().
188 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000189 Tool(globalSoong.Profman)
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100190
191 // The profile is a test listing of methods.
192 // We need to generate the actual binary profile.
193 cmd.FlagWithInput("--create-profile-from=", module.ProfileBootListing.Path())
194
195 cmd.
196 Flag("--generate-boot-profile").
197 FlagWithInput("--apk=", module.DexPath).
198 Flag("--dex-location="+module.DexLocation).
199 FlagWithOutput("--reference-profile-file=", profilePath)
200
201 if !module.ProfileIsTextListing {
202 cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath))
203 }
204 rule.Install(profilePath, profileInstalledPath)
205
206 return profilePath
207}
208
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000209func dexpreoptCommand(ctx android.PathContext, globalSoong *GlobalSoongConfig, global *GlobalConfig,
210 module *ModuleConfig, rule *android.RuleBuilder, archIdx int, profile android.WritablePath,
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000211 appImage bool, generateDM bool) {
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000212
213 arch := module.Archs[archIdx]
Colin Cross43f08db2018-11-12 10:13:39 -0800214
215 // HACK: make soname in Soong-generated .odex files match Make.
216 base := filepath.Base(module.DexLocation)
217 if filepath.Ext(base) == ".jar" {
218 base = "javalib.jar"
219 } else if filepath.Ext(base) == ".apk" {
220 base = "package.apk"
221 }
222
223 toOdexPath := func(path string) string {
224 return filepath.Join(
225 filepath.Dir(path),
226 "oat",
Colin Cross74ba9622019-02-11 15:11:14 -0800227 arch.String(),
Colin Cross43f08db2018-11-12 10:13:39 -0800228 pathtools.ReplaceExtension(filepath.Base(path), "odex"))
229 }
230
Colin Cross69f59a32019-02-15 10:39:37 -0800231 odexPath := module.BuildPath.InSameDir(ctx, "oat", arch.String(), pathtools.ReplaceExtension(base, "odex"))
Colin Cross43f08db2018-11-12 10:13:39 -0800232 odexInstallPath := toOdexPath(module.DexLocation)
233 if odexOnSystemOther(module, global) {
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100234 odexInstallPath = filepath.Join(SystemOtherPartition, odexInstallPath)
Colin Cross43f08db2018-11-12 10:13:39 -0800235 }
236
Colin Cross69f59a32019-02-15 10:39:37 -0800237 vdexPath := odexPath.ReplaceExtension(ctx, "vdex")
Colin Cross43f08db2018-11-12 10:13:39 -0800238 vdexInstallPath := pathtools.ReplaceExtension(odexInstallPath, "vdex")
239
Colin Cross69f59a32019-02-15 10:39:37 -0800240 invocationPath := odexPath.ReplaceExtension(ctx, "invocation")
Alex Light5de41962018-12-18 15:16:26 -0800241
Colin Cross43f08db2018-11-12 10:13:39 -0800242 // The class loader context using paths in the build
Colin Cross69f59a32019-02-15 10:39:37 -0800243 var classLoaderContextHost android.Paths
Colin Cross43f08db2018-11-12 10:13:39 -0800244
245 // The class loader context using paths as they will be on the device
246 var classLoaderContextTarget []string
247
248 // Extra paths that will be appended to the class loader if the APK manifest has targetSdkVersion < 28
Colin Cross69f59a32019-02-15 10:39:37 -0800249 var conditionalClassLoaderContextHost28 android.Paths
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000250 var conditionalClassLoaderContextTarget28 []string
251
252 // Extra paths that will be appended to the class loader if the APK manifest has targetSdkVersion < 29
Colin Cross69f59a32019-02-15 10:39:37 -0800253 var conditionalClassLoaderContextHost29 android.Paths
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000254 var conditionalClassLoaderContextTarget29 []string
Colin Cross43f08db2018-11-12 10:13:39 -0800255
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000256 var classLoaderContextHostString, classLoaderContextDeviceString string
257 var classLoaderDeps android.Paths
Colin Cross69f59a32019-02-15 10:39:37 -0800258
Colin Cross43f08db2018-11-12 10:13:39 -0800259 if module.EnforceUsesLibraries {
Colin Cross50ddcc42019-05-16 12:28:22 -0700260 usesLibs := append(copyOf(module.UsesLibraries), module.PresentOptionalUsesLibraries...)
Colin Cross43f08db2018-11-12 10:13:39 -0800261
262 // Create class loader context for dex2oat from uses libraries and filtered optional libraries
Colin Cross50ddcc42019-05-16 12:28:22 -0700263 for _, l := range usesLibs {
Colin Cross43f08db2018-11-12 10:13:39 -0800264
265 classLoaderContextHost = append(classLoaderContextHost,
266 pathForLibrary(module, l))
267 classLoaderContextTarget = append(classLoaderContextTarget,
268 filepath.Join("/system/framework", l+".jar"))
269 }
270
271 const httpLegacy = "org.apache.http.legacy"
272 const httpLegacyImpl = "org.apache.http.legacy.impl"
273
Colin Cross38b96852019-05-22 10:21:09 -0700274 // org.apache.http.legacy contains classes that were in the default classpath until API 28. If the
275 // targetSdkVersion in the manifest or APK is < 28, and the module does not explicitly depend on
276 // org.apache.http.legacy, then implicitly add the classes to the classpath for dexpreopt. One the
277 // device the classes will be in a file called org.apache.http.legacy.impl.jar.
Colin Cross50ddcc42019-05-16 12:28:22 -0700278 module.LibraryPaths[httpLegacyImpl] = module.LibraryPaths[httpLegacy]
279
280 if !contains(module.UsesLibraries, httpLegacy) && !contains(module.PresentOptionalUsesLibraries, httpLegacy) {
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000281 conditionalClassLoaderContextHost28 = append(conditionalClassLoaderContextHost28,
Colin Cross43f08db2018-11-12 10:13:39 -0800282 pathForLibrary(module, httpLegacyImpl))
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000283 conditionalClassLoaderContextTarget28 = append(conditionalClassLoaderContextTarget28,
Colin Cross43f08db2018-11-12 10:13:39 -0800284 filepath.Join("/system/framework", httpLegacyImpl+".jar"))
285 }
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000286
287 const hidlBase = "android.hidl.base-V1.0-java"
288 const hidlManager = "android.hidl.manager-V1.0-java"
289
Colin Cross38b96852019-05-22 10:21:09 -0700290 // android.hidl.base-V1.0-java and android.hidl.manager-V1.0 contain classes that were in the default
291 // classpath until API 29. If the targetSdkVersion in the manifest or APK is < 29 then implicitly add
292 // the classes to the classpath for dexpreopt.
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000293 conditionalClassLoaderContextHost29 = append(conditionalClassLoaderContextHost29,
Alex Light5de41962018-12-18 15:16:26 -0800294 pathForLibrary(module, hidlManager))
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000295 conditionalClassLoaderContextTarget29 = append(conditionalClassLoaderContextTarget29,
296 filepath.Join("/system/framework", hidlManager+".jar"))
297 conditionalClassLoaderContextHost29 = append(conditionalClassLoaderContextHost29,
Alex Light5de41962018-12-18 15:16:26 -0800298 pathForLibrary(module, hidlBase))
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000299 conditionalClassLoaderContextTarget29 = append(conditionalClassLoaderContextTarget29,
300 filepath.Join("/system/framework", hidlBase+".jar"))
Colin Cross69f59a32019-02-15 10:39:37 -0800301
302 classLoaderContextHostString = strings.Join(classLoaderContextHost.Strings(), ":")
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000303 } else if android.InList(module.Name, NonUpdatableSystemServerJars(ctx, global)) {
304 // We expect that all dexpreopted system server jars are Soong modules.
305 mctx, isModule := ctx.(android.ModuleContext)
306 if !isModule {
307 panic("Cannot dexpreopt system server jar that is not a soong module.")
308 }
309
310 // System server jars should be dexpreopted together: class loader context of each jar
311 // should include preceding jars (which can be found as dependencies of the current jar
312 // with a special tag).
313 var jarsOnHost android.Paths
314 var jarsOnDevice []string
315 mctx.VisitDirectDepsWithTag(SystemServerDepTag, func(dep android.Module) {
316 depName := mctx.OtherModuleName(dep)
317 if jar, ok := dep.(interface{ DexJar() android.Path }); ok {
318 jarsOnHost = append(jarsOnHost, jar.DexJar())
319 jarsOnDevice = append(jarsOnDevice, "/system/framework/"+depName+".jar")
320 } else {
321 mctx.ModuleErrorf("module \"%s\" is not a jar", depName)
322 }
323 })
324 classLoaderContextHostString = strings.Join(jarsOnHost.Strings(), ":")
325 classLoaderContextDeviceString = strings.Join(jarsOnDevice, ":")
326 classLoaderDeps = jarsOnHost
Colin Cross43f08db2018-11-12 10:13:39 -0800327 } else {
328 // Pass special class loader context to skip the classpath and collision check.
329 // This will get removed once LOCAL_USES_LIBRARIES is enforced.
330 // Right now LOCAL_USES_LIBRARIES is opt in, for the case where it's not specified we still default
331 // to the &.
Colin Cross69f59a32019-02-15 10:39:37 -0800332 classLoaderContextHostString = `\&`
Colin Cross43f08db2018-11-12 10:13:39 -0800333 }
334
Colin Cross69f59a32019-02-15 10:39:37 -0800335 rule.Command().FlagWithArg("mkdir -p ", filepath.Dir(odexPath.String()))
Colin Cross43f08db2018-11-12 10:13:39 -0800336 rule.Command().FlagWithOutput("rm -f ", odexPath)
337 // Set values in the environment of the rule. These may be modified by construct_context.sh.
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000338 if classLoaderContextHostString == `\&` {
339 rule.Command().Text(`class_loader_context_arg=--class-loader-context=\&`)
340 rule.Command().Text(`stored_class_loader_context_arg=""`)
341 } else {
342 rule.Command().Text("class_loader_context_arg=--class-loader-context=PCL[" + classLoaderContextHostString + "]")
343 rule.Command().Text("stored_class_loader_context_arg=--stored-class-loader-context=PCL[" + classLoaderContextDeviceString + "]")
344 }
Colin Cross43f08db2018-11-12 10:13:39 -0800345
346 if module.EnforceUsesLibraries {
Colin Cross38b96852019-05-22 10:21:09 -0700347 if module.ManifestPath != nil {
348 rule.Command().Text(`target_sdk_version="$(`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000349 Tool(globalSoong.ManifestCheck).
Colin Cross38b96852019-05-22 10:21:09 -0700350 Flag("--extract-target-sdk-version").
351 Input(module.ManifestPath).
352 Text(`)"`)
353 } else {
354 // No manifest to extract targetSdkVersion from, hope that DexJar is an APK
355 rule.Command().Text(`target_sdk_version="$(`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000356 Tool(globalSoong.Aapt).
Colin Cross38b96852019-05-22 10:21:09 -0700357 Flag("dump badging").
358 Input(module.DexPath).
359 Text(`| grep "targetSdkVersion" | sed -n "s/targetSdkVersion:'\(.*\)'/\1/p"`).
360 Text(`)"`)
361 }
Colin Cross69f59a32019-02-15 10:39:37 -0800362 rule.Command().Textf(`dex_preopt_host_libraries="%s"`,
363 strings.Join(classLoaderContextHost.Strings(), " ")).
364 Implicits(classLoaderContextHost)
365 rule.Command().Textf(`dex_preopt_target_libraries="%s"`,
366 strings.Join(classLoaderContextTarget, " "))
367 rule.Command().Textf(`conditional_host_libs_28="%s"`,
368 strings.Join(conditionalClassLoaderContextHost28.Strings(), " ")).
369 Implicits(conditionalClassLoaderContextHost28)
370 rule.Command().Textf(`conditional_target_libs_28="%s"`,
371 strings.Join(conditionalClassLoaderContextTarget28, " "))
372 rule.Command().Textf(`conditional_host_libs_29="%s"`,
373 strings.Join(conditionalClassLoaderContextHost29.Strings(), " ")).
374 Implicits(conditionalClassLoaderContextHost29)
375 rule.Command().Textf(`conditional_target_libs_29="%s"`,
376 strings.Join(conditionalClassLoaderContextTarget29, " "))
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000377 rule.Command().Text("source").Tool(globalSoong.ConstructContext).Input(module.DexPath)
Colin Cross43f08db2018-11-12 10:13:39 -0800378 }
379
Nicolas Geoffray2464ef42019-03-05 14:07:07 +0000380 // Devices that do not have a product partition use a symlink from /product to /system/product.
381 // Because on-device dexopt will see dex locations starting with /product, we change the paths
382 // to mimic this behavior.
383 dexLocationArg := module.DexLocation
384 if strings.HasPrefix(dexLocationArg, "/system/product/") {
385 dexLocationArg = strings.TrimPrefix(dexLocationArg, "/system")
386 }
387
Colin Cross43f08db2018-11-12 10:13:39 -0800388 cmd := rule.Command().
389 Text(`ANDROID_LOG_TAGS="*:e"`).
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000390 Tool(globalSoong.Dex2oat).
Colin Cross43f08db2018-11-12 10:13:39 -0800391 Flag("--avoid-storing-invocation").
Alex Light5de41962018-12-18 15:16:26 -0800392 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross43f08db2018-11-12 10:13:39 -0800393 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatXms).
394 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatXmx).
Colin Cross800fe132019-02-11 14:21:24 -0800395 Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", module.PreoptBootClassPathDexFiles, ":").
396 Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", module.PreoptBootClassPathDexLocations, ":").
Colin Cross43f08db2018-11-12 10:13:39 -0800397 Flag("${class_loader_context_arg}").
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000398 Flag("${stored_class_loader_context_arg}").Implicits(classLoaderDeps).
Ulya Trafimovich3391a1e2020-01-03 17:33:17 +0000399 FlagWithArg("--boot-image=", strings.Join(module.DexPreoptImageLocations, ":")).Implicits(module.DexPreoptImagesDeps[archIdx].Paths()).
Colin Cross43f08db2018-11-12 10:13:39 -0800400 FlagWithInput("--dex-file=", module.DexPath).
Nicolas Geoffray2464ef42019-03-05 14:07:07 +0000401 FlagWithArg("--dex-location=", dexLocationArg).
Colin Cross43f08db2018-11-12 10:13:39 -0800402 FlagWithOutput("--oat-file=", odexPath).ImplicitOutput(vdexPath).
403 // Pass an empty directory, dex2oat shouldn't be reading arbitrary files
404 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross74ba9622019-02-11 15:11:14 -0800405 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross43f08db2018-11-12 10:13:39 -0800406 FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]).
407 FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]).
408 Flag("--no-generate-debug-info").
409 Flag("--generate-build-id").
410 Flag("--abort-on-hard-verifier-error").
411 Flag("--force-determinism").
412 FlagWithArg("--no-inline-from=", "core-oj.jar")
413
414 var preoptFlags []string
415 if len(module.PreoptFlags) > 0 {
416 preoptFlags = module.PreoptFlags
417 } else if len(global.PreoptFlags) > 0 {
418 preoptFlags = global.PreoptFlags
419 }
420
421 if len(preoptFlags) > 0 {
422 cmd.Text(strings.Join(preoptFlags, " "))
423 }
424
425 if module.UncompressedDex {
426 cmd.FlagWithArg("--copy-dex-files=", "false")
427 }
428
429 if !anyHavePrefix(preoptFlags, "--compiler-filter=") {
430 var compilerFilter string
431 if contains(global.SystemServerJars, module.Name) {
432 // Jars of system server, use the product option if it is set, speed otherwise.
433 if global.SystemServerCompilerFilter != "" {
434 compilerFilter = global.SystemServerCompilerFilter
435 } else {
436 compilerFilter = "speed"
437 }
438 } else if contains(global.SpeedApps, module.Name) || contains(global.SystemServerApps, module.Name) {
439 // Apps loaded into system server, and apps the product default to being compiled with the
440 // 'speed' compiler filter.
441 compilerFilter = "speed"
Colin Cross69f59a32019-02-15 10:39:37 -0800442 } else if profile != nil {
Colin Cross43f08db2018-11-12 10:13:39 -0800443 // For non system server jars, use speed-profile when we have a profile.
444 compilerFilter = "speed-profile"
445 } else if global.DefaultCompilerFilter != "" {
446 compilerFilter = global.DefaultCompilerFilter
447 } else {
448 compilerFilter = "quicken"
449 }
450 cmd.FlagWithArg("--compiler-filter=", compilerFilter)
451 }
452
453 if generateDM {
454 cmd.FlagWithArg("--copy-dex-files=", "false")
Colin Cross69f59a32019-02-15 10:39:37 -0800455 dmPath := module.BuildPath.InSameDir(ctx, "generated.dm")
Colin Cross43f08db2018-11-12 10:13:39 -0800456 dmInstalledPath := pathtools.ReplaceExtension(module.DexLocation, "dm")
Colin Cross69f59a32019-02-15 10:39:37 -0800457 tmpPath := module.BuildPath.InSameDir(ctx, "primary.vdex")
Colin Cross43f08db2018-11-12 10:13:39 -0800458 rule.Command().Text("cp -f").Input(vdexPath).Output(tmpPath)
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000459 rule.Command().Tool(globalSoong.SoongZip).
Colin Cross43f08db2018-11-12 10:13:39 -0800460 FlagWithArg("-L", "9").
461 FlagWithOutput("-o", dmPath).
462 Flag("-j").
463 Input(tmpPath)
464 rule.Install(dmPath, dmInstalledPath)
465 }
466
467 // By default, emit debug info.
468 debugInfo := true
469 if global.NoDebugInfo {
470 // If the global setting suppresses mini-debug-info, disable it.
471 debugInfo = false
472 }
473
474 // PRODUCT_SYSTEM_SERVER_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO.
475 // PRODUCT_OTHER_JAVA_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO.
476 if contains(global.SystemServerJars, module.Name) {
477 if global.AlwaysSystemServerDebugInfo {
478 debugInfo = true
479 } else if global.NeverSystemServerDebugInfo {
480 debugInfo = false
481 }
482 } else {
483 if global.AlwaysOtherDebugInfo {
484 debugInfo = true
485 } else if global.NeverOtherDebugInfo {
486 debugInfo = false
487 }
488 }
489
490 // Never enable on eng.
491 if global.IsEng {
492 debugInfo = false
493 }
494
495 if debugInfo {
496 cmd.Flag("--generate-mini-debug-info")
497 } else {
498 cmd.Flag("--no-generate-mini-debug-info")
499 }
500
501 // Set the compiler reason to 'prebuilt' to identify the oat files produced
502 // during the build, as opposed to compiled on the device.
503 cmd.FlagWithArg("--compilation-reason=", "prebuilt")
504
505 if appImage {
Colin Cross69f59a32019-02-15 10:39:37 -0800506 appImagePath := odexPath.ReplaceExtension(ctx, "art")
Colin Cross43f08db2018-11-12 10:13:39 -0800507 appImageInstallPath := pathtools.ReplaceExtension(odexInstallPath, "art")
508 cmd.FlagWithOutput("--app-image-file=", appImagePath).
509 FlagWithArg("--image-format=", "lz4")
Mathieu Chartier3f7ddbb2019-04-29 09:33:50 -0700510 if !global.DontResolveStartupStrings {
511 cmd.FlagWithArg("--resolve-startup-const-strings=", "true")
512 }
Colin Cross43f08db2018-11-12 10:13:39 -0800513 rule.Install(appImagePath, appImageInstallPath)
514 }
515
Colin Cross69f59a32019-02-15 10:39:37 -0800516 if profile != nil {
517 cmd.FlagWithInput("--profile-file=", profile)
Colin Cross43f08db2018-11-12 10:13:39 -0800518 }
519
520 rule.Install(odexPath, odexInstallPath)
521 rule.Install(vdexPath, vdexInstallPath)
522}
523
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000524func shouldGenerateDM(module *ModuleConfig, global *GlobalConfig) bool {
Colin Cross43f08db2018-11-12 10:13:39 -0800525 // Generating DM files only makes sense for verify, avoid doing for non verify compiler filter APKs.
526 // No reason to use a dm file if the dex is already uncompressed.
527 return global.GenerateDMFiles && !module.UncompressedDex &&
528 contains(module.PreoptFlags, "--compiler-filter=verify")
529}
530
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000531func OdexOnSystemOtherByName(name string, dexLocation string, global *GlobalConfig) bool {
Colin Cross43f08db2018-11-12 10:13:39 -0800532 if !global.HasSystemOther {
533 return false
534 }
535
536 if global.SanitizeLite {
537 return false
538 }
539
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000540 if contains(global.SpeedApps, name) || contains(global.SystemServerApps, name) {
Colin Cross43f08db2018-11-12 10:13:39 -0800541 return false
542 }
543
544 for _, f := range global.PatternsOnSystemOther {
Anton Hanssond57bd3c2019-10-14 16:53:02 +0100545 if makefileMatch(filepath.Join(SystemPartition, f), dexLocation) {
Colin Cross43f08db2018-11-12 10:13:39 -0800546 return true
547 }
548 }
549
550 return false
551}
552
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000553func odexOnSystemOther(module *ModuleConfig, global *GlobalConfig) bool {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000554 return OdexOnSystemOtherByName(module.Name, module.DexLocation, global)
555}
556
Colin Crossc7e40aa2019-02-08 21:37:00 -0800557// PathToLocation converts .../system/framework/arm64/boot.art to .../system/framework/boot.art
Colin Cross69f59a32019-02-15 10:39:37 -0800558func PathToLocation(path android.Path, arch android.ArchType) string {
559 pathArch := filepath.Base(filepath.Dir(path.String()))
Colin Cross74ba9622019-02-11 15:11:14 -0800560 if pathArch != arch.String() {
561 panic(fmt.Errorf("last directory in %q must be %q", path, arch.String()))
Colin Crossc7e40aa2019-02-08 21:37:00 -0800562 }
Colin Cross69f59a32019-02-15 10:39:37 -0800563 return filepath.Join(filepath.Dir(filepath.Dir(path.String())), filepath.Base(path.String()))
Colin Crossc7e40aa2019-02-08 21:37:00 -0800564}
565
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000566func pathForLibrary(module *ModuleConfig, lib string) android.Path {
Colin Cross69f59a32019-02-15 10:39:37 -0800567 path, ok := module.LibraryPaths[lib]
568 if !ok {
Colin Cross43f08db2018-11-12 10:13:39 -0800569 panic(fmt.Errorf("unknown library path for %q", lib))
570 }
571 return path
572}
573
574func makefileMatch(pattern, s string) bool {
575 percent := strings.IndexByte(pattern, '%')
576 switch percent {
577 case -1:
578 return pattern == s
579 case len(pattern) - 1:
580 return strings.HasPrefix(s, pattern[:len(pattern)-1])
581 default:
582 panic(fmt.Errorf("unsupported makefile pattern %q", pattern))
583 }
584}
585
Ulyana Trafimovichf2cb7e92019-11-27 12:26:49 +0000586// Expected format for apexJarValue = <apex name>:<jar name>
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000587func GetJarLocationFromApexJarPair(apexJarValue string) string {
Ulya Trafimovich4cdada22020-02-10 15:29:28 +0000588 apex, jar := android.SplitApexJarPair(apexJarValue)
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000589 return filepath.Join("/apex", apex, "javalib", jar+".jar")
Roshan Piusccc26ef2019-11-27 09:37:46 -0800590}
591
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000592func GetJarsFromApexJarPairs(apexJarPairs []string) []string {
593 modules := make([]string, len(apexJarPairs))
594 for i, p := range apexJarPairs {
595 _, jar := android.SplitApexJarPair(p)
596 modules[i] = jar
597 }
598 return modules
599}
600
601var nonUpdatableSystemServerJarsKey = android.NewOnceKey("nonUpdatableSystemServerJars")
602
603// TODO: eliminate the superficial global config parameter by moving global config definition
604// from java subpackage to dexpreopt.
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000605func NonUpdatableSystemServerJars(ctx android.PathContext, global *GlobalConfig) []string {
Ulya Trafimovichf3ff0102019-12-03 15:39:23 +0000606 return ctx.Config().Once(nonUpdatableSystemServerJarsKey, func() interface{} {
607 return android.RemoveListFromList(global.SystemServerJars,
608 GetJarsFromApexJarPairs(global.UpdatableSystemServerJars))
609 }).([]string)
610}
611
Colin Cross43f08db2018-11-12 10:13:39 -0800612func contains(l []string, s string) bool {
613 for _, e := range l {
614 if e == s {
615 return true
616 }
617 }
618 return false
619}
620
621// remove all elements in a from b, returning a new slice
622func filterOut(a []string, b []string) []string {
623 var ret []string
624 for _, x := range b {
625 if !contains(a, x) {
626 ret = append(ret, x)
627 }
628 }
629 return ret
630}
631
632func replace(l []string, from, to string) {
633 for i := range l {
634 if l[i] == from {
635 l[i] = to
636 }
637 }
638}
639
Colin Cross454c0872019-02-15 23:03:34 -0800640var copyOf = android.CopyOf
Colin Cross43f08db2018-11-12 10:13:39 -0800641
642func anyHavePrefix(l []string, prefix string) bool {
643 for _, x := range l {
644 if strings.HasPrefix(x, prefix) {
645 return true
646 }
647 }
648 return false
649}