blob: 807b95b320fc3e70dc38860e33d98b4935174e77 [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 Li40449502024-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
Sasha Smundakb6d23052019-04-01 18:37:36 -070041 requiredModuleNames []string
42 hostRequiredModuleNames []string
43 targetRequiredModuleNames []string
Nan Zhang6d34b302017-02-04 17:47:46 -080044}
45
Steven Moreland00e1e612018-07-11 15:24:31 -070046func PhonyFactory() android.Module {
Nan Zhang6d34b302017-02-04 17:47:46 -080047 module := &phony{}
48
Dan Willemsen60294ef2019-04-02 17:04:18 -070049 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Colin Cross36242852017-06-23 15:06:31 -070050 return module
Nan Zhang6d34b302017-02-04 17:47:46 -080051}
52
Nan Zhang6d34b302017-02-04 17:47:46 -080053func (p *phony) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faustab6046f2024-06-17 12:32:40 -070054 p.requiredModuleNames = ctx.RequiredModuleNames(ctx)
Sasha Smundakb6d23052019-04-01 18:37:36 -070055 p.hostRequiredModuleNames = ctx.HostRequiredModuleNames()
56 p.targetRequiredModuleNames = ctx.TargetRequiredModuleNames()
Nan Zhang6d34b302017-02-04 17:47:46 -080057}
58
Colin Crossa18e9cf2017-08-10 17:00:19 -070059func (p *phony) AndroidMk() android.AndroidMkData {
60 return android.AndroidMkData{
Colin Cross0f86d182017-08-10 17:07:28 -070061 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Sasha Smundak5c4729d2022-12-01 10:49:23 -080062 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)", " # phony.phony")
Colin Crossa18e9cf2017-08-10 17:00:19 -070063 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
64 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Dan Willemsen60294ef2019-04-02 17:04:18 -070065 if p.Host() {
66 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
67 }
Sasha Smundakb6d23052019-04-01 18:37:36 -070068 if len(p.requiredModuleNames) > 0 {
69 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=",
70 strings.Join(p.requiredModuleNames, " "))
71 }
72 if len(p.hostRequiredModuleNames) > 0 {
73 fmt.Fprintln(w, "LOCAL_HOST_REQUIRED_MODULES :=",
74 strings.Join(p.hostRequiredModuleNames, " "))
75 }
76 if len(p.targetRequiredModuleNames) > 0 {
77 fmt.Fprintln(w, "LOCAL_TARGET_REQUIRED_MODULES :=",
78 strings.Join(p.targetRequiredModuleNames, " "))
79 }
LaMont Jonesb5099382024-01-10 23:42:36 +000080 // AconfigUpdateAndroidMkData may have added elements to Extra. Process them here.
81 for _, extra := range data.Extra {
82 extra(w, nil)
83 }
Colin Crossa18e9cf2017-08-10 17:00:19 -070084 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
85 },
Nan Zhang6d34b302017-02-04 17:47:46 -080086 }
Nan Zhang6d34b302017-02-04 17:47:46 -080087}
Nelson Lif3c70682023-12-20 02:37:52 +000088
89type PhonyRule struct {
90 android.ModuleBase
Nelson Li1f969942024-04-01 11:30:56 +080091 android.DefaultableModuleBase
Nelson Lif3c70682023-12-20 02:37:52 +000092
Nelson Li40449502024-07-23 15:26:01 +080093 phonyDepsModuleNames []string
94 properties PhonyProperties
Nelson Lif3c70682023-12-20 02:37:52 +000095}
96
97type PhonyProperties struct {
98 // The Phony_deps is the set of all dependencies for this target,
99 // and it can function similarly to .PHONY in a makefile.
100 // Additionally, dependencies within it can even include genrule.
Nelson Li40449502024-07-23 15:26:01 +0800101 Phony_deps proptools.Configurable[[]string]
Nelson Lif3c70682023-12-20 02:37:52 +0000102}
103
104// The phony_rule provides functionality similar to the .PHONY in a makefile.
105// It can create a phony target and include relevant dependencies associated with it.
106func PhonyRuleFactory() android.Module {
107 module := &PhonyRule{}
108 android.InitAndroidModule(module)
109 module.AddProperties(&module.properties)
Nelson Li1f969942024-04-01 11:30:56 +0800110 android.InitDefaultableModule(module)
Nelson Lif3c70682023-12-20 02:37:52 +0000111 return module
112}
113
114func (p *PhonyRule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faustf53b6b12024-07-29 12:24:25 -0700115 p.phonyDepsModuleNames = p.properties.Phony_deps.GetOrDefault(ctx, nil)
Nelson Lif3c70682023-12-20 02:37:52 +0000116}
117
118func (p *PhonyRule) AndroidMk() android.AndroidMkData {
119 return android.AndroidMkData{
120 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Nelson Li40449502024-07-23 15:26:01 +0800121 if len(p.phonyDepsModuleNames) > 0 {
122 depModulesStr := strings.Join(p.phonyDepsModuleNames, " ")
Nelson Lif3c70682023-12-20 02:37:52 +0000123 fmt.Fprintln(w, ".PHONY:", name)
124 fmt.Fprintln(w, name, ":", depModulesStr)
125 }
126 },
127 }
128}
Nelson Li1f969942024-04-01 11:30:56 +0800129
130// PhonyRuleDefaults
131type PhonyRuleDefaults struct {
132 android.ModuleBase
133 android.DefaultsModuleBase
134}
135
136// phony_rule_defaults provides a set of properties that can be inherited by other phony_rules.
137//
138// A module can use the properties from a phony_rule_defaults module using `defaults: ["defaults_module_name"]`. Each
139// property in the defaults module that exists in the depending module will be prepended to the depending module's
140// value for that property.
141//
142// Example:
143//
144// phony_rule_defaults {
145// name: "add_module1_defaults",
146// phony_deps: [
147// "module1",
148// ],
149// }
150//
151// phony_rule {
152// name: "example",
153// defaults: ["add_module1_defaults"],
154// }
155//
156// is functionally identical to:
157//
158// phony_rule {
159// name: "example",
160// phony_deps: [
161// "module1",
162// ],
163// }
164func PhonyRuleDefaultsFactory() android.Module {
165 module := &PhonyRuleDefaults{}
166 module.AddProperties(&PhonyProperties{})
167 android.InitDefaultsModule(module)
168
169 return module
170}