blob: 17f6d66a8b51423b22db75fba73fe00872bc0477 [file] [log] [blame]
Colin Cross9d34f352019-11-22 16:03:51 -08001// 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
15package android
16
17// This file provides module types that implement wrapper module types that add conditionals on
18// Soong config variables.
19
20import (
21 "fmt"
22 "path/filepath"
23 "strings"
24 "text/scanner"
25
26 "github.com/google/blueprint"
27 "github.com/google/blueprint/parser"
28 "github.com/google/blueprint/proptools"
29
30 "android/soong/android/soongconfig"
31)
32
33func init() {
34 RegisterModuleType("soong_config_module_type_import", soongConfigModuleTypeImportFactory)
35 RegisterModuleType("soong_config_module_type", soongConfigModuleTypeFactory)
36 RegisterModuleType("soong_config_string_variable", soongConfigStringVariableDummyFactory)
37 RegisterModuleType("soong_config_bool_variable", soongConfigBoolVariableDummyFactory)
38}
39
40type soongConfigModuleTypeImport struct {
41 ModuleBase
42 properties soongConfigModuleTypeImportProperties
43}
44
45type soongConfigModuleTypeImportProperties struct {
46 From string
47 Module_types []string
48}
49
50// soong_config_module_type_import imports module types with conditionals on Soong config
51// variables from another Android.bp file. The imported module type will exist for all
52// modules after the import in the Android.bp file.
53//
Liz Kammer432bd592020-12-16 12:42:02 -080054// Each soong_config_variable supports an additional value `conditions_default`. The properties
55// specified in `conditions_default` will only be used under the following conditions:
56// bool variable: the variable is unspecified or not set to a true value
57// value variable: the variable is unspecified
58// string variable: the variable is unspecified or the variable is set to a string unused in the
59// given module. For example, string variable `test` takes values: "a" and "b",
60// if the module contains a property `a` and `conditions_default`, when test=b,
61// the properties under `conditions_default` will be used. To specify that no
62// properties should be amended for `b`, you can set `b: {},`.
63//
Colin Cross9d34f352019-11-22 16:03:51 -080064// For example, an Android.bp file could have:
65//
66// soong_config_module_type_import {
Bill Peckhamc93258b2020-02-04 13:17:24 -080067// from: "device/acme/Android.bp",
Colin Cross9d34f352019-11-22 16:03:51 -080068// module_types: ["acme_cc_defaults"],
69// }
70//
71// acme_cc_defaults {
72// name: "acme_defaults",
73// cflags: ["-DGENERIC"],
74// soong_config_variables: {
75// board: {
76// soc_a: {
77// cflags: ["-DSOC_A"],
78// },
79// soc_b: {
80// cflags: ["-DSOC_B"],
81// },
Liz Kammer432bd592020-12-16 12:42:02 -080082// conditions_default: {
83// cflags: ["-DSOC_DEFAULT"],
84// },
Colin Cross9d34f352019-11-22 16:03:51 -080085// },
86// feature: {
87// cflags: ["-DFEATURE"],
Liz Kammer432bd592020-12-16 12:42:02 -080088// conditions_default: {
89// cflags: ["-DFEATURE_DEFAULT"],
90// },
Colin Cross9d34f352019-11-22 16:03:51 -080091// },
Dan Willemsenb0935db2020-03-23 19:42:18 -070092// width: {
93// cflags: ["-DWIDTH=%s"],
Liz Kammer432bd592020-12-16 12:42:02 -080094// conditions_default: {
95// cflags: ["-DWIDTH=DEFAULT"],
96// },
Dan Willemsenb0935db2020-03-23 19:42:18 -070097// },
Colin Cross9d34f352019-11-22 16:03:51 -080098// },
99// }
100//
101// cc_library {
102// name: "libacme_foo",
103// defaults: ["acme_defaults"],
104// srcs: ["*.cpp"],
105// }
106//
107// And device/acme/Android.bp could have:
108//
109// soong_config_module_type {
110// name: "acme_cc_defaults",
111// module_type: "cc_defaults",
112// config_namespace: "acme",
Dan Willemsen2b8b89c2020-03-23 19:39:34 -0700113// variables: ["board"],
114// bool_variables: ["feature"],
Dan Willemsenb0935db2020-03-23 19:42:18 -0700115// value_variables: ["width"],
Colin Cross9d34f352019-11-22 16:03:51 -0800116// properties: ["cflags", "srcs"],
117// }
118//
119// soong_config_string_variable {
120// name: "board",
Liz Kammer432bd592020-12-16 12:42:02 -0800121// values: ["soc_a", "soc_b", "soc_c"],
Colin Cross9d34f352019-11-22 16:03:51 -0800122// }
123//
Colin Cross9d34f352019-11-22 16:03:51 -0800124// If an acme BoardConfig.mk file contained:
Sasha Smundak18fd0992021-08-24 14:05:19 -0700125// $(call add_sonng_config_namespace, acme)
126// $(call add_soong_config_var_value, acme, board, soc_a)
127// $(call add_soong_config_var_value, acme, feature, true)
128// $(call add_soong_config_var_value, acme, width, 200)
Colin Cross9d34f352019-11-22 16:03:51 -0800129//
Dan Willemsenb0935db2020-03-23 19:42:18 -0700130// Then libacme_foo would build with cflags "-DGENERIC -DSOC_A -DFEATURE -DWIDTH=200".
Liz Kammer432bd592020-12-16 12:42:02 -0800131//
132// Alternatively, if acme BoardConfig.mk file contained:
133//
134// SOONG_CONFIG_NAMESPACES += acme
135// SOONG_CONFIG_acme += \
136// board \
137// feature \
138//
139// SOONG_CONFIG_acme_feature := false
140//
141// Then libacme_foo would build with cflags:
142// "-DGENERIC -DSOC_DEFAULT -DFEATURE_DEFAULT -DSIZE=DEFAULT".
143//
144// Similarly, if acme BoardConfig.mk file contained:
145//
146// SOONG_CONFIG_NAMESPACES += acme
147// SOONG_CONFIG_acme += \
148// board \
149// feature \
150//
151// SOONG_CONFIG_acme_board := soc_c
152//
153// Then libacme_foo would build with cflags:
154// "-DGENERIC -DSOC_DEFAULT -DFEATURE_DEFAULT -DSIZE=DEFAULT".
155
Colin Cross9d34f352019-11-22 16:03:51 -0800156func soongConfigModuleTypeImportFactory() Module {
157 module := &soongConfigModuleTypeImport{}
158
159 module.AddProperties(&module.properties)
160 AddLoadHook(module, func(ctx LoadHookContext) {
161 importModuleTypes(ctx, module.properties.From, module.properties.Module_types...)
162 })
163
164 initAndroidModuleBase(module)
165 return module
166}
167
168func (m *soongConfigModuleTypeImport) Name() string {
Sasha Smundak116ec922020-03-10 16:10:06 -0700169 // The generated name is non-deterministic, but it does not
170 // matter because this module does not emit any rules.
171 return soongconfig.CanonicalizeToProperty(m.properties.From) +
172 "soong_config_module_type_import_" + fmt.Sprintf("%p", m)
Colin Cross9d34f352019-11-22 16:03:51 -0800173}
174
175func (*soongConfigModuleTypeImport) Nameless() {}
176func (*soongConfigModuleTypeImport) GenerateAndroidBuildActions(ModuleContext) {}
177
178// Create dummy modules for soong_config_module_type and soong_config_*_variable
179
180type soongConfigModuleTypeModule struct {
181 ModuleBase
182 properties soongconfig.ModuleTypeProperties
183}
184
185// soong_config_module_type defines module types with conditionals on Soong config
Bill Peckhamc93258b2020-02-04 13:17:24 -0800186// variables. The new module type will exist for all modules after the definition
187// in an Android.bp file, and can be imported into other Android.bp files using
188// soong_config_module_type_import.
Colin Cross9d34f352019-11-22 16:03:51 -0800189//
Liz Kammer432bd592020-12-16 12:42:02 -0800190// Each soong_config_variable supports an additional value `conditions_default`. The properties
191// specified in `conditions_default` will only be used under the following conditions:
192// bool variable: the variable is unspecified or not set to a true value
193// value variable: the variable is unspecified
194// string variable: the variable is unspecified or the variable is set to a string unused in the
195// given module. For example, string variable `test` takes values: "a" and "b",
196// if the module contains a property `a` and `conditions_default`, when test=b,
197// the properties under `conditions_default` will be used. To specify that no
198// properties should be amended for `b`, you can set `b: {},`.
199//
Colin Cross9d34f352019-11-22 16:03:51 -0800200// For example, an Android.bp file could have:
201//
202// soong_config_module_type {
203// name: "acme_cc_defaults",
204// module_type: "cc_defaults",
205// config_namespace: "acme",
Dan Willemsen2b8b89c2020-03-23 19:39:34 -0700206// variables: ["board"],
207// bool_variables: ["feature"],
Dan Willemsenb0935db2020-03-23 19:42:18 -0700208// value_variables: ["width"],
Colin Cross9d34f352019-11-22 16:03:51 -0800209// properties: ["cflags", "srcs"],
210// }
211//
212// soong_config_string_variable {
213// name: "board",
214// values: ["soc_a", "soc_b"],
215// }
216//
Colin Cross9d34f352019-11-22 16:03:51 -0800217// acme_cc_defaults {
218// name: "acme_defaults",
219// cflags: ["-DGENERIC"],
220// soong_config_variables: {
221// board: {
222// soc_a: {
223// cflags: ["-DSOC_A"],
224// },
225// soc_b: {
226// cflags: ["-DSOC_B"],
227// },
Liz Kammer432bd592020-12-16 12:42:02 -0800228// conditions_default: {
229// cflags: ["-DSOC_DEFAULT"],
230// },
Colin Cross9d34f352019-11-22 16:03:51 -0800231// },
232// feature: {
233// cflags: ["-DFEATURE"],
Liz Kammer432bd592020-12-16 12:42:02 -0800234// conditions_default: {
235// cflags: ["-DFEATURE_DEFAULT"],
236// },
Colin Cross9d34f352019-11-22 16:03:51 -0800237// },
Dan Willemsenb0935db2020-03-23 19:42:18 -0700238// width: {
239// cflags: ["-DWIDTH=%s"],
Liz Kammer432bd592020-12-16 12:42:02 -0800240// conditions_default: {
241// cflags: ["-DWIDTH=DEFAULT"],
242// },
Dan Willemsenb0935db2020-03-23 19:42:18 -0700243// },
Colin Cross9d34f352019-11-22 16:03:51 -0800244// },
245// }
246//
247// cc_library {
248// name: "libacme_foo",
249// defaults: ["acme_defaults"],
250// srcs: ["*.cpp"],
251// }
252//
Colin Cross9d34f352019-11-22 16:03:51 -0800253// If an acme BoardConfig.mk file contained:
254//
255// SOONG_CONFIG_NAMESPACES += acme
256// SOONG_CONFIG_acme += \
257// board \
258// feature \
259//
260// SOONG_CONFIG_acme_board := soc_a
261// SOONG_CONFIG_acme_feature := true
Dan Willemsenb0935db2020-03-23 19:42:18 -0700262// SOONG_CONFIG_acme_width := 200
Colin Cross9d34f352019-11-22 16:03:51 -0800263//
264// Then libacme_foo would build with cflags "-DGENERIC -DSOC_A -DFEATURE".
265func soongConfigModuleTypeFactory() Module {
266 module := &soongConfigModuleTypeModule{}
267
268 module.AddProperties(&module.properties)
269
270 AddLoadHook(module, func(ctx LoadHookContext) {
271 // A soong_config_module_type module should implicitly import itself.
272 importModuleTypes(ctx, ctx.BlueprintsFile(), module.properties.Name)
273 })
274
275 initAndroidModuleBase(module)
276
277 return module
278}
279
280func (m *soongConfigModuleTypeModule) Name() string {
281 return m.properties.Name
282}
283func (*soongConfigModuleTypeModule) Nameless() {}
284func (*soongConfigModuleTypeModule) GenerateAndroidBuildActions(ctx ModuleContext) {}
285
286type soongConfigStringVariableDummyModule struct {
287 ModuleBase
288 properties soongconfig.VariableProperties
289 stringProperties soongconfig.StringVariableProperties
290}
291
292type soongConfigBoolVariableDummyModule struct {
293 ModuleBase
294 properties soongconfig.VariableProperties
295}
296
297// soong_config_string_variable defines a variable and a set of possible string values for use
298// in a soong_config_module_type definition.
299func soongConfigStringVariableDummyFactory() Module {
300 module := &soongConfigStringVariableDummyModule{}
301 module.AddProperties(&module.properties, &module.stringProperties)
302 initAndroidModuleBase(module)
303 return module
304}
305
306// soong_config_string_variable defines a variable with true or false values for use
307// in a soong_config_module_type definition.
308func soongConfigBoolVariableDummyFactory() Module {
309 module := &soongConfigBoolVariableDummyModule{}
310 module.AddProperties(&module.properties)
311 initAndroidModuleBase(module)
312 return module
313}
314
315func (m *soongConfigStringVariableDummyModule) Name() string {
316 return m.properties.Name
317}
318func (*soongConfigStringVariableDummyModule) Nameless() {}
319func (*soongConfigStringVariableDummyModule) GenerateAndroidBuildActions(ctx ModuleContext) {}
320
321func (m *soongConfigBoolVariableDummyModule) Name() string {
322 return m.properties.Name
323}
324func (*soongConfigBoolVariableDummyModule) Nameless() {}
325func (*soongConfigBoolVariableDummyModule) GenerateAndroidBuildActions(ctx ModuleContext) {}
326
327func importModuleTypes(ctx LoadHookContext, from string, moduleTypes ...string) {
328 from = filepath.Clean(from)
329 if filepath.Ext(from) != ".bp" {
330 ctx.PropertyErrorf("from", "%q must be a file with extension .bp", from)
331 return
332 }
333
334 if strings.HasPrefix(from, "../") {
335 ctx.PropertyErrorf("from", "%q must not use ../ to escape the source tree",
336 from)
337 return
338 }
339
340 moduleTypeDefinitions := loadSoongConfigModuleTypeDefinition(ctx, from)
341 if moduleTypeDefinitions == nil {
342 return
343 }
344 for _, moduleType := range moduleTypes {
345 if factory, ok := moduleTypeDefinitions[moduleType]; ok {
346 ctx.registerScopedModuleType(moduleType, factory)
347 } else {
348 ctx.PropertyErrorf("module_types", "module type %q not defined in %q",
349 moduleType, from)
350 }
351 }
352}
353
354// loadSoongConfigModuleTypeDefinition loads module types from an Android.bp file. It caches the
355// result so each file is only parsed once.
356func loadSoongConfigModuleTypeDefinition(ctx LoadHookContext, from string) map[string]blueprint.ModuleFactory {
357 type onceKeyType string
358 key := NewCustomOnceKey(onceKeyType(filepath.Clean(from)))
359
360 reportErrors := func(ctx LoadHookContext, filename string, errs ...error) {
361 for _, err := range errs {
362 if parseErr, ok := err.(*parser.ParseError); ok {
363 ctx.Errorf(parseErr.Pos, "%s", parseErr.Err)
364 } else {
365 ctx.Errorf(scanner.Position{Filename: filename}, "%s", err)
366 }
367 }
368 }
369
370 return ctx.Config().Once(key, func() interface{} {
Colin Cross39e545c2020-02-05 16:26:19 -0800371 ctx.AddNinjaFileDeps(from)
Colin Cross9d34f352019-11-22 16:03:51 -0800372 r, err := ctx.Config().fs.Open(from)
373 if err != nil {
374 ctx.PropertyErrorf("from", "failed to open %q: %s", from, err)
375 return (map[string]blueprint.ModuleFactory)(nil)
376 }
377
378 mtDef, errs := soongconfig.Parse(r, from)
379
380 if len(errs) > 0 {
381 reportErrors(ctx, from, errs...)
382 return (map[string]blueprint.ModuleFactory)(nil)
383 }
384
385 globalModuleTypes := ctx.moduleFactories()
386
387 factories := make(map[string]blueprint.ModuleFactory)
388
389 for name, moduleType := range mtDef.ModuleTypes {
390 factory := globalModuleTypes[moduleType.BaseModuleType]
391 if factory != nil {
392 factories[name] = soongConfigModuleFactory(factory, moduleType)
393 } else {
394 reportErrors(ctx, from,
395 fmt.Errorf("missing global module type factory for %q", moduleType.BaseModuleType))
396 }
397 }
398
399 if ctx.Failed() {
400 return (map[string]blueprint.ModuleFactory)(nil)
401 }
402
403 return factories
404 }).(map[string]blueprint.ModuleFactory)
405}
406
407// soongConfigModuleFactory takes an existing soongConfigModuleFactory and a ModuleType and returns
408// a new soongConfigModuleFactory that wraps the existing soongConfigModuleFactory and adds conditional on Soong config
409// variables.
410func soongConfigModuleFactory(factory blueprint.ModuleFactory,
411 moduleType *soongconfig.ModuleType) blueprint.ModuleFactory {
412
413 conditionalFactoryProps := soongconfig.CreateProperties(factory, moduleType)
414 if conditionalFactoryProps.IsValid() {
415 return func() (blueprint.Module, []interface{}) {
416 module, props := factory()
417
Colin Cross43e789d2020-01-28 09:46:50 -0800418 conditionalProps := proptools.CloneEmptyProperties(conditionalFactoryProps)
Colin Cross9d34f352019-11-22 16:03:51 -0800419 props = append(props, conditionalProps.Interface())
420
421 AddLoadHook(module, func(ctx LoadHookContext) {
422 config := ctx.Config().VendorConfig(moduleType.ConfigNamespace)
Dan Willemsenb0935db2020-03-23 19:42:18 -0700423 newProps, err := soongconfig.PropertiesToApply(moduleType, conditionalProps, config)
424 if err != nil {
425 ctx.ModuleErrorf("%s", err)
426 return
427 }
428 for _, ps := range newProps {
Colin Cross9d34f352019-11-22 16:03:51 -0800429 ctx.AppendProperties(ps)
430 }
431 })
432
433 return module, props
434 }
435 } else {
436 return factory
437 }
438}