Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame^] | 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 | |
| 15 | package soongconfig |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "io" |
| 20 | "reflect" |
| 21 | "sort" |
| 22 | "strings" |
| 23 | |
| 24 | "github.com/google/blueprint" |
| 25 | "github.com/google/blueprint/parser" |
| 26 | "github.com/google/blueprint/proptools" |
| 27 | ) |
| 28 | |
| 29 | var soongConfigProperty = proptools.FieldNameForProperty("soong_config_variables") |
| 30 | |
| 31 | // loadSoongConfigModuleTypeDefinition loads module types from an Android.bp file. It caches the |
| 32 | // result so each file is only parsed once. |
| 33 | func Parse(r io.Reader, from string) (*SoongConfigDefinition, []error) { |
| 34 | scope := parser.NewScope(nil) |
| 35 | file, errs := parser.ParseAndEval(from, r, scope) |
| 36 | |
| 37 | if len(errs) > 0 { |
| 38 | return nil, errs |
| 39 | } |
| 40 | |
| 41 | mtDef := &SoongConfigDefinition{ |
| 42 | ModuleTypes: make(map[string]*ModuleType), |
| 43 | variables: make(map[string]soongConfigVariable), |
| 44 | } |
| 45 | |
| 46 | for _, def := range file.Defs { |
| 47 | switch def := def.(type) { |
| 48 | case *parser.Module: |
| 49 | newErrs := processImportModuleDef(mtDef, def) |
| 50 | |
| 51 | if len(newErrs) > 0 { |
| 52 | errs = append(errs, newErrs...) |
| 53 | } |
| 54 | |
| 55 | case *parser.Assignment: |
| 56 | // Already handled via Scope object |
| 57 | default: |
| 58 | panic("unknown definition type") |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if len(errs) > 0 { |
| 63 | return nil, errs |
| 64 | } |
| 65 | |
| 66 | for name, moduleType := range mtDef.ModuleTypes { |
| 67 | for _, varName := range moduleType.variableNames { |
| 68 | if v, ok := mtDef.variables[varName]; ok { |
| 69 | moduleType.Variables = append(moduleType.Variables, v) |
| 70 | } else { |
| 71 | return nil, []error{ |
| 72 | fmt.Errorf("unknown variable %q in module type %q", varName, name), |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return mtDef, nil |
| 79 | } |
| 80 | |
| 81 | func processImportModuleDef(v *SoongConfigDefinition, def *parser.Module) (errs []error) { |
| 82 | switch def.Type { |
| 83 | case "soong_config_module_type": |
| 84 | return processModuleTypeDef(v, def) |
| 85 | case "soong_config_string_variable": |
| 86 | return processStringVariableDef(v, def) |
| 87 | case "soong_config_bool_variable": |
| 88 | return processBoolVariableDef(v, def) |
| 89 | default: |
| 90 | // Unknown module types will be handled when the file is parsed as a normal |
| 91 | // Android.bp file. |
| 92 | } |
| 93 | |
| 94 | return nil |
| 95 | } |
| 96 | |
| 97 | type ModuleTypeProperties struct { |
| 98 | // the name of the new module type. Unlike most modules, this name does not need to be unique, |
| 99 | // although only one module type with any name will be importable into an Android.bp file. |
| 100 | Name string |
| 101 | |
| 102 | // the module type that this module type will extend. |
| 103 | Module_type string |
| 104 | |
| 105 | // the SOONG_CONFIG_NAMESPACE value from a BoardConfig.mk that this module type will read |
| 106 | // configuration variables from. |
| 107 | Config_namespace string |
| 108 | |
| 109 | // the list of SOONG_CONFIG variables that this module type will read |
| 110 | Variables []string |
| 111 | |
| 112 | // the list of properties that this module type will extend. |
| 113 | Properties []string |
| 114 | } |
| 115 | |
| 116 | func processModuleTypeDef(v *SoongConfigDefinition, def *parser.Module) (errs []error) { |
| 117 | |
| 118 | props := &ModuleTypeProperties{} |
| 119 | |
| 120 | _, errs = proptools.UnpackProperties(def.Properties, props) |
| 121 | if len(errs) > 0 { |
| 122 | return errs |
| 123 | } |
| 124 | |
| 125 | if props.Name == "" { |
| 126 | errs = append(errs, fmt.Errorf("name property must be set")) |
| 127 | } |
| 128 | |
| 129 | if props.Config_namespace == "" { |
| 130 | errs = append(errs, fmt.Errorf("config_namespace property must be set")) |
| 131 | } |
| 132 | |
| 133 | if props.Module_type == "" { |
| 134 | errs = append(errs, fmt.Errorf("module_type property must be set")) |
| 135 | } |
| 136 | |
| 137 | if len(errs) > 0 { |
| 138 | return errs |
| 139 | } |
| 140 | |
| 141 | mt := &ModuleType{ |
| 142 | affectableProperties: props.Properties, |
| 143 | ConfigNamespace: props.Config_namespace, |
| 144 | BaseModuleType: props.Module_type, |
| 145 | variableNames: props.Variables, |
| 146 | } |
| 147 | v.ModuleTypes[props.Name] = mt |
| 148 | |
| 149 | return nil |
| 150 | } |
| 151 | |
| 152 | type VariableProperties struct { |
| 153 | Name string |
| 154 | } |
| 155 | |
| 156 | type StringVariableProperties struct { |
| 157 | Values []string |
| 158 | } |
| 159 | |
| 160 | func processStringVariableDef(v *SoongConfigDefinition, def *parser.Module) (errs []error) { |
| 161 | stringProps := &StringVariableProperties{} |
| 162 | |
| 163 | base, errs := processVariableDef(def, stringProps) |
| 164 | if len(errs) > 0 { |
| 165 | return errs |
| 166 | } |
| 167 | |
| 168 | if len(stringProps.Values) == 0 { |
| 169 | return []error{fmt.Errorf("values property must be set")} |
| 170 | } |
| 171 | |
| 172 | v.variables[base.variable] = &stringVariable{ |
| 173 | baseVariable: base, |
| 174 | values: CanonicalizeToProperties(stringProps.Values), |
| 175 | } |
| 176 | |
| 177 | return nil |
| 178 | } |
| 179 | |
| 180 | func processBoolVariableDef(v *SoongConfigDefinition, def *parser.Module) (errs []error) { |
| 181 | base, errs := processVariableDef(def) |
| 182 | if len(errs) > 0 { |
| 183 | return errs |
| 184 | } |
| 185 | |
| 186 | v.variables[base.variable] = &boolVariable{ |
| 187 | baseVariable: base, |
| 188 | } |
| 189 | |
| 190 | return nil |
| 191 | } |
| 192 | |
| 193 | func processVariableDef(def *parser.Module, |
| 194 | extraProps ...interface{}) (cond baseVariable, errs []error) { |
| 195 | |
| 196 | props := &VariableProperties{} |
| 197 | |
| 198 | allProps := append([]interface{}{props}, extraProps...) |
| 199 | |
| 200 | _, errs = proptools.UnpackProperties(def.Properties, allProps...) |
| 201 | if len(errs) > 0 { |
| 202 | return baseVariable{}, errs |
| 203 | } |
| 204 | |
| 205 | if props.Name == "" { |
| 206 | return baseVariable{}, []error{fmt.Errorf("name property must be set")} |
| 207 | } |
| 208 | |
| 209 | return baseVariable{ |
| 210 | variable: props.Name, |
| 211 | }, nil |
| 212 | } |
| 213 | |
| 214 | type SoongConfigDefinition struct { |
| 215 | ModuleTypes map[string]*ModuleType |
| 216 | |
| 217 | variables map[string]soongConfigVariable |
| 218 | } |
| 219 | |
| 220 | // CreateProperties returns a reflect.Value of a newly constructed type that contains the desired |
| 221 | // property layout for the Soong config variables, with each possible value an interface{} that |
| 222 | // contains a nil pointer to another newly constructed type that contains the affectable properties. |
| 223 | // The reflect.Value will be cloned for each call to the Soong config module type's factory method. |
| 224 | // |
| 225 | // For example, the acme_cc_defaults example above would |
| 226 | // produce a reflect.Value whose type is: |
| 227 | // *struct { |
| 228 | // Soong_config_variables struct { |
| 229 | // Board struct { |
| 230 | // Soc_a interface{} |
| 231 | // Soc_b interface{} |
| 232 | // } |
| 233 | // } |
| 234 | // } |
| 235 | // And whose value is: |
| 236 | // &{ |
| 237 | // Soong_config_variables: { |
| 238 | // Board: { |
| 239 | // Soc_a: (*struct{ Cflags []string })(nil), |
| 240 | // Soc_b: (*struct{ Cflags []string })(nil), |
| 241 | // }, |
| 242 | // }, |
| 243 | // } |
| 244 | func CreateProperties(factory blueprint.ModuleFactory, moduleType *ModuleType) reflect.Value { |
| 245 | var fields []reflect.StructField |
| 246 | |
| 247 | _, factoryProps := factory() |
| 248 | affectablePropertiesType := createAffectablePropertiesType(moduleType.affectableProperties, factoryProps) |
| 249 | if affectablePropertiesType == nil { |
| 250 | return reflect.Value{} |
| 251 | } |
| 252 | |
| 253 | for _, c := range moduleType.Variables { |
| 254 | fields = append(fields, reflect.StructField{ |
| 255 | Name: proptools.FieldNameForProperty(c.variableProperty()), |
| 256 | Type: c.variableValuesType(), |
| 257 | }) |
| 258 | } |
| 259 | |
| 260 | typ := reflect.StructOf([]reflect.StructField{{ |
| 261 | Name: soongConfigProperty, |
| 262 | Type: reflect.StructOf(fields), |
| 263 | }}) |
| 264 | |
| 265 | props := reflect.New(typ) |
| 266 | structConditions := props.Elem().FieldByName(soongConfigProperty) |
| 267 | |
| 268 | for i, c := range moduleType.Variables { |
| 269 | c.initializeProperties(structConditions.Field(i), affectablePropertiesType) |
| 270 | } |
| 271 | |
| 272 | return props |
| 273 | } |
| 274 | |
| 275 | // createAffectablePropertiesType creates a reflect.Type of a struct that has a field for each affectable property |
| 276 | // that exists in factoryProps. |
| 277 | func createAffectablePropertiesType(affectableProperties []string, factoryProps []interface{}) reflect.Type { |
| 278 | affectableProperties = append([]string(nil), affectableProperties...) |
| 279 | sort.Strings(affectableProperties) |
| 280 | |
| 281 | var recurse func(prefix string, aps []string) ([]string, reflect.Type) |
| 282 | recurse = func(prefix string, aps []string) ([]string, reflect.Type) { |
| 283 | var fields []reflect.StructField |
| 284 | |
| 285 | for len(affectableProperties) > 0 { |
| 286 | p := affectableProperties[0] |
| 287 | if !strings.HasPrefix(affectableProperties[0], prefix) { |
| 288 | break |
| 289 | } |
| 290 | affectableProperties = affectableProperties[1:] |
| 291 | |
| 292 | nestedProperty := strings.TrimPrefix(p, prefix) |
| 293 | if i := strings.IndexRune(nestedProperty, '.'); i >= 0 { |
| 294 | var nestedType reflect.Type |
| 295 | nestedPrefix := nestedProperty[:i+1] |
| 296 | |
| 297 | affectableProperties, nestedType = recurse(prefix+nestedPrefix, affectableProperties) |
| 298 | |
| 299 | if nestedType != nil { |
| 300 | nestedFieldName := proptools.FieldNameForProperty(strings.TrimSuffix(nestedPrefix, ".")) |
| 301 | |
| 302 | fields = append(fields, reflect.StructField{ |
| 303 | Name: nestedFieldName, |
| 304 | Type: nestedType, |
| 305 | }) |
| 306 | } |
| 307 | } else { |
| 308 | typ := typeForPropertyFromPropertyStructs(factoryProps, p) |
| 309 | if typ != nil { |
| 310 | fields = append(fields, reflect.StructField{ |
| 311 | Name: proptools.FieldNameForProperty(nestedProperty), |
| 312 | Type: typ, |
| 313 | }) |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | var typ reflect.Type |
| 319 | if len(fields) > 0 { |
| 320 | typ = reflect.StructOf(fields) |
| 321 | } |
| 322 | return affectableProperties, typ |
| 323 | } |
| 324 | |
| 325 | affectableProperties, typ := recurse("", affectableProperties) |
| 326 | if len(affectableProperties) > 0 { |
| 327 | panic(fmt.Errorf("didn't handle all affectable properties")) |
| 328 | } |
| 329 | |
| 330 | if typ != nil { |
| 331 | return reflect.PtrTo(typ) |
| 332 | } |
| 333 | |
| 334 | return nil |
| 335 | } |
| 336 | |
| 337 | func typeForPropertyFromPropertyStructs(psList []interface{}, property string) reflect.Type { |
| 338 | for _, ps := range psList { |
| 339 | if typ := typeForPropertyFromPropertyStruct(ps, property); typ != nil { |
| 340 | return typ |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | return nil |
| 345 | } |
| 346 | |
| 347 | func typeForPropertyFromPropertyStruct(ps interface{}, property string) reflect.Type { |
| 348 | v := reflect.ValueOf(ps) |
| 349 | for len(property) > 0 { |
| 350 | if !v.IsValid() { |
| 351 | return nil |
| 352 | } |
| 353 | |
| 354 | if v.Kind() == reflect.Interface { |
| 355 | if v.IsNil() { |
| 356 | return nil |
| 357 | } else { |
| 358 | v = v.Elem() |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | if v.Kind() == reflect.Ptr { |
| 363 | if v.IsNil() { |
| 364 | v = reflect.Zero(v.Type().Elem()) |
| 365 | } else { |
| 366 | v = v.Elem() |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | if v.Kind() != reflect.Struct { |
| 371 | return nil |
| 372 | } |
| 373 | |
| 374 | if index := strings.IndexRune(property, '.'); index >= 0 { |
| 375 | prefix := property[:index] |
| 376 | property = property[index+1:] |
| 377 | |
| 378 | v = v.FieldByName(proptools.FieldNameForProperty(prefix)) |
| 379 | } else { |
| 380 | f := v.FieldByName(proptools.FieldNameForProperty(property)) |
| 381 | if !f.IsValid() { |
| 382 | return nil |
| 383 | } |
| 384 | return f.Type() |
| 385 | } |
| 386 | } |
| 387 | return nil |
| 388 | } |
| 389 | |
| 390 | // PropertiesToApply returns the applicable properties from a ModuleType that should be applied |
| 391 | // based on SoongConfig values. |
| 392 | func PropertiesToApply(moduleType *ModuleType, props reflect.Value, config SoongConfig) []interface{} { |
| 393 | var ret []interface{} |
| 394 | props = props.Elem().FieldByName(soongConfigProperty) |
| 395 | for i, c := range moduleType.Variables { |
| 396 | if ps := c.PropertiesToApply(config, props.Field(i)); ps != nil { |
| 397 | ret = append(ret, ps) |
| 398 | } |
| 399 | } |
| 400 | return ret |
| 401 | } |
| 402 | |
| 403 | type ModuleType struct { |
| 404 | BaseModuleType string |
| 405 | ConfigNamespace string |
| 406 | Variables []soongConfigVariable |
| 407 | |
| 408 | affectableProperties []string |
| 409 | variableNames []string |
| 410 | } |
| 411 | |
| 412 | type soongConfigVariable interface { |
| 413 | // variableProperty returns the name of the variable. |
| 414 | variableProperty() string |
| 415 | |
| 416 | // conditionalValuesType returns a reflect.Type that contains an interface{} for each possible value. |
| 417 | variableValuesType() reflect.Type |
| 418 | |
| 419 | // initializeProperties is passed a reflect.Value of the reflect.Type returned by conditionalValuesType and a |
| 420 | // reflect.Type of the affectable properties, and should initialize each interface{} in the reflect.Value with |
| 421 | // the zero value of the affectable properties type. |
| 422 | initializeProperties(v reflect.Value, typ reflect.Type) |
| 423 | |
| 424 | // PropertiesToApply should return one of the interface{} values set by initializeProperties to be applied |
| 425 | // to the module. |
| 426 | PropertiesToApply(config SoongConfig, values reflect.Value) interface{} |
| 427 | } |
| 428 | |
| 429 | type baseVariable struct { |
| 430 | variable string |
| 431 | } |
| 432 | |
| 433 | func (c *baseVariable) variableProperty() string { |
| 434 | return CanonicalizeToProperty(c.variable) |
| 435 | } |
| 436 | |
| 437 | type stringVariable struct { |
| 438 | baseVariable |
| 439 | values []string |
| 440 | } |
| 441 | |
| 442 | func (s *stringVariable) variableValuesType() reflect.Type { |
| 443 | var fields []reflect.StructField |
| 444 | |
| 445 | for _, v := range s.values { |
| 446 | fields = append(fields, reflect.StructField{ |
| 447 | Name: proptools.FieldNameForProperty(v), |
| 448 | Type: emptyInterfaceType, |
| 449 | }) |
| 450 | } |
| 451 | |
| 452 | return reflect.StructOf(fields) |
| 453 | } |
| 454 | |
| 455 | func (s *stringVariable) initializeProperties(v reflect.Value, typ reflect.Type) { |
| 456 | for i := range s.values { |
| 457 | v.Field(i).Set(reflect.Zero(typ)) |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | func (s *stringVariable) PropertiesToApply(config SoongConfig, values reflect.Value) interface{} { |
| 462 | for j, v := range s.values { |
| 463 | if config.String(s.variable) == v { |
| 464 | return values.Field(j).Interface() |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | return nil |
| 469 | } |
| 470 | |
| 471 | type boolVariable struct { |
| 472 | baseVariable |
| 473 | } |
| 474 | |
| 475 | func (b boolVariable) variableValuesType() reflect.Type { |
| 476 | return emptyInterfaceType |
| 477 | } |
| 478 | |
| 479 | func (b boolVariable) initializeProperties(v reflect.Value, typ reflect.Type) { |
| 480 | v.Set(reflect.Zero(typ)) |
| 481 | } |
| 482 | |
| 483 | func (b boolVariable) PropertiesToApply(config SoongConfig, values reflect.Value) interface{} { |
| 484 | if config.Bool(b.variable) { |
| 485 | return values.Interface() |
| 486 | } |
| 487 | |
| 488 | return nil |
| 489 | } |
| 490 | |
| 491 | func CanonicalizeToProperty(v string) string { |
| 492 | return strings.Map(func(r rune) rune { |
| 493 | switch { |
| 494 | case r >= 'A' && r <= 'Z', |
| 495 | r >= 'a' && r <= 'z', |
| 496 | r >= '0' && r <= '9', |
| 497 | r == '_': |
| 498 | return r |
| 499 | default: |
| 500 | return '_' |
| 501 | } |
| 502 | }, v) |
| 503 | } |
| 504 | |
| 505 | func CanonicalizeToProperties(values []string) []string { |
| 506 | ret := make([]string, len(values)) |
| 507 | for i, v := range values { |
| 508 | ret[i] = CanonicalizeToProperty(v) |
| 509 | } |
| 510 | return ret |
| 511 | } |
| 512 | |
| 513 | type emptyInterfaceStruct struct { |
| 514 | i interface{} |
| 515 | } |
| 516 | |
| 517 | var emptyInterfaceType = reflect.TypeOf(emptyInterfaceStruct{}).Field(0).Type |