blob: 6e23a447f428449abe6cc5db949296898e38ff90 [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
Julien Desprez66534a02021-02-09 09:27:39 -080020 "github.com/google/blueprint/proptools"
21
Nan Zhang5323f8e2017-05-10 13:37:54 -070022 "android/soong/android"
yelinhsieh80880a32018-11-06 11:49:55 +080023 "android/soong/tradefed"
Nan Zhang5323f8e2017-05-10 13:37:54 -070024)
25
26// This file contains the module types for building Python test.
27
28func init() {
Paul Duffind0890452021-03-17 21:57:08 +000029 registerPythonTestComponents(android.InitRegistrationContext)
30}
31
32func registerPythonTestComponents(ctx android.RegistrationContext) {
33 ctx.RegisterModuleType("python_test_host", PythonTestHostFactory)
34 ctx.RegisterModuleType("python_test", PythonTestFactory)
Nan Zhang5323f8e2017-05-10 13:37:54 -070035}
36
Cole Faust4d247e62023-01-23 10:14:58 -080037func NewTest(hod android.HostOrDeviceSupported) *PythonTestModule {
38 return &PythonTestModule{PythonBinaryModule: *NewBinary(hod)}
39}
40
41func PythonTestHostFactory() android.Module {
Colin Crosse8c70c52023-05-18 11:51:56 -070042 return NewTest(android.HostSupported).init()
Cole Faust4d247e62023-01-23 10:14:58 -080043}
44
45func PythonTestFactory() android.Module {
46 module := NewTest(android.HostAndDeviceSupported)
47 module.multilib = android.MultilibBoth
48 return module.init()
49}
50
Julien Despreze146e392018-08-02 15:00:46 -070051type TestProperties struct {
52 // the name of the test configuration (for example "AndroidTest.xml") that should be
53 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070054 Test_config *string `android:"path,arch_variant"`
yelinhsieh80880a32018-11-06 11:49:55 +080055
56 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
57 // should be installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070058 Test_config_template *string `android:"path,arch_variant"`
Dan Shi31949122020-09-21 12:11:02 -070059
60 // list of files or filegroup modules that provide data that should be installed alongside
61 // the test
62 Data []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -080063
Colin Cross1bc63932020-11-22 20:12:45 -080064 // list of java modules that provide data that should be installed alongside the test.
65 Java_data []string
66
Dan Shid79572f2020-11-13 14:33:46 -080067 // Test options.
Ziwei Zhangc3bb83a2023-03-01 14:42:47 +080068 Test_options TestOptions
Zi Wangbbb1b742023-05-08 11:07:26 -070069
70 // list of device binary modules that should be installed alongside the test
71 // This property adds 64bit AND 32bit variants of the dependency
72 Data_device_bins_both []string `android:"arch_variant"`
Ziwei Zhangc3bb83a2023-03-01 14:42:47 +080073}
74
75type TestOptions struct {
76 android.CommonTestOptions
77
78 // Runner for the test. Supports "tradefed" and "mobly" (for multi-device tests). Default is "tradefed".
79 Runner *string
80
81 // Metadata to describe the test configuration.
82 Metadata []Metadata
83}
84
85type Metadata struct {
86 Name string
87 Value string
Julien Despreze146e392018-08-02 15:00:46 -070088}
89
Cole Faust4d247e62023-01-23 10:14:58 -080090type PythonTestModule struct {
91 PythonBinaryModule
Julien Despreze146e392018-08-02 15:00:46 -070092
93 testProperties TestProperties
Cole Faust4d247e62023-01-23 10:14:58 -080094 testConfig android.Path
95 data []android.DataPath
Julien Despreze146e392018-08-02 15:00:46 -070096}
97
Cole Faust4d247e62023-01-23 10:14:58 -080098func (p *PythonTestModule) init() android.Module {
99 p.AddProperties(&p.properties, &p.protoProperties)
100 p.AddProperties(&p.binaryProperties)
101 p.AddProperties(&p.testProperties)
102 android.InitAndroidArchModule(p, p.hod, p.multilib)
103 android.InitDefaultableModule(p)
104 android.InitBazelModule(p)
Zi Wangbbb1b742023-05-08 11:07:26 -0700105 if p.isTestHost() && p.testProperties.Test_options.Unit_test == nil {
Cole Faust4d247e62023-01-23 10:14:58 -0800106 p.testProperties.Test_options.Unit_test = proptools.BoolPtr(true)
107 }
108 return p
Nan Zhang5323f8e2017-05-10 13:37:54 -0700109}
110
Zi Wangbbb1b742023-05-08 11:07:26 -0700111func (p *PythonTestModule) isTestHost() bool {
112 return p.hod == android.HostSupported
113}
114
115var dataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"}
116
117// python_test_host DepsMutator uses this method to add multilib dependencies of
118// data_device_bin_both
119func (p *PythonTestModule) addDataDeviceBinsDeps(ctx android.BottomUpMutatorContext, filter string) {
120 if len(p.testProperties.Data_device_bins_both) < 1 {
121 return
122 }
123
124 var maybeAndroidTarget *android.Target
125 androidTargetList := android.FirstTarget(ctx.Config().Targets[android.Android], filter)
126 if len(androidTargetList) > 0 {
127 maybeAndroidTarget = &androidTargetList[0]
128 }
129
130 if maybeAndroidTarget != nil {
131 ctx.AddFarVariationDependencies(
132 maybeAndroidTarget.Variations(),
133 dataDeviceBinsTag,
134 p.testProperties.Data_device_bins_both...,
135 )
136 }
137}
138
139func (p *PythonTestModule) DepsMutator(ctx android.BottomUpMutatorContext) {
140 p.PythonBinaryModule.DepsMutator(ctx)
141 if p.isTestHost() {
142 p.addDataDeviceBinsDeps(ctx, "lib32")
143 p.addDataDeviceBinsDeps(ctx, "lib64")
144 }
145}
146
Cole Faust4d247e62023-01-23 10:14:58 -0800147func (p *PythonTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
148 // We inherit from only the library's GenerateAndroidBuildActions, and then
149 // just use buildBinary() so that the binary is not installed into the location
150 // it would be for regular binaries.
151 p.PythonLibraryModule.GenerateAndroidBuildActions(ctx)
152 p.buildBinary(ctx)
153
Ziwei Zhangc3bb83a2023-03-01 14:42:47 +0800154 var configs []tradefed.Option
155 for _, metadata := range p.testProperties.Test_options.Metadata {
156 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: metadata.Name, Value: metadata.Value})
157 }
158
159 runner := proptools.StringDefault(p.testProperties.Test_options.Runner, "tradefed")
160 if runner == "tradefed" {
161 p.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
162 TestConfigProp: p.testProperties.Test_config,
163 TestConfigTemplateProp: p.testProperties.Test_config_template,
164 TestSuites: p.binaryProperties.Test_suites,
165 OptionsForAutogenerated: configs,
166 AutoGenConfig: p.binaryProperties.Auto_gen_config,
167 DeviceTemplate: "${PythonBinaryHostTestConfigTemplate}",
168 HostTemplate: "${PythonBinaryHostTestConfigTemplate}",
169 })
170 } else if runner == "mobly" {
171 if p.testProperties.Test_config != nil || p.testProperties.Test_config_template != nil || p.binaryProperties.Auto_gen_config != nil {
172 panic(fmt.Errorf("cannot set test_config, test_config_template or auto_gen_config for mobly test"))
173 }
174
175 for _, testSuite := range p.binaryProperties.Test_suites {
176 if testSuite == "cts" {
177 configs = append(configs, tradefed.Option{Name: "test-suite-tag", Value: "cts"})
178 break
179 }
180 }
181 p.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
182 OptionsForAutogenerated: configs,
183 DeviceTemplate: "${PythonBinaryHostMoblyTestConfigTemplate}",
184 HostTemplate: "${PythonBinaryHostMoblyTestConfigTemplate}",
185 })
186 } else {
187 panic(fmt.Errorf("unknown python test runner '%s', should be 'tradefed' or 'mobly'", runner))
188 }
yelinhsieh80880a32018-11-06 11:49:55 +0800189
Cole Faust4d247e62023-01-23 10:14:58 -0800190 p.installedDest = ctx.InstallFile(installDir(ctx, "nativetest", "nativetest64", ctx.ModuleName()), p.installSource.Base(), p.installSource)
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000191
Cole Faust4d247e62023-01-23 10:14:58 -0800192 for _, dataSrcPath := range android.PathsForModuleSrc(ctx, p.testProperties.Data) {
193 p.data = append(p.data, android.DataPath{SrcPath: dataSrcPath})
Dan Shi31949122020-09-21 12:11:02 -0700194 }
Colin Cross1bc63932020-11-22 20:12:45 -0800195
Zi Wangbbb1b742023-05-08 11:07:26 -0700196 if p.isTestHost() && len(p.testProperties.Data_device_bins_both) > 0 {
197 ctx.VisitDirectDepsWithTag(dataDeviceBinsTag, func(dep android.Module) {
198 p.data = append(p.data, android.DataPath{SrcPath: android.OutputFileForModule(ctx, dep, "")})
199 })
200 }
201
Colin Cross1bc63932020-11-22 20:12:45 -0800202 // Emulate the data property for java_data dependencies.
203 for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) {
204 for _, javaDataSrcPath := range android.OutputFilesForModule(ctx, javaData, "") {
Cole Faust4d247e62023-01-23 10:14:58 -0800205 p.data = append(p.data, android.DataPath{SrcPath: javaDataSrcPath})
Colin Cross1bc63932020-11-22 20:12:45 -0800206 }
207 }
Nan Zhang5323f8e2017-05-10 13:37:54 -0700208}
209
Cole Faust4d247e62023-01-23 10:14:58 -0800210func (p *PythonTestModule) AndroidMkEntries() []android.AndroidMkEntries {
211 entriesList := p.PythonBinaryModule.AndroidMkEntries()
212 if len(entriesList) != 1 {
213 panic("Expected 1 entry")
Julien Desprez66534a02021-02-09 09:27:39 -0800214 }
Cole Faust4d247e62023-01-23 10:14:58 -0800215 entries := &entriesList[0]
Nan Zhangd4e641b2017-07-12 12:55:28 -0700216
Cole Faust4d247e62023-01-23 10:14:58 -0800217 entries.Class = "NATIVE_TESTS"
Nan Zhangd4e641b2017-07-12 12:55:28 -0700218
Cole Faust4d247e62023-01-23 10:14:58 -0800219 entries.ExtraEntries = append(entries.ExtraEntries,
220 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
221 //entries.AddCompatibilityTestSuites(p.binaryProperties.Test_suites...)
222 if p.testConfig != nil {
223 entries.SetString("LOCAL_FULL_TEST_CONFIG", p.testConfig.String())
224 }
Nan Zhang5323f8e2017-05-10 13:37:54 -0700225
Cole Faust4d247e62023-01-23 10:14:58 -0800226 entries.SetBoolIfTrue("LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG", !BoolDefault(p.binaryProperties.Auto_gen_config, true))
Nan Zhang5323f8e2017-05-10 13:37:54 -0700227
Cole Faust4d247e62023-01-23 10:14:58 -0800228 entries.AddStrings("LOCAL_TEST_DATA", android.AndroidMkDataPaths(p.data)...)
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000229
Cole Faust4d247e62023-01-23 10:14:58 -0800230 p.testProperties.Test_options.SetAndroidMkEntries(entries)
231 })
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000232
Cole Faust4d247e62023-01-23 10:14:58 -0800233 return entriesList
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000234}