blob: f128187d2b249b9b47dd7f3f7b7dc41b82f6973a [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// 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
15package cc
16
17import (
18 "path/filepath"
nelsonli0d7111e2019-09-17 16:35:23 +080019 "strconv"
Colin Cross4d9c2d12016-07-29 12:48:20 -070020 "strings"
21
Julien Desprez3b933d32021-01-14 11:49:15 -080022 "github.com/google/blueprint/proptools"
23
Colin Cross4d9c2d12016-07-29 12:48:20 -070024 "android/soong/android"
Jingwen Chen537242c2022-08-24 11:53:27 +000025 "android/soong/bazel"
Jingwen Chen3952a902022-12-12 12:20:58 +000026 "android/soong/bazel/cquery"
Colin Cross303e21f2018-08-07 16:49:25 -070027 "android/soong/tradefed"
Liz Kammer80b54d22023-09-12 14:27:17 -040028 "android/soong/ui/metrics/bp2build_metrics_proto"
Colin Cross4d9c2d12016-07-29 12:48:20 -070029)
30
Trevor Radcliffef389cb42022-03-24 21:06:14 +000031// TestLinkerProperties properties to be registered via the linker
32type TestLinkerProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070033 // if set, build against the gtest library. Defaults to true.
Colin Cross600c9df2016-09-13 12:26:16 -070034 Gtest *bool
Christopher Ferris9df92d62018-08-21 12:40:08 -070035
Trevor Radcliffecd8fd672022-05-03 14:51:16 +000036 // if set, use the isolated gtest runner. Defaults to true if gtest is also true and the arch is Windows, false
37 // otherwise.
Christopher Ferris9df92d62018-08-21 12:40:08 -070038 Isolated *bool
Colin Crossb916a382016-07-29 17:28:03 -070039}
Colin Cross4d9c2d12016-07-29 12:48:20 -070040
Trevor Radcliffef389cb42022-03-24 21:06:14 +000041// TestInstallerProperties properties to be registered via the installer
42type TestInstallerProperties struct {
43 // list of compatibility suites (for example "cts", "vts") that the module should be installed into.
44 Test_suites []string `android:"arch_variant"`
45}
46
yelinhsieh9fc60402018-10-01 19:23:14 +080047// Test option struct.
48type TestOptions struct {
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +080049 android.CommonTestOptions
50
yelinhsieh9fc60402018-10-01 19:23:14 +080051 // The UID that you want to run the test as on a device.
52 Run_test_as *string
Dan Shi95d19422020-08-15 12:24:26 -070053
David Srbecky519db272020-06-18 18:07:00 +010054 // A list of free-formed strings without spaces that categorize the test.
55 Test_suite_tag []string
Dan Shi95d19422020-08-15 12:24:26 -070056
57 // a list of extra test configuration files that should be installed with the module.
58 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -080059
Justin Yun46f66052021-05-04 18:42:24 +090060 // Add ShippingApiLevelModuleController to auto generated test config. If the device properties
Justin Yuna364cfb2021-05-13 12:39:42 +090061 // for the shipping api level is less than the min_shipping_api_level, skip this module.
62 Min_shipping_api_level *int64
63
64 // Add ShippingApiLevelModuleController to auto generated test config. If any of the device
65 // shipping api level and vendor api level properties are less than the
66 // vsr_min_shipping_api_level, skip this module.
67 // As this includes the shipping api level check, it is not allowed to define
68 // min_shipping_api_level at the same time with this property.
69 Vsr_min_shipping_api_level *int64
Justin Yun46f66052021-05-04 18:42:24 +090070
71 // Add MinApiLevelModuleController with ro.vndk.version property. If ro.vndk.version has an
Justin Yuna364cfb2021-05-13 12:39:42 +090072 // integer value and the value is less than the min_vndk_version, skip this module.
73 Min_vndk_version *int64
Dan Shiec731432023-05-26 04:21:44 +000074
75 // Extra <option> tags to add to the auto generated test xml file under the test runner, e.g., GTest.
76 // The "key" is optional in each of these.
77 Test_runner_options []tradefed.Option
yelinhsieh9fc60402018-10-01 19:23:14 +080078}
79
Colin Crossb916a382016-07-29 17:28:03 -070080type TestBinaryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070081 // Create a separate binary for each source file. Useful when there is
82 // global state that can not be torn down and reset between each test suite.
83 Test_per_src *bool
Dan Willemsen3340d602016-12-27 14:40:40 -080084
85 // Disables the creation of a test-specific directory when used with
86 // relative_install_path. Useful if several tests need to be in the same
87 // directory, but test_per_src doesn't work.
88 No_named_install_directory *bool
Colin Crossfaeb7aa2017-02-01 14:12:44 -080089
90 // list of files or filegroup modules that provide data that should be installed alongside
91 // the test
Dan Shi67a88342020-02-25 16:34:39 -080092 Data []string `android:"path,arch_variant"`
Colin Crossa929db02017-03-27 16:27:50 -070093
Chris Parsons79d66a52020-06-05 17:26:16 -040094 // list of shared library modules that should be installed alongside the test
95 Data_libs []string `android:"arch_variant"`
96
Colin Crossc8caa062021-09-24 16:50:14 -070097 // list of binary modules that should be installed alongside the test
98 Data_bins []string `android:"arch_variant"`
99
Julien Despreze146e392018-08-02 15:00:46 -0700100 // the name of the test configuration (for example "AndroidTest.xml") that should be
101 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800102 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700103
104 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
105 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800106 Test_config_template *string `android:"path,arch_variant"`
yelinhsieh9fc60402018-10-01 19:23:14 +0800107
108 // Test options.
109 Test_options TestOptions
Dan Shi37ee3b82019-06-06 16:23:32 -0700110
111 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
112 // with root permission.
113 Require_root *bool
Dan Shi20ccd212019-08-27 10:37:24 -0700114
115 // Add RunCommandTargetPreparer to stop framework before the test and start it after the test.
116 Disable_framework *bool
nelsonli0d7111e2019-09-17 16:35:23 +0800117
Dan Shi6ffaaa82019-09-26 11:41:36 -0700118 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
119 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
120 // explicitly.
121 Auto_gen_config *bool
easoncylee1e3fdcd2020-04-30 10:08:33 +0800122
123 // Add parameterized mainline modules to auto generated test config. The options will be
124 // handled by TradeFed to download and install the specified modules on the device.
125 Test_mainline_modules []string
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700126
127 // Install the test into a folder named for the module in all test suites.
128 Per_testcase_directory *bool
Colin Cross4d9c2d12016-07-29 12:48:20 -0700129}
130
131func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700132 android.RegisterModuleType("cc_test", TestFactory)
133 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
134 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
135 android.RegisterModuleType("cc_test_host", TestHostFactory)
136 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700137}
138
Patrice Arrudac249c712019-03-19 17:00:29 -0700139// cc_test generates a test config file and an executable binary file to test
140// specific functionality on a device. The executable binary gets an implicit
141// static_libs dependency on libgtests unless the gtest flag is set to false.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700142func TestFactory() android.Module {
Jingwen Chen537242c2022-08-24 11:53:27 +0000143 module := NewTest(android.HostAndDeviceSupported, true)
Jingwen Chen3952a902022-12-12 12:20:58 +0000144 module.bazelHandler = &ccTestBazelHandler{module: module}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700145 return module.Init()
146}
147
Patrice Arrudac249c712019-03-19 17:00:29 -0700148// cc_test_library creates an archive of files (i.e. .o files) which is later
149// referenced by another module (such as cc_test, cc_defaults or cc_test_library)
150// for archiving or linking.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700151func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700152 module := NewTestLibrary(android.HostAndDeviceSupported)
153 return module.Init()
154}
155
Patrice Arrudac249c712019-03-19 17:00:29 -0700156// cc_benchmark compiles an executable binary that performs benchmark testing
157// of a specific component in a device. Additional files such as test suites
158// and test configuration are installed on the side of the compiled executed
159// binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700160func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700161 module := NewBenchmark(android.HostAndDeviceSupported)
162 return module.Init()
163}
164
Patrice Arrudac249c712019-03-19 17:00:29 -0700165// cc_test_host compiles a test host binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700166func TestHostFactory() android.Module {
Jingwen Chen537242c2022-08-24 11:53:27 +0000167 module := NewTest(android.HostSupported, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700168 return module.Init()
169}
170
Patrice Arrudac249c712019-03-19 17:00:29 -0700171// 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 Moreland87c9d7b2017-11-02 21:38:28 -0700175func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700176 module := NewBenchmark(android.HostSupported)
177 return module.Init()
178}
179
Colin Crossb916a382016-07-29 17:28:03 -0700180type testPerSrc interface {
181 testPerSrc() bool
182 srcs() []string
Roland Levillainf2fad972019-06-28 15:41:19 +0100183 isAllTestsVariation() bool
Colin Crossb916a382016-07-29 17:28:03 -0700184 setSrc(string, string)
Roland Levillainf2fad972019-06-28 15:41:19 +0100185 unsetSrc()
Colin Crossb916a382016-07-29 17:28:03 -0700186}
187
188func (test *testBinary) testPerSrc() bool {
189 return Bool(test.Properties.Test_per_src)
190}
191
192func (test *testBinary) srcs() []string {
193 return test.baseCompiler.Properties.Srcs
194}
195
Chris Parsons216e10a2020-07-09 17:12:52 -0400196func (test *testBinary) dataPaths() []android.DataPath {
Liz Kammer1c14a212020-05-12 15:26:55 -0700197 return test.data
198}
199
Roland Levillainf2fad972019-06-28 15:41:19 +0100200func (test *testBinary) isAllTestsVariation() bool {
201 stem := test.binaryDecorator.Properties.Stem
202 return stem != nil && *stem == ""
203}
204
Colin Crossb916a382016-07-29 17:28:03 -0700205func (test *testBinary) setSrc(name, src string) {
206 test.baseCompiler.Properties.Srcs = []string{src}
Nan Zhang0007d812017-11-07 10:57:05 -0800207 test.binaryDecorator.Properties.Stem = StringPtr(name)
Colin Crossb916a382016-07-29 17:28:03 -0700208}
209
Roland Levillainf2fad972019-06-28 15:41:19 +0100210func (test *testBinary) unsetSrc() {
211 test.baseCompiler.Properties.Srcs = nil
212 test.binaryDecorator.Properties.Stem = StringPtr("")
213}
214
Jingwen Chen537242c2022-08-24 11:53:27 +0000215func (test *testBinary) testBinary() bool {
216 return true
217}
218
Colin Crossb916a382016-07-29 17:28:03 -0700219var _ testPerSrc = (*testBinary)(nil)
220
Roland Levillain9b5fde92019-06-28 15:41:19 +0100221func TestPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700223 if test, ok := m.linker.(testPerSrc); ok {
Roland Levillainf2fad972019-06-28 15:41:19 +0100224 numTests := len(test.srcs())
225 if test.testPerSrc() && numTests > 0 {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700226 if duplicate, found := android.CheckDuplicate(test.srcs()); found {
Jooyung Hana61ff2c2019-02-28 18:06:34 +0900227 mctx.PropertyErrorf("srcs", "found a duplicate entry %q", duplicate)
228 return
229 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100230 testNames := make([]string, numTests)
Colin Crossb916a382016-07-29 17:28:03 -0700231 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700232 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
233 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100234 // 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 Cross4d9c2d12016-07-29 12:48:20 -0700243 tests := mctx.CreateLocalVariations(testNames...)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800244 allTests := tests[numTests]
245 allTests.(*Module).linker.(testPerSrc).unsetSrc()
Roland Levillainf2fad972019-06-28 15:41:19 +0100246 // Prevent the "all tests" variation from being installable nor
247 // exporting to Make, as it won't create any output file.
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800248 allTests.(*Module).Properties.PreventInstall = true
249 allTests.(*Module).Properties.HideFromMake = true
Colin Crossb916a382016-07-29 17:28:03 -0700250 for i, src := range test.srcs() {
251 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800252 mctx.AddInterVariantDependency(testPerSrcDepTag, allTests, tests[i])
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253 }
Colin Cross90dab342020-08-21 15:55:50 -0700254 mctx.AliasVariation("")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255 }
256 }
257 }
258}
259
Colin Crossb916a382016-07-29 17:28:03 -0700260type testDecorator struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000261 LinkerProperties TestLinkerProperties
262 InstallerProperties TestInstallerProperties
263 installer *baseInstaller
264 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700265}
266
Colin Cross600c9df2016-09-13 12:26:16 -0700267func (test *testDecorator) gtest() bool {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000268 return BoolDefault(test.LinkerProperties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700269}
270
Spandan Dasf5a86552023-07-22 03:16:53 +0000271func (test *testDecorator) isolated(ctx android.EarlyModuleContext) bool {
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000272 return BoolDefault(test.LinkerProperties.Isolated, false)
273}
274
Jingwen Chen537242c2022-08-24 11:53:27 +0000275// NOTE: Keep this in sync with cc/cc_test.bzl#gtest_copts
Colin Crossb916a382016-07-29 17:28:03 -0700276func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700277 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700278 return flags
279 }
280
Colin Cross4af21ed2019-11-04 09:37:55 -0800281 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700282 if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800283 flags.Local.CFlags = append(flags.Local.CFlags, "-O0", "-g")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700284
285 switch ctx.Os() {
286 case android.Windows:
Colin Cross4af21ed2019-11-04 09:37:55 -0800287 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700288 case android.Linux:
Colin Cross4af21ed2019-11-04 09:37:55 -0800289 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700290 case android.Darwin:
Colin Cross4af21ed2019-11-04 09:37:55 -0800291 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700292 }
293 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800294 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700295 }
296
297 return flags
298}
299
Colin Crossb916a382016-07-29 17:28:03 -0700300func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700301 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700302 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800303 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000304 } else if test.isolated(ctx) {
Christopher Ferris9df92d62018-08-21 12:40:08 -0700305 deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main")
Christopher Ferris34cbba62019-07-17 15:46:29 -0700306 // 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 Cross4d9c2d12016-07-29 12:48:20 -0700311 } else {
312 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
313 }
314 }
Colin Crossb916a382016-07-29 17:28:03 -0700315
Colin Cross4d9c2d12016-07-29 12:48:20 -0700316 return deps
317}
318
Colin Crossb916a382016-07-29 17:28:03 -0700319func (test *testDecorator) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000320 return []interface{}{&test.LinkerProperties}
321}
322
323func (test *testDecorator) installerProps() []interface{} {
324 return []interface{}{&test.InstallerProperties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700325}
326
Colin Crossb916a382016-07-29 17:28:03 -0700327func NewTestInstaller() *baseInstaller {
328 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700329}
330
Colin Crossb916a382016-07-29 17:28:03 -0700331type testBinary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000332 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700333 *binaryDecorator
334 *baseCompiler
Dan Shi95d19422020-08-15 12:24:26 -0700335 Properties TestBinaryProperties
336 data []android.DataPath
337 testConfig android.Path
338 extraTestConfigs android.Paths
Colin Crossb916a382016-07-29 17:28:03 -0700339}
340
341func (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 Cross37047f12016-12-13 17:06:13 -0800347func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700348 deps = test.testDecorator.linkerDeps(ctx, deps)
349 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Chris Parsons79d66a52020-06-05 17:26:16 -0400350 deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...)
Colin Crossc8caa062021-09-24 16:50:14 -0700351 deps.DataBins = append(deps.DataBins, test.Properties.Data_bins...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700352 return deps
353}
354
Colin Crossb916a382016-07-29 17:28:03 -0700355func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
356 flags = test.binaryDecorator.linkerFlags(ctx, flags)
357 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700358 return flags
359}
360
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000361func (test *testBinary) installerProps() []interface{} {
362 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
363}
364
Colin Crossb916a382016-07-29 17:28:03 -0700365func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Chris Parsons216e10a2020-07-09 17:12:52 -0400366 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 Parsons79d66a52020-06-05 17:26:16 -0400371
372 ctx.VisitDirectDepsWithTag(dataLibDepTag, func(dep android.Module) {
373 depName := ctx.OtherModuleName(dep)
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400374 linkableDep, ok := dep.(LinkableInterface)
Chris Parsons79d66a52020-06-05 17:26:16 -0400375 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400376 ctx.ModuleErrorf("data_lib %q is not a LinkableInterface module", depName)
Chris Parsons79d66a52020-06-05 17:26:16 -0400377 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400378 if linkableDep.OutputFile().Valid() {
Chris Parsons216e10a2020-07-09 17:12:52 -0400379 test.data = append(test.data,
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400380 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
381 RelativeInstallPath: linkableDep.RelativeInstallPath()})
Chris Parsons79d66a52020-06-05 17:26:16 -0400382 }
383 })
Colin Crossc8caa062021-09-24 16:50:14 -0700384 ctx.VisitDirectDepsWithTag(dataBinDepTag, func(dep android.Module) {
385 depName := ctx.OtherModuleName(dep)
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400386 linkableDep, ok := dep.(LinkableInterface)
Colin Crossc8caa062021-09-24 16:50:14 -0700387 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400388 ctx.ModuleErrorf("data_bin %q is not a LinkableInterface module", depName)
Colin Crossc8caa062021-09-24 16:50:14 -0700389 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400390 if linkableDep.OutputFile().Valid() {
Colin Crossc8caa062021-09-24 16:50:14 -0700391 test.data = append(test.data,
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400392 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
393 RelativeInstallPath: linkableDep.RelativeInstallPath()})
Colin Crossc8caa062021-09-24 16:50:14 -0700394 }
395 })
Chris Parsons79d66a52020-06-05 17:26:16 -0400396
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000397 useVendor := ctx.inVendor() || ctx.useVndk()
398 testInstallBase := getTestInstallBase(useVendor)
Dan Shiad042502023-08-02 13:53:00 -0700399 configs := getTradefedConfigOptions(ctx, &test.Properties, test.isolated(ctx), ctx.Device())
yelinhsieh9fc60402018-10-01 19:23:14 +0800400
Cole Faust21680542022-12-07 18:18:37 -0800401 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 Shiec731432023-05-26 04:21:44 +0000406 TestRunnerOptions: test.Properties.Test_options.Test_runner_options,
Cole Faust21680542022-12-07 18:18:37 -0800407 AutoGenConfig: test.Properties.Auto_gen_config,
408 TestInstallBase: testInstallBase,
409 DeviceTemplate: "${NativeTestConfigTemplate}",
410 HostTemplate: "${NativeHostTestConfigTemplate}",
411 })
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800412
Dan Shi95d19422020-08-15 12:24:26 -0700413 test.extraTestConfigs = android.PathsForModuleSrc(ctx, test.Properties.Test_options.Extra_test_configs)
414
Colin Cross600c9df2016-09-13 12:26:16 -0700415 test.binaryDecorator.baseInstaller.dir = "nativetest"
416 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800417
418 if !Bool(test.Properties.No_named_install_directory) {
419 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800420 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800421 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
422 }
423
Julien Desprez8908b372021-02-04 10:10:16 -0800424 if ctx.Host() && test.gtest() && test.Properties.Test_options.Unit_test == nil {
Julien Desprez3b933d32021-01-14 11:49:15 -0800425 test.Properties.Test_options.Unit_test = proptools.BoolPtr(true)
426 }
Dan Willemsen1d577e22016-08-29 15:53:15 -0700427 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700428}
429
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000430func 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 Shiad042502023-08-02 13:53:00 -0700439func getTradefedConfigOptions(ctx android.EarlyModuleContext, properties *TestBinaryProperties, isolated bool, device bool) []tradefed.Config {
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000440 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 Shiad042502023-08-02 13:53:00 -0700445 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 Dagostino32edd1a2022-12-04 11:16:42 +0000457 }
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 Chen537242c2022-08-24 11:53:27 +0000489func NewTest(hod android.HostOrDeviceSupported, bazelable bool) *Module {
490 module, binary := newBinary(hod, bazelable)
Jingwen Chen3952a902022-12-12 12:20:58 +0000491 module.bazelable = bazelable
Colin Crossb916a382016-07-29 17:28:03 -0700492 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700493 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700494
495 test := &testBinary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000496 testDecorator: &testDecorator{
497 linker: binary.baseLinker,
498 installer: binary.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700499 },
Colin Crossb916a382016-07-29 17:28:03 -0700500 binaryDecorator: binary,
501 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700502 }
Colin Crossb916a382016-07-29 17:28:03 -0700503 module.compiler = test
504 module.linker = test
505 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700506 return module
507}
508
Colin Crossb916a382016-07-29 17:28:03 -0700509type testLibrary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000510 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700511 *libraryDecorator
512}
513
Jingwen Chen537242c2022-08-24 11:53:27 +0000514func (test *testLibrary) testLibrary() bool {
515 return true
516}
517
Colin Crossb916a382016-07-29 17:28:03 -0700518func (test *testLibrary) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000519 var props []interface{}
520 props = append(props, test.testDecorator.linkerProps()...)
521 return append(props, test.libraryDecorator.linkerProps()...)
Colin Crossb916a382016-07-29 17:28:03 -0700522}
523
Colin Cross37047f12016-12-13 17:06:13 -0800524func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700525 deps = test.testDecorator.linkerDeps(ctx, deps)
526 deps = test.libraryDecorator.linkerDeps(ctx, deps)
527 return deps
528}
529
530func (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 Radcliffef389cb42022-03-24 21:06:14 +0000536func (test *testLibrary) installerProps() []interface{} {
537 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
538}
539
Colin Cross4d9c2d12016-07-29 12:48:20 -0700540func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800541 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700542 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700543 test := &testLibrary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000544 testDecorator: &testDecorator{
545 linker: library.baseLinker,
546 installer: library.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700547 },
Colin Crossb916a382016-07-29 17:28:03 -0700548 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700549 }
Colin Crossb916a382016-07-29 17:28:03 -0700550 module.linker = test
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000551 module.installer = test
Jingwen Chen537242c2022-08-24 11:53:27 +0000552 module.bazelable = true
Colin Cross4d9c2d12016-07-29 12:48:20 -0700553 return module
554}
555
Colin Crosse28f4e22017-04-24 18:10:29 -0700556type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700557 // list of files or filegroup modules that provide data that should be installed alongside
558 // the test
Colin Cross27b922f2019-03-04 22:35:41 -0800559 Data []string `android:"path"`
Anders Lewisb97e8182017-07-14 15:20:13 -0700560
Colin Crosse28f4e22017-04-24 18:10:29 -0700561 // list of compatibility suites (for example "cts", "vts") that the module should be
562 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700563 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 Cross27b922f2019-03-04 22:35:41 -0800567 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700568
569 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
570 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800571 Test_config_template *string `android:"path,arch_variant"`
Dan Shi37ee3b82019-06-06 16:23:32 -0700572
573 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
574 // with root permission.
575 Require_root *bool
Dan Shi6ffaaa82019-09-26 11:41:36 -0700576
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 Crosse28f4e22017-04-24 18:10:29 -0700581}
582
Colin Crossb916a382016-07-29 17:28:03 -0700583type benchmarkDecorator struct {
584 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700585 Properties BenchmarkProperties
Anders Lewisb97e8182017-07-14 15:20:13 -0700586 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700587 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700588}
589
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400590func (benchmark *benchmarkDecorator) benchmarkBinary() bool {
591 return true
592}
593
Colin Crosse28f4e22017-04-24 18:10:29 -0700594func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
595 props := benchmark.binaryDecorator.linkerProps()
596 props = append(props, &benchmark.Properties)
597 return props
598}
599
Colin Cross37047f12016-12-13 17:06:13 -0800600func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700601 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700602 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
603 return deps
604}
605
Colin Crossb916a382016-07-29 17:28:03 -0700606func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Colin Cross8a497952019-03-05 22:25:09 -0800607 benchmark.data = android.PathsForModuleSrc(ctx, benchmark.Properties.Data)
Chris Parsons79d66a52020-06-05 17:26:16 -0400608
Dan Shi37ee3b82019-06-06 16:23:32 -0700609 var configs []tradefed.Config
610 if Bool(benchmark.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800611 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shi37ee3b82019-06-06 16:23:32 -0700612 }
Cole Faust21680542022-12-07 18:18:37 -0800613 benchmark.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
614 TestConfigProp: benchmark.Properties.Test_config,
615 TestConfigTemplateProp: benchmark.Properties.Test_config_template,
616 TestSuites: benchmark.Properties.Test_suites,
617 Config: configs,
618 AutoGenConfig: benchmark.Properties.Auto_gen_config,
619 DeviceTemplate: "${NativeBenchmarkTestConfigTemplate}",
620 HostTemplate: "${NativeBenchmarkTestConfigTemplate}",
621 })
Colin Cross303e21f2018-08-07 16:49:25 -0700622
Colin Cross28690e92017-09-08 16:20:30 -0700623 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
624 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Dan Willemsen1d577e22016-08-29 15:53:15 -0700625 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700626}
627
Colin Cross4d9c2d12016-07-29 12:48:20 -0700628func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400629 module, binary := newBinary(hod, false)
Colin Crossb916a382016-07-29 17:28:03 -0700630 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700631 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700632
633 benchmark := &benchmarkDecorator{
634 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700635 }
Colin Crossb916a382016-07-29 17:28:03 -0700636 module.linker = benchmark
637 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700638 return module
639}
Jingwen Chen537242c2022-08-24 11:53:27 +0000640
Jingwen Chen3952a902022-12-12 12:20:58 +0000641type ccTestBazelHandler struct {
642 module *Module
643}
644
645var _ BazelHandler = (*ccTestBazelHandler)(nil)
646
Jingwen Chen6ee23ad2023-07-24 14:56:28 +0000647// The top level target named $label is a test_suite target,
648// not the internal cc_test executable target.
649//
650// This is to ensure `b test //$label` runs the test_suite target directly,
651// which depends on tradefed_test targets, instead of the internal cc_test
652// target, which doesn't have tradefed integrations.
653//
654// However, for cquery, we want the internal cc_test executable target, which
655// has the suffix "__tf_internal".
656func mixedBuildsTestLabel(label string) string {
657 return label + "__tf_internal"
658}
659
Jingwen Chen3952a902022-12-12 12:20:58 +0000660func (handler *ccTestBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
661 bazelCtx := ctx.Config().BazelContext
Jingwen Chen6ee23ad2023-07-24 14:56:28 +0000662 bazelCtx.QueueBazelRequest(mixedBuildsTestLabel(label), cquery.GetCcUnstrippedInfo, android.GetConfigKey(ctx))
Jingwen Chen3952a902022-12-12 12:20:58 +0000663}
664
665func (handler *ccTestBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
666 bazelCtx := ctx.Config().BazelContext
Jingwen Chen6ee23ad2023-07-24 14:56:28 +0000667 info, err := bazelCtx.GetCcUnstrippedInfo(mixedBuildsTestLabel(label), android.GetConfigKey(ctx))
Jingwen Chen3952a902022-12-12 12:20:58 +0000668 if err != nil {
669 ctx.ModuleErrorf(err.Error())
670 return
671 }
672
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500673 var outputFilePath android.Path = android.PathForBazelOut(ctx, info.OutputFile)
674 if len(info.TidyFiles) > 0 {
675 handler.module.tidyFiles = android.PathsForBazelOut(ctx, info.TidyFiles)
676 outputFilePath = android.AttachValidationActions(ctx, outputFilePath, handler.module.tidyFiles)
677 }
Jingwen Chen3952a902022-12-12 12:20:58 +0000678 handler.module.outputFile = android.OptionalPathForPath(outputFilePath)
679 handler.module.linker.(*testBinary).unstrippedOutputFile = android.PathForBazelOut(ctx, info.UnstrippedOutput)
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500680
681 handler.module.setAndroidMkVariablesFromCquery(info.CcAndroidMkInfo)
Jingwen Chen3952a902022-12-12 12:20:58 +0000682}
683
Jingwen Chen537242c2022-08-24 11:53:27 +0000684// binaryAttributes contains Bazel attributes corresponding to a cc test
685type testBinaryAttributes struct {
686 binaryAttributes
687
Spandan Dase50fd112023-07-26 17:35:20 +0000688 Gtest *bool
Sam Delmericofb3bb322022-10-21 10:42:24 -0400689
690 tidyAttributes
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000691 tradefed.TestConfigAttributes
yikefdca7fe2023-08-17 01:03:37 +0000692
693 Runs_on bazel.StringListAttribute
Jingwen Chen537242c2022-08-24 11:53:27 +0000694}
695
696// testBinaryBp2build is the bp2build converter for cc_test modules. A cc_test's
697// dependency graph and compilation/linking steps are functionally similar to a
698// cc_binary, but has additional dependencies on test deps like gtest, and
699// produces additional runfiles like XML plans for Tradefed orchestration
700//
701// TODO(b/244432609): handle `isolated` property.
702// TODO(b/244432134): handle custom runpaths for tests that assume runfile layouts not
703// default to bazel. (see linkerInit function)
Chris Parsons637458d2023-09-19 20:09:00 +0000704func testBinaryBp2build(ctx android.Bp2buildMutatorContext, m *Module) {
Jingwen Chen537242c2022-08-24 11:53:27 +0000705 var testBinaryAttrs testBinaryAttributes
706 testBinaryAttrs.binaryAttributes = binaryBp2buildAttrs(ctx, m)
707
Jingwen Chenebdc20f2022-09-01 18:54:50 +0000708 var data bazel.LabelListAttribute
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000709 var tags bazel.StringListAttribute
Jingwen Chenebdc20f2022-09-01 18:54:50 +0000710
Jingwen Chen537242c2022-08-24 11:53:27 +0000711 testBinaryProps := m.GetArchVariantProperties(ctx, &TestBinaryProperties{})
712 for axis, configToProps := range testBinaryProps {
713 for config, props := range configToProps {
714 if p, ok := props.(*TestBinaryProperties); ok {
715 // Combine data, data_bins and data_libs into a single 'data' attribute.
716 var combinedData bazel.LabelList
717 combinedData.Append(android.BazelLabelForModuleSrc(ctx, p.Data))
718 combinedData.Append(android.BazelLabelForModuleDeps(ctx, p.Data_bins))
719 combinedData.Append(android.BazelLabelForModuleDeps(ctx, p.Data_libs))
Jingwen Chenebdc20f2022-09-01 18:54:50 +0000720 data.SetSelectValue(axis, config, combinedData)
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000721 tags.SetSelectValue(axis, config, p.Test_options.Tags)
Liz Kammer80b54d22023-09-12 14:27:17 -0400722
723 // TODO: b/300117121 - handle bp2build conversion of non-unit tests
724 // default to true to only handle non-nil falses
725 if !BoolDefault(p.Test_options.Unit_test, true) {
726 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_PROPERTY_UNSUPPORTED, "Host unit_test = false")
727 return
728 }
Jingwen Chen537242c2022-08-24 11:53:27 +0000729 }
730 }
731 }
732
Yu Liu7ece4aa2023-08-25 16:56:33 -0700733 // The logic comes from https://cs.android.com/android/platform/superproject/main/+/0df8153267f96da877febc5332240fa06ceb8533:build/soong/cc/sanitize.go;l=488
734 var features bazel.StringListAttribute
735 curFeatures := testBinaryAttrs.binaryAttributes.Features.SelectValue(bazel.OsArchConfigurationAxis, bazel.OsArchAndroidArm64)
736 var newFeatures []string
737 if !android.InList("memtag_heap", curFeatures) && !android.InList("-memtag_heap", curFeatures) {
738 newFeatures = append(newFeatures, "memtag_heap")
739 if !android.InList("diag_memtag_heap", curFeatures) && !android.InList("-diag_memtag_heap", curFeatures) {
740 newFeatures = append(newFeatures, "diag_memtag_heap")
741 }
742 }
743
744 features.SetSelectValue(bazel.OsArchConfigurationAxis, bazel.OsArchAndroidArm64, newFeatures)
745 testBinaryAttrs.binaryAttributes.Features.Append(features)
746 testBinaryAttrs.binaryAttributes.Features.DeduplicateAxesFromBase()
747
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -0400748 m.convertTidyAttributes(ctx, &testBinaryAttrs.tidyAttributes)
Sam Delmericofb3bb322022-10-21 10:42:24 -0400749
Spandan Dase50fd112023-07-26 17:35:20 +0000750 testBinary := m.linker.(*testBinary)
751 gtest := testBinary.gtest()
752 gtestIsolated := testBinary.isolated(ctx)
753 // Use the underling bool pointer for Gtest in attrs
754 // This ensures that if this property is not set in Android.bp file, it will not be set in BUILD file either
755 // cc_test macro will default gtest to True
756 testBinaryAttrs.Gtest = testBinary.LinkerProperties.Gtest
Jingwen Chen537242c2022-08-24 11:53:27 +0000757
Spandan Dase50fd112023-07-26 17:35:20 +0000758 addImplicitGtestDeps(ctx, &testBinaryAttrs, gtest, gtestIsolated)
Spandan Das651203d2023-07-21 22:55:32 +0000759
yikefdca7fe2023-08-17 01:03:37 +0000760 var unitTest *bool
761
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000762 for _, testProps := range m.GetProperties() {
763 if p, ok := testProps.(*TestBinaryProperties); ok {
764 useVendor := false // TODO Bug: 262914724
765 testInstallBase := getTestInstallBase(useVendor)
766 testConfigAttributes := tradefed.GetTestConfigAttributes(
767 ctx,
768 p.Test_config,
769 p.Test_options.Extra_test_configs,
770 p.Auto_gen_config,
771 p.Test_options.Test_suite_tag,
772 p.Test_config_template,
Dan Shiad042502023-08-02 13:53:00 -0700773 getTradefedConfigOptions(ctx, p, gtestIsolated, true),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000774 &testInstallBase,
775 )
776 testBinaryAttrs.TestConfigAttributes = testConfigAttributes
yikefdca7fe2023-08-17 01:03:37 +0000777 unitTest = p.Test_options.Unit_test
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000778 }
779 }
780
yikefdca7fe2023-08-17 01:03:37 +0000781 testBinaryAttrs.Runs_on = bazel.MakeStringListAttribute(android.RunsOn(
782 m.ModuleBase.HostSupported(),
783 m.ModuleBase.DeviceSupported(),
784 gtest || (unitTest != nil && *unitTest)))
785
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000786 // TODO (b/262914724): convert to tradefed_cc_test and tradefed_cc_test_host
Jingwen Chen537242c2022-08-24 11:53:27 +0000787 ctx.CreateBazelTargetModule(
788 bazel.BazelTargetModuleProperties{
789 Rule_class: "cc_test",
790 Bzl_load_location: "//build/bazel/rules/cc:cc_test.bzl",
791 },
Jingwen Chenebdc20f2022-09-01 18:54:50 +0000792 android.CommonAttributes{
793 Name: m.Name(),
794 Data: data,
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000795 Tags: tags,
Jingwen Chenebdc20f2022-09-01 18:54:50 +0000796 },
Jingwen Chen537242c2022-08-24 11:53:27 +0000797 &testBinaryAttrs)
798}
Spandan Das651203d2023-07-21 22:55:32 +0000799
800// cc_test that builds using gtest needs some additional deps
801// addImplicitGtestDeps makes these deps explicit in the generated BUILD files
Chris Parsons5f1b3c72023-09-28 20:41:03 +0000802func addImplicitGtestDeps(ctx android.Bp2buildMutatorContext, attrs *testBinaryAttributes, gtest, gtestIsolated bool) {
Spandan Dasd1cd3512023-07-22 02:19:42 +0000803 addDepsAndDedupe := func(lla *bazel.LabelListAttribute, modules []string) {
804 moduleLabels := android.BazelLabelForModuleDeps(ctx, modules)
805 lla.Value.Append(moduleLabels)
806 // Dedupe
807 lla.Value = bazel.FirstUniqueBazelLabelList(lla.Value)
808 }
809 // this must be kept in sync with Soong's implementation in:
810 // https://cs.android.com/android/_/android/platform/build/soong/+/460fb2d6d546b5ab493a7e5479998c4933a80f73:cc/test.go;l=300-313;drc=ec7314336a2b35ea30ce5438b83949c28e3ac429;bpv=1;bpt=0
Spandan Dase50fd112023-07-26 17:35:20 +0000811 if gtest {
Spandan Dasd1cd3512023-07-22 02:19:42 +0000812 // TODO - b/244433197: Handle canUseSdk
813 if gtestIsolated {
814 addDepsAndDedupe(&attrs.Deps, []string{"libgtest_isolated_main"})
815 addDepsAndDedupe(&attrs.Dynamic_deps, []string{"liblog"})
816 } else {
817 addDepsAndDedupe(&attrs.Deps, []string{
Spandan Das651203d2023-07-21 22:55:32 +0000818 "libgtest_main",
819 "libgtest",
Spandan Dasd1cd3512023-07-22 02:19:42 +0000820 })
821 }
Spandan Das651203d2023-07-21 22:55:32 +0000822 }
Spandan Das651203d2023-07-21 22:55:32 +0000823}