android_products.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. "path/filepath"
  21. "strings"
  22. mkparser "android/soong/androidmk/parser"
  23. )
  24. // Implements mkparser.Scope, to be used by mkparser.Value.Value()
  25. type localDirEval struct {
  26. localDir string
  27. hasErrors bool
  28. }
  29. func (l *localDirEval) Get(name string) string {
  30. if name == "LOCAL_DIR" {
  31. return l.localDir
  32. }
  33. l.hasErrors = true
  34. return fmt.Sprintf("$(%s)", name)
  35. }
  36. func (l *localDirEval) Set(_, _ string) {
  37. }
  38. func (l *localDirEval) Call(_ string, _ []string) []string {
  39. l.hasErrors = true
  40. return []string{"$(call ...)"}
  41. }
  42. func (l *localDirEval) SetFunc(_ string, _ func([]string) []string) {
  43. }
  44. // UpdateProductConfigMap builds product configuration map.
  45. // The product configuration map maps a product name (i.e., the value of the
  46. // TARGET_PRODUCT variable) to the top-level configuration file.
  47. // In the Android's Make-based build machinery, the equivalent of the
  48. // product configuration map is $(PRODUCT_MAKEFILES), which is the list
  49. // of <product>:<configuration makefile> pairs (if <product>: is missing,
  50. // <product> is the basename of the configuration makefile).
  51. // UpdateProductConfigMap emulates this build logic by processing the
  52. // assignments to PRODUCT_MAKEFILES in the file passed to it.
  53. func UpdateProductConfigMap(configMap map[string]string, configMakefile string) error {
  54. contents, err := ioutil.ReadFile(configMakefile)
  55. if err != nil {
  56. return err
  57. }
  58. parser := mkparser.NewParser(configMakefile, bytes.NewBuffer(contents))
  59. nodes, errs := parser.Parse()
  60. if len(errs) > 0 {
  61. for _, e := range errs {
  62. fmt.Fprintln(os.Stderr, "ERROR:", e)
  63. }
  64. return fmt.Errorf("cannot parse %s", configMakefile)
  65. }
  66. ldEval := &localDirEval{localDir: filepath.Dir(configMakefile)}
  67. for _, node := range nodes {
  68. // We are interested in assignments to 'PRODUCT_MAKEFILES'
  69. asgn, ok := node.(*mkparser.Assignment)
  70. if !ok {
  71. continue
  72. }
  73. if !(asgn.Name.Const() && asgn.Name.Strings[0] == "PRODUCT_MAKEFILES") {
  74. continue
  75. }
  76. // Resolve the references to $(LOCAL_DIR) in $(PRODUCT_MAKEFILES).
  77. ldEval.hasErrors = false
  78. value := asgn.Value.Value(ldEval)
  79. if ldEval.hasErrors {
  80. return fmt.Errorf("cannot evaluate %s", asgn.Value.Dump())
  81. }
  82. // Each item is either <product>:<configuration makefile>, or
  83. // just <configuration makefile>
  84. for _, token := range strings.Fields(value) {
  85. var product, config_path string
  86. if n := strings.Index(token, ":"); n >= 0 {
  87. product = token[0:n]
  88. config_path = token[n+1:]
  89. } else {
  90. config_path = token
  91. product = filepath.Base(config_path)
  92. product = strings.TrimSuffix(product, filepath.Ext(product))
  93. }
  94. configMap[product] = config_path
  95. }
  96. }
  97. return nil
  98. }