| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 1 | // Copyright 2019 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 android | 
 | 16 |  | 
 | 17 | // This file provides module types that implement wrapper module types that add conditionals on | 
 | 18 | // Soong config variables. | 
 | 19 |  | 
 | 20 | import ( | 
 | 21 | 	"fmt" | 
 | 22 | 	"path/filepath" | 
| Paul Duffin | e8b4768 | 2023-01-09 15:42:57 +0000 | [diff] [blame] | 23 | 	"reflect" | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 24 | 	"strings" | 
| Paul Duffin | e8b4768 | 2023-01-09 15:42:57 +0000 | [diff] [blame] | 25 | 	"sync" | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 26 | 	"text/scanner" | 
 | 27 |  | 
 | 28 | 	"github.com/google/blueprint" | 
 | 29 | 	"github.com/google/blueprint/parser" | 
 | 30 | 	"github.com/google/blueprint/proptools" | 
 | 31 |  | 
 | 32 | 	"android/soong/android/soongconfig" | 
 | 33 | ) | 
 | 34 |  | 
 | 35 | func init() { | 
| Paul Duffin | 3229998 | 2023-01-09 14:02:06 +0000 | [diff] [blame] | 36 | 	RegisterSoongConfigModuleBuildComponents(InitRegistrationContext) | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 37 | } | 
 | 38 |  | 
| Paul Duffin | 3229998 | 2023-01-09 14:02:06 +0000 | [diff] [blame] | 39 | func RegisterSoongConfigModuleBuildComponents(ctx RegistrationContext) { | 
 | 40 | 	ctx.RegisterModuleType("soong_config_module_type_import", SoongConfigModuleTypeImportFactory) | 
 | 41 | 	ctx.RegisterModuleType("soong_config_module_type", SoongConfigModuleTypeFactory) | 
 | 42 | 	ctx.RegisterModuleType("soong_config_string_variable", SoongConfigStringVariableDummyFactory) | 
 | 43 | 	ctx.RegisterModuleType("soong_config_bool_variable", SoongConfigBoolVariableDummyFactory) | 
 | 44 | } | 
 | 45 |  | 
 | 46 | var PrepareForTestWithSoongConfigModuleBuildComponents = FixtureRegisterWithContext(RegisterSoongConfigModuleBuildComponents) | 
 | 47 |  | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 48 | type soongConfigModuleTypeImport struct { | 
 | 49 | 	ModuleBase | 
 | 50 | 	properties soongConfigModuleTypeImportProperties | 
 | 51 | } | 
 | 52 |  | 
 | 53 | type soongConfigModuleTypeImportProperties struct { | 
 | 54 | 	From         string | 
 | 55 | 	Module_types []string | 
 | 56 | } | 
 | 57 |  | 
 | 58 | // soong_config_module_type_import imports module types with conditionals on Soong config | 
 | 59 | // variables from another Android.bp file.  The imported module type will exist for all | 
 | 60 | // modules after the import in the Android.bp file. | 
 | 61 | // | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 62 | // Each soong_config_variable supports an additional value `conditions_default`. The properties | 
 | 63 | // specified in `conditions_default` will only be used under the following conditions: | 
 | 64 | //   bool variable: the variable is unspecified or not set to a true value | 
 | 65 | //   value variable: the variable is unspecified | 
 | 66 | //   string variable: the variable is unspecified or the variable is set to a string unused in the | 
 | 67 | //                    given module. For example, string variable `test` takes values: "a" and "b", | 
 | 68 | //                    if the module contains a property `a` and `conditions_default`, when test=b, | 
 | 69 | //                    the properties under `conditions_default` will be used. To specify that no | 
 | 70 | //                    properties should be amended for `b`, you can set `b: {},`. | 
 | 71 | // | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 72 | // For example, an Android.bp file could have: | 
 | 73 | // | 
 | 74 | //     soong_config_module_type_import { | 
| Bill Peckham | c93258b | 2020-02-04 13:17:24 -0800 | [diff] [blame] | 75 | //         from: "device/acme/Android.bp", | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 76 | //         module_types: ["acme_cc_defaults"], | 
 | 77 | //     } | 
 | 78 | // | 
 | 79 | //     acme_cc_defaults { | 
 | 80 | //         name: "acme_defaults", | 
 | 81 | //         cflags: ["-DGENERIC"], | 
 | 82 | //         soong_config_variables: { | 
 | 83 | //             board: { | 
 | 84 | //                 soc_a: { | 
 | 85 | //                     cflags: ["-DSOC_A"], | 
 | 86 | //                 }, | 
 | 87 | //                 soc_b: { | 
 | 88 | //                     cflags: ["-DSOC_B"], | 
 | 89 | //                 }, | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 90 | //                 conditions_default: { | 
 | 91 | //                     cflags: ["-DSOC_DEFAULT"], | 
 | 92 | //                 }, | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 93 | //             }, | 
 | 94 | //             feature: { | 
 | 95 | //                 cflags: ["-DFEATURE"], | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 96 | //                 conditions_default: { | 
 | 97 | //                     cflags: ["-DFEATURE_DEFAULT"], | 
 | 98 | //                 }, | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 99 | //             }, | 
| Dan Willemsen | b0935db | 2020-03-23 19:42:18 -0700 | [diff] [blame] | 100 | //             width: { | 
 | 101 | //                 cflags: ["-DWIDTH=%s"], | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 102 | //                 conditions_default: { | 
 | 103 | //                     cflags: ["-DWIDTH=DEFAULT"], | 
 | 104 | //                 }, | 
| Dan Willemsen | b0935db | 2020-03-23 19:42:18 -0700 | [diff] [blame] | 105 | //             }, | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 106 | //         }, | 
 | 107 | //     } | 
 | 108 | // | 
 | 109 | //     cc_library { | 
 | 110 | //         name: "libacme_foo", | 
 | 111 | //         defaults: ["acme_defaults"], | 
 | 112 | //         srcs: ["*.cpp"], | 
 | 113 | //     } | 
 | 114 | // | 
 | 115 | // And device/acme/Android.bp could have: | 
 | 116 | // | 
 | 117 | //     soong_config_module_type { | 
 | 118 | //         name: "acme_cc_defaults", | 
 | 119 | //         module_type: "cc_defaults", | 
 | 120 | //         config_namespace: "acme", | 
| Dan Willemsen | 2b8b89c | 2020-03-23 19:39:34 -0700 | [diff] [blame] | 121 | //         variables: ["board"], | 
 | 122 | //         bool_variables: ["feature"], | 
| Dan Willemsen | b0935db | 2020-03-23 19:42:18 -0700 | [diff] [blame] | 123 | //         value_variables: ["width"], | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 124 | //         properties: ["cflags", "srcs"], | 
 | 125 | //     } | 
 | 126 | // | 
 | 127 | //     soong_config_string_variable { | 
 | 128 | //         name: "board", | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 129 | //         values: ["soc_a", "soc_b", "soc_c"], | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 130 | //     } | 
 | 131 | // | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 132 | // If an acme BoardConfig.mk file contained: | 
| Sasha Smundak | 18fd099 | 2021-08-24 14:05:19 -0700 | [diff] [blame] | 133 | //     $(call add_sonng_config_namespace, acme) | 
 | 134 | //     $(call add_soong_config_var_value, acme, board, soc_a) | 
 | 135 | //     $(call add_soong_config_var_value, acme, feature, true) | 
 | 136 | //     $(call add_soong_config_var_value, acme, width, 200) | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 137 | // | 
| Dan Willemsen | b0935db | 2020-03-23 19:42:18 -0700 | [diff] [blame] | 138 | // Then libacme_foo would build with cflags "-DGENERIC -DSOC_A -DFEATURE -DWIDTH=200". | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 139 | // | 
 | 140 | // Alternatively, if acme BoardConfig.mk file contained: | 
 | 141 | // | 
 | 142 | //     SOONG_CONFIG_NAMESPACES += acme | 
 | 143 | //     SOONG_CONFIG_acme += \ | 
 | 144 | //         board \ | 
 | 145 | //         feature \ | 
 | 146 | // | 
 | 147 | //     SOONG_CONFIG_acme_feature := false | 
 | 148 | // | 
 | 149 | // Then libacme_foo would build with cflags: | 
 | 150 | //   "-DGENERIC -DSOC_DEFAULT -DFEATURE_DEFAULT -DSIZE=DEFAULT". | 
 | 151 | // | 
 | 152 | // Similarly, if acme BoardConfig.mk file contained: | 
 | 153 | // | 
 | 154 | //     SOONG_CONFIG_NAMESPACES += acme | 
 | 155 | //     SOONG_CONFIG_acme += \ | 
 | 156 | //         board \ | 
 | 157 | //         feature \ | 
 | 158 | // | 
 | 159 | //     SOONG_CONFIG_acme_board := soc_c | 
 | 160 | // | 
 | 161 | // Then libacme_foo would build with cflags: | 
 | 162 | //   "-DGENERIC -DSOC_DEFAULT -DFEATURE_DEFAULT -DSIZE=DEFAULT". | 
 | 163 |  | 
| Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 164 | func SoongConfigModuleTypeImportFactory() Module { | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 165 | 	module := &soongConfigModuleTypeImport{} | 
 | 166 |  | 
 | 167 | 	module.AddProperties(&module.properties) | 
 | 168 | 	AddLoadHook(module, func(ctx LoadHookContext) { | 
 | 169 | 		importModuleTypes(ctx, module.properties.From, module.properties.Module_types...) | 
 | 170 | 	}) | 
 | 171 |  | 
 | 172 | 	initAndroidModuleBase(module) | 
 | 173 | 	return module | 
 | 174 | } | 
 | 175 |  | 
 | 176 | func (m *soongConfigModuleTypeImport) Name() string { | 
| Sasha Smundak | 116ec92 | 2020-03-10 16:10:06 -0700 | [diff] [blame] | 177 | 	// The generated name is non-deterministic, but it does not | 
 | 178 | 	// matter because this module does not emit any rules. | 
 | 179 | 	return soongconfig.CanonicalizeToProperty(m.properties.From) + | 
 | 180 | 		"soong_config_module_type_import_" + fmt.Sprintf("%p", m) | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 181 | } | 
 | 182 |  | 
| Colin Cross | a6389e9 | 2022-06-22 16:44:07 -0700 | [diff] [blame] | 183 | func (*soongConfigModuleTypeImport) Namespaceless()                            {} | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 184 | func (*soongConfigModuleTypeImport) GenerateAndroidBuildActions(ModuleContext) {} | 
 | 185 |  | 
 | 186 | // Create dummy modules for soong_config_module_type and soong_config_*_variable | 
 | 187 |  | 
 | 188 | type soongConfigModuleTypeModule struct { | 
 | 189 | 	ModuleBase | 
 | 190 | 	properties soongconfig.ModuleTypeProperties | 
 | 191 | } | 
 | 192 |  | 
 | 193 | // soong_config_module_type defines module types with conditionals on Soong config | 
| Bill Peckham | c93258b | 2020-02-04 13:17:24 -0800 | [diff] [blame] | 194 | // variables.  The new module type will exist for all modules after the definition | 
 | 195 | // in an Android.bp file, and can be imported into other Android.bp files using | 
 | 196 | // soong_config_module_type_import. | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 197 | // | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 198 | // Each soong_config_variable supports an additional value `conditions_default`. The properties | 
 | 199 | // specified in `conditions_default` will only be used under the following conditions: | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 200 | // | 
 | 201 | //	bool variable: the variable is unspecified or not set to a true value | 
 | 202 | //	value variable: the variable is unspecified | 
 | 203 | //	string variable: the variable is unspecified or the variable is set to a string unused in the | 
 | 204 | //	                 given module. For example, string variable `test` takes values: "a" and "b", | 
 | 205 | //	                 if the module contains a property `a` and `conditions_default`, when test=b, | 
 | 206 | //	                 the properties under `conditions_default` will be used. To specify that no | 
 | 207 | //	                 properties should be amended for `b`, you can set `b: {},`. | 
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 208 | // | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 209 | // For example, an Android.bp file could have: | 
 | 210 | // | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 211 | //	    soong_config_module_type { | 
 | 212 | //	        name: "acme_cc_defaults", | 
 | 213 | //	        module_type: "cc_defaults", | 
 | 214 | //	        config_namespace: "acme", | 
 | 215 | //	        variables: ["board"], | 
 | 216 | //	        bool_variables: ["feature"], | 
 | 217 | //	        value_variables: ["width"], | 
 | 218 | //	        properties: ["cflags", "srcs"], | 
 | 219 | //	    } | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 220 | // | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 221 | //	    soong_config_string_variable { | 
 | 222 | //	        name: "board", | 
 | 223 | //	        values: ["soc_a", "soc_b"], | 
 | 224 | //	    } | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 225 | // | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 226 | //	    acme_cc_defaults { | 
 | 227 | //	        name: "acme_defaults", | 
 | 228 | //	        cflags: ["-DGENERIC"], | 
 | 229 | //	        soong_config_variables: { | 
 | 230 | //	            board: { | 
 | 231 | //	                soc_a: { | 
 | 232 | //	                    cflags: ["-DSOC_A"], | 
 | 233 | //	                }, | 
 | 234 | //	                soc_b: { | 
 | 235 | //	                    cflags: ["-DSOC_B"], | 
 | 236 | //	                }, | 
 | 237 | //	                conditions_default: { | 
 | 238 | //	                    cflags: ["-DSOC_DEFAULT"], | 
 | 239 | //	                }, | 
 | 240 | //	            }, | 
 | 241 | //	            feature: { | 
 | 242 | //	                cflags: ["-DFEATURE"], | 
 | 243 | //	                conditions_default: { | 
 | 244 | //	                    cflags: ["-DFEATURE_DEFAULT"], | 
 | 245 | //	                }, | 
 | 246 | //	            }, | 
 | 247 | //	            width: { | 
 | 248 | //		               cflags: ["-DWIDTH=%s"], | 
 | 249 | //	                conditions_default: { | 
 | 250 | //	                    cflags: ["-DWIDTH=DEFAULT"], | 
 | 251 | //	                }, | 
 | 252 | //	            }, | 
 | 253 | //	        }, | 
 | 254 | //	    } | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 255 | // | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 256 | //	    cc_library { | 
 | 257 | //	        name: "libacme_foo", | 
 | 258 | //	        defaults: ["acme_defaults"], | 
 | 259 | //	        srcs: ["*.cpp"], | 
 | 260 | //	    } | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 261 | // | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 262 | // If an acme BoardConfig.mk file contained: | 
 | 263 | // | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 264 | //	SOONG_CONFIG_NAMESPACES += acme | 
 | 265 | //	SOONG_CONFIG_acme += \ | 
 | 266 | //	    board \ | 
 | 267 | //	    feature \ | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 268 | // | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 269 | //	SOONG_CONFIG_acme_board := soc_a | 
 | 270 | //	SOONG_CONFIG_acme_feature := true | 
 | 271 | //	SOONG_CONFIG_acme_width := 200 | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 272 | // | 
 | 273 | // Then libacme_foo would build with cflags "-DGENERIC -DSOC_A -DFEATURE". | 
| Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 274 | func SoongConfigModuleTypeFactory() Module { | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 275 | 	module := &soongConfigModuleTypeModule{} | 
 | 276 |  | 
 | 277 | 	module.AddProperties(&module.properties) | 
 | 278 |  | 
 | 279 | 	AddLoadHook(module, func(ctx LoadHookContext) { | 
 | 280 | 		// A soong_config_module_type module should implicitly import itself. | 
 | 281 | 		importModuleTypes(ctx, ctx.BlueprintsFile(), module.properties.Name) | 
 | 282 | 	}) | 
 | 283 |  | 
 | 284 | 	initAndroidModuleBase(module) | 
 | 285 |  | 
 | 286 | 	return module | 
 | 287 | } | 
 | 288 |  | 
 | 289 | func (m *soongConfigModuleTypeModule) Name() string { | 
| Colin Cross | a6389e9 | 2022-06-22 16:44:07 -0700 | [diff] [blame] | 290 | 	return m.properties.Name + fmt.Sprintf("%p", m) | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 291 | } | 
| Colin Cross | a6389e9 | 2022-06-22 16:44:07 -0700 | [diff] [blame] | 292 | func (*soongConfigModuleTypeModule) Namespaceless()                                {} | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 293 | func (*soongConfigModuleTypeModule) GenerateAndroidBuildActions(ctx ModuleContext) {} | 
 | 294 |  | 
 | 295 | type soongConfigStringVariableDummyModule struct { | 
 | 296 | 	ModuleBase | 
 | 297 | 	properties       soongconfig.VariableProperties | 
 | 298 | 	stringProperties soongconfig.StringVariableProperties | 
 | 299 | } | 
 | 300 |  | 
 | 301 | type soongConfigBoolVariableDummyModule struct { | 
 | 302 | 	ModuleBase | 
 | 303 | 	properties soongconfig.VariableProperties | 
 | 304 | } | 
 | 305 |  | 
 | 306 | // soong_config_string_variable defines a variable and a set of possible string values for use | 
 | 307 | // in a soong_config_module_type definition. | 
| Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 308 | func SoongConfigStringVariableDummyFactory() Module { | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 309 | 	module := &soongConfigStringVariableDummyModule{} | 
 | 310 | 	module.AddProperties(&module.properties, &module.stringProperties) | 
 | 311 | 	initAndroidModuleBase(module) | 
 | 312 | 	return module | 
 | 313 | } | 
 | 314 |  | 
 | 315 | // soong_config_string_variable defines a variable with true or false values for use | 
 | 316 | // in a soong_config_module_type definition. | 
| Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 317 | func SoongConfigBoolVariableDummyFactory() Module { | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 318 | 	module := &soongConfigBoolVariableDummyModule{} | 
 | 319 | 	module.AddProperties(&module.properties) | 
 | 320 | 	initAndroidModuleBase(module) | 
 | 321 | 	return module | 
 | 322 | } | 
 | 323 |  | 
 | 324 | func (m *soongConfigStringVariableDummyModule) Name() string { | 
| Colin Cross | a6389e9 | 2022-06-22 16:44:07 -0700 | [diff] [blame] | 325 | 	return m.properties.Name + fmt.Sprintf("%p", m) | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 326 | } | 
| Colin Cross | a6389e9 | 2022-06-22 16:44:07 -0700 | [diff] [blame] | 327 | func (*soongConfigStringVariableDummyModule) Namespaceless()                                {} | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 328 | func (*soongConfigStringVariableDummyModule) GenerateAndroidBuildActions(ctx ModuleContext) {} | 
 | 329 |  | 
 | 330 | func (m *soongConfigBoolVariableDummyModule) Name() string { | 
| Colin Cross | a6389e9 | 2022-06-22 16:44:07 -0700 | [diff] [blame] | 331 | 	return m.properties.Name + fmt.Sprintf("%p", m) | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 332 | } | 
| Colin Cross | a6389e9 | 2022-06-22 16:44:07 -0700 | [diff] [blame] | 333 | func (*soongConfigBoolVariableDummyModule) Namespaceless()                                {} | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 334 | func (*soongConfigBoolVariableDummyModule) GenerateAndroidBuildActions(ctx ModuleContext) {} | 
 | 335 |  | 
| Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 336 | // importModuleTypes registers the module factories for a list of module types defined | 
 | 337 | // in an Android.bp file. These module factories are scoped for the current Android.bp | 
 | 338 | // file only. | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 339 | func importModuleTypes(ctx LoadHookContext, from string, moduleTypes ...string) { | 
 | 340 | 	from = filepath.Clean(from) | 
 | 341 | 	if filepath.Ext(from) != ".bp" { | 
 | 342 | 		ctx.PropertyErrorf("from", "%q must be a file with extension .bp", from) | 
 | 343 | 		return | 
 | 344 | 	} | 
 | 345 |  | 
 | 346 | 	if strings.HasPrefix(from, "../") { | 
 | 347 | 		ctx.PropertyErrorf("from", "%q must not use ../ to escape the source tree", | 
 | 348 | 			from) | 
 | 349 | 		return | 
 | 350 | 	} | 
 | 351 |  | 
 | 352 | 	moduleTypeDefinitions := loadSoongConfigModuleTypeDefinition(ctx, from) | 
 | 353 | 	if moduleTypeDefinitions == nil { | 
 | 354 | 		return | 
 | 355 | 	} | 
 | 356 | 	for _, moduleType := range moduleTypes { | 
 | 357 | 		if factory, ok := moduleTypeDefinitions[moduleType]; ok { | 
 | 358 | 			ctx.registerScopedModuleType(moduleType, factory) | 
 | 359 | 		} else { | 
 | 360 | 			ctx.PropertyErrorf("module_types", "module type %q not defined in %q", | 
 | 361 | 				moduleType, from) | 
 | 362 | 		} | 
 | 363 | 	} | 
 | 364 | } | 
 | 365 |  | 
 | 366 | // loadSoongConfigModuleTypeDefinition loads module types from an Android.bp file.  It caches the | 
 | 367 | // result so each file is only parsed once. | 
 | 368 | func loadSoongConfigModuleTypeDefinition(ctx LoadHookContext, from string) map[string]blueprint.ModuleFactory { | 
 | 369 | 	type onceKeyType string | 
 | 370 | 	key := NewCustomOnceKey(onceKeyType(filepath.Clean(from))) | 
 | 371 |  | 
 | 372 | 	reportErrors := func(ctx LoadHookContext, filename string, errs ...error) { | 
 | 373 | 		for _, err := range errs { | 
 | 374 | 			if parseErr, ok := err.(*parser.ParseError); ok { | 
 | 375 | 				ctx.Errorf(parseErr.Pos, "%s", parseErr.Err) | 
 | 376 | 			} else { | 
 | 377 | 				ctx.Errorf(scanner.Position{Filename: filename}, "%s", err) | 
 | 378 | 			} | 
 | 379 | 		} | 
 | 380 | 	} | 
 | 381 |  | 
 | 382 | 	return ctx.Config().Once(key, func() interface{} { | 
| Colin Cross | 39e545c | 2020-02-05 16:26:19 -0800 | [diff] [blame] | 383 | 		ctx.AddNinjaFileDeps(from) | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 384 | 		r, err := ctx.Config().fs.Open(from) | 
 | 385 | 		if err != nil { | 
 | 386 | 			ctx.PropertyErrorf("from", "failed to open %q: %s", from, err) | 
 | 387 | 			return (map[string]blueprint.ModuleFactory)(nil) | 
 | 388 | 		} | 
| Liz Kammer | 0fe123d | 2022-02-07 10:17:35 -0500 | [diff] [blame] | 389 | 		defer r.Close() | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 390 |  | 
 | 391 | 		mtDef, errs := soongconfig.Parse(r, from) | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 392 | 		if len(errs) > 0 { | 
 | 393 | 			reportErrors(ctx, from, errs...) | 
 | 394 | 			return (map[string]blueprint.ModuleFactory)(nil) | 
 | 395 | 		} | 
 | 396 |  | 
 | 397 | 		globalModuleTypes := ctx.moduleFactories() | 
 | 398 |  | 
 | 399 | 		factories := make(map[string]blueprint.ModuleFactory) | 
 | 400 |  | 
 | 401 | 		for name, moduleType := range mtDef.ModuleTypes { | 
 | 402 | 			factory := globalModuleTypes[moduleType.BaseModuleType] | 
 | 403 | 			if factory != nil { | 
| Colin Cross | 8ff1058 | 2023-12-07 13:10:56 -0800 | [diff] [blame] | 404 | 				factories[name] = configModuleFactory(factory, moduleType) | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 405 | 			} else { | 
 | 406 | 				reportErrors(ctx, from, | 
 | 407 | 					fmt.Errorf("missing global module type factory for %q", moduleType.BaseModuleType)) | 
 | 408 | 			} | 
 | 409 | 		} | 
 | 410 |  | 
 | 411 | 		if ctx.Failed() { | 
 | 412 | 			return (map[string]blueprint.ModuleFactory)(nil) | 
 | 413 | 		} | 
 | 414 |  | 
 | 415 | 		return factories | 
 | 416 | 	}).(map[string]blueprint.ModuleFactory) | 
 | 417 | } | 
 | 418 |  | 
| Inseob Kim | 81b00a8 | 2023-05-15 18:06:31 +0900 | [diff] [blame] | 419 | // tracingConfig is a wrapper to soongconfig.SoongConfig which records all accesses to SoongConfig. | 
 | 420 | type tracingConfig struct { | 
 | 421 | 	config    soongconfig.SoongConfig | 
 | 422 | 	boolSet   map[string]bool | 
 | 423 | 	stringSet map[string]string | 
 | 424 | 	isSetSet  map[string]bool | 
 | 425 | } | 
 | 426 |  | 
 | 427 | func (c *tracingConfig) Bool(name string) bool { | 
 | 428 | 	c.boolSet[name] = c.config.Bool(name) | 
 | 429 | 	return c.boolSet[name] | 
 | 430 | } | 
 | 431 |  | 
 | 432 | func (c *tracingConfig) String(name string) string { | 
 | 433 | 	c.stringSet[name] = c.config.String(name) | 
 | 434 | 	return c.stringSet[name] | 
 | 435 | } | 
 | 436 |  | 
 | 437 | func (c *tracingConfig) IsSet(name string) bool { | 
 | 438 | 	c.isSetSet[name] = c.config.IsSet(name) | 
 | 439 | 	return c.isSetSet[name] | 
 | 440 | } | 
 | 441 |  | 
 | 442 | func (c *tracingConfig) getTrace() soongConfigTrace { | 
 | 443 | 	ret := soongConfigTrace{} | 
 | 444 |  | 
 | 445 | 	for k, v := range c.boolSet { | 
 | 446 | 		ret.Bools = append(ret.Bools, fmt.Sprintf("%q:%t", k, v)) | 
 | 447 | 	} | 
 | 448 | 	for k, v := range c.stringSet { | 
 | 449 | 		ret.Strings = append(ret.Strings, fmt.Sprintf("%q:%q", k, v)) | 
 | 450 | 	} | 
 | 451 | 	for k, v := range c.isSetSet { | 
 | 452 | 		ret.IsSets = append(ret.IsSets, fmt.Sprintf("%q:%t", k, v)) | 
 | 453 | 	} | 
 | 454 |  | 
 | 455 | 	return ret | 
 | 456 | } | 
 | 457 |  | 
 | 458 | func newTracingConfig(config soongconfig.SoongConfig) *tracingConfig { | 
 | 459 | 	c := tracingConfig{ | 
 | 460 | 		config:    config, | 
 | 461 | 		boolSet:   make(map[string]bool), | 
 | 462 | 		stringSet: make(map[string]string), | 
 | 463 | 		isSetSet:  make(map[string]bool), | 
 | 464 | 	} | 
 | 465 | 	return &c | 
 | 466 | } | 
 | 467 |  | 
 | 468 | var _ soongconfig.SoongConfig = (*tracingConfig)(nil) | 
 | 469 |  | 
| Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 470 | // configModuleFactory takes an existing soongConfigModuleFactory and a | 
 | 471 | // ModuleType to create a new ModuleFactory that uses a custom loadhook. | 
| Colin Cross | 8ff1058 | 2023-12-07 13:10:56 -0800 | [diff] [blame] | 472 | func configModuleFactory(factory blueprint.ModuleFactory, moduleType *soongconfig.ModuleType) blueprint.ModuleFactory { | 
| Paul Duffin | e8b4768 | 2023-01-09 15:42:57 +0000 | [diff] [blame] | 473 | 	// Defer creation of conditional properties struct until the first call from the factory | 
 | 474 | 	// method. That avoids having to make a special call to the factory to create the properties | 
 | 475 | 	// structs from which the conditional properties struct is created. This is needed in order to | 
 | 476 | 	// allow singleton modules to be customized by soong_config_module_type as the | 
 | 477 | 	// SingletonModuleFactoryAdaptor factory registers a load hook for the singleton module | 
 | 478 | 	// everytime that it is called. Calling the factory twice causes a build failure as the load | 
 | 479 | 	// hook is called twice, the first time it updates the singleton module to indicate that it has | 
 | 480 | 	// been registered as a module, and the second time it fails because it thinks it has been | 
 | 481 | 	// registered again and a singleton module can only be registered once. | 
 | 482 | 	// | 
 | 483 | 	// This is an issue for singleton modules because: | 
 | 484 | 	// * Load hooks are registered on the module object and are only called when the module object | 
 | 485 | 	//   is created by Blueprint while processing the Android.bp file. | 
 | 486 | 	// * The module factory for a singleton module returns the same module object each time it is | 
 | 487 | 	//   called, and registers its load hook on that same module object. | 
 | 488 | 	// * When the module factory is called by Blueprint it then calls all the load hooks that have | 
 | 489 | 	//   been registered for every call to that module factory. | 
 | 490 | 	// | 
 | 491 | 	// It is not an issue for normal modules because they return a new module object each time the | 
 | 492 | 	// factory is called and so any load hooks registered on module objects which are discarded will | 
 | 493 | 	// not be run. | 
 | 494 | 	once := &sync.Once{} | 
 | 495 | 	conditionalFactoryProps := reflect.Value{} | 
 | 496 | 	getConditionalFactoryProps := func(props []interface{}) reflect.Value { | 
 | 497 | 		once.Do(func() { | 
 | 498 | 			conditionalFactoryProps = soongconfig.CreateProperties(props, moduleType) | 
 | 499 | 		}) | 
 | 500 | 		return conditionalFactoryProps | 
| Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 501 | 	} | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 502 |  | 
| Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 503 | 	return func() (blueprint.Module, []interface{}) { | 
 | 504 | 		module, props := factory() | 
| Paul Duffin | e8b4768 | 2023-01-09 15:42:57 +0000 | [diff] [blame] | 505 | 		conditionalFactoryProps := getConditionalFactoryProps(props) | 
 | 506 | 		if !conditionalFactoryProps.IsValid() { | 
 | 507 | 			return module, props | 
 | 508 | 		} | 
 | 509 |  | 
| Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 510 | 		conditionalProps := proptools.CloneEmptyProperties(conditionalFactoryProps) | 
 | 511 | 		props = append(props, conditionalProps.Interface()) | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 512 |  | 
| Colin Cross | 8ff1058 | 2023-12-07 13:10:56 -0800 | [diff] [blame] | 513 | 		// Regular Soong operation wraps the existing module factory with a | 
 | 514 | 		// conditional on Soong config variables by reading the product | 
 | 515 | 		// config variables from Make. | 
 | 516 | 		AddLoadHook(module, func(ctx LoadHookContext) { | 
 | 517 | 			tracingConfig := newTracingConfig(ctx.Config().VendorConfig(moduleType.ConfigNamespace)) | 
 | 518 | 			newProps, err := soongconfig.PropertiesToApply(moduleType, conditionalProps, tracingConfig) | 
 | 519 | 			if err != nil { | 
 | 520 | 				ctx.ModuleErrorf("%s", err) | 
 | 521 | 				return | 
 | 522 | 			} | 
 | 523 | 			for _, ps := range newProps { | 
 | 524 | 				ctx.AppendProperties(ps) | 
 | 525 | 			} | 
| Inseob Kim | 81b00a8 | 2023-05-15 18:06:31 +0900 | [diff] [blame] | 526 |  | 
| Colin Cross | 8ff1058 | 2023-12-07 13:10:56 -0800 | [diff] [blame] | 527 | 			module.(Module).base().commonProperties.SoongConfigTrace = tracingConfig.getTrace() | 
 | 528 | 		}) | 
| Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 529 | 		return module, props | 
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 530 | 	} | 
 | 531 | } |