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 | 4044950 | 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 |
Sasha Smundak | b6d2305 | 2019-04-01 18:37:36 -0700 | [diff] [blame] | 41 | requiredModuleNames []string |
| 42 | hostRequiredModuleNames []string |
| 43 | targetRequiredModuleNames []string |
Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Steven Moreland | 00e1e61 | 2018-07-11 15:24:31 -0700 | [diff] [blame] | 46 | func PhonyFactory() android.Module { |
Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 47 | module := &phony{} |
| 48 | |
Dan Willemsen | 60294ef | 2019-04-02 17:04:18 -0700 | [diff] [blame] | 49 | android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 50 | return module |
Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 53 | func (p *phony) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Cole Faust | ab6046f | 2024-06-17 12:32:40 -0700 | [diff] [blame] | 54 | p.requiredModuleNames = ctx.RequiredModuleNames(ctx) |
Sasha Smundak | b6d2305 | 2019-04-01 18:37:36 -0700 | [diff] [blame] | 55 | p.hostRequiredModuleNames = ctx.HostRequiredModuleNames() |
| 56 | p.targetRequiredModuleNames = ctx.TargetRequiredModuleNames() |
Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Colin Cross | a18e9cf | 2017-08-10 17:00:19 -0700 | [diff] [blame] | 59 | func (p *phony) AndroidMk() android.AndroidMkData { |
| 60 | return android.AndroidMkData{ |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 61 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
Sasha Smundak | 5c4729d | 2022-12-01 10:49:23 -0800 | [diff] [blame] | 62 | fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)", " # phony.phony") |
Colin Cross | a18e9cf | 2017-08-10 17:00:19 -0700 | [diff] [blame] | 63 | fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir) |
| 64 | fmt.Fprintln(w, "LOCAL_MODULE :=", name) |
Dan Willemsen | 60294ef | 2019-04-02 17:04:18 -0700 | [diff] [blame] | 65 | if p.Host() { |
| 66 | fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true") |
| 67 | } |
Sasha Smundak | b6d2305 | 2019-04-01 18:37:36 -0700 | [diff] [blame] | 68 | 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 Jones | b509938 | 2024-01-10 23:42:36 +0000 | [diff] [blame] | 80 | // AconfigUpdateAndroidMkData may have added elements to Extra. Process them here. |
| 81 | for _, extra := range data.Extra { |
| 82 | extra(w, nil) |
| 83 | } |
Colin Cross | a18e9cf | 2017-08-10 17:00:19 -0700 | [diff] [blame] | 84 | fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)") |
| 85 | }, |
Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 86 | } |
Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 87 | } |
Nelson Li | f3c7068 | 2023-12-20 02:37:52 +0000 | [diff] [blame] | 88 | |
| 89 | type PhonyRule struct { |
| 90 | android.ModuleBase |
Nelson Li | 1f96994 | 2024-04-01 11:30:56 +0800 | [diff] [blame] | 91 | android.DefaultableModuleBase |
Nelson Li | f3c7068 | 2023-12-20 02:37:52 +0000 | [diff] [blame] | 92 | |
Nelson Li | 4044950 | 2024-07-23 15:26:01 +0800 | [diff] [blame] | 93 | phonyDepsModuleNames []string |
| 94 | properties PhonyProperties |
Nelson Li | f3c7068 | 2023-12-20 02:37:52 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | type 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 Li | 4044950 | 2024-07-23 15:26:01 +0800 | [diff] [blame] | 101 | Phony_deps proptools.Configurable[[]string] |
Nelson Li | f3c7068 | 2023-12-20 02:37:52 +0000 | [diff] [blame] | 102 | } |
| 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. |
| 106 | func PhonyRuleFactory() android.Module { |
| 107 | module := &PhonyRule{} |
| 108 | android.InitAndroidModule(module) |
| 109 | module.AddProperties(&module.properties) |
Nelson Li | 1f96994 | 2024-04-01 11:30:56 +0800 | [diff] [blame] | 110 | android.InitDefaultableModule(module) |
Nelson Li | f3c7068 | 2023-12-20 02:37:52 +0000 | [diff] [blame] | 111 | return module |
| 112 | } |
| 113 | |
| 114 | func (p *PhonyRule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Cole Faust | f53b6b1 | 2024-07-29 12:24:25 -0700 | [diff] [blame] | 115 | p.phonyDepsModuleNames = p.properties.Phony_deps.GetOrDefault(ctx, nil) |
Nelson Li | f3c7068 | 2023-12-20 02:37:52 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | func (p *PhonyRule) AndroidMk() android.AndroidMkData { |
| 119 | return android.AndroidMkData{ |
| 120 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
Nelson Li | 4044950 | 2024-07-23 15:26:01 +0800 | [diff] [blame] | 121 | if len(p.phonyDepsModuleNames) > 0 { |
| 122 | depModulesStr := strings.Join(p.phonyDepsModuleNames, " ") |
Nelson Li | f3c7068 | 2023-12-20 02:37:52 +0000 | [diff] [blame] | 123 | fmt.Fprintln(w, ".PHONY:", name) |
| 124 | fmt.Fprintln(w, name, ":", depModulesStr) |
| 125 | } |
| 126 | }, |
| 127 | } |
| 128 | } |
Nelson Li | 1f96994 | 2024-04-01 11:30:56 +0800 | [diff] [blame] | 129 | |
| 130 | // PhonyRuleDefaults |
| 131 | type 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 | // } |
| 164 | func PhonyRuleDefaultsFactory() android.Module { |
| 165 | module := &PhonyRuleDefaults{} |
| 166 | module.AddProperties(&PhonyProperties{}) |
| 167 | android.InitDefaultsModule(module) |
| 168 | |
| 169 | return module |
| 170 | } |