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 | |
Aditya Choudhary | 9b59352 | 2023-10-06 19:54:58 +0000 | [diff] [blame] | 20 | "android/soong/testing" |
Julien Desprez | 66534a0 | 2021-02-09 09:27:39 -0800 | [diff] [blame] | 21 | "github.com/google/blueprint/proptools" |
| 22 | |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 23 | "android/soong/android" |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 24 | "android/soong/tradefed" |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | // This file contains the module types for building Python test. |
| 28 | |
| 29 | func init() { |
Paul Duffin | d089045 | 2021-03-17 21:57:08 +0000 | [diff] [blame] | 30 | registerPythonTestComponents(android.InitRegistrationContext) |
| 31 | } |
| 32 | |
| 33 | func registerPythonTestComponents(ctx android.RegistrationContext) { |
| 34 | ctx.RegisterModuleType("python_test_host", PythonTestHostFactory) |
| 35 | ctx.RegisterModuleType("python_test", PythonTestFactory) |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 38 | func NewTest(hod android.HostOrDeviceSupported) *PythonTestModule { |
| 39 | return &PythonTestModule{PythonBinaryModule: *NewBinary(hod)} |
| 40 | } |
| 41 | |
| 42 | func PythonTestHostFactory() android.Module { |
Colin Cross | e8c70c5 | 2023-05-18 11:51:56 -0700 | [diff] [blame] | 43 | return NewTest(android.HostSupported).init() |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | func PythonTestFactory() android.Module { |
| 47 | module := NewTest(android.HostAndDeviceSupported) |
| 48 | module.multilib = android.MultilibBoth |
| 49 | return module.init() |
| 50 | } |
| 51 | |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 52 | type TestProperties struct { |
| 53 | // the name of the test configuration (for example "AndroidTest.xml") that should be |
| 54 | // installed with the module. |
Colin Cross | a638482 | 2020-06-09 15:09:22 -0700 | [diff] [blame] | 55 | Test_config *string `android:"path,arch_variant"` |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 56 | |
| 57 | // the name of the test configuration template (for example "AndroidTestTemplate.xml") that |
| 58 | // should be installed with the module. |
Colin Cross | a638482 | 2020-06-09 15:09:22 -0700 | [diff] [blame] | 59 | Test_config_template *string `android:"path,arch_variant"` |
Dan Shi | 3194912 | 2020-09-21 12:11:02 -0700 | [diff] [blame] | 60 | |
| 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 Shi | d79572f | 2020-11-13 14:33:46 -0800 | [diff] [blame] | 64 | |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 65 | // list of java modules that provide data that should be installed alongside the test. |
| 66 | Java_data []string |
| 67 | |
Dan Shi | d79572f | 2020-11-13 14:33:46 -0800 | [diff] [blame] | 68 | // Test options. |
Ziwei Zhang | c3bb83a | 2023-03-01 14:42:47 +0800 | [diff] [blame] | 69 | Test_options TestOptions |
Zi Wang | bbb1b74 | 2023-05-08 11:07:26 -0700 | [diff] [blame] | 70 | |
| 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 Zhang | c3bb83a | 2023-03-01 14:42:47 +0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | type 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 | |
| 86 | type Metadata struct { |
| 87 | Name string |
| 88 | Value string |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 91 | type PythonTestModule struct { |
| 92 | PythonBinaryModule |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 93 | |
| 94 | testProperties TestProperties |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 95 | testConfig android.Path |
| 96 | data []android.DataPath |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 99 | func (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) |
Zi Wang | bbb1b74 | 2023-05-08 11:07:26 -0700 | [diff] [blame] | 105 | if p.isTestHost() && p.testProperties.Test_options.Unit_test == nil { |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 106 | p.testProperties.Test_options.Unit_test = proptools.BoolPtr(true) |
| 107 | } |
| 108 | return p |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Zi Wang | bbb1b74 | 2023-05-08 11:07:26 -0700 | [diff] [blame] | 111 | func (p *PythonTestModule) isTestHost() bool { |
| 112 | return p.hod == android.HostSupported |
| 113 | } |
| 114 | |
| 115 | var dataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"} |
| 116 | |
| 117 | // python_test_host DepsMutator uses this method to add multilib dependencies of |
| 118 | // data_device_bin_both |
| 119 | func (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 | |
| 139 | func (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 Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 147 | func (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) |
LaMont Jones | afe7baf | 2024-01-09 22:47:39 +0000 | [diff] [blame] | 152 | android.CollectDependencyAconfigFiles(ctx, &p.mergedAconfigFiles) |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 153 | p.buildBinary(ctx) |
| 154 | |
Ziwei Zhang | c3bb83a | 2023-03-01 14:42:47 +0800 | [diff] [blame] | 155 | 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 | } |
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 | } |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 194 | |
Zi Wang | bbb1b74 | 2023-05-08 11:07:26 -0700 | [diff] [blame] | 195 | 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 Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 201 | // Emulate the data property for java_data dependencies. |
| 202 | for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) { |
| 203 | for _, javaDataSrcPath := range android.OutputFilesForModule(ctx, javaData, "") { |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 204 | p.data = append(p.data, android.DataPath{SrcPath: javaDataSrcPath}) |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 205 | } |
| 206 | } |
Colin Cross | 5c1d5fb | 2023-11-15 12:39:40 -0800 | [diff] [blame] | 207 | |
| 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 Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 212 | android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{}) |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 215 | func (p *PythonTestModule) AndroidMkEntries() []android.AndroidMkEntries { |
| 216 | entriesList := p.PythonBinaryModule.AndroidMkEntries() |
| 217 | if len(entriesList) != 1 { |
| 218 | panic("Expected 1 entry") |
Julien Desprez | 66534a0 | 2021-02-09 09:27:39 -0800 | [diff] [blame] | 219 | } |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 220 | entries := &entriesList[0] |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 221 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 222 | entries.Class = "NATIVE_TESTS" |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 223 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 224 | 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 Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 230 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 231 | entries.SetBoolIfTrue("LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG", !BoolDefault(p.binaryProperties.Auto_gen_config, true)) |
LaMont Jones | afe7baf | 2024-01-09 22:47:39 +0000 | [diff] [blame] | 232 | android.SetAconfigFileMkEntries(&p.ModuleBase, entries, p.mergedAconfigFiles) |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 233 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 234 | p.testProperties.Test_options.SetAndroidMkEntries(entries) |
| 235 | }) |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 236 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 237 | return entriesList |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 238 | } |