| 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" | 
| Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 25 | "android/soong/bazel" | 
| Jingwen Chen | 3952a90 | 2022-12-12 12:20:58 +0000 | [diff] [blame] | 26 | "android/soong/bazel/cquery" | 
| Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 27 | "android/soong/tradefed" | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 28 | ) | 
|  | 29 |  | 
| Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 30 | // TestLinkerProperties properties to be registered via the linker | 
|  | 31 | type TestLinkerProperties struct { | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 32 | // if set, build against the gtest library. Defaults to true. | 
| Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 33 | Gtest *bool | 
| Christopher Ferris | 9df92d6 | 2018-08-21 12:40:08 -0700 | [diff] [blame] | 34 |  | 
| Trevor Radcliffe | cd8fd67 | 2022-05-03 14:51:16 +0000 | [diff] [blame] | 35 | // if set, use the isolated gtest runner. Defaults to true if gtest is also true and the arch is Windows, false | 
|  | 36 | // otherwise. | 
| Christopher Ferris | 9df92d6 | 2018-08-21 12:40:08 -0700 | [diff] [blame] | 37 | Isolated *bool | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 38 | } | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 39 |  | 
| Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 40 | // TestInstallerProperties properties to be registered via the installer | 
|  | 41 | type TestInstallerProperties struct { | 
|  | 42 | // list of compatibility suites (for example "cts", "vts") that the module should be installed into. | 
|  | 43 | Test_suites []string `android:"arch_variant"` | 
|  | 44 | } | 
|  | 45 |  | 
| yelinhsieh | 9fc6040 | 2018-10-01 19:23:14 +0800 | [diff] [blame] | 46 | // Test option struct. | 
|  | 47 | type TestOptions struct { | 
| Zhenhuang Wang | 0ac5a43 | 2022-08-12 18:49:20 +0800 | [diff] [blame] | 48 | android.CommonTestOptions | 
|  | 49 |  | 
| yelinhsieh | 9fc6040 | 2018-10-01 19:23:14 +0800 | [diff] [blame] | 50 | // The UID that you want to run the test as on a device. | 
|  | 51 | Run_test_as *string | 
| Dan Shi | 95d1942 | 2020-08-15 12:24:26 -0700 | [diff] [blame] | 52 |  | 
| David Srbecky | 519db27 | 2020-06-18 18:07:00 +0100 | [diff] [blame] | 53 | // A list of free-formed strings without spaces that categorize the test. | 
|  | 54 | Test_suite_tag []string | 
| Dan Shi | 95d1942 | 2020-08-15 12:24:26 -0700 | [diff] [blame] | 55 |  | 
|  | 56 | // a list of extra test configuration files that should be installed with the module. | 
|  | 57 | Extra_test_configs []string `android:"path,arch_variant"` | 
| Dan Shi | d79572f | 2020-11-13 14:33:46 -0800 | [diff] [blame] | 58 |  | 
| Justin Yun | 46f6605 | 2021-05-04 18:42:24 +0900 | [diff] [blame] | 59 | // Add ShippingApiLevelModuleController to auto generated test config. If the device properties | 
| Justin Yun | a364cfb | 2021-05-13 12:39:42 +0900 | [diff] [blame] | 60 | // for the shipping api level is less than the min_shipping_api_level, skip this module. | 
|  | 61 | Min_shipping_api_level *int64 | 
|  | 62 |  | 
|  | 63 | // Add ShippingApiLevelModuleController to auto generated test config. If any of the device | 
|  | 64 | // shipping api level and vendor api level properties are less than the | 
|  | 65 | // vsr_min_shipping_api_level, skip this module. | 
|  | 66 | // As this includes the shipping api level check, it is not allowed to define | 
|  | 67 | // min_shipping_api_level at the same time with this property. | 
|  | 68 | Vsr_min_shipping_api_level *int64 | 
| Justin Yun | 46f6605 | 2021-05-04 18:42:24 +0900 | [diff] [blame] | 69 |  | 
|  | 70 | // 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] | 71 | // integer value and the value is less than the min_vndk_version, skip this module. | 
|  | 72 | Min_vndk_version *int64 | 
| Dan Shi | ec73143 | 2023-05-26 04:21:44 +0000 | [diff] [blame] | 73 |  | 
|  | 74 | // Extra <option> tags to add to the auto generated test xml file under the test runner, e.g., GTest. | 
|  | 75 | // The "key" is optional in each of these. | 
|  | 76 | Test_runner_options []tradefed.Option | 
| yelinhsieh | 9fc6040 | 2018-10-01 19:23:14 +0800 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 79 | type TestBinaryProperties struct { | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 80 | // Create a separate binary for each source file.  Useful when there is | 
|  | 81 | // global state that can not be torn down and reset between each test suite. | 
|  | 82 | Test_per_src *bool | 
| Dan Willemsen | 3340d60 | 2016-12-27 14:40:40 -0800 | [diff] [blame] | 83 |  | 
|  | 84 | // Disables the creation of a test-specific directory when used with | 
|  | 85 | // relative_install_path. Useful if several tests need to be in the same | 
|  | 86 | // directory, but test_per_src doesn't work. | 
|  | 87 | No_named_install_directory *bool | 
| Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 88 |  | 
|  | 89 | // list of files or filegroup modules that provide data that should be installed alongside | 
|  | 90 | // the test | 
| Dan Shi | 67a8834 | 2020-02-25 16:34:39 -0800 | [diff] [blame] | 91 | Data []string `android:"path,arch_variant"` | 
| Colin Cross | a929db0 | 2017-03-27 16:27:50 -0700 | [diff] [blame] | 92 |  | 
| Chris Parsons | 79d66a5 | 2020-06-05 17:26:16 -0400 | [diff] [blame] | 93 | // list of shared library modules that should be installed alongside the test | 
|  | 94 | Data_libs []string `android:"arch_variant"` | 
|  | 95 |  | 
| Colin Cross | c8caa06 | 2021-09-24 16:50:14 -0700 | [diff] [blame] | 96 | // list of binary modules that should be installed alongside the test | 
|  | 97 | Data_bins []string `android:"arch_variant"` | 
|  | 98 |  | 
| Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 99 | // the name of the test configuration (for example "AndroidTest.xml") that should be | 
|  | 100 | // installed with the module. | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 101 | Test_config *string `android:"path,arch_variant"` | 
| Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 102 |  | 
|  | 103 | // the name of the test configuration template (for example "AndroidTestTemplate.xml") that | 
|  | 104 | // should be installed with the module. | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 105 | Test_config_template *string `android:"path,arch_variant"` | 
| yelinhsieh | 9fc6040 | 2018-10-01 19:23:14 +0800 | [diff] [blame] | 106 |  | 
|  | 107 | // Test options. | 
|  | 108 | Test_options TestOptions | 
| Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 109 |  | 
|  | 110 | // Add RootTargetPreparer to auto generated test config. This guarantees the test to run | 
|  | 111 | // with root permission. | 
|  | 112 | Require_root *bool | 
| Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 113 |  | 
|  | 114 | // Add RunCommandTargetPreparer to stop framework before the test and start it after the test. | 
|  | 115 | Disable_framework *bool | 
| nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 116 |  | 
| Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 117 | // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml | 
|  | 118 | // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true | 
|  | 119 | // explicitly. | 
|  | 120 | Auto_gen_config *bool | 
| easoncylee | 1e3fdcd | 2020-04-30 10:08:33 +0800 | [diff] [blame] | 121 |  | 
|  | 122 | // Add parameterized mainline modules to auto generated test config. The options will be | 
|  | 123 | // handled by TradeFed to download and install the specified modules on the device. | 
|  | 124 | Test_mainline_modules []string | 
| Colin Cross | cfb0f5e | 2021-09-24 15:47:17 -0700 | [diff] [blame] | 125 |  | 
|  | 126 | // Install the test into a folder named for the module in all test suites. | 
|  | 127 | Per_testcase_directory *bool | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 128 | } | 
|  | 129 |  | 
|  | 130 | func init() { | 
| Steven Moreland | 87c9d7b | 2017-11-02 21:38:28 -0700 | [diff] [blame] | 131 | android.RegisterModuleType("cc_test", TestFactory) | 
|  | 132 | android.RegisterModuleType("cc_test_library", TestLibraryFactory) | 
|  | 133 | android.RegisterModuleType("cc_benchmark", BenchmarkFactory) | 
|  | 134 | android.RegisterModuleType("cc_test_host", TestHostFactory) | 
|  | 135 | android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory) | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 136 | } | 
|  | 137 |  | 
| Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 138 | // cc_test generates a test config file and an executable binary file to test | 
|  | 139 | // specific functionality on a device. The executable binary gets an implicit | 
|  | 140 | // 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] | 141 | func TestFactory() android.Module { | 
| Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 142 | module := NewTest(android.HostAndDeviceSupported, true) | 
| Jingwen Chen | 3952a90 | 2022-12-12 12:20:58 +0000 | [diff] [blame] | 143 | module.bazelHandler = &ccTestBazelHandler{module: module} | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 144 | return module.Init() | 
|  | 145 | } | 
|  | 146 |  | 
| Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 147 | // cc_test_library creates an archive of files (i.e. .o files) which is later | 
|  | 148 | // referenced by another module (such as cc_test, cc_defaults or cc_test_library) | 
|  | 149 | // for archiving or linking. | 
| Steven Moreland | 87c9d7b | 2017-11-02 21:38:28 -0700 | [diff] [blame] | 150 | func TestLibraryFactory() android.Module { | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 151 | module := NewTestLibrary(android.HostAndDeviceSupported) | 
|  | 152 | return module.Init() | 
|  | 153 | } | 
|  | 154 |  | 
| Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 155 | // cc_benchmark compiles an executable binary that performs benchmark testing | 
|  | 156 | // of a specific component in a device. Additional files such as test suites | 
|  | 157 | // and test configuration are installed on the side of the compiled executed | 
|  | 158 | // binary. | 
| Steven Moreland | 87c9d7b | 2017-11-02 21:38:28 -0700 | [diff] [blame] | 159 | func BenchmarkFactory() android.Module { | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 160 | module := NewBenchmark(android.HostAndDeviceSupported) | 
| Aditya Choudhary | 87b2ab2 | 2023-11-17 15:27:06 +0000 | [diff] [blame] | 161 | module.testModule = true | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 162 | return module.Init() | 
|  | 163 | } | 
|  | 164 |  | 
| Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 165 | // cc_test_host compiles a test host binary. | 
| Steven Moreland | 87c9d7b | 2017-11-02 21:38:28 -0700 | [diff] [blame] | 166 | func TestHostFactory() android.Module { | 
| Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 167 | module := NewTest(android.HostSupported, true) | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 168 | return module.Init() | 
|  | 169 | } | 
|  | 170 |  | 
| Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 171 | // cc_benchmark_host compiles an executable binary that performs benchmark | 
|  | 172 | // testing of a specific component in the host. Additional files such as | 
|  | 173 | // test suites and test configuration are installed on the side of the | 
|  | 174 | // compiled executed binary. | 
| Steven Moreland | 87c9d7b | 2017-11-02 21:38:28 -0700 | [diff] [blame] | 175 | func BenchmarkHostFactory() android.Module { | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 176 | module := NewBenchmark(android.HostSupported) | 
|  | 177 | return module.Init() | 
|  | 178 | } | 
|  | 179 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 180 | type testPerSrc interface { | 
|  | 181 | testPerSrc() bool | 
|  | 182 | srcs() []string | 
| Roland Levillain | f2fad97 | 2019-06-28 15:41:19 +0100 | [diff] [blame] | 183 | isAllTestsVariation() bool | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 184 | setSrc(string, string) | 
| Roland Levillain | f2fad97 | 2019-06-28 15:41:19 +0100 | [diff] [blame] | 185 | unsetSrc() | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 186 | } | 
|  | 187 |  | 
|  | 188 | func (test *testBinary) testPerSrc() bool { | 
|  | 189 | return Bool(test.Properties.Test_per_src) | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | func (test *testBinary) srcs() []string { | 
|  | 193 | return test.baseCompiler.Properties.Srcs | 
|  | 194 | } | 
|  | 195 |  | 
| Chris Parsons | 216e10a | 2020-07-09 17:12:52 -0400 | [diff] [blame] | 196 | func (test *testBinary) dataPaths() []android.DataPath { | 
| Liz Kammer | 1c14a21 | 2020-05-12 15:26:55 -0700 | [diff] [blame] | 197 | return test.data | 
|  | 198 | } | 
|  | 199 |  | 
| Roland Levillain | f2fad97 | 2019-06-28 15:41:19 +0100 | [diff] [blame] | 200 | func (test *testBinary) isAllTestsVariation() bool { | 
|  | 201 | stem := test.binaryDecorator.Properties.Stem | 
|  | 202 | return stem != nil && *stem == "" | 
|  | 203 | } | 
|  | 204 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 205 | func (test *testBinary) setSrc(name, src string) { | 
|  | 206 | test.baseCompiler.Properties.Srcs = []string{src} | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 207 | test.binaryDecorator.Properties.Stem = StringPtr(name) | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 208 | } | 
|  | 209 |  | 
| Roland Levillain | f2fad97 | 2019-06-28 15:41:19 +0100 | [diff] [blame] | 210 | func (test *testBinary) unsetSrc() { | 
|  | 211 | test.baseCompiler.Properties.Srcs = nil | 
|  | 212 | test.binaryDecorator.Properties.Stem = StringPtr("") | 
|  | 213 | } | 
|  | 214 |  | 
| Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 215 | func (test *testBinary) testBinary() bool { | 
|  | 216 | return true | 
|  | 217 | } | 
|  | 218 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 219 | var _ testPerSrc = (*testBinary)(nil) | 
|  | 220 |  | 
| Roland Levillain | 9b5fde9 | 2019-06-28 15:41:19 +0100 | [diff] [blame] | 221 | func TestPerSrcMutator(mctx android.BottomUpMutatorContext) { | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 222 | if m, ok := mctx.Module().(*Module); ok { | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 223 | if test, ok := m.linker.(testPerSrc); ok { | 
| Roland Levillain | f2fad97 | 2019-06-28 15:41:19 +0100 | [diff] [blame] | 224 | numTests := len(test.srcs()) | 
|  | 225 | if test.testPerSrc() && numTests > 0 { | 
| Chih-Hung Hsieh | a5f22ed | 2019-10-24 20:47:54 -0700 | [diff] [blame] | 226 | if duplicate, found := android.CheckDuplicate(test.srcs()); found { | 
| Jooyung Han | a61ff2c | 2019-02-28 18:06:34 +0900 | [diff] [blame] | 227 | mctx.PropertyErrorf("srcs", "found a duplicate entry %q", duplicate) | 
|  | 228 | return | 
|  | 229 | } | 
| Roland Levillain | f2fad97 | 2019-06-28 15:41:19 +0100 | [diff] [blame] | 230 | testNames := make([]string, numTests) | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 231 | for i, src := range test.srcs() { | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 232 | testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src)) | 
|  | 233 | } | 
| Roland Levillain | f2fad97 | 2019-06-28 15:41:19 +0100 | [diff] [blame] | 234 | // In addition to creating one variation per test source file, | 
|  | 235 | // create an additional "all tests" variation named "", and have it | 
|  | 236 | // depends on all other test_per_src variations. This is useful to | 
|  | 237 | // create subsequent dependencies of a given module on all | 
|  | 238 | // test_per_src variations created above: by depending on | 
|  | 239 | // variation "", that module will transitively depend on all the | 
|  | 240 | // other test_per_src variations without the need to know their | 
|  | 241 | // name or even their number. | 
|  | 242 | testNames = append(testNames, "") | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 243 | tests := mctx.CreateLocalVariations(testNames...) | 
| Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 244 | allTests := tests[numTests] | 
|  | 245 | allTests.(*Module).linker.(testPerSrc).unsetSrc() | 
| Roland Levillain | f2fad97 | 2019-06-28 15:41:19 +0100 | [diff] [blame] | 246 | // Prevent the "all tests" variation from being installable nor | 
|  | 247 | // exporting to Make, as it won't create any output file. | 
| Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 248 | allTests.(*Module).Properties.PreventInstall = true | 
|  | 249 | allTests.(*Module).Properties.HideFromMake = true | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 250 | for i, src := range test.srcs() { | 
|  | 251 | tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src) | 
| Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 252 | mctx.AddInterVariantDependency(testPerSrcDepTag, allTests, tests[i]) | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 253 | } | 
| Colin Cross | 90dab34 | 2020-08-21 15:55:50 -0700 | [diff] [blame] | 254 | mctx.AliasVariation("") | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 255 | } | 
|  | 256 | } | 
|  | 257 | } | 
|  | 258 | } | 
|  | 259 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 260 | type testDecorator struct { | 
| Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 261 | LinkerProperties    TestLinkerProperties | 
|  | 262 | InstallerProperties TestInstallerProperties | 
|  | 263 | installer           *baseInstaller | 
|  | 264 | linker              *baseLinker | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 265 | } | 
|  | 266 |  | 
| Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 267 | func (test *testDecorator) gtest() bool { | 
| Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 268 | return BoolDefault(test.LinkerProperties.Gtest, true) | 
| Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 269 | } | 
|  | 270 |  | 
| Spandan Das | f5a8655 | 2023-07-22 03:16:53 +0000 | [diff] [blame] | 271 | func (test *testDecorator) isolated(ctx android.EarlyModuleContext) bool { | 
| Trevor Radcliffe | cd8fd67 | 2022-05-03 14:51:16 +0000 | [diff] [blame] | 272 | return BoolDefault(test.LinkerProperties.Isolated, false) | 
|  | 273 | } | 
|  | 274 |  | 
| Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 275 | // NOTE: Keep this in sync with cc/cc_test.bzl#gtest_copts | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 276 | func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { | 
| Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 277 | if !test.gtest() { | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 278 | return flags | 
|  | 279 | } | 
|  | 280 |  | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 281 | flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_HAS_STD_STRING") | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 282 | if ctx.Host() { | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 283 | flags.Local.CFlags = append(flags.Local.CFlags, "-O0", "-g") | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 284 |  | 
|  | 285 | switch ctx.Os() { | 
|  | 286 | case android.Windows: | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 287 | flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_WINDOWS") | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 288 | case android.Linux: | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 289 | flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX") | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 290 | case android.Darwin: | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 291 | flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_MAC") | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 292 | } | 
|  | 293 | } else { | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 294 | flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX_ANDROID") | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 295 | } | 
|  | 296 |  | 
|  | 297 | return flags | 
|  | 298 | } | 
|  | 299 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 300 | func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps { | 
| Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 301 | if test.gtest() { | 
| Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 302 | if ctx.useSdk() && ctx.Device() { | 
| Dan Albert | 7dd5899 | 2018-02-09 15:22:59 -0800 | [diff] [blame] | 303 | deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++") | 
| Trevor Radcliffe | cd8fd67 | 2022-05-03 14:51:16 +0000 | [diff] [blame] | 304 | } else if test.isolated(ctx) { | 
| Christopher Ferris | 9df92d6 | 2018-08-21 12:40:08 -0700 | [diff] [blame] | 305 | deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main") | 
| Christopher Ferris | 34cbba6 | 2019-07-17 15:46:29 -0700 | [diff] [blame] | 306 | // The isolated library requires liblog, but adding it | 
|  | 307 | // as a static library means unit tests cannot override | 
|  | 308 | // liblog functions. Instead make it a shared library | 
|  | 309 | // dependency. | 
|  | 310 | deps.SharedLibs = append(deps.SharedLibs, "liblog") | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 311 | } else { | 
|  | 312 | deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest") | 
|  | 313 | } | 
|  | 314 | } | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 315 |  | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 316 | return deps | 
|  | 317 | } | 
|  | 318 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 319 | func (test *testDecorator) linkerProps() []interface{} { | 
| Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 320 | return []interface{}{&test.LinkerProperties} | 
|  | 321 | } | 
|  | 322 |  | 
|  | 323 | func (test *testDecorator) installerProps() []interface{} { | 
|  | 324 | return []interface{}{&test.InstallerProperties} | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 325 | } | 
|  | 326 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 327 | func NewTestInstaller() *baseInstaller { | 
|  | 328 | return NewBaseInstaller("nativetest", "nativetest64", InstallInData) | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 329 | } | 
|  | 330 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 331 | type testBinary struct { | 
| Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 332 | *testDecorator | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 333 | *binaryDecorator | 
|  | 334 | *baseCompiler | 
| Dan Shi | 95d1942 | 2020-08-15 12:24:26 -0700 | [diff] [blame] | 335 | Properties       TestBinaryProperties | 
|  | 336 | data             []android.DataPath | 
|  | 337 | testConfig       android.Path | 
|  | 338 | extraTestConfigs android.Paths | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 339 | } | 
|  | 340 |  | 
|  | 341 | func (test *testBinary) linkerProps() []interface{} { | 
|  | 342 | props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...) | 
|  | 343 | props = append(props, &test.Properties) | 
|  | 344 | return props | 
|  | 345 | } | 
|  | 346 |  | 
| Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 347 | func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps { | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 348 | deps = test.testDecorator.linkerDeps(ctx, deps) | 
|  | 349 | deps = test.binaryDecorator.linkerDeps(ctx, deps) | 
| Chris Parsons | 79d66a5 | 2020-06-05 17:26:16 -0400 | [diff] [blame] | 350 | deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...) | 
| Colin Cross | c8caa06 | 2021-09-24 16:50:14 -0700 | [diff] [blame] | 351 | deps.DataBins = append(deps.DataBins, test.Properties.Data_bins...) | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 352 | return deps | 
|  | 353 | } | 
|  | 354 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 355 | func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags { | 
|  | 356 | flags = test.binaryDecorator.linkerFlags(ctx, flags) | 
|  | 357 | flags = test.testDecorator.linkerFlags(ctx, flags) | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 358 | return flags | 
|  | 359 | } | 
|  | 360 |  | 
| Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 361 | func (test *testBinary) installerProps() []interface{} { | 
|  | 362 | return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...) | 
|  | 363 | } | 
|  | 364 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 365 | func (test *testBinary) install(ctx ModuleContext, file android.Path) { | 
| Chris Parsons | 216e10a | 2020-07-09 17:12:52 -0400 | [diff] [blame] | 366 | dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data) | 
|  | 367 |  | 
|  | 368 | for _, dataSrcPath := range dataSrcPaths { | 
|  | 369 | test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath}) | 
|  | 370 | } | 
| Chris Parsons | 79d66a5 | 2020-06-05 17:26:16 -0400 | [diff] [blame] | 371 |  | 
|  | 372 | ctx.VisitDirectDepsWithTag(dataLibDepTag, func(dep android.Module) { | 
|  | 373 | depName := ctx.OtherModuleName(dep) | 
| Ivan Lozano | 4e5f07d | 2021-11-04 14:09:38 -0400 | [diff] [blame] | 374 | linkableDep, ok := dep.(LinkableInterface) | 
| Chris Parsons | 79d66a5 | 2020-06-05 17:26:16 -0400 | [diff] [blame] | 375 | if !ok { | 
| Ivan Lozano | 4e5f07d | 2021-11-04 14:09:38 -0400 | [diff] [blame] | 376 | ctx.ModuleErrorf("data_lib %q is not a LinkableInterface module", depName) | 
| Chris Parsons | 79d66a5 | 2020-06-05 17:26:16 -0400 | [diff] [blame] | 377 | } | 
| Ivan Lozano | 4e5f07d | 2021-11-04 14:09:38 -0400 | [diff] [blame] | 378 | if linkableDep.OutputFile().Valid() { | 
| Chris Parsons | 216e10a | 2020-07-09 17:12:52 -0400 | [diff] [blame] | 379 | test.data = append(test.data, | 
| Ivan Lozano | 4e5f07d | 2021-11-04 14:09:38 -0400 | [diff] [blame] | 380 | android.DataPath{SrcPath: linkableDep.OutputFile().Path(), | 
|  | 381 | RelativeInstallPath: linkableDep.RelativeInstallPath()}) | 
| Chris Parsons | 79d66a5 | 2020-06-05 17:26:16 -0400 | [diff] [blame] | 382 | } | 
|  | 383 | }) | 
| Colin Cross | c8caa06 | 2021-09-24 16:50:14 -0700 | [diff] [blame] | 384 | ctx.VisitDirectDepsWithTag(dataBinDepTag, func(dep android.Module) { | 
|  | 385 | depName := ctx.OtherModuleName(dep) | 
| Ivan Lozano | 4e5f07d | 2021-11-04 14:09:38 -0400 | [diff] [blame] | 386 | linkableDep, ok := dep.(LinkableInterface) | 
| Colin Cross | c8caa06 | 2021-09-24 16:50:14 -0700 | [diff] [blame] | 387 | if !ok { | 
| Ivan Lozano | 4e5f07d | 2021-11-04 14:09:38 -0400 | [diff] [blame] | 388 | ctx.ModuleErrorf("data_bin %q is not a LinkableInterface module", depName) | 
| Colin Cross | c8caa06 | 2021-09-24 16:50:14 -0700 | [diff] [blame] | 389 | } | 
| Ivan Lozano | 4e5f07d | 2021-11-04 14:09:38 -0400 | [diff] [blame] | 390 | if linkableDep.OutputFile().Valid() { | 
| Colin Cross | c8caa06 | 2021-09-24 16:50:14 -0700 | [diff] [blame] | 391 | test.data = append(test.data, | 
| Ivan Lozano | 4e5f07d | 2021-11-04 14:09:38 -0400 | [diff] [blame] | 392 | android.DataPath{SrcPath: linkableDep.OutputFile().Path(), | 
|  | 393 | RelativeInstallPath: linkableDep.RelativeInstallPath()}) | 
| Colin Cross | c8caa06 | 2021-09-24 16:50:14 -0700 | [diff] [blame] | 394 | } | 
|  | 395 | }) | 
| Chris Parsons | 79d66a5 | 2020-06-05 17:26:16 -0400 | [diff] [blame] | 396 |  | 
| Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 397 | useVendor := ctx.inVendor() || ctx.useVndk() | 
|  | 398 | testInstallBase := getTestInstallBase(useVendor) | 
| Dan Shi | ad04250 | 2023-08-02 13:53:00 -0700 | [diff] [blame] | 399 | configs := getTradefedConfigOptions(ctx, &test.Properties, test.isolated(ctx), ctx.Device()) | 
| yelinhsieh | 9fc6040 | 2018-10-01 19:23:14 +0800 | [diff] [blame] | 400 |  | 
| Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 401 | test.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{ | 
|  | 402 | TestConfigProp:         test.Properties.Test_config, | 
|  | 403 | TestConfigTemplateProp: test.Properties.Test_config_template, | 
|  | 404 | TestSuites:             test.testDecorator.InstallerProperties.Test_suites, | 
|  | 405 | Config:                 configs, | 
| Dan Shi | ec73143 | 2023-05-26 04:21:44 +0000 | [diff] [blame] | 406 | TestRunnerOptions:      test.Properties.Test_options.Test_runner_options, | 
| Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 407 | AutoGenConfig:          test.Properties.Auto_gen_config, | 
|  | 408 | TestInstallBase:        testInstallBase, | 
|  | 409 | DeviceTemplate:         "${NativeTestConfigTemplate}", | 
|  | 410 | HostTemplate:           "${NativeHostTestConfigTemplate}", | 
|  | 411 | }) | 
| Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 412 |  | 
| Dan Shi | 95d1942 | 2020-08-15 12:24:26 -0700 | [diff] [blame] | 413 | test.extraTestConfigs = android.PathsForModuleSrc(ctx, test.Properties.Test_options.Extra_test_configs) | 
|  | 414 |  | 
| Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 415 | test.binaryDecorator.baseInstaller.dir = "nativetest" | 
|  | 416 | test.binaryDecorator.baseInstaller.dir64 = "nativetest64" | 
| Dan Willemsen | 3340d60 | 2016-12-27 14:40:40 -0800 | [diff] [blame] | 417 |  | 
|  | 418 | if !Bool(test.Properties.No_named_install_directory) { | 
|  | 419 | test.binaryDecorator.baseInstaller.relative = ctx.ModuleName() | 
| Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 420 | } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" { | 
| Dan Willemsen | 3340d60 | 2016-12-27 14:40:40 -0800 | [diff] [blame] | 421 | ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set") | 
|  | 422 | } | 
|  | 423 |  | 
| Julien Desprez | 8908b37 | 2021-02-04 10:10:16 -0800 | [diff] [blame] | 424 | if ctx.Host() && test.gtest() && test.Properties.Test_options.Unit_test == nil { | 
| Julien Desprez | 3b933d3 | 2021-01-14 11:49:15 -0800 | [diff] [blame] | 425 | test.Properties.Test_options.Unit_test = proptools.BoolPtr(true) | 
|  | 426 | } | 
| Dan Willemsen | 1d577e2 | 2016-08-29 15:53:15 -0700 | [diff] [blame] | 427 | test.binaryDecorator.baseInstaller.install(ctx, file) | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 428 | } | 
|  | 429 |  | 
| Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 430 | func getTestInstallBase(useVendor bool) string { | 
|  | 431 | // TODO: (b/167308193) Switch to /data/local/tests/unrestricted as the default install base. | 
|  | 432 | testInstallBase := "/data/local/tmp" | 
|  | 433 | if useVendor { | 
|  | 434 | testInstallBase = "/data/local/tests/vendor" | 
|  | 435 | } | 
|  | 436 | return testInstallBase | 
|  | 437 | } | 
|  | 438 |  | 
| Dan Shi | ad04250 | 2023-08-02 13:53:00 -0700 | [diff] [blame] | 439 | 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] | 440 | var configs []tradefed.Config | 
|  | 441 |  | 
|  | 442 | for _, module := range properties.Test_mainline_modules { | 
|  | 443 | configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module}) | 
|  | 444 | } | 
| Dan Shi | ad04250 | 2023-08-02 13:53:00 -0700 | [diff] [blame] | 445 | if device { | 
|  | 446 | if Bool(properties.Require_root) { | 
|  | 447 | configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil}) | 
|  | 448 | } else { | 
|  | 449 | var options []tradefed.Option | 
|  | 450 | options = append(options, tradefed.Option{Name: "force-root", Value: "false"}) | 
|  | 451 | configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options}) | 
|  | 452 | } | 
|  | 453 | if Bool(properties.Disable_framework) { | 
|  | 454 | var options []tradefed.Option | 
|  | 455 | configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.StopServicesSetup", options}) | 
|  | 456 | } | 
| Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 457 | } | 
|  | 458 | if isolated { | 
|  | 459 | configs = append(configs, tradefed.Option{Name: "not-shardable", Value: "true"}) | 
|  | 460 | } | 
|  | 461 | if properties.Test_options.Run_test_as != nil { | 
|  | 462 | configs = append(configs, tradefed.Option{Name: "run-test-as", Value: String(properties.Test_options.Run_test_as)}) | 
|  | 463 | } | 
|  | 464 | for _, tag := range properties.Test_options.Test_suite_tag { | 
|  | 465 | configs = append(configs, tradefed.Option{Name: "test-suite-tag", Value: tag}) | 
|  | 466 | } | 
|  | 467 | if properties.Test_options.Min_shipping_api_level != nil { | 
|  | 468 | if properties.Test_options.Vsr_min_shipping_api_level != nil { | 
|  | 469 | ctx.PropertyErrorf("test_options.min_shipping_api_level", "must not be set at the same time as 'vsr_min_shipping_api_level'.") | 
|  | 470 | } | 
|  | 471 | var options []tradefed.Option | 
|  | 472 | options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_shipping_api_level), 10)}) | 
|  | 473 | configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options}) | 
|  | 474 | } | 
|  | 475 | if properties.Test_options.Vsr_min_shipping_api_level != nil { | 
|  | 476 | var options []tradefed.Option | 
|  | 477 | options = append(options, tradefed.Option{Name: "vsr-min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Vsr_min_shipping_api_level), 10)}) | 
|  | 478 | configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options}) | 
|  | 479 | } | 
|  | 480 | if properties.Test_options.Min_vndk_version != nil { | 
|  | 481 | var options []tradefed.Option | 
|  | 482 | options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_vndk_version), 10)}) | 
|  | 483 | options = append(options, tradefed.Option{Name: "api-level-prop", Value: "ro.vndk.version"}) | 
|  | 484 | configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.MinApiLevelModuleController", options}) | 
|  | 485 | } | 
|  | 486 | return configs | 
|  | 487 | } | 
|  | 488 |  | 
| Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 489 | func NewTest(hod android.HostOrDeviceSupported, bazelable bool) *Module { | 
|  | 490 | module, binary := newBinary(hod, bazelable) | 
| Jingwen Chen | 3952a90 | 2022-12-12 12:20:58 +0000 | [diff] [blame] | 491 | module.bazelable = bazelable | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 492 | module.multilib = android.MultilibBoth | 
| Aditya Choudhary | 4b6eaf4 | 2023-11-21 15:38:37 +0000 | [diff] [blame] | 493 | module.testModule = true | 
| Dan Willemsen | 1d577e2 | 2016-08-29 15:53:15 -0700 | [diff] [blame] | 494 | binary.baseInstaller = NewTestInstaller() | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 495 |  | 
|  | 496 | test := &testBinary{ | 
| Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 497 | testDecorator: &testDecorator{ | 
|  | 498 | linker:    binary.baseLinker, | 
|  | 499 | installer: binary.baseInstaller, | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 500 | }, | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 501 | binaryDecorator: binary, | 
|  | 502 | baseCompiler:    NewBaseCompiler(), | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 503 | } | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 504 | module.compiler = test | 
|  | 505 | module.linker = test | 
|  | 506 | module.installer = test | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 507 | return module | 
|  | 508 | } | 
|  | 509 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 510 | type testLibrary struct { | 
| Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 511 | *testDecorator | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 512 | *libraryDecorator | 
|  | 513 | } | 
|  | 514 |  | 
| Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 515 | func (test *testLibrary) testLibrary() bool { | 
|  | 516 | return true | 
|  | 517 | } | 
|  | 518 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 519 | func (test *testLibrary) linkerProps() []interface{} { | 
| Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 520 | var props []interface{} | 
|  | 521 | props = append(props, test.testDecorator.linkerProps()...) | 
|  | 522 | return append(props, test.libraryDecorator.linkerProps()...) | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 523 | } | 
|  | 524 |  | 
| Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 525 | func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps { | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 526 | deps = test.testDecorator.linkerDeps(ctx, deps) | 
|  | 527 | deps = test.libraryDecorator.linkerDeps(ctx, deps) | 
|  | 528 | return deps | 
|  | 529 | } | 
|  | 530 |  | 
|  | 531 | func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags { | 
|  | 532 | flags = test.libraryDecorator.linkerFlags(ctx, flags) | 
|  | 533 | flags = test.testDecorator.linkerFlags(ctx, flags) | 
|  | 534 | return flags | 
|  | 535 | } | 
|  | 536 |  | 
| Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 537 | func (test *testLibrary) installerProps() []interface{} { | 
|  | 538 | return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...) | 
|  | 539 | } | 
|  | 540 |  | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 541 | func NewTestLibrary(hod android.HostOrDeviceSupported) *Module { | 
| Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 542 | module, library := NewLibrary(android.HostAndDeviceSupported) | 
| Dan Willemsen | 28bda51 | 2016-08-31 16:32:55 -0700 | [diff] [blame] | 543 | library.baseInstaller = NewTestInstaller() | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 544 | test := &testLibrary{ | 
| Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 545 | testDecorator: &testDecorator{ | 
|  | 546 | linker:    library.baseLinker, | 
|  | 547 | installer: library.baseInstaller, | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 548 | }, | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 549 | libraryDecorator: library, | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 550 | } | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 551 | module.linker = test | 
| Trevor Radcliffe | f389cb4 | 2022-03-24 21:06:14 +0000 | [diff] [blame] | 552 | module.installer = test | 
| Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 553 | module.bazelable = true | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 554 | return module | 
|  | 555 | } | 
|  | 556 |  | 
| Colin Cross | e28f4e2 | 2017-04-24 18:10:29 -0700 | [diff] [blame] | 557 | type BenchmarkProperties struct { | 
| Anders Lewis | b97e818 | 2017-07-14 15:20:13 -0700 | [diff] [blame] | 558 | // list of files or filegroup modules that provide data that should be installed alongside | 
|  | 559 | // the test | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 560 | Data []string `android:"path"` | 
| Anders Lewis | b97e818 | 2017-07-14 15:20:13 -0700 | [diff] [blame] | 561 |  | 
| Colin Cross | e28f4e2 | 2017-04-24 18:10:29 -0700 | [diff] [blame] | 562 | // list of compatibility suites (for example "cts", "vts") that the module should be | 
|  | 563 | // installed into. | 
| Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 564 | Test_suites []string `android:"arch_variant"` | 
|  | 565 |  | 
|  | 566 | // the name of the test configuration (for example "AndroidTest.xml") that should be | 
|  | 567 | // installed with the module. | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 568 | Test_config *string `android:"path,arch_variant"` | 
| Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 569 |  | 
|  | 570 | // the name of the test configuration template (for example "AndroidTestTemplate.xml") that | 
|  | 571 | // should be installed with the module. | 
| Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 572 | Test_config_template *string `android:"path,arch_variant"` | 
| Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 573 |  | 
|  | 574 | // Add RootTargetPreparer to auto generated test config. This guarantees the test to run | 
|  | 575 | // with root permission. | 
|  | 576 | Require_root *bool | 
| Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 577 |  | 
|  | 578 | // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml | 
|  | 579 | // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true | 
|  | 580 | // explicitly. | 
|  | 581 | Auto_gen_config *bool | 
| Colin Cross | e28f4e2 | 2017-04-24 18:10:29 -0700 | [diff] [blame] | 582 | } | 
|  | 583 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 584 | type benchmarkDecorator struct { | 
|  | 585 | *binaryDecorator | 
| Colin Cross | e28f4e2 | 2017-04-24 18:10:29 -0700 | [diff] [blame] | 586 | Properties BenchmarkProperties | 
| Anders Lewis | b97e818 | 2017-07-14 15:20:13 -0700 | [diff] [blame] | 587 | data       android.Paths | 
| Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 588 | testConfig android.Path | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 589 | } | 
|  | 590 |  | 
| Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 591 | func (benchmark *benchmarkDecorator) benchmarkBinary() bool { | 
|  | 592 | return true | 
|  | 593 | } | 
|  | 594 |  | 
| Colin Cross | e28f4e2 | 2017-04-24 18:10:29 -0700 | [diff] [blame] | 595 | func (benchmark *benchmarkDecorator) linkerProps() []interface{} { | 
|  | 596 | props := benchmark.binaryDecorator.linkerProps() | 
|  | 597 | props = append(props, &benchmark.Properties) | 
|  | 598 | return props | 
|  | 599 | } | 
|  | 600 |  | 
| Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 601 | func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps { | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 602 | deps = benchmark.binaryDecorator.linkerDeps(ctx, deps) | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 603 | deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark") | 
|  | 604 | return deps | 
|  | 605 | } | 
|  | 606 |  | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 607 | func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) { | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 608 | benchmark.data = android.PathsForModuleSrc(ctx, benchmark.Properties.Data) | 
| Chris Parsons | 79d66a5 | 2020-06-05 17:26:16 -0400 | [diff] [blame] | 609 |  | 
| Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 610 | var configs []tradefed.Config | 
|  | 611 | if Bool(benchmark.Properties.Require_root) { | 
| nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 612 | 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] | 613 | } | 
| Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 614 | benchmark.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{ | 
|  | 615 | TestConfigProp:         benchmark.Properties.Test_config, | 
|  | 616 | TestConfigTemplateProp: benchmark.Properties.Test_config_template, | 
|  | 617 | TestSuites:             benchmark.Properties.Test_suites, | 
|  | 618 | Config:                 configs, | 
|  | 619 | AutoGenConfig:          benchmark.Properties.Auto_gen_config, | 
|  | 620 | DeviceTemplate:         "${NativeBenchmarkTestConfigTemplate}", | 
|  | 621 | HostTemplate:           "${NativeBenchmarkTestConfigTemplate}", | 
|  | 622 | }) | 
| Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 623 |  | 
| Colin Cross | 28690e9 | 2017-09-08 16:20:30 -0700 | [diff] [blame] | 624 | benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName()) | 
|  | 625 | benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName()) | 
| Dan Willemsen | 1d577e2 | 2016-08-29 15:53:15 -0700 | [diff] [blame] | 626 | benchmark.binaryDecorator.baseInstaller.install(ctx, file) | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 627 | } | 
|  | 628 |  | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 629 | func NewBenchmark(hod android.HostOrDeviceSupported) *Module { | 
| Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 630 | module, binary := newBinary(hod, false) | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 631 | module.multilib = android.MultilibBoth | 
| Colin Cross | 28690e9 | 2017-09-08 16:20:30 -0700 | [diff] [blame] | 632 | binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData) | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 633 |  | 
|  | 634 | benchmark := &benchmarkDecorator{ | 
|  | 635 | binaryDecorator: binary, | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 636 | } | 
| Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 637 | module.linker = benchmark | 
|  | 638 | module.installer = benchmark | 
| Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 639 | return module | 
|  | 640 | } | 
| Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 641 |  | 
| Jingwen Chen | 3952a90 | 2022-12-12 12:20:58 +0000 | [diff] [blame] | 642 | type ccTestBazelHandler struct { | 
|  | 643 | module *Module | 
|  | 644 | } | 
|  | 645 |  | 
|  | 646 | var _ BazelHandler = (*ccTestBazelHandler)(nil) | 
|  | 647 |  | 
| Jingwen Chen | 6ee23ad | 2023-07-24 14:56:28 +0000 | [diff] [blame] | 648 | // The top level target named $label is a test_suite target, | 
|  | 649 | // not the internal cc_test executable target. | 
|  | 650 | // | 
|  | 651 | // This is to ensure `b test //$label` runs the test_suite target directly, | 
|  | 652 | // which depends on tradefed_test targets, instead of the internal cc_test | 
|  | 653 | // target, which doesn't have tradefed integrations. | 
|  | 654 | // | 
|  | 655 | // However, for cquery, we want the internal cc_test executable target, which | 
|  | 656 | // has the suffix "__tf_internal". | 
|  | 657 | func mixedBuildsTestLabel(label string) string { | 
|  | 658 | return label + "__tf_internal" | 
|  | 659 | } | 
|  | 660 |  | 
| Jingwen Chen | 3952a90 | 2022-12-12 12:20:58 +0000 | [diff] [blame] | 661 | func (handler *ccTestBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) { | 
|  | 662 | bazelCtx := ctx.Config().BazelContext | 
| Jingwen Chen | 6ee23ad | 2023-07-24 14:56:28 +0000 | [diff] [blame] | 663 | bazelCtx.QueueBazelRequest(mixedBuildsTestLabel(label), cquery.GetCcUnstrippedInfo, android.GetConfigKey(ctx)) | 
| Jingwen Chen | 3952a90 | 2022-12-12 12:20:58 +0000 | [diff] [blame] | 664 | } | 
|  | 665 |  | 
|  | 666 | func (handler *ccTestBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) { | 
|  | 667 | bazelCtx := ctx.Config().BazelContext | 
| Jingwen Chen | 6ee23ad | 2023-07-24 14:56:28 +0000 | [diff] [blame] | 668 | info, err := bazelCtx.GetCcUnstrippedInfo(mixedBuildsTestLabel(label), android.GetConfigKey(ctx)) | 
| Jingwen Chen | 3952a90 | 2022-12-12 12:20:58 +0000 | [diff] [blame] | 669 | if err != nil { | 
|  | 670 | ctx.ModuleErrorf(err.Error()) | 
|  | 671 | return | 
|  | 672 | } | 
|  | 673 |  | 
| Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame] | 674 | var outputFilePath android.Path = android.PathForBazelOut(ctx, info.OutputFile) | 
|  | 675 | if len(info.TidyFiles) > 0 { | 
|  | 676 | handler.module.tidyFiles = android.PathsForBazelOut(ctx, info.TidyFiles) | 
|  | 677 | outputFilePath = android.AttachValidationActions(ctx, outputFilePath, handler.module.tidyFiles) | 
|  | 678 | } | 
| Jingwen Chen | 3952a90 | 2022-12-12 12:20:58 +0000 | [diff] [blame] | 679 | handler.module.outputFile = android.OptionalPathForPath(outputFilePath) | 
|  | 680 | handler.module.linker.(*testBinary).unstrippedOutputFile = android.PathForBazelOut(ctx, info.UnstrippedOutput) | 
| Sam Delmerico | 5fb794a | 2023-01-27 16:01:37 -0500 | [diff] [blame] | 681 |  | 
|  | 682 | handler.module.setAndroidMkVariablesFromCquery(info.CcAndroidMkInfo) | 
| Jingwen Chen | 3952a90 | 2022-12-12 12:20:58 +0000 | [diff] [blame] | 683 | } | 
|  | 684 |  | 
| Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 685 | // binaryAttributes contains Bazel attributes corresponding to a cc test | 
|  | 686 | type testBinaryAttributes struct { | 
|  | 687 | binaryAttributes | 
|  | 688 |  | 
| Spandan Das | e50fd11 | 2023-07-26 17:35:20 +0000 | [diff] [blame] | 689 | Gtest *bool | 
| Sam Delmerico | fb3bb32 | 2022-10-21 10:42:24 -0400 | [diff] [blame] | 690 |  | 
|  | 691 | tidyAttributes | 
| Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 692 | tradefed.TestConfigAttributes | 
| yike | fdca7fe | 2023-08-17 01:03:37 +0000 | [diff] [blame] | 693 |  | 
|  | 694 | Runs_on bazel.StringListAttribute | 
| Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 695 | } | 
|  | 696 |  | 
|  | 697 | // testBinaryBp2build is the bp2build converter for cc_test modules. A cc_test's | 
|  | 698 | // dependency graph and compilation/linking steps are functionally similar to a | 
|  | 699 | // cc_binary, but has additional dependencies on test deps like gtest, and | 
|  | 700 | // produces additional runfiles like XML plans for Tradefed orchestration | 
|  | 701 | // | 
|  | 702 | // TODO(b/244432609): handle `isolated` property. | 
|  | 703 | // TODO(b/244432134): handle custom runpaths for tests that assume runfile layouts not | 
|  | 704 | // default to bazel. (see linkerInit function) | 
| Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 705 | func testBinaryBp2build(ctx android.Bp2buildMutatorContext, m *Module) { | 
| Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 706 | var testBinaryAttrs testBinaryAttributes | 
|  | 707 | testBinaryAttrs.binaryAttributes = binaryBp2buildAttrs(ctx, m) | 
|  | 708 |  | 
| Jingwen Chen | ebdc20f | 2022-09-01 18:54:50 +0000 | [diff] [blame] | 709 | var data bazel.LabelListAttribute | 
| Jingwen Chen | fbff97a | 2022-09-16 02:32:03 +0000 | [diff] [blame] | 710 | var tags bazel.StringListAttribute | 
| Jingwen Chen | ebdc20f | 2022-09-01 18:54:50 +0000 | [diff] [blame] | 711 |  | 
| Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 712 | testBinaryProps := m.GetArchVariantProperties(ctx, &TestBinaryProperties{}) | 
|  | 713 | for axis, configToProps := range testBinaryProps { | 
|  | 714 | for config, props := range configToProps { | 
|  | 715 | if p, ok := props.(*TestBinaryProperties); ok { | 
|  | 716 | // Combine data, data_bins and data_libs into a single 'data' attribute. | 
|  | 717 | var combinedData bazel.LabelList | 
|  | 718 | combinedData.Append(android.BazelLabelForModuleSrc(ctx, p.Data)) | 
|  | 719 | combinedData.Append(android.BazelLabelForModuleDeps(ctx, p.Data_bins)) | 
|  | 720 | combinedData.Append(android.BazelLabelForModuleDeps(ctx, p.Data_libs)) | 
| Jingwen Chen | ebdc20f | 2022-09-01 18:54:50 +0000 | [diff] [blame] | 721 | data.SetSelectValue(axis, config, combinedData) | 
| Jingwen Chen | fbff97a | 2022-09-16 02:32:03 +0000 | [diff] [blame] | 722 | tags.SetSelectValue(axis, config, p.Test_options.Tags) | 
| Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 723 | } | 
|  | 724 | } | 
|  | 725 | } | 
|  | 726 |  | 
| Yu Liu | 7ece4aa | 2023-08-25 16:56:33 -0700 | [diff] [blame] | 727 | // The logic comes from https://cs.android.com/android/platform/superproject/main/+/0df8153267f96da877febc5332240fa06ceb8533:build/soong/cc/sanitize.go;l=488 | 
|  | 728 | var features bazel.StringListAttribute | 
|  | 729 | curFeatures := testBinaryAttrs.binaryAttributes.Features.SelectValue(bazel.OsArchConfigurationAxis, bazel.OsArchAndroidArm64) | 
|  | 730 | var newFeatures []string | 
|  | 731 | if !android.InList("memtag_heap", curFeatures) && !android.InList("-memtag_heap", curFeatures) { | 
|  | 732 | newFeatures = append(newFeatures, "memtag_heap") | 
|  | 733 | if !android.InList("diag_memtag_heap", curFeatures) && !android.InList("-diag_memtag_heap", curFeatures) { | 
|  | 734 | newFeatures = append(newFeatures, "diag_memtag_heap") | 
|  | 735 | } | 
|  | 736 | } | 
|  | 737 |  | 
|  | 738 | features.SetSelectValue(bazel.OsArchConfigurationAxis, bazel.OsArchAndroidArm64, newFeatures) | 
|  | 739 | testBinaryAttrs.binaryAttributes.Features.Append(features) | 
|  | 740 | testBinaryAttrs.binaryAttributes.Features.DeduplicateAxesFromBase() | 
|  | 741 |  | 
| Sam Delmerico | c9b8fbd | 2022-10-25 15:47:17 -0400 | [diff] [blame] | 742 | m.convertTidyAttributes(ctx, &testBinaryAttrs.tidyAttributes) | 
| Sam Delmerico | fb3bb32 | 2022-10-21 10:42:24 -0400 | [diff] [blame] | 743 |  | 
| Spandan Das | e50fd11 | 2023-07-26 17:35:20 +0000 | [diff] [blame] | 744 | testBinary := m.linker.(*testBinary) | 
|  | 745 | gtest := testBinary.gtest() | 
|  | 746 | gtestIsolated := testBinary.isolated(ctx) | 
|  | 747 | // Use the underling bool pointer for Gtest in attrs | 
|  | 748 | // This ensures that if this property is not set in Android.bp file, it will not be set in BUILD file either | 
|  | 749 | // cc_test macro will default gtest to True | 
|  | 750 | testBinaryAttrs.Gtest = testBinary.LinkerProperties.Gtest | 
| Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 751 |  | 
| Spandan Das | e50fd11 | 2023-07-26 17:35:20 +0000 | [diff] [blame] | 752 | addImplicitGtestDeps(ctx, &testBinaryAttrs, gtest, gtestIsolated) | 
| Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 753 |  | 
| yike | fdca7fe | 2023-08-17 01:03:37 +0000 | [diff] [blame] | 754 | var unitTest *bool | 
|  | 755 |  | 
| Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 756 | for _, testProps := range m.GetProperties() { | 
|  | 757 | if p, ok := testProps.(*TestBinaryProperties); ok { | 
|  | 758 | useVendor := false // TODO Bug: 262914724 | 
|  | 759 | testInstallBase := getTestInstallBase(useVendor) | 
|  | 760 | testConfigAttributes := tradefed.GetTestConfigAttributes( | 
|  | 761 | ctx, | 
|  | 762 | p.Test_config, | 
|  | 763 | p.Test_options.Extra_test_configs, | 
|  | 764 | p.Auto_gen_config, | 
|  | 765 | p.Test_options.Test_suite_tag, | 
|  | 766 | p.Test_config_template, | 
| Dan Shi | ad04250 | 2023-08-02 13:53:00 -0700 | [diff] [blame] | 767 | getTradefedConfigOptions(ctx, p, gtestIsolated, true), | 
| Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 768 | &testInstallBase, | 
|  | 769 | ) | 
|  | 770 | testBinaryAttrs.TestConfigAttributes = testConfigAttributes | 
| yike | fdca7fe | 2023-08-17 01:03:37 +0000 | [diff] [blame] | 771 | unitTest = p.Test_options.Unit_test | 
| Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 772 | } | 
|  | 773 | } | 
|  | 774 |  | 
| yike | fdca7fe | 2023-08-17 01:03:37 +0000 | [diff] [blame] | 775 | testBinaryAttrs.Runs_on = bazel.MakeStringListAttribute(android.RunsOn( | 
|  | 776 | m.ModuleBase.HostSupported(), | 
|  | 777 | m.ModuleBase.DeviceSupported(), | 
|  | 778 | gtest || (unitTest != nil && *unitTest))) | 
|  | 779 |  | 
| Kevin Dagostino | 32edd1a | 2022-12-04 11:16:42 +0000 | [diff] [blame] | 780 | // TODO (b/262914724): convert to tradefed_cc_test and tradefed_cc_test_host | 
| Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 781 | ctx.CreateBazelTargetModule( | 
|  | 782 | bazel.BazelTargetModuleProperties{ | 
|  | 783 | Rule_class:        "cc_test", | 
|  | 784 | Bzl_load_location: "//build/bazel/rules/cc:cc_test.bzl", | 
|  | 785 | }, | 
| Jingwen Chen | ebdc20f | 2022-09-01 18:54:50 +0000 | [diff] [blame] | 786 | android.CommonAttributes{ | 
|  | 787 | Name: m.Name(), | 
|  | 788 | Data: data, | 
| Jingwen Chen | fbff97a | 2022-09-16 02:32:03 +0000 | [diff] [blame] | 789 | Tags: tags, | 
| Jingwen Chen | ebdc20f | 2022-09-01 18:54:50 +0000 | [diff] [blame] | 790 | }, | 
| Jingwen Chen | 537242c | 2022-08-24 11:53:27 +0000 | [diff] [blame] | 791 | &testBinaryAttrs) | 
|  | 792 | } | 
| Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 793 |  | 
|  | 794 | // cc_test that builds using gtest needs some additional deps | 
|  | 795 | // addImplicitGtestDeps makes these deps explicit in the generated BUILD files | 
| Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 796 | func addImplicitGtestDeps(ctx android.Bp2buildMutatorContext, attrs *testBinaryAttributes, gtest, gtestIsolated bool) { | 
| Spandan Das | d1cd351 | 2023-07-22 02:19:42 +0000 | [diff] [blame] | 797 | addDepsAndDedupe := func(lla *bazel.LabelListAttribute, modules []string) { | 
|  | 798 | moduleLabels := android.BazelLabelForModuleDeps(ctx, modules) | 
|  | 799 | lla.Value.Append(moduleLabels) | 
|  | 800 | // Dedupe | 
|  | 801 | lla.Value = bazel.FirstUniqueBazelLabelList(lla.Value) | 
|  | 802 | } | 
|  | 803 | // this must be kept in sync with Soong's implementation in: | 
|  | 804 | // https://cs.android.com/android/_/android/platform/build/soong/+/460fb2d6d546b5ab493a7e5479998c4933a80f73:cc/test.go;l=300-313;drc=ec7314336a2b35ea30ce5438b83949c28e3ac429;bpv=1;bpt=0 | 
| Spandan Das | e50fd11 | 2023-07-26 17:35:20 +0000 | [diff] [blame] | 805 | if gtest { | 
| Spandan Das | d1cd351 | 2023-07-22 02:19:42 +0000 | [diff] [blame] | 806 | // TODO - b/244433197: Handle canUseSdk | 
|  | 807 | if gtestIsolated { | 
|  | 808 | addDepsAndDedupe(&attrs.Deps, []string{"libgtest_isolated_main"}) | 
|  | 809 | addDepsAndDedupe(&attrs.Dynamic_deps, []string{"liblog"}) | 
|  | 810 | } else { | 
|  | 811 | addDepsAndDedupe(&attrs.Deps, []string{ | 
| Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 812 | "libgtest_main", | 
|  | 813 | "libgtest", | 
| Spandan Das | d1cd351 | 2023-07-22 02:19:42 +0000 | [diff] [blame] | 814 | }) | 
|  | 815 | } | 
| Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 816 | } | 
| Spandan Das | 651203d | 2023-07-21 22:55:32 +0000 | [diff] [blame] | 817 | } |