blob: 4f61c458302414579062502cd87448c87f8eec4a [file] [log] [blame]
Nan Zhang6d34b302017-02-04 17:47:46 -08001// Copyright 2016 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 phony
16
17import (
18 "fmt"
19 "io"
20 "strings"
21
Nan Zhang6d34b302017-02-04 17:47:46 -080022 "android/soong/android"
Nelson Li154e05b2024-07-23 15:26:01 +080023
24 "github.com/google/blueprint/proptools"
Nan Zhang6d34b302017-02-04 17:47:46 -080025)
26
27func init() {
Jiyong Park92b8e8f2024-03-26 10:47:18 +090028 registerPhonyModuleTypes(android.InitRegistrationContext)
Nan Zhang6d34b302017-02-04 17:47:46 -080029}
30
Jiyong Park92b8e8f2024-03-26 10:47:18 +090031func registerPhonyModuleTypes(ctx android.RegistrationContext) {
32 ctx.RegisterModuleType("phony", PhonyFactory)
33 ctx.RegisterModuleType("phony_rule", PhonyRuleFactory)
Nelson Li1f969942024-04-01 11:30:56 +080034 ctx.RegisterModuleType("phony_rule_defaults", PhonyRuleDefaultsFactory)
Jiyong Park92b8e8f2024-03-26 10:47:18 +090035}
36
37var PrepareForTestWithPhony = android.FixtureRegisterWithContext(registerPhonyModuleTypes)
38
Nan Zhang6d34b302017-02-04 17:47:46 -080039type phony struct {
40 android.ModuleBase
Inseob Kim323344b2025-02-12 20:58:05 -080041
Sasha Smundakb6d23052019-04-01 18:37:36 -070042 requiredModuleNames []string
43 hostRequiredModuleNames []string
44 targetRequiredModuleNames []string
Inseob Kim323344b2025-02-12 20:58:05 -080045 outputDeps android.Paths
Nan Zhang6d34b302017-02-04 17:47:46 -080046}
47
Steven Moreland00e1e612018-07-11 15:24:31 -070048func PhonyFactory() android.Module {
Nan Zhang6d34b302017-02-04 17:47:46 -080049 module := &phony{}
50
Dan Willemsen60294ef2019-04-02 17:04:18 -070051 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Colin Cross36242852017-06-23 15:06:31 -070052 return module
Nan Zhang6d34b302017-02-04 17:47:46 -080053}
54
Nan Zhang6d34b302017-02-04 17:47:46 -080055func (p *phony) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust43ddd082024-06-17 12:32:40 -070056 p.requiredModuleNames = ctx.RequiredModuleNames(ctx)
Sasha Smundakb6d23052019-04-01 18:37:36 -070057 p.hostRequiredModuleNames = ctx.HostRequiredModuleNames()
58 p.targetRequiredModuleNames = ctx.TargetRequiredModuleNames()
Inseob Kim323344b2025-02-12 20:58:05 -080059
60 ctx.VisitDirectDepsWithTag(android.RequiredDepTag, func(dep android.Module) {
61 if o, ok := android.OtherModuleProvider(ctx, dep, android.OutputFilesProvider); ok {
62 p.outputDeps = append(p.outputDeps, o.DefaultOutputFiles...)
63 }
64 })
65
66 ctx.Phony(p.Name(), p.outputDeps...)
Nan Zhang6d34b302017-02-04 17:47:46 -080067}
68
Colin Crossa18e9cf2017-08-10 17:00:19 -070069func (p *phony) AndroidMk() android.AndroidMkData {
70 return android.AndroidMkData{
Colin Cross0f86d182017-08-10 17:07:28 -070071 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Sasha Smundak5c4729d2022-12-01 10:49:23 -080072 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)", " # phony.phony")
Colin Crossa18e9cf2017-08-10 17:00:19 -070073 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
74 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Dan Willemsen60294ef2019-04-02 17:04:18 -070075 if p.Host() {
76 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
77 }
Sasha Smundakb6d23052019-04-01 18:37:36 -070078 if len(p.requiredModuleNames) > 0 {
79 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=",
80 strings.Join(p.requiredModuleNames, " "))
81 }
82 if len(p.hostRequiredModuleNames) > 0 {
83 fmt.Fprintln(w, "LOCAL_HOST_REQUIRED_MODULES :=",
84 strings.Join(p.hostRequiredModuleNames, " "))
85 }
86 if len(p.targetRequiredModuleNames) > 0 {
87 fmt.Fprintln(w, "LOCAL_TARGET_REQUIRED_MODULES :=",
88 strings.Join(p.targetRequiredModuleNames, " "))
89 }
Inseob Kim323344b2025-02-12 20:58:05 -080090 if len(p.outputDeps) > 0 {
91 fmt.Fprintln(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=",
92 strings.Join(p.outputDeps.Strings(), " "))
93 }
LaMont Jonesb5099382024-01-10 23:42:36 +000094 // AconfigUpdateAndroidMkData may have added elements to Extra. Process them here.
95 for _, extra := range data.Extra {
96 extra(w, nil)
97 }
Colin Crossa18e9cf2017-08-10 17:00:19 -070098 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
99 },
Nan Zhang6d34b302017-02-04 17:47:46 -0800100 }
Nan Zhang6d34b302017-02-04 17:47:46 -0800101}
Nelson Lif3c70682023-12-20 02:37:52 +0000102
103type PhonyRule struct {
104 android.ModuleBase
Nelson Li1f969942024-04-01 11:30:56 +0800105 android.DefaultableModuleBase
Nelson Lif3c70682023-12-20 02:37:52 +0000106
Nelson Li154e05b2024-07-23 15:26:01 +0800107 phonyDepsModuleNames []string
108 properties PhonyProperties
Nelson Lif3c70682023-12-20 02:37:52 +0000109}
110
111type PhonyProperties struct {
112 // The Phony_deps is the set of all dependencies for this target,
113 // and it can function similarly to .PHONY in a makefile.
114 // Additionally, dependencies within it can even include genrule.
Nelson Li154e05b2024-07-23 15:26:01 +0800115 Phony_deps proptools.Configurable[[]string]
Nelson Lif3c70682023-12-20 02:37:52 +0000116}
117
118// The phony_rule provides functionality similar to the .PHONY in a makefile.
119// It can create a phony target and include relevant dependencies associated with it.
120func PhonyRuleFactory() android.Module {
121 module := &PhonyRule{}
122 android.InitAndroidModule(module)
123 module.AddProperties(&module.properties)
Nelson Li1f969942024-04-01 11:30:56 +0800124 android.InitDefaultableModule(module)
Nelson Lif3c70682023-12-20 02:37:52 +0000125 return module
126}
127
128func (p *PhonyRule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faustbf1d92a2024-07-29 12:24:25 -0700129 p.phonyDepsModuleNames = p.properties.Phony_deps.GetOrDefault(ctx, nil)
Nelson Lif3c70682023-12-20 02:37:52 +0000130}
131
132func (p *PhonyRule) AndroidMk() android.AndroidMkData {
133 return android.AndroidMkData{
134 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Nelson Li154e05b2024-07-23 15:26:01 +0800135 if len(p.phonyDepsModuleNames) > 0 {
136 depModulesStr := strings.Join(p.phonyDepsModuleNames, " ")
Nelson Lif3c70682023-12-20 02:37:52 +0000137 fmt.Fprintln(w, ".PHONY:", name)
138 fmt.Fprintln(w, name, ":", depModulesStr)
139 }
140 },
141 }
142}
Nelson Li1f969942024-04-01 11:30:56 +0800143
144// PhonyRuleDefaults
145type PhonyRuleDefaults struct {
146 android.ModuleBase
147 android.DefaultsModuleBase
148}
149
150// phony_rule_defaults provides a set of properties that can be inherited by other phony_rules.
151//
152// A module can use the properties from a phony_rule_defaults module using `defaults: ["defaults_module_name"]`. Each
153// property in the defaults module that exists in the depending module will be prepended to the depending module's
154// value for that property.
155//
156// Example:
157//
158// phony_rule_defaults {
159// name: "add_module1_defaults",
160// phony_deps: [
161// "module1",
162// ],
163// }
164//
165// phony_rule {
166// name: "example",
167// defaults: ["add_module1_defaults"],
168// }
169//
170// is functionally identical to:
171//
172// phony_rule {
173// name: "example",
174// phony_deps: [
175// "module1",
176// ],
177// }
178func PhonyRuleDefaultsFactory() android.Module {
179 module := &PhonyRuleDefaults{}
180 module.AddProperties(&PhonyProperties{})
181 android.InitDefaultsModule(module)
182
183 return module
184}