Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package python |
| 16 | |
| 17 | import ( |
Ziwei Zhang | c3bb83a | 2023-03-01 14:42:47 +0800 | [diff] [blame] | 18 | "fmt" |
| 19 | |
Julien Desprez | 66534a0 | 2021-02-09 09:27:39 -0800 | [diff] [blame] | 20 | "github.com/google/blueprint/proptools" |
| 21 | |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 22 | "android/soong/android" |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 23 | "android/soong/tradefed" |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | // This file contains the module types for building Python test. |
| 27 | |
| 28 | func init() { |
Paul Duffin | d089045 | 2021-03-17 21:57:08 +0000 | [diff] [blame] | 29 | registerPythonTestComponents(android.InitRegistrationContext) |
| 30 | } |
| 31 | |
| 32 | func registerPythonTestComponents(ctx android.RegistrationContext) { |
| 33 | ctx.RegisterModuleType("python_test_host", PythonTestHostFactory) |
| 34 | ctx.RegisterModuleType("python_test", PythonTestFactory) |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 37 | func NewTest(hod android.HostOrDeviceSupported) *PythonTestModule { |
Ronald Braunstein | c4cd7a1 | 2024-04-16 16:39:48 -0700 | [diff] [blame] | 38 | p := &PythonTestModule{PythonBinaryModule: *NewBinary(hod)} |
| 39 | p.sourceProperties = android.SourceProperties{Test_only: proptools.BoolPtr(true), Top_level_test_target: true} |
| 40 | return p |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | func PythonTestHostFactory() android.Module { |
Colin Cross | e8c70c5 | 2023-05-18 11:51:56 -0700 | [diff] [blame] | 44 | return NewTest(android.HostSupported).init() |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | func PythonTestFactory() android.Module { |
| 48 | module := NewTest(android.HostAndDeviceSupported) |
| 49 | module.multilib = android.MultilibBoth |
| 50 | return module.init() |
| 51 | } |
| 52 | |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 53 | type TestProperties struct { |
| 54 | // the name of the test configuration (for example "AndroidTest.xml") that should be |
| 55 | // installed with the module. |
Colin Cross | a638482 | 2020-06-09 15:09:22 -0700 | [diff] [blame] | 56 | Test_config *string `android:"path,arch_variant"` |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 57 | |
| 58 | // the name of the test configuration template (for example "AndroidTestTemplate.xml") that |
| 59 | // should be installed with the module. |
Colin Cross | a638482 | 2020-06-09 15:09:22 -0700 | [diff] [blame] | 60 | Test_config_template *string `android:"path,arch_variant"` |
Dan Shi | 3194912 | 2020-09-21 12:11:02 -0700 | [diff] [blame] | 61 | |
| 62 | // list of files or filegroup modules that provide data that should be installed alongside |
| 63 | // the test |
| 64 | Data []string `android:"path,arch_variant"` |
Dan Shi | d79572f | 2020-11-13 14:33:46 -0800 | [diff] [blame] | 65 | |
Cole Faust | ee45d33 | 2024-10-23 11:17:51 -0700 | [diff] [blame] | 66 | // Same as data, but will add dependencies on modules using the device's os variation and |
| 67 | // the common arch variation. Useful for a host test that wants to embed a module built for |
| 68 | // device. |
| 69 | Device_common_data []string `android:"path_device_common"` |
| 70 | |
Inseob Kim | 25f5ae4 | 2025-01-07 22:27:58 +0900 | [diff] [blame] | 71 | // Same as data, but will add dependencies on modules via a device os variation and the |
| 72 | // device's first supported arch's variation. Useful for a host test that wants to embed a |
| 73 | // module built for device. |
| 74 | Device_first_data []string `android:"path_device_first"` |
| 75 | |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 76 | // list of java modules that provide data that should be installed alongside the test. |
| 77 | Java_data []string |
| 78 | |
Dan Shi | d79572f | 2020-11-13 14:33:46 -0800 | [diff] [blame] | 79 | // Test options. |
Ziwei Zhang | c3bb83a | 2023-03-01 14:42:47 +0800 | [diff] [blame] | 80 | Test_options TestOptions |
Zi Wang | bbb1b74 | 2023-05-08 11:07:26 -0700 | [diff] [blame] | 81 | |
| 82 | // list of device binary modules that should be installed alongside the test |
| 83 | // This property adds 64bit AND 32bit variants of the dependency |
| 84 | Data_device_bins_both []string `android:"arch_variant"` |
Ziwei Zhang | c3bb83a | 2023-03-01 14:42:47 +0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | type TestOptions struct { |
| 88 | android.CommonTestOptions |
| 89 | |
| 90 | // Runner for the test. Supports "tradefed" and "mobly" (for multi-device tests). Default is "tradefed". |
| 91 | Runner *string |
| 92 | |
| 93 | // Metadata to describe the test configuration. |
| 94 | Metadata []Metadata |
| 95 | } |
| 96 | |
| 97 | type Metadata struct { |
| 98 | Name string |
| 99 | Value string |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 102 | type PythonTestModule struct { |
| 103 | PythonBinaryModule |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 104 | |
| 105 | testProperties TestProperties |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 106 | testConfig android.Path |
| 107 | data []android.DataPath |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 110 | func (p *PythonTestModule) init() android.Module { |
| 111 | p.AddProperties(&p.properties, &p.protoProperties) |
| 112 | p.AddProperties(&p.binaryProperties) |
| 113 | p.AddProperties(&p.testProperties) |
| 114 | android.InitAndroidArchModule(p, p.hod, p.multilib) |
| 115 | android.InitDefaultableModule(p) |
Zi Wang | bbb1b74 | 2023-05-08 11:07:26 -0700 | [diff] [blame] | 116 | if p.isTestHost() && p.testProperties.Test_options.Unit_test == nil { |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 117 | p.testProperties.Test_options.Unit_test = proptools.BoolPtr(true) |
| 118 | } |
| 119 | return p |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Zi Wang | bbb1b74 | 2023-05-08 11:07:26 -0700 | [diff] [blame] | 122 | func (p *PythonTestModule) isTestHost() bool { |
| 123 | return p.hod == android.HostSupported |
| 124 | } |
| 125 | |
| 126 | var dataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"} |
| 127 | |
| 128 | // python_test_host DepsMutator uses this method to add multilib dependencies of |
| 129 | // data_device_bin_both |
| 130 | func (p *PythonTestModule) addDataDeviceBinsDeps(ctx android.BottomUpMutatorContext, filter string) { |
| 131 | if len(p.testProperties.Data_device_bins_both) < 1 { |
| 132 | return |
| 133 | } |
| 134 | |
| 135 | var maybeAndroidTarget *android.Target |
| 136 | androidTargetList := android.FirstTarget(ctx.Config().Targets[android.Android], filter) |
| 137 | if len(androidTargetList) > 0 { |
| 138 | maybeAndroidTarget = &androidTargetList[0] |
| 139 | } |
| 140 | |
| 141 | if maybeAndroidTarget != nil { |
| 142 | ctx.AddFarVariationDependencies( |
| 143 | maybeAndroidTarget.Variations(), |
| 144 | dataDeviceBinsTag, |
| 145 | p.testProperties.Data_device_bins_both..., |
| 146 | ) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | func (p *PythonTestModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 151 | p.PythonBinaryModule.DepsMutator(ctx) |
| 152 | if p.isTestHost() { |
| 153 | p.addDataDeviceBinsDeps(ctx, "lib32") |
| 154 | p.addDataDeviceBinsDeps(ctx, "lib64") |
| 155 | } |
| 156 | } |
| 157 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 158 | func (p *PythonTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 159 | // We inherit from only the library's GenerateAndroidBuildActions, and then |
| 160 | // just use buildBinary() so that the binary is not installed into the location |
| 161 | // it would be for regular binaries. |
| 162 | p.PythonLibraryModule.GenerateAndroidBuildActions(ctx) |
| 163 | p.buildBinary(ctx) |
| 164 | |
Ziwei Zhang | c3bb83a | 2023-03-01 14:42:47 +0800 | [diff] [blame] | 165 | var configs []tradefed.Option |
| 166 | for _, metadata := range p.testProperties.Test_options.Metadata { |
| 167 | configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: metadata.Name, Value: metadata.Value}) |
| 168 | } |
| 169 | |
| 170 | runner := proptools.StringDefault(p.testProperties.Test_options.Runner, "tradefed") |
Ziwei Zhang | 3dfec5a | 2024-02-01 11:05:09 +0800 | [diff] [blame] | 171 | template := "${PythonBinaryHostTestConfigTemplate}" |
| 172 | if runner == "mobly" { |
| 173 | // Add tag to enable Atest mobly runner |
| 174 | if !android.InList("mobly", p.testProperties.Test_options.Tags) { |
| 175 | p.testProperties.Test_options.Tags = append(p.testProperties.Test_options.Tags, "mobly") |
Ziwei Zhang | c3bb83a | 2023-03-01 14:42:47 +0800 | [diff] [blame] | 176 | } |
Ziwei Zhang | 3dfec5a | 2024-02-01 11:05:09 +0800 | [diff] [blame] | 177 | template = "${PythonBinaryHostMoblyTestConfigTemplate}" |
| 178 | } else if runner != "tradefed" { |
Ziwei Zhang | c3bb83a | 2023-03-01 14:42:47 +0800 | [diff] [blame] | 179 | panic(fmt.Errorf("unknown python test runner '%s', should be 'tradefed' or 'mobly'", runner)) |
| 180 | } |
Ziwei Zhang | 3dfec5a | 2024-02-01 11:05:09 +0800 | [diff] [blame] | 181 | p.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{ |
| 182 | TestConfigProp: p.testProperties.Test_config, |
| 183 | TestConfigTemplateProp: p.testProperties.Test_config_template, |
| 184 | TestSuites: p.binaryProperties.Test_suites, |
| 185 | OptionsForAutogenerated: configs, |
| 186 | AutoGenConfig: p.binaryProperties.Auto_gen_config, |
| 187 | DeviceTemplate: template, |
| 188 | HostTemplate: template, |
| 189 | }) |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 190 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 191 | for _, dataSrcPath := range android.PathsForModuleSrc(ctx, p.testProperties.Data) { |
| 192 | p.data = append(p.data, android.DataPath{SrcPath: dataSrcPath}) |
Dan Shi | 3194912 | 2020-09-21 12:11:02 -0700 | [diff] [blame] | 193 | } |
Cole Faust | ee45d33 | 2024-10-23 11:17:51 -0700 | [diff] [blame] | 194 | for _, dataSrcPath := range android.PathsForModuleSrc(ctx, p.testProperties.Device_common_data) { |
| 195 | p.data = append(p.data, android.DataPath{SrcPath: dataSrcPath}) |
| 196 | } |
Inseob Kim | 25f5ae4 | 2025-01-07 22:27:58 +0900 | [diff] [blame] | 197 | for _, dataSrcPath := range android.PathsForModuleSrc(ctx, p.testProperties.Device_first_data) { |
| 198 | p.data = append(p.data, android.DataPath{SrcPath: dataSrcPath}) |
| 199 | } |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 200 | |
Zi Wang | bbb1b74 | 2023-05-08 11:07:26 -0700 | [diff] [blame] | 201 | if p.isTestHost() && len(p.testProperties.Data_device_bins_both) > 0 { |
Yu Liu | e98f706 | 2025-01-17 22:52:43 +0000 | [diff] [blame] | 202 | ctx.VisitDirectDepsProxyWithTag(dataDeviceBinsTag, func(dep android.ModuleProxy) { |
Zi Wang | bbb1b74 | 2023-05-08 11:07:26 -0700 | [diff] [blame] | 203 | p.data = append(p.data, android.DataPath{SrcPath: android.OutputFileForModule(ctx, dep, "")}) |
| 204 | }) |
| 205 | } |
| 206 | |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 207 | // Emulate the data property for java_data dependencies. |
Yu Liu | e98f706 | 2025-01-17 22:52:43 +0000 | [diff] [blame] | 208 | for _, javaData := range ctx.GetDirectDepsProxyWithTag(javaDataTag) { |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 209 | for _, javaDataSrcPath := range android.OutputFilesForModule(ctx, javaData, "") { |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 210 | p.data = append(p.data, android.DataPath{SrcPath: javaDataSrcPath}) |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
Colin Cross | 5c1d5fb | 2023-11-15 12:39:40 -0800 | [diff] [blame] | 213 | |
| 214 | installDir := installDir(ctx, "nativetest", "nativetest64", ctx.ModuleName()) |
| 215 | installedData := ctx.InstallTestData(installDir, p.data) |
| 216 | p.installedDest = ctx.InstallFile(installDir, p.installSource.Base(), p.installSource, installedData...) |
Cole Faust | 5edca14 | 2025-01-28 16:47:17 -0800 | [diff] [blame^] | 217 | |
| 218 | moduleInfoJSON := ctx.ModuleInfoJSON() |
| 219 | moduleInfoJSON.Class = []string{"NATIVE_TESTS"} |
| 220 | if len(p.binaryProperties.Test_suites) > 0 { |
| 221 | moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, p.binaryProperties.Test_suites...) |
| 222 | } else { |
| 223 | moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "null-suite") |
| 224 | } |
| 225 | if p.testConfig != nil { |
| 226 | moduleInfoJSON.TestConfig = append(moduleInfoJSON.TestConfig, p.testConfig.String()) |
| 227 | } |
| 228 | if _, ok := p.testConfig.(android.WritablePath); ok { |
| 229 | moduleInfoJSON.AutoTestConfig = []string{"true"} |
| 230 | } |
| 231 | moduleInfoJSON.TestOptionsTags = append(moduleInfoJSON.TestOptionsTags, p.testProperties.Test_options.Tags...) |
| 232 | moduleInfoJSON.Dependencies = append(moduleInfoJSON.Dependencies, p.androidMkSharedLibs...) |
| 233 | moduleInfoJSON.SharedLibs = append(moduleInfoJSON.Dependencies, p.androidMkSharedLibs...) |
| 234 | moduleInfoJSON.SystemSharedLibs = []string{"none"} |
| 235 | if proptools.Bool(p.testProperties.Test_options.Unit_test) { |
| 236 | moduleInfoJSON.IsUnitTest = "true" |
| 237 | if p.isTestHost() { |
| 238 | moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "host-unit-tests") |
| 239 | } |
| 240 | } |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 243 | func (p *PythonTestModule) AndroidMkEntries() []android.AndroidMkEntries { |
| 244 | entriesList := p.PythonBinaryModule.AndroidMkEntries() |
| 245 | if len(entriesList) != 1 { |
| 246 | panic("Expected 1 entry") |
Julien Desprez | 66534a0 | 2021-02-09 09:27:39 -0800 | [diff] [blame] | 247 | } |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 248 | entries := &entriesList[0] |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 249 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 250 | entries.Class = "NATIVE_TESTS" |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 251 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 252 | entries.ExtraEntries = append(entries.ExtraEntries, |
| 253 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 254 | //entries.AddCompatibilityTestSuites(p.binaryProperties.Test_suites...) |
| 255 | if p.testConfig != nil { |
| 256 | entries.SetString("LOCAL_FULL_TEST_CONFIG", p.testConfig.String()) |
| 257 | } |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 258 | |
Ziwei Zhang | 3dfec5a | 2024-02-01 11:05:09 +0800 | [diff] [blame] | 259 | // ATS 2.0 is the test harness for mobly tests and the test config is for ATS 2.0. |
| 260 | // Add "v2" suffix to test config name to distinguish it from the config for TF. |
| 261 | if proptools.String(p.testProperties.Test_options.Runner) == "mobly" { |
| 262 | entries.SetString("LOCAL_TEST_CONFIG_SUFFIX", "v2") |
| 263 | } |
| 264 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 265 | entries.SetBoolIfTrue("LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG", !BoolDefault(p.binaryProperties.Auto_gen_config, true)) |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 266 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 267 | p.testProperties.Test_options.SetAndroidMkEntries(entries) |
| 268 | }) |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 269 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 270 | return entriesList |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 271 | } |