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