blob: 35e04fff6c38f3a6dd7cb7cea2a120dc5b337b7e [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 (
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070018 "android/soong/android"
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070019 "android/soong/tradefed"
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070020)
21
Dan Shid79572f2020-11-13 14:33:46 -080022// Test option struct.
23type TestOptions struct {
24 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
25 Unit_test *bool
26}
27
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070028type TestProperties struct {
Ivan Lozanofc80fe72020-06-11 13:25:36 -040029 // Disables the creation of a test-specific directory when used with
30 // relative_install_path. Useful if several tests need to be in the same
31 // directory, but test_per_src doesn't work.
32 No_named_install_directory *bool
33
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070034 // the name of the test configuration (for example "AndroidTest.xml") that should be
35 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070036 Test_config *string `android:"path,arch_variant"`
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070037
38 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
39 // should be installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070040 Test_config_template *string `android:"path,arch_variant"`
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070041
42 // list of compatibility suites (for example "cts", "vts") that the module should be
43 // installed into.
44 Test_suites []string `android:"arch_variant"`
45
46 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
47 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
48 // explicitly.
49 Auto_gen_config *bool
Stephen Crane02a623d2020-04-25 21:40:35 -070050
51 // if set, build with the standard Rust test harness. Defaults to true.
52 Test_harness *bool
Dan Shid79572f2020-11-13 14:33:46 -080053
54 // Test options.
55 Test_options TestOptions
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070056}
57
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070058// A test module is a binary module with extra --test compiler flag
59// and different default installation directory.
60// In golang, inheriance is written as a component.
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070061type testDecorator struct {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070062 *binaryDecorator
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070063 Properties TestProperties
64 testConfig android.Path
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070065}
66
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040067func (test *testDecorator) nativeCoverage() bool {
68 return true
69}
70
Stephen Crane02a623d2020-04-25 21:40:35 -070071func (test *testDecorator) testHarness() bool {
72 return BoolDefault(test.Properties.Test_harness, true)
73}
74
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070075func NewRustTest(hod android.HostOrDeviceSupported) (*Module, *testDecorator) {
Chih-Hung Hsiehe728a892020-06-16 01:25:27 -070076 // Build both 32 and 64 targets for device tests.
77 // Cannot build both for host tests yet if the test depends on
78 // something like proc-macro2 that cannot be built for both.
79 multilib := android.MultilibBoth
80 if hod != android.DeviceSupported && hod != android.HostAndDeviceSupported {
81 multilib = android.MultilibFirst
82 }
83 module := newModule(hod, multilib)
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070084
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070085 test := &testDecorator{
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070086 binaryDecorator: &binaryDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080087 baseCompiler: NewBaseCompiler("nativetest", "nativetest64", InstallInData),
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070088 },
89 }
90
91 module.compiler = test
Ivan Lozanofc80fe72020-06-11 13:25:36 -040092 module.AddProperties(&test.Properties)
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070093 return module, test
94}
95
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070096func (test *testDecorator) compilerProps() []interface{} {
97 return append(test.binaryDecorator.compilerProps(), &test.Properties)
98}
99
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +0200100func (test *testDecorator) install(ctx ModuleContext) {
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400101 test.testConfig = tradefed.AutoGenRustTestConfig(ctx,
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700102 test.Properties.Test_config,
103 test.Properties.Test_config_template,
104 test.Properties.Test_suites,
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400105 nil,
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700106 test.Properties.Auto_gen_config)
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400107
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800108 // default relative install path is module name
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400109 if !Bool(test.Properties.No_named_install_directory) {
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800110 test.baseCompiler.relative = ctx.ModuleName()
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400111 } else if String(test.baseCompiler.Properties.Relative_install_path) == "" {
112 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 -0800113 }
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400114
ThiƩbaud Weksteenfabaff62020-08-27 13:48:36 +0200115 test.binaryDecorator.install(ctx)
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700116}
117
118func (test *testDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700119 flags = test.binaryDecorator.compilerFlags(ctx, flags)
Stephen Crane02a623d2020-04-25 21:40:35 -0700120 if test.testHarness() {
121 flags.RustFlags = append(flags.RustFlags, "--test")
122 }
Jeff Vander Stoepbf7a9022021-01-26 14:35:38 +0100123 if ctx.Device() {
124 flags.RustFlags = append(flags.RustFlags, "-Z panic_abort_tests")
125 }
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700126 return flags
127}
128
Ivan Lozano042504f2020-08-18 14:31:23 -0400129func (test *testDecorator) autoDep(ctx BaseModuleContext) autoDep {
Matthew Maurer0f003b12020-06-29 14:34:06 -0700130 return rlibAutoDep
131}
132
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700133func init() {
134 // Rust tests are binary files built with --test.
135 android.RegisterModuleType("rust_test", RustTestFactory)
136 android.RegisterModuleType("rust_test_host", RustTestHostFactory)
137}
138
139func RustTestFactory() android.Module {
140 module, _ := NewRustTest(android.HostAndDeviceSupported)
141 return module.Init()
142}
143
144func RustTestHostFactory() android.Module {
145 module, _ := NewRustTest(android.HostSupported)
146 return module.Init()
147}
Ivan Lozano2b081132020-09-08 12:46:52 -0400148
Ivan Lozanodd055472020-09-28 13:22:45 -0400149func (test *testDecorator) stdLinkage(ctx *depsContext) RustLinkage {
150 return RlibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400151}