config_variables.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2021 Google LLC
  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 mk2rbc
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "strings"
  21. mkparser "android/soong/androidmk/parser"
  22. )
  23. // Extracts the list of product config variables from a file, calling
  24. // given registrar for each variable.
  25. func FindConfigVariables(mkFile string, vr variableRegistrar) error {
  26. mkContents, err := ioutil.ReadFile(mkFile)
  27. if err != nil {
  28. return err
  29. }
  30. parser := mkparser.NewParser(mkFile, bytes.NewBuffer(mkContents))
  31. nodes, errs := parser.Parse()
  32. if len(errs) > 0 {
  33. for _, e := range errs {
  34. fmt.Fprintln(os.Stderr, "ERROR:", e)
  35. }
  36. return fmt.Errorf("cannot parse %s", mkFile)
  37. }
  38. for _, node := range nodes {
  39. asgn, ok := node.(*mkparser.Assignment)
  40. if !ok {
  41. continue
  42. }
  43. // We are looking for a variable called '_product_list_vars'
  44. // or '_product_single_value_vars'.
  45. if !asgn.Name.Const() {
  46. continue
  47. }
  48. varName := asgn.Name.Strings[0]
  49. var starType starlarkType
  50. if varName == "_product_list_vars" {
  51. starType = starlarkTypeList
  52. } else if varName == "_product_single_value_vars" {
  53. starType = starlarkTypeUnknown
  54. } else {
  55. continue
  56. }
  57. for _, name := range strings.Fields(asgn.Value.Dump()) {
  58. vr.NewVariable(name, VarClassConfig, starType)
  59. }
  60. }
  61. return nil
  62. }