| 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) | 
|  | 26 | } | 
|  | 27 |  | 
|  | 28 | var androidResourceIgnoreFilenames = []string{ | 
|  | 29 | ".svn", | 
|  | 30 | ".git", | 
|  | 31 | ".ds_store", | 
|  | 32 | "*.scc", | 
|  | 33 | ".*", | 
|  | 34 | "CVS", | 
|  | 35 | "thumbs.db", | 
|  | 36 | "picasa.ini", | 
|  | 37 | "*~", | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | func androidResourceGlob(ctx android.ModuleContext, dir android.Path) android.Paths { | 
|  | 41 | return ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), androidResourceIgnoreFilenames) | 
|  | 42 | } | 
|  | 43 |  | 
| Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 44 | type overlayType int | 
|  | 45 |  | 
|  | 46 | const ( | 
|  | 47 | device overlayType = iota + 1 | 
|  | 48 | product | 
|  | 49 | ) | 
|  | 50 |  | 
|  | 51 | type rroDir struct { | 
|  | 52 | path        android.Path | 
|  | 53 | overlayType overlayType | 
|  | 54 | } | 
|  | 55 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 56 | type overlayGlobResult struct { | 
| Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 57 | dir         string | 
|  | 58 | paths       android.DirectorySortedPaths | 
|  | 59 | overlayType overlayType | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 60 | } | 
|  | 61 |  | 
| Colin Cross | 571cccf | 2019-02-04 11:22:08 -0800 | [diff] [blame] | 62 | var overlayDataKey = android.NewOnceKey("overlayDataKey") | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 63 |  | 
|  | 64 | type globbedResourceDir struct { | 
|  | 65 | dir   android.Path | 
|  | 66 | files android.Paths | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | func overlayResourceGlob(ctx android.ModuleContext, dir android.Path) (res []globbedResourceDir, | 
| Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 70 | rroDirs []rroDir) { | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 71 |  | 
|  | 72 | overlayData := ctx.Config().Get(overlayDataKey).([]overlayGlobResult) | 
|  | 73 |  | 
|  | 74 | // Runtime resource overlays (RRO) may be turned on by the product config for some modules | 
|  | 75 | rroEnabled := ctx.Config().EnforceRROForModule(ctx.ModuleName()) | 
|  | 76 |  | 
|  | 77 | for _, data := range overlayData { | 
|  | 78 | files := data.paths.PathsInDirectory(filepath.Join(data.dir, dir.String())) | 
|  | 79 | if len(files) > 0 { | 
|  | 80 | overlayModuleDir := android.PathForSource(ctx, data.dir, dir.String()) | 
| Anton Hansson | 94c93f3 | 2019-01-30 16:03:37 +0000 | [diff] [blame] | 81 |  | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 82 | // If enforce RRO is enabled for this module and this overlay is not in the | 
|  | 83 | // exclusion list, ignore the overlay.  The list of ignored overlays will be | 
|  | 84 | // passed to Make to be turned into an RRO package. | 
| Anton Hansson | 94c93f3 | 2019-01-30 16:03:37 +0000 | [diff] [blame] | 85 | if rroEnabled && !ctx.Config().EnforceRROExcludedOverlay(overlayModuleDir.String()) { | 
| Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 86 | rroDirs = append(rroDirs, rroDir{overlayModuleDir, data.overlayType}) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 87 | } else { | 
|  | 88 | res = append(res, globbedResourceDir{ | 
|  | 89 | dir:   overlayModuleDir, | 
|  | 90 | files: files, | 
|  | 91 | }) | 
|  | 92 | } | 
|  | 93 | } | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | return res, rroDirs | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | func OverlaySingletonFactory() android.Singleton { | 
|  | 100 | return overlaySingleton{} | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | type overlaySingleton struct{} | 
|  | 104 |  | 
|  | 105 | func (overlaySingleton) GenerateBuildActions(ctx android.SingletonContext) { | 
|  | 106 | var overlayData []overlayGlobResult | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 107 |  | 
| Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 108 | appendOverlayData := func(overlayDirs []string, t overlayType) { | 
|  | 109 | for i := range overlayDirs { | 
|  | 110 | // Iterate backwards through the list of overlay directories so that the later, lower-priority | 
|  | 111 | // directories in the list show up earlier in the command line to aapt2. | 
|  | 112 | overlay := overlayDirs[len(overlayDirs)-1-i] | 
|  | 113 | var result overlayGlobResult | 
|  | 114 | result.dir = overlay | 
|  | 115 | result.overlayType = t | 
|  | 116 |  | 
|  | 117 | files, err := ctx.GlobWithDeps(filepath.Join(overlay, "**/*"), androidResourceIgnoreFilenames) | 
|  | 118 | if err != nil { | 
|  | 119 | ctx.Errorf("failed to glob resource dir %q: %s", overlay, err.Error()) | 
|  | 120 | continue | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 121 | } | 
| Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 122 | var paths android.Paths | 
|  | 123 | for _, f := range files { | 
|  | 124 | if !strings.HasSuffix(f, "/") { | 
|  | 125 | paths = append(paths, android.PathForSource(ctx, f)) | 
|  | 126 | } | 
|  | 127 | } | 
|  | 128 | result.paths = android.PathsToDirectorySortedPaths(paths) | 
|  | 129 | overlayData = append(overlayData, result) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 130 | } | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 131 | } | 
|  | 132 |  | 
| Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 133 | appendOverlayData(ctx.Config().DeviceResourceOverlays(), device) | 
|  | 134 | appendOverlayData(ctx.Config().ProductResourceOverlays(), product) | 
| Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 135 | ctx.Config().Once(overlayDataKey, func() interface{} { | 
|  | 136 | return overlayData | 
|  | 137 | }) | 
|  | 138 | } |