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