blob: b12ca0b6bd26cf7281ce9efbbaf46ecc17369f30 [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"
40 "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
50// GenerateStripRule generates a set of commands that will take an APK or JAR as an input and strip the dex files if
51// they are no longer necessary after preopting.
Colin Crossfeec25b2019-01-30 17:32:39 -080052func GenerateStripRule(global GlobalConfig, module ModuleConfig) (rule *android.RuleBuilder, err error) {
Colin Cross43f08db2018-11-12 10:13:39 -080053 defer func() {
54 if r := recover(); r != nil {
55 if e, ok := r.(error); ok {
56 err = e
57 rule = nil
58 } else {
59 panic(r)
60 }
61 }
62 }()
63
64 tools := global.Tools
65
Colin Crossfeec25b2019-01-30 17:32:39 -080066 rule = &android.RuleBuilder{}
Colin Cross43f08db2018-11-12 10:13:39 -080067
68 strip := shouldStripDex(module, global)
69
70 if strip {
71 // Only strips if the dex files are not already uncompressed
72 rule.Command().
73 Textf(`if (zipinfo %s '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, module.StripInputPath).
74 Tool(tools.Zip2zip).FlagWithInput("-i ", module.StripInputPath).FlagWithOutput("-o ", module.StripOutputPath).
75 FlagWithArg("-x ", `"classes*.dex"`).
76 Textf(`; else cp -f %s %s; fi`, module.StripInputPath, module.StripOutputPath)
77 } else {
78 rule.Command().Text("cp -f").Input(module.StripInputPath).Output(module.StripOutputPath)
79 }
80
81 return rule, nil
82}
83
84// GenerateDexpreoptRule generates a set of commands that will preopt a module based on a GlobalConfig and a
85// ModuleConfig. The produced files and their install locations will be available through rule.Installs().
Colin Crossfeec25b2019-01-30 17:32:39 -080086func GenerateDexpreoptRule(global GlobalConfig, module ModuleConfig) (rule *android.RuleBuilder, err error) {
Colin Cross43f08db2018-11-12 10:13:39 -080087 defer func() {
88 if r := recover(); r != nil {
89 if e, ok := r.(error); ok {
90 err = e
91 rule = nil
92 } else {
93 panic(r)
94 }
95 }
96 }()
97
Colin Crossfeec25b2019-01-30 17:32:39 -080098 rule = &android.RuleBuilder{}
Colin Cross43f08db2018-11-12 10:13:39 -080099
Colin Crosscbed6572019-01-08 17:38:37 -0800100 generateProfile := module.ProfileClassListing != "" && !global.DisableGenerateProfile
Colin Cross43f08db2018-11-12 10:13:39 -0800101
Colin Crosscbed6572019-01-08 17:38:37 -0800102 var profile string
103 if generateProfile {
104 profile = profileCommand(global, module, rule)
105 }
106
107 if !dexpreoptDisabled(global, module) {
108 // Don't preopt individual boot jars, they will be preopted together.
109 // This check is outside dexpreoptDisabled because they still need to be stripped.
110 if !contains(global.BootJars, module.Name) {
111 appImage := (generateProfile || module.ForceCreateAppImage || global.DefaultAppImages) &&
112 !module.NoCreateAppImage
113
114 generateDM := shouldGenerateDM(module, global)
115
116 for _, arch := range module.Archs {
117 imageLocation := module.DexPreoptImageLocation
118 if imageLocation == "" {
119 imageLocation = global.DefaultDexPreoptImageLocation[arch]
120 }
121 dexpreoptCommand(global, module, rule, profile, arch, imageLocation, appImage, generateDM)
122 }
123 }
124 }
125
126 return rule, nil
127}
128
129func dexpreoptDisabled(global GlobalConfig, module ModuleConfig) bool {
130 if contains(global.DisablePreoptModules, module.Name) {
131 return true
Colin Cross43f08db2018-11-12 10:13:39 -0800132 }
133
134 // If OnlyPreoptBootImageAndSystemServer=true and module is not in boot class path skip
135 // Also preopt system server jars since selinux prevents system server from loading anything from
136 // /data. If we don't do this they will need to be extracted which is not favorable for RAM usage
137 // or performance. If PreoptExtractedApk is true, we ignore the only preopt boot image options.
138 if global.OnlyPreoptBootImageAndSystemServer && !contains(global.BootJars, module.Name) &&
139 !contains(global.SystemServerJars, module.Name) && !module.PreoptExtractedApk {
Colin Crosscbed6572019-01-08 17:38:37 -0800140 return true
Colin Cross43f08db2018-11-12 10:13:39 -0800141 }
142
Colin Crosscbed6572019-01-08 17:38:37 -0800143 return false
Colin Cross43f08db2018-11-12 10:13:39 -0800144}
145
Colin Crossfeec25b2019-01-30 17:32:39 -0800146func profileCommand(global GlobalConfig, module ModuleConfig, rule *android.RuleBuilder) string {
Colin Cross43f08db2018-11-12 10:13:39 -0800147 profilePath := filepath.Join(filepath.Dir(module.BuildPath), "profile.prof")
148 profileInstalledPath := module.DexLocation + ".prof"
149
150 if !module.ProfileIsTextListing {
151 rule.Command().FlagWithOutput("touch ", profilePath)
152 }
153
154 cmd := rule.Command().
155 Text(`ANDROID_LOG_TAGS="*:e"`).
156 Tool(global.Tools.Profman)
157
158 if module.ProfileIsTextListing {
159 // The profile is a test listing of classes (used for framework jars).
160 // We need to generate the actual binary profile before being able to compile.
161 cmd.FlagWithInput("--create-profile-from=", module.ProfileClassListing)
162 } else {
163 // The profile is binary profile (used for apps). Run it through profman to
164 // ensure the profile keys match the apk.
165 cmd.
166 Flag("--copy-and-update-profile-key").
167 FlagWithInput("--profile-file=", module.ProfileClassListing)
168 }
169
170 cmd.
171 FlagWithInput("--apk=", module.DexPath).
172 Flag("--dex-location="+module.DexLocation).
173 FlagWithOutput("--reference-profile-file=", profilePath)
174
175 if !module.ProfileIsTextListing {
176 cmd.Text(fmt.Sprintf(`|| echo "Profile out of date for %s"`, module.DexPath))
177 }
178 rule.Install(profilePath, profileInstalledPath)
179
180 return profilePath
181}
182
Colin Crossfeec25b2019-01-30 17:32:39 -0800183func dexpreoptCommand(global GlobalConfig, module ModuleConfig, rule *android.RuleBuilder,
184 profile, arch, bootImageLocation string, appImage, generateDM bool) {
Colin Cross43f08db2018-11-12 10:13:39 -0800185
186 // HACK: make soname in Soong-generated .odex files match Make.
187 base := filepath.Base(module.DexLocation)
188 if filepath.Ext(base) == ".jar" {
189 base = "javalib.jar"
190 } else if filepath.Ext(base) == ".apk" {
191 base = "package.apk"
192 }
193
194 toOdexPath := func(path string) string {
195 return filepath.Join(
196 filepath.Dir(path),
197 "oat",
198 arch,
199 pathtools.ReplaceExtension(filepath.Base(path), "odex"))
200 }
201
Vladimir Markod2ee5322018-12-19 17:57:57 +0000202 bcp := strings.Join(global.PreoptBootClassPathDexFiles, ":")
203 bcp_locations := strings.Join(global.PreoptBootClassPathDexLocations, ":")
204
Colin Cross43f08db2018-11-12 10:13:39 -0800205 odexPath := toOdexPath(filepath.Join(filepath.Dir(module.BuildPath), base))
206 odexInstallPath := toOdexPath(module.DexLocation)
207 if odexOnSystemOther(module, global) {
208 odexInstallPath = strings.Replace(odexInstallPath, SystemPartition, SystemOtherPartition, 1)
209 }
210
211 vdexPath := pathtools.ReplaceExtension(odexPath, "vdex")
212 vdexInstallPath := pathtools.ReplaceExtension(odexInstallPath, "vdex")
213
Alex Light5de41962018-12-18 15:16:26 -0800214 invocationPath := pathtools.ReplaceExtension(odexPath, "invocation")
215
Colin Cross43f08db2018-11-12 10:13:39 -0800216 // bootImageLocation is $OUT/dex_bootjars/system/framework/boot.art, but dex2oat actually reads
217 // $OUT/dex_bootjars/system/framework/arm64/boot.art
218 var bootImagePath string
219 if bootImageLocation != "" {
220 bootImagePath = filepath.Join(filepath.Dir(bootImageLocation), arch, filepath.Base(bootImageLocation))
221 }
222
223 // Lists of used and optional libraries from the build config to be verified against the manifest in the APK
224 var verifyUsesLibs []string
225 var verifyOptionalUsesLibs []string
226
227 // Lists of used and optional libraries from the build config, with optional libraries that are known to not
228 // be present in the current product removed.
229 var filteredUsesLibs []string
230 var filteredOptionalUsesLibs []string
231
232 // The class loader context using paths in the build
233 var classLoaderContextHost []string
234
235 // The class loader context using paths as they will be on the device
236 var classLoaderContextTarget []string
237
238 // Extra paths that will be appended to the class loader if the APK manifest has targetSdkVersion < 28
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000239 var conditionalClassLoaderContextHost28 []string
240 var conditionalClassLoaderContextTarget28 []string
241
242 // Extra paths that will be appended to the class loader if the APK manifest has targetSdkVersion < 29
243 var conditionalClassLoaderContextHost29 []string
244 var conditionalClassLoaderContextTarget29 []string
Colin Cross43f08db2018-11-12 10:13:39 -0800245
246 if module.EnforceUsesLibraries {
247 verifyUsesLibs = copyOf(module.UsesLibraries)
248 verifyOptionalUsesLibs = copyOf(module.OptionalUsesLibraries)
249
250 filteredOptionalUsesLibs = filterOut(global.MissingUsesLibraries, module.OptionalUsesLibraries)
251 filteredUsesLibs = append(copyOf(module.UsesLibraries), filteredOptionalUsesLibs...)
252
253 // Create class loader context for dex2oat from uses libraries and filtered optional libraries
254 for _, l := range filteredUsesLibs {
255
256 classLoaderContextHost = append(classLoaderContextHost,
257 pathForLibrary(module, l))
258 classLoaderContextTarget = append(classLoaderContextTarget,
259 filepath.Join("/system/framework", l+".jar"))
260 }
261
262 const httpLegacy = "org.apache.http.legacy"
263 const httpLegacyImpl = "org.apache.http.legacy.impl"
264
265 // Fix up org.apache.http.legacy.impl since it should be org.apache.http.legacy in the manifest.
266 replace(verifyUsesLibs, httpLegacyImpl, httpLegacy)
267 replace(verifyOptionalUsesLibs, httpLegacyImpl, httpLegacy)
268
269 if !contains(verifyUsesLibs, httpLegacy) && !contains(verifyOptionalUsesLibs, httpLegacy) {
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000270 conditionalClassLoaderContextHost28 = append(conditionalClassLoaderContextHost28,
Colin Cross43f08db2018-11-12 10:13:39 -0800271 pathForLibrary(module, httpLegacyImpl))
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000272 conditionalClassLoaderContextTarget28 = append(conditionalClassLoaderContextTarget28,
Colin Cross43f08db2018-11-12 10:13:39 -0800273 filepath.Join("/system/framework", httpLegacyImpl+".jar"))
274 }
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000275
276 const hidlBase = "android.hidl.base-V1.0-java"
277 const hidlManager = "android.hidl.manager-V1.0-java"
278
279 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"))
Colin Cross43f08db2018-11-12 10:13:39 -0800287 } else {
288 // Pass special class loader context to skip the classpath and collision check.
289 // This will get removed once LOCAL_USES_LIBRARIES is enforced.
290 // Right now LOCAL_USES_LIBRARIES is opt in, for the case where it's not specified we still default
291 // to the &.
292 classLoaderContextHost = []string{`\&`}
293 }
294
295 rule.Command().FlagWithArg("mkdir -p ", filepath.Dir(odexPath))
296 rule.Command().FlagWithOutput("rm -f ", odexPath)
297 // Set values in the environment of the rule. These may be modified by construct_context.sh.
298 rule.Command().FlagWithArg("class_loader_context_arg=--class-loader-context=",
299 strings.Join(classLoaderContextHost, ":"))
300 rule.Command().Text(`stored_class_loader_context_arg=""`)
301
302 if module.EnforceUsesLibraries {
Colin Cross43f08db2018-11-12 10:13:39 -0800303 rule.Command().Textf(`uses_library_names="%s"`, strings.Join(verifyUsesLibs, " "))
304 rule.Command().Textf(`optional_uses_library_names="%s"`, strings.Join(verifyOptionalUsesLibs, " "))
305 rule.Command().Textf(`aapt_binary="%s"`, global.Tools.Aapt)
Alex Light5de41962018-12-18 15:16:26 -0800306 rule.Command().Textf(`dex_preopt_host_libraries="%s"`, strings.Join(classLoaderContextHost, " "))
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000307 rule.Command().Textf(`dex_preopt_target_libraries="%s"`, strings.Join(classLoaderContextTarget, " "))
308 rule.Command().Textf(`conditional_host_libs_28="%s"`, strings.Join(conditionalClassLoaderContextHost28, " "))
309 rule.Command().Textf(`conditional_target_libs_28="%s"`, strings.Join(conditionalClassLoaderContextTarget28, " "))
310 rule.Command().Textf(`conditional_host_libs_29="%s"`, strings.Join(conditionalClassLoaderContextHost29, " "))
311 rule.Command().Textf(`conditional_target_libs_29="%s"`, strings.Join(conditionalClassLoaderContextTarget29, " "))
Colin Cross43f08db2018-11-12 10:13:39 -0800312 rule.Command().Text("source").Tool(global.Tools.VerifyUsesLibraries).Input(module.DexPath)
Nicolas Geoffray05aa7d22018-12-18 14:15:12 +0000313 rule.Command().Text("source").Tool(global.Tools.ConstructContext)
Colin Cross43f08db2018-11-12 10:13:39 -0800314 }
315
316 cmd := rule.Command().
317 Text(`ANDROID_LOG_TAGS="*:e"`).
318 Tool(global.Tools.Dex2oat).
319 Flag("--avoid-storing-invocation").
Alex Light5de41962018-12-18 15:16:26 -0800320 FlagWithOutput("--write-invocation-to=", invocationPath).ImplicitOutput(invocationPath).
Colin Cross43f08db2018-11-12 10:13:39 -0800321 Flag("--runtime-arg").FlagWithArg("-Xms", global.Dex2oatXms).
322 Flag("--runtime-arg").FlagWithArg("-Xmx", global.Dex2oatXmx).
Vladimir Markod2ee5322018-12-19 17:57:57 +0000323 Flag("--runtime-arg").FlagWithArg("-Xbootclasspath:", bcp).
324 Implicits(global.PreoptBootClassPathDexFiles).
325 Flag("--runtime-arg").FlagWithArg("-Xbootclasspath-locations:", bcp_locations).
Colin Cross43f08db2018-11-12 10:13:39 -0800326 Flag("${class_loader_context_arg}").
327 Flag("${stored_class_loader_context_arg}").
328 FlagWithArg("--boot-image=", bootImageLocation).Implicit(bootImagePath).
329 FlagWithInput("--dex-file=", module.DexPath).
330 FlagWithArg("--dex-location=", module.DexLocation).
331 FlagWithOutput("--oat-file=", odexPath).ImplicitOutput(vdexPath).
332 // Pass an empty directory, dex2oat shouldn't be reading arbitrary files
333 FlagWithArg("--android-root=", global.EmptyDirectory).
334 FlagWithArg("--instruction-set=", arch).
335 FlagWithArg("--instruction-set-variant=", global.CpuVariant[arch]).
336 FlagWithArg("--instruction-set-features=", global.InstructionSetFeatures[arch]).
337 Flag("--no-generate-debug-info").
338 Flag("--generate-build-id").
339 Flag("--abort-on-hard-verifier-error").
340 Flag("--force-determinism").
341 FlagWithArg("--no-inline-from=", "core-oj.jar")
342
343 var preoptFlags []string
344 if len(module.PreoptFlags) > 0 {
345 preoptFlags = module.PreoptFlags
346 } else if len(global.PreoptFlags) > 0 {
347 preoptFlags = global.PreoptFlags
348 }
349
350 if len(preoptFlags) > 0 {
351 cmd.Text(strings.Join(preoptFlags, " "))
352 }
353
354 if module.UncompressedDex {
355 cmd.FlagWithArg("--copy-dex-files=", "false")
356 }
357
358 if !anyHavePrefix(preoptFlags, "--compiler-filter=") {
359 var compilerFilter string
360 if contains(global.SystemServerJars, module.Name) {
361 // Jars of system server, use the product option if it is set, speed otherwise.
362 if global.SystemServerCompilerFilter != "" {
363 compilerFilter = global.SystemServerCompilerFilter
364 } else {
365 compilerFilter = "speed"
366 }
367 } else if contains(global.SpeedApps, module.Name) || contains(global.SystemServerApps, module.Name) {
368 // Apps loaded into system server, and apps the product default to being compiled with the
369 // 'speed' compiler filter.
370 compilerFilter = "speed"
371 } else if profile != "" {
372 // For non system server jars, use speed-profile when we have a profile.
373 compilerFilter = "speed-profile"
374 } else if global.DefaultCompilerFilter != "" {
375 compilerFilter = global.DefaultCompilerFilter
376 } else {
377 compilerFilter = "quicken"
378 }
379 cmd.FlagWithArg("--compiler-filter=", compilerFilter)
380 }
381
382 if generateDM {
383 cmd.FlagWithArg("--copy-dex-files=", "false")
384 dmPath := filepath.Join(filepath.Dir(module.BuildPath), "generated.dm")
385 dmInstalledPath := pathtools.ReplaceExtension(module.DexLocation, "dm")
386 tmpPath := filepath.Join(filepath.Dir(module.BuildPath), "primary.vdex")
387 rule.Command().Text("cp -f").Input(vdexPath).Output(tmpPath)
388 rule.Command().Tool(global.Tools.SoongZip).
389 FlagWithArg("-L", "9").
390 FlagWithOutput("-o", dmPath).
391 Flag("-j").
392 Input(tmpPath)
393 rule.Install(dmPath, dmInstalledPath)
394 }
395
396 // By default, emit debug info.
397 debugInfo := true
398 if global.NoDebugInfo {
399 // If the global setting suppresses mini-debug-info, disable it.
400 debugInfo = false
401 }
402
403 // PRODUCT_SYSTEM_SERVER_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO.
404 // PRODUCT_OTHER_JAVA_DEBUG_INFO overrides WITH_DEXPREOPT_DEBUG_INFO.
405 if contains(global.SystemServerJars, module.Name) {
406 if global.AlwaysSystemServerDebugInfo {
407 debugInfo = true
408 } else if global.NeverSystemServerDebugInfo {
409 debugInfo = false
410 }
411 } else {
412 if global.AlwaysOtherDebugInfo {
413 debugInfo = true
414 } else if global.NeverOtherDebugInfo {
415 debugInfo = false
416 }
417 }
418
419 // Never enable on eng.
420 if global.IsEng {
421 debugInfo = false
422 }
423
424 if debugInfo {
425 cmd.Flag("--generate-mini-debug-info")
426 } else {
427 cmd.Flag("--no-generate-mini-debug-info")
428 }
429
430 // Set the compiler reason to 'prebuilt' to identify the oat files produced
431 // during the build, as opposed to compiled on the device.
432 cmd.FlagWithArg("--compilation-reason=", "prebuilt")
433
434 if appImage {
435 appImagePath := pathtools.ReplaceExtension(odexPath, "art")
436 appImageInstallPath := pathtools.ReplaceExtension(odexInstallPath, "art")
437 cmd.FlagWithOutput("--app-image-file=", appImagePath).
438 FlagWithArg("--image-format=", "lz4")
439 rule.Install(appImagePath, appImageInstallPath)
440 }
441
442 if profile != "" {
443 cmd.FlagWithArg("--profile-file=", profile)
444 }
445
446 rule.Install(odexPath, odexInstallPath)
447 rule.Install(vdexPath, vdexInstallPath)
448}
449
450// Return if the dex file in the APK should be stripped. If an APK is found to contain uncompressed dex files at
451// dex2oat time it will not be stripped even if strip=true.
452func shouldStripDex(module ModuleConfig, global GlobalConfig) bool {
453 strip := !global.DefaultNoStripping
454
Colin Crosscbed6572019-01-08 17:38:37 -0800455 if dexpreoptDisabled(global, module) {
456 strip = false
457 }
458
Colin Cross8c6d2502019-01-09 21:09:14 -0800459 if module.NoStripping {
460 strip = false
461 }
462
Colin Cross43f08db2018-11-12 10:13:39 -0800463 // Don't strip modules that are not on the system partition in case the oat/vdex version in system ROM
464 // doesn't match the one in other partitions. It needs to be able to fall back to the APK for that case.
465 if !strings.HasPrefix(module.DexLocation, SystemPartition) {
466 strip = false
467 }
468
469 // system_other isn't there for an OTA, so don't strip if module is on system, and odex is on system_other.
470 if odexOnSystemOther(module, global) {
471 strip = false
472 }
473
474 if module.HasApkLibraries {
475 strip = false
476 }
477
478 // Don't strip with dex files we explicitly uncompress (dexopt will not store the dex code).
479 if module.UncompressedDex {
480 strip = false
481 }
482
483 if shouldGenerateDM(module, global) {
484 strip = false
485 }
486
487 if module.PresignedPrebuilt {
488 // Only strip out files if we can re-sign the package.
489 strip = false
490 }
491
492 return strip
493}
494
495func shouldGenerateDM(module ModuleConfig, global GlobalConfig) bool {
496 // Generating DM files only makes sense for verify, avoid doing for non verify compiler filter APKs.
497 // No reason to use a dm file if the dex is already uncompressed.
498 return global.GenerateDMFiles && !module.UncompressedDex &&
499 contains(module.PreoptFlags, "--compiler-filter=verify")
500}
501
502func odexOnSystemOther(module ModuleConfig, global GlobalConfig) bool {
503 if !global.HasSystemOther {
504 return false
505 }
506
507 if global.SanitizeLite {
508 return false
509 }
510
511 if contains(global.SpeedApps, module.Name) || contains(global.SystemServerApps, module.Name) {
512 return false
513 }
514
515 for _, f := range global.PatternsOnSystemOther {
516 if makefileMatch(filepath.Join(SystemPartition, f), module.DexLocation) {
517 return true
518 }
519 }
520
521 return false
522}
523
524func pathForLibrary(module ModuleConfig, lib string) string {
525 path := module.LibraryPaths[lib]
526 if path == "" {
527 panic(fmt.Errorf("unknown library path for %q", lib))
528 }
529 return path
530}
531
532func makefileMatch(pattern, s string) bool {
533 percent := strings.IndexByte(pattern, '%')
534 switch percent {
535 case -1:
536 return pattern == s
537 case len(pattern) - 1:
538 return strings.HasPrefix(s, pattern[:len(pattern)-1])
539 default:
540 panic(fmt.Errorf("unsupported makefile pattern %q", pattern))
541 }
542}
543
544func contains(l []string, s string) bool {
545 for _, e := range l {
546 if e == s {
547 return true
548 }
549 }
550 return false
551}
552
553// remove all elements in a from b, returning a new slice
554func filterOut(a []string, b []string) []string {
555 var ret []string
556 for _, x := range b {
557 if !contains(a, x) {
558 ret = append(ret, x)
559 }
560 }
561 return ret
562}
563
564func replace(l []string, from, to string) {
565 for i := range l {
566 if l[i] == from {
567 l[i] = to
568 }
569 }
570}
571
572func copyOf(l []string) []string {
573 return append([]string(nil), l...)
574}
575
576func anyHavePrefix(l []string, prefix string) bool {
577 for _, x := range l {
578 if strings.HasPrefix(x, prefix) {
579 return true
580 }
581 }
582 return false
583}