blob: acf5b26da12176902ab78b2476cbe879848b4e8b [file] [log] [blame]
Nan Zhang5323f8e2017-05-10 13:37:54 -07001// Copyright 2017 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 python
16
17import (
Ziwei Zhangc3bb83a2023-03-01 14:42:47 +080018 "fmt"
19
Aditya Choudhary9b593522023-10-06 19:54:58 +000020 "android/soong/testing"
Justin Yun40182b62024-05-07 10:22:19 +090021
Julien Desprez66534a02021-02-09 09:27:39 -080022 "github.com/google/blueprint/proptools"
23
Nan Zhang5323f8e2017-05-10 13:37:54 -070024 "android/soong/android"
yelinhsieh80880a32018-11-06 11:49:55 +080025 "android/soong/tradefed"
Nan Zhang5323f8e2017-05-10 13:37:54 -070026)
27
28// This file contains the module types for building Python test.
29
30func init() {
Paul Duffind0890452021-03-17 21:57:08 +000031 registerPythonTestComponents(android.InitRegistrationContext)
32}
33
34func registerPythonTestComponents(ctx android.RegistrationContext) {
35 ctx.RegisterModuleType("python_test_host", PythonTestHostFactory)
36 ctx.RegisterModuleType("python_test", PythonTestFactory)
Nan Zhang5323f8e2017-05-10 13:37:54 -070037}
38
Cole Faust4d247e62023-01-23 10:14:58 -080039func NewTest(hod android.HostOrDeviceSupported) *PythonTestModule {
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -070040 p := &PythonTestModule{PythonBinaryModule: *NewBinary(hod)}
41 p.sourceProperties = android.SourceProperties{Test_only: proptools.BoolPtr(true), Top_level_test_target: true}
42 return p
Cole Faust4d247e62023-01-23 10:14:58 -080043}
44
45func PythonTestHostFactory() android.Module {
Colin Crosse8c70c52023-05-18 11:51:56 -070046 return NewTest(android.HostSupported).init()
Cole Faust4d247e62023-01-23 10:14:58 -080047}
48
49func PythonTestFactory() android.Module {
50 module := NewTest(android.HostAndDeviceSupported)
51 module.multilib = android.MultilibBoth
52 return module.init()
53}
54
Julien Despreze146e392018-08-02 15:00:46 -070055type TestProperties struct {
56 // the name of the test configuration (for example "AndroidTest.xml") that should be
57 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070058 Test_config *string `android:"path,arch_variant"`
yelinhsieh80880a32018-11-06 11:49:55 +080059
60 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
61 // should be installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070062 Test_config_template *string `android:"path,arch_variant"`
Dan Shi31949122020-09-21 12:11:02 -070063
64 // list of files or filegroup modules that provide data that should be installed alongside
65 // the test
66 Data []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -080067
Cole Faustee45d332024-10-23 11:17:51 -070068 // Same as data, but will add dependencies on modules using the device's os variation and
69 // the common arch variation. Useful for a host test that wants to embed a module built for
70 // device.
71 Device_common_data []string `android:"path_device_common"`
72
Colin Cross1bc63932020-11-22 20:12:45 -080073 // list of java modules that provide data that should be installed alongside the test.
74 Java_data []string
75
Dan Shid79572f2020-11-13 14:33:46 -080076 // Test options.
Ziwei Zhangc3bb83a2023-03-01 14:42:47 +080077 Test_options TestOptions
Zi Wangbbb1b742023-05-08 11:07:26 -070078
79 // list of device binary modules that should be installed alongside the test
80 // This property adds 64bit AND 32bit variants of the dependency
81 Data_device_bins_both []string `android:"arch_variant"`
Ziwei Zhangc3bb83a2023-03-01 14:42:47 +080082}
83
84type TestOptions struct {
85 android.CommonTestOptions
86
87 // Runner for the test. Supports "tradefed" and "mobly" (for multi-device tests). Default is "tradefed".
88 Runner *string
89
90 // Metadata to describe the test configuration.
91 Metadata []Metadata
92}
93
94type Metadata struct {
95 Name string
96 Value string
Julien Despreze146e392018-08-02 15:00:46 -070097}
98
Cole Faust4d247e62023-01-23 10:14:58 -080099type PythonTestModule struct {
100 PythonBinaryModule
Julien Despreze146e392018-08-02 15:00:46 -0700101
102 testProperties TestProperties
Cole Faust4d247e62023-01-23 10:14:58 -0800103 testConfig android.Path
104 data []android.DataPath
Julien Despreze146e392018-08-02 15:00:46 -0700105}
106
Cole Faust4d247e62023-01-23 10:14:58 -0800107func (p *PythonTestModule) init() android.Module {
108 p.AddProperties(&p.properties, &p.protoProperties)
109 p.AddProperties(&p.binaryProperties)
110 p.AddProperties(&p.testProperties)
111 android.InitAndroidArchModule(p, p.hod, p.multilib)
112 android.InitDefaultableModule(p)
Zi Wangbbb1b742023-05-08 11:07:26 -0700113 if p.isTestHost() && p.testProperties.Test_options.Unit_test == nil {
Cole Faust4d247e62023-01-23 10:14:58 -0800114 p.testProperties.Test_options.Unit_test = proptools.BoolPtr(true)
115 }
116 return p
Nan Zhang5323f8e2017-05-10 13:37:54 -0700117}
118
Zi Wangbbb1b742023-05-08 11:07:26 -0700119func (p *PythonTestModule) isTestHost() bool {
120 return p.hod == android.HostSupported
121}
122
123var dataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"}
124
125// python_test_host DepsMutator uses this method to add multilib dependencies of
126// data_device_bin_both
127func (p *PythonTestModule) addDataDeviceBinsDeps(ctx android.BottomUpMutatorContext, filter string) {
128 if len(p.testProperties.Data_device_bins_both) < 1 {
129 return
130 }
131
132 var maybeAndroidTarget *android.Target
133 androidTargetList := android.FirstTarget(ctx.Config().Targets[android.Android], filter)
134 if len(androidTargetList) > 0 {
135 maybeAndroidTarget = &androidTargetList[0]
136 }
137
138 if maybeAndroidTarget != nil {
139 ctx.AddFarVariationDependencies(
140 maybeAndroidTarget.Variations(),
141 dataDeviceBinsTag,
142 p.testProperties.Data_device_bins_both...,
143 )
144 }
145}
146
147func (p *PythonTestModule) DepsMutator(ctx android.BottomUpMutatorContext) {
148 p.PythonBinaryModule.DepsMutator(ctx)
149 if p.isTestHost() {
150 p.addDataDeviceBinsDeps(ctx, "lib32")
151 p.addDataDeviceBinsDeps(ctx, "lib64")
152 }
153}
154
Cole Faust4d247e62023-01-23 10:14:58 -0800155func (p *PythonTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
156 // We inherit from only the library's GenerateAndroidBuildActions, and then
157 // just use buildBinary() so that the binary is not installed into the location
158 // it would be for regular binaries.
159 p.PythonLibraryModule.GenerateAndroidBuildActions(ctx)
160 p.buildBinary(ctx)
161
Ziwei Zhangc3bb83a2023-03-01 14:42:47 +0800162 var configs []tradefed.Option
163 for _, metadata := range p.testProperties.Test_options.Metadata {
164 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: metadata.Name, Value: metadata.Value})
165 }
166
167 runner := proptools.StringDefault(p.testProperties.Test_options.Runner, "tradefed")
Ziwei Zhang3dfec5a2024-02-01 11:05:09 +0800168 template := "${PythonBinaryHostTestConfigTemplate}"
169 if runner == "mobly" {
170 // Add tag to enable Atest mobly runner
171 if !android.InList("mobly", p.testProperties.Test_options.Tags) {
172 p.testProperties.Test_options.Tags = append(p.testProperties.Test_options.Tags, "mobly")
Ziwei Zhangc3bb83a2023-03-01 14:42:47 +0800173 }
Ziwei Zhang3dfec5a2024-02-01 11:05:09 +0800174 template = "${PythonBinaryHostMoblyTestConfigTemplate}"
175 } else if runner != "tradefed" {
Ziwei Zhangc3bb83a2023-03-01 14:42:47 +0800176 panic(fmt.Errorf("unknown python test runner '%s', should be 'tradefed' or 'mobly'", runner))
177 }
Ziwei Zhang3dfec5a2024-02-01 11:05:09 +0800178 p.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
179 TestConfigProp: p.testProperties.Test_config,
180 TestConfigTemplateProp: p.testProperties.Test_config_template,
181 TestSuites: p.binaryProperties.Test_suites,
182 OptionsForAutogenerated: configs,
183 AutoGenConfig: p.binaryProperties.Auto_gen_config,
184 DeviceTemplate: template,
185 HostTemplate: template,
186 })
yelinhsieh80880a32018-11-06 11:49:55 +0800187
Cole Faust4d247e62023-01-23 10:14:58 -0800188 for _, dataSrcPath := range android.PathsForModuleSrc(ctx, p.testProperties.Data) {
189 p.data = append(p.data, android.DataPath{SrcPath: dataSrcPath})
Dan Shi31949122020-09-21 12:11:02 -0700190 }
Cole Faustee45d332024-10-23 11:17:51 -0700191 for _, dataSrcPath := range android.PathsForModuleSrc(ctx, p.testProperties.Device_common_data) {
192 p.data = append(p.data, android.DataPath{SrcPath: dataSrcPath})
193 }
Colin Cross1bc63932020-11-22 20:12:45 -0800194
Zi Wangbbb1b742023-05-08 11:07:26 -0700195 if p.isTestHost() && len(p.testProperties.Data_device_bins_both) > 0 {
196 ctx.VisitDirectDepsWithTag(dataDeviceBinsTag, func(dep android.Module) {
197 p.data = append(p.data, android.DataPath{SrcPath: android.OutputFileForModule(ctx, dep, "")})
198 })
199 }
200
Colin Cross1bc63932020-11-22 20:12:45 -0800201 // Emulate the data property for java_data dependencies.
202 for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) {
203 for _, javaDataSrcPath := range android.OutputFilesForModule(ctx, javaData, "") {
Cole Faust4d247e62023-01-23 10:14:58 -0800204 p.data = append(p.data, android.DataPath{SrcPath: javaDataSrcPath})
Colin Cross1bc63932020-11-22 20:12:45 -0800205 }
206 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800207
208 installDir := installDir(ctx, "nativetest", "nativetest64", ctx.ModuleName())
209 installedData := ctx.InstallTestData(installDir, p.data)
210 p.installedDest = ctx.InstallFile(installDir, p.installSource.Base(), p.installSource, installedData...)
211
Colin Cross40213022023-12-13 15:19:49 -0800212 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Nan Zhang5323f8e2017-05-10 13:37:54 -0700213}
214
Cole Faust4d247e62023-01-23 10:14:58 -0800215func (p *PythonTestModule) AndroidMkEntries() []android.AndroidMkEntries {
216 entriesList := p.PythonBinaryModule.AndroidMkEntries()
217 if len(entriesList) != 1 {
218 panic("Expected 1 entry")
Julien Desprez66534a02021-02-09 09:27:39 -0800219 }
Cole Faust4d247e62023-01-23 10:14:58 -0800220 entries := &entriesList[0]
Nan Zhangd4e641b2017-07-12 12:55:28 -0700221
Cole Faust4d247e62023-01-23 10:14:58 -0800222 entries.Class = "NATIVE_TESTS"
Nan Zhangd4e641b2017-07-12 12:55:28 -0700223
Cole Faust4d247e62023-01-23 10:14:58 -0800224 entries.ExtraEntries = append(entries.ExtraEntries,
225 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
226 //entries.AddCompatibilityTestSuites(p.binaryProperties.Test_suites...)
227 if p.testConfig != nil {
228 entries.SetString("LOCAL_FULL_TEST_CONFIG", p.testConfig.String())
229 }
Nan Zhang5323f8e2017-05-10 13:37:54 -0700230
Ziwei Zhang3dfec5a2024-02-01 11:05:09 +0800231 // ATS 2.0 is the test harness for mobly tests and the test config is for ATS 2.0.
232 // Add "v2" suffix to test config name to distinguish it from the config for TF.
233 if proptools.String(p.testProperties.Test_options.Runner) == "mobly" {
234 entries.SetString("LOCAL_TEST_CONFIG_SUFFIX", "v2")
235 }
236
Cole Faust4d247e62023-01-23 10:14:58 -0800237 entries.SetBoolIfTrue("LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG", !BoolDefault(p.binaryProperties.Auto_gen_config, true))
Nan Zhang5323f8e2017-05-10 13:37:54 -0700238
Cole Faust4d247e62023-01-23 10:14:58 -0800239 p.testProperties.Test_options.SetAndroidMkEntries(entries)
240 })
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000241
Cole Faust4d247e62023-01-23 10:14:58 -0800242 return entriesList
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000243}