modules.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. // Copyright 2020 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. package soongconfig
  15. import (
  16. "fmt"
  17. "io"
  18. "reflect"
  19. "sort"
  20. "strings"
  21. "sync"
  22. "github.com/google/blueprint"
  23. "github.com/google/blueprint/parser"
  24. "github.com/google/blueprint/proptools"
  25. "android/soong/starlark_fmt"
  26. )
  27. const conditionsDefault = "conditions_default"
  28. var SoongConfigProperty = proptools.FieldNameForProperty("soong_config_variables")
  29. // loadSoongConfigModuleTypeDefinition loads module types from an Android.bp file. It caches the
  30. // result so each file is only parsed once.
  31. func Parse(r io.Reader, from string) (*SoongConfigDefinition, []error) {
  32. scope := parser.NewScope(nil)
  33. file, errs := parser.ParseAndEval(from, r, scope)
  34. if len(errs) > 0 {
  35. return nil, errs
  36. }
  37. mtDef := &SoongConfigDefinition{
  38. ModuleTypes: make(map[string]*ModuleType),
  39. variables: make(map[string]soongConfigVariable),
  40. }
  41. for _, def := range file.Defs {
  42. switch def := def.(type) {
  43. case *parser.Module:
  44. newErrs := processImportModuleDef(mtDef, def)
  45. if len(newErrs) > 0 {
  46. errs = append(errs, newErrs...)
  47. }
  48. case *parser.Assignment:
  49. // Already handled via Scope object
  50. default:
  51. panic("unknown definition type")
  52. }
  53. }
  54. if len(errs) > 0 {
  55. return nil, errs
  56. }
  57. for name, moduleType := range mtDef.ModuleTypes {
  58. for _, varName := range moduleType.variableNames {
  59. if v, ok := mtDef.variables[varName]; ok {
  60. moduleType.Variables = append(moduleType.Variables, v)
  61. } else {
  62. return nil, []error{
  63. fmt.Errorf("unknown variable %q in module type %q", varName, name),
  64. }
  65. }
  66. }
  67. }
  68. return mtDef, nil
  69. }
  70. func processImportModuleDef(v *SoongConfigDefinition, def *parser.Module) (errs []error) {
  71. switch def.Type {
  72. case "soong_config_module_type":
  73. return processModuleTypeDef(v, def)
  74. case "soong_config_string_variable":
  75. return processStringVariableDef(v, def)
  76. case "soong_config_bool_variable":
  77. return processBoolVariableDef(v, def)
  78. default:
  79. // Unknown module types will be handled when the file is parsed as a normal
  80. // Android.bp file.
  81. }
  82. return nil
  83. }
  84. type ModuleTypeProperties struct {
  85. // the name of the new module type. Unlike most modules, this name does not need to be unique,
  86. // although only one module type with any name will be importable into an Android.bp file.
  87. Name string
  88. // the module type that this module type will extend.
  89. Module_type string
  90. // the SOONG_CONFIG_NAMESPACE value from a BoardConfig.mk that this module type will read
  91. // configuration variables from.
  92. Config_namespace string
  93. // the list of SOONG_CONFIG variables that this module type will read
  94. Variables []string
  95. // the list of boolean SOONG_CONFIG variables that this module type will read
  96. Bool_variables []string
  97. // the list of SOONG_CONFIG variables that this module type will read. The value will be
  98. // inserted into the properties with %s substitution.
  99. Value_variables []string
  100. // the list of properties that this module type will extend.
  101. Properties []string
  102. }
  103. func processModuleTypeDef(v *SoongConfigDefinition, def *parser.Module) (errs []error) {
  104. props := &ModuleTypeProperties{}
  105. _, errs = proptools.UnpackProperties(def.Properties, props)
  106. if len(errs) > 0 {
  107. return errs
  108. }
  109. if props.Name == "" {
  110. errs = append(errs, fmt.Errorf("name property must be set"))
  111. }
  112. if props.Config_namespace == "" {
  113. errs = append(errs, fmt.Errorf("config_namespace property must be set"))
  114. }
  115. if props.Module_type == "" {
  116. errs = append(errs, fmt.Errorf("module_type property must be set"))
  117. }
  118. if len(errs) > 0 {
  119. return errs
  120. }
  121. if mt, errs := newModuleType(props); len(errs) > 0 {
  122. return errs
  123. } else {
  124. v.ModuleTypes[props.Name] = mt
  125. }
  126. return nil
  127. }
  128. type VariableProperties struct {
  129. Name string
  130. }
  131. type StringVariableProperties struct {
  132. Values []string
  133. }
  134. func processStringVariableDef(v *SoongConfigDefinition, def *parser.Module) (errs []error) {
  135. stringProps := &StringVariableProperties{}
  136. base, errs := processVariableDef(def, stringProps)
  137. if len(errs) > 0 {
  138. return errs
  139. }
  140. if len(stringProps.Values) == 0 {
  141. return []error{fmt.Errorf("values property must be set")}
  142. }
  143. vals := make(map[string]bool, len(stringProps.Values))
  144. for _, name := range stringProps.Values {
  145. if err := checkVariableName(name); err != nil {
  146. return []error{fmt.Errorf("soong_config_string_variable: values property error %s", err)}
  147. } else if _, ok := vals[name]; ok {
  148. return []error{fmt.Errorf("soong_config_string_variable: values property error: duplicate value: %q", name)}
  149. }
  150. vals[name] = true
  151. }
  152. v.variables[base.variable] = &stringVariable{
  153. baseVariable: base,
  154. values: CanonicalizeToProperties(stringProps.Values),
  155. }
  156. return nil
  157. }
  158. func processBoolVariableDef(v *SoongConfigDefinition, def *parser.Module) (errs []error) {
  159. base, errs := processVariableDef(def)
  160. if len(errs) > 0 {
  161. return errs
  162. }
  163. v.variables[base.variable] = &boolVariable{
  164. baseVariable: base,
  165. }
  166. return nil
  167. }
  168. func processVariableDef(def *parser.Module,
  169. extraProps ...interface{}) (cond baseVariable, errs []error) {
  170. props := &VariableProperties{}
  171. allProps := append([]interface{}{props}, extraProps...)
  172. _, errs = proptools.UnpackProperties(def.Properties, allProps...)
  173. if len(errs) > 0 {
  174. return baseVariable{}, errs
  175. }
  176. if props.Name == "" {
  177. return baseVariable{}, []error{fmt.Errorf("name property must be set")}
  178. }
  179. return baseVariable{
  180. variable: props.Name,
  181. }, nil
  182. }
  183. type SoongConfigDefinition struct {
  184. ModuleTypes map[string]*ModuleType
  185. variables map[string]soongConfigVariable
  186. }
  187. // Bp2BuildSoongConfigDefinition keeps a global record of all soong config
  188. // string vars, bool vars and value vars created by every
  189. // soong_config_module_type in this build.
  190. type Bp2BuildSoongConfigDefinitions struct {
  191. // varCache contains a cache of string variables namespace + property
  192. // The same variable may be used in multiple module types (for example, if need support
  193. // for cc_default and java_default), only need to process once
  194. varCache map[string]bool
  195. StringVars map[string][]string
  196. BoolVars map[string]bool
  197. ValueVars map[string]bool
  198. }
  199. var bp2buildSoongConfigVarsLock sync.Mutex
  200. // SoongConfigVariablesForBp2build extracts information from a
  201. // SoongConfigDefinition that bp2build needs to generate constraint settings and
  202. // values for, in order to migrate soong_config_module_type usages to Bazel.
  203. func (defs *Bp2BuildSoongConfigDefinitions) AddVars(mtDef SoongConfigDefinition) {
  204. // In bp2build mode, this method is called concurrently in goroutines from
  205. // loadhooks while parsing soong_config_module_type, so add a mutex to
  206. // prevent concurrent map writes. See b/207572723
  207. bp2buildSoongConfigVarsLock.Lock()
  208. defer bp2buildSoongConfigVarsLock.Unlock()
  209. if defs.StringVars == nil {
  210. defs.StringVars = make(map[string][]string)
  211. }
  212. if defs.BoolVars == nil {
  213. defs.BoolVars = make(map[string]bool)
  214. }
  215. if defs.ValueVars == nil {
  216. defs.ValueVars = make(map[string]bool)
  217. }
  218. if defs.varCache == nil {
  219. defs.varCache = make(map[string]bool)
  220. }
  221. for _, moduleType := range mtDef.ModuleTypes {
  222. for _, v := range moduleType.Variables {
  223. key := strings.Join([]string{moduleType.ConfigNamespace, v.variableProperty()}, "__")
  224. // The same variable may be used in multiple module types (for example, if need support
  225. // for cc_default and java_default), only need to process once
  226. if _, keyInCache := defs.varCache[key]; keyInCache {
  227. continue
  228. } else {
  229. defs.varCache[key] = true
  230. }
  231. if strVar, ok := v.(*stringVariable); ok {
  232. for _, value := range strVar.values {
  233. defs.StringVars[key] = append(defs.StringVars[key], value)
  234. }
  235. } else if _, ok := v.(*boolVariable); ok {
  236. defs.BoolVars[key] = true
  237. } else if _, ok := v.(*valueVariable); ok {
  238. defs.ValueVars[key] = true
  239. } else {
  240. panic(fmt.Errorf("Unsupported variable type: %+v", v))
  241. }
  242. }
  243. }
  244. }
  245. // This is a copy of the one available in soong/android/util.go, but depending
  246. // on the android package causes a cyclic dependency. A refactoring here is to
  247. // extract common utils out from android/utils.go for other packages like this.
  248. func sortedStringKeys(m interface{}) []string {
  249. v := reflect.ValueOf(m)
  250. if v.Kind() != reflect.Map {
  251. panic(fmt.Sprintf("%#v is not a map", m))
  252. }
  253. keys := v.MapKeys()
  254. s := make([]string, 0, len(keys))
  255. for _, key := range keys {
  256. s = append(s, key.String())
  257. }
  258. sort.Strings(s)
  259. return s
  260. }
  261. // String emits the Soong config variable definitions as Starlark dictionaries.
  262. func (defs Bp2BuildSoongConfigDefinitions) String() string {
  263. ret := ""
  264. ret += "soong_config_bool_variables = "
  265. ret += starlark_fmt.PrintBoolDict(defs.BoolVars, 0)
  266. ret += "\n\n"
  267. ret += "soong_config_value_variables = "
  268. ret += starlark_fmt.PrintBoolDict(defs.ValueVars, 0)
  269. ret += "\n\n"
  270. ret += "soong_config_string_variables = "
  271. ret += starlark_fmt.PrintStringListDict(defs.StringVars, 0)
  272. return ret
  273. }
  274. // CreateProperties returns a reflect.Value of a newly constructed type that contains the desired
  275. // property layout for the Soong config variables, with each possible value an interface{} that
  276. // contains a nil pointer to another newly constructed type that contains the affectable properties.
  277. // The reflect.Value will be cloned for each call to the Soong config module type's factory method.
  278. //
  279. // For example, the acme_cc_defaults example above would
  280. // produce a reflect.Value whose type is:
  281. // *struct {
  282. // Soong_config_variables struct {
  283. // Board struct {
  284. // Soc_a interface{}
  285. // Soc_b interface{}
  286. // }
  287. // }
  288. // }
  289. // And whose value is:
  290. // &{
  291. // Soong_config_variables: {
  292. // Board: {
  293. // Soc_a: (*struct{ Cflags []string })(nil),
  294. // Soc_b: (*struct{ Cflags []string })(nil),
  295. // },
  296. // },
  297. // }
  298. func CreateProperties(factory blueprint.ModuleFactory, moduleType *ModuleType) reflect.Value {
  299. var fields []reflect.StructField
  300. _, factoryProps := factory()
  301. affectablePropertiesType := createAffectablePropertiesType(moduleType.affectableProperties, factoryProps)
  302. if affectablePropertiesType == nil {
  303. return reflect.Value{}
  304. }
  305. for _, c := range moduleType.Variables {
  306. fields = append(fields, reflect.StructField{
  307. Name: proptools.FieldNameForProperty(c.variableProperty()),
  308. Type: c.variableValuesType(),
  309. })
  310. }
  311. typ := reflect.StructOf([]reflect.StructField{{
  312. Name: SoongConfigProperty,
  313. Type: reflect.StructOf(fields),
  314. }})
  315. props := reflect.New(typ)
  316. structConditions := props.Elem().FieldByName(SoongConfigProperty)
  317. for i, c := range moduleType.Variables {
  318. c.initializeProperties(structConditions.Field(i), affectablePropertiesType)
  319. }
  320. return props
  321. }
  322. // createAffectablePropertiesType creates a reflect.Type of a struct that has a field for each affectable property
  323. // that exists in factoryProps.
  324. func createAffectablePropertiesType(affectableProperties []string, factoryProps []interface{}) reflect.Type {
  325. affectableProperties = append([]string(nil), affectableProperties...)
  326. sort.Strings(affectableProperties)
  327. var recurse func(prefix string, aps []string) ([]string, reflect.Type)
  328. recurse = func(prefix string, aps []string) ([]string, reflect.Type) {
  329. var fields []reflect.StructField
  330. // Iterate while the list is non-empty so it can be modified in the loop.
  331. for len(affectableProperties) > 0 {
  332. p := affectableProperties[0]
  333. if !strings.HasPrefix(affectableProperties[0], prefix) {
  334. // The properties are sorted and recurse is always called with a prefix that matches
  335. // the first property in the list, so if we've reached one that doesn't match the
  336. // prefix we are done with this prefix.
  337. break
  338. }
  339. nestedProperty := strings.TrimPrefix(p, prefix)
  340. if i := strings.IndexRune(nestedProperty, '.'); i >= 0 {
  341. var nestedType reflect.Type
  342. nestedPrefix := nestedProperty[:i+1]
  343. // Recurse to handle the properties with the found prefix. This will return
  344. // an updated affectableProperties with the handled entries removed from the front
  345. // of the list, and the type that contains the handled entries. The type may be
  346. // nil if none of the entries matched factoryProps.
  347. affectableProperties, nestedType = recurse(prefix+nestedPrefix, affectableProperties)
  348. if nestedType != nil {
  349. nestedFieldName := proptools.FieldNameForProperty(strings.TrimSuffix(nestedPrefix, "."))
  350. fields = append(fields, reflect.StructField{
  351. Name: nestedFieldName,
  352. Type: nestedType,
  353. })
  354. }
  355. } else {
  356. typ := typeForPropertyFromPropertyStructs(factoryProps, p)
  357. if typ != nil {
  358. fields = append(fields, reflect.StructField{
  359. Name: proptools.FieldNameForProperty(nestedProperty),
  360. Type: typ,
  361. })
  362. }
  363. // The first element in the list has been handled, remove it from the list.
  364. affectableProperties = affectableProperties[1:]
  365. }
  366. }
  367. var typ reflect.Type
  368. if len(fields) > 0 {
  369. typ = reflect.StructOf(fields)
  370. }
  371. return affectableProperties, typ
  372. }
  373. affectableProperties, typ := recurse("", affectableProperties)
  374. if len(affectableProperties) > 0 {
  375. panic(fmt.Errorf("didn't handle all affectable properties"))
  376. }
  377. if typ != nil {
  378. return reflect.PtrTo(typ)
  379. }
  380. return nil
  381. }
  382. func typeForPropertyFromPropertyStructs(psList []interface{}, property string) reflect.Type {
  383. for _, ps := range psList {
  384. if typ := typeForPropertyFromPropertyStruct(ps, property); typ != nil {
  385. return typ
  386. }
  387. }
  388. return nil
  389. }
  390. func typeForPropertyFromPropertyStruct(ps interface{}, property string) reflect.Type {
  391. v := reflect.ValueOf(ps)
  392. for len(property) > 0 {
  393. if !v.IsValid() {
  394. return nil
  395. }
  396. if v.Kind() == reflect.Interface {
  397. if v.IsNil() {
  398. return nil
  399. } else {
  400. v = v.Elem()
  401. }
  402. }
  403. if v.Kind() == reflect.Ptr {
  404. if v.IsNil() {
  405. v = reflect.Zero(v.Type().Elem())
  406. } else {
  407. v = v.Elem()
  408. }
  409. }
  410. if v.Kind() != reflect.Struct {
  411. return nil
  412. }
  413. if index := strings.IndexRune(property, '.'); index >= 0 {
  414. prefix := property[:index]
  415. property = property[index+1:]
  416. v = v.FieldByName(proptools.FieldNameForProperty(prefix))
  417. } else {
  418. f := v.FieldByName(proptools.FieldNameForProperty(property))
  419. if !f.IsValid() {
  420. return nil
  421. }
  422. return f.Type()
  423. }
  424. }
  425. return nil
  426. }
  427. // PropertiesToApply returns the applicable properties from a ModuleType that should be applied
  428. // based on SoongConfig values.
  429. // Expects that props contains a struct field with name soong_config_variables. The fields within
  430. // soong_config_variables are expected to be in the same order as moduleType.Variables.
  431. func PropertiesToApply(moduleType *ModuleType, props reflect.Value, config SoongConfig) ([]interface{}, error) {
  432. var ret []interface{}
  433. props = props.Elem().FieldByName(SoongConfigProperty)
  434. for i, c := range moduleType.Variables {
  435. if ps, err := c.PropertiesToApply(config, props.Field(i)); err != nil {
  436. return nil, err
  437. } else if ps != nil {
  438. ret = append(ret, ps)
  439. }
  440. }
  441. return ret, nil
  442. }
  443. type ModuleType struct {
  444. BaseModuleType string
  445. ConfigNamespace string
  446. Variables []soongConfigVariable
  447. affectableProperties []string
  448. variableNames []string
  449. }
  450. func newModuleType(props *ModuleTypeProperties) (*ModuleType, []error) {
  451. mt := &ModuleType{
  452. affectableProperties: props.Properties,
  453. ConfigNamespace: props.Config_namespace,
  454. BaseModuleType: props.Module_type,
  455. variableNames: props.Variables,
  456. }
  457. for _, name := range props.Bool_variables {
  458. if err := checkVariableName(name); err != nil {
  459. return nil, []error{fmt.Errorf("bool_variables %s", err)}
  460. }
  461. mt.Variables = append(mt.Variables, newBoolVariable(name))
  462. }
  463. for _, name := range props.Value_variables {
  464. if err := checkVariableName(name); err != nil {
  465. return nil, []error{fmt.Errorf("value_variables %s", err)}
  466. }
  467. mt.Variables = append(mt.Variables, &valueVariable{
  468. baseVariable: baseVariable{
  469. variable: name,
  470. },
  471. })
  472. }
  473. return mt, nil
  474. }
  475. func checkVariableName(name string) error {
  476. if name == "" {
  477. return fmt.Errorf("name must not be blank")
  478. } else if name == conditionsDefault {
  479. return fmt.Errorf("%q is reserved", conditionsDefault)
  480. }
  481. return nil
  482. }
  483. type soongConfigVariable interface {
  484. // variableProperty returns the name of the variable.
  485. variableProperty() string
  486. // conditionalValuesType returns a reflect.Type that contains an interface{} for each possible value.
  487. variableValuesType() reflect.Type
  488. // initializeProperties is passed a reflect.Value of the reflect.Type returned by conditionalValuesType and a
  489. // reflect.Type of the affectable properties, and should initialize each interface{} in the reflect.Value with
  490. // the zero value of the affectable properties type.
  491. initializeProperties(v reflect.Value, typ reflect.Type)
  492. // PropertiesToApply should return one of the interface{} values set by initializeProperties to be applied
  493. // to the module.
  494. PropertiesToApply(config SoongConfig, values reflect.Value) (interface{}, error)
  495. }
  496. type baseVariable struct {
  497. variable string
  498. }
  499. func (c *baseVariable) variableProperty() string {
  500. return CanonicalizeToProperty(c.variable)
  501. }
  502. type stringVariable struct {
  503. baseVariable
  504. values []string
  505. }
  506. func (s *stringVariable) variableValuesType() reflect.Type {
  507. var fields []reflect.StructField
  508. var values []string
  509. values = append(values, s.values...)
  510. values = append(values, conditionsDefault)
  511. for _, v := range values {
  512. fields = append(fields, reflect.StructField{
  513. Name: proptools.FieldNameForProperty(v),
  514. Type: emptyInterfaceType,
  515. })
  516. }
  517. return reflect.StructOf(fields)
  518. }
  519. // initializeProperties initializes properties to zero value of typ for supported values and a final
  520. // conditions default field.
  521. func (s *stringVariable) initializeProperties(v reflect.Value, typ reflect.Type) {
  522. for i := range s.values {
  523. v.Field(i).Set(reflect.Zero(typ))
  524. }
  525. v.Field(len(s.values)).Set(reflect.Zero(typ)) // conditions default is the final value
  526. }
  527. // Extracts an interface from values containing the properties to apply based on config.
  528. // If config does not match a value with a non-nil property set, the default value will be returned.
  529. func (s *stringVariable) PropertiesToApply(config SoongConfig, values reflect.Value) (interface{}, error) {
  530. for j, v := range s.values {
  531. f := values.Field(j)
  532. if config.String(s.variable) == v && !f.Elem().IsNil() {
  533. return f.Interface(), nil
  534. }
  535. }
  536. // if we have reached this point, we have checked all valid values of string and either:
  537. // * the value was not set
  538. // * the value was set but that value was not specified in the Android.bp file
  539. return values.Field(len(s.values)).Interface(), nil
  540. }
  541. // Struct to allow conditions set based on a boolean variable
  542. type boolVariable struct {
  543. baseVariable
  544. }
  545. // newBoolVariable constructs a boolVariable with the given name
  546. func newBoolVariable(name string) *boolVariable {
  547. return &boolVariable{
  548. baseVariable{
  549. variable: name,
  550. },
  551. }
  552. }
  553. func (b boolVariable) variableValuesType() reflect.Type {
  554. return emptyInterfaceType
  555. }
  556. // initializeProperties initializes a property to zero value of typ with an additional conditions
  557. // default field.
  558. func (b boolVariable) initializeProperties(v reflect.Value, typ reflect.Type) {
  559. initializePropertiesWithDefault(v, typ)
  560. }
  561. // initializePropertiesWithDefault, initialize with zero value, v to contain a field for each field
  562. // in typ, with an additional field for defaults of type typ. This should be used to initialize
  563. // boolVariable, valueVariable, or any future implementations of soongConfigVariable which support
  564. // one variable and a default.
  565. func initializePropertiesWithDefault(v reflect.Value, typ reflect.Type) {
  566. sTyp := typ.Elem()
  567. var fields []reflect.StructField
  568. for i := 0; i < sTyp.NumField(); i++ {
  569. fields = append(fields, sTyp.Field(i))
  570. }
  571. // create conditions_default field
  572. nestedFieldName := proptools.FieldNameForProperty(conditionsDefault)
  573. fields = append(fields, reflect.StructField{
  574. Name: nestedFieldName,
  575. Type: typ,
  576. })
  577. newTyp := reflect.PtrTo(reflect.StructOf(fields))
  578. v.Set(reflect.Zero(newTyp))
  579. }
  580. // conditionsDefaultField extracts the conditions_default field from v. This is always the final
  581. // field if initialized with initializePropertiesWithDefault.
  582. func conditionsDefaultField(v reflect.Value) reflect.Value {
  583. return v.Field(v.NumField() - 1)
  584. }
  585. // removeDefault removes the conditions_default field from values while retaining values from all
  586. // other fields. This allows
  587. func removeDefault(values reflect.Value) reflect.Value {
  588. v := values.Elem().Elem()
  589. s := conditionsDefaultField(v)
  590. // if conditions_default field was not set, there will be no issues extending properties.
  591. if !s.IsValid() {
  592. return v
  593. }
  594. // If conditions_default field was set, it has the correct type for our property. Create a new
  595. // reflect.Value of the conditions_default type and copy all fields (except for
  596. // conditions_default) based on values to the result.
  597. res := reflect.New(s.Type().Elem())
  598. for i := 0; i < res.Type().Elem().NumField(); i++ {
  599. val := v.Field(i)
  600. res.Elem().Field(i).Set(val)
  601. }
  602. return res
  603. }
  604. // PropertiesToApply returns an interface{} value based on initializeProperties to be applied to
  605. // the module. If the value was not set, conditions_default interface will be returned; otherwise,
  606. // the interface in values, without conditions_default will be returned.
  607. func (b boolVariable) PropertiesToApply(config SoongConfig, values reflect.Value) (interface{}, error) {
  608. // If this variable was not referenced in the module, there are no properties to apply.
  609. if values.Elem().IsZero() {
  610. return nil, nil
  611. }
  612. if config.Bool(b.variable) {
  613. values = removeDefault(values)
  614. return values.Interface(), nil
  615. }
  616. v := values.Elem().Elem()
  617. if f := conditionsDefaultField(v); f.IsValid() {
  618. return f.Interface(), nil
  619. }
  620. return nil, nil
  621. }
  622. // Struct to allow conditions set based on a value variable, supporting string substitution.
  623. type valueVariable struct {
  624. baseVariable
  625. }
  626. func (s *valueVariable) variableValuesType() reflect.Type {
  627. return emptyInterfaceType
  628. }
  629. // initializeProperties initializes a property to zero value of typ with an additional conditions
  630. // default field.
  631. func (s *valueVariable) initializeProperties(v reflect.Value, typ reflect.Type) {
  632. initializePropertiesWithDefault(v, typ)
  633. }
  634. // PropertiesToApply returns an interface{} value based on initializeProperties to be applied to
  635. // the module. If the variable was not set, conditions_default interface will be returned;
  636. // otherwise, the interface in values, without conditions_default will be returned with all
  637. // appropriate string substitutions based on variable being set.
  638. func (s *valueVariable) PropertiesToApply(config SoongConfig, values reflect.Value) (interface{}, error) {
  639. // If this variable was not referenced in the module, there are no properties to apply.
  640. if !values.IsValid() || values.Elem().IsZero() {
  641. return nil, nil
  642. }
  643. if !config.IsSet(s.variable) {
  644. return conditionsDefaultField(values.Elem().Elem()).Interface(), nil
  645. }
  646. configValue := config.String(s.variable)
  647. values = removeDefault(values)
  648. propStruct := values.Elem()
  649. if !propStruct.IsValid() {
  650. return nil, nil
  651. }
  652. for i := 0; i < propStruct.NumField(); i++ {
  653. field := propStruct.Field(i)
  654. kind := field.Kind()
  655. if kind == reflect.Ptr {
  656. if field.IsNil() {
  657. continue
  658. }
  659. field = field.Elem()
  660. }
  661. switch kind {
  662. case reflect.String:
  663. err := printfIntoProperty(field, configValue)
  664. if err != nil {
  665. return nil, fmt.Errorf("soong_config_variables.%s.%s: %s", s.variable, propStruct.Type().Field(i).Name, err)
  666. }
  667. case reflect.Slice:
  668. for j := 0; j < field.Len(); j++ {
  669. err := printfIntoProperty(field.Index(j), configValue)
  670. if err != nil {
  671. return nil, fmt.Errorf("soong_config_variables.%s.%s: %s", s.variable, propStruct.Type().Field(i).Name, err)
  672. }
  673. }
  674. case reflect.Bool:
  675. // Nothing to do
  676. default:
  677. return nil, fmt.Errorf("soong_config_variables.%s.%s: unsupported property type %q", s.variable, propStruct.Type().Field(i).Name, kind)
  678. }
  679. }
  680. return values.Interface(), nil
  681. }
  682. func printfIntoProperty(propertyValue reflect.Value, configValue string) error {
  683. s := propertyValue.String()
  684. count := strings.Count(s, "%")
  685. if count == 0 {
  686. return nil
  687. }
  688. if count > 1 {
  689. return fmt.Errorf("value variable properties only support a single '%%'")
  690. }
  691. if !strings.Contains(s, "%s") {
  692. return fmt.Errorf("unsupported %% in value variable property")
  693. }
  694. propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, configValue)))
  695. return nil
  696. }
  697. func CanonicalizeToProperty(v string) string {
  698. return strings.Map(func(r rune) rune {
  699. switch {
  700. case r >= 'A' && r <= 'Z',
  701. r >= 'a' && r <= 'z',
  702. r >= '0' && r <= '9',
  703. r == '_':
  704. return r
  705. default:
  706. return '_'
  707. }
  708. }, v)
  709. }
  710. func CanonicalizeToProperties(values []string) []string {
  711. ret := make([]string, len(values))
  712. for i, v := range values {
  713. ret[i] = CanonicalizeToProperty(v)
  714. }
  715. return ret
  716. }
  717. type emptyInterfaceStruct struct {
  718. i interface{}
  719. }
  720. var emptyInterfaceType = reflect.TypeOf(emptyInterfaceStruct{}).Field(0).Type