soong_variables.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. "regexp"
  21. "strings"
  22. mkparser "android/soong/androidmk/parser"
  23. )
  24. type context struct {
  25. includeFileScope mkparser.Scope
  26. registrar variableRegistrar
  27. }
  28. // Scans the makefile Soong uses to generate soong.variables file,
  29. // collecting variable names and types from the lines that look like this:
  30. //
  31. // $(call add_json_XXX, <...>, $(VAR))
  32. func FindSoongVariables(mkFile string, includeFileScope mkparser.Scope, registrar variableRegistrar) error {
  33. ctx := context{includeFileScope, registrar}
  34. return ctx.doFind(mkFile)
  35. }
  36. func (ctx *context) doFind(mkFile string) error {
  37. mkContents, err := ioutil.ReadFile(mkFile)
  38. if err != nil {
  39. return err
  40. }
  41. parser := mkparser.NewParser(mkFile, bytes.NewBuffer(mkContents))
  42. nodes, errs := parser.Parse()
  43. if len(errs) > 0 {
  44. for _, e := range errs {
  45. fmt.Fprintln(os.Stderr, "ERROR:", e)
  46. }
  47. return fmt.Errorf("cannot parse %s", mkFile)
  48. }
  49. for _, node := range nodes {
  50. switch t := node.(type) {
  51. case *mkparser.Variable:
  52. ctx.handleVariable(t)
  53. case *mkparser.Directive:
  54. ctx.handleInclude(t)
  55. }
  56. }
  57. return nil
  58. }
  59. func (ctx context) NewSoongVariable(name, typeString string) {
  60. var valueType starlarkType
  61. switch typeString {
  62. case "bool":
  63. valueType = starlarkTypeBool
  64. case "csv":
  65. // Only PLATFORM_VERSION_ALL_CODENAMES, and it's a list
  66. valueType = starlarkTypeList
  67. case "list":
  68. valueType = starlarkTypeList
  69. case "str":
  70. valueType = starlarkTypeString
  71. case "val":
  72. // Only PLATFORM_SDK_VERSION uses this, and it's integer
  73. valueType = starlarkTypeInt
  74. default:
  75. panic(fmt.Errorf("unknown Soong variable type %s", typeString))
  76. }
  77. ctx.registrar.NewVariable(name, VarClassSoong, valueType)
  78. }
  79. func (ctx context) handleInclude(t *mkparser.Directive) {
  80. if t.Name != "include" && t.Name != "-include" {
  81. return
  82. }
  83. includedPath := t.Args.Value(ctx.includeFileScope)
  84. err := ctx.doFind(includedPath)
  85. if err != nil && t.Name == "include" {
  86. fmt.Fprintf(os.Stderr, "cannot include %s: %s", includedPath, err)
  87. }
  88. }
  89. var callFuncRex = regexp.MustCompile("^call +add_json_(str|val|bool|csv|list) *,")
  90. func (ctx context) handleVariable(t *mkparser.Variable) {
  91. // From the variable reference looking as follows:
  92. // $(call json_add_TYPE,arg1,$(VAR))
  93. // we infer that the type of $(VAR) is TYPE
  94. // VAR can be a simple variable name, or another call
  95. // (e.g., $(call invert_bool, $(X)), from which we can infer
  96. // that the type of X is bool
  97. if prefix, v, ok := prefixedVariable(t.Name); ok && strings.HasPrefix(prefix, "call add_json") {
  98. if match := callFuncRex.FindStringSubmatch(prefix); match != nil {
  99. ctx.inferSoongVariableType(match[1], v)
  100. // NOTE(asmundak): sometimes arg1 (the name of the Soong variable defined
  101. // in this statement) may indicate that there is a Make counterpart. E.g, from
  102. // $(call add_json_bool, DisablePreopt, $(call invert_bool,$(ENABLE_PREOPT)))
  103. // it may be inferred that there is a Make boolean variable DISABLE_PREOPT.
  104. // Unfortunately, Soong variable names have no 1:1 correspondence to Make variables,
  105. // for instance,
  106. // $(call add_json_list, PatternsOnSystemOther, $(SYSTEM_OTHER_ODEX_FILTER))
  107. // does not mean that there is PATTERNS_ON_SYSTEM_OTHER
  108. // Our main interest lies in finding the variables whose values are lists, and
  109. // so far there are none that can be found this way, so it is not important.
  110. } else {
  111. panic(fmt.Errorf("cannot match the call: %s", prefix))
  112. }
  113. }
  114. }
  115. var (
  116. callInvertBoolRex = regexp.MustCompile("^call +invert_bool *, *$")
  117. callFilterBoolRex = regexp.MustCompile("^(filter|filter-out) +(true|false), *$")
  118. )
  119. func (ctx context) inferSoongVariableType(vType string, n *mkparser.MakeString) {
  120. if n.Const() {
  121. ctx.NewSoongVariable(n.Strings[0], vType)
  122. return
  123. }
  124. if prefix, v, ok := prefixedVariable(n); ok {
  125. if callInvertBoolRex.MatchString(prefix) || callFilterBoolRex.MatchString(prefix) {
  126. // It is $(call invert_bool, $(VAR)) or $(filter[-out] [false|true],$(VAR))
  127. ctx.inferSoongVariableType("bool", v)
  128. }
  129. }
  130. }
  131. // If MakeString is foo$(BAR), returns 'foo', BAR(as *MakeString) and true
  132. func prefixedVariable(s *mkparser.MakeString) (string, *mkparser.MakeString, bool) {
  133. if len(s.Strings) != 2 || s.Strings[1] != "" {
  134. return "", nil, false
  135. }
  136. return s.Strings[0], s.Variables[0].Name, true
  137. }