blob: 44cb709e4703d4fd67399613de32e7d771d3f106 [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)
26}
27
28var androidResourceIgnoreFilenames = []string{
29 ".svn",
30 ".git",
31 ".ds_store",
32 "*.scc",
33 ".*",
34 "CVS",
35 "thumbs.db",
36 "picasa.ini",
37 "*~",
38}
39
40func androidResourceGlob(ctx android.ModuleContext, dir android.Path) android.Paths {
41 return ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), androidResourceIgnoreFilenames)
42}
43
44type overlayGlobResult struct {
45 dir string
46 paths android.DirectorySortedPaths
Colin Crossa97c5d32018-03-28 14:58:31 -070047}
48
Colin Cross571cccf2019-02-04 11:22:08 -080049var overlayDataKey = android.NewOnceKey("overlayDataKey")
Colin Crossa97c5d32018-03-28 14:58:31 -070050
51type globbedResourceDir struct {
52 dir android.Path
53 files android.Paths
54}
55
56func overlayResourceGlob(ctx android.ModuleContext, dir android.Path) (res []globbedResourceDir,
57 rroDirs android.Paths) {
58
59 overlayData := ctx.Config().Get(overlayDataKey).([]overlayGlobResult)
60
61 // Runtime resource overlays (RRO) may be turned on by the product config for some modules
62 rroEnabled := ctx.Config().EnforceRROForModule(ctx.ModuleName())
63
64 for _, data := range overlayData {
65 files := data.paths.PathsInDirectory(filepath.Join(data.dir, dir.String()))
66 if len(files) > 0 {
67 overlayModuleDir := android.PathForSource(ctx, data.dir, dir.String())
Anton Hansson94c93f32019-01-30 16:03:37 +000068
Colin Crossa97c5d32018-03-28 14:58:31 -070069 // If enforce RRO is enabled for this module and this overlay is not in the
70 // exclusion list, ignore the overlay. The list of ignored overlays will be
71 // passed to Make to be turned into an RRO package.
Anton Hansson94c93f32019-01-30 16:03:37 +000072 if rroEnabled && !ctx.Config().EnforceRROExcludedOverlay(overlayModuleDir.String()) {
Colin Crossa97c5d32018-03-28 14:58:31 -070073 rroDirs = append(rroDirs, overlayModuleDir)
74 } else {
75 res = append(res, globbedResourceDir{
76 dir: overlayModuleDir,
77 files: files,
78 })
79 }
80 }
81 }
82
83 return res, rroDirs
84}
85
86func OverlaySingletonFactory() android.Singleton {
87 return overlaySingleton{}
88}
89
90type overlaySingleton struct{}
91
92func (overlaySingleton) GenerateBuildActions(ctx android.SingletonContext) {
93 var overlayData []overlayGlobResult
94 overlayDirs := ctx.Config().ResourceOverlays()
95 for i := range overlayDirs {
96 // Iterate backwards through the list of overlay directories so that the later, lower-priority
97 // directories in the list show up earlier in the command line to aapt2.
98 overlay := overlayDirs[len(overlayDirs)-1-i]
99 var result overlayGlobResult
100 result.dir = overlay
101
Colin Crossa97c5d32018-03-28 14:58:31 -0700102 files, err := ctx.GlobWithDeps(filepath.Join(overlay, "**/*"), androidResourceIgnoreFilenames)
103 if err != nil {
104 ctx.Errorf("failed to glob resource dir %q: %s", overlay, err.Error())
105 continue
106 }
107 var paths android.Paths
108 for _, f := range files {
109 if !strings.HasSuffix(f, "/") {
110 paths = append(paths, android.PathForSource(ctx, f))
111 }
112 }
113 result.paths = android.PathsToDirectorySortedPaths(paths)
114 overlayData = append(overlayData, result)
115 }
116
117 ctx.Config().Once(overlayDataKey, func() interface{} {
118 return overlayData
119 })
120}