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 ( |
Cole Faust | c5bfbdd | 2025-01-08 13:05:40 -0800 | [diff] [blame] | 18 | "strings" |
Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 19 | "sync" |
| 20 | |
| 21 | "github.com/google/blueprint" |
| 22 | ) |
| 23 | |
| 24 | var phonyMapOnceKey = NewOnceKey("phony") |
| 25 | |
| 26 | type phonyMap map[string]Paths |
| 27 | |
| 28 | var phonyMapLock sync.Mutex |
| 29 | |
Yu Liu | 5451362 | 2024-08-19 20:00:32 +0000 | [diff] [blame] | 30 | type ModulePhonyInfo struct { |
| 31 | Phonies map[string]Paths |
| 32 | } |
| 33 | |
| 34 | var ModulePhonyProvider = blueprint.NewProvider[ModulePhonyInfo]() |
| 35 | |
| 36 | func getSingletonPhonyMap(config Config) phonyMap { |
Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 37 | return config.Once(phonyMapOnceKey, func() interface{} { |
| 38 | return make(phonyMap) |
| 39 | }).(phonyMap) |
| 40 | } |
| 41 | |
Yu Liu | 5451362 | 2024-08-19 20:00:32 +0000 | [diff] [blame] | 42 | func addSingletonPhony(config Config, name string, deps ...Path) { |
| 43 | phonyMap := getSingletonPhonyMap(config) |
Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 44 | phonyMapLock.Lock() |
| 45 | defer phonyMapLock.Unlock() |
| 46 | phonyMap[name] = append(phonyMap[name], deps...) |
| 47 | } |
| 48 | |
| 49 | type phonySingleton struct { |
| 50 | phonyMap phonyMap |
| 51 | phonyList []string |
| 52 | } |
| 53 | |
| 54 | var _ SingletonMakeVarsProvider = (*phonySingleton)(nil) |
| 55 | |
| 56 | func (p *phonySingleton) GenerateBuildActions(ctx SingletonContext) { |
Yu Liu | 5451362 | 2024-08-19 20:00:32 +0000 | [diff] [blame] | 57 | p.phonyMap = getSingletonPhonyMap(ctx.Config()) |
| 58 | ctx.VisitAllModules(func(m Module) { |
| 59 | if info, ok := OtherModuleProvider(ctx, m, ModulePhonyProvider); ok { |
| 60 | for k, v := range info.Phonies { |
| 61 | p.phonyMap[k] = append(p.phonyMap[k], v...) |
| 62 | } |
| 63 | } |
| 64 | }) |
| 65 | |
Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 66 | p.phonyList = SortedKeys(p.phonyMap) |
Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 67 | for _, phony := range p.phonyList { |
| 68 | p.phonyMap[phony] = SortedUniquePaths(p.phonyMap[phony]) |
| 69 | } |
| 70 | |
Jingwen Chen | cda22c9 | 2020-11-23 00:22:30 -0500 | [diff] [blame] | 71 | if !ctx.Config().KatiEnabled() { |
Cole Faust | c5bfbdd | 2025-01-08 13:05:40 -0800 | [diff] [blame] | 72 | // In soong-only builds, the phonies can conflict with dist targets that will |
| 73 | // be generated in the packaging step. Instead of emitting a blueprint/ninja phony directly, |
| 74 | // create a makefile that defines the phonies that will be included in the packaging step. |
| 75 | // Make will dedup the phonies there. |
| 76 | var buildPhonyFileContents strings.Builder |
Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 77 | for _, phony := range p.phonyList { |
Cole Faust | c5bfbdd | 2025-01-08 13:05:40 -0800 | [diff] [blame] | 78 | buildPhonyFileContents.WriteString(".PHONY: ") |
| 79 | buildPhonyFileContents.WriteString(phony) |
| 80 | buildPhonyFileContents.WriteString("\n") |
| 81 | buildPhonyFileContents.WriteString(phony) |
| 82 | buildPhonyFileContents.WriteString(":") |
| 83 | for _, dep := range p.phonyMap[phony] { |
| 84 | buildPhonyFileContents.WriteString(" ") |
| 85 | buildPhonyFileContents.WriteString(dep.String()) |
| 86 | } |
| 87 | buildPhonyFileContents.WriteString("\n") |
Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 88 | } |
Cole Faust | c5bfbdd | 2025-01-08 13:05:40 -0800 | [diff] [blame] | 89 | buildPhonyFile := PathForOutput(ctx, "soong_phony_targets.mk") |
| 90 | writeValueIfChanged(ctx, absolutePath(buildPhonyFile.String()), buildPhonyFileContents.String()) |
Colin Cross | c3d87d3 | 2020-06-04 13:25:17 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | func (p phonySingleton) MakeVars(ctx MakeVarsContext) { |
| 95 | for _, phony := range p.phonyList { |
| 96 | ctx.Phony(phony, p.phonyMap[phony]...) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func phonySingletonFactory() Singleton { |
| 101 | return &phonySingleton{} |
| 102 | } |