soong_variables.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. // TODO: We run into several issues later on if we type this as a bool:
  64. // - We still assign bool-typed variables to strings
  65. // - When emitting the final results as make code, some bool's false values have to
  66. // be an empty string, and some have to be false in order to match the make variables.
  67. valueType = starlarkTypeString
  68. case "csv":
  69. // Only PLATFORM_VERSION_ALL_CODENAMES, and it's a list
  70. valueType = starlarkTypeList
  71. case "list":
  72. valueType = starlarkTypeList
  73. case "str":
  74. valueType = starlarkTypeString
  75. case "val":
  76. // Only PLATFORM_SDK_VERSION uses this, and it's integer
  77. valueType = starlarkTypeInt
  78. default:
  79. panic(fmt.Errorf("unknown Soong variable type %s", typeString))
  80. }
  81. ctx.registrar.NewVariable(name, VarClassSoong, valueType)
  82. }
  83. func (ctx context) handleInclude(t *mkparser.Directive) {
  84. if t.Name != "include" && t.Name != "-include" {
  85. return
  86. }
  87. includedPath := t.Args.Value(ctx.includeFileScope)
  88. err := ctx.doFind(includedPath)
  89. if err != nil && t.Name == "include" {
  90. fmt.Fprintf(os.Stderr, "cannot include %s: %s", includedPath, err)
  91. }
  92. }
  93. var callFuncRex = regexp.MustCompile("^call +add_json_(str|val|bool|csv|list) *,")
  94. func (ctx context) handleVariable(t *mkparser.Variable) {
  95. // From the variable reference looking as follows:
  96. // $(call json_add_TYPE,arg1,$(VAR))
  97. // we infer that the type of $(VAR) is TYPE
  98. // VAR can be a simple variable name, or another call
  99. // (e.g., $(call invert_bool, $(X)), from which we can infer
  100. // that the type of X is bool
  101. if prefix, v, ok := prefixedVariable(t.Name); ok && strings.HasPrefix(prefix, "call add_json") {
  102. if match := callFuncRex.FindStringSubmatch(prefix); match != nil {
  103. ctx.inferSoongVariableType(match[1], v)
  104. // NOTE(asmundak): sometimes arg1 (the name of the Soong variable defined
  105. // in this statement) may indicate that there is a Make counterpart. E.g, from
  106. // $(call add_json_bool, DisablePreopt, $(call invert_bool,$(ENABLE_PREOPT)))
  107. // it may be inferred that there is a Make boolean variable DISABLE_PREOPT.
  108. // Unfortunately, Soong variable names have no 1:1 correspondence to Make variables,
  109. // for instance,
  110. // $(call add_json_list, PatternsOnSystemOther, $(SYSTEM_OTHER_ODEX_FILTER))
  111. // does not mean that there is PATTERNS_ON_SYSTEM_OTHER
  112. // Our main interest lies in finding the variables whose values are lists, and
  113. // so far there are none that can be found this way, so it is not important.
  114. } else {
  115. panic(fmt.Errorf("cannot match the call: %s", prefix))
  116. }
  117. }
  118. }
  119. var (
  120. callInvertBoolRex = regexp.MustCompile("^call +invert_bool *, *$")
  121. callFilterBoolRex = regexp.MustCompile("^(filter|filter-out) +(true|false), *$")
  122. )
  123. func (ctx context) inferSoongVariableType(vType string, n *mkparser.MakeString) {
  124. if n.Const() {
  125. ctx.NewSoongVariable(n.Strings[0], vType)
  126. return
  127. }
  128. if prefix, v, ok := prefixedVariable(n); ok {
  129. if callInvertBoolRex.MatchString(prefix) || callFilterBoolRex.MatchString(prefix) {
  130. // It is $(call invert_bool, $(VAR)) or $(filter[-out] [false|true],$(VAR))
  131. ctx.inferSoongVariableType("bool", v)
  132. }
  133. }
  134. }
  135. // If MakeString is foo$(BAR), returns 'foo', BAR(as *MakeString) and true
  136. func prefixedVariable(s *mkparser.MakeString) (string, *mkparser.MakeString, bool) {
  137. if len(s.Strings) != 2 || s.Strings[1] != "" {
  138. return "", nil, false
  139. }
  140. return s.Strings[0], s.Variables[0].Name, true
  141. }