android_resources.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // 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. package java
  15. import (
  16. "path/filepath"
  17. "strings"
  18. "android/soong/android"
  19. )
  20. func init() {
  21. registerOverlayBuildComponents(android.InitRegistrationContext)
  22. }
  23. func registerOverlayBuildComponents(ctx android.RegistrationContext) {
  24. ctx.RegisterPreSingletonType("overlay", OverlaySingletonFactory)
  25. }
  26. var androidResourceIgnoreFilenames = []string{
  27. ".svn",
  28. ".git",
  29. ".ds_store",
  30. "*.scc",
  31. ".*",
  32. "CVS",
  33. "thumbs.db",
  34. "picasa.ini",
  35. "*~",
  36. }
  37. // androidResourceGlob returns the list of files in the given directory, using the standard
  38. // exclusion patterns for Android resources.
  39. func androidResourceGlob(ctx android.EarlyModuleContext, dir android.Path) android.Paths {
  40. return ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), androidResourceIgnoreFilenames)
  41. }
  42. // androidResourceGlobList creates a rule to write the list of files in the given directory, using
  43. // the standard exclusion patterns for Android resources, to the given output file.
  44. func androidResourceGlobList(ctx android.ModuleContext, dir android.Path,
  45. fileListFile android.WritablePath) {
  46. android.GlobToListFileRule(ctx, filepath.Join(dir.String(), "**/*"),
  47. androidResourceIgnoreFilenames, fileListFile)
  48. }
  49. type overlayType int
  50. const (
  51. device overlayType = iota + 1
  52. product
  53. )
  54. type rroDir struct {
  55. path android.Path
  56. overlayType overlayType
  57. }
  58. type overlayGlobResult struct {
  59. dir string
  60. paths android.DirectorySortedPaths
  61. overlayType overlayType
  62. }
  63. var overlayDataKey = android.NewOnceKey("overlayDataKey")
  64. type globbedResourceDir struct {
  65. dir android.Path
  66. files android.Paths
  67. }
  68. func overlayResourceGlob(ctx android.ModuleContext, a *aapt, dir android.Path) (res []globbedResourceDir,
  69. rroDirs []rroDir) {
  70. overlayData := ctx.Config().Get(overlayDataKey).([]overlayGlobResult)
  71. // Runtime resource overlays (RRO) may be turned on by the product config for some modules
  72. rroEnabled := a.IsRROEnforced(ctx)
  73. for _, data := range overlayData {
  74. files := data.paths.PathsInDirectory(filepath.Join(data.dir, dir.String()))
  75. if len(files) > 0 {
  76. overlayModuleDir := android.PathForSource(ctx, data.dir, dir.String())
  77. // If enforce RRO is enabled for this module and this overlay is not in the
  78. // exclusion list, ignore the overlay. The list of ignored overlays will be
  79. // passed to Make to be turned into an RRO package.
  80. if rroEnabled && !ctx.Config().EnforceRROExcludedOverlay(overlayModuleDir.String()) {
  81. rroDirs = append(rroDirs, rroDir{overlayModuleDir, data.overlayType})
  82. } else {
  83. res = append(res, globbedResourceDir{
  84. dir: overlayModuleDir,
  85. files: files,
  86. })
  87. }
  88. }
  89. }
  90. return res, rroDirs
  91. }
  92. func OverlaySingletonFactory() android.Singleton {
  93. return overlaySingleton{}
  94. }
  95. type overlaySingleton struct{}
  96. func (overlaySingleton) GenerateBuildActions(ctx android.SingletonContext) {
  97. var overlayData []overlayGlobResult
  98. appendOverlayData := func(overlayDirs []string, t overlayType) {
  99. for i := range overlayDirs {
  100. // Iterate backwards through the list of overlay directories so that the later, lower-priority
  101. // directories in the list show up earlier in the command line to aapt2.
  102. overlay := overlayDirs[len(overlayDirs)-1-i]
  103. var result overlayGlobResult
  104. result.dir = overlay
  105. result.overlayType = t
  106. files, err := ctx.GlobWithDeps(filepath.Join(overlay, "**/*"), androidResourceIgnoreFilenames)
  107. if err != nil {
  108. ctx.Errorf("failed to glob resource dir %q: %s", overlay, err.Error())
  109. continue
  110. }
  111. var paths android.Paths
  112. for _, f := range files {
  113. if !strings.HasSuffix(f, "/") {
  114. paths = append(paths, android.PathForSource(ctx, f))
  115. }
  116. }
  117. result.paths = android.PathsToDirectorySortedPaths(paths)
  118. overlayData = append(overlayData, result)
  119. }
  120. }
  121. appendOverlayData(ctx.Config().DeviceResourceOverlays(), device)
  122. appendOverlayData(ctx.Config().ProductResourceOverlays(), product)
  123. ctx.Config().Once(overlayDataKey, func() interface{} {
  124. return overlayData
  125. })
  126. }