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