blob: 720d3a5ae26d6c4c613138d47a22330953cea11c [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() {
25 android.RegisterPreSingletonType("overlay", OverlaySingletonFactory)
Colin Crossae8600b2020-10-29 17:09:13 -070026
Colin Crossa97c5d32018-03-28 14:58:31 -070027}
28
29var androidResourceIgnoreFilenames = []string{
30 ".svn",
31 ".git",
32 ".ds_store",
33 "*.scc",
34 ".*",
35 "CVS",
36 "thumbs.db",
37 "picasa.ini",
38 "*~",
39}
40
41func androidResourceGlob(ctx android.ModuleContext, dir android.Path) android.Paths {
42 return ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), androidResourceIgnoreFilenames)
43}
44
Anton Hansson53c88442019-03-18 15:53:16 +000045type overlayType int
46
47const (
48 device overlayType = iota + 1
49 product
50)
51
52type rroDir struct {
53 path android.Path
54 overlayType overlayType
55}
56
Colin Crossa97c5d32018-03-28 14:58:31 -070057type overlayGlobResult struct {
Anton Hansson53c88442019-03-18 15:53:16 +000058 dir string
59 paths android.DirectorySortedPaths
60 overlayType overlayType
Colin Crossa97c5d32018-03-28 14:58:31 -070061}
62
Colin Cross571cccf2019-02-04 11:22:08 -080063var overlayDataKey = android.NewOnceKey("overlayDataKey")
Colin Crossa97c5d32018-03-28 14:58:31 -070064
65type globbedResourceDir struct {
66 dir android.Path
67 files android.Paths
68}
69
Jaewoong Jungc779cd42020-10-06 18:56:10 -070070func overlayResourceGlob(ctx android.ModuleContext, a *aapt, dir android.Path) (res []globbedResourceDir,
Anton Hansson53c88442019-03-18 15:53:16 +000071 rroDirs []rroDir) {
Colin Crossa97c5d32018-03-28 14:58:31 -070072
73 overlayData := ctx.Config().Get(overlayDataKey).([]overlayGlobResult)
74
75 // Runtime resource overlays (RRO) may be turned on by the product config for some modules
Jaewoong Jungc779cd42020-10-06 18:56:10 -070076 rroEnabled := a.IsRROEnforced(ctx)
Colin Crossa97c5d32018-03-28 14:58:31 -070077
78 for _, data := range overlayData {
79 files := data.paths.PathsInDirectory(filepath.Join(data.dir, dir.String()))
80 if len(files) > 0 {
81 overlayModuleDir := android.PathForSource(ctx, data.dir, dir.String())
Anton Hansson94c93f32019-01-30 16:03:37 +000082
Colin Crossa97c5d32018-03-28 14:58:31 -070083 // If enforce RRO is enabled for this module and this overlay is not in the
84 // exclusion list, ignore the overlay. The list of ignored overlays will be
85 // passed to Make to be turned into an RRO package.
Anton Hansson94c93f32019-01-30 16:03:37 +000086 if rroEnabled && !ctx.Config().EnforceRROExcludedOverlay(overlayModuleDir.String()) {
Anton Hansson53c88442019-03-18 15:53:16 +000087 rroDirs = append(rroDirs, rroDir{overlayModuleDir, data.overlayType})
Colin Crossa97c5d32018-03-28 14:58:31 -070088 } else {
89 res = append(res, globbedResourceDir{
90 dir: overlayModuleDir,
91 files: files,
92 })
93 }
94 }
95 }
96
97 return res, rroDirs
98}
99
100func OverlaySingletonFactory() android.Singleton {
101 return overlaySingleton{}
102}
103
104type overlaySingleton struct{}
105
106func (overlaySingleton) GenerateBuildActions(ctx android.SingletonContext) {
107 var overlayData []overlayGlobResult
Colin Crossa97c5d32018-03-28 14:58:31 -0700108
Anton Hansson53c88442019-03-18 15:53:16 +0000109 appendOverlayData := func(overlayDirs []string, t overlayType) {
110 for i := range overlayDirs {
111 // Iterate backwards through the list of overlay directories so that the later, lower-priority
112 // directories in the list show up earlier in the command line to aapt2.
113 overlay := overlayDirs[len(overlayDirs)-1-i]
114 var result overlayGlobResult
115 result.dir = overlay
116 result.overlayType = t
117
118 files, err := ctx.GlobWithDeps(filepath.Join(overlay, "**/*"), androidResourceIgnoreFilenames)
119 if err != nil {
120 ctx.Errorf("failed to glob resource dir %q: %s", overlay, err.Error())
121 continue
Colin Crossa97c5d32018-03-28 14:58:31 -0700122 }
Anton Hansson53c88442019-03-18 15:53:16 +0000123 var paths android.Paths
124 for _, f := range files {
125 if !strings.HasSuffix(f, "/") {
126 paths = append(paths, android.PathForSource(ctx, f))
127 }
128 }
129 result.paths = android.PathsToDirectorySortedPaths(paths)
130 overlayData = append(overlayData, result)
Colin Crossa97c5d32018-03-28 14:58:31 -0700131 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700132 }
133
Anton Hansson53c88442019-03-18 15:53:16 +0000134 appendOverlayData(ctx.Config().DeviceResourceOverlays(), device)
135 appendOverlayData(ctx.Config().ProductResourceOverlays(), product)
Colin Crossa97c5d32018-03-28 14:58:31 -0700136 ctx.Config().Once(overlayDataKey, func() interface{} {
137 return overlayData
138 })
139}