blob: e75f4c809d72abf2379be95281f28a331852c953 [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
Jihoon Kang030756c2025-03-06 18:46:23 +0000107 properties PhonyProperties
Nelson Lif3c70682023-12-20 02:37:52 +0000108}
109
110type PhonyProperties struct {
111 // The Phony_deps is the set of all dependencies for this target,
112 // and it can function similarly to .PHONY in a makefile.
113 // Additionally, dependencies within it can even include genrule.
Nelson Li154e05b2024-07-23 15:26:01 +0800114 Phony_deps proptools.Configurable[[]string]
Nelson Lif3c70682023-12-20 02:37:52 +0000115}
116
117// The phony_rule provides functionality similar to the .PHONY in a makefile.
118// It can create a phony target and include relevant dependencies associated with it.
119func PhonyRuleFactory() android.Module {
120 module := &PhonyRule{}
121 android.InitAndroidModule(module)
122 module.AddProperties(&module.properties)
Nelson Li1f969942024-04-01 11:30:56 +0800123 android.InitDefaultableModule(module)
Nelson Lif3c70682023-12-20 02:37:52 +0000124 return module
125}
126
127func (p *PhonyRule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jihoon Kang030756c2025-03-06 18:46:23 +0000128 for _, dep := range p.properties.Phony_deps.GetOrDefault(ctx, nil) {
129 ctx.Phony(ctx.ModuleName(), android.PathForPhony(ctx, dep))
Nelson Lif3c70682023-12-20 02:37:52 +0000130 }
131}
Nelson Li1f969942024-04-01 11:30:56 +0800132
133// PhonyRuleDefaults
134type PhonyRuleDefaults struct {
135 android.ModuleBase
136 android.DefaultsModuleBase
137}
138
139// phony_rule_defaults provides a set of properties that can be inherited by other phony_rules.
140//
141// A module can use the properties from a phony_rule_defaults module using `defaults: ["defaults_module_name"]`. Each
142// property in the defaults module that exists in the depending module will be prepended to the depending module's
143// value for that property.
144//
145// Example:
146//
147// phony_rule_defaults {
148// name: "add_module1_defaults",
149// phony_deps: [
150// "module1",
151// ],
152// }
153//
154// phony_rule {
155// name: "example",
156// defaults: ["add_module1_defaults"],
157// }
158//
159// is functionally identical to:
160//
161// phony_rule {
162// name: "example",
163// phony_deps: [
164// "module1",
165// ],
166// }
167func PhonyRuleDefaultsFactory() android.Module {
168 module := &PhonyRuleDefaults{}
169 module.AddProperties(&PhonyProperties{})
170 android.InitDefaultsModule(module)
171
172 return module
173}