blob: d3109d934c4eaa8515ed65104028f03c6cee09e6 [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 {
42 Test_config *bazel.Label
43
44 Auto_generate_test_config *bool
45 Template_test_config *bazel.Label
46 Template_configs []string
47 Template_install_base *string
48}
49
50func GetTestConfigAttributes(
51 ctx android.TopDownMutatorContext,
52 testConfig *string,
53 extraTestConfigs []string,
54 autoGenConfig *bool,
55 testSuites []string,
56 template *string,
57 templateConfigs []Config,
58 templateInstallBase *string) TestConfigAttributes {
59
60 attrs := TestConfigAttributes{}
61 attrs.Test_config = GetTestConfig(ctx, testConfig)
62 // do not generate a test config if
63 // 1) test config already found
64 // 2) autoGenConfig == false
65 // 3) CTS tests and no template specified.
66 // CTS Modules can be used for test data, so test config files must be explicitly specified.
67 if (attrs.Template_test_config != nil) ||
68 proptools.Bool(autoGenConfig) == false ||
69 (template == nil && !android.InList("cts", testSuites)) {
70
71 return attrs
72 }
73
74 // Add properties for the bazel rule to generate a test config
75 // since a test config was not specified.
76 templateLabel := android.BazelLabelForModuleSrcSingle(ctx, *template)
77 attrs.Template_test_config = &templateLabel
78 attrs.Auto_generate_test_config = autoGenConfig
79 var configStrings []string
80 for _, c := range templateConfigs {
81 configString := proptools.NinjaAndShellEscape(c.Config())
82 configStrings = append(configStrings, configString)
83 }
84 attrs.Template_configs = configStrings
85 attrs.Template_install_base = templateInstallBase
86 return attrs
87}
88
89func GetTestConfig(
90 ctx android.TopDownMutatorContext,
91 testConfig *string,
92) *bazel.Label {
93
94 if testConfig != nil {
95 c, _ := android.BazelStringOrLabelFromProp(ctx, testConfig)
96 if c.Value != nil {
97 return c.Value
98 }
99 }
100
101 // check for default AndroidTest.xml
102 defaultTestConfigPath := ctx.ModuleDir() + "/AndroidTest.xml"
103 c, _ := android.BazelStringOrLabelFromProp(ctx, &defaultTestConfigPath)
104 return c.Value
105}