soong_config_modules_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2019 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. package android
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. type soongConfigTestModule struct {
  20. ModuleBase
  21. props soongConfigTestModuleProperties
  22. }
  23. type soongConfigTestModuleProperties struct {
  24. Cflags []string
  25. }
  26. func soongConfigTestModuleFactory() Module {
  27. m := &soongConfigTestModule{}
  28. m.AddProperties(&m.props)
  29. InitAndroidModule(m)
  30. return m
  31. }
  32. func (t soongConfigTestModule) GenerateAndroidBuildActions(ModuleContext) {}
  33. func TestSoongConfigModule(t *testing.T) {
  34. configBp := `
  35. soong_config_module_type {
  36. name: "acme_test_defaults",
  37. module_type: "test_defaults",
  38. config_namespace: "acme",
  39. variables: ["board", "feature1", "FEATURE3"],
  40. bool_variables: ["feature2"],
  41. value_variables: ["size"],
  42. properties: ["cflags", "srcs"],
  43. }
  44. soong_config_string_variable {
  45. name: "board",
  46. values: ["soc_a", "soc_b"],
  47. }
  48. soong_config_bool_variable {
  49. name: "feature1",
  50. }
  51. soong_config_bool_variable {
  52. name: "FEATURE3",
  53. }
  54. `
  55. importBp := `
  56. soong_config_module_type_import {
  57. from: "SoongConfig.bp",
  58. module_types: ["acme_test_defaults"],
  59. }
  60. `
  61. bp := `
  62. acme_test_defaults {
  63. name: "foo",
  64. cflags: ["-DGENERIC"],
  65. soong_config_variables: {
  66. board: {
  67. soc_a: {
  68. cflags: ["-DSOC_A"],
  69. },
  70. soc_b: {
  71. cflags: ["-DSOC_B"],
  72. },
  73. },
  74. size: {
  75. cflags: ["-DSIZE=%s"],
  76. },
  77. feature1: {
  78. cflags: ["-DFEATURE1"],
  79. },
  80. feature2: {
  81. cflags: ["-DFEATURE2"],
  82. },
  83. FEATURE3: {
  84. cflags: ["-DFEATURE3"],
  85. },
  86. },
  87. }
  88. `
  89. run := func(t *testing.T, bp string, fs map[string][]byte) {
  90. config := TestConfig(buildDir, nil, bp, fs)
  91. config.TestProductVariables.VendorVars = map[string]map[string]string{
  92. "acme": map[string]string{
  93. "board": "soc_a",
  94. "size": "42",
  95. "feature1": "true",
  96. "feature2": "false",
  97. // FEATURE3 unset
  98. },
  99. }
  100. ctx := NewTestContext()
  101. ctx.RegisterModuleType("soong_config_module_type_import", soongConfigModuleTypeImportFactory)
  102. ctx.RegisterModuleType("soong_config_module_type", soongConfigModuleTypeFactory)
  103. ctx.RegisterModuleType("soong_config_string_variable", soongConfigStringVariableDummyFactory)
  104. ctx.RegisterModuleType("soong_config_bool_variable", soongConfigBoolVariableDummyFactory)
  105. ctx.RegisterModuleType("test_defaults", soongConfigTestModuleFactory)
  106. ctx.Register(config)
  107. _, errs := ctx.ParseBlueprintsFiles("Android.bp")
  108. FailIfErrored(t, errs)
  109. _, errs = ctx.PrepareBuildActions(config)
  110. FailIfErrored(t, errs)
  111. foo := ctx.ModuleForTests("foo", "").Module().(*soongConfigTestModule)
  112. if g, w := foo.props.Cflags, []string{"-DGENERIC", "-DSIZE=42", "-DSOC_A", "-DFEATURE1"}; !reflect.DeepEqual(g, w) {
  113. t.Errorf("wanted foo cflags %q, got %q", w, g)
  114. }
  115. }
  116. t.Run("single file", func(t *testing.T) {
  117. run(t, configBp+bp, nil)
  118. })
  119. t.Run("import", func(t *testing.T) {
  120. run(t, importBp+bp, map[string][]byte{
  121. "SoongConfig.bp": []byte(configBp),
  122. })
  123. })
  124. }