blob: b421176be9539d0daf7dfadd927f8bf04d06756d [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"
23)
24
25func init() {
Jiyong Park92b8e8f2024-03-26 10:47:18 +090026 registerPhonyModuleTypes(android.InitRegistrationContext)
Nan Zhang6d34b302017-02-04 17:47:46 -080027}
28
Jiyong Park92b8e8f2024-03-26 10:47:18 +090029func registerPhonyModuleTypes(ctx android.RegistrationContext) {
30 ctx.RegisterModuleType("phony", PhonyFactory)
31 ctx.RegisterModuleType("phony_rule", PhonyRuleFactory)
Nelson Li1f969942024-04-01 11:30:56 +080032 ctx.RegisterModuleType("phony_rule_defaults", PhonyRuleDefaultsFactory)
Jiyong Park92b8e8f2024-03-26 10:47:18 +090033}
34
35var PrepareForTestWithPhony = android.FixtureRegisterWithContext(registerPhonyModuleTypes)
36
Nan Zhang6d34b302017-02-04 17:47:46 -080037type phony struct {
38 android.ModuleBase
Sasha Smundakb6d23052019-04-01 18:37:36 -070039 requiredModuleNames []string
40 hostRequiredModuleNames []string
41 targetRequiredModuleNames []string
Nan Zhang6d34b302017-02-04 17:47:46 -080042}
43
Steven Moreland00e1e612018-07-11 15:24:31 -070044func PhonyFactory() android.Module {
Nan Zhang6d34b302017-02-04 17:47:46 -080045 module := &phony{}
46
Dan Willemsen60294ef2019-04-02 17:04:18 -070047 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Colin Cross36242852017-06-23 15:06:31 -070048 return module
Nan Zhang6d34b302017-02-04 17:47:46 -080049}
50
Nan Zhang6d34b302017-02-04 17:47:46 -080051func (p *phony) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Cole Faust43ddd082024-06-17 12:32:40 -070052 p.requiredModuleNames = ctx.RequiredModuleNames(ctx)
Sasha Smundakb6d23052019-04-01 18:37:36 -070053 p.hostRequiredModuleNames = ctx.HostRequiredModuleNames()
54 p.targetRequiredModuleNames = ctx.TargetRequiredModuleNames()
Nan Zhang6d34b302017-02-04 17:47:46 -080055}
56
Colin Crossa18e9cf2017-08-10 17:00:19 -070057func (p *phony) AndroidMk() android.AndroidMkData {
58 return android.AndroidMkData{
Colin Cross0f86d182017-08-10 17:07:28 -070059 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Sasha Smundak5c4729d2022-12-01 10:49:23 -080060 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)", " # phony.phony")
Colin Crossa18e9cf2017-08-10 17:00:19 -070061 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
62 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Dan Willemsen60294ef2019-04-02 17:04:18 -070063 if p.Host() {
64 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
65 }
Sasha Smundakb6d23052019-04-01 18:37:36 -070066 if len(p.requiredModuleNames) > 0 {
67 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=",
68 strings.Join(p.requiredModuleNames, " "))
69 }
70 if len(p.hostRequiredModuleNames) > 0 {
71 fmt.Fprintln(w, "LOCAL_HOST_REQUIRED_MODULES :=",
72 strings.Join(p.hostRequiredModuleNames, " "))
73 }
74 if len(p.targetRequiredModuleNames) > 0 {
75 fmt.Fprintln(w, "LOCAL_TARGET_REQUIRED_MODULES :=",
76 strings.Join(p.targetRequiredModuleNames, " "))
77 }
LaMont Jonesb5099382024-01-10 23:42:36 +000078 // AconfigUpdateAndroidMkData may have added elements to Extra. Process them here.
79 for _, extra := range data.Extra {
80 extra(w, nil)
81 }
Colin Crossa18e9cf2017-08-10 17:00:19 -070082 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
83 },
Nan Zhang6d34b302017-02-04 17:47:46 -080084 }
Nan Zhang6d34b302017-02-04 17:47:46 -080085}
Nelson Lif3c70682023-12-20 02:37:52 +000086
87type PhonyRule struct {
88 android.ModuleBase
Nelson Li1f969942024-04-01 11:30:56 +080089 android.DefaultableModuleBase
Nelson Lif3c70682023-12-20 02:37:52 +000090
91 properties PhonyProperties
92}
93
94type PhonyProperties struct {
95 // The Phony_deps is the set of all dependencies for this target,
96 // and it can function similarly to .PHONY in a makefile.
97 // Additionally, dependencies within it can even include genrule.
98 Phony_deps []string
99}
100
101// The phony_rule provides functionality similar to the .PHONY in a makefile.
102// It can create a phony target and include relevant dependencies associated with it.
103func PhonyRuleFactory() android.Module {
104 module := &PhonyRule{}
105 android.InitAndroidModule(module)
106 module.AddProperties(&module.properties)
Nelson Li1f969942024-04-01 11:30:56 +0800107 android.InitDefaultableModule(module)
Nelson Lif3c70682023-12-20 02:37:52 +0000108 return module
109}
110
111func (p *PhonyRule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
112}
113
114func (p *PhonyRule) AndroidMk() android.AndroidMkData {
115 return android.AndroidMkData{
116 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
117 if len(p.properties.Phony_deps) > 0 {
118 depModulesStr := strings.Join(p.properties.Phony_deps, " ")
119 fmt.Fprintln(w, ".PHONY:", name)
120 fmt.Fprintln(w, name, ":", depModulesStr)
121 }
122 },
123 }
124}
Nelson Li1f969942024-04-01 11:30:56 +0800125
126// PhonyRuleDefaults
127type PhonyRuleDefaults struct {
128 android.ModuleBase
129 android.DefaultsModuleBase
130}
131
132// phony_rule_defaults provides a set of properties that can be inherited by other phony_rules.
133//
134// A module can use the properties from a phony_rule_defaults module using `defaults: ["defaults_module_name"]`. Each
135// property in the defaults module that exists in the depending module will be prepended to the depending module's
136// value for that property.
137//
138// Example:
139//
140// phony_rule_defaults {
141// name: "add_module1_defaults",
142// phony_deps: [
143// "module1",
144// ],
145// }
146//
147// phony_rule {
148// name: "example",
149// defaults: ["add_module1_defaults"],
150// }
151//
152// is functionally identical to:
153//
154// phony_rule {
155// name: "example",
156// phony_deps: [
157// "module1",
158// ],
159// }
160func PhonyRuleDefaultsFactory() android.Module {
161 module := &PhonyRuleDefaults{}
162 module.AddProperties(&PhonyProperties{})
163 android.InitDefaultsModule(module)
164
165 return module
166}