blob: 6caa7b16876dbe96dcd511e86055b1243cb9dfb1 [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"
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070021 "android/soong/tradefed"
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070022)
23
Dan Shid79572f2020-11-13 14:33:46 -080024// Test option struct.
25type 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 Hsieh41805be2019-10-31 20:56:47 -070030type TestProperties struct {
Ivan Lozanofc80fe72020-06-11 13:25:36 -040031 // 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 Hsieh41805be2019-10-31 20:56:47 -070036 // the name of the test configuration (for example "AndroidTest.xml") that should be
37 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070038 Test_config *string `android:"path,arch_variant"`
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070039
40 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
41 // should be installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070042 Test_config_template *string `android:"path,arch_variant"`
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070043
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 Lozano9da4aa82021-01-29 12:48:05 -050048 // 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 Hsieh41805be2019-10-31 20:56:47 -070052 // 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 Crane02a623d2020-04-25 21:40:35 -070056
57 // if set, build with the standard Rust test harness. Defaults to true.
58 Test_harness *bool
Dan Shid79572f2020-11-13 14:33:46 -080059
60 // Test options.
61 Test_options TestOptions
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070062}
63
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070064// A test module is a binary module with extra --test compiler flag
65// and different default installation directory.
66// In golang, inheriance is written as a component.
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070067type testDecorator struct {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070068 *binaryDecorator
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070069 Properties TestProperties
70 testConfig android.Path
Ivan Lozano9da4aa82021-01-29 12:48:05 -050071
72 data []android.DataPath
73}
74
75func (test *testDecorator) dataPaths() []android.DataPath {
76 return test.data
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070077}
78
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040079func (test *testDecorator) nativeCoverage() bool {
80 return true
81}
82
Stephen Crane02a623d2020-04-25 21:40:35 -070083func (test *testDecorator) testHarness() bool {
84 return BoolDefault(test.Properties.Test_harness, true)
85}
86
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070087func NewRustTest(hod android.HostOrDeviceSupported) (*Module, *testDecorator) {
Chih-Hung Hsiehe728a892020-06-16 01:25:27 -070088 // Build both 32 and 64 targets for device tests.
89 // Cannot build both for host tests yet if the test depends on
90 // something like proc-macro2 that cannot be built for both.
91 multilib := android.MultilibBoth
92 if hod != android.DeviceSupported && hod != android.HostAndDeviceSupported {
93 multilib = android.MultilibFirst
94 }
95 module := newModule(hod, multilib)
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070096
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070097 test := &testDecorator{
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070098 binaryDecorator: &binaryDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080099 baseCompiler: NewBaseCompiler("nativetest", "nativetest64", InstallInData),
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700100 },
101 }
102
103 module.compiler = test
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700104 return module, test
105}
106
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700107func (test *testDecorator) compilerProps() []interface{} {
108 return append(test.binaryDecorator.compilerProps(), &test.Properties)
109}
110
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +0200111func (test *testDecorator) install(ctx ModuleContext) {
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400112 test.testConfig = tradefed.AutoGenRustTestConfig(ctx,
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700113 test.Properties.Test_config,
114 test.Properties.Test_config_template,
115 test.Properties.Test_suites,
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400116 nil,
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700117 test.Properties.Auto_gen_config)
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400118
Ivan Lozano9da4aa82021-01-29 12:48:05 -0500119 dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
120
121 for _, dataSrcPath := range dataSrcPaths {
122 test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
123 }
124
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800125 // default relative install path is module name
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400126 if !Bool(test.Properties.No_named_install_directory) {
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800127 test.baseCompiler.relative = ctx.ModuleName()
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400128 } else if String(test.baseCompiler.Properties.Relative_install_path) == "" {
129 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 -0800130 }
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400131
Julien Desprez84c94942021-01-15 15:10:01 -0800132 if ctx.Host() && test.Properties.Test_options.Unit_test == nil {
133 test.Properties.Test_options.Unit_test = proptools.BoolPtr(true)
134 }
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +0200135 test.binaryDecorator.install(ctx)
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700136}
137
138func (test *testDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700139 flags = test.binaryDecorator.compilerFlags(ctx, flags)
Stephen Crane02a623d2020-04-25 21:40:35 -0700140 if test.testHarness() {
141 flags.RustFlags = append(flags.RustFlags, "--test")
142 }
Jeff Vander Stoepbf7a9022021-01-26 14:35:38 +0100143 if ctx.Device() {
144 flags.RustFlags = append(flags.RustFlags, "-Z panic_abort_tests")
145 }
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700146 return flags
147}
148
Liz Kammer356f7d42021-01-26 09:18:53 -0500149func (test *testDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700150 return rlibAutoDep
151}
152
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700153func init() {
154 // Rust tests are binary files built with --test.
155 android.RegisterModuleType("rust_test", RustTestFactory)
156 android.RegisterModuleType("rust_test_host", RustTestHostFactory)
157}
158
159func RustTestFactory() android.Module {
160 module, _ := NewRustTest(android.HostAndDeviceSupported)
161 return module.Init()
162}
163
164func RustTestHostFactory() android.Module {
165 module, _ := NewRustTest(android.HostSupported)
166 return module.Init()
167}
Ivan Lozano2b081132020-09-08 12:46:52 -0400168
Ivan Lozanodd055472020-09-28 13:22:45 -0400169func (test *testDecorator) stdLinkage(ctx *depsContext) RustLinkage {
170 return RlibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400171}