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 ( |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 18 | "android/soong/android" |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 19 | "android/soong/tradefed" |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 22 | type TestProperties struct { |
Ivan Lozano | fc80fe7 | 2020-06-11 13:25:36 -0400 | [diff] [blame] | 23 | // Disables the creation of a test-specific directory when used with |
| 24 | // relative_install_path. Useful if several tests need to be in the same |
| 25 | // directory, but test_per_src doesn't work. |
| 26 | No_named_install_directory *bool |
| 27 | |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 28 | // the name of the test configuration (for example "AndroidTest.xml") that should be |
| 29 | // installed with the module. |
Colin Cross | a638482 | 2020-06-09 15:09:22 -0700 | [diff] [blame] | 30 | Test_config *string `android:"path,arch_variant"` |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 31 | |
| 32 | // the name of the test configuration template (for example "AndroidTestTemplate.xml") that |
| 33 | // should be installed with the module. |
Colin Cross | a638482 | 2020-06-09 15:09:22 -0700 | [diff] [blame] | 34 | Test_config_template *string `android:"path,arch_variant"` |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 35 | |
| 36 | // list of compatibility suites (for example "cts", "vts") that the module should be |
| 37 | // installed into. |
| 38 | Test_suites []string `android:"arch_variant"` |
| 39 | |
| 40 | // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml |
| 41 | // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true |
| 42 | // explicitly. |
| 43 | Auto_gen_config *bool |
Stephen Crane | 02a623d | 2020-04-25 21:40:35 -0700 | [diff] [blame^] | 44 | |
| 45 | // if set, build with the standard Rust test harness. Defaults to true. |
| 46 | Test_harness *bool |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 49 | // A test module is a binary module with extra --test compiler flag |
| 50 | // and different default installation directory. |
| 51 | // In golang, inheriance is written as a component. |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 52 | type testDecorator struct { |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 53 | *binaryDecorator |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 54 | Properties TestProperties |
| 55 | testConfig android.Path |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 58 | func (test *testDecorator) nativeCoverage() bool { |
| 59 | return true |
| 60 | } |
| 61 | |
Stephen Crane | 02a623d | 2020-04-25 21:40:35 -0700 | [diff] [blame^] | 62 | func (test *testDecorator) testHarness() bool { |
| 63 | return BoolDefault(test.Properties.Test_harness, true) |
| 64 | } |
| 65 | |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 66 | func NewRustTest(hod android.HostOrDeviceSupported) (*Module, *testDecorator) { |
Chih-Hung Hsieh | e728a89 | 2020-06-16 01:25:27 -0700 | [diff] [blame] | 67 | // Build both 32 and 64 targets for device tests. |
| 68 | // Cannot build both for host tests yet if the test depends on |
| 69 | // something like proc-macro2 that cannot be built for both. |
| 70 | multilib := android.MultilibBoth |
| 71 | if hod != android.DeviceSupported && hod != android.HostAndDeviceSupported { |
| 72 | multilib = android.MultilibFirst |
| 73 | } |
| 74 | module := newModule(hod, multilib) |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 75 | |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 76 | test := &testDecorator{ |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 77 | binaryDecorator: &binaryDecorator{ |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 78 | baseCompiler: NewBaseCompiler("nativetest", "nativetest64", InstallInData), |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 79 | }, |
| 80 | } |
| 81 | |
| 82 | module.compiler = test |
Ivan Lozano | fc80fe7 | 2020-06-11 13:25:36 -0400 | [diff] [blame] | 83 | module.AddProperties(&test.Properties) |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 84 | return module, test |
| 85 | } |
| 86 | |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 87 | func (test *testDecorator) compilerProps() []interface{} { |
| 88 | return append(test.binaryDecorator.compilerProps(), &test.Properties) |
| 89 | } |
| 90 | |
| 91 | func (test *testDecorator) install(ctx ModuleContext, file android.Path) { |
Ivan Lozano | fc80fe7 | 2020-06-11 13:25:36 -0400 | [diff] [blame] | 92 | test.testConfig = tradefed.AutoGenRustTestConfig(ctx, |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 93 | test.Properties.Test_config, |
| 94 | test.Properties.Test_config_template, |
| 95 | test.Properties.Test_suites, |
Ivan Lozano | fc80fe7 | 2020-06-11 13:25:36 -0400 | [diff] [blame] | 96 | nil, |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 97 | test.Properties.Auto_gen_config) |
Ivan Lozano | fc80fe7 | 2020-06-11 13:25:36 -0400 | [diff] [blame] | 98 | |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 99 | // default relative install path is module name |
Ivan Lozano | fc80fe7 | 2020-06-11 13:25:36 -0400 | [diff] [blame] | 100 | if !Bool(test.Properties.No_named_install_directory) { |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 101 | test.baseCompiler.relative = ctx.ModuleName() |
Ivan Lozano | fc80fe7 | 2020-06-11 13:25:36 -0400 | [diff] [blame] | 102 | } else if String(test.baseCompiler.Properties.Relative_install_path) == "" { |
| 103 | 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] | 104 | } |
Ivan Lozano | fc80fe7 | 2020-06-11 13:25:36 -0400 | [diff] [blame] | 105 | |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 106 | test.binaryDecorator.install(ctx, file) |
| 107 | } |
| 108 | |
| 109 | func (test *testDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 110 | flags = test.binaryDecorator.compilerFlags(ctx, flags) |
Stephen Crane | 02a623d | 2020-04-25 21:40:35 -0700 | [diff] [blame^] | 111 | if test.testHarness() { |
| 112 | flags.RustFlags = append(flags.RustFlags, "--test") |
| 113 | } |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 114 | return flags |
| 115 | } |
| 116 | |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 117 | func (test *testDecorator) autoDep() autoDep { |
| 118 | return rlibAutoDep |
| 119 | } |
| 120 | |
Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 121 | func init() { |
| 122 | // Rust tests are binary files built with --test. |
| 123 | android.RegisterModuleType("rust_test", RustTestFactory) |
| 124 | android.RegisterModuleType("rust_test_host", RustTestHostFactory) |
| 125 | } |
| 126 | |
| 127 | func RustTestFactory() android.Module { |
| 128 | module, _ := NewRustTest(android.HostAndDeviceSupported) |
| 129 | return module.Init() |
| 130 | } |
| 131 | |
| 132 | func RustTestHostFactory() android.Module { |
| 133 | module, _ := NewRustTest(android.HostSupported) |
| 134 | return module.Init() |
| 135 | } |