blob: 6e539359531c35606eae96edee8433a856d5cdbd [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 (
Jooyung Han10bea7d2022-04-29 02:04:30 +090018 "path/filepath"
19
Julien Desprez84c94942021-01-15 15:10:01 -080020 "github.com/google/blueprint/proptools"
21
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070022 "android/soong/android"
Ivan Lozano4e5f07d2021-11-04 14:09:38 -040023 "android/soong/cc"
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070024 "android/soong/tradefed"
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070025)
26
Dan Shid79572f2020-11-13 14:33:46 -080027// Test option struct.
28type TestOptions struct {
29 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
30 Unit_test *bool
31}
32
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070033type TestProperties struct {
Ivan Lozanofc80fe72020-06-11 13:25:36 -040034 // Disables the creation of a test-specific directory when used with
35 // relative_install_path. Useful if several tests need to be in the same
36 // directory, but test_per_src doesn't work.
37 No_named_install_directory *bool
38
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070039 // the name of the test configuration (for example "AndroidTest.xml") that should be
40 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070041 Test_config *string `android:"path,arch_variant"`
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070042
43 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
44 // should be installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070045 Test_config_template *string `android:"path,arch_variant"`
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070046
47 // list of compatibility suites (for example "cts", "vts") that the module should be
48 // installed into.
49 Test_suites []string `android:"arch_variant"`
50
Ivan Lozano9da4aa82021-01-29 12:48:05 -050051 // list of files or filegroup modules that provide data that should be installed alongside
52 // the test
53 Data []string `android:"path,arch_variant"`
54
Ivan Lozano4e5f07d2021-11-04 14:09:38 -040055 // list of shared library modules that should be installed alongside the test
56 Data_libs []string `android:"arch_variant"`
57
58 // list of binary modules that should be installed alongside the test
59 Data_bins []string `android:"arch_variant"`
60
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070061 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
62 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
63 // explicitly.
64 Auto_gen_config *bool
Stephen Crane02a623d2020-04-25 21:40:35 -070065
66 // if set, build with the standard Rust test harness. Defaults to true.
67 Test_harness *bool
Dan Shid79572f2020-11-13 14:33:46 -080068
69 // Test options.
70 Test_options TestOptions
Ivan Lozanoba222612021-09-21 10:49:13 -040071
72 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
73 // with root permission.
74 Require_root *bool
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070075}
76
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070077// A test module is a binary module with extra --test compiler flag
78// and different default installation directory.
79// In golang, inheriance is written as a component.
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070080type testDecorator struct {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070081 *binaryDecorator
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070082 Properties TestProperties
83 testConfig android.Path
Ivan Lozano9da4aa82021-01-29 12:48:05 -050084
85 data []android.DataPath
86}
87
88func (test *testDecorator) dataPaths() []android.DataPath {
89 return test.data
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070090}
91
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040092func (test *testDecorator) nativeCoverage() bool {
93 return true
94}
95
Stephen Crane02a623d2020-04-25 21:40:35 -070096func (test *testDecorator) testHarness() bool {
97 return BoolDefault(test.Properties.Test_harness, true)
98}
99
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700100func NewRustTest(hod android.HostOrDeviceSupported) (*Module, *testDecorator) {
Chih-Hung Hsiehe728a892020-06-16 01:25:27 -0700101 // Build both 32 and 64 targets for device tests.
102 // Cannot build both for host tests yet if the test depends on
103 // something like proc-macro2 that cannot be built for both.
104 multilib := android.MultilibBoth
105 if hod != android.DeviceSupported && hod != android.HostAndDeviceSupported {
106 multilib = android.MultilibFirst
107 }
108 module := newModule(hod, multilib)
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700109
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700110 test := &testDecorator{
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700111 binaryDecorator: &binaryDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800112 baseCompiler: NewBaseCompiler("nativetest", "nativetest64", InstallInData),
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700113 },
114 }
115
116 module.compiler = test
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700117 return module, test
118}
119
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700120func (test *testDecorator) compilerProps() []interface{} {
121 return append(test.binaryDecorator.compilerProps(), &test.Properties)
122}
123
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +0200124func (test *testDecorator) install(ctx ModuleContext) {
Ivan Lozanoba222612021-09-21 10:49:13 -0400125 testInstallBase := "/data/local/tests/unrestricted"
126 if ctx.RustModule().InVendor() || ctx.RustModule().UseVndk() {
127 testInstallBase = "/data/local/tests/vendor"
128 }
129
130 var configs []tradefed.Config
131 if Bool(test.Properties.Require_root) {
132 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
133 } else {
134 var options []tradefed.Option
135 options = append(options, tradefed.Option{Name: "force-root", Value: "false"})
136 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
137 }
138
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400139 test.testConfig = tradefed.AutoGenRustTestConfig(ctx,
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700140 test.Properties.Test_config,
141 test.Properties.Test_config_template,
142 test.Properties.Test_suites,
Ivan Lozanoba222612021-09-21 10:49:13 -0400143 configs,
144 test.Properties.Auto_gen_config,
145 testInstallBase)
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400146
Ivan Lozano9da4aa82021-01-29 12:48:05 -0500147 dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
148
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400149 ctx.VisitDirectDepsWithTag(dataLibDepTag, func(dep android.Module) {
150 depName := ctx.OtherModuleName(dep)
151 linkableDep, ok := dep.(cc.LinkableInterface)
152 if !ok {
153 ctx.ModuleErrorf("data_lib %q is not a linkable module", depName)
154 }
155 if linkableDep.OutputFile().Valid() {
Jooyung Han10bea7d2022-04-29 02:04:30 +0900156 // Copy the output in "lib[64]" so that it's compatible with
157 // the default rpath values.
158 libDir := "lib"
159 if linkableDep.Target().Arch.ArchType.Multilib == "lib64" {
160 libDir = "lib64"
161 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400162 test.data = append(test.data,
163 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
Jooyung Han10bea7d2022-04-29 02:04:30 +0900164 RelativeInstallPath: filepath.Join(libDir, linkableDep.RelativeInstallPath())})
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400165 }
166 })
167
168 ctx.VisitDirectDepsWithTag(dataBinDepTag, func(dep android.Module) {
169 depName := ctx.OtherModuleName(dep)
170 linkableDep, ok := dep.(cc.LinkableInterface)
171 if !ok {
172 ctx.ModuleErrorf("data_bin %q is not a linkable module", depName)
173 }
174 if linkableDep.OutputFile().Valid() {
175 test.data = append(test.data,
176 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
177 RelativeInstallPath: linkableDep.RelativeInstallPath()})
178 }
179 })
180
Ivan Lozano9da4aa82021-01-29 12:48:05 -0500181 for _, dataSrcPath := range dataSrcPaths {
182 test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
183 }
184
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800185 // default relative install path is module name
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400186 if !Bool(test.Properties.No_named_install_directory) {
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800187 test.baseCompiler.relative = ctx.ModuleName()
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400188 } else if String(test.baseCompiler.Properties.Relative_install_path) == "" {
189 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 -0800190 }
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400191
Julien Desprez84c94942021-01-15 15:10:01 -0800192 if ctx.Host() && test.Properties.Test_options.Unit_test == nil {
193 test.Properties.Test_options.Unit_test = proptools.BoolPtr(true)
194 }
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +0200195 test.binaryDecorator.install(ctx)
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700196}
197
198func (test *testDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700199 flags = test.binaryDecorator.compilerFlags(ctx, flags)
Stephen Crane02a623d2020-04-25 21:40:35 -0700200 if test.testHarness() {
201 flags.RustFlags = append(flags.RustFlags, "--test")
202 }
Jeff Vander Stoepbf7a9022021-01-26 14:35:38 +0100203 if ctx.Device() {
204 flags.RustFlags = append(flags.RustFlags, "-Z panic_abort_tests")
205 }
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700206 return flags
207}
208
Liz Kammer356f7d42021-01-26 09:18:53 -0500209func (test *testDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700210 return rlibAutoDep
211}
212
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700213func init() {
214 // Rust tests are binary files built with --test.
215 android.RegisterModuleType("rust_test", RustTestFactory)
216 android.RegisterModuleType("rust_test_host", RustTestHostFactory)
217}
218
219func RustTestFactory() android.Module {
220 module, _ := NewRustTest(android.HostAndDeviceSupported)
Ivan Lozanof5c98a22021-12-07 10:17:00 -0500221
222 // NewRustTest will set MultilibBoth true, however the host variant
223 // cannot produce the non-primary target. Therefore, add the
224 // rustTestHostMultilib load hook to set MultilibFirst for the
225 // host target.
226 android.AddLoadHook(module, rustTestHostMultilib)
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700227 return module.Init()
228}
229
230func RustTestHostFactory() android.Module {
231 module, _ := NewRustTest(android.HostSupported)
232 return module.Init()
233}
Ivan Lozano2b081132020-09-08 12:46:52 -0400234
Ivan Lozanodd055472020-09-28 13:22:45 -0400235func (test *testDecorator) stdLinkage(ctx *depsContext) RustLinkage {
236 return RlibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400237}
Ivan Lozano3ee74c82021-07-15 15:44:10 -0400238
239func (test *testDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
240 deps = test.binaryDecorator.compilerDeps(ctx, deps)
241
242 deps.Rustlibs = append(deps.Rustlibs, "libtest")
243
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400244 deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...)
245 deps.DataBins = append(deps.DataBins, test.Properties.Data_bins...)
246
Ivan Lozano3ee74c82021-07-15 15:44:10 -0400247 return deps
248}
Ivan Lozano62cd0382021-11-01 10:27:54 -0400249
250func (test *testDecorator) testBinary() bool {
251 return true
252}
Ivan Lozanof5c98a22021-12-07 10:17:00 -0500253
254func rustTestHostMultilib(ctx android.LoadHookContext) {
255 type props struct {
256 Target struct {
257 Host struct {
258 Compile_multilib *string
259 }
260 }
261 }
262 p := &props{}
263 p.Target.Host.Compile_multilib = proptools.StringPtr("first")
264 ctx.AppendProperties(p)
265}