blob: 4d420cfedb42cc27a2bf322b7f1d1500269565e9 [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
Colin Crossc20dc852020-11-10 12:27:45 -080041// androidResourceGlob returns the list of files in the given directory, using the standard
42// exclusion patterns for Android resources.
Colin Crossa97c5d32018-03-28 14:58:31 -070043func androidResourceGlob(ctx android.ModuleContext, dir android.Path) android.Paths {
44 return ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), androidResourceIgnoreFilenames)
45}
46
Colin Crossc20dc852020-11-10 12:27:45 -080047// androidResourceGlobList creates a rule to write the list of files in the given directory, using
48// the standard exclusion patterns for Android resources, to the given output file.
49func androidResourceGlobList(ctx android.ModuleContext, dir android.Path,
50 fileListFile android.WritablePath) {
51
52 android.GlobToListFileRule(ctx, filepath.Join(dir.String(), "**/*"),
53 androidResourceIgnoreFilenames, fileListFile)
54}
55
Anton Hansson53c88442019-03-18 15:53:16 +000056type overlayType int
57
58const (
59 device overlayType = iota + 1
60 product
61)
62
63type rroDir struct {
64 path android.Path
65 overlayType overlayType
66}
67
Colin Crossa97c5d32018-03-28 14:58:31 -070068type overlayGlobResult struct {
Anton Hansson53c88442019-03-18 15:53:16 +000069 dir string
70 paths android.DirectorySortedPaths
71 overlayType overlayType
Colin Crossa97c5d32018-03-28 14:58:31 -070072}
73
Colin Cross571cccf2019-02-04 11:22:08 -080074var overlayDataKey = android.NewOnceKey("overlayDataKey")
Colin Crossa97c5d32018-03-28 14:58:31 -070075
76type globbedResourceDir struct {
77 dir android.Path
78 files android.Paths
79}
80
Jaewoong Jungc779cd42020-10-06 18:56:10 -070081func overlayResourceGlob(ctx android.ModuleContext, a *aapt, dir android.Path) (res []globbedResourceDir,
Anton Hansson53c88442019-03-18 15:53:16 +000082 rroDirs []rroDir) {
Colin Crossa97c5d32018-03-28 14:58:31 -070083
84 overlayData := ctx.Config().Get(overlayDataKey).([]overlayGlobResult)
85
86 // Runtime resource overlays (RRO) may be turned on by the product config for some modules
Jaewoong Jungc779cd42020-10-06 18:56:10 -070087 rroEnabled := a.IsRROEnforced(ctx)
Colin Crossa97c5d32018-03-28 14:58:31 -070088
89 for _, data := range overlayData {
90 files := data.paths.PathsInDirectory(filepath.Join(data.dir, dir.String()))
91 if len(files) > 0 {
92 overlayModuleDir := android.PathForSource(ctx, data.dir, dir.String())
Anton Hansson94c93f32019-01-30 16:03:37 +000093
Colin Crossa97c5d32018-03-28 14:58:31 -070094 // If enforce RRO is enabled for this module and this overlay is not in the
95 // exclusion list, ignore the overlay. The list of ignored overlays will be
96 // passed to Make to be turned into an RRO package.
Anton Hansson94c93f32019-01-30 16:03:37 +000097 if rroEnabled && !ctx.Config().EnforceRROExcludedOverlay(overlayModuleDir.String()) {
Anton Hansson53c88442019-03-18 15:53:16 +000098 rroDirs = append(rroDirs, rroDir{overlayModuleDir, data.overlayType})
Colin Crossa97c5d32018-03-28 14:58:31 -070099 } else {
100 res = append(res, globbedResourceDir{
101 dir: overlayModuleDir,
102 files: files,
103 })
104 }
105 }
106 }
107
108 return res, rroDirs
109}
110
111func OverlaySingletonFactory() android.Singleton {
112 return overlaySingleton{}
113}
114
115type overlaySingleton struct{}
116
117func (overlaySingleton) GenerateBuildActions(ctx android.SingletonContext) {
118 var overlayData []overlayGlobResult
Colin Crossa97c5d32018-03-28 14:58:31 -0700119
Anton Hansson53c88442019-03-18 15:53:16 +0000120 appendOverlayData := func(overlayDirs []string, t overlayType) {
121 for i := range overlayDirs {
122 // Iterate backwards through the list of overlay directories so that the later, lower-priority
123 // directories in the list show up earlier in the command line to aapt2.
124 overlay := overlayDirs[len(overlayDirs)-1-i]
125 var result overlayGlobResult
126 result.dir = overlay
127 result.overlayType = t
128
129 files, err := ctx.GlobWithDeps(filepath.Join(overlay, "**/*"), androidResourceIgnoreFilenames)
130 if err != nil {
131 ctx.Errorf("failed to glob resource dir %q: %s", overlay, err.Error())
132 continue
Colin Crossa97c5d32018-03-28 14:58:31 -0700133 }
Anton Hansson53c88442019-03-18 15:53:16 +0000134 var paths android.Paths
135 for _, f := range files {
136 if !strings.HasSuffix(f, "/") {
137 paths = append(paths, android.PathForSource(ctx, f))
138 }
139 }
140 result.paths = android.PathsToDirectorySortedPaths(paths)
141 overlayData = append(overlayData, result)
Colin Crossa97c5d32018-03-28 14:58:31 -0700142 }
Colin Crossa97c5d32018-03-28 14:58:31 -0700143 }
144
Anton Hansson53c88442019-03-18 15:53:16 +0000145 appendOverlayData(ctx.Config().DeviceResourceOverlays(), device)
146 appendOverlayData(ctx.Config().ProductResourceOverlays(), product)
Colin Crossa97c5d32018-03-28 14:58:31 -0700147 ctx.Config().Once(overlayDataKey, func() interface{} {
148 return overlayData
149 })
150}