blob: 3bb3eb554c6e07961aff3e4a3fbe04957f59c13f [file] [log] [blame]
Colin Crossa97c5d32018-03-28 14:58:31 -07001// 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
15package java
16
17import (
18 "path/filepath"
19 "strings"
20
21 "android/soong/android"
22)
23
Colin Crossa97c5d32018-03-28 14:58:31 -070024var androidResourceIgnoreFilenames = []string{
25 ".svn",
26 ".git",
27 ".ds_store",
28 "*.scc",
29 ".*",
30 "CVS",
31 "thumbs.db",
32 "picasa.ini",
33 "*~",
34}
35
Colin Crossc20dc852020-11-10 12:27:45 -080036// androidResourceGlob returns the list of files in the given directory, using the standard
37// exclusion patterns for Android resources.
Romain Jobredeaux1282c422021-10-29 10:52:59 -040038func androidResourceGlob(ctx android.EarlyModuleContext, dir android.Path) android.Paths {
Colin Crossa97c5d32018-03-28 14:58:31 -070039 return ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), androidResourceIgnoreFilenames)
40}
41
Anton Hansson53c88442019-03-18 15:53:16 +000042type overlayType int
43
44const (
45 device overlayType = iota + 1
46 product
47)
48
49type rroDir struct {
50 path android.Path
51 overlayType overlayType
52}
53
Colin Crossa97c5d32018-03-28 14:58:31 -070054type overlayGlobResult struct {
Anton Hansson53c88442019-03-18 15:53:16 +000055 dir string
56 paths android.DirectorySortedPaths
57 overlayType overlayType
Colin Crossa97c5d32018-03-28 14:58:31 -070058}
59
Colin Cross571cccf2019-02-04 11:22:08 -080060var overlayDataKey = android.NewOnceKey("overlayDataKey")
Colin Crossa97c5d32018-03-28 14:58:31 -070061
62type globbedResourceDir struct {
63 dir android.Path
64 files android.Paths
65}
66
Jaewoong Jungc779cd42020-10-06 18:56:10 -070067func overlayResourceGlob(ctx android.ModuleContext, a *aapt, dir android.Path) (res []globbedResourceDir,
Anton Hansson53c88442019-03-18 15:53:16 +000068 rroDirs []rroDir) {
Colin Crossa97c5d32018-03-28 14:58:31 -070069
Cole Faust9bef6742023-11-01 15:29:09 -070070 overlayData := ctx.Config().Once(overlayDataKey, func() interface{} {
71 var overlayData []overlayGlobResult
72
73 appendOverlayData := func(overlayDirs []string, t overlayType) {
74 for i := range overlayDirs {
75 // Iterate backwards through the list of overlay directories so that the later, lower-priority
76 // directories in the list show up earlier in the command line to aapt2.
77 overlay := overlayDirs[len(overlayDirs)-1-i]
78 var result overlayGlobResult
79 result.dir = overlay
80 result.overlayType = t
81
82 files, err := ctx.GlobWithDeps(filepath.Join(overlay, "**/*"), androidResourceIgnoreFilenames)
83 if err != nil {
84 ctx.ModuleErrorf("failed to glob resource dir %q: %s", overlay, err.Error())
85 continue
86 }
87 var paths android.Paths
88 for _, f := range files {
89 if !strings.HasSuffix(f, "/") {
90 paths = append(paths, android.PathForSource(ctx, f))
91 }
92 }
93 result.paths = android.PathsToDirectorySortedPaths(paths)
94 overlayData = append(overlayData, result)
95 }
96 }
97
98 appendOverlayData(ctx.Config().DeviceResourceOverlays(), device)
99 appendOverlayData(ctx.Config().ProductResourceOverlays(), product)
100 return overlayData
101 }).([]overlayGlobResult)
Colin Crossa97c5d32018-03-28 14:58:31 -0700102
103 // Runtime resource overlays (RRO) may be turned on by the product config for some modules
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700104 rroEnabled := a.IsRROEnforced(ctx)
Colin Crossa97c5d32018-03-28 14:58:31 -0700105
106 for _, data := range overlayData {
107 files := data.paths.PathsInDirectory(filepath.Join(data.dir, dir.String()))
108 if len(files) > 0 {
109 overlayModuleDir := android.PathForSource(ctx, data.dir, dir.String())
Anton Hansson94c93f32019-01-30 16:03:37 +0000110
Colin Crossa97c5d32018-03-28 14:58:31 -0700111 // If enforce RRO is enabled for this module and this overlay is not in the
112 // exclusion list, ignore the overlay. The list of ignored overlays will be
113 // passed to Make to be turned into an RRO package.
Anton Hansson94c93f32019-01-30 16:03:37 +0000114 if rroEnabled && !ctx.Config().EnforceRROExcludedOverlay(overlayModuleDir.String()) {
Anton Hansson53c88442019-03-18 15:53:16 +0000115 rroDirs = append(rroDirs, rroDir{overlayModuleDir, data.overlayType})
Colin Crossa97c5d32018-03-28 14:58:31 -0700116 } else {
117 res = append(res, globbedResourceDir{
118 dir: overlayModuleDir,
119 files: files,
120 })
121 }
122 }
123 }
124
125 return res, rroDirs
126}