blob: cd7c73b5a629aaec87730c4e011590678f995bd7 [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 {
39 return &PythonTestModule{PythonBinaryModule: *NewBinary(hod)}
40}
41
42func PythonTestHostFactory() android.Module {
Colin Crosse8c70c52023-05-18 11:51:56 -070043 return NewTest(android.HostSupported).init()
Cole Faust4d247e62023-01-23 10:14:58 -080044}
45
46func PythonTestFactory() android.Module {
47 module := NewTest(android.HostAndDeviceSupported)
48 module.multilib = android.MultilibBoth
49 return module.init()
50}
51
Julien Despreze146e392018-08-02 15:00:46 -070052type TestProperties struct {
53 // the name of the test configuration (for example "AndroidTest.xml") that should be
54 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070055 Test_config *string `android:"path,arch_variant"`
yelinhsieh80880a32018-11-06 11:49:55 +080056
57 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
58 // should be installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070059 Test_config_template *string `android:"path,arch_variant"`
Dan Shi31949122020-09-21 12:11:02 -070060
61 // list of files or filegroup modules that provide data that should be installed alongside
62 // the test
63 Data []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -080064
Colin Cross1bc63932020-11-22 20:12:45 -080065 // list of java modules that provide data that should be installed alongside the test.
66 Java_data []string
67
Dan Shid79572f2020-11-13 14:33:46 -080068 // Test options.
Ziwei Zhangc3bb83a2023-03-01 14:42:47 +080069 Test_options TestOptions
Zi Wangbbb1b742023-05-08 11:07:26 -070070
71 // list of device binary modules that should be installed alongside the test
72 // This property adds 64bit AND 32bit variants of the dependency
73 Data_device_bins_both []string `android:"arch_variant"`
Ziwei Zhangc3bb83a2023-03-01 14:42:47 +080074}
75
76type TestOptions struct {
77 android.CommonTestOptions
78
79 // Runner for the test. Supports "tradefed" and "mobly" (for multi-device tests). Default is "tradefed".
80 Runner *string
81
82 // Metadata to describe the test configuration.
83 Metadata []Metadata
84}
85
86type Metadata struct {
87 Name string
88 Value string
Julien Despreze146e392018-08-02 15:00:46 -070089}
90
Cole Faust4d247e62023-01-23 10:14:58 -080091type PythonTestModule struct {
92 PythonBinaryModule
Julien Despreze146e392018-08-02 15:00:46 -070093
94 testProperties TestProperties
Cole Faust4d247e62023-01-23 10:14:58 -080095 testConfig android.Path
96 data []android.DataPath
Julien Despreze146e392018-08-02 15:00:46 -070097}
98
Cole Faust4d247e62023-01-23 10:14:58 -080099func (p *PythonTestModule) init() android.Module {
100 p.AddProperties(&p.properties, &p.protoProperties)
101 p.AddProperties(&p.binaryProperties)
102 p.AddProperties(&p.testProperties)
103 android.InitAndroidArchModule(p, p.hod, p.multilib)
104 android.InitDefaultableModule(p)
105 android.InitBazelModule(p)
Zi Wangbbb1b742023-05-08 11:07:26 -0700106 if p.isTestHost() && p.testProperties.Test_options.Unit_test == nil {
Cole Faust4d247e62023-01-23 10:14:58 -0800107 p.testProperties.Test_options.Unit_test = proptools.BoolPtr(true)
108 }
109 return p
Nan Zhang5323f8e2017-05-10 13:37:54 -0700110}
111
Zi Wangbbb1b742023-05-08 11:07:26 -0700112func (p *PythonTestModule) isTestHost() bool {
113 return p.hod == android.HostSupported
114}
115
116var dataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"}
117
118// python_test_host DepsMutator uses this method to add multilib dependencies of
119// data_device_bin_both
120func (p *PythonTestModule) addDataDeviceBinsDeps(ctx android.BottomUpMutatorContext, filter string) {
121 if len(p.testProperties.Data_device_bins_both) < 1 {
122 return
123 }
124
125 var maybeAndroidTarget *android.Target
126 androidTargetList := android.FirstTarget(ctx.Config().Targets[android.Android], filter)
127 if len(androidTargetList) > 0 {
128 maybeAndroidTarget = &androidTargetList[0]
129 }
130
131 if maybeAndroidTarget != nil {
132 ctx.AddFarVariationDependencies(
133 maybeAndroidTarget.Variations(),
134 dataDeviceBinsTag,
135 p.testProperties.Data_device_bins_both...,
136 )
137 }
138}
139
140func (p *PythonTestModule) DepsMutator(ctx android.BottomUpMutatorContext) {
141 p.PythonBinaryModule.DepsMutator(ctx)
142 if p.isTestHost() {
143 p.addDataDeviceBinsDeps(ctx, "lib32")
144 p.addDataDeviceBinsDeps(ctx, "lib64")
145 }
146}
147
Cole Faust4d247e62023-01-23 10:14:58 -0800148func (p *PythonTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
149 // We inherit from only the library's GenerateAndroidBuildActions, and then
150 // just use buildBinary() so that the binary is not installed into the location
151 // it would be for regular binaries.
152 p.PythonLibraryModule.GenerateAndroidBuildActions(ctx)
153 p.buildBinary(ctx)
154
Ziwei Zhangc3bb83a2023-03-01 14:42:47 +0800155 var configs []tradefed.Option
156 for _, metadata := range p.testProperties.Test_options.Metadata {
157 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: metadata.Name, Value: metadata.Value})
158 }
159
160 runner := proptools.StringDefault(p.testProperties.Test_options.Runner, "tradefed")
161 if runner == "tradefed" {
162 p.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
163 TestConfigProp: p.testProperties.Test_config,
164 TestConfigTemplateProp: p.testProperties.Test_config_template,
165 TestSuites: p.binaryProperties.Test_suites,
166 OptionsForAutogenerated: configs,
167 AutoGenConfig: p.binaryProperties.Auto_gen_config,
168 DeviceTemplate: "${PythonBinaryHostTestConfigTemplate}",
169 HostTemplate: "${PythonBinaryHostTestConfigTemplate}",
170 })
171 } else if runner == "mobly" {
172 if p.testProperties.Test_config != nil || p.testProperties.Test_config_template != nil || p.binaryProperties.Auto_gen_config != nil {
173 panic(fmt.Errorf("cannot set test_config, test_config_template or auto_gen_config for mobly test"))
174 }
175
176 for _, testSuite := range p.binaryProperties.Test_suites {
177 if testSuite == "cts" {
178 configs = append(configs, tradefed.Option{Name: "test-suite-tag", Value: "cts"})
179 break
180 }
181 }
182 p.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
183 OptionsForAutogenerated: configs,
184 DeviceTemplate: "${PythonBinaryHostMoblyTestConfigTemplate}",
185 HostTemplate: "${PythonBinaryHostMoblyTestConfigTemplate}",
186 })
187 } else {
188 panic(fmt.Errorf("unknown python test runner '%s', should be 'tradefed' or 'mobly'", runner))
189 }
yelinhsieh80880a32018-11-06 11:49:55 +0800190
Cole Faust4d247e62023-01-23 10:14:58 -0800191 p.installedDest = ctx.InstallFile(installDir(ctx, "nativetest", "nativetest64", ctx.ModuleName()), p.installSource.Base(), p.installSource)
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000192
Cole Faust4d247e62023-01-23 10:14:58 -0800193 for _, dataSrcPath := range android.PathsForModuleSrc(ctx, p.testProperties.Data) {
194 p.data = append(p.data, android.DataPath{SrcPath: dataSrcPath})
Dan Shi31949122020-09-21 12:11:02 -0700195 }
Colin Cross1bc63932020-11-22 20:12:45 -0800196
Zi Wangbbb1b742023-05-08 11:07:26 -0700197 if p.isTestHost() && len(p.testProperties.Data_device_bins_both) > 0 {
198 ctx.VisitDirectDepsWithTag(dataDeviceBinsTag, func(dep android.Module) {
199 p.data = append(p.data, android.DataPath{SrcPath: android.OutputFileForModule(ctx, dep, "")})
200 })
201 }
202
Colin Cross1bc63932020-11-22 20:12:45 -0800203 // Emulate the data property for java_data dependencies.
204 for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) {
205 for _, javaDataSrcPath := range android.OutputFilesForModule(ctx, javaData, "") {
Cole Faust4d247e62023-01-23 10:14:58 -0800206 p.data = append(p.data, android.DataPath{SrcPath: javaDataSrcPath})
Colin Cross1bc63932020-11-22 20:12:45 -0800207 }
208 }
Aditya Choudhary9b593522023-10-06 19:54:58 +0000209 ctx.SetProvider(testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Nan Zhang5323f8e2017-05-10 13:37:54 -0700210}
211
Cole Faust4d247e62023-01-23 10:14:58 -0800212func (p *PythonTestModule) AndroidMkEntries() []android.AndroidMkEntries {
213 entriesList := p.PythonBinaryModule.AndroidMkEntries()
214 if len(entriesList) != 1 {
215 panic("Expected 1 entry")
Julien Desprez66534a02021-02-09 09:27:39 -0800216 }
Cole Faust4d247e62023-01-23 10:14:58 -0800217 entries := &entriesList[0]
Nan Zhangd4e641b2017-07-12 12:55:28 -0700218
Cole Faust4d247e62023-01-23 10:14:58 -0800219 entries.Class = "NATIVE_TESTS"
Nan Zhangd4e641b2017-07-12 12:55:28 -0700220
Cole Faust4d247e62023-01-23 10:14:58 -0800221 entries.ExtraEntries = append(entries.ExtraEntries,
222 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
223 //entries.AddCompatibilityTestSuites(p.binaryProperties.Test_suites...)
224 if p.testConfig != nil {
225 entries.SetString("LOCAL_FULL_TEST_CONFIG", p.testConfig.String())
226 }
Nan Zhang5323f8e2017-05-10 13:37:54 -0700227
Cole Faust4d247e62023-01-23 10:14:58 -0800228 entries.SetBoolIfTrue("LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG", !BoolDefault(p.binaryProperties.Auto_gen_config, true))
Nan Zhang5323f8e2017-05-10 13:37:54 -0700229
Cole Faust4d247e62023-01-23 10:14:58 -0800230 entries.AddStrings("LOCAL_TEST_DATA", android.AndroidMkDataPaths(p.data)...)
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000231
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}