blob: 8c5908f69b9b704ba66cc01b05834a907a419b2c [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
24func init() {
Paul Duffin42da69d2021-03-22 13:41:36 +000025 registerOverlayBuildComponents(android.InitRegistrationContext)
26}
Colin Crossae8600b2020-10-29 17:09:13 -070027
Paul Duffin42da69d2021-03-22 13:41:36 +000028func registerOverlayBuildComponents(ctx android.RegistrationContext) {
29 ctx.RegisterPreSingletonType("overlay", OverlaySingletonFactory)
Colin Crossa97c5d32018-03-28 14:58:31 -070030}
31
32var androidResourceIgnoreFilenames = []string{
33 ".svn",
34 ".git",
35 ".ds_store",
36 "*.scc",
37 ".*",
38 "CVS",
39 "thumbs.db",
40 "picasa.ini",
41 "*~",
42}
43
Colin Crossc20dc852020-11-10 12:27:45 -080044// androidResourceGlob returns the list of files in the given directory, using the standard
45// exclusion patterns for Android resources.
Romain Jobredeaux1282c422021-10-29 10:52:59 -040046func androidResourceGlob(ctx android.EarlyModuleContext, dir android.Path) android.Paths {
Colin Crossa97c5d32018-03-28 14:58:31 -070047 return ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), androidResourceIgnoreFilenames)
48}
49
Colin Crossc20dc852020-11-10 12:27:45 -080050// androidResourceGlobList creates a rule to write the list of files in the given directory, using
51// the standard exclusion patterns for Android resources, to the given output file.
52func androidResourceGlobList(ctx android.ModuleContext, dir android.Path,
53 fileListFile android.WritablePath) {
54
55 android.GlobToListFileRule(ctx, filepath.Join(dir.String(), "**/*"),
56 androidResourceIgnoreFilenames, fileListFile)
57}
58
Anton Hansson53c88442019-03-18 15:53:16 +000059type overlayType int
60
61const (
62 device overlayType = iota + 1
63 product
64)
65
66type rroDir struct {
67 path android.Path
68 overlayType overlayType
69}
70
Colin Crossa97c5d32018-03-28 14:58:31 -070071type overlayGlobResult struct {
Anton Hansson53c88442019-03-18 15:53:16 +000072 dir string
73 paths android.DirectorySortedPaths
74 overlayType overlayType
Colin Crossa97c5d32018-03-28 14:58:31 -070075}
76
Colin Cross571cccf2019-02-04 11:22:08 -080077var overlayDataKey = android.NewOnceKey("overlayDataKey")
Colin Crossa97c5d32018-03-28 14:58:31 -070078
79type globbedResourceDir struct {
80 dir android.Path
81 files android.Paths
82}
83
Jaewoong Jungc779cd42020-10-06 18:56:10 -070084func overlayResourceGlob(ctx android.ModuleContext, a *aapt, dir android.Path) (res []globbedResourceDir,
Anton Hansson53c88442019-03-18 15:53:16 +000085 rroDirs []rroDir) {
Colin Crossa97c5d32018-03-28 14:58:31 -070086
87 overlayData := ctx.Config().Get(overlayDataKey).([]overlayGlobResult)
88
89 // Runtime resource overlays (RRO) may be turned on by the product config for some modules
Jaewoong Jungc779cd42020-10-06 18:56:10 -070090 rroEnabled := a.IsRROEnforced(ctx)
Colin Crossa97c5d32018-03-28 14:58:31 -070091
92 for _, data := range overlayData {
93 files := data.paths.PathsInDirectory(filepath.Join(data.dir, dir.String()))
94 if len(files) > 0 {
95 overlayModuleDir := android.PathForSource(ctx, data.dir, dir.String())
Anton Hansson94c93f32019-01-30 16:03:37 +000096
Colin Crossa97c5d32018-03-28 14:58:31 -070097 // If enforce RRO is enabled for this module and this overlay is not in the
98 // exclusion list, ignore the overlay. The list of ignored overlays will be
99 // passed to Make to be turned into an RRO package.
Anton Hansson94c93f32019-01-30 16:03:37 +0000100 if rroEnabled && !ctx.Config().EnforceRROExcludedOverlay(overlayModuleDir.String()) {
Anton Hansson53c88442019-03-18 15:53:16 +0000101 rroDirs = append(rroDirs, rroDir{overlayModuleDir, data.overlayType})
Colin Crossa97c5d32018-03-28 14:58:31 -0700102 } else {
103 res = append(res, globbedResourceDir{
104 dir: overlayModuleDir,
105 files: files,
106 })
107 }
108 }
109 }
110
111 return res, rroDirs
112}
113
114func OverlaySingletonFactory() android.Singleton {
115 return overlaySingleton{}
116}
117
118type overlaySingleton struct{}
119
120func (overlaySingleton) GenerateBuildActions(ctx android.SingletonContext) {
121 var overlayData []overlayGlobResult
Colin Crossa97c5d32018-03-28 14:58:31 -0700122
Anton Hansson53c88442019-03-18 15:53:16 +0000123 appendOverlayData := func(overlayDirs []string, t overlayType) {
124 for i := range overlayDirs {
125 // Iterate backwards through the list of overlay directories so that the later, lower-priority
126 // directories in the list show up earlier in the command line to aapt2.
127 overlay := overlayDirs[len(overlayDirs)-1-i]
128 var result overlayGlobResult
129 result.dir = overlay
130 result.overlayType = t
131
132 files, err := ctx.GlobWithDeps(filepath.Join(overlay, "**/*"), androidResourceIgnoreFilenames)
133 if err != nil {
134 ctx.Errorf("failed to glob resource dir %q: %s", overlay, err.Error())
135 continue
Colin Crossa97c5d32018-03-28 14:58:31 -0700136 }
Anton Hansson53c88442019-03-18 15:53:16 +0000137 var paths android.Paths
138 for _, f := range files {
139 if !strings.HasSuffix(f, "/") {
140 paths = append(paths, android.PathForSource(ctx, f))
141 }
142 }
143 result.paths = android.PathsToDirectorySortedPaths(paths)
144 overlayData = append(overlayData, result)
Colin Crossa97c5d32018-03-28 14:58:31 -0700145 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700146 }
147
Anton Hansson53c88442019-03-18 15:53:16 +0000148 appendOverlayData(ctx.Config().DeviceResourceOverlays(), device)
149 appendOverlayData(ctx.Config().ProductResourceOverlays(), product)
Colin Crossa97c5d32018-03-28 14:58:31 -0700150 ctx.Config().Once(overlayDataKey, func() interface{} {
151 return overlayData
152 })
153}