blob: 250b7657efc525f32b4d48a014d7db9f63993c33 [file] [log] [blame]
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -07001// 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
15package rust
16
17import (
Julien Desprez84c94942021-01-15 15:10:01 -080018 "github.com/google/blueprint/proptools"
19
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070020 "android/soong/android"
Ivan Lozano4e5f07d2021-11-04 14:09:38 -040021 "android/soong/cc"
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070022 "android/soong/tradefed"
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070023)
24
Dan Shid79572f2020-11-13 14:33:46 -080025// Test option struct.
26type TestOptions struct {
27 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
28 Unit_test *bool
29}
30
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070031type TestProperties struct {
Ivan Lozanofc80fe72020-06-11 13:25:36 -040032 // Disables the creation of a test-specific directory when used with
33 // relative_install_path. Useful if several tests need to be in the same
34 // directory, but test_per_src doesn't work.
35 No_named_install_directory *bool
36
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070037 // the name of the test configuration (for example "AndroidTest.xml") that should be
38 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070039 Test_config *string `android:"path,arch_variant"`
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070040
41 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
42 // should be installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070043 Test_config_template *string `android:"path,arch_variant"`
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070044
45 // list of compatibility suites (for example "cts", "vts") that the module should be
46 // installed into.
47 Test_suites []string `android:"arch_variant"`
48
Ivan Lozano9da4aa82021-01-29 12:48:05 -050049 // list of files or filegroup modules that provide data that should be installed alongside
50 // the test
51 Data []string `android:"path,arch_variant"`
52
Ivan Lozano4e5f07d2021-11-04 14:09:38 -040053 // list of shared library modules that should be installed alongside the test
54 Data_libs []string `android:"arch_variant"`
55
56 // list of binary modules that should be installed alongside the test
57 Data_bins []string `android:"arch_variant"`
58
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070059 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
60 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
61 // explicitly.
62 Auto_gen_config *bool
Stephen Crane02a623d2020-04-25 21:40:35 -070063
64 // if set, build with the standard Rust test harness. Defaults to true.
65 Test_harness *bool
Dan Shid79572f2020-11-13 14:33:46 -080066
67 // Test options.
68 Test_options TestOptions
Ivan Lozanoba222612021-09-21 10:49:13 -040069
70 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
71 // with root permission.
72 Require_root *bool
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070073}
74
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070075// A test module is a binary module with extra --test compiler flag
76// and different default installation directory.
77// In golang, inheriance is written as a component.
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070078type testDecorator struct {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070079 *binaryDecorator
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070080 Properties TestProperties
81 testConfig android.Path
Ivan Lozano9da4aa82021-01-29 12:48:05 -050082
83 data []android.DataPath
84}
85
86func (test *testDecorator) dataPaths() []android.DataPath {
87 return test.data
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070088}
89
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040090func (test *testDecorator) nativeCoverage() bool {
91 return true
92}
93
Stephen Crane02a623d2020-04-25 21:40:35 -070094func (test *testDecorator) testHarness() bool {
95 return BoolDefault(test.Properties.Test_harness, true)
96}
97
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070098func NewRustTest(hod android.HostOrDeviceSupported) (*Module, *testDecorator) {
Chih-Hung Hsiehe728a892020-06-16 01:25:27 -070099 // Build both 32 and 64 targets for device tests.
100 // Cannot build both for host tests yet if the test depends on
101 // something like proc-macro2 that cannot be built for both.
102 multilib := android.MultilibBoth
103 if hod != android.DeviceSupported && hod != android.HostAndDeviceSupported {
104 multilib = android.MultilibFirst
105 }
106 module := newModule(hod, multilib)
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700107
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700108 test := &testDecorator{
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700109 binaryDecorator: &binaryDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800110 baseCompiler: NewBaseCompiler("nativetest", "nativetest64", InstallInData),
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700111 },
112 }
113
114 module.compiler = test
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700115 return module, test
116}
117
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700118func (test *testDecorator) compilerProps() []interface{} {
119 return append(test.binaryDecorator.compilerProps(), &test.Properties)
120}
121
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +0200122func (test *testDecorator) install(ctx ModuleContext) {
Ivan Lozanoba222612021-09-21 10:49:13 -0400123 testInstallBase := "/data/local/tests/unrestricted"
124 if ctx.RustModule().InVendor() || ctx.RustModule().UseVndk() {
125 testInstallBase = "/data/local/tests/vendor"
126 }
127
128 var configs []tradefed.Config
129 if Bool(test.Properties.Require_root) {
130 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
131 } else {
132 var options []tradefed.Option
133 options = append(options, tradefed.Option{Name: "force-root", Value: "false"})
134 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
135 }
136
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400137 test.testConfig = tradefed.AutoGenRustTestConfig(ctx,
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700138 test.Properties.Test_config,
139 test.Properties.Test_config_template,
140 test.Properties.Test_suites,
Ivan Lozanoba222612021-09-21 10:49:13 -0400141 configs,
142 test.Properties.Auto_gen_config,
143 testInstallBase)
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400144
Ivan Lozano9da4aa82021-01-29 12:48:05 -0500145 dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
146
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400147 ctx.VisitDirectDepsWithTag(dataLibDepTag, func(dep android.Module) {
148 depName := ctx.OtherModuleName(dep)
149 linkableDep, ok := dep.(cc.LinkableInterface)
150 if !ok {
151 ctx.ModuleErrorf("data_lib %q is not a linkable module", depName)
152 }
153 if linkableDep.OutputFile().Valid() {
154 test.data = append(test.data,
155 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
156 RelativeInstallPath: linkableDep.RelativeInstallPath()})
157 }
158 })
159
160 ctx.VisitDirectDepsWithTag(dataBinDepTag, func(dep android.Module) {
161 depName := ctx.OtherModuleName(dep)
162 linkableDep, ok := dep.(cc.LinkableInterface)
163 if !ok {
164 ctx.ModuleErrorf("data_bin %q is not a linkable module", depName)
165 }
166 if linkableDep.OutputFile().Valid() {
167 test.data = append(test.data,
168 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
169 RelativeInstallPath: linkableDep.RelativeInstallPath()})
170 }
171 })
172
Ivan Lozano9da4aa82021-01-29 12:48:05 -0500173 for _, dataSrcPath := range dataSrcPaths {
174 test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
175 }
176
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800177 // default relative install path is module name
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400178 if !Bool(test.Properties.No_named_install_directory) {
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800179 test.baseCompiler.relative = ctx.ModuleName()
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400180 } else if String(test.baseCompiler.Properties.Relative_install_path) == "" {
181 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800182 }
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400183
Julien Desprez84c94942021-01-15 15:10:01 -0800184 if ctx.Host() && test.Properties.Test_options.Unit_test == nil {
185 test.Properties.Test_options.Unit_test = proptools.BoolPtr(true)
186 }
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +0200187 test.binaryDecorator.install(ctx)
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700188}
189
190func (test *testDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700191 flags = test.binaryDecorator.compilerFlags(ctx, flags)
Stephen Crane02a623d2020-04-25 21:40:35 -0700192 if test.testHarness() {
193 flags.RustFlags = append(flags.RustFlags, "--test")
194 }
Jeff Vander Stoepbf7a9022021-01-26 14:35:38 +0100195 if ctx.Device() {
196 flags.RustFlags = append(flags.RustFlags, "-Z panic_abort_tests")
197 }
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700198 return flags
199}
200
Liz Kammer356f7d42021-01-26 09:18:53 -0500201func (test *testDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700202 return rlibAutoDep
203}
204
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700205func init() {
206 // Rust tests are binary files built with --test.
207 android.RegisterModuleType("rust_test", RustTestFactory)
208 android.RegisterModuleType("rust_test_host", RustTestHostFactory)
209}
210
211func RustTestFactory() android.Module {
212 module, _ := NewRustTest(android.HostAndDeviceSupported)
Ivan Lozanof5c98a22021-12-07 10:17:00 -0500213
214 // NewRustTest will set MultilibBoth true, however the host variant
215 // cannot produce the non-primary target. Therefore, add the
216 // rustTestHostMultilib load hook to set MultilibFirst for the
217 // host target.
218 android.AddLoadHook(module, rustTestHostMultilib)
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700219 return module.Init()
220}
221
222func RustTestHostFactory() android.Module {
223 module, _ := NewRustTest(android.HostSupported)
224 return module.Init()
225}
Ivan Lozano2b081132020-09-08 12:46:52 -0400226
Ivan Lozanodd055472020-09-28 13:22:45 -0400227func (test *testDecorator) stdLinkage(ctx *depsContext) RustLinkage {
228 return RlibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400229}
Ivan Lozano3ee74c82021-07-15 15:44:10 -0400230
231func (test *testDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
232 deps = test.binaryDecorator.compilerDeps(ctx, deps)
233
234 deps.Rustlibs = append(deps.Rustlibs, "libtest")
235
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400236 deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...)
237 deps.DataBins = append(deps.DataBins, test.Properties.Data_bins...)
238
Ivan Lozano3ee74c82021-07-15 15:44:10 -0400239 return deps
240}
Ivan Lozano62cd0382021-11-01 10:27:54 -0400241
242func (test *testDecorator) testBinary() bool {
243 return true
244}
Ivan Lozanof5c98a22021-12-07 10:17:00 -0500245
246func rustTestHostMultilib(ctx android.LoadHookContext) {
247 type props struct {
248 Target struct {
249 Host struct {
250 Compile_multilib *string
251 }
252 }
253 }
254 p := &props{}
255 p.Target.Host.Compile_multilib = proptools.StringPtr("first")
256 ctx.AppendProperties(p)
257}