blob: f8db88d439499f7137e90e0ba8f9ae4c5126f170 [file] [log] [blame]
Colin Crossc3d87d32020-06-04 13:25:17 -07001// 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
15package android
16
17import (
18 "sync"
19
20 "github.com/google/blueprint"
21)
22
23var phonyMapOnceKey = NewOnceKey("phony")
24
25type phonyMap map[string]Paths
26
27var phonyMapLock sync.Mutex
28
Yu Liu54513622024-08-19 20:00:32 +000029type ModulePhonyInfo struct {
30 Phonies map[string]Paths
31}
32
33var ModulePhonyProvider = blueprint.NewProvider[ModulePhonyInfo]()
34
35func getSingletonPhonyMap(config Config) phonyMap {
Colin Crossc3d87d32020-06-04 13:25:17 -070036 return config.Once(phonyMapOnceKey, func() interface{} {
37 return make(phonyMap)
38 }).(phonyMap)
39}
40
Yu Liu54513622024-08-19 20:00:32 +000041func addSingletonPhony(config Config, name string, deps ...Path) {
42 phonyMap := getSingletonPhonyMap(config)
Colin Crossc3d87d32020-06-04 13:25:17 -070043 phonyMapLock.Lock()
44 defer phonyMapLock.Unlock()
45 phonyMap[name] = append(phonyMap[name], deps...)
46}
47
48type phonySingleton struct {
49 phonyMap phonyMap
50 phonyList []string
51}
52
53var _ SingletonMakeVarsProvider = (*phonySingleton)(nil)
54
55func (p *phonySingleton) GenerateBuildActions(ctx SingletonContext) {
Yu Liu54513622024-08-19 20:00:32 +000056 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 Faust18994c72023-02-28 16:02:16 -080065 p.phonyList = SortedKeys(p.phonyMap)
Colin Crossc3d87d32020-06-04 13:25:17 -070066 for _, phony := range p.phonyList {
67 p.phonyMap[phony] = SortedUniquePaths(p.phonyMap[phony])
68 }
69
Jingwen Chencda22c92020-11-23 00:22:30 -050070 if !ctx.Config().KatiEnabled() {
Colin Crossc3d87d32020-06-04 13:25:17 -070071 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
81func (p phonySingleton) MakeVars(ctx MakeVarsContext) {
82 for _, phony := range p.phonyList {
83 ctx.Phony(phony, p.phonyMap[phony]...)
84 }
85}
86
87func phonySingletonFactory() Singleton {
88 return &phonySingleton{}
89}