Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Android Open Source Project |
| 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 rust |
| 16 | |
| 17 | import ( |
Julien Desprez | 84c9494 | 2021-01-15 15:10:01 -0800 | [diff] [blame] | 18 | "github.com/google/blueprint/proptools" |
| 19 | |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 20 | "android/soong/android" |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 21 | "android/soong/tradefed" |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 22 | ) |
| 23 | |
Dan Shi | d79572f | 2020-11-13 14:33:46 -0800 | [diff] [blame] | 24 | // Test option struct. |
| 25 | type TestOptions struct { |
| 26 | // If the test is a hostside(no device required) unittest that shall be run during presubmit check. |
| 27 | Unit_test *bool |
| 28 | } |
| 29 | |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 30 | type TestProperties struct { |
Ivan Lozano | fc80fe7 | 2020-06-11 13:25:36 -0400 | [diff] [blame] | 31 | // Disables the creation of a test-specific directory when used with |
| 32 | // relative_install_path. Useful if several tests need to be in the same |
| 33 | // directory, but test_per_src doesn't work. |
| 34 | No_named_install_directory *bool |
| 35 | |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 36 | // the name of the test configuration (for example "AndroidTest.xml") that should be |
| 37 | // installed with the module. |
Colin Cross | a638482 | 2020-06-09 15:09:22 -0700 | [diff] [blame] | 38 | Test_config *string `android:"path,arch_variant"` |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 39 | |
| 40 | // the name of the test configuration template (for example "AndroidTestTemplate.xml") that |
| 41 | // should be installed with the module. |
Colin Cross | a638482 | 2020-06-09 15:09:22 -0700 | [diff] [blame] | 42 | Test_config_template *string `android:"path,arch_variant"` |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 43 | |
| 44 | // list of compatibility suites (for example "cts", "vts") that the module should be |
| 45 | // installed into. |
| 46 | Test_suites []string `android:"arch_variant"` |
| 47 | |
Ivan Lozano | 9da4aa8 | 2021-01-29 12:48:05 -0500 | [diff] [blame] | 48 | // list of files or filegroup modules that provide data that should be installed alongside |
| 49 | // the test |
| 50 | Data []string `android:"path,arch_variant"` |
| 51 | |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 52 | // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml |
| 53 | // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true |
| 54 | // explicitly. |
| 55 | Auto_gen_config *bool |
Stephen Crane | 02a623d | 2020-04-25 21:40:35 -0700 | [diff] [blame] | 56 | |
| 57 | // if set, build with the standard Rust test harness. Defaults to true. |
| 58 | Test_harness *bool |
Dan Shi | d79572f | 2020-11-13 14:33:46 -0800 | [diff] [blame] | 59 | |
| 60 | // Test options. |
| 61 | Test_options TestOptions |
Ivan Lozano | ba22261 | 2021-09-21 10:49:13 -0400 | [diff] [blame] | 62 | |
| 63 | // Add RootTargetPreparer to auto generated test config. This guarantees the test to run |
| 64 | // with root permission. |
| 65 | Require_root *bool |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 68 | // A test module is a binary module with extra --test compiler flag |
| 69 | // and different default installation directory. |
| 70 | // In golang, inheriance is written as a component. |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 71 | type testDecorator struct { |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 72 | *binaryDecorator |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 73 | Properties TestProperties |
| 74 | testConfig android.Path |
Ivan Lozano | 9da4aa8 | 2021-01-29 12:48:05 -0500 | [diff] [blame] | 75 | |
| 76 | data []android.DataPath |
| 77 | } |
| 78 | |
| 79 | func (test *testDecorator) dataPaths() []android.DataPath { |
| 80 | return test.data |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 83 | func (test *testDecorator) nativeCoverage() bool { |
| 84 | return true |
| 85 | } |
| 86 | |
Stephen Crane | 02a623d | 2020-04-25 21:40:35 -0700 | [diff] [blame] | 87 | func (test *testDecorator) testHarness() bool { |
| 88 | return BoolDefault(test.Properties.Test_harness, true) |
| 89 | } |
| 90 | |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 91 | func NewRustTest(hod android.HostOrDeviceSupported) (*Module, *testDecorator) { |
Chih-Hung Hsieh | e728a89 | 2020-06-16 01:25:27 -0700 | [diff] [blame] | 92 | // Build both 32 and 64 targets for device tests. |
| 93 | // Cannot build both for host tests yet if the test depends on |
| 94 | // something like proc-macro2 that cannot be built for both. |
| 95 | multilib := android.MultilibBoth |
| 96 | if hod != android.DeviceSupported && hod != android.HostAndDeviceSupported { |
| 97 | multilib = android.MultilibFirst |
| 98 | } |
| 99 | module := newModule(hod, multilib) |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 100 | |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 101 | test := &testDecorator{ |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 102 | binaryDecorator: &binaryDecorator{ |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 103 | baseCompiler: NewBaseCompiler("nativetest", "nativetest64", InstallInData), |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 104 | }, |
| 105 | } |
| 106 | |
| 107 | module.compiler = test |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 108 | return module, test |
| 109 | } |
| 110 | |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 111 | func (test *testDecorator) compilerProps() []interface{} { |
| 112 | return append(test.binaryDecorator.compilerProps(), &test.Properties) |
| 113 | } |
| 114 | |
ThiƩbaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 115 | func (test *testDecorator) install(ctx ModuleContext) { |
Ivan Lozano | ba22261 | 2021-09-21 10:49:13 -0400 | [diff] [blame] | 116 | testInstallBase := "/data/local/tests/unrestricted" |
| 117 | if ctx.RustModule().InVendor() || ctx.RustModule().UseVndk() { |
| 118 | testInstallBase = "/data/local/tests/vendor" |
| 119 | } |
| 120 | |
| 121 | var configs []tradefed.Config |
| 122 | if Bool(test.Properties.Require_root) { |
| 123 | configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil}) |
| 124 | } else { |
| 125 | var options []tradefed.Option |
| 126 | options = append(options, tradefed.Option{Name: "force-root", Value: "false"}) |
| 127 | configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options}) |
| 128 | } |
| 129 | |
Ivan Lozano | fc80fe7 | 2020-06-11 13:25:36 -0400 | [diff] [blame] | 130 | test.testConfig = tradefed.AutoGenRustTestConfig(ctx, |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 131 | test.Properties.Test_config, |
| 132 | test.Properties.Test_config_template, |
| 133 | test.Properties.Test_suites, |
Ivan Lozano | ba22261 | 2021-09-21 10:49:13 -0400 | [diff] [blame] | 134 | configs, |
| 135 | test.Properties.Auto_gen_config, |
| 136 | testInstallBase) |
Ivan Lozano | fc80fe7 | 2020-06-11 13:25:36 -0400 | [diff] [blame] | 137 | |
Ivan Lozano | 9da4aa8 | 2021-01-29 12:48:05 -0500 | [diff] [blame] | 138 | dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data) |
| 139 | |
| 140 | for _, dataSrcPath := range dataSrcPaths { |
| 141 | test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath}) |
| 142 | } |
| 143 | |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 144 | // default relative install path is module name |
Ivan Lozano | fc80fe7 | 2020-06-11 13:25:36 -0400 | [diff] [blame] | 145 | if !Bool(test.Properties.No_named_install_directory) { |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 146 | test.baseCompiler.relative = ctx.ModuleName() |
Ivan Lozano | fc80fe7 | 2020-06-11 13:25:36 -0400 | [diff] [blame] | 147 | } else if String(test.baseCompiler.Properties.Relative_install_path) == "" { |
| 148 | ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set") |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 149 | } |
Ivan Lozano | fc80fe7 | 2020-06-11 13:25:36 -0400 | [diff] [blame] | 150 | |
Julien Desprez | 84c9494 | 2021-01-15 15:10:01 -0800 | [diff] [blame] | 151 | if ctx.Host() && test.Properties.Test_options.Unit_test == nil { |
| 152 | test.Properties.Test_options.Unit_test = proptools.BoolPtr(true) |
| 153 | } |
ThiƩbaud Weksteen | fabaff6 | 2020-08-27 13:48:36 +0200 | [diff] [blame] | 154 | test.binaryDecorator.install(ctx) |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | func (test *testDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 158 | flags = test.binaryDecorator.compilerFlags(ctx, flags) |
Stephen Crane | 02a623d | 2020-04-25 21:40:35 -0700 | [diff] [blame] | 159 | if test.testHarness() { |
| 160 | flags.RustFlags = append(flags.RustFlags, "--test") |
| 161 | } |
Jeff Vander Stoep | bf7a902 | 2021-01-26 14:35:38 +0100 | [diff] [blame] | 162 | if ctx.Device() { |
| 163 | flags.RustFlags = append(flags.RustFlags, "-Z panic_abort_tests") |
| 164 | } |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 165 | return flags |
| 166 | } |
| 167 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 168 | func (test *testDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep { |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 169 | return rlibAutoDep |
| 170 | } |
| 171 | |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 172 | func init() { |
| 173 | // Rust tests are binary files built with --test. |
| 174 | android.RegisterModuleType("rust_test", RustTestFactory) |
| 175 | android.RegisterModuleType("rust_test_host", RustTestHostFactory) |
| 176 | } |
| 177 | |
| 178 | func RustTestFactory() android.Module { |
| 179 | module, _ := NewRustTest(android.HostAndDeviceSupported) |
| 180 | return module.Init() |
| 181 | } |
| 182 | |
| 183 | func RustTestHostFactory() android.Module { |
| 184 | module, _ := NewRustTest(android.HostSupported) |
| 185 | return module.Init() |
| 186 | } |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 187 | |
Ivan Lozano | dd05547 | 2020-09-28 13:22:45 -0400 | [diff] [blame] | 188 | func (test *testDecorator) stdLinkage(ctx *depsContext) RustLinkage { |
| 189 | return RlibLinkage |
Ivan Lozano | 2b08113 | 2020-09-08 12:46:52 -0400 | [diff] [blame] | 190 | } |
Ivan Lozano | 3ee74c8 | 2021-07-15 15:44:10 -0400 | [diff] [blame] | 191 | |
| 192 | func (test *testDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps { |
| 193 | deps = test.binaryDecorator.compilerDeps(ctx, deps) |
| 194 | |
| 195 | deps.Rustlibs = append(deps.Rustlibs, "libtest") |
| 196 | |
| 197 | return deps |
| 198 | } |