Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. All rights reserved. |
| 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
| 18 | "path/filepath" |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 19 | "strconv" |
Julien Desprez | 3b933d3 | 2021-01-14 11:49:15 -0800 | [diff] [blame] | 20 | |
Yifeng Zeng | 1876f5b | 2025-02-05 17:00:56 -0800 | [diff] [blame] | 21 | "github.com/google/blueprint/depset" |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
| 23 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 24 | "android/soong/android" |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 25 | "android/soong/tradefed" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 28 | // TestLinkerProperties properties to be registered via the linker |
| 29 | type TestLinkerProperties struct { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 30 | // if set, build against the gtest library. Defaults to true. |
Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 31 | Gtest *bool |
Christopher Ferris | 9df92d6 | 2018-08-21 12:40:08 -0700 | [diff] [blame] | 32 | |
Elliott Hughes | 90e76f1 | 2025-01-16 13:48:31 -0800 | [diff] [blame] | 33 | // if set, use the isolated gtest runner. Defaults to false. |
| 34 | // Isolation is not supported on Windows. |
Christopher Ferris | 9df92d6 | 2018-08-21 12:40:08 -0700 | [diff] [blame] | 35 | Isolated *bool |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 36 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 37 | |
Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 38 | // TestInstallerProperties properties to be registered via the installer |
| 39 | type TestInstallerProperties struct { |
| 40 | // list of compatibility suites (for example "cts", "vts") that the module should be installed into. |
| 41 | Test_suites []string `android:"arch_variant"` |
| 42 | } |
| 43 | |
yelinhsieh | 9fc6040 | 2018-10-01 19:23:14 +0800 | [diff] [blame] | 44 | // Test option struct. |
| 45 | type TestOptions struct { |
Zhenhuang Wang | 0ac5a43 | 2022-08-12 18:49:20 +0800 | [diff] [blame] | 46 | android.CommonTestOptions |
| 47 | |
yelinhsieh | 9fc6040 | 2018-10-01 19:23:14 +0800 | [diff] [blame] | 48 | // The UID that you want to run the test as on a device. |
| 49 | Run_test_as *string |
Dan Shi | 95d1942 | 2020-08-15 12:24:26 -0700 | [diff] [blame] | 50 | |
David Srbecky | 519db27 | 2020-06-18 18:07:00 +0100 | [diff] [blame] | 51 | // A list of free-formed strings without spaces that categorize the test. |
| 52 | Test_suite_tag []string |
Dan Shi | 95d1942 | 2020-08-15 12:24:26 -0700 | [diff] [blame] | 53 | |
| 54 | // a list of extra test configuration files that should be installed with the module. |
| 55 | Extra_test_configs []string `android:"path,arch_variant"` |
Dan Shi | d79572f | 2020-11-13 14:33:46 -0800 | [diff] [blame] | 56 | |
Justin Yun | 46f6605 | 2021-05-04 18:42:24 +0900 | [diff] [blame] | 57 | // Add ShippingApiLevelModuleController to auto generated test config. If the device properties |
Justin Yun | a364cfb | 2021-05-13 12:39:42 +0900 | [diff] [blame] | 58 | // for the shipping api level is less than the min_shipping_api_level, skip this module. |
| 59 | Min_shipping_api_level *int64 |
| 60 | |
| 61 | // Add ShippingApiLevelModuleController to auto generated test config. If any of the device |
| 62 | // shipping api level and vendor api level properties are less than the |
| 63 | // vsr_min_shipping_api_level, skip this module. |
| 64 | // As this includes the shipping api level check, it is not allowed to define |
| 65 | // min_shipping_api_level at the same time with this property. |
| 66 | Vsr_min_shipping_api_level *int64 |
Justin Yun | 46f6605 | 2021-05-04 18:42:24 +0900 | [diff] [blame] | 67 | |
| 68 | // Add MinApiLevelModuleController with ro.vndk.version property. If ro.vndk.version has an |
Justin Yun | a364cfb | 2021-05-13 12:39:42 +0900 | [diff] [blame] | 69 | // integer value and the value is less than the min_vndk_version, skip this module. |
| 70 | Min_vndk_version *int64 |
Dan Shi | ec73143 | 2023-05-26 04:21:44 +0000 | [diff] [blame] | 71 | |
| 72 | // Extra <option> tags to add to the auto generated test xml file under the test runner, e.g., GTest. |
| 73 | // The "key" is optional in each of these. |
| 74 | Test_runner_options []tradefed.Option |
yelinhsieh | 9fc6040 | 2018-10-01 19:23:14 +0800 | [diff] [blame] | 75 | } |
| 76 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 77 | type TestBinaryProperties struct { |
Dan Willemsen | 3340d60 | 2016-12-27 14:40:40 -0800 | [diff] [blame] | 78 | // Disables the creation of a test-specific directory when used with |
| 79 | // relative_install_path. Useful if several tests need to be in the same |
Colin Cross | 3a02c7b | 2024-05-21 13:46:22 -0700 | [diff] [blame] | 80 | // directory. |
Dan Willemsen | 3340d60 | 2016-12-27 14:40:40 -0800 | [diff] [blame] | 81 | No_named_install_directory *bool |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 82 | |
| 83 | // list of files or filegroup modules that provide data that should be installed alongside |
| 84 | // the test |
Dan Shi | 67a8834 | 2020-02-25 16:34:39 -0800 | [diff] [blame] | 85 | Data []string `android:"path,arch_variant"` |
Colin Cross | a929db0 | 2017-03-27 16:27:50 -0700 | [diff] [blame] | 86 | |
Cole Faust | c3489f4 | 2024-12-06 13:07:18 -0800 | [diff] [blame] | 87 | // Same as data, but adds dependencies on modules using the device's os variant, and common |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 88 | // architecture's variant. Can be useful to add device-built apps to the data of a host |
| 89 | // test. |
| 90 | Device_common_data []string `android:"path_device_common"` |
| 91 | |
Cole Faust | c3489f4 | 2024-12-06 13:07:18 -0800 | [diff] [blame] | 92 | // Same as data, but adds dependencies on modules using the device's os variant, and the |
| 93 | // device's first architecture's variant. Can be useful to add device-built apps to the data |
| 94 | // of a host test. |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 95 | Device_first_data []string `android:"path_device_first"` |
| 96 | |
Colin Cross | b361442 | 2025-02-18 15:18:18 -0800 | [diff] [blame^] | 97 | // Same as data, but will add dependencies on modules using the host's os variation and |
| 98 | // the common arch variation. Useful for a device test that wants to depend on a host |
| 99 | // module, for example to include a custom Tradefed test runner. |
| 100 | Host_common_data []string `android:"path_host_common"` |
| 101 | |
Chris Parsons | 79d66a5 | 2020-06-05 17:26:16 -0400 | [diff] [blame] | 102 | // list of shared library modules that should be installed alongside the test |
| 103 | Data_libs []string `android:"arch_variant"` |
| 104 | |
Colin Cross | c8caa06 | 2021-09-24 16:50:14 -0700 | [diff] [blame] | 105 | // list of binary modules that should be installed alongside the test |
| 106 | Data_bins []string `android:"arch_variant"` |
| 107 | |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 108 | // the name of the test configuration (for example "AndroidTest.xml") that should be |
| 109 | // installed with the module. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 110 | Test_config *string `android:"path,arch_variant"` |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 111 | |
| 112 | // the name of the test configuration template (for example "AndroidTestTemplate.xml") that |
| 113 | // should be installed with the module. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 114 | Test_config_template *string `android:"path,arch_variant"` |
yelinhsieh | 9fc6040 | 2018-10-01 19:23:14 +0800 | [diff] [blame] | 115 | |
| 116 | // Test options. |
| 117 | Test_options TestOptions |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 118 | |
| 119 | // Add RootTargetPreparer to auto generated test config. This guarantees the test to run |
| 120 | // with root permission. |
| 121 | Require_root *bool |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 122 | |
| 123 | // Add RunCommandTargetPreparer to stop framework before the test and start it after the test. |
| 124 | Disable_framework *bool |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 125 | |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 126 | // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml |
| 127 | // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true |
| 128 | // explicitly. |
| 129 | Auto_gen_config *bool |
easoncylee | 1e3fdcd | 2020-04-30 10:08:33 +0800 | [diff] [blame] | 130 | |
| 131 | // Add parameterized mainline modules to auto generated test config. The options will be |
| 132 | // handled by TradeFed to download and install the specified modules on the device. |
| 133 | Test_mainline_modules []string |
Colin Cross | cfb0f5e | 2021-09-24 15:47:17 -0700 | [diff] [blame] | 134 | |
| 135 | // Install the test into a folder named for the module in all test suites. |
| 136 | Per_testcase_directory *bool |
Yifeng Zeng | 1876f5b | 2025-02-05 17:00:56 -0800 | [diff] [blame] | 137 | |
| 138 | // Install the test's dependencies into a folder named standalone-libs relative to the |
| 139 | // test's installation path. ld-library-path will be set to this path in the test's |
| 140 | // auto-generated config. This way the dependencies can be used by the test without having |
| 141 | // to manually install them to the device. See more details in |
| 142 | // go/standalone-native-device-tests. |
| 143 | Standalone_test *bool |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | func init() { |
Steven Moreland | 87c9d7b | 2017-11-02 21:38:28 -0700 | [diff] [blame] | 147 | android.RegisterModuleType("cc_test", TestFactory) |
| 148 | android.RegisterModuleType("cc_test_library", TestLibraryFactory) |
| 149 | android.RegisterModuleType("cc_benchmark", BenchmarkFactory) |
| 150 | android.RegisterModuleType("cc_test_host", TestHostFactory) |
| 151 | android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 154 | // cc_test generates a test config file and an executable binary file to test |
| 155 | // specific functionality on a device. The executable binary gets an implicit |
| 156 | // static_libs dependency on libgtests unless the gtest flag is set to false. |
Steven Moreland | 87c9d7b | 2017-11-02 21:38:28 -0700 | [diff] [blame] | 157 | func TestFactory() android.Module { |
Colin Cross | 8ff1058 | 2023-12-07 13:10:56 -0800 | [diff] [blame] | 158 | module := NewTest(android.HostAndDeviceSupported) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 159 | return module.Init() |
| 160 | } |
| 161 | |
Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 162 | // cc_test_library creates an archive of files (i.e. .o files) which is later |
| 163 | // referenced by another module (such as cc_test, cc_defaults or cc_test_library) |
| 164 | // for archiving or linking. |
Steven Moreland | 87c9d7b | 2017-11-02 21:38:28 -0700 | [diff] [blame] | 165 | func TestLibraryFactory() android.Module { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 166 | module := NewTestLibrary(android.HostAndDeviceSupported) |
| 167 | return module.Init() |
| 168 | } |
| 169 | |
Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 170 | // cc_benchmark compiles an executable binary that performs benchmark testing |
| 171 | // of a specific component in a device. Additional files such as test suites |
| 172 | // and test configuration are installed on the side of the compiled executed |
| 173 | // binary. |
Steven Moreland | 87c9d7b | 2017-11-02 21:38:28 -0700 | [diff] [blame] | 174 | func BenchmarkFactory() android.Module { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 175 | module := NewBenchmark(android.HostAndDeviceSupported) |
Aditya Choudhary | 87b2ab2 | 2023-11-17 15:27:06 +0000 | [diff] [blame] | 176 | module.testModule = true |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 177 | return module.Init() |
| 178 | } |
| 179 | |
Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 180 | // cc_test_host compiles a test host binary. |
Steven Moreland | 87c9d7b | 2017-11-02 21:38:28 -0700 | [diff] [blame] | 181 | func TestHostFactory() android.Module { |
Colin Cross | 8ff1058 | 2023-12-07 13:10:56 -0800 | [diff] [blame] | 182 | module := NewTest(android.HostSupported) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 183 | return module.Init() |
| 184 | } |
| 185 | |
Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 186 | // cc_benchmark_host compiles an executable binary that performs benchmark |
| 187 | // testing of a specific component in the host. Additional files such as |
| 188 | // test suites and test configuration are installed on the side of the |
| 189 | // compiled executed binary. |
Steven Moreland | 87c9d7b | 2017-11-02 21:38:28 -0700 | [diff] [blame] | 190 | func BenchmarkHostFactory() android.Module { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 191 | module := NewBenchmark(android.HostSupported) |
| 192 | return module.Init() |
| 193 | } |
| 194 | |
Chris Parsons | 216e10a | 2020-07-09 17:12:52 -0400 | [diff] [blame] | 195 | func (test *testBinary) dataPaths() []android.DataPath { |
Liz Kammer | 1c14a21 | 2020-05-12 15:26:55 -0700 | [diff] [blame] | 196 | return test.data |
| 197 | } |
| 198 | |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 199 | func (test *testBinary) testBinary() bool { |
| 200 | return true |
| 201 | } |
| 202 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 203 | type testDecorator struct { |
Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 204 | LinkerProperties TestLinkerProperties |
| 205 | InstallerProperties TestInstallerProperties |
| 206 | installer *baseInstaller |
| 207 | linker *baseLinker |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 210 | func (test *testDecorator) gtest() bool { |
Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 211 | return BoolDefault(test.LinkerProperties.Gtest, true) |
Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Elliott Hughes | 90e76f1 | 2025-01-16 13:48:31 -0800 | [diff] [blame] | 214 | func (test *testDecorator) isolated(ctx android.BaseModuleContext) bool { |
| 215 | return BoolDefault(test.LinkerProperties.Isolated, false) && !ctx.Windows() |
Trevor Radcliffe | cd8fd67 | 2022-05-03 14:51:16 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 218 | // NOTE: Keep this in sync with cc/cc_test.bzl#gtest_copts |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 219 | func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 220 | if !test.gtest() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 221 | return flags |
| 222 | } |
| 223 | |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 224 | flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_HAS_STD_STRING") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 225 | if ctx.Host() { |
Liana Kazanova | b88279f | 2024-07-15 18:45:23 +0000 | [diff] [blame] | 226 | flags.Local.CFlags = append(flags.Local.CFlags, "-O0", "-g") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 227 | |
| 228 | switch ctx.Os() { |
| 229 | case android.Windows: |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 230 | flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_WINDOWS") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 231 | case android.Linux: |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 232 | flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 233 | case android.Darwin: |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 234 | flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_MAC") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 235 | } |
| 236 | } else { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 237 | flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX_ANDROID") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | return flags |
| 241 | } |
| 242 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 243 | func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps { |
Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 244 | if test.gtest() { |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 245 | if ctx.useSdk() && ctx.Device() { |
Dan Albert | 7dd5899 | 2018-02-09 15:22:59 -0800 | [diff] [blame] | 246 | deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++") |
Trevor Radcliffe | cd8fd67 | 2022-05-03 14:51:16 +0000 | [diff] [blame] | 247 | } else if test.isolated(ctx) { |
Christopher Ferris | 9df92d6 | 2018-08-21 12:40:08 -0700 | [diff] [blame] | 248 | deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main") |
Christopher Ferris | 34cbba6 | 2019-07-17 15:46:29 -0700 | [diff] [blame] | 249 | // The isolated library requires liblog, but adding it |
| 250 | // as a static library means unit tests cannot override |
| 251 | // liblog functions. Instead make it a shared library |
| 252 | // dependency. |
| 253 | deps.SharedLibs = append(deps.SharedLibs, "liblog") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 254 | } else { |
| 255 | deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest") |
| 256 | } |
| 257 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 258 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 259 | return deps |
| 260 | } |
| 261 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 262 | func (test *testDecorator) linkerProps() []interface{} { |
Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 263 | return []interface{}{&test.LinkerProperties} |
| 264 | } |
| 265 | |
| 266 | func (test *testDecorator) installerProps() []interface{} { |
| 267 | return []interface{}{&test.InstallerProperties} |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Colin Cross | 4a9e6ec | 2023-12-18 15:29:41 -0800 | [diff] [blame] | 270 | func (test *testDecorator) moduleInfoJSON(ctx android.ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) { |
| 271 | if android.PrefixInList(moduleInfoJSON.CompatibilitySuites, "mts-") && |
| 272 | !android.InList("mts", moduleInfoJSON.CompatibilitySuites) { |
| 273 | moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "mts") |
| 274 | } |
| 275 | } |
| 276 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 277 | func NewTestInstaller() *baseInstaller { |
| 278 | return NewBaseInstaller("nativetest", "nativetest64", InstallInData) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 281 | type testBinary struct { |
Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 282 | *testDecorator |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 283 | *binaryDecorator |
| 284 | *baseCompiler |
Dan Shi | 95d1942 | 2020-08-15 12:24:26 -0700 | [diff] [blame] | 285 | Properties TestBinaryProperties |
| 286 | data []android.DataPath |
| 287 | testConfig android.Path |
| 288 | extraTestConfigs android.Paths |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | func (test *testBinary) linkerProps() []interface{} { |
| 292 | props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...) |
| 293 | props = append(props, &test.Properties) |
| 294 | return props |
| 295 | } |
| 296 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 297 | func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 298 | deps = test.testDecorator.linkerDeps(ctx, deps) |
| 299 | deps = test.binaryDecorator.linkerDeps(ctx, deps) |
Chris Parsons | 79d66a5 | 2020-06-05 17:26:16 -0400 | [diff] [blame] | 300 | deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...) |
Colin Cross | c8caa06 | 2021-09-24 16:50:14 -0700 | [diff] [blame] | 301 | deps.DataBins = append(deps.DataBins, test.Properties.Data_bins...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 302 | return deps |
| 303 | } |
| 304 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 305 | func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 306 | flags = test.binaryDecorator.linkerFlags(ctx, flags) |
| 307 | flags = test.testDecorator.linkerFlags(ctx, flags) |
Colin Cross | b881e32 | 2024-05-10 13:24:17 -0700 | [diff] [blame] | 308 | |
| 309 | // Add a default rpath to allow tests to dlopen libraries specified in data_libs. |
| 310 | // Host modules already get an rpath specified in linker.go. |
| 311 | if !ctx.Host() { |
| 312 | flags.Global.LdFlags = append(flags.Global.LdFlags, `-Wl,-rpath,\$$ORIGIN`) |
| 313 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 314 | return flags |
| 315 | } |
| 316 | |
Colin Cross | 4a9e6ec | 2023-12-18 15:29:41 -0800 | [diff] [blame] | 317 | func (test *testBinary) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) { |
| 318 | if ctx.Host() && Bool(test.Properties.Test_options.Unit_test) { |
| 319 | moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "host-unit-tests") |
| 320 | } |
| 321 | moduleInfoJSON.TestOptionsTags = append(moduleInfoJSON.TestOptionsTags, test.Properties.Test_options.Tags...) |
| 322 | moduleInfoJSON.TestMainlineModules = append(moduleInfoJSON.TestMainlineModules, test.Properties.Test_mainline_modules...) |
| 323 | if test.testConfig != nil { |
| 324 | if _, ok := test.testConfig.(android.WritablePath); ok { |
| 325 | moduleInfoJSON.AutoTestConfig = []string{"true"} |
| 326 | } |
| 327 | moduleInfoJSON.TestConfig = append(moduleInfoJSON.TestConfig, test.testConfig.String()) |
| 328 | } |
| 329 | moduleInfoJSON.TestConfig = append(moduleInfoJSON.TestConfig, test.extraTestConfigs.Strings()...) |
| 330 | |
Colin Cross | 4a9e6ec | 2023-12-18 15:29:41 -0800 | [diff] [blame] | 331 | moduleInfoJSON.DataDependencies = append(moduleInfoJSON.DataDependencies, test.Properties.Data_bins...) |
| 332 | |
| 333 | if len(test.InstallerProperties.Test_suites) > 0 { |
| 334 | moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, test.InstallerProperties.Test_suites...) |
| 335 | } else { |
| 336 | moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "null-suite") |
| 337 | } |
| 338 | |
| 339 | test.binaryDecorator.moduleInfoJSON(ctx, moduleInfoJSON) |
| 340 | test.testDecorator.moduleInfoJSON(ctx, moduleInfoJSON) |
| 341 | moduleInfoJSON.Class = []string{"NATIVE_TESTS"} |
| 342 | |
| 343 | } |
| 344 | |
Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 345 | func (test *testBinary) installerProps() []interface{} { |
| 346 | return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...) |
| 347 | } |
| 348 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 349 | func (test *testBinary) install(ctx ModuleContext, file android.Path) { |
Chris Parsons | 216e10a | 2020-07-09 17:12:52 -0400 | [diff] [blame] | 350 | dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data) |
Cole Faust | 65cb40a | 2024-10-21 15:41:42 -0700 | [diff] [blame] | 351 | dataSrcPaths = append(dataSrcPaths, android.PathsForModuleSrc(ctx, test.Properties.Device_common_data)...) |
| 352 | dataSrcPaths = append(dataSrcPaths, android.PathsForModuleSrc(ctx, test.Properties.Device_first_data)...) |
Colin Cross | b361442 | 2025-02-18 15:18:18 -0800 | [diff] [blame^] | 353 | dataSrcPaths = append(dataSrcPaths, android.PathsForModuleSrc(ctx, test.Properties.Host_common_data)...) |
Chris Parsons | 216e10a | 2020-07-09 17:12:52 -0400 | [diff] [blame] | 354 | |
| 355 | for _, dataSrcPath := range dataSrcPaths { |
| 356 | test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath}) |
| 357 | } |
Chris Parsons | 79d66a5 | 2020-06-05 17:26:16 -0400 | [diff] [blame] | 358 | |
Yu Liu | 97880e1 | 2025-01-07 19:03:34 +0000 | [diff] [blame] | 359 | ctx.VisitDirectDepsProxyWithTag(dataLibDepTag, func(dep android.ModuleProxy) { |
Chris Parsons | 79d66a5 | 2020-06-05 17:26:16 -0400 | [diff] [blame] | 360 | depName := ctx.OtherModuleName(dep) |
Yu Liu | 97880e1 | 2025-01-07 19:03:34 +0000 | [diff] [blame] | 361 | linkableDep, ok := android.OtherModuleProvider(ctx, dep, LinkableInfoProvider) |
Chris Parsons | 79d66a5 | 2020-06-05 17:26:16 -0400 | [diff] [blame] | 362 | if !ok { |
Ivan Lozano | 4e5f07d | 2021-11-04 14:09:38 -0400 | [diff] [blame] | 363 | ctx.ModuleErrorf("data_lib %q is not a LinkableInterface module", depName) |
Chris Parsons | 79d66a5 | 2020-06-05 17:26:16 -0400 | [diff] [blame] | 364 | } |
Yu Liu | 97880e1 | 2025-01-07 19:03:34 +0000 | [diff] [blame] | 365 | if linkableDep.OutputFile.Valid() { |
Chris Parsons | 216e10a | 2020-07-09 17:12:52 -0400 | [diff] [blame] | 366 | test.data = append(test.data, |
Yu Liu | 97880e1 | 2025-01-07 19:03:34 +0000 | [diff] [blame] | 367 | android.DataPath{SrcPath: linkableDep.OutputFile.Path(), |
| 368 | RelativeInstallPath: linkableDep.RelativeInstallPath}) |
Chris Parsons | 79d66a5 | 2020-06-05 17:26:16 -0400 | [diff] [blame] | 369 | } |
| 370 | }) |
Yu Liu | 97880e1 | 2025-01-07 19:03:34 +0000 | [diff] [blame] | 371 | ctx.VisitDirectDepsProxyWithTag(dataBinDepTag, func(dep android.ModuleProxy) { |
Colin Cross | c8caa06 | 2021-09-24 16:50:14 -0700 | [diff] [blame] | 372 | depName := ctx.OtherModuleName(dep) |
Yu Liu | 97880e1 | 2025-01-07 19:03:34 +0000 | [diff] [blame] | 373 | linkableDep, ok := android.OtherModuleProvider(ctx, dep, LinkableInfoProvider) |
Colin Cross | c8caa06 | 2021-09-24 16:50:14 -0700 | [diff] [blame] | 374 | if !ok { |
Ivan Lozano | 4e5f07d | 2021-11-04 14:09:38 -0400 | [diff] [blame] | 375 | ctx.ModuleErrorf("data_bin %q is not a LinkableInterface module", depName) |
Colin Cross | c8caa06 | 2021-09-24 16:50:14 -0700 | [diff] [blame] | 376 | } |
Yu Liu | 97880e1 | 2025-01-07 19:03:34 +0000 | [diff] [blame] | 377 | if linkableDep.OutputFile.Valid() { |
Colin Cross | c8caa06 | 2021-09-24 16:50:14 -0700 | [diff] [blame] | 378 | test.data = append(test.data, |
Yu Liu | 97880e1 | 2025-01-07 19:03:34 +0000 | [diff] [blame] | 379 | android.DataPath{SrcPath: linkableDep.OutputFile.Path(), |
| 380 | RelativeInstallPath: linkableDep.RelativeInstallPath}) |
Colin Cross | c8caa06 | 2021-09-24 16:50:14 -0700 | [diff] [blame] | 381 | } |
| 382 | }) |
Chris Parsons | 79d66a5 | 2020-06-05 17:26:16 -0400 | [diff] [blame] | 383 | |
Kiyoung Kim | aa39480 | 2024-01-08 12:55:45 +0900 | [diff] [blame] | 384 | testInstallBase := getTestInstallBase(ctx.InVendorOrProduct()) |
Dan Shi | ad04250 | 2023-08-02 13:53:00 -0700 | [diff] [blame] | 385 | configs := getTradefedConfigOptions(ctx, &test.Properties, test.isolated(ctx), ctx.Device()) |
yelinhsieh | 9fc6040 | 2018-10-01 19:23:14 +0800 | [diff] [blame] | 386 | |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 387 | test.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{ |
| 388 | TestConfigProp: test.Properties.Test_config, |
| 389 | TestConfigTemplateProp: test.Properties.Test_config_template, |
| 390 | TestSuites: test.testDecorator.InstallerProperties.Test_suites, |
| 391 | Config: configs, |
Dan Shi | ec73143 | 2023-05-26 04:21:44 +0000 | [diff] [blame] | 392 | TestRunnerOptions: test.Properties.Test_options.Test_runner_options, |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 393 | AutoGenConfig: test.Properties.Auto_gen_config, |
| 394 | TestInstallBase: testInstallBase, |
| 395 | DeviceTemplate: "${NativeTestConfigTemplate}", |
| 396 | HostTemplate: "${NativeHostTestConfigTemplate}", |
Yifeng Zeng | 1876f5b | 2025-02-05 17:00:56 -0800 | [diff] [blame] | 397 | StandaloneTest: test.Properties.Standalone_test, |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 398 | }) |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 399 | |
Dan Shi | 95d1942 | 2020-08-15 12:24:26 -0700 | [diff] [blame] | 400 | test.extraTestConfigs = android.PathsForModuleSrc(ctx, test.Properties.Test_options.Extra_test_configs) |
| 401 | |
Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 402 | test.binaryDecorator.baseInstaller.dir = "nativetest" |
| 403 | test.binaryDecorator.baseInstaller.dir64 = "nativetest64" |
Dan Willemsen | 3340d60 | 2016-12-27 14:40:40 -0800 | [diff] [blame] | 404 | |
| 405 | if !Bool(test.Properties.No_named_install_directory) { |
| 406 | test.binaryDecorator.baseInstaller.relative = ctx.ModuleName() |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 407 | } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" { |
Dan Willemsen | 3340d60 | 2016-12-27 14:40:40 -0800 | [diff] [blame] | 408 | ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set") |
| 409 | } |
| 410 | |
Julien Desprez | 8908b37 | 2021-02-04 10:10:16 -0800 | [diff] [blame] | 411 | if ctx.Host() && test.gtest() && test.Properties.Test_options.Unit_test == nil { |
Julien Desprez | 3b933d3 | 2021-01-14 11:49:15 -0800 | [diff] [blame] | 412 | test.Properties.Test_options.Unit_test = proptools.BoolPtr(true) |
| 413 | } |
Colin Cross | 5c1d5fb | 2023-11-15 12:39:40 -0800 | [diff] [blame] | 414 | |
Spandan Das | 2f238a9 | 2025-01-28 23:39:13 +0000 | [diff] [blame] | 415 | if !ctx.Config().KatiEnabled() { // TODO(spandandas): Remove the special case for kati |
Spandan Das | 323631f | 2025-01-24 21:57:47 +0000 | [diff] [blame] | 416 | // Install the test config in testcases/ directory for atest. |
Spandan Das | 323631f | 2025-01-24 21:57:47 +0000 | [diff] [blame] | 417 | c, ok := ctx.Module().(*Module) |
| 418 | if !ok { |
| 419 | ctx.ModuleErrorf("Not a cc_test module") |
| 420 | } |
Spandan Das | 2f238a9 | 2025-01-28 23:39:13 +0000 | [diff] [blame] | 421 | // Install configs in the root of $PRODUCT_OUT/testcases/$module |
Spandan Das | 323631f | 2025-01-24 21:57:47 +0000 | [diff] [blame] | 422 | testCases := android.PathForModuleInPartitionInstall(ctx, "testcases", ctx.ModuleName()+c.SubName()) |
Spandan Das | 2f238a9 | 2025-01-28 23:39:13 +0000 | [diff] [blame] | 423 | if ctx.PrimaryArch() { |
| 424 | if test.testConfig != nil { |
| 425 | ctx.InstallFile(testCases, ctx.ModuleName()+".config", test.testConfig) |
| 426 | } |
| 427 | for _, extraTestConfig := range test.extraTestConfigs { |
| 428 | ctx.InstallFile(testCases, extraTestConfig.Base(), extraTestConfig) |
| 429 | } |
Spandan Das | 323631f | 2025-01-24 21:57:47 +0000 | [diff] [blame] | 430 | } |
Spandan Das | 2f238a9 | 2025-01-28 23:39:13 +0000 | [diff] [blame] | 431 | // Install tests and data in arch specific subdir $PRODUCT_OUT/testcases/$module/$arch |
| 432 | testCases = testCases.Join(ctx, ctx.Target().Arch.ArchType.String()) |
Spandan Das | 323631f | 2025-01-24 21:57:47 +0000 | [diff] [blame] | 433 | ctx.InstallTestData(testCases, test.data) |
| 434 | ctx.InstallFile(testCases, file.Base(), file) |
| 435 | } |
| 436 | |
Colin Cross | 5c1d5fb | 2023-11-15 12:39:40 -0800 | [diff] [blame] | 437 | test.binaryDecorator.baseInstaller.installTestData(ctx, test.data) |
Dan Willemsen | 1d577e2 | 2016-08-29 15:53:15 -0700 | [diff] [blame] | 438 | test.binaryDecorator.baseInstaller.install(ctx, file) |
Yifeng Zeng | 1876f5b | 2025-02-05 17:00:56 -0800 | [diff] [blame] | 439 | if Bool(test.Properties.Standalone_test) { |
| 440 | packagingSpecsBuilder := depset.NewBuilder[android.PackagingSpec](depset.TOPOLOGICAL) |
| 441 | |
| 442 | ctx.VisitDirectDeps(func(dep android.Module) { |
| 443 | deps := android.OtherModuleProviderOrDefault(ctx, dep, android.InstallFilesProvider) |
| 444 | packagingSpecsBuilder.Transitive(deps.TransitivePackagingSpecs) |
| 445 | }) |
| 446 | |
| 447 | for _, standaloneTestDep := range packagingSpecsBuilder.Build().ToList() { |
| 448 | if standaloneTestDep.ToGob().SrcPath == nil { |
| 449 | continue |
| 450 | } |
Yifeng Zeng | 02c56e9 | 2025-02-12 13:25:16 -0800 | [diff] [blame] | 451 | if standaloneTestDep.SkipInstall() { |
| 452 | continue |
| 453 | } |
Yifeng Zeng | 1876f5b | 2025-02-05 17:00:56 -0800 | [diff] [blame] | 454 | test.binaryDecorator.baseInstaller.installStandaloneTestDep(ctx, standaloneTestDep) |
| 455 | } |
| 456 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 459 | func getTestInstallBase(useVendor bool) string { |
| 460 | // TODO: (b/167308193) Switch to /data/local/tests/unrestricted as the default install base. |
| 461 | testInstallBase := "/data/local/tmp" |
| 462 | if useVendor { |
| 463 | testInstallBase = "/data/local/tests/vendor" |
| 464 | } |
| 465 | return testInstallBase |
| 466 | } |
| 467 | |
Dan Shi | ad04250 | 2023-08-02 13:53:00 -0700 | [diff] [blame] | 468 | func getTradefedConfigOptions(ctx android.EarlyModuleContext, properties *TestBinaryProperties, isolated bool, device bool) []tradefed.Config { |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 469 | var configs []tradefed.Config |
| 470 | |
| 471 | for _, module := range properties.Test_mainline_modules { |
| 472 | configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module}) |
| 473 | } |
Dan Shi | ad04250 | 2023-08-02 13:53:00 -0700 | [diff] [blame] | 474 | if device { |
| 475 | if Bool(properties.Require_root) { |
| 476 | configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil}) |
| 477 | } else { |
| 478 | var options []tradefed.Option |
| 479 | options = append(options, tradefed.Option{Name: "force-root", Value: "false"}) |
| 480 | configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options}) |
| 481 | } |
| 482 | if Bool(properties.Disable_framework) { |
| 483 | var options []tradefed.Option |
| 484 | configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.StopServicesSetup", options}) |
| 485 | } |
Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 486 | } |
| 487 | if isolated { |
| 488 | configs = append(configs, tradefed.Option{Name: "not-shardable", Value: "true"}) |
| 489 | } |
| 490 | if properties.Test_options.Run_test_as != nil { |
| 491 | configs = append(configs, tradefed.Option{Name: "run-test-as", Value: String(properties.Test_options.Run_test_as)}) |
| 492 | } |
| 493 | for _, tag := range properties.Test_options.Test_suite_tag { |
| 494 | configs = append(configs, tradefed.Option{Name: "test-suite-tag", Value: tag}) |
| 495 | } |
| 496 | if properties.Test_options.Min_shipping_api_level != nil { |
| 497 | if properties.Test_options.Vsr_min_shipping_api_level != nil { |
| 498 | ctx.PropertyErrorf("test_options.min_shipping_api_level", "must not be set at the same time as 'vsr_min_shipping_api_level'.") |
| 499 | } |
| 500 | var options []tradefed.Option |
| 501 | options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_shipping_api_level), 10)}) |
| 502 | configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options}) |
| 503 | } |
| 504 | if properties.Test_options.Vsr_min_shipping_api_level != nil { |
| 505 | var options []tradefed.Option |
| 506 | options = append(options, tradefed.Option{Name: "vsr-min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Vsr_min_shipping_api_level), 10)}) |
| 507 | configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options}) |
| 508 | } |
| 509 | if properties.Test_options.Min_vndk_version != nil { |
| 510 | var options []tradefed.Option |
| 511 | options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_vndk_version), 10)}) |
| 512 | options = append(options, tradefed.Option{Name: "api-level-prop", Value: "ro.vndk.version"}) |
| 513 | configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.MinApiLevelModuleController", options}) |
| 514 | } |
| 515 | return configs |
| 516 | } |
| 517 | |
Colin Cross | 8ff1058 | 2023-12-07 13:10:56 -0800 | [diff] [blame] | 518 | func NewTest(hod android.HostOrDeviceSupported) *Module { |
| 519 | module, binary := newBinary(hod) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 520 | module.multilib = android.MultilibBoth |
Aditya Choudhary | 4b6eaf4 | 2023-11-21 15:38:37 +0000 | [diff] [blame] | 521 | module.testModule = true |
Dan Willemsen | 1d577e2 | 2016-08-29 15:53:15 -0700 | [diff] [blame] | 522 | binary.baseInstaller = NewTestInstaller() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 523 | |
| 524 | test := &testBinary{ |
Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 525 | testDecorator: &testDecorator{ |
| 526 | linker: binary.baseLinker, |
| 527 | installer: binary.baseInstaller, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 528 | }, |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 529 | binaryDecorator: binary, |
| 530 | baseCompiler: NewBaseCompiler(), |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 531 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 532 | module.compiler = test |
| 533 | module.linker = test |
| 534 | module.installer = test |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 535 | return module |
| 536 | } |
| 537 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 538 | type testLibrary struct { |
Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 539 | *testDecorator |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 540 | *libraryDecorator |
| 541 | } |
| 542 | |
Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 543 | func (test *testLibrary) testLibrary() bool { |
| 544 | return true |
| 545 | } |
| 546 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 547 | func (test *testLibrary) linkerProps() []interface{} { |
Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 548 | var props []interface{} |
| 549 | props = append(props, test.testDecorator.linkerProps()...) |
| 550 | return append(props, test.libraryDecorator.linkerProps()...) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 553 | func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 554 | deps = test.testDecorator.linkerDeps(ctx, deps) |
| 555 | deps = test.libraryDecorator.linkerDeps(ctx, deps) |
| 556 | return deps |
| 557 | } |
| 558 | |
| 559 | func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 560 | flags = test.libraryDecorator.linkerFlags(ctx, flags) |
| 561 | flags = test.testDecorator.linkerFlags(ctx, flags) |
| 562 | return flags |
| 563 | } |
| 564 | |
Colin Cross | 4a9e6ec | 2023-12-18 15:29:41 -0800 | [diff] [blame] | 565 | func (test *testLibrary) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) { |
| 566 | if len(test.InstallerProperties.Test_suites) > 0 { |
| 567 | moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, test.InstallerProperties.Test_suites...) |
| 568 | } |
| 569 | |
| 570 | test.libraryDecorator.moduleInfoJSON(ctx, moduleInfoJSON) |
| 571 | test.testDecorator.moduleInfoJSON(ctx, moduleInfoJSON) |
| 572 | } |
| 573 | |
Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 574 | func (test *testLibrary) installerProps() []interface{} { |
| 575 | return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...) |
| 576 | } |
| 577 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 578 | func NewTestLibrary(hod android.HostOrDeviceSupported) *Module { |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 579 | module, library := NewLibrary(android.HostAndDeviceSupported) |
Dan Willemsen | 28bda51 | 2016-08-31 16:32:55 -0700 | [diff] [blame] | 580 | library.baseInstaller = NewTestInstaller() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 581 | test := &testLibrary{ |
Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 582 | testDecorator: &testDecorator{ |
| 583 | linker: library.baseLinker, |
| 584 | installer: library.baseInstaller, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 585 | }, |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 586 | libraryDecorator: library, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 587 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 588 | module.linker = test |
Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 589 | module.installer = test |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 590 | return module |
| 591 | } |
| 592 | |
Colin Cross | e28f4e2 | 2017-04-24 18:10:29 -0700 | [diff] [blame] | 593 | type BenchmarkProperties struct { |
Anders Lewis | b97e818 | 2017-07-14 15:20:13 -0700 | [diff] [blame] | 594 | // list of files or filegroup modules that provide data that should be installed alongside |
| 595 | // the test |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 596 | Data []string `android:"path"` |
Anders Lewis | b97e818 | 2017-07-14 15:20:13 -0700 | [diff] [blame] | 597 | |
Colin Cross | e28f4e2 | 2017-04-24 18:10:29 -0700 | [diff] [blame] | 598 | // list of compatibility suites (for example "cts", "vts") that the module should be |
| 599 | // installed into. |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 600 | Test_suites []string `android:"arch_variant"` |
| 601 | |
| 602 | // the name of the test configuration (for example "AndroidTest.xml") that should be |
| 603 | // installed with the module. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 604 | Test_config *string `android:"path,arch_variant"` |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 605 | |
| 606 | // the name of the test configuration template (for example "AndroidTestTemplate.xml") that |
| 607 | // should be installed with the module. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 608 | Test_config_template *string `android:"path,arch_variant"` |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 609 | |
| 610 | // Add RootTargetPreparer to auto generated test config. This guarantees the test to run |
| 611 | // with root permission. |
| 612 | Require_root *bool |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 613 | |
| 614 | // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml |
| 615 | // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true |
| 616 | // explicitly. |
| 617 | Auto_gen_config *bool |
Colin Cross | e28f4e2 | 2017-04-24 18:10:29 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 620 | type benchmarkDecorator struct { |
| 621 | *binaryDecorator |
Colin Cross | e28f4e2 | 2017-04-24 18:10:29 -0700 | [diff] [blame] | 622 | Properties BenchmarkProperties |
Colin Cross | 5c1d5fb | 2023-11-15 12:39:40 -0800 | [diff] [blame] | 623 | data []android.DataPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 624 | testConfig android.Path |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 627 | func (benchmark *benchmarkDecorator) benchmarkBinary() bool { |
| 628 | return true |
| 629 | } |
| 630 | |
Colin Cross | e28f4e2 | 2017-04-24 18:10:29 -0700 | [diff] [blame] | 631 | func (benchmark *benchmarkDecorator) linkerProps() []interface{} { |
| 632 | props := benchmark.binaryDecorator.linkerProps() |
| 633 | props = append(props, &benchmark.Properties) |
| 634 | return props |
| 635 | } |
| 636 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 637 | func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 638 | deps = benchmark.binaryDecorator.linkerDeps(ctx, deps) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 639 | deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark") |
| 640 | return deps |
| 641 | } |
| 642 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 643 | func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) { |
Colin Cross | 5c1d5fb | 2023-11-15 12:39:40 -0800 | [diff] [blame] | 644 | for _, d := range android.PathsForModuleSrc(ctx, benchmark.Properties.Data) { |
| 645 | benchmark.data = append(benchmark.data, android.DataPath{SrcPath: d}) |
| 646 | } |
Chris Parsons | 79d66a5 | 2020-06-05 17:26:16 -0400 | [diff] [blame] | 647 | |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 648 | var configs []tradefed.Config |
| 649 | if Bool(benchmark.Properties.Require_root) { |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 650 | configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil}) |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 651 | } |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 652 | benchmark.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{ |
| 653 | TestConfigProp: benchmark.Properties.Test_config, |
| 654 | TestConfigTemplateProp: benchmark.Properties.Test_config_template, |
| 655 | TestSuites: benchmark.Properties.Test_suites, |
| 656 | Config: configs, |
| 657 | AutoGenConfig: benchmark.Properties.Auto_gen_config, |
| 658 | DeviceTemplate: "${NativeBenchmarkTestConfigTemplate}", |
| 659 | HostTemplate: "${NativeBenchmarkTestConfigTemplate}", |
| 660 | }) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 661 | |
Colin Cross | 28690e9 | 2017-09-08 16:20:30 -0700 | [diff] [blame] | 662 | benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName()) |
| 663 | benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName()) |
Colin Cross | 5c1d5fb | 2023-11-15 12:39:40 -0800 | [diff] [blame] | 664 | benchmark.binaryDecorator.baseInstaller.installTestData(ctx, benchmark.data) |
Dan Willemsen | 1d577e2 | 2016-08-29 15:53:15 -0700 | [diff] [blame] | 665 | benchmark.binaryDecorator.baseInstaller.install(ctx, file) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Colin Cross | 4a9e6ec | 2023-12-18 15:29:41 -0800 | [diff] [blame] | 668 | func (benchmark *benchmarkDecorator) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) { |
| 669 | benchmark.binaryDecorator.moduleInfoJSON(ctx, moduleInfoJSON) |
| 670 | |
| 671 | moduleInfoJSON.Class = []string{"NATIVE_TESTS"} |
| 672 | if len(benchmark.Properties.Test_suites) > 0 { |
| 673 | moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, benchmark.Properties.Test_suites...) |
| 674 | } else { |
| 675 | moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "null-suite") |
| 676 | } |
| 677 | |
| 678 | if android.PrefixInList(moduleInfoJSON.CompatibilitySuites, "mts-") && |
| 679 | !android.InList("mts", moduleInfoJSON.CompatibilitySuites) { |
| 680 | moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "mts") |
| 681 | } |
| 682 | |
| 683 | if benchmark.testConfig != nil { |
| 684 | if _, ok := benchmark.testConfig.(android.WritablePath); ok { |
| 685 | moduleInfoJSON.AutoTestConfig = []string{"true"} |
| 686 | } |
| 687 | moduleInfoJSON.TestConfig = []string{benchmark.testConfig.String()} |
| 688 | } |
| 689 | } |
| 690 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 691 | func NewBenchmark(hod android.HostOrDeviceSupported) *Module { |
Colin Cross | 8ff1058 | 2023-12-07 13:10:56 -0800 | [diff] [blame] | 692 | module, binary := newBinary(hod) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 693 | module.multilib = android.MultilibBoth |
Colin Cross | 28690e9 | 2017-09-08 16:20:30 -0700 | [diff] [blame] | 694 | binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 695 | |
| 696 | benchmark := &benchmarkDecorator{ |
| 697 | binaryDecorator: binary, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 698 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 699 | module.linker = benchmark |
| 700 | module.installer = benchmark |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 701 | return module |
| 702 | } |