| 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 ( | 
|  | 18 | "path/filepath" | 
|  | 19 | "strings" | 
|  | 20 |  | 
|  | 21 | "android/soong/android" | 
| Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 22 | "android/soong/tradefed" | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 23 | ) | 
|  | 24 |  | 
| Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 25 | type TestProperties struct { | 
|  | 26 | // the name of the test configuration (for example "AndroidTest.xml") that should be | 
|  | 27 | // installed with the module. | 
|  | 28 | Test_config *string `android:"arch_variant"` | 
|  | 29 |  | 
|  | 30 | // the name of the test configuration template (for example "AndroidTestTemplate.xml") that | 
|  | 31 | // should be installed with the module. | 
|  | 32 | Test_config_template *string `android:"arch_variant"` | 
|  | 33 |  | 
|  | 34 | // list of compatibility suites (for example "cts", "vts") that the module should be | 
|  | 35 | // installed into. | 
|  | 36 | Test_suites []string `android:"arch_variant"` | 
|  | 37 |  | 
|  | 38 | // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml | 
|  | 39 | // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true | 
|  | 40 | // explicitly. | 
|  | 41 | Auto_gen_config *bool | 
|  | 42 | } | 
|  | 43 |  | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 44 | // A test module is a binary module with extra --test compiler flag | 
|  | 45 | // and different default installation directory. | 
|  | 46 | // In golang, inheriance is written as a component. | 
| Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 47 | type testDecorator struct { | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 48 | *binaryDecorator | 
| Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 49 | Properties TestProperties | 
|  | 50 | testConfig android.Path | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 51 | } | 
|  | 52 |  | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 53 | func (test *testDecorator) nativeCoverage() bool { | 
|  | 54 | return true | 
|  | 55 | } | 
|  | 56 |  | 
| Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 57 | func NewRustTest(hod android.HostOrDeviceSupported) (*Module, *testDecorator) { | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 58 | module := newModule(hod, android.MultilibFirst) | 
|  | 59 |  | 
| Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 60 | test := &testDecorator{ | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 61 | binaryDecorator: &binaryDecorator{ | 
| Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 62 | baseCompiler: NewBaseCompiler("nativetest", "nativetest64", InstallInData), | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 63 | }, | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | module.compiler = test | 
|  | 67 |  | 
|  | 68 | return module, test | 
|  | 69 | } | 
|  | 70 |  | 
| Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 71 | func (test *testDecorator) compilerProps() []interface{} { | 
|  | 72 | return append(test.binaryDecorator.compilerProps(), &test.Properties) | 
|  | 73 | } | 
|  | 74 |  | 
| Chih-Hung Hsieh | ede57ae | 2019-11-22 20:22:35 -0800 | [diff] [blame] | 75 | func (test *testDecorator) getMutatedModuleSubName(moduleName string) string { | 
|  | 76 | stem := String(test.baseCompiler.Properties.Stem) | 
|  | 77 | if stem != "" && !strings.HasSuffix(moduleName, "_"+stem) { | 
|  | 78 | // Avoid repeated suffix in the module name. | 
|  | 79 | return "_" + stem | 
|  | 80 | } | 
|  | 81 | return "" | 
|  | 82 | } | 
|  | 83 |  | 
| Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 84 | func (test *testDecorator) install(ctx ModuleContext, file android.Path) { | 
| Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 85 | name := ctx.ModuleName() | 
|  | 86 | path := test.baseCompiler.relativeInstallPath() | 
|  | 87 | // on device, use mutated module name | 
|  | 88 | name = name + test.getMutatedModuleSubName(name) | 
|  | 89 | if !ctx.Device() { // on host, use mutated module name + arch type + stem name | 
|  | 90 | stem := String(test.baseCompiler.Properties.Stem) | 
|  | 91 | if stem == "" { | 
|  | 92 | stem = name | 
| Chih-Hung Hsieh | ede57ae | 2019-11-22 20:22:35 -0800 | [diff] [blame] | 93 | } | 
| Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 94 | name = filepath.Join(name, ctx.Arch().ArchType.String(), stem) | 
| Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 95 | } | 
| Chih-Hung Hsieh | ede57ae | 2019-11-22 20:22:35 -0800 | [diff] [blame] | 96 | test.testConfig = tradefed.AutoGenRustTestConfig(ctx, name, | 
| Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 97 | test.Properties.Test_config, | 
|  | 98 | test.Properties.Test_config_template, | 
|  | 99 | test.Properties.Test_suites, | 
|  | 100 | test.Properties.Auto_gen_config) | 
| Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 101 | // default relative install path is module name | 
|  | 102 | if path == "" { | 
|  | 103 | test.baseCompiler.relative = ctx.ModuleName() | 
|  | 104 | } | 
| Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 105 | test.binaryDecorator.install(ctx, file) | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | func (test *testDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags { | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 109 | flags = test.binaryDecorator.compilerFlags(ctx, flags) | 
|  | 110 | flags.RustFlags = append(flags.RustFlags, "--test") | 
|  | 111 | return flags | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | func init() { | 
|  | 115 | // Rust tests are binary files built with --test. | 
|  | 116 | android.RegisterModuleType("rust_test", RustTestFactory) | 
|  | 117 | android.RegisterModuleType("rust_test_host", RustTestHostFactory) | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | func RustTestFactory() android.Module { | 
|  | 121 | module, _ := NewRustTest(android.HostAndDeviceSupported) | 
|  | 122 | return module.Init() | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | func RustTestHostFactory() android.Module { | 
|  | 126 | module, _ := NewRustTest(android.HostSupported) | 
|  | 127 | return module.Init() | 
|  | 128 | } | 
|  | 129 |  | 
| Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 130 | func (test *testDecorator) testPerSrc() bool { | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 131 | return true | 
|  | 132 | } | 
|  | 133 |  | 
| Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 134 | func (test *testDecorator) srcs() []string { | 
|  | 135 | return test.binaryDecorator.Properties.Srcs | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 136 | } | 
|  | 137 |  | 
| Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 138 | func (test *testDecorator) setSrc(name, src string) { | 
|  | 139 | test.binaryDecorator.Properties.Srcs = []string{src} | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 140 | test.baseCompiler.Properties.Stem = StringPtr(name) | 
|  | 141 | } | 
|  | 142 |  | 
| Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 143 | func (test *testDecorator) unsetSrc() { | 
|  | 144 | test.binaryDecorator.Properties.Srcs = nil | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 145 | test.baseCompiler.Properties.Stem = StringPtr("") | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | type testPerSrc interface { | 
|  | 149 | testPerSrc() bool | 
|  | 150 | srcs() []string | 
|  | 151 | setSrc(string, string) | 
|  | 152 | unsetSrc() | 
|  | 153 | } | 
|  | 154 |  | 
| Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 155 | var _ testPerSrc = (*testDecorator)(nil) | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 156 |  | 
|  | 157 | func TestPerSrcMutator(mctx android.BottomUpMutatorContext) { | 
|  | 158 | if m, ok := mctx.Module().(*Module); ok { | 
|  | 159 | if test, ok := m.compiler.(testPerSrc); ok { | 
|  | 160 | numTests := len(test.srcs()) | 
|  | 161 | if test.testPerSrc() && numTests > 0 { | 
|  | 162 | if duplicate, found := android.CheckDuplicate(test.srcs()); found { | 
|  | 163 | mctx.PropertyErrorf("srcs", "found a duplicate entry %q", duplicate) | 
|  | 164 | return | 
|  | 165 | } | 
| Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 166 | // Rust compiler always compiles one source file at a time and | 
|  | 167 | // uses the crate name as output file name. | 
|  | 168 | // Cargo uses the test source file name as default crate name, | 
|  | 169 | // but that can be redefined. | 
|  | 170 | // So when there are multiple source files, the source file names will | 
|  | 171 | // be the output file names, but when there is only one test file, | 
|  | 172 | // use the crate name. | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 173 | testNames := make([]string, numTests) | 
|  | 174 | for i, src := range test.srcs() { | 
|  | 175 | testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src)) | 
|  | 176 | } | 
| Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 177 | crateName := m.compiler.crateName() | 
|  | 178 | if numTests == 1 && crateName != "" { | 
|  | 179 | testNames[0] = crateName | 
|  | 180 | } | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 181 | // TODO(chh): Add an "all tests" variation like cc/test.go? | 
|  | 182 | tests := mctx.CreateLocalVariations(testNames...) | 
|  | 183 | for i, src := range test.srcs() { | 
|  | 184 | tests[i].(*Module).compiler.(testPerSrc).setSrc(testNames[i], src) | 
|  | 185 | } | 
|  | 186 | } | 
|  | 187 | } | 
|  | 188 | } | 
|  | 189 | } |