autogen_bazel.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2022 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 tradefed
  15. import (
  16. "android/soong/android"
  17. "android/soong/bazel"
  18. "github.com/google/blueprint/proptools"
  19. )
  20. const (
  21. InstrumentationTestConfigTemplate = "build/make/core/instrumentation_test_config_template.xml"
  22. JavaTestConfigTemplate = "build/make/core/java_test_config_template.xml"
  23. JavaHostTestConfigTemplate = "build/make/core/java_host_test_config_template.xml"
  24. JavaHostUnitTestConfigTemplate = "build/make/core/java_host_unit_test_config_template.xml"
  25. NativeBenchmarkTestConfigTemplate = "build/make/core/native_benchmark_test_config_template.xml"
  26. NativeHostTestConfigTemplate = "build/make/core/native_host_test_config_template.xml"
  27. NativeTestConfigTemplate = "build/make/core/native_test_config_template.xml"
  28. PythonBinaryHostTestConfigTemplate = "build/make/core/python_binary_host_test_config_template.xml"
  29. RustDeviceTestConfigTemplate = "build/make/core/rust_device_test_config_template.xml"
  30. RustHostTestConfigTemplate = "build/make/core/rust_host_test_config_template.xml"
  31. RustDeviceBenchmarkConfigTemplate = "build/make/core/rust_device_benchmark_config_template.xml"
  32. RustHostBenchmarkConfigTemplate = "build/make/core/rust_host_benchmark_config_template.xml"
  33. RobolectricTestConfigTemplate = "build/make/core/robolectric_test_config_template.xml"
  34. ShellTestConfigTemplate = "build/make/core/shell_test_config_template.xml"
  35. )
  36. type TestConfigAttributes struct {
  37. Test_config *bazel.Label
  38. Auto_generate_test_config *bool
  39. Template_test_config *bazel.Label
  40. Template_configs []string
  41. Template_install_base *string
  42. }
  43. func GetTestConfigAttributes(
  44. ctx android.TopDownMutatorContext,
  45. testConfig *string,
  46. extraTestConfigs []string,
  47. autoGenConfig *bool,
  48. testSuites []string,
  49. template *string,
  50. templateConfigs []Config,
  51. templateInstallBase *string) TestConfigAttributes {
  52. attrs := TestConfigAttributes{}
  53. attrs.Test_config = GetTestConfig(ctx, testConfig)
  54. // do not generate a test config if
  55. // 1) test config already found
  56. // 2) autoGenConfig == false
  57. // 3) CTS tests and no template specified.
  58. // CTS Modules can be used for test data, so test config files must be explicitly specified.
  59. if (attrs.Template_test_config != nil) ||
  60. proptools.Bool(autoGenConfig) == false ||
  61. (template == nil && !android.InList("cts", testSuites)) {
  62. return attrs
  63. }
  64. // Add properties for the bazel rule to generate a test config
  65. // since a test config was not specified.
  66. templateLabel := android.BazelLabelForModuleSrcSingle(ctx, *template)
  67. attrs.Template_test_config = &templateLabel
  68. attrs.Auto_generate_test_config = autoGenConfig
  69. var configStrings []string
  70. for _, c := range templateConfigs {
  71. configString := proptools.NinjaAndShellEscape(c.Config())
  72. configStrings = append(configStrings, configString)
  73. }
  74. attrs.Template_configs = configStrings
  75. attrs.Template_install_base = templateInstallBase
  76. return attrs
  77. }
  78. func GetTestConfig(
  79. ctx android.TopDownMutatorContext,
  80. testConfig *string,
  81. ) *bazel.Label {
  82. if testConfig != nil {
  83. c, _ := android.BazelStringOrLabelFromProp(ctx, testConfig)
  84. if c.Value != nil {
  85. return c.Value
  86. }
  87. }
  88. // check for default AndroidTest.xml
  89. defaultTestConfigPath := ctx.ModuleDir() + "/AndroidTest.xml"
  90. c, _ := android.BazelStringOrLabelFromProp(ctx, &defaultTestConfigPath)
  91. return c.Value
  92. }