blob: 68fbf48d06bd0be1e208562d411d8a462419ecbd [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)
32}
33
34var PrepareForTestWithPhony = android.FixtureRegisterWithContext(registerPhonyModuleTypes)
35
Nan Zhang6d34b302017-02-04 17:47:46 -080036type phony struct {
37 android.ModuleBase
Sasha Smundakb6d23052019-04-01 18:37:36 -070038 requiredModuleNames []string
39 hostRequiredModuleNames []string
40 targetRequiredModuleNames []string
Nan Zhang6d34b302017-02-04 17:47:46 -080041}
42
Steven Moreland00e1e612018-07-11 15:24:31 -070043func PhonyFactory() android.Module {
Nan Zhang6d34b302017-02-04 17:47:46 -080044 module := &phony{}
45
Dan Willemsen60294ef2019-04-02 17:04:18 -070046 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Colin Cross36242852017-06-23 15:06:31 -070047 return module
Nan Zhang6d34b302017-02-04 17:47:46 -080048}
49
Nan Zhang6d34b302017-02-04 17:47:46 -080050func (p *phony) GenerateAndroidBuildActions(ctx android.ModuleContext) {
51 p.requiredModuleNames = ctx.RequiredModuleNames()
Sasha Smundakb6d23052019-04-01 18:37:36 -070052 p.hostRequiredModuleNames = ctx.HostRequiredModuleNames()
53 p.targetRequiredModuleNames = ctx.TargetRequiredModuleNames()
Nan Zhang6d34b302017-02-04 17:47:46 -080054}
55
Colin Crossa18e9cf2017-08-10 17:00:19 -070056func (p *phony) AndroidMk() android.AndroidMkData {
57 return android.AndroidMkData{
Colin Cross0f86d182017-08-10 17:07:28 -070058 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Sasha Smundak5c4729d2022-12-01 10:49:23 -080059 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)", " # phony.phony")
Colin Crossa18e9cf2017-08-10 17:00:19 -070060 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
61 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Dan Willemsen60294ef2019-04-02 17:04:18 -070062 if p.Host() {
63 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
64 }
Sasha Smundakb6d23052019-04-01 18:37:36 -070065 if len(p.requiredModuleNames) > 0 {
66 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=",
67 strings.Join(p.requiredModuleNames, " "))
68 }
69 if len(p.hostRequiredModuleNames) > 0 {
70 fmt.Fprintln(w, "LOCAL_HOST_REQUIRED_MODULES :=",
71 strings.Join(p.hostRequiredModuleNames, " "))
72 }
73 if len(p.targetRequiredModuleNames) > 0 {
74 fmt.Fprintln(w, "LOCAL_TARGET_REQUIRED_MODULES :=",
75 strings.Join(p.targetRequiredModuleNames, " "))
76 }
LaMont Jonesb5099382024-01-10 23:42:36 +000077 // AconfigUpdateAndroidMkData may have added elements to Extra. Process them here.
78 for _, extra := range data.Extra {
79 extra(w, nil)
80 }
Colin Crossa18e9cf2017-08-10 17:00:19 -070081 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
82 },
Nan Zhang6d34b302017-02-04 17:47:46 -080083 }
Nan Zhang6d34b302017-02-04 17:47:46 -080084}
Nelson Lif3c70682023-12-20 02:37:52 +000085
86type PhonyRule struct {
87 android.ModuleBase
88
89 properties PhonyProperties
90}
91
92type PhonyProperties struct {
93 // The Phony_deps is the set of all dependencies for this target,
94 // and it can function similarly to .PHONY in a makefile.
95 // Additionally, dependencies within it can even include genrule.
96 Phony_deps []string
97}
98
99// The phony_rule provides functionality similar to the .PHONY in a makefile.
100// It can create a phony target and include relevant dependencies associated with it.
101func PhonyRuleFactory() android.Module {
102 module := &PhonyRule{}
103 android.InitAndroidModule(module)
104 module.AddProperties(&module.properties)
105 return module
106}
107
108func (p *PhonyRule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
109}
110
111func (p *PhonyRule) AndroidMk() android.AndroidMkData {
112 return android.AndroidMkData{
113 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
114 if len(p.properties.Phony_deps) > 0 {
115 depModulesStr := strings.Join(p.properties.Phony_deps, " ")
116 fmt.Fprintln(w, ".PHONY:", name)
117 fmt.Fprintln(w, name, ":", depModulesStr)
118 }
119 },
120 }
121}