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