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 ( |
Julien Desprez | 66534a0 | 2021-02-09 09:27:39 -0800 | [diff] [blame] | 18 | "github.com/google/blueprint/proptools" |
| 19 | |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 20 | "android/soong/android" |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 21 | "android/soong/tradefed" |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | // This file contains the module types for building Python test. |
| 25 | |
| 26 | func init() { |
Paul Duffin | d089045 | 2021-03-17 21:57:08 +0000 | [diff] [blame] | 27 | registerPythonTestComponents(android.InitRegistrationContext) |
| 28 | } |
| 29 | |
| 30 | func registerPythonTestComponents(ctx android.RegistrationContext) { |
| 31 | ctx.RegisterModuleType("python_test_host", PythonTestHostFactory) |
| 32 | ctx.RegisterModuleType("python_test", PythonTestFactory) |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 35 | func NewTest(hod android.HostOrDeviceSupported) *PythonTestModule { |
| 36 | return &PythonTestModule{PythonBinaryModule: *NewBinary(hod)} |
| 37 | } |
| 38 | |
| 39 | func PythonTestHostFactory() android.Module { |
| 40 | return NewTest(android.HostSupportedNoCross).init() |
| 41 | } |
| 42 | |
| 43 | func PythonTestFactory() android.Module { |
| 44 | module := NewTest(android.HostAndDeviceSupported) |
| 45 | module.multilib = android.MultilibBoth |
| 46 | return module.init() |
| 47 | } |
| 48 | |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 49 | type TestProperties struct { |
| 50 | // the name of the test configuration (for example "AndroidTest.xml") that should be |
| 51 | // installed with the module. |
Colin Cross | a638482 | 2020-06-09 15:09:22 -0700 | [diff] [blame] | 52 | Test_config *string `android:"path,arch_variant"` |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 53 | |
| 54 | // the name of the test configuration template (for example "AndroidTestTemplate.xml") that |
| 55 | // should be installed with the module. |
Colin Cross | a638482 | 2020-06-09 15:09:22 -0700 | [diff] [blame] | 56 | Test_config_template *string `android:"path,arch_variant"` |
Dan Shi | 3194912 | 2020-09-21 12:11:02 -0700 | [diff] [blame] | 57 | |
| 58 | // list of files or filegroup modules that provide data that should be installed alongside |
| 59 | // the test |
| 60 | Data []string `android:"path,arch_variant"` |
Dan Shi | d79572f | 2020-11-13 14:33:46 -0800 | [diff] [blame] | 61 | |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 62 | // list of java modules that provide data that should be installed alongside the test. |
| 63 | Java_data []string |
| 64 | |
Dan Shi | d79572f | 2020-11-13 14:33:46 -0800 | [diff] [blame] | 65 | // Test options. |
Zhenhuang Wang | 0ac5a43 | 2022-08-12 18:49:20 +0800 | [diff] [blame] | 66 | Test_options android.CommonTestOptions |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 69 | type PythonTestModule struct { |
| 70 | PythonBinaryModule |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 71 | |
| 72 | testProperties TestProperties |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 73 | testConfig android.Path |
| 74 | data []android.DataPath |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 77 | func (p *PythonTestModule) init() android.Module { |
| 78 | p.AddProperties(&p.properties, &p.protoProperties) |
| 79 | p.AddProperties(&p.binaryProperties) |
| 80 | p.AddProperties(&p.testProperties) |
| 81 | android.InitAndroidArchModule(p, p.hod, p.multilib) |
| 82 | android.InitDefaultableModule(p) |
| 83 | android.InitBazelModule(p) |
| 84 | if p.hod == android.HostSupportedNoCross && p.testProperties.Test_options.Unit_test == nil { |
| 85 | p.testProperties.Test_options.Unit_test = proptools.BoolPtr(true) |
| 86 | } |
| 87 | return p |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 90 | func (p *PythonTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 91 | // We inherit from only the library's GenerateAndroidBuildActions, and then |
| 92 | // just use buildBinary() so that the binary is not installed into the location |
| 93 | // it would be for regular binaries. |
| 94 | p.PythonLibraryModule.GenerateAndroidBuildActions(ctx) |
| 95 | p.buildBinary(ctx) |
| 96 | |
| 97 | p.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{ |
| 98 | TestConfigProp: p.testProperties.Test_config, |
| 99 | TestConfigTemplateProp: p.testProperties.Test_config_template, |
| 100 | TestSuites: p.binaryProperties.Test_suites, |
| 101 | AutoGenConfig: p.binaryProperties.Auto_gen_config, |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 102 | DeviceTemplate: "${PythonBinaryHostTestConfigTemplate}", |
| 103 | HostTemplate: "${PythonBinaryHostTestConfigTemplate}", |
| 104 | }) |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 105 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 106 | p.installedDest = ctx.InstallFile(installDir(ctx, "nativetest", "nativetest64", ctx.ModuleName()), p.installSource.Base(), p.installSource) |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 107 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 108 | for _, dataSrcPath := range android.PathsForModuleSrc(ctx, p.testProperties.Data) { |
| 109 | p.data = append(p.data, android.DataPath{SrcPath: dataSrcPath}) |
Dan Shi | 3194912 | 2020-09-21 12:11:02 -0700 | [diff] [blame] | 110 | } |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 111 | |
| 112 | // Emulate the data property for java_data dependencies. |
| 113 | for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) { |
| 114 | for _, javaDataSrcPath := range android.OutputFilesForModule(ctx, javaData, "") { |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 115 | p.data = append(p.data, android.DataPath{SrcPath: javaDataSrcPath}) |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 116 | } |
| 117 | } |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 120 | func (p *PythonTestModule) AndroidMkEntries() []android.AndroidMkEntries { |
| 121 | entriesList := p.PythonBinaryModule.AndroidMkEntries() |
| 122 | if len(entriesList) != 1 { |
| 123 | panic("Expected 1 entry") |
Julien Desprez | 66534a0 | 2021-02-09 09:27:39 -0800 | [diff] [blame] | 124 | } |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 125 | entries := &entriesList[0] |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 126 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 127 | entries.Class = "NATIVE_TESTS" |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 128 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 129 | entries.ExtraEntries = append(entries.ExtraEntries, |
| 130 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 131 | //entries.AddCompatibilityTestSuites(p.binaryProperties.Test_suites...) |
| 132 | if p.testConfig != nil { |
| 133 | entries.SetString("LOCAL_FULL_TEST_CONFIG", p.testConfig.String()) |
| 134 | } |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 135 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 136 | 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] | 137 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 138 | entries.AddStrings("LOCAL_TEST_DATA", android.AndroidMkDataPaths(p.data)...) |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 139 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 140 | p.testProperties.Test_options.SetAndroidMkEntries(entries) |
| 141 | }) |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 142 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame^] | 143 | return entriesList |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 144 | } |