Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
| 18 | "path/filepath" |
| 19 | "strings" |
| 20 | |
| 21 | "android/soong/android" |
| 22 | ) |
| 23 | |
| 24 | func init() { |
| 25 | android.RegisterPreSingletonType("overlay", OverlaySingletonFactory) |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 26 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | var androidResourceIgnoreFilenames = []string{ |
| 30 | ".svn", |
| 31 | ".git", |
| 32 | ".ds_store", |
| 33 | "*.scc", |
| 34 | ".*", |
| 35 | "CVS", |
| 36 | "thumbs.db", |
| 37 | "picasa.ini", |
| 38 | "*~", |
| 39 | } |
| 40 | |
Colin Cross | c20dc85 | 2020-11-10 12:27:45 -0800 | [diff] [blame^] | 41 | // androidResourceGlob returns the list of files in the given directory, using the standard |
| 42 | // exclusion patterns for Android resources. |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 43 | func androidResourceGlob(ctx android.ModuleContext, dir android.Path) android.Paths { |
| 44 | return ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), androidResourceIgnoreFilenames) |
| 45 | } |
| 46 | |
Colin Cross | c20dc85 | 2020-11-10 12:27:45 -0800 | [diff] [blame^] | 47 | // 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. |
| 49 | func 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 Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 56 | type overlayType int |
| 57 | |
| 58 | const ( |
| 59 | device overlayType = iota + 1 |
| 60 | product |
| 61 | ) |
| 62 | |
| 63 | type rroDir struct { |
| 64 | path android.Path |
| 65 | overlayType overlayType |
| 66 | } |
| 67 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 68 | type overlayGlobResult struct { |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 69 | dir string |
| 70 | paths android.DirectorySortedPaths |
| 71 | overlayType overlayType |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 74 | var overlayDataKey = android.NewOnceKey("overlayDataKey") |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 75 | |
| 76 | type globbedResourceDir struct { |
| 77 | dir android.Path |
| 78 | files android.Paths |
| 79 | } |
| 80 | |
Jaewoong Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 81 | func overlayResourceGlob(ctx android.ModuleContext, a *aapt, dir android.Path) (res []globbedResourceDir, |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 82 | rroDirs []rroDir) { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 83 | |
| 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 Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 87 | rroEnabled := a.IsRROEnforced(ctx) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 88 | |
| 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 Hansson | 94c93f3 | 2019-01-30 16:03:37 +0000 | [diff] [blame] | 93 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 94 | // 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 Hansson | 94c93f3 | 2019-01-30 16:03:37 +0000 | [diff] [blame] | 97 | if rroEnabled && !ctx.Config().EnforceRROExcludedOverlay(overlayModuleDir.String()) { |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 98 | rroDirs = append(rroDirs, rroDir{overlayModuleDir, data.overlayType}) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 99 | } else { |
| 100 | res = append(res, globbedResourceDir{ |
| 101 | dir: overlayModuleDir, |
| 102 | files: files, |
| 103 | }) |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return res, rroDirs |
| 109 | } |
| 110 | |
| 111 | func OverlaySingletonFactory() android.Singleton { |
| 112 | return overlaySingleton{} |
| 113 | } |
| 114 | |
| 115 | type overlaySingleton struct{} |
| 116 | |
| 117 | func (overlaySingleton) GenerateBuildActions(ctx android.SingletonContext) { |
| 118 | var overlayData []overlayGlobResult |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 119 | |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 120 | 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 Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 133 | } |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 134 | 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 Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 142 | } |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 145 | appendOverlayData(ctx.Config().DeviceResourceOverlays(), device) |
| 146 | appendOverlayData(ctx.Config().ProductResourceOverlays(), product) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 147 | ctx.Config().Once(overlayDataKey, func() interface{} { |
| 148 | return overlayData |
| 149 | }) |
| 150 | } |