blob: 416c557d8e915baed965a0d77e8562a5ad840673 [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
44}
45
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070046// A test module is a binary module with extra --test compiler flag
47// and different default installation directory.
48// In golang, inheriance is written as a component.
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070049type testDecorator struct {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070050 *binaryDecorator
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070051 Properties TestProperties
52 testConfig android.Path
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070053}
54
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040055func (test *testDecorator) nativeCoverage() bool {
56 return true
57}
58
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070059func NewRustTest(hod android.HostOrDeviceSupported) (*Module, *testDecorator) {
Chih-Hung Hsiehe728a892020-06-16 01:25:27 -070060 // Build both 32 and 64 targets for device tests.
61 // Cannot build both for host tests yet if the test depends on
62 // something like proc-macro2 that cannot be built for both.
63 multilib := android.MultilibBoth
64 if hod != android.DeviceSupported && hod != android.HostAndDeviceSupported {
65 multilib = android.MultilibFirst
66 }
67 module := newModule(hod, multilib)
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070068
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070069 test := &testDecorator{
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070070 binaryDecorator: &binaryDecorator{
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080071 baseCompiler: NewBaseCompiler("nativetest", "nativetest64", InstallInData),
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070072 },
73 }
74
75 module.compiler = test
Ivan Lozanofc80fe72020-06-11 13:25:36 -040076 module.AddProperties(&test.Properties)
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -070077 return module, test
78}
79
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070080func (test *testDecorator) compilerProps() []interface{} {
81 return append(test.binaryDecorator.compilerProps(), &test.Properties)
82}
83
84func (test *testDecorator) install(ctx ModuleContext, file android.Path) {
Ivan Lozanofc80fe72020-06-11 13:25:36 -040085 test.testConfig = tradefed.AutoGenRustTestConfig(ctx,
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070086 test.Properties.Test_config,
87 test.Properties.Test_config_template,
88 test.Properties.Test_suites,
Ivan Lozanofc80fe72020-06-11 13:25:36 -040089 nil,
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070090 test.Properties.Auto_gen_config)
Ivan Lozanofc80fe72020-06-11 13:25:36 -040091
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080092 // default relative install path is module name
Ivan Lozanofc80fe72020-06-11 13:25:36 -040093 if !Bool(test.Properties.No_named_install_directory) {
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080094 test.baseCompiler.relative = ctx.ModuleName()
Ivan Lozanofc80fe72020-06-11 13:25:36 -040095 } else if String(test.baseCompiler.Properties.Relative_install_path) == "" {
96 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 -080097 }
Ivan Lozanofc80fe72020-06-11 13:25:36 -040098
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -070099 test.binaryDecorator.install(ctx, file)
100}
101
102func (test *testDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700103 flags = test.binaryDecorator.compilerFlags(ctx, flags)
104 flags.RustFlags = append(flags.RustFlags, "--test")
105 return flags
106}
107
108func init() {
109 // Rust tests are binary files built with --test.
110 android.RegisterModuleType("rust_test", RustTestFactory)
111 android.RegisterModuleType("rust_test_host", RustTestHostFactory)
112}
113
114func RustTestFactory() android.Module {
115 module, _ := NewRustTest(android.HostAndDeviceSupported)
116 return module.Init()
117}
118
119func RustTestHostFactory() android.Module {
120 module, _ := NewRustTest(android.HostSupported)
121 return module.Init()
122}