blob: cc16176c8d095bc759f6847efe7bd86693bf1777 [file] [log] [blame]
Kevin Dagostino32edd1a2022-12-04 11:16:42 +00001// 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
15package tradefed
16
17import (
18 "android/soong/android"
19 "android/soong/bazel"
20
21 "github.com/google/blueprint/proptools"
22)
23
24const (
25 InstrumentationTestConfigTemplate = "build/make/core/instrumentation_test_config_template.xml"
26 JavaTestConfigTemplate = "build/make/core/java_test_config_template.xml"
27 JavaHostTestConfigTemplate = "build/make/core/java_host_test_config_template.xml"
28 JavaHostUnitTestConfigTemplate = "build/make/core/java_host_unit_test_config_template.xml"
29 NativeBenchmarkTestConfigTemplate = "build/make/core/native_benchmark_test_config_template.xml"
30 NativeHostTestConfigTemplate = "build/make/core/native_host_test_config_template.xml"
31 NativeTestConfigTemplate = "build/make/core/native_test_config_template.xml"
32 PythonBinaryHostTestConfigTemplate = "build/make/core/python_binary_host_test_config_template.xml"
33 RustDeviceTestConfigTemplate = "build/make/core/rust_device_test_config_template.xml"
34 RustHostTestConfigTemplate = "build/make/core/rust_host_test_config_template.xml"
35 RustDeviceBenchmarkConfigTemplate = "build/make/core/rust_device_benchmark_config_template.xml"
36 RustHostBenchmarkConfigTemplate = "build/make/core/rust_host_benchmark_config_template.xml"
37 RobolectricTestConfigTemplate = "build/make/core/robolectric_test_config_template.xml"
38 ShellTestConfigTemplate = "build/make/core/shell_test_config_template.xml"
39)
40
41type TestConfigAttributes struct {
Yu Liud136a6a2023-08-21 21:23:04 +000042 Test_config *bazel.Label
43 Dynamic_config *bazel.Label
Kevin Dagostino32edd1a2022-12-04 11:16:42 +000044
45 Auto_generate_test_config *bool
46 Template_test_config *bazel.Label
47 Template_configs []string
48 Template_install_base *string
49}
50
51func GetTestConfigAttributes(
Chris Parsons637458d2023-09-19 20:09:00 +000052 ctx android.Bp2buildMutatorContext,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +000053 testConfig *string,
54 extraTestConfigs []string,
55 autoGenConfig *bool,
56 testSuites []string,
57 template *string,
58 templateConfigs []Config,
59 templateInstallBase *string) TestConfigAttributes {
60
61 attrs := TestConfigAttributes{}
Yu Liud136a6a2023-08-21 21:23:04 +000062
63 dynamicConfig := "DynamicConfig.xml"
64 c, _ := android.BazelStringOrLabelFromProp(ctx, &dynamicConfig)
65 attrs.Dynamic_config = c.Value
66
Kevin Dagostino32edd1a2022-12-04 11:16:42 +000067 attrs.Test_config = GetTestConfig(ctx, testConfig)
68 // do not generate a test config if
69 // 1) test config already found
70 // 2) autoGenConfig == false
71 // 3) CTS tests and no template specified.
72 // CTS Modules can be used for test data, so test config files must be explicitly specified.
73 if (attrs.Template_test_config != nil) ||
74 proptools.Bool(autoGenConfig) == false ||
75 (template == nil && !android.InList("cts", testSuites)) {
76
77 return attrs
78 }
79
80 // Add properties for the bazel rule to generate a test config
81 // since a test config was not specified.
82 templateLabel := android.BazelLabelForModuleSrcSingle(ctx, *template)
83 attrs.Template_test_config = &templateLabel
84 attrs.Auto_generate_test_config = autoGenConfig
85 var configStrings []string
86 for _, c := range templateConfigs {
87 configString := proptools.NinjaAndShellEscape(c.Config())
88 configStrings = append(configStrings, configString)
89 }
90 attrs.Template_configs = configStrings
91 attrs.Template_install_base = templateInstallBase
92 return attrs
93}
94
95func GetTestConfig(
Chris Parsons637458d2023-09-19 20:09:00 +000096 ctx android.Bp2buildMutatorContext,
Kevin Dagostino32edd1a2022-12-04 11:16:42 +000097 testConfig *string,
98) *bazel.Label {
99
100 if testConfig != nil {
101 c, _ := android.BazelStringOrLabelFromProp(ctx, testConfig)
102 if c.Value != nil {
103 return c.Value
104 }
105 }
106
107 // check for default AndroidTest.xml
Yu Liu574e3532023-08-19 00:38:15 +0000108 defaultTestConfigPath := "AndroidTest.xml"
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000109 c, _ := android.BazelStringOrLabelFromProp(ctx, &defaultTestConfigPath)
110 return c.Value
111}