Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 1 | // Copyright 2020 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 android |
| 16 | |
| 17 | import ( |
| 18 | "sync" |
| 19 | |
| 20 | "github.com/google/blueprint" |
| 21 | ) |
| 22 | |
| 23 | var phonyMapOnceKey = NewOnceKey("phony") |
| 24 | |
| 25 | type phonyMap map[string]Paths |
| 26 | |
| 27 | var phonyMapLock sync.Mutex |
| 28 | |
Yu Liu | 5451362 | 2024-08-19 20:00:32 +0000 | [diff] [blame] | 29 | type ModulePhonyInfo struct { |
| 30 | Phonies map[string]Paths |
| 31 | } |
| 32 | |
| 33 | var ModulePhonyProvider = blueprint.NewProvider[ModulePhonyInfo]() |
| 34 | |
| 35 | func getSingletonPhonyMap(config Config) phonyMap { |
Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 36 | return config.Once(phonyMapOnceKey, func() interface{} { |
| 37 | return make(phonyMap) |
| 38 | }).(phonyMap) |
| 39 | } |
| 40 | |
Yu Liu | 5451362 | 2024-08-19 20:00:32 +0000 | [diff] [blame] | 41 | func addSingletonPhony(config Config, name string, deps ...Path) { |
| 42 | phonyMap := getSingletonPhonyMap(config) |
Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 43 | phonyMapLock.Lock() |
| 44 | defer phonyMapLock.Unlock() |
| 45 | phonyMap[name] = append(phonyMap[name], deps...) |
| 46 | } |
| 47 | |
| 48 | type phonySingleton struct { |
| 49 | phonyMap phonyMap |
| 50 | phonyList []string |
| 51 | } |
| 52 | |
| 53 | var _ SingletonMakeVarsProvider = (*phonySingleton)(nil) |
| 54 | |
| 55 | func (p *phonySingleton) GenerateBuildActions(ctx SingletonContext) { |
Yu Liu | 5451362 | 2024-08-19 20:00:32 +0000 | [diff] [blame] | 56 | p.phonyMap = getSingletonPhonyMap(ctx.Config()) |
| 57 | ctx.VisitAllModules(func(m Module) { |
| 58 | if info, ok := OtherModuleProvider(ctx, m, ModulePhonyProvider); ok { |
| 59 | for k, v := range info.Phonies { |
| 60 | p.phonyMap[k] = append(p.phonyMap[k], v...) |
| 61 | } |
| 62 | } |
| 63 | }) |
| 64 | |
Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 65 | p.phonyList = SortedKeys(p.phonyMap) |
Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 66 | for _, phony := range p.phonyList { |
| 67 | p.phonyMap[phony] = SortedUniquePaths(p.phonyMap[phony]) |
| 68 | } |
| 69 | |
Jingwen Chen | cda22c9 | 2020-11-23 00:22:30 -0500 | [diff] [blame] | 70 | if !ctx.Config().KatiEnabled() { |
Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 71 | for _, phony := range p.phonyList { |
| 72 | ctx.Build(pctx, BuildParams{ |
| 73 | Rule: blueprint.Phony, |
| 74 | Outputs: []WritablePath{PathForPhony(ctx, phony)}, |
| 75 | Implicits: p.phonyMap[phony], |
| 76 | }) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func (p phonySingleton) MakeVars(ctx MakeVarsContext) { |
| 82 | for _, phony := range p.phonyList { |
| 83 | ctx.Phony(phony, p.phonyMap[phony]...) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func phonySingletonFactory() Singleton { |
| 88 | return &phonySingleton{} |
| 89 | } |