blob: c378f096b4f202e6d81aa3e3fef6c5051a2229f0 [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
16// dexpreopting and to strip the dex files from the APK or JAR.
17//
18// It is used in two places; in the dexpeopt_gen binary for modules defined in Make, and directly linked into Soong.
19//
20// For Make modules it is built into the dexpreopt_gen binary, which is executed as a Make rule using global config and
21// module config specified in JSON files. The binary writes out two shell scripts, only updating them if they have
22// changed. One script takes an APK or JAR as an input and produces a zip file containing any outputs of preopting,
23// in the location they should be on the device. The Make build rules will unzip the zip file into $(PRODUCT_OUT) when
24// installing the APK, which will install the preopt outputs into $(PRODUCT_OUT)/system or $(PRODUCT_OUT)/system_other
25// as necessary. The zip file may be empty if preopting was disabled for any reason. The second script takes an APK or
26// JAR as an input and strips the dex files in it as necessary.
27//
28// The intermediate shell scripts allow changes to this package or to the global config to regenerate the shell scripts
29// but only require re-executing preopting if the script has changed.
30//
31// For Soong modules this package is linked directly into Soong and run from the java package. It generates the same
32// commands as for make, using athe same global config JSON file used by make, but using a module config structure
33// provided by Soong. The generated commands are then converted into Soong rule and written directly to the ninja file,
34// with no extra shell scripts involved.
35package dexpreopt
36
37import (
38 "fmt"
39 "path/filepath"
Colin Cross69f59a32019-02-15 10:39:37 -080040 "runtime"
Colin Cross43f08db2018-11-12 10:13:39 -080041 "strings"
42
Colin Crossfeec25b2019-01-30 17:32:39 -080043 "android/soong/android"
44
Colin Cross43f08db2018-11-12 10:13:39 -080045 "github.com/google/blueprint/pathtools"
46)
47
48const SystemPartition = "/system/"
49const SystemOtherPartition = "/system_other/"
50
51// GenerateStripRule generates a set of commands that will take an APK or JAR as an input and strip the dex files if
52// they are no longer necessary after preopting.
Colin Crossfeec25b2019-01-30 17:32:39 -080053func GenerateStripRule(global GlobalConfig, module ModuleConfig) (rule *android.RuleBuilder, err error) {
Colin Cross43f08db2018-11-12 10:13:39 -080054 defer func() {
55 if r := recover(); r != nil {
Colin Cross69f59a32019-02-15 10:39:37 -080056 if _, ok := r.(runtime.Error); ok {
57 panic(r)
58 } else if e, ok := r.(error); ok {
Colin Cross43f08db2018-11-12 10:13:39 -080059 err = e
60 rule = nil
61 } else {
62 panic(r)
63 }
64 }
65 }()
66
67 tools := global.Tools
68
Colin Cross758290d2019-02-01 16:42:32 -080069 rule = android.NewRuleBuilder()
Colin Cross43f08db2018-11-12 10:13:39 -080070
71 strip := shouldStripDex(module, global)
72
73 if strip {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +000074 if global.NeverAllowStripping {
75 panic(fmt.Errorf("Stripping requested on %q, though the product does not allow it", module.DexLocation))
76 }
Colin Cross43f08db2018-11-12 10:13:39 -080077 // Only strips if the dex files are not already uncompressed
78 rule.Command().
79 Textf(`if (zipinfo %s '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, module.StripInputPath).
80 Tool(tools.Zip2zip).FlagWithInput("-i ", module.StripInputPath).FlagWithOutput("-o ", module.StripOutputPath).
81 FlagWithArg("-x ", `"classes*.dex"`).
82 Textf(`; else cp -f %s %s; fi`, module.StripInputPath, module.StripOutputPath)
83 } else {
84 rule.Command().Text("cp -f").Input(module.StripInputPath).Output(module.StripOutputPath)
85 }
86
87 return rule, nil
88}
89
90// GenerateDexpreoptRule generates a set of commands that will preopt a module based on a GlobalConfig and a
91// ModuleConfig. The produced files and their install locations will be available through rule.Installs().
Colin Cross69f59a32019-02-15 10:39:37 -080092func GenerateDexpreoptRule(ctx android.PathContext,
93 global GlobalConfig, module ModuleConfig) (rule *android.RuleBuilder, err error) {
94
Colin Cross43f08db2018-11-12 10:13:39 -080095 defer func() {
96 if r := recover(); r != nil {
Colin Cross69f59a32019-02-15 10:39:37 -080097 if _, ok := r.(runtime.Error); ok {
98 panic(r)
99 } else if e, ok := r.(error); ok {
Colin Cross43f08db2018-11-12 10:13:39 -0800100 err = e
101 rule = nil
102 } else {
103 panic(r)
104 }
105 }
106 }()
107
Colin Cross758290d2019-02-01 16:42:32 -0800108 rule = android.NewRuleBuilder()
Colin Cross43f08db2018-11-12 10:13:39 -0800109
Colin Cross69f59a32019-02-15 10:39:37 -0800110 generateProfile := module.ProfileClassListing.Valid() && !global.DisableGenerateProfile
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100111 generateBootProfile := module.ProfileBootListing.Valid() && !global.DisableGenerateProfile
Colin Cross43f08db2018-11-12 10:13:39 -0800112
Colin Cross69f59a32019-02-15 10:39:37 -0800113 var profile android.WritablePath
Colin Crosscbed6572019-01-08 17:38:37 -0800114 if generateProfile {
Colin Cross69f59a32019-02-15 10:39:37 -0800115 profile = profileCommand(ctx, global, module, rule)
Colin Crosscbed6572019-01-08 17:38:37 -0800116 }
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100117 if generateBootProfile {
118 bootProfileCommand(ctx, global, module, rule)
119 }
Colin Crosscbed6572019-01-08 17:38:37 -0800120
121 if !dexpreoptDisabled(global, module) {
122 // Don't preopt individual boot jars, they will be preopted together.
123 // This check is outside dexpreoptDisabled because they still need to be stripped.
124 if !contains(global.BootJars, module.Name) {
125 appImage := (generateProfile || module.ForceCreateAppImage || global.DefaultAppImages) &&
126 !module.NoCreateAppImage
127
128 generateDM := shouldGenerateDM(module, global)
129
Colin Crossc7e40aa2019-02-08 21:37:00 -0800130 for i, arch := range module.Archs {
131 image := module.DexPreoptImages[i]
Dan Willemsen0f416782019-06-13 21:44:53 +0000132 imageDeps := module.DexPreoptImagesDeps[i]
133 dexpreoptCommand(ctx, global, module, rule, arch, profile, image, imageDeps, appImage, generateDM)
Colin Crosscbed6572019-01-08 17:38:37 -0800134 }
135 }
136 }
137
138 return rule, nil
139}
140
141func dexpreoptDisabled(global GlobalConfig, module ModuleConfig) bool {
142 if contains(global.DisablePreoptModules, module.Name) {
143 return true
Colin Cross43f08db2018-11-12 10:13:39 -0800144 }
145
146 // If OnlyPreoptBootImageAndSystemServer=true and module is not in boot class path skip
147 // Also preopt system server jars since selinux prevents system server from loading anything from
148 // /data. If we don't do this they will need to be extracted which is not favorable for RAM usage
149 // or performance. If PreoptExtractedApk is true, we ignore the only preopt boot image options.
150 if global.OnlyPreoptBootImageAndSystemServer && !contains(global.BootJars, module.Name) &&
151 !contains(global.SystemServerJars, module.Name) && !module.PreoptExtractedApk {
Colin Crosscbed6572019-01-08 17:38:37 -0800152 return true
Colin Cross43f08db2018-11-12 10:13:39 -0800153 }
154
Colin Crosscbed6572019-01-08 17:38:37 -0800155 return false
Colin Cross43f08db2018-11-12 10:13:39 -0800156}
157
Colin Cross69f59a32019-02-15 10:39:37 -0800158func profileCommand(ctx android.PathContext, global GlobalConfig, module ModuleConfig,
159 rule *android.RuleBuilder) android.WritablePath {
160
161 profilePath := module.BuildPath.InSameDir(ctx, "profile.prof")
Colin Cross43f08db2018-11-12 10:13:39 -0800162 profileInstalledPath := module.DexLocation + ".prof"
163
164 if !module.ProfileIsTextListing {
165 rule.Command().FlagWithOutput("touch ", profilePath)
166 }
167
168 cmd := rule.Command().
169 Text(`ANDROID_LOG_TAGS="*:e"`).
170 Tool(global.Tools.Profman)
171
172 if module.ProfileIsTextListing {
173 // The profile is a test listing of classes (used for framework jars).
174 // We need to generate the actual binary profile before being able to compile.
Colin Cross69f59a32019-02-15 10:39:37 -0800175 cmd.FlagWithInput("--create-profile-from=", module.ProfileClassListing.Path())
Colin Cross43f08db2018-11-12 10:13:39 -0800176 } else {
177 // The profile is binary profile (used for apps). Run it through profman to
178 // ensure the profile keys match the apk.
179 cmd.
180 Flag("--copy-and-update-profile-key").
Colin Cross69f59a32019-02-15 10:39:37 -0800181 FlagWithInput("--profile-file=", module.ProfileClassListing.Path())
Colin Cross43f08db2018-11-12 10:13:39 -0800182 }
183
184 cmd.
185 FlagWithInput("--apk=", module.DexPath).
186 Flag("--dex-location="+module.DexLocation).
187 FlagWithOutput("--reference-profile-file=", profilePath)
188
189 if !module.ProfileIsTextListing {
190 cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath))
191 }
192 rule.Install(profilePath, profileInstalledPath)
193
194 return profilePath
195}
196
Nicolas Geoffraye7102422019-07-24 13:19:29 +0100197func bootProfileCommand(ctx android.PathContext, global GlobalConfig, module ModuleConfig,
198 rule *android.RuleBuilder) android.WritablePath {
199
200 profilePath := module.BuildPath.InSameDir(ctx, "profile.bprof")
201 profileInstalledPath := module.DexLocation + ".bprof"
202
203 if !module.ProfileIsTextListing {
204 rule.Command().FlagWithOutput("touch ", profilePath)
205 }
206
207 cmd := rule.Command().
208 Text(`ANDROID_LOG_TAGS="*:e"`).
209 Tool(global.Tools.Profman)
210
211 // The profile is a test listing of methods.
212 // We need to generate the actual binary profile.
213 cmd.FlagWithInput("--create-profile-from=", module.ProfileBootListing.Path())
214
215 cmd.
216 Flag("--generate-boot-profile").
217 FlagWithInput("--apk=", module.DexPath).
218 Flag("--dex-location="+module.DexLocation).
219 FlagWithOutput("--reference-profile-file=", profilePath)
220
221 if !module.ProfileIsTextListing {
222 cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath))
223 }
224 rule.Install(profilePath, profileInstalledPath)
225
226 return profilePath
227}
228
Colin Cross69f59a32019-02-15 10:39:37 -0800229func dexpreoptCommand(ctx android.PathContext, global GlobalConfig, module ModuleConfig, rule *android.RuleBuilder,
Dan Willemsen0f416782019-06-13 21:44:53 +0000230 arch android.ArchType, profile, bootImage android.Path, bootImageDeps android.Paths, appImage, generateDM bool) {
Colin Cross43f08db2018-11-12 10:13:39 -0800231
232 // HACK: make soname in Soong-generated .odex files match Make.
233 base := filepath.Base(module.DexLocation)
234 if filepath.Ext(base) == ".jar" {
235 base = "javalib.jar"
236 } else if filepath.Ext(base) == ".apk" {
237 base = "package.apk"
238 }
239
240 toOdexPath := func(path string) string {
241 return filepath.Join(
242 filepath.Dir(path),
243 "oat",
Colin Cross74ba9622019-02-11 15:11:14 -0800244 arch.String(),
Colin Cross43f08db2018-11-12 10:13:39 -0800245 pathtools.ReplaceExtension(filepath.Base(path), "odex"))
246 }
247
Colin Cross69f59a32019-02-15 10:39:37 -0800248 odexPath := module.BuildPath.InSameDir(ctx, "oat", arch.String(), pathtools.ReplaceExtension(base, "odex"))
Colin Cross43f08db2018-11-12 10:13:39 -0800249 odexInstallPath := toOdexPath(module.DexLocation)
250 if odexOnSystemOther(module, global) {
251 odexInstallPath = strings.Replace(odexInstallPath, SystemPartition, SystemOtherPartition, 1)
252 }
253
Colin Cross69f59a32019-02-15 10:39:37 -0800254 vdexPath := odexPath.ReplaceExtension(ctx, "vdex")
Colin Cross43f08db2018-11-12 10:13:39 -0800255 vdexInstallPath := pathtools.ReplaceExtension(odexInstallPath, "vdex")
256
Colin Cross69f59a32019-02-15 10:39:37 -0800257 invocationPath := odexPath.ReplaceExtension(ctx, "invocation")
Alex Light5de41962018-12-18 15:16:26 -0800258
Colin Crossc7e40aa2019-02-08 21:37:00 -0800259 // bootImage is .../dex_bootjars/system/framework/arm64/boot.art, but dex2oat wants
260 // .../dex_bootjars/system/framework/boot.art on the command line
261 var bootImageLocation string
Colin Cross69f59a32019-02-15 10:39:37 -0800262 if bootImage != nil {
Colin Crossc7e40aa2019-02-08 21:37:00 -0800263 bootImageLocation = PathToLocation(bootImage, arch)
Colin Cross43f08db2018-11-12 10:13:39 -0800264 }
265
Colin Cross43f08db2018-11-12 10:13:39 -0800266 // The class loader context using paths in the build
Colin Cross69f59a32019-02-15 10:39:37 -0800267 var classLoaderContextHost android.Paths
Colin Cross43f08db2018-11-12 10:13:39 -0800268
269 // The class loader context using paths as they will be on the device
270 var classLoaderContextTarget []string
271
272 // Extra paths that will be appended to the class loader if the APK manifest has targetSdkVersion < 28
Colin Cross69f59a32019-02-15 10:39:37 -0800273 var conditionalClassLoaderContextHost28 android.Paths
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000274 var conditionalClassLoaderContextTarget28 []string
275
276 // Extra paths that will be appended to the class loader if the APK manifest has targetSdkVersion < 29
Colin Cross69f59a32019-02-15 10:39:37 -0800277 var conditionalClassLoaderContextHost29 android.Paths
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000278 var conditionalClassLoaderContextTarget29 []string
Colin Cross43f08db2018-11-12 10:13:39 -0800279
Colin Cross69f59a32019-02-15 10:39:37 -0800280 var classLoaderContextHostString string
281
Colin Cross43f08db2018-11-12 10:13:39 -0800282 if module.EnforceUsesLibraries {
Colin Cross50ddcc42019-05-16 12:28:22 -0700283 usesLibs := append(copyOf(module.UsesLibraries), module.PresentOptionalUsesLibraries...)
Colin Cross43f08db2018-11-12 10:13:39 -0800284
285 // Create class loader context for dex2oat from uses libraries and filtered optional libraries
Colin Cross50ddcc42019-05-16 12:28:22 -0700286 for _, l := range usesLibs {
Colin Cross43f08db2018-11-12 10:13:39 -0800287
288 classLoaderContextHost = append(classLoaderContextHost,
289 pathForLibrary(module, l))
290 classLoaderContextTarget = append(classLoaderContextTarget,
291 filepath.Join("/system/framework", l+".jar"))
292 }
293
294 const httpLegacy = "org.apache.http.legacy"
295 const httpLegacyImpl = "org.apache.http.legacy.impl"
296
Colin Cross38b96852019-05-22 10:21:09 -0700297 // org.apache.http.legacy contains classes that were in the default classpath until API 28. If the
298 // targetSdkVersion in the manifest or APK is < 28, and the module does not explicitly depend on
299 // org.apache.http.legacy, then implicitly add the classes to the classpath for dexpreopt. One the
300 // device the classes will be in a file called org.apache.http.legacy.impl.jar.
Colin Cross50ddcc42019-05-16 12:28:22 -0700301 module.LibraryPaths[httpLegacyImpl] = module.LibraryPaths[httpLegacy]
302
303 if !contains(module.UsesLibraries, httpLegacy) && !contains(module.PresentOptionalUsesLibraries, httpLegacy) {
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000304 conditionalClassLoaderContextHost28 = append(conditionalClassLoaderContextHost28,
Colin Cross43f08db2018-11-12 10:13:39 -0800305 pathForLibrary(module, httpLegacyImpl))
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000306 conditionalClassLoaderContextTarget28 = append(conditionalClassLoaderContextTarget28,
Colin Cross43f08db2018-11-12 10:13:39 -0800307 filepath.Join("/system/framework", httpLegacyImpl+".jar"))
308 }
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000309
310 const hidlBase = "android.hidl.base-V1.0-java"
311 const hidlManager = "android.hidl.manager-V1.0-java"
312
Colin Cross38b96852019-05-22 10:21:09 -0700313 // android.hidl.base-V1.0-java and android.hidl.manager-V1.0 contain classes that were in the default
314 // classpath until API 29. If the targetSdkVersion in the manifest or APK is < 29 then implicitly add
315 // the classes to the classpath for dexpreopt.
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000316 conditionalClassLoaderContextHost29 = append(conditionalClassLoaderContextHost29,
Alex Light5de41962018-12-18 15:16:26 -0800317 pathForLibrary(module, hidlManager))
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000318 conditionalClassLoaderContextTarget29 = append(conditionalClassLoaderContextTarget29,
319 filepath.Join("/system/framework", hidlManager+".jar"))
320 conditionalClassLoaderContextHost29 = append(conditionalClassLoaderContextHost29,
Alex Light5de41962018-12-18 15:16:26 -0800321 pathForLibrary(module, hidlBase))
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000322 conditionalClassLoaderContextTarget29 = append(conditionalClassLoaderContextTarget29,
323 filepath.Join("/system/framework", hidlBase+".jar"))
Colin Cross69f59a32019-02-15 10:39:37 -0800324
325 classLoaderContextHostString = strings.Join(classLoaderContextHost.Strings(), ":")
Colin Cross43f08db2018-11-12 10:13:39 -0800326 } else {
327 // Pass special class loader context to skip the classpath and collision check.
328 // This will get removed once LOCAL_USES_LIBRARIES is enforced.
329 // Right now LOCAL_USES_LIBRARIES is opt in, for the case where it's not specified we still default
330 // to the &.
Colin Cross69f59a32019-02-15 10:39:37 -0800331 classLoaderContextHostString = `\&`
Colin Cross43f08db2018-11-12 10:13:39 -0800332 }
333
Colin Cross69f59a32019-02-15 10:39:37 -0800334 rule.Command().FlagWithArg("mkdir -p ", filepath.Dir(odexPath.String()))
Colin Cross43f08db2018-11-12 10:13:39 -0800335 rule.Command().FlagWithOutput("rm -f ", odexPath)
336 // Set values in the environment of the rule. These may be modified by construct_context.sh.
Colin Cross69f59a32019-02-15 10:39:37 -0800337 rule.Command().FlagWithArg("class_loader_context_arg=--class-loader-context=", classLoaderContextHostString)
Colin Cross43f08db2018-11-12 10:13:39 -0800338 rule.Command().Text(`stored_class_loader_context_arg=""`)
339
340 if module.EnforceUsesLibraries {
Colin Cross38b96852019-05-22 10:21:09 -0700341 if module.ManifestPath != nil {
342 rule.Command().Text(`target_sdk_version="$(`).
343 Tool(global.Tools.ManifestCheck).
344 Flag("--extract-target-sdk-version").
345 Input(module.ManifestPath).
346 Text(`)"`)
347 } else {
348 // No manifest to extract targetSdkVersion from, hope that DexJar is an APK
349 rule.Command().Text(`target_sdk_version="$(`).
350 Tool(global.Tools.Aapt).
351 Flag("dump badging").
352 Input(module.DexPath).
353 Text(`| grep "targetSdkVersion" | sed -n "s/targetSdkVersion:'\(.*\)'/\1/p"`).
354 Text(`)"`)
355 }
Colin Cross69f59a32019-02-15 10:39:37 -0800356 rule.Command().Textf(`dex_preopt_host_libraries="%s"`,
357 strings.Join(classLoaderContextHost.Strings(), " ")).
358 Implicits(classLoaderContextHost)
359 rule.Command().Textf(`dex_preopt_target_libraries="%s"`,
360 strings.Join(classLoaderContextTarget, " "))
361 rule.Command().Textf(`conditional_host_libs_28="%s"`,
362 strings.Join(conditionalClassLoaderContextHost28.Strings(), " ")).
363 Implicits(conditionalClassLoaderContextHost28)
364 rule.Command().Textf(`conditional_target_libs_28="%s"`,
365 strings.Join(conditionalClassLoaderContextTarget28, " "))
366 rule.Command().Textf(`conditional_host_libs_29="%s"`,
367 strings.Join(conditionalClassLoaderContextHost29.Strings(), " ")).
368 Implicits(conditionalClassLoaderContextHost29)
369 rule.Command().Textf(`conditional_target_libs_29="%s"`,
370 strings.Join(conditionalClassLoaderContextTarget29, " "))
Colin Cross38b96852019-05-22 10:21:09 -0700371 rule.Command().Text("source").Tool(global.Tools.ConstructContext).Input(module.DexPath)
Colin Cross43f08db2018-11-12 10:13:39 -0800372 }
373
Nicolas Geoffray2464ef42019-03-05 14:07:07 +0000374 // Devices that do not have a product partition use a symlink from /product to /system/product.
375 // Because on-device dexopt will see dex locations starting with /product, we change the paths
376 // to mimic this behavior.
377 dexLocationArg := module.DexLocation
378 if strings.HasPrefix(dexLocationArg, "/system/product/") {
379 dexLocationArg = strings.TrimPrefix(dexLocationArg, "/system")
380 }
381
Colin Cross43f08db2018-11-12 10:13:39 -0800382 cmd := rule.Command().
383 Text(`ANDROID_LOG_TAGS="*:e"`).
384 Tool(global.Tools.Dex2oat).
385 Flag("--avoid-storing-invocation").
Alex Light5de41962018-12-18 15:16:26 -0800386 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross43f08db2018-11-12 10:13:39 -0800387 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatXms).
388 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatXmx).
Colin Cross800fe132019-02-11 14:21:24 -0800389 Flag("--runtime-arg").FlagWithInputList("-Xbootclasspath:", module.PreoptBootClassPathDexFiles, ":").
390 Flag("--runtime-arg").FlagWithList("-Xbootclasspath-locations:", module.PreoptBootClassPathDexLocations, ":").
Colin Cross43f08db2018-11-12 10:13:39 -0800391 Flag("${class_loader_context_arg}").
392 Flag("${stored_class_loader_context_arg}").
Dan Willemsen0f416782019-06-13 21:44:53 +0000393 FlagWithArg("--boot-image=", bootImageLocation).Implicits(bootImageDeps).
Colin Cross43f08db2018-11-12 10:13:39 -0800394 FlagWithInput("--dex-file=", module.DexPath).
Nicolas Geoffray2464ef42019-03-05 14:07:07 +0000395 FlagWithArg("--dex-location=", dexLocationArg).
Colin Cross43f08db2018-11-12 10:13:39 -0800396 FlagWithOutput("--oat-file=", odexPath).ImplicitOutput(vdexPath).
397 // Pass an empty directory, dex2oat shouldn't be reading arbitrary files
398 FlagWithArg("--android-root=", global.EmptyDirectory).
Colin Cross74ba9622019-02-11 15:11:14 -0800399 FlagWithArg("--instruction-set=", arch.String()).
Colin Cross43f08db2018-11-12 10:13:39 -0800400 FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]).
401 FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]).
402 Flag("--no-generate-debug-info").
403 Flag("--generate-build-id").
404 Flag("--abort-on-hard-verifier-error").
405 Flag("--force-determinism").
406 FlagWithArg("--no-inline-from=", "core-oj.jar")
407
408 var preoptFlags []string
409 if len(module.PreoptFlags) > 0 {
410 preoptFlags = module.PreoptFlags
411 } else if len(global.PreoptFlags) > 0 {
412 preoptFlags = global.PreoptFlags
413 }
414
415 if len(preoptFlags) > 0 {
416 cmd.Text(strings.Join(preoptFlags, " "))
417 }
418
419 if module.UncompressedDex {
420 cmd.FlagWithArg("--copy-dex-files=", "false")
421 }
422
423 if !anyHavePrefix(preoptFlags, "--compiler-filter=") {
424 var compilerFilter string
425 if contains(global.SystemServerJars, module.Name) {
426 // Jars of system server, use the product option if it is set, speed otherwise.
427 if global.SystemServerCompilerFilter != "" {
428 compilerFilter = global.SystemServerCompilerFilter
429 } else {
430 compilerFilter = "speed"
431 }
432 } else if contains(global.SpeedApps, module.Name) || contains(global.SystemServerApps, module.Name) {
433 // Apps loaded into system server, and apps the product default to being compiled with the
434 // 'speed' compiler filter.
435 compilerFilter = "speed"
Colin Cross69f59a32019-02-15 10:39:37 -0800436 } else if profile != nil {
Colin Cross43f08db2018-11-12 10:13:39 -0800437 // For non system server jars, use speed-profile when we have a profile.
438 compilerFilter = "speed-profile"
439 } else if global.DefaultCompilerFilter != "" {
440 compilerFilter = global.DefaultCompilerFilter
441 } else {
442 compilerFilter = "quicken"
443 }
444 cmd.FlagWithArg("--compiler-filter=", compilerFilter)
445 }
446
447 if generateDM {
448 cmd.FlagWithArg("--copy-dex-files=", "false")
Colin Cross69f59a32019-02-15 10:39:37 -0800449 dmPath := module.BuildPath.InSameDir(ctx, "generated.dm")
Colin Cross43f08db2018-11-12 10:13:39 -0800450 dmInstalledPath := pathtools.ReplaceExtension(module.DexLocation, "dm")
Colin Cross69f59a32019-02-15 10:39:37 -0800451 tmpPath := module.BuildPath.InSameDir(ctx, "primary.vdex")
Colin Cross43f08db2018-11-12 10:13:39 -0800452 rule.Command().Text("cp -f").Input(vdexPath).Output(tmpPath)
453 rule.Command().Tool(global.Tools.SoongZip).
454 FlagWithArg("-L", "9").
455 FlagWithOutput("-o", dmPath).
456 Flag("-j").
457 Input(tmpPath)
458 rule.Install(dmPath, dmInstalledPath)
459 }
460
461 // By default, emit debug info.
462 debugInfo := true
463 if global.NoDebugInfo {
464 // If the global setting suppresses mini-debug-info, disable it.
465 debugInfo = false
466 }
467
468 // PRODUCT_SYSTEM_SERVER_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO.
469 // PRODUCT_OTHER_JAVA_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO.
470 if contains(global.SystemServerJars, module.Name) {
471 if global.AlwaysSystemServerDebugInfo {
472 debugInfo = true
473 } else if global.NeverSystemServerDebugInfo {
474 debugInfo = false
475 }
476 } else {
477 if global.AlwaysOtherDebugInfo {
478 debugInfo = true
479 } else if global.NeverOtherDebugInfo {
480 debugInfo = false
481 }
482 }
483
484 // Never enable on eng.
485 if global.IsEng {
486 debugInfo = false
487 }
488
489 if debugInfo {
490 cmd.Flag("--generate-mini-debug-info")
491 } else {
492 cmd.Flag("--no-generate-mini-debug-info")
493 }
494
495 // Set the compiler reason to 'prebuilt' to identify the oat files produced
496 // during the build, as opposed to compiled on the device.
497 cmd.FlagWithArg("--compilation-reason=", "prebuilt")
498
499 if appImage {
Colin 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
518// Return if the dex file in the APK should be stripped. If an APK is found to contain uncompressed dex files at
519// dex2oat time it will not be stripped even if strip=true.
520func shouldStripDex(module ModuleConfig, global GlobalConfig) bool {
521 strip := !global.DefaultNoStripping
522
Colin Crosscbed6572019-01-08 17:38:37 -0800523 if dexpreoptDisabled(global, module) {
524 strip = false
525 }
526
Colin Cross8c6d2502019-01-09 21:09:14 -0800527 if module.NoStripping {
528 strip = false
529 }
530
Colin Cross43f08db2018-11-12 10:13:39 -0800531 // Don't strip modules that are not on the system partition in case the oat/vdex version in system ROM
532 // doesn't match the one in other partitions. It needs to be able to fall back to the APK for that case.
533 if !strings.HasPrefix(module.DexLocation, SystemPartition) {
534 strip = false
535 }
536
537 // system_other isn't there for an OTA, so don't strip if module is on system, and odex is on system_other.
538 if odexOnSystemOther(module, global) {
539 strip = false
540 }
541
542 if module.HasApkLibraries {
543 strip = false
544 }
545
546 // Don't strip with dex files we explicitly uncompress (dexopt will not store the dex code).
547 if module.UncompressedDex {
548 strip = false
549 }
550
551 if shouldGenerateDM(module, global) {
552 strip = false
553 }
554
555 if module.PresignedPrebuilt {
556 // Only strip out files if we can re-sign the package.
557 strip = false
558 }
559
560 return strip
561}
562
563func shouldGenerateDM(module ModuleConfig, global GlobalConfig) bool {
564 // Generating DM files only makes sense for verify, avoid doing for non verify compiler filter APKs.
565 // No reason to use a dm file if the dex is already uncompressed.
566 return global.GenerateDMFiles && !module.UncompressedDex &&
567 contains(module.PreoptFlags, "--compiler-filter=verify")
568}
569
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000570func OdexOnSystemOtherByName(name string, dexLocation string, global GlobalConfig) bool {
Colin Cross43f08db2018-11-12 10:13:39 -0800571 if !global.HasSystemOther {
572 return false
573 }
574
575 if global.SanitizeLite {
576 return false
577 }
578
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000579 if contains(global.SpeedApps, name) || contains(global.SystemServerApps, name) {
Colin Cross43f08db2018-11-12 10:13:39 -0800580 return false
581 }
582
583 for _, f := range global.PatternsOnSystemOther {
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000584 if makefileMatch(filepath.Join(SystemPartition, f), dexLocation) {
Colin Cross43f08db2018-11-12 10:13:39 -0800585 return true
586 }
587 }
588
589 return false
590}
591
Nicolas Geoffrayfa6e9ec2019-02-12 13:12:16 +0000592func odexOnSystemOther(module ModuleConfig, global GlobalConfig) bool {
593 return OdexOnSystemOtherByName(module.Name, module.DexLocation, global)
594}
595
Colin Crossc7e40aa2019-02-08 21:37:00 -0800596// PathToLocation converts .../system/framework/arm64/boot.art to .../system/framework/boot.art
Colin Cross69f59a32019-02-15 10:39:37 -0800597func PathToLocation(path android.Path, arch android.ArchType) string {
598 pathArch := filepath.Base(filepath.Dir(path.String()))
Colin Cross74ba9622019-02-11 15:11:14 -0800599 if pathArch != arch.String() {
600 panic(fmt.Errorf("last directory in %q must be %q", path, arch.String()))
Colin Crossc7e40aa2019-02-08 21:37:00 -0800601 }
Colin Cross69f59a32019-02-15 10:39:37 -0800602 return filepath.Join(filepath.Dir(filepath.Dir(path.String())), filepath.Base(path.String()))
Colin Crossc7e40aa2019-02-08 21:37:00 -0800603}
604
Colin Cross69f59a32019-02-15 10:39:37 -0800605func pathForLibrary(module ModuleConfig, lib string) android.Path {
606 path, ok := module.LibraryPaths[lib]
607 if !ok {
Colin Cross43f08db2018-11-12 10:13:39 -0800608 panic(fmt.Errorf("unknown library path for %q", lib))
609 }
610 return path
611}
612
613func makefileMatch(pattern, s string) bool {
614 percent := strings.IndexByte(pattern, '%')
615 switch percent {
616 case -1:
617 return pattern == s
618 case len(pattern) - 1:
619 return strings.HasPrefix(s, pattern[:len(pattern)-1])
620 default:
621 panic(fmt.Errorf("unsupported makefile pattern %q", pattern))
622 }
623}
624
625func contains(l []string, s string) bool {
626 for _, e := range l {
627 if e == s {
628 return true
629 }
630 }
631 return false
632}
633
634// remove all elements in a from b, returning a new slice
635func filterOut(a []string, b []string) []string {
636 var ret []string
637 for _, x := range b {
638 if !contains(a, x) {
639 ret = append(ret, x)
640 }
641 }
642 return ret
643}
644
645func replace(l []string, from, to string) {
646 for i := range l {
647 if l[i] == from {
648 l[i] = to
649 }
650 }
651}
652
Colin Cross454c0872019-02-15 23:03:34 -0800653var copyOf = android.CopyOf
Colin Cross43f08db2018-11-12 10:13:39 -0800654
655func anyHavePrefix(l []string, prefix string) bool {
656 for _, x := range l {
657 if strings.HasPrefix(x, prefix) {
658 return true
659 }
660 }
661 return false
662}