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
29func getPhonyMap(config Config) phonyMap {
30	return config.Once(phonyMapOnceKey, func() interface{} {
31		return make(phonyMap)
32	}).(phonyMap)
33}
34
35func addPhony(config Config, name string, deps ...Path) {
36	phonyMap := getPhonyMap(config)
37	phonyMapLock.Lock()
38	defer phonyMapLock.Unlock()
39	phonyMap[name] = append(phonyMap[name], deps...)
40}
41
42type phonySingleton struct {
43	phonyMap  phonyMap
44	phonyList []string
45}
46
47var _ SingletonMakeVarsProvider = (*phonySingleton)(nil)
48
49func (p *phonySingleton) GenerateBuildActions(ctx SingletonContext) {
50	p.phonyMap = getPhonyMap(ctx.Config())
51	p.phonyList = SortedStringKeys(p.phonyMap)
52	for _, phony := range p.phonyList {
53		p.phonyMap[phony] = SortedUniquePaths(p.phonyMap[phony])
54	}
55
56	if !ctx.Config().EmbeddedInMake() {
57		for _, phony := range p.phonyList {
58			ctx.Build(pctx, BuildParams{
59				Rule:      blueprint.Phony,
60				Outputs:   []WritablePath{PathForPhony(ctx, phony)},
61				Implicits: p.phonyMap[phony],
62			})
63		}
64	}
65}
66
67func (p phonySingleton) MakeVars(ctx MakeVarsContext) {
68	for _, phony := range p.phonyList {
69		ctx.Phony(phony, p.phonyMap[phony]...)
70	}
71}
72
73func phonySingletonFactory() Singleton {
74	return &phonySingleton{}
75}
76