blob: 27de06b079c0d081f222faa8acee0a9c130b96d6 [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"
Colin Cross4d9c2d12016-07-29 12:48:20 -070028)
29
Trevor Radcliffef389cb42022-03-24 21:06:14 +000030// TestLinkerProperties properties to be registered via the linker
31type TestLinkerProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070032 // if set, build against the gtest library. Defaults to true.
Colin Cross600c9df2016-09-13 12:26:16 -070033 Gtest *bool
Christopher Ferris9df92d62018-08-21 12:40:08 -070034
Trevor Radcliffecd8fd672022-05-03 14:51:16 +000035 // if set, use the isolated gtest runner. Defaults to true if gtest is also true and the arch is Windows, false
36 // otherwise.
Christopher Ferris9df92d62018-08-21 12:40:08 -070037 Isolated *bool
Colin Crossb916a382016-07-29 17:28:03 -070038}
Colin Cross4d9c2d12016-07-29 12:48:20 -070039
Trevor Radcliffef389cb42022-03-24 21:06:14 +000040// TestInstallerProperties properties to be registered via the installer
41type 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
yelinhsieh9fc60402018-10-01 19:23:14 +080046// Test option struct.
47type TestOptions struct {
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +080048 android.CommonTestOptions
49
yelinhsieh9fc60402018-10-01 19:23:14 +080050 // The UID that you want to run the test as on a device.
51 Run_test_as *string
Dan Shi95d19422020-08-15 12:24:26 -070052
David Srbecky519db272020-06-18 18:07:00 +010053 // A list of free-formed strings without spaces that categorize the test.
54 Test_suite_tag []string
Dan Shi95d19422020-08-15 12:24:26 -070055
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 Shid79572f2020-11-13 14:33:46 -080058
Justin Yun46f66052021-05-04 18:42:24 +090059 // Add ShippingApiLevelModuleController to auto generated test config. If the device properties
Justin Yuna364cfb2021-05-13 12:39:42 +090060 // 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 Yun46f66052021-05-04 18:42:24 +090069
70 // Add MinApiLevelModuleController with ro.vndk.version property. If ro.vndk.version has an
Justin Yuna364cfb2021-05-13 12:39:42 +090071 // integer value and the value is less than the min_vndk_version, skip this module.
72 Min_vndk_version *int64
yelinhsieh9fc60402018-10-01 19:23:14 +080073}
74
Colin Crossb916a382016-07-29 17:28:03 -070075type TestBinaryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070076 // 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 Willemsen3340d602016-12-27 14:40:40 -080079
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 Crossfaeb7aa2017-02-01 14:12:44 -080084
85 // list of files or filegroup modules that provide data that should be installed alongside
86 // the test
Dan Shi67a88342020-02-25 16:34:39 -080087 Data []string `android:"path,arch_variant"`
Colin Crossa929db02017-03-27 16:27:50 -070088
Chris Parsons79d66a52020-06-05 17:26:16 -040089 // list of shared library modules that should be installed alongside the test
90 Data_libs []string `android:"arch_variant"`
91
Colin Crossc8caa062021-09-24 16:50:14 -070092 // list of binary modules that should be installed alongside the test
93 Data_bins []string `android:"arch_variant"`
94
Julien Despreze146e392018-08-02 15:00:46 -070095 // the name of the test configuration (for example "AndroidTest.xml") that should be
96 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -080097 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -070098
99 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
100 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800101 Test_config_template *string `android:"path,arch_variant"`
yelinhsieh9fc60402018-10-01 19:23:14 +0800102
103 // Test options.
104 Test_options TestOptions
Dan Shi37ee3b82019-06-06 16:23:32 -0700105
106 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
107 // with root permission.
108 Require_root *bool
Dan Shi20ccd212019-08-27 10:37:24 -0700109
110 // Add RunCommandTargetPreparer to stop framework before the test and start it after the test.
111 Disable_framework *bool
nelsonli0d7111e2019-09-17 16:35:23 +0800112
Dan Shi6ffaaa82019-09-26 11:41:36 -0700113 // 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
easoncylee1e3fdcd2020-04-30 10:08:33 +0800117
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 Crosscfb0f5e2021-09-24 15:47:17 -0700121
122 // Install the test into a folder named for the module in all test suites.
123 Per_testcase_directory *bool
Colin Cross4d9c2d12016-07-29 12:48:20 -0700124}
125
126func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700127 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 Cross4d9c2d12016-07-29 12:48:20 -0700132}
133
Patrice Arrudac249c712019-03-19 17:00:29 -0700134// 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 Moreland87c9d7b2017-11-02 21:38:28 -0700137func TestFactory() android.Module {
Jingwen Chen537242c2022-08-24 11:53:27 +0000138 module := NewTest(android.HostAndDeviceSupported, true)
Jingwen Chen3952a902022-12-12 12:20:58 +0000139 module.bazelHandler = &ccTestBazelHandler{module: module}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700140 return module.Init()
141}
142
Patrice Arrudac249c712019-03-19 17:00:29 -0700143// 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 Moreland87c9d7b2017-11-02 21:38:28 -0700146func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700147 module := NewTestLibrary(android.HostAndDeviceSupported)
148 return module.Init()
149}
150
Patrice Arrudac249c712019-03-19 17:00:29 -0700151// 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 Moreland87c9d7b2017-11-02 21:38:28 -0700155func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700156 module := NewBenchmark(android.HostAndDeviceSupported)
157 return module.Init()
158}
159
Patrice Arrudac249c712019-03-19 17:00:29 -0700160// cc_test_host compiles a test host binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700161func TestHostFactory() android.Module {
Jingwen Chen537242c2022-08-24 11:53:27 +0000162 module := NewTest(android.HostSupported, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700163 return module.Init()
164}
165
Patrice Arrudac249c712019-03-19 17:00:29 -0700166// 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 Moreland87c9d7b2017-11-02 21:38:28 -0700170func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700171 module := NewBenchmark(android.HostSupported)
172 return module.Init()
173}
174
Colin Crossb916a382016-07-29 17:28:03 -0700175type testPerSrc interface {
176 testPerSrc() bool
177 srcs() []string
Roland Levillainf2fad972019-06-28 15:41:19 +0100178 isAllTestsVariation() bool
Colin Crossb916a382016-07-29 17:28:03 -0700179 setSrc(string, string)
Roland Levillainf2fad972019-06-28 15:41:19 +0100180 unsetSrc()
Colin Crossb916a382016-07-29 17:28:03 -0700181}
182
183func (test *testBinary) testPerSrc() bool {
184 return Bool(test.Properties.Test_per_src)
185}
186
187func (test *testBinary) srcs() []string {
188 return test.baseCompiler.Properties.Srcs
189}
190
Chris Parsons216e10a2020-07-09 17:12:52 -0400191func (test *testBinary) dataPaths() []android.DataPath {
Liz Kammer1c14a212020-05-12 15:26:55 -0700192 return test.data
193}
194
Roland Levillainf2fad972019-06-28 15:41:19 +0100195func (test *testBinary) isAllTestsVariation() bool {
196 stem := test.binaryDecorator.Properties.Stem
197 return stem != nil && *stem == ""
198}
199
Colin Crossb916a382016-07-29 17:28:03 -0700200func (test *testBinary) setSrc(name, src string) {
201 test.baseCompiler.Properties.Srcs = []string{src}
Nan Zhang0007d812017-11-07 10:57:05 -0800202 test.binaryDecorator.Properties.Stem = StringPtr(name)
Colin Crossb916a382016-07-29 17:28:03 -0700203}
204
Roland Levillainf2fad972019-06-28 15:41:19 +0100205func (test *testBinary) unsetSrc() {
206 test.baseCompiler.Properties.Srcs = nil
207 test.binaryDecorator.Properties.Stem = StringPtr("")
208}
209
Jingwen Chen537242c2022-08-24 11:53:27 +0000210func (test *testBinary) testBinary() bool {
211 return true
212}
213
Colin Crossb916a382016-07-29 17:28:03 -0700214var _ testPerSrc = (*testBinary)(nil)
215
Roland Levillain9b5fde92019-06-28 15:41:19 +0100216func TestPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700218 if test, ok := m.linker.(testPerSrc); ok {
Roland Levillainf2fad972019-06-28 15:41:19 +0100219 numTests := len(test.srcs())
220 if test.testPerSrc() && numTests > 0 {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700221 if duplicate, found := android.CheckDuplicate(test.srcs()); found {
Jooyung Hana61ff2c2019-02-28 18:06:34 +0900222 mctx.PropertyErrorf("srcs", "found a duplicate entry %q", duplicate)
223 return
224 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100225 testNames := make([]string, numTests)
Colin Crossb916a382016-07-29 17:28:03 -0700226 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700227 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
228 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100229 // 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 Cross4d9c2d12016-07-29 12:48:20 -0700238 tests := mctx.CreateLocalVariations(testNames...)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800239 allTests := tests[numTests]
240 allTests.(*Module).linker.(testPerSrc).unsetSrc()
Roland Levillainf2fad972019-06-28 15:41:19 +0100241 // Prevent the "all tests" variation from being installable nor
242 // exporting to Make, as it won't create any output file.
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800243 allTests.(*Module).Properties.PreventInstall = true
244 allTests.(*Module).Properties.HideFromMake = true
Colin Crossb916a382016-07-29 17:28:03 -0700245 for i, src := range test.srcs() {
246 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800247 mctx.AddInterVariantDependency(testPerSrcDepTag, allTests, tests[i])
Colin Cross4d9c2d12016-07-29 12:48:20 -0700248 }
Colin Cross90dab342020-08-21 15:55:50 -0700249 mctx.AliasVariation("")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700250 }
251 }
252 }
253}
254
Colin Crossb916a382016-07-29 17:28:03 -0700255type testDecorator struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000256 LinkerProperties TestLinkerProperties
257 InstallerProperties TestInstallerProperties
258 installer *baseInstaller
259 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700260}
261
Colin Cross600c9df2016-09-13 12:26:16 -0700262func (test *testDecorator) gtest() bool {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000263 return BoolDefault(test.LinkerProperties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700264}
265
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000266func (test *testDecorator) isolated(ctx BaseModuleContext) bool {
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000267 return BoolDefault(test.LinkerProperties.Isolated, false)
268}
269
Jingwen Chen537242c2022-08-24 11:53:27 +0000270// NOTE: Keep this in sync with cc/cc_test.bzl#gtest_copts
Colin Crossb916a382016-07-29 17:28:03 -0700271func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700272 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700273 return flags
274 }
275
Colin Cross4af21ed2019-11-04 09:37:55 -0800276 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700277 if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800278 flags.Local.CFlags = append(flags.Local.CFlags, "-O0", "-g")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700279
280 switch ctx.Os() {
281 case android.Windows:
Colin Cross4af21ed2019-11-04 09:37:55 -0800282 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700283 case android.Linux:
Colin Cross4af21ed2019-11-04 09:37:55 -0800284 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700285 case android.Darwin:
Colin Cross4af21ed2019-11-04 09:37:55 -0800286 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700287 }
288 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800289 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700290 }
291
292 return flags
293}
294
Colin Crossb916a382016-07-29 17:28:03 -0700295func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700296 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700297 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800298 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000299 } else if test.isolated(ctx) {
Christopher Ferris9df92d62018-08-21 12:40:08 -0700300 deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main")
Christopher Ferris34cbba62019-07-17 15:46:29 -0700301 // 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 Cross4d9c2d12016-07-29 12:48:20 -0700306 } else {
307 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
308 }
309 }
Colin Crossb916a382016-07-29 17:28:03 -0700310
Colin Cross4d9c2d12016-07-29 12:48:20 -0700311 return deps
312}
313
Colin Crossb916a382016-07-29 17:28:03 -0700314func (test *testDecorator) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000315 return []interface{}{&test.LinkerProperties}
316}
317
318func (test *testDecorator) installerProps() []interface{} {
319 return []interface{}{&test.InstallerProperties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700320}
321
Colin Crossb916a382016-07-29 17:28:03 -0700322func NewTestInstaller() *baseInstaller {
323 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700324}
325
Colin Crossb916a382016-07-29 17:28:03 -0700326type testBinary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000327 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700328 *binaryDecorator
329 *baseCompiler
Dan Shi95d19422020-08-15 12:24:26 -0700330 Properties TestBinaryProperties
331 data []android.DataPath
332 testConfig android.Path
333 extraTestConfigs android.Paths
Colin Crossb916a382016-07-29 17:28:03 -0700334}
335
336func (test *testBinary) linkerProps() []interface{} {
337 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
338 props = append(props, &test.Properties)
339 return props
340}
341
Colin Cross37047f12016-12-13 17:06:13 -0800342func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700343 deps = test.testDecorator.linkerDeps(ctx, deps)
344 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Chris Parsons79d66a52020-06-05 17:26:16 -0400345 deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...)
Colin Crossc8caa062021-09-24 16:50:14 -0700346 deps.DataBins = append(deps.DataBins, test.Properties.Data_bins...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700347 return deps
348}
349
Colin Crossb916a382016-07-29 17:28:03 -0700350func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
351 flags = test.binaryDecorator.linkerFlags(ctx, flags)
352 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700353 return flags
354}
355
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000356func (test *testBinary) installerProps() []interface{} {
357 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
358}
359
Colin Crossb916a382016-07-29 17:28:03 -0700360func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Chris Parsons216e10a2020-07-09 17:12:52 -0400361 dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
362
363 for _, dataSrcPath := range dataSrcPaths {
364 test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
365 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400366
367 ctx.VisitDirectDepsWithTag(dataLibDepTag, func(dep android.Module) {
368 depName := ctx.OtherModuleName(dep)
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400369 linkableDep, ok := dep.(LinkableInterface)
Chris Parsons79d66a52020-06-05 17:26:16 -0400370 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400371 ctx.ModuleErrorf("data_lib %q is not a LinkableInterface module", depName)
Chris Parsons79d66a52020-06-05 17:26:16 -0400372 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400373 if linkableDep.OutputFile().Valid() {
Chris Parsons216e10a2020-07-09 17:12:52 -0400374 test.data = append(test.data,
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400375 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
376 RelativeInstallPath: linkableDep.RelativeInstallPath()})
Chris Parsons79d66a52020-06-05 17:26:16 -0400377 }
378 })
Colin Crossc8caa062021-09-24 16:50:14 -0700379 ctx.VisitDirectDepsWithTag(dataBinDepTag, func(dep android.Module) {
380 depName := ctx.OtherModuleName(dep)
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400381 linkableDep, ok := dep.(LinkableInterface)
Colin Crossc8caa062021-09-24 16:50:14 -0700382 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400383 ctx.ModuleErrorf("data_bin %q is not a LinkableInterface module", depName)
Colin Crossc8caa062021-09-24 16:50:14 -0700384 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400385 if linkableDep.OutputFile().Valid() {
Colin Crossc8caa062021-09-24 16:50:14 -0700386 test.data = append(test.data,
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400387 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
388 RelativeInstallPath: linkableDep.RelativeInstallPath()})
Colin Crossc8caa062021-09-24 16:50:14 -0700389 }
390 })
Chris Parsons79d66a52020-06-05 17:26:16 -0400391
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000392 useVendor := ctx.inVendor() || ctx.useVndk()
393 testInstallBase := getTestInstallBase(useVendor)
394 configs := getTradefedConfigOptions(ctx, &test.Properties, test.isolated(ctx))
yelinhsieh9fc60402018-10-01 19:23:14 +0800395
Cole Faust21680542022-12-07 18:18:37 -0800396 test.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
397 TestConfigProp: test.Properties.Test_config,
398 TestConfigTemplateProp: test.Properties.Test_config_template,
399 TestSuites: test.testDecorator.InstallerProperties.Test_suites,
400 Config: configs,
401 AutoGenConfig: test.Properties.Auto_gen_config,
402 TestInstallBase: testInstallBase,
403 DeviceTemplate: "${NativeTestConfigTemplate}",
404 HostTemplate: "${NativeHostTestConfigTemplate}",
405 })
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800406
Dan Shi95d19422020-08-15 12:24:26 -0700407 test.extraTestConfigs = android.PathsForModuleSrc(ctx, test.Properties.Test_options.Extra_test_configs)
408
Colin Cross600c9df2016-09-13 12:26:16 -0700409 test.binaryDecorator.baseInstaller.dir = "nativetest"
410 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800411
412 if !Bool(test.Properties.No_named_install_directory) {
413 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800414 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800415 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
416 }
417
Julien Desprez8908b372021-02-04 10:10:16 -0800418 if ctx.Host() && test.gtest() && test.Properties.Test_options.Unit_test == nil {
Julien Desprez3b933d32021-01-14 11:49:15 -0800419 test.Properties.Test_options.Unit_test = proptools.BoolPtr(true)
420 }
Dan Willemsen1d577e22016-08-29 15:53:15 -0700421 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700422}
423
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000424func getTestInstallBase(useVendor bool) string {
425 // TODO: (b/167308193) Switch to /data/local/tests/unrestricted as the default install base.
426 testInstallBase := "/data/local/tmp"
427 if useVendor {
428 testInstallBase = "/data/local/tests/vendor"
429 }
430 return testInstallBase
431}
432
433func getTradefedConfigOptions(ctx android.EarlyModuleContext, properties *TestBinaryProperties, isolated bool) []tradefed.Config {
434 var configs []tradefed.Config
435
436 for _, module := range properties.Test_mainline_modules {
437 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module})
438 }
439 if Bool(properties.Require_root) {
440 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
441 } else {
442 var options []tradefed.Option
443 options = append(options, tradefed.Option{Name: "force-root", Value: "false"})
444 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
445 }
446 if Bool(properties.Disable_framework) {
447 var options []tradefed.Option
448 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.StopServicesSetup", options})
449 }
450 if isolated {
451 configs = append(configs, tradefed.Option{Name: "not-shardable", Value: "true"})
452 }
453 if properties.Test_options.Run_test_as != nil {
454 configs = append(configs, tradefed.Option{Name: "run-test-as", Value: String(properties.Test_options.Run_test_as)})
455 }
456 for _, tag := range properties.Test_options.Test_suite_tag {
457 configs = append(configs, tradefed.Option{Name: "test-suite-tag", Value: tag})
458 }
459 if properties.Test_options.Min_shipping_api_level != nil {
460 if properties.Test_options.Vsr_min_shipping_api_level != nil {
461 ctx.PropertyErrorf("test_options.min_shipping_api_level", "must not be set at the same time as 'vsr_min_shipping_api_level'.")
462 }
463 var options []tradefed.Option
464 options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_shipping_api_level), 10)})
465 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
466 }
467 if properties.Test_options.Vsr_min_shipping_api_level != nil {
468 var options []tradefed.Option
469 options = append(options, tradefed.Option{Name: "vsr-min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Vsr_min_shipping_api_level), 10)})
470 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
471 }
472 if properties.Test_options.Min_vndk_version != nil {
473 var options []tradefed.Option
474 options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_vndk_version), 10)})
475 options = append(options, tradefed.Option{Name: "api-level-prop", Value: "ro.vndk.version"})
476 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.MinApiLevelModuleController", options})
477 }
478 return configs
479}
480
Jingwen Chen537242c2022-08-24 11:53:27 +0000481func NewTest(hod android.HostOrDeviceSupported, bazelable bool) *Module {
482 module, binary := newBinary(hod, bazelable)
Jingwen Chen3952a902022-12-12 12:20:58 +0000483 module.bazelable = bazelable
Colin Crossb916a382016-07-29 17:28:03 -0700484 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700485 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700486
487 test := &testBinary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000488 testDecorator: &testDecorator{
489 linker: binary.baseLinker,
490 installer: binary.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700491 },
Colin Crossb916a382016-07-29 17:28:03 -0700492 binaryDecorator: binary,
493 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700494 }
Colin Crossb916a382016-07-29 17:28:03 -0700495 module.compiler = test
496 module.linker = test
497 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700498 return module
499}
500
Colin Crossb916a382016-07-29 17:28:03 -0700501type testLibrary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000502 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700503 *libraryDecorator
504}
505
Jingwen Chen537242c2022-08-24 11:53:27 +0000506func (test *testLibrary) testLibrary() bool {
507 return true
508}
509
Colin Crossb916a382016-07-29 17:28:03 -0700510func (test *testLibrary) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000511 var props []interface{}
512 props = append(props, test.testDecorator.linkerProps()...)
513 return append(props, test.libraryDecorator.linkerProps()...)
Colin Crossb916a382016-07-29 17:28:03 -0700514}
515
Colin Cross37047f12016-12-13 17:06:13 -0800516func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700517 deps = test.testDecorator.linkerDeps(ctx, deps)
518 deps = test.libraryDecorator.linkerDeps(ctx, deps)
519 return deps
520}
521
522func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
523 flags = test.libraryDecorator.linkerFlags(ctx, flags)
524 flags = test.testDecorator.linkerFlags(ctx, flags)
525 return flags
526}
527
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000528func (test *testLibrary) installerProps() []interface{} {
529 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
530}
531
Colin Cross4d9c2d12016-07-29 12:48:20 -0700532func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800533 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700534 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700535 test := &testLibrary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000536 testDecorator: &testDecorator{
537 linker: library.baseLinker,
538 installer: library.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700539 },
Colin Crossb916a382016-07-29 17:28:03 -0700540 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700541 }
Colin Crossb916a382016-07-29 17:28:03 -0700542 module.linker = test
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000543 module.installer = test
Jingwen Chen537242c2022-08-24 11:53:27 +0000544 module.bazelable = true
Colin Cross4d9c2d12016-07-29 12:48:20 -0700545 return module
546}
547
Colin Crosse28f4e22017-04-24 18:10:29 -0700548type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700549 // list of files or filegroup modules that provide data that should be installed alongside
550 // the test
Colin Cross27b922f2019-03-04 22:35:41 -0800551 Data []string `android:"path"`
Anders Lewisb97e8182017-07-14 15:20:13 -0700552
Colin Crosse28f4e22017-04-24 18:10:29 -0700553 // list of compatibility suites (for example "cts", "vts") that the module should be
554 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700555 Test_suites []string `android:"arch_variant"`
556
557 // the name of the test configuration (for example "AndroidTest.xml") that should be
558 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800559 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700560
561 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
562 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800563 Test_config_template *string `android:"path,arch_variant"`
Dan Shi37ee3b82019-06-06 16:23:32 -0700564
565 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
566 // with root permission.
567 Require_root *bool
Dan Shi6ffaaa82019-09-26 11:41:36 -0700568
569 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
570 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
571 // explicitly.
572 Auto_gen_config *bool
Colin Crosse28f4e22017-04-24 18:10:29 -0700573}
574
Colin Crossb916a382016-07-29 17:28:03 -0700575type benchmarkDecorator struct {
576 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700577 Properties BenchmarkProperties
Anders Lewisb97e8182017-07-14 15:20:13 -0700578 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700579 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700580}
581
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400582func (benchmark *benchmarkDecorator) benchmarkBinary() bool {
583 return true
584}
585
Colin Crosse28f4e22017-04-24 18:10:29 -0700586func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
587 props := benchmark.binaryDecorator.linkerProps()
588 props = append(props, &benchmark.Properties)
589 return props
590}
591
Colin Cross37047f12016-12-13 17:06:13 -0800592func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700593 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700594 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
595 return deps
596}
597
Colin Crossb916a382016-07-29 17:28:03 -0700598func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Colin Cross8a497952019-03-05 22:25:09 -0800599 benchmark.data = android.PathsForModuleSrc(ctx, benchmark.Properties.Data)
Chris Parsons79d66a52020-06-05 17:26:16 -0400600
Dan Shi37ee3b82019-06-06 16:23:32 -0700601 var configs []tradefed.Config
602 if Bool(benchmark.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800603 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shi37ee3b82019-06-06 16:23:32 -0700604 }
Cole Faust21680542022-12-07 18:18:37 -0800605 benchmark.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
606 TestConfigProp: benchmark.Properties.Test_config,
607 TestConfigTemplateProp: benchmark.Properties.Test_config_template,
608 TestSuites: benchmark.Properties.Test_suites,
609 Config: configs,
610 AutoGenConfig: benchmark.Properties.Auto_gen_config,
611 DeviceTemplate: "${NativeBenchmarkTestConfigTemplate}",
612 HostTemplate: "${NativeBenchmarkTestConfigTemplate}",
613 })
Colin Cross303e21f2018-08-07 16:49:25 -0700614
Colin Cross28690e92017-09-08 16:20:30 -0700615 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
616 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Dan Willemsen1d577e22016-08-29 15:53:15 -0700617 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700618}
619
Colin Cross4d9c2d12016-07-29 12:48:20 -0700620func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400621 module, binary := newBinary(hod, false)
Colin Crossb916a382016-07-29 17:28:03 -0700622 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700623 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700624
625 benchmark := &benchmarkDecorator{
626 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700627 }
Colin Crossb916a382016-07-29 17:28:03 -0700628 module.linker = benchmark
629 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700630 return module
631}
Jingwen Chen537242c2022-08-24 11:53:27 +0000632
Jingwen Chen3952a902022-12-12 12:20:58 +0000633type ccTestBazelHandler struct {
634 module *Module
635}
636
637var _ BazelHandler = (*ccTestBazelHandler)(nil)
638
639func (handler *ccTestBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
640 bazelCtx := ctx.Config().BazelContext
641 bazelCtx.QueueBazelRequest(label, cquery.GetCcUnstrippedInfo, android.GetConfigKey(ctx))
642}
643
644func (handler *ccTestBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
645 bazelCtx := ctx.Config().BazelContext
646 info, err := bazelCtx.GetCcUnstrippedInfo(label, android.GetConfigKey(ctx))
647 if err != nil {
648 ctx.ModuleErrorf(err.Error())
649 return
650 }
651
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500652 var outputFilePath android.Path = android.PathForBazelOut(ctx, info.OutputFile)
653 if len(info.TidyFiles) > 0 {
654 handler.module.tidyFiles = android.PathsForBazelOut(ctx, info.TidyFiles)
655 outputFilePath = android.AttachValidationActions(ctx, outputFilePath, handler.module.tidyFiles)
656 }
Jingwen Chen3952a902022-12-12 12:20:58 +0000657 handler.module.outputFile = android.OptionalPathForPath(outputFilePath)
658 handler.module.linker.(*testBinary).unstrippedOutputFile = android.PathForBazelOut(ctx, info.UnstrippedOutput)
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500659
660 handler.module.setAndroidMkVariablesFromCquery(info.CcAndroidMkInfo)
Jingwen Chen3952a902022-12-12 12:20:58 +0000661}
662
Jingwen Chen537242c2022-08-24 11:53:27 +0000663// binaryAttributes contains Bazel attributes corresponding to a cc test
664type testBinaryAttributes struct {
665 binaryAttributes
666
667 Gtest bool
668 Isolated bool
Sam Delmericofb3bb322022-10-21 10:42:24 -0400669
670 tidyAttributes
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000671 tradefed.TestConfigAttributes
Jingwen Chen537242c2022-08-24 11:53:27 +0000672}
673
674// testBinaryBp2build is the bp2build converter for cc_test modules. A cc_test's
675// dependency graph and compilation/linking steps are functionally similar to a
676// cc_binary, but has additional dependencies on test deps like gtest, and
677// produces additional runfiles like XML plans for Tradefed orchestration
678//
679// TODO(b/244432609): handle `isolated` property.
680// TODO(b/244432134): handle custom runpaths for tests that assume runfile layouts not
681// default to bazel. (see linkerInit function)
Jingwen Chen537242c2022-08-24 11:53:27 +0000682func testBinaryBp2build(ctx android.TopDownMutatorContext, m *Module) {
683 var testBinaryAttrs testBinaryAttributes
684 testBinaryAttrs.binaryAttributes = binaryBp2buildAttrs(ctx, m)
685
Jingwen Chenebdc20f2022-09-01 18:54:50 +0000686 var data bazel.LabelListAttribute
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000687 var tags bazel.StringListAttribute
Jingwen Chenebdc20f2022-09-01 18:54:50 +0000688
Jingwen Chen537242c2022-08-24 11:53:27 +0000689 testBinaryProps := m.GetArchVariantProperties(ctx, &TestBinaryProperties{})
690 for axis, configToProps := range testBinaryProps {
691 for config, props := range configToProps {
692 if p, ok := props.(*TestBinaryProperties); ok {
693 // Combine data, data_bins and data_libs into a single 'data' attribute.
694 var combinedData bazel.LabelList
695 combinedData.Append(android.BazelLabelForModuleSrc(ctx, p.Data))
696 combinedData.Append(android.BazelLabelForModuleDeps(ctx, p.Data_bins))
697 combinedData.Append(android.BazelLabelForModuleDeps(ctx, p.Data_libs))
Jingwen Chenebdc20f2022-09-01 18:54:50 +0000698 data.SetSelectValue(axis, config, combinedData)
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000699 tags.SetSelectValue(axis, config, p.Test_options.Tags)
Jingwen Chen537242c2022-08-24 11:53:27 +0000700 }
701 }
702 }
703
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -0400704 m.convertTidyAttributes(ctx, &testBinaryAttrs.tidyAttributes)
Sam Delmericofb3bb322022-10-21 10:42:24 -0400705
Jingwen Chen537242c2022-08-24 11:53:27 +0000706 for _, propIntf := range m.GetProperties() {
707 if testLinkerProps, ok := propIntf.(*TestLinkerProperties); ok {
708 testBinaryAttrs.Gtest = proptools.BoolDefault(testLinkerProps.Gtest, true)
709 testBinaryAttrs.Isolated = proptools.BoolDefault(testLinkerProps.Isolated, true)
710 break
711 }
712 }
713
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000714 for _, testProps := range m.GetProperties() {
715 if p, ok := testProps.(*TestBinaryProperties); ok {
716 useVendor := false // TODO Bug: 262914724
717 testInstallBase := getTestInstallBase(useVendor)
718 testConfigAttributes := tradefed.GetTestConfigAttributes(
719 ctx,
720 p.Test_config,
721 p.Test_options.Extra_test_configs,
722 p.Auto_gen_config,
723 p.Test_options.Test_suite_tag,
724 p.Test_config_template,
725 getTradefedConfigOptions(ctx, p, testBinaryAttrs.Isolated),
726 &testInstallBase,
727 )
728 testBinaryAttrs.TestConfigAttributes = testConfigAttributes
729 }
730 }
731
732 // TODO (b/262914724): convert to tradefed_cc_test and tradefed_cc_test_host
Jingwen Chen537242c2022-08-24 11:53:27 +0000733 ctx.CreateBazelTargetModule(
734 bazel.BazelTargetModuleProperties{
735 Rule_class: "cc_test",
736 Bzl_load_location: "//build/bazel/rules/cc:cc_test.bzl",
737 },
Jingwen Chenebdc20f2022-09-01 18:54:50 +0000738 android.CommonAttributes{
739 Name: m.Name(),
740 Data: data,
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000741 Tags: tags,
Jingwen Chenebdc20f2022-09-01 18:54:50 +0000742 },
Jingwen Chen537242c2022-08-24 11:53:27 +0000743 &testBinaryAttrs)
744}