xref: /aosp_15_r20/build/soong/android/phony.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
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
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
29type ModulePhonyInfo struct {
30	Phonies map[string]Paths
31}
32
33var ModulePhonyProvider = blueprint.NewProvider[ModulePhonyInfo]()
34
35func getSingletonPhonyMap(config Config) phonyMap {
36	return config.Once(phonyMapOnceKey, func() interface{} {
37		return make(phonyMap)
38	}).(phonyMap)
39}
40
41func addSingletonPhony(config Config, name string, deps ...Path) {
42	phonyMap := getSingletonPhonyMap(config)
43	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) {
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
65	p.phonyList = SortedKeys(p.phonyMap)
66	for _, phony := range p.phonyList {
67		p.phonyMap[phony] = SortedUniquePaths(p.phonyMap[phony])
68	}
69
70	if !ctx.Config().KatiEnabled() {
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
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}
90