Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package phony |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "io" |
| 20 | "strings" |
| 21 | |
Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 22 | "android/soong/android" |
Nelson Li | 154e05b | 2024-07-23 15:26:01 +0800 | [diff] [blame] | 23 | |
| 24 | "github.com/google/blueprint/proptools" |
Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | func init() { |
Jiyong Park | 92b8e8f | 2024-03-26 10:47:18 +0900 | [diff] [blame] | 28 | registerPhonyModuleTypes(android.InitRegistrationContext) |
Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 29 | } |
| 30 | |
Jiyong Park | 92b8e8f | 2024-03-26 10:47:18 +0900 | [diff] [blame] | 31 | func registerPhonyModuleTypes(ctx android.RegistrationContext) { |
| 32 | ctx.RegisterModuleType("phony", PhonyFactory) |
| 33 | ctx.RegisterModuleType("phony_rule", PhonyRuleFactory) |
Nelson Li | 1f96994 | 2024-04-01 11:30:56 +0800 | [diff] [blame] | 34 | ctx.RegisterModuleType("phony_rule_defaults", PhonyRuleDefaultsFactory) |
Jiyong Park | 92b8e8f | 2024-03-26 10:47:18 +0900 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | var PrepareForTestWithPhony = android.FixtureRegisterWithContext(registerPhonyModuleTypes) |
| 38 | |
Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 39 | type phony struct { |
| 40 | android.ModuleBase |
Inseob Kim | 323344b | 2025-02-12 20:58:05 -0800 | [diff] [blame] | 41 | |
Sasha Smundak | b6d2305 | 2019-04-01 18:37:36 -0700 | [diff] [blame] | 42 | requiredModuleNames []string |
| 43 | hostRequiredModuleNames []string |
| 44 | targetRequiredModuleNames []string |
Inseob Kim | 323344b | 2025-02-12 20:58:05 -0800 | [diff] [blame] | 45 | outputDeps android.Paths |
Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Steven Moreland | 00e1e61 | 2018-07-11 15:24:31 -0700 | [diff] [blame] | 48 | func PhonyFactory() android.Module { |
Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 49 | module := &phony{} |
| 50 | |
Dan Willemsen | 60294ef | 2019-04-02 17:04:18 -0700 | [diff] [blame] | 51 | android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 52 | return module |
Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 55 | func (p *phony) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Cole Faust | 43ddd08 | 2024-06-17 12:32:40 -0700 | [diff] [blame] | 56 | p.requiredModuleNames = ctx.RequiredModuleNames(ctx) |
Sasha Smundak | b6d2305 | 2019-04-01 18:37:36 -0700 | [diff] [blame] | 57 | p.hostRequiredModuleNames = ctx.HostRequiredModuleNames() |
| 58 | p.targetRequiredModuleNames = ctx.TargetRequiredModuleNames() |
Inseob Kim | 323344b | 2025-02-12 20:58:05 -0800 | [diff] [blame] | 59 | |
| 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 Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Colin Cross | a18e9cf | 2017-08-10 17:00:19 -0700 | [diff] [blame] | 69 | func (p *phony) AndroidMk() android.AndroidMkData { |
| 70 | return android.AndroidMkData{ |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 71 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
Sasha Smundak | 5c4729d | 2022-12-01 10:49:23 -0800 | [diff] [blame] | 72 | fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)", " # phony.phony") |
Colin Cross | a18e9cf | 2017-08-10 17:00:19 -0700 | [diff] [blame] | 73 | fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) |
| 74 | fmt.Fprintln(w, "LOCAL_MODULE :=", name) |
Dan Willemsen | 60294ef | 2019-04-02 17:04:18 -0700 | [diff] [blame] | 75 | if p.Host() { |
| 76 | fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true") |
| 77 | } |
Sasha Smundak | b6d2305 | 2019-04-01 18:37:36 -0700 | [diff] [blame] | 78 | 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 Kim | 323344b | 2025-02-12 20:58:05 -0800 | [diff] [blame] | 90 | if len(p.outputDeps) > 0 { |
| 91 | fmt.Fprintln(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=", |
| 92 | strings.Join(p.outputDeps.Strings(), " ")) |
| 93 | } |
LaMont Jones | b509938 | 2024-01-10 23:42:36 +0000 | [diff] [blame] | 94 | // AconfigUpdateAndroidMkData may have added elements to Extra. Process them here. |
| 95 | for _, extra := range data.Extra { |
| 96 | extra(w, nil) |
| 97 | } |
Colin Cross | a18e9cf | 2017-08-10 17:00:19 -0700 | [diff] [blame] | 98 | fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)") |
| 99 | }, |
Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 100 | } |
Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 101 | } |
Nelson Li | f3c7068 | 2023-12-20 02:37:52 +0000 | [diff] [blame] | 102 | |
| 103 | type PhonyRule struct { |
| 104 | android.ModuleBase |
Nelson Li | 1f96994 | 2024-04-01 11:30:56 +0800 | [diff] [blame] | 105 | android.DefaultableModuleBase |
Nelson Li | f3c7068 | 2023-12-20 02:37:52 +0000 | [diff] [blame] | 106 | |
Jihoon Kang | 030756c | 2025-03-06 18:46:23 +0000 | [diff] [blame] | 107 | properties PhonyProperties |
Nelson Li | f3c7068 | 2023-12-20 02:37:52 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | type 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 Li | 154e05b | 2024-07-23 15:26:01 +0800 | [diff] [blame] | 114 | Phony_deps proptools.Configurable[[]string] |
Nelson Li | f3c7068 | 2023-12-20 02:37:52 +0000 | [diff] [blame] | 115 | } |
| 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. |
| 119 | func PhonyRuleFactory() android.Module { |
| 120 | module := &PhonyRule{} |
| 121 | android.InitAndroidModule(module) |
| 122 | module.AddProperties(&module.properties) |
Nelson Li | 1f96994 | 2024-04-01 11:30:56 +0800 | [diff] [blame] | 123 | android.InitDefaultableModule(module) |
Nelson Li | f3c7068 | 2023-12-20 02:37:52 +0000 | [diff] [blame] | 124 | return module |
| 125 | } |
| 126 | |
| 127 | func (p *PhonyRule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jihoon Kang | 030756c | 2025-03-06 18:46:23 +0000 | [diff] [blame] | 128 | for _, dep := range p.properties.Phony_deps.GetOrDefault(ctx, nil) { |
| 129 | ctx.Phony(ctx.ModuleName(), android.PathForPhony(ctx, dep)) |
Nelson Li | f3c7068 | 2023-12-20 02:37:52 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
Nelson Li | 1f96994 | 2024-04-01 11:30:56 +0800 | [diff] [blame] | 132 | |
| 133 | // PhonyRuleDefaults |
| 134 | type 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 | // } |
| 167 | func PhonyRuleDefaultsFactory() android.Module { |
| 168 | module := &PhonyRuleDefaults{} |
| 169 | module.AddProperties(&PhonyProperties{}) |
| 170 | android.InitDefaultsModule(module) |
| 171 | |
| 172 | return module |
| 173 | } |