blob: 2b939e7e4d0df62d286df0055ad0b7811398e958 [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"
Julien Desprez66534a02021-02-09 09:27:39 -080021 "github.com/google/blueprint/proptools"
22
Nan Zhang5323f8e2017-05-10 13:37:54 -070023 "android/soong/android"
yelinhsieh80880a32018-11-06 11:49:55 +080024 "android/soong/tradefed"
Nan Zhang5323f8e2017-05-10 13:37:54 -070025)
26
27// This file contains the module types for building Python test.
28
29func init() {
Paul Duffind0890452021-03-17 21:57:08 +000030 registerPythonTestComponents(android.InitRegistrationContext)
31}
32
33func registerPythonTestComponents(ctx android.RegistrationContext) {
34 ctx.RegisterModuleType("python_test_host", PythonTestHostFactory)
35 ctx.RegisterModuleType("python_test", PythonTestFactory)
Nan Zhang5323f8e2017-05-10 13:37:54 -070036}
37
Cole Faust4d247e62023-01-23 10:14:58 -080038func NewTest(hod android.HostOrDeviceSupported) *PythonTestModule {
Ronald Braunsteinc4cd7a12024-04-16 16:39:48 -070039 p := &PythonTestModule{PythonBinaryModule: *NewBinary(hod)}
40 p.sourceProperties = android.SourceProperties{Test_only: proptools.BoolPtr(true), Top_level_test_target: true}
41 return p
Cole Faust4d247e62023-01-23 10:14:58 -080042}
43
44func PythonTestHostFactory() android.Module {
Colin Crosse8c70c52023-05-18 11:51:56 -070045 return NewTest(android.HostSupported).init()
Cole Faust4d247e62023-01-23 10:14:58 -080046}
47
48func PythonTestFactory() android.Module {
49 module := NewTest(android.HostAndDeviceSupported)
50 module.multilib = android.MultilibBoth
51 return module.init()
52}
53
Julien Despreze146e392018-08-02 15:00:46 -070054type TestProperties struct {
55 // the name of the test configuration (for example "AndroidTest.xml") that should be
56 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070057 Test_config *string `android:"path,arch_variant"`
yelinhsieh80880a32018-11-06 11:49:55 +080058
59 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
60 // should be installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070061 Test_config_template *string `android:"path,arch_variant"`
Dan Shi31949122020-09-21 12:11:02 -070062
63 // list of files or filegroup modules that provide data that should be installed alongside
64 // the test
65 Data []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -080066
Colin Cross1bc63932020-11-22 20:12:45 -080067 // list of java modules that provide data that should be installed alongside the test.
68 Java_data []string
69
Dan Shid79572f2020-11-13 14:33:46 -080070 // Test options.
Ziwei Zhangc3bb83a2023-03-01 14:42:47 +080071 Test_options TestOptions
Zi Wangbbb1b742023-05-08 11:07:26 -070072
73 // list of device binary modules that should be installed alongside the test
74 // This property adds 64bit AND 32bit variants of the dependency
75 Data_device_bins_both []string `android:"arch_variant"`
Ziwei Zhangc3bb83a2023-03-01 14:42:47 +080076}
77
78type TestOptions struct {
79 android.CommonTestOptions
80
81 // Runner for the test. Supports "tradefed" and "mobly" (for multi-device tests). Default is "tradefed".
82 Runner *string
83
84 // Metadata to describe the test configuration.
85 Metadata []Metadata
86}
87
88type Metadata struct {
89 Name string
90 Value string
Julien Despreze146e392018-08-02 15:00:46 -070091}
92
Cole Faust4d247e62023-01-23 10:14:58 -080093type PythonTestModule struct {
94 PythonBinaryModule
Julien Despreze146e392018-08-02 15:00:46 -070095
96 testProperties TestProperties
Cole Faust4d247e62023-01-23 10:14:58 -080097 testConfig android.Path
98 data []android.DataPath
Julien Despreze146e392018-08-02 15:00:46 -070099}
100
Cole Faust4d247e62023-01-23 10:14:58 -0800101func (p *PythonTestModule) init() android.Module {
102 p.AddProperties(&p.properties, &p.protoProperties)
103 p.AddProperties(&p.binaryProperties)
104 p.AddProperties(&p.testProperties)
105 android.InitAndroidArchModule(p, p.hod, p.multilib)
106 android.InitDefaultableModule(p)
Zi Wangbbb1b742023-05-08 11:07:26 -0700107 if p.isTestHost() && p.testProperties.Test_options.Unit_test == nil {
Cole Faust4d247e62023-01-23 10:14:58 -0800108 p.testProperties.Test_options.Unit_test = proptools.BoolPtr(true)
109 }
110 return p
Nan Zhang5323f8e2017-05-10 13:37:54 -0700111}
112
Zi Wangbbb1b742023-05-08 11:07:26 -0700113func (p *PythonTestModule) isTestHost() bool {
114 return p.hod == android.HostSupported
115}
116
117var dataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"}
118
119// python_test_host DepsMutator uses this method to add multilib dependencies of
120// data_device_bin_both
121func (p *PythonTestModule) addDataDeviceBinsDeps(ctx android.BottomUpMutatorContext, filter string) {
122 if len(p.testProperties.Data_device_bins_both) < 1 {
123 return
124 }
125
126 var maybeAndroidTarget *android.Target
127 androidTargetList := android.FirstTarget(ctx.Config().Targets[android.Android], filter)
128 if len(androidTargetList) > 0 {
129 maybeAndroidTarget = &androidTargetList[0]
130 }
131
132 if maybeAndroidTarget != nil {
133 ctx.AddFarVariationDependencies(
134 maybeAndroidTarget.Variations(),
135 dataDeviceBinsTag,
136 p.testProperties.Data_device_bins_both...,
137 )
138 }
139}
140
141func (p *PythonTestModule) DepsMutator(ctx android.BottomUpMutatorContext) {
142 p.PythonBinaryModule.DepsMutator(ctx)
143 if p.isTestHost() {
144 p.addDataDeviceBinsDeps(ctx, "lib32")
145 p.addDataDeviceBinsDeps(ctx, "lib64")
146 }
147}
148
Cole Faust4d247e62023-01-23 10:14:58 -0800149func (p *PythonTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
150 // We inherit from only the library's GenerateAndroidBuildActions, and then
151 // just use buildBinary() so that the binary is not installed into the location
152 // it would be for regular binaries.
153 p.PythonLibraryModule.GenerateAndroidBuildActions(ctx)
LaMont Jonesafe7baf2024-01-09 22:47:39 +0000154 android.CollectDependencyAconfigFiles(ctx, &p.mergedAconfigFiles)
Cole Faust4d247e62023-01-23 10:14:58 -0800155 p.buildBinary(ctx)
156
Ziwei Zhangc3bb83a2023-03-01 14:42:47 +0800157 var configs []tradefed.Option
158 for _, metadata := range p.testProperties.Test_options.Metadata {
159 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: metadata.Name, Value: metadata.Value})
160 }
161
162 runner := proptools.StringDefault(p.testProperties.Test_options.Runner, "tradefed")
Ziwei Zhang3dfec5a2024-02-01 11:05:09 +0800163 template := "${PythonBinaryHostTestConfigTemplate}"
164 if runner == "mobly" {
165 // Add tag to enable Atest mobly runner
166 if !android.InList("mobly", p.testProperties.Test_options.Tags) {
167 p.testProperties.Test_options.Tags = append(p.testProperties.Test_options.Tags, "mobly")
Ziwei Zhangc3bb83a2023-03-01 14:42:47 +0800168 }
Ziwei Zhang3dfec5a2024-02-01 11:05:09 +0800169 template = "${PythonBinaryHostMoblyTestConfigTemplate}"
170 } else if runner != "tradefed" {
Ziwei Zhangc3bb83a2023-03-01 14:42:47 +0800171 panic(fmt.Errorf("unknown python test runner '%s', should be 'tradefed' or 'mobly'", runner))
172 }
Ziwei Zhang3dfec5a2024-02-01 11:05:09 +0800173 p.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
174 TestConfigProp: p.testProperties.Test_config,
175 TestConfigTemplateProp: p.testProperties.Test_config_template,
176 TestSuites: p.binaryProperties.Test_suites,
177 OptionsForAutogenerated: configs,
178 AutoGenConfig: p.binaryProperties.Auto_gen_config,
179 DeviceTemplate: template,
180 HostTemplate: template,
181 })
yelinhsieh80880a32018-11-06 11:49:55 +0800182
Cole Faust4d247e62023-01-23 10:14:58 -0800183 for _, dataSrcPath := range android.PathsForModuleSrc(ctx, p.testProperties.Data) {
184 p.data = append(p.data, android.DataPath{SrcPath: dataSrcPath})
Dan Shi31949122020-09-21 12:11:02 -0700185 }
Colin Cross1bc63932020-11-22 20:12:45 -0800186
Zi Wangbbb1b742023-05-08 11:07:26 -0700187 if p.isTestHost() && len(p.testProperties.Data_device_bins_both) > 0 {
188 ctx.VisitDirectDepsWithTag(dataDeviceBinsTag, func(dep android.Module) {
189 p.data = append(p.data, android.DataPath{SrcPath: android.OutputFileForModule(ctx, dep, "")})
190 })
191 }
192
Colin Cross1bc63932020-11-22 20:12:45 -0800193 // Emulate the data property for java_data dependencies.
194 for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) {
195 for _, javaDataSrcPath := range android.OutputFilesForModule(ctx, javaData, "") {
Cole Faust4d247e62023-01-23 10:14:58 -0800196 p.data = append(p.data, android.DataPath{SrcPath: javaDataSrcPath})
Colin Cross1bc63932020-11-22 20:12:45 -0800197 }
198 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800199
200 installDir := installDir(ctx, "nativetest", "nativetest64", ctx.ModuleName())
201 installedData := ctx.InstallTestData(installDir, p.data)
202 p.installedDest = ctx.InstallFile(installDir, p.installSource.Base(), p.installSource, installedData...)
203
Colin Cross40213022023-12-13 15:19:49 -0800204 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Nan Zhang5323f8e2017-05-10 13:37:54 -0700205}
206
Cole Faust4d247e62023-01-23 10:14:58 -0800207func (p *PythonTestModule) AndroidMkEntries() []android.AndroidMkEntries {
208 entriesList := p.PythonBinaryModule.AndroidMkEntries()
209 if len(entriesList) != 1 {
210 panic("Expected 1 entry")
Julien Desprez66534a02021-02-09 09:27:39 -0800211 }
Cole Faust4d247e62023-01-23 10:14:58 -0800212 entries := &entriesList[0]
Nan Zhangd4e641b2017-07-12 12:55:28 -0700213
Cole Faust4d247e62023-01-23 10:14:58 -0800214 entries.Class = "NATIVE_TESTS"
Nan Zhangd4e641b2017-07-12 12:55:28 -0700215
Cole Faust4d247e62023-01-23 10:14:58 -0800216 entries.ExtraEntries = append(entries.ExtraEntries,
217 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
218 //entries.AddCompatibilityTestSuites(p.binaryProperties.Test_suites...)
219 if p.testConfig != nil {
220 entries.SetString("LOCAL_FULL_TEST_CONFIG", p.testConfig.String())
221 }
Nan Zhang5323f8e2017-05-10 13:37:54 -0700222
Ziwei Zhang3dfec5a2024-02-01 11:05:09 +0800223 // ATS 2.0 is the test harness for mobly tests and the test config is for ATS 2.0.
224 // Add "v2" suffix to test config name to distinguish it from the config for TF.
225 if proptools.String(p.testProperties.Test_options.Runner) == "mobly" {
226 entries.SetString("LOCAL_TEST_CONFIG_SUFFIX", "v2")
227 }
228
Cole Faust4d247e62023-01-23 10:14:58 -0800229 entries.SetBoolIfTrue("LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG", !BoolDefault(p.binaryProperties.Auto_gen_config, true))
LaMont Jonesafe7baf2024-01-09 22:47:39 +0000230 android.SetAconfigFileMkEntries(&p.ModuleBase, entries, p.mergedAconfigFiles)
Nan Zhang5323f8e2017-05-10 13:37:54 -0700231
Cole Faust4d247e62023-01-23 10:14:58 -0800232 p.testProperties.Test_options.SetAndroidMkEntries(entries)
233 })
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000234
Cole Faust4d247e62023-01-23 10:14:58 -0800235 return entriesList
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000236}