blob: 05c361e8723d3b773a43c4217a1af62db671afb5 [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
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070022type TestProperties struct {
Ivan Lozanofc80fe72020-06-11 13:25:36 -040023 // Disables the creation of a test-specific directory when used with
24 // relative_install_path. Useful if several tests need to be in the same
25 // directory, but test_per_src doesn't work.
26 No_named_install_directory *bool
27
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070028 // the name of the test configuration (for example "AndroidTest.xml") that should be
29 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070030 Test_config *string `android:"path,arch_variant"`
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070031
32 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
33 // should be installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070034 Test_config_template *string `android:"path,arch_variant"`
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070035
36 // list of compatibility suites (for example "cts", "vts") that the module should be
37 // installed into.
38 Test_suites []string `android:"arch_variant"`
39
40 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
41 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
42 // explicitly.
43 Auto_gen_config *bool
Stephen Crane02a623d2020-04-25 21:40:35 -070044
45 // if set, build with the standard Rust test harness. Defaults to true.
46 Test_harness *bool
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070047}
48
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070049// A test module is a binary module with extra --test compiler flag
50// and different default installation directory.
51// In golang, inheriance is written as a component.
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070052type testDecorator struct {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070053 *binaryDecorator
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070054 Properties TestProperties
55 testConfig android.Path
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070056}
57
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040058func (test *testDecorator) nativeCoverage() bool {
59 return true
60}
61
Stephen Crane02a623d2020-04-25 21:40:35 -070062func (test *testDecorator) testHarness() bool {
63 return BoolDefault(test.Properties.Test_harness, true)
64}
65
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070066func NewRustTest(hod android.HostOrDeviceSupported) (*Module, *testDecorator) {
Chih-Hung Hsiehe728a892020-06-16 01:25:27 -070067 // Build both 32 and 64 targets for device tests.
68 // Cannot build both for host tests yet if the test depends on
69 // something like proc-macro2 that cannot be built for both.
70 multilib := android.MultilibBoth
71 if hod != android.DeviceSupported && hod != android.HostAndDeviceSupported {
72 multilib = android.MultilibFirst
73 }
74 module := newModule(hod, multilib)
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070075
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070076 test := &testDecorator{
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070077 binaryDecorator: &binaryDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080078 baseCompiler: NewBaseCompiler("nativetest", "nativetest64", InstallInData),
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070079 },
80 }
81
82 module.compiler = test
Ivan Lozanofc80fe72020-06-11 13:25:36 -040083 module.AddProperties(&test.Properties)
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070084 return module, test
85}
86
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070087func (test *testDecorator) compilerProps() []interface{} {
88 return append(test.binaryDecorator.compilerProps(), &test.Properties)
89}
90
91func (test *testDecorator) install(ctx ModuleContext, file android.Path) {
Ivan Lozanofc80fe72020-06-11 13:25:36 -040092 test.testConfig = tradefed.AutoGenRustTestConfig(ctx,
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070093 test.Properties.Test_config,
94 test.Properties.Test_config_template,
95 test.Properties.Test_suites,
Ivan Lozanofc80fe72020-06-11 13:25:36 -040096 nil,
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070097 test.Properties.Auto_gen_config)
Ivan Lozanofc80fe72020-06-11 13:25:36 -040098
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080099 // default relative install path is module name
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400100 if !Bool(test.Properties.No_named_install_directory) {
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800101 test.baseCompiler.relative = ctx.ModuleName()
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400102 } else if String(test.baseCompiler.Properties.Relative_install_path) == "" {
103 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 -0800104 }
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400105
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700106 test.binaryDecorator.install(ctx, file)
107}
108
109func (test *testDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700110 flags = test.binaryDecorator.compilerFlags(ctx, flags)
Stephen Crane02a623d2020-04-25 21:40:35 -0700111 if test.testHarness() {
112 flags.RustFlags = append(flags.RustFlags, "--test")
113 }
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700114 return flags
115}
116
Matthew Maurer0f003b12020-06-29 14:34:06 -0700117func (test *testDecorator) autoDep() autoDep {
118 return rlibAutoDep
119}
120
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700121func init() {
122 // Rust tests are binary files built with --test.
123 android.RegisterModuleType("rust_test", RustTestFactory)
124 android.RegisterModuleType("rust_test_host", RustTestHostFactory)
125}
126
127func RustTestFactory() android.Module {
128 module, _ := NewRustTest(android.HostAndDeviceSupported)
129 return module.Init()
130}
131
132func RustTestHostFactory() android.Module {
133 module, _ := NewRustTest(android.HostSupported)
134 return module.Init()
135}