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 | |
Dan Shi | d79572f | 2020-11-13 14:33:46 -0800 | [diff] [blame] | 35 | // Test option struct. |
| 36 | type TestOptions struct { |
| 37 | // If the test is a hostside(no device required) unittest that shall be run during presubmit check. |
| 38 | Unit_test *bool |
| 39 | } |
| 40 | |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 41 | type TestProperties struct { |
| 42 | // the name of the test configuration (for example "AndroidTest.xml") that should be |
| 43 | // installed with the module. |
Colin Cross | a638482 | 2020-06-09 15:09:22 -0700 | [diff] [blame] | 44 | Test_config *string `android:"path,arch_variant"` |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 45 | |
| 46 | // the name of the test configuration template (for example "AndroidTestTemplate.xml") that |
| 47 | // should be installed with the module. |
Colin Cross | a638482 | 2020-06-09 15:09:22 -0700 | [diff] [blame] | 48 | Test_config_template *string `android:"path,arch_variant"` |
Dan Shi | 3194912 | 2020-09-21 12:11:02 -0700 | [diff] [blame] | 49 | |
| 50 | // list of files or filegroup modules that provide data that should be installed alongside |
| 51 | // the test |
| 52 | Data []string `android:"path,arch_variant"` |
Dan Shi | d79572f | 2020-11-13 14:33:46 -0800 | [diff] [blame] | 53 | |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 54 | // list of java modules that provide data that should be installed alongside the test. |
| 55 | Java_data []string |
| 56 | |
Dan Shi | d79572f | 2020-11-13 14:33:46 -0800 | [diff] [blame] | 57 | // Test options. |
| 58 | Test_options TestOptions |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 61 | type testDecorator struct { |
| 62 | *binaryDecorator |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 63 | |
| 64 | testProperties TestProperties |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 65 | |
| 66 | testConfig android.Path |
Dan Shi | 3194912 | 2020-09-21 12:11:02 -0700 | [diff] [blame] | 67 | |
| 68 | data []android.DataPath |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | func (test *testDecorator) bootstrapperProps() []interface{} { |
| 72 | return append(test.binaryDecorator.bootstrapperProps(), &test.testProperties) |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 75 | func (test *testDecorator) install(ctx android.ModuleContext, file android.Path) { |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 76 | test.testConfig = tradefed.AutoGenPythonBinaryHostTestConfig(ctx, test.testProperties.Test_config, |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 77 | test.testProperties.Test_config_template, test.binaryDecorator.binaryProperties.Test_suites, |
| 78 | test.binaryDecorator.binaryProperties.Auto_gen_config) |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 79 | |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 80 | test.binaryDecorator.pythonInstaller.dir = "nativetest" |
| 81 | test.binaryDecorator.pythonInstaller.dir64 = "nativetest64" |
| 82 | |
| 83 | test.binaryDecorator.pythonInstaller.relative = ctx.ModuleName() |
| 84 | |
| 85 | test.binaryDecorator.pythonInstaller.install(ctx, file) |
Dan Shi | 3194912 | 2020-09-21 12:11:02 -0700 | [diff] [blame] | 86 | |
| 87 | dataSrcPaths := android.PathsForModuleSrc(ctx, test.testProperties.Data) |
| 88 | |
| 89 | for _, dataSrcPath := range dataSrcPaths { |
| 90 | test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath}) |
| 91 | } |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 92 | |
| 93 | // Emulate the data property for java_data dependencies. |
| 94 | for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) { |
| 95 | for _, javaDataSrcPath := range android.OutputFilesForModule(ctx, javaData, "") { |
| 96 | test.data = append(test.data, android.DataPath{SrcPath: javaDataSrcPath}) |
| 97 | } |
| 98 | } |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 101 | func NewTest(hod android.HostOrDeviceSupported) *Module { |
| 102 | module, binary := NewBinary(hod) |
| 103 | |
Wei Li | 664a4fd | 2021-10-07 06:26:48 +0000 | [diff] [blame^] | 104 | binary.pythonInstaller = NewPythonInstaller("nativetest", "nativetest64", module) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 105 | |
| 106 | test := &testDecorator{binaryDecorator: binary} |
Julien Desprez | 66534a0 | 2021-02-09 09:27:39 -0800 | [diff] [blame] | 107 | if hod == android.HostSupportedNoCross && test.testProperties.Test_options.Unit_test == nil { |
| 108 | test.testProperties.Test_options.Unit_test = proptools.BoolPtr(true) |
| 109 | } |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 110 | |
| 111 | module.bootstrapper = test |
| 112 | module.installer = test |
| 113 | |
| 114 | return module |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 117 | func PythonTestHostFactory() android.Module { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 118 | module := NewTest(android.HostSupportedNoCross) |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 119 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 120 | return module.init() |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 121 | } |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 122 | |
| 123 | func PythonTestFactory() android.Module { |
| 124 | module := NewTest(android.HostAndDeviceSupported) |
| 125 | module.multilib = android.MultilibBoth |
| 126 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 127 | return module.init() |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 128 | } |