blob: ae73886286576a47fda5815ad0d665b9dbd98e07 [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"
Julien Desprez3b933d32021-01-14 11:49:15 -080020
Cole Faust65cb40a2024-10-21 15:41:42 -070021 "github.com/google/blueprint/proptools"
22
Colin Cross4d9c2d12016-07-29 12:48:20 -070023 "android/soong/android"
Colin Cross303e21f2018-08-07 16:49:25 -070024 "android/soong/tradefed"
Colin Cross4d9c2d12016-07-29 12:48:20 -070025)
26
Trevor Radcliffef389cb42022-03-24 21:06:14 +000027// TestLinkerProperties properties to be registered via the linker
28type TestLinkerProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070029 // if set, build against the gtest library. Defaults to true.
Colin Cross600c9df2016-09-13 12:26:16 -070030 Gtest *bool
Christopher Ferris9df92d62018-08-21 12:40:08 -070031
Trevor Radcliffecd8fd672022-05-03 14:51:16 +000032 // if set, use the isolated gtest runner. Defaults to true if gtest is also true and the arch is Windows, false
33 // otherwise.
Christopher Ferris9df92d62018-08-21 12:40:08 -070034 Isolated *bool
Colin Crossb916a382016-07-29 17:28:03 -070035}
Colin Cross4d9c2d12016-07-29 12:48:20 -070036
Trevor Radcliffef389cb42022-03-24 21:06:14 +000037// TestInstallerProperties properties to be registered via the installer
38type TestInstallerProperties struct {
39 // list of compatibility suites (for example "cts", "vts") that the module should be installed into.
40 Test_suites []string `android:"arch_variant"`
41}
42
yelinhsieh9fc60402018-10-01 19:23:14 +080043// Test option struct.
44type TestOptions struct {
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +080045 android.CommonTestOptions
46
yelinhsieh9fc60402018-10-01 19:23:14 +080047 // The UID that you want to run the test as on a device.
48 Run_test_as *string
Dan Shi95d19422020-08-15 12:24:26 -070049
David Srbecky519db272020-06-18 18:07:00 +010050 // A list of free-formed strings without spaces that categorize the test.
51 Test_suite_tag []string
Dan Shi95d19422020-08-15 12:24:26 -070052
53 // a list of extra test configuration files that should be installed with the module.
54 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -080055
Justin Yun46f66052021-05-04 18:42:24 +090056 // Add ShippingApiLevelModuleController to auto generated test config. If the device properties
Justin Yuna364cfb2021-05-13 12:39:42 +090057 // for the shipping api level is less than the min_shipping_api_level, skip this module.
58 Min_shipping_api_level *int64
59
60 // Add ShippingApiLevelModuleController to auto generated test config. If any of the device
61 // shipping api level and vendor api level properties are less than the
62 // vsr_min_shipping_api_level, skip this module.
63 // As this includes the shipping api level check, it is not allowed to define
64 // min_shipping_api_level at the same time with this property.
65 Vsr_min_shipping_api_level *int64
Justin Yun46f66052021-05-04 18:42:24 +090066
67 // Add MinApiLevelModuleController with ro.vndk.version property. If ro.vndk.version has an
Justin Yuna364cfb2021-05-13 12:39:42 +090068 // integer value and the value is less than the min_vndk_version, skip this module.
69 Min_vndk_version *int64
Dan Shiec731432023-05-26 04:21:44 +000070
71 // Extra <option> tags to add to the auto generated test xml file under the test runner, e.g., GTest.
72 // The "key" is optional in each of these.
73 Test_runner_options []tradefed.Option
yelinhsieh9fc60402018-10-01 19:23:14 +080074}
75
Colin Crossb916a382016-07-29 17:28:03 -070076type TestBinaryProperties struct {
Dan Willemsen3340d602016-12-27 14:40:40 -080077 // Disables the creation of a test-specific directory when used with
78 // relative_install_path. Useful if several tests need to be in the same
Colin Cross3a02c7b2024-05-21 13:46:22 -070079 // directory.
Dan Willemsen3340d602016-12-27 14:40:40 -080080 No_named_install_directory *bool
Colin Crossfaeb7aa2017-02-01 14:12:44 -080081
82 // list of files or filegroup modules that provide data that should be installed alongside
83 // the test
Dan Shi67a88342020-02-25 16:34:39 -080084 Data []string `android:"path,arch_variant"`
Colin Crossa929db02017-03-27 16:27:50 -070085
Cole Faust65cb40a2024-10-21 15:41:42 -070086 // Same as data, but adds depedencies on modules using the device's os variant, and common
87 // architecture's variant. Can be useful to add device-built apps to the data of a host
88 // test.
89 Device_common_data []string `android:"path_device_common"`
90
91 // Same as data, but adds depedencies on modules using the device's os variant, and the device's
92 // first architecture's variant. Can be useful to add device-built apps to the data of a host
93 // test.
94 Device_first_data []string `android:"path_device_first"`
95
Chris Parsons79d66a52020-06-05 17:26:16 -040096 // list of shared library modules that should be installed alongside the test
97 Data_libs []string `android:"arch_variant"`
98
Colin Crossc8caa062021-09-24 16:50:14 -070099 // list of binary modules that should be installed alongside the test
100 Data_bins []string `android:"arch_variant"`
101
Julien Despreze146e392018-08-02 15:00:46 -0700102 // the name of the test configuration (for example "AndroidTest.xml") that should be
103 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800104 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700105
106 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
107 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800108 Test_config_template *string `android:"path,arch_variant"`
yelinhsieh9fc60402018-10-01 19:23:14 +0800109
110 // Test options.
111 Test_options TestOptions
Dan Shi37ee3b82019-06-06 16:23:32 -0700112
113 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
114 // with root permission.
115 Require_root *bool
Dan Shi20ccd212019-08-27 10:37:24 -0700116
117 // Add RunCommandTargetPreparer to stop framework before the test and start it after the test.
118 Disable_framework *bool
nelsonli0d7111e2019-09-17 16:35:23 +0800119
Dan Shi6ffaaa82019-09-26 11:41:36 -0700120 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
121 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
122 // explicitly.
123 Auto_gen_config *bool
easoncylee1e3fdcd2020-04-30 10:08:33 +0800124
125 // Add parameterized mainline modules to auto generated test config. The options will be
126 // handled by TradeFed to download and install the specified modules on the device.
127 Test_mainline_modules []string
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700128
129 // Install the test into a folder named for the module in all test suites.
130 Per_testcase_directory *bool
Colin Cross4d9c2d12016-07-29 12:48:20 -0700131}
132
133func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700134 android.RegisterModuleType("cc_test", TestFactory)
135 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
136 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
137 android.RegisterModuleType("cc_test_host", TestHostFactory)
138 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700139}
140
Patrice Arrudac249c712019-03-19 17:00:29 -0700141// cc_test generates a test config file and an executable binary file to test
142// specific functionality on a device. The executable binary gets an implicit
143// static_libs dependency on libgtests unless the gtest flag is set to false.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700144func TestFactory() android.Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800145 module := NewTest(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700146 return module.Init()
147}
148
Patrice Arrudac249c712019-03-19 17:00:29 -0700149// cc_test_library creates an archive of files (i.e. .o files) which is later
150// referenced by another module (such as cc_test, cc_defaults or cc_test_library)
151// for archiving or linking.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700152func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700153 module := NewTestLibrary(android.HostAndDeviceSupported)
154 return module.Init()
155}
156
Patrice Arrudac249c712019-03-19 17:00:29 -0700157// cc_benchmark compiles an executable binary that performs benchmark testing
158// of a specific component in a device. Additional files such as test suites
159// and test configuration are installed on the side of the compiled executed
160// binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700161func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162 module := NewBenchmark(android.HostAndDeviceSupported)
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000163 module.testModule = true
Colin Cross4d9c2d12016-07-29 12:48:20 -0700164 return module.Init()
165}
166
Patrice Arrudac249c712019-03-19 17:00:29 -0700167// cc_test_host compiles a test host binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700168func TestHostFactory() android.Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800169 module := NewTest(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700170 return module.Init()
171}
172
Patrice Arrudac249c712019-03-19 17:00:29 -0700173// cc_benchmark_host compiles an executable binary that performs benchmark
174// testing of a specific component in the host. Additional files such as
175// test suites and test configuration are installed on the side of the
176// compiled executed binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700177func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700178 module := NewBenchmark(android.HostSupported)
179 return module.Init()
180}
181
Chris Parsons216e10a2020-07-09 17:12:52 -0400182func (test *testBinary) dataPaths() []android.DataPath {
Liz Kammer1c14a212020-05-12 15:26:55 -0700183 return test.data
184}
185
Jingwen Chen537242c2022-08-24 11:53:27 +0000186func (test *testBinary) testBinary() bool {
187 return true
188}
189
Colin Crossb916a382016-07-29 17:28:03 -0700190type testDecorator struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000191 LinkerProperties TestLinkerProperties
192 InstallerProperties TestInstallerProperties
193 installer *baseInstaller
194 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700195}
196
Colin Cross600c9df2016-09-13 12:26:16 -0700197func (test *testDecorator) gtest() bool {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000198 return BoolDefault(test.LinkerProperties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700199}
200
Spandan Dasf5a86552023-07-22 03:16:53 +0000201func (test *testDecorator) isolated(ctx android.EarlyModuleContext) bool {
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000202 return BoolDefault(test.LinkerProperties.Isolated, false)
203}
204
Jingwen Chen537242c2022-08-24 11:53:27 +0000205// NOTE: Keep this in sync with cc/cc_test.bzl#gtest_copts
Colin Crossb916a382016-07-29 17:28:03 -0700206func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700207 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700208 return flags
209 }
210
Colin Cross4af21ed2019-11-04 09:37:55 -0800211 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700212 if ctx.Host() {
Liana Kazanovab88279f2024-07-15 18:45:23 +0000213 flags.Local.CFlags = append(flags.Local.CFlags, "-O0", "-g")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700214
215 switch ctx.Os() {
216 case android.Windows:
Colin Cross4af21ed2019-11-04 09:37:55 -0800217 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700218 case android.Linux:
Colin Cross4af21ed2019-11-04 09:37:55 -0800219 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220 case android.Darwin:
Colin Cross4af21ed2019-11-04 09:37:55 -0800221 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222 }
223 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800224 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700225 }
226
227 return flags
228}
229
Colin Crossb916a382016-07-29 17:28:03 -0700230func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700231 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700232 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800233 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000234 } else if test.isolated(ctx) {
Christopher Ferris9df92d62018-08-21 12:40:08 -0700235 deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main")
Christopher Ferris34cbba62019-07-17 15:46:29 -0700236 // The isolated library requires liblog, but adding it
237 // as a static library means unit tests cannot override
238 // liblog functions. Instead make it a shared library
239 // dependency.
240 deps.SharedLibs = append(deps.SharedLibs, "liblog")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241 } else {
242 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
243 }
244 }
Colin Crossb916a382016-07-29 17:28:03 -0700245
Colin Cross4d9c2d12016-07-29 12:48:20 -0700246 return deps
247}
248
Colin Crossb916a382016-07-29 17:28:03 -0700249func (test *testDecorator) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000250 return []interface{}{&test.LinkerProperties}
251}
252
253func (test *testDecorator) installerProps() []interface{} {
254 return []interface{}{&test.InstallerProperties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255}
256
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800257func (test *testDecorator) moduleInfoJSON(ctx android.ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
258 if android.PrefixInList(moduleInfoJSON.CompatibilitySuites, "mts-") &&
259 !android.InList("mts", moduleInfoJSON.CompatibilitySuites) {
260 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "mts")
261 }
262}
263
Colin Crossb916a382016-07-29 17:28:03 -0700264func NewTestInstaller() *baseInstaller {
265 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700266}
267
Colin Crossb916a382016-07-29 17:28:03 -0700268type testBinary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000269 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700270 *binaryDecorator
271 *baseCompiler
Dan Shi95d19422020-08-15 12:24:26 -0700272 Properties TestBinaryProperties
273 data []android.DataPath
274 testConfig android.Path
275 extraTestConfigs android.Paths
Colin Crossb916a382016-07-29 17:28:03 -0700276}
277
278func (test *testBinary) linkerProps() []interface{} {
279 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
280 props = append(props, &test.Properties)
281 return props
282}
283
Colin Cross37047f12016-12-13 17:06:13 -0800284func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700285 deps = test.testDecorator.linkerDeps(ctx, deps)
286 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Chris Parsons79d66a52020-06-05 17:26:16 -0400287 deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...)
Colin Crossc8caa062021-09-24 16:50:14 -0700288 deps.DataBins = append(deps.DataBins, test.Properties.Data_bins...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700289 return deps
290}
291
Colin Crossb916a382016-07-29 17:28:03 -0700292func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
293 flags = test.binaryDecorator.linkerFlags(ctx, flags)
294 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Crossb881e322024-05-10 13:24:17 -0700295
296 // Add a default rpath to allow tests to dlopen libraries specified in data_libs.
297 // Host modules already get an rpath specified in linker.go.
298 if !ctx.Host() {
299 flags.Global.LdFlags = append(flags.Global.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
300 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700301 return flags
302}
303
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800304func (test *testBinary) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
305 if ctx.Host() && Bool(test.Properties.Test_options.Unit_test) {
306 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "host-unit-tests")
307 }
308 moduleInfoJSON.TestOptionsTags = append(moduleInfoJSON.TestOptionsTags, test.Properties.Test_options.Tags...)
309 moduleInfoJSON.TestMainlineModules = append(moduleInfoJSON.TestMainlineModules, test.Properties.Test_mainline_modules...)
310 if test.testConfig != nil {
311 if _, ok := test.testConfig.(android.WritablePath); ok {
312 moduleInfoJSON.AutoTestConfig = []string{"true"}
313 }
314 moduleInfoJSON.TestConfig = append(moduleInfoJSON.TestConfig, test.testConfig.String())
315 }
316 moduleInfoJSON.TestConfig = append(moduleInfoJSON.TestConfig, test.extraTestConfigs.Strings()...)
317
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800318 moduleInfoJSON.DataDependencies = append(moduleInfoJSON.DataDependencies, test.Properties.Data_bins...)
319
320 if len(test.InstallerProperties.Test_suites) > 0 {
321 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, test.InstallerProperties.Test_suites...)
322 } else {
323 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "null-suite")
324 }
325
326 test.binaryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
327 test.testDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
328 moduleInfoJSON.Class = []string{"NATIVE_TESTS"}
329
330}
331
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000332func (test *testBinary) installerProps() []interface{} {
333 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
334}
335
Colin Crossb916a382016-07-29 17:28:03 -0700336func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Chris Parsons216e10a2020-07-09 17:12:52 -0400337 dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
Cole Faust65cb40a2024-10-21 15:41:42 -0700338 dataSrcPaths = append(dataSrcPaths, android.PathsForModuleSrc(ctx, test.Properties.Device_common_data)...)
339 dataSrcPaths = append(dataSrcPaths, android.PathsForModuleSrc(ctx, test.Properties.Device_first_data)...)
Chris Parsons216e10a2020-07-09 17:12:52 -0400340
341 for _, dataSrcPath := range dataSrcPaths {
342 test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
343 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400344
345 ctx.VisitDirectDepsWithTag(dataLibDepTag, func(dep android.Module) {
346 depName := ctx.OtherModuleName(dep)
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400347 linkableDep, ok := dep.(LinkableInterface)
Chris Parsons79d66a52020-06-05 17:26:16 -0400348 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400349 ctx.ModuleErrorf("data_lib %q is not a LinkableInterface module", depName)
Chris Parsons79d66a52020-06-05 17:26:16 -0400350 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400351 if linkableDep.OutputFile().Valid() {
Chris Parsons216e10a2020-07-09 17:12:52 -0400352 test.data = append(test.data,
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400353 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
354 RelativeInstallPath: linkableDep.RelativeInstallPath()})
Chris Parsons79d66a52020-06-05 17:26:16 -0400355 }
356 })
Colin Crossc8caa062021-09-24 16:50:14 -0700357 ctx.VisitDirectDepsWithTag(dataBinDepTag, func(dep android.Module) {
358 depName := ctx.OtherModuleName(dep)
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400359 linkableDep, ok := dep.(LinkableInterface)
Colin Crossc8caa062021-09-24 16:50:14 -0700360 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400361 ctx.ModuleErrorf("data_bin %q is not a LinkableInterface module", depName)
Colin Crossc8caa062021-09-24 16:50:14 -0700362 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400363 if linkableDep.OutputFile().Valid() {
Colin Crossc8caa062021-09-24 16:50:14 -0700364 test.data = append(test.data,
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400365 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
366 RelativeInstallPath: linkableDep.RelativeInstallPath()})
Colin Crossc8caa062021-09-24 16:50:14 -0700367 }
368 })
Chris Parsons79d66a52020-06-05 17:26:16 -0400369
Kiyoung Kimaa394802024-01-08 12:55:45 +0900370 testInstallBase := getTestInstallBase(ctx.InVendorOrProduct())
Dan Shiad042502023-08-02 13:53:00 -0700371 configs := getTradefedConfigOptions(ctx, &test.Properties, test.isolated(ctx), ctx.Device())
yelinhsieh9fc60402018-10-01 19:23:14 +0800372
Cole Faust21680542022-12-07 18:18:37 -0800373 test.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
374 TestConfigProp: test.Properties.Test_config,
375 TestConfigTemplateProp: test.Properties.Test_config_template,
376 TestSuites: test.testDecorator.InstallerProperties.Test_suites,
377 Config: configs,
Dan Shiec731432023-05-26 04:21:44 +0000378 TestRunnerOptions: test.Properties.Test_options.Test_runner_options,
Cole Faust21680542022-12-07 18:18:37 -0800379 AutoGenConfig: test.Properties.Auto_gen_config,
380 TestInstallBase: testInstallBase,
381 DeviceTemplate: "${NativeTestConfigTemplate}",
382 HostTemplate: "${NativeHostTestConfigTemplate}",
383 })
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800384
Dan Shi95d19422020-08-15 12:24:26 -0700385 test.extraTestConfigs = android.PathsForModuleSrc(ctx, test.Properties.Test_options.Extra_test_configs)
386
Colin Cross600c9df2016-09-13 12:26:16 -0700387 test.binaryDecorator.baseInstaller.dir = "nativetest"
388 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800389
390 if !Bool(test.Properties.No_named_install_directory) {
391 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800392 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800393 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
394 }
395
Julien Desprez8908b372021-02-04 10:10:16 -0800396 if ctx.Host() && test.gtest() && test.Properties.Test_options.Unit_test == nil {
Julien Desprez3b933d32021-01-14 11:49:15 -0800397 test.Properties.Test_options.Unit_test = proptools.BoolPtr(true)
398 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800399
400 test.binaryDecorator.baseInstaller.installTestData(ctx, test.data)
Dan Willemsen1d577e22016-08-29 15:53:15 -0700401 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700402}
403
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000404func getTestInstallBase(useVendor bool) string {
405 // TODO: (b/167308193) Switch to /data/local/tests/unrestricted as the default install base.
406 testInstallBase := "/data/local/tmp"
407 if useVendor {
408 testInstallBase = "/data/local/tests/vendor"
409 }
410 return testInstallBase
411}
412
Dan Shiad042502023-08-02 13:53:00 -0700413func getTradefedConfigOptions(ctx android.EarlyModuleContext, properties *TestBinaryProperties, isolated bool, device bool) []tradefed.Config {
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000414 var configs []tradefed.Config
415
416 for _, module := range properties.Test_mainline_modules {
417 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module})
418 }
Dan Shiad042502023-08-02 13:53:00 -0700419 if device {
420 if Bool(properties.Require_root) {
421 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
422 } else {
423 var options []tradefed.Option
424 options = append(options, tradefed.Option{Name: "force-root", Value: "false"})
425 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
426 }
427 if Bool(properties.Disable_framework) {
428 var options []tradefed.Option
429 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.StopServicesSetup", options})
430 }
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000431 }
432 if isolated {
433 configs = append(configs, tradefed.Option{Name: "not-shardable", Value: "true"})
434 }
435 if properties.Test_options.Run_test_as != nil {
436 configs = append(configs, tradefed.Option{Name: "run-test-as", Value: String(properties.Test_options.Run_test_as)})
437 }
438 for _, tag := range properties.Test_options.Test_suite_tag {
439 configs = append(configs, tradefed.Option{Name: "test-suite-tag", Value: tag})
440 }
441 if properties.Test_options.Min_shipping_api_level != nil {
442 if properties.Test_options.Vsr_min_shipping_api_level != nil {
443 ctx.PropertyErrorf("test_options.min_shipping_api_level", "must not be set at the same time as 'vsr_min_shipping_api_level'.")
444 }
445 var options []tradefed.Option
446 options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_shipping_api_level), 10)})
447 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
448 }
449 if properties.Test_options.Vsr_min_shipping_api_level != nil {
450 var options []tradefed.Option
451 options = append(options, tradefed.Option{Name: "vsr-min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Vsr_min_shipping_api_level), 10)})
452 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
453 }
454 if properties.Test_options.Min_vndk_version != nil {
455 var options []tradefed.Option
456 options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_vndk_version), 10)})
457 options = append(options, tradefed.Option{Name: "api-level-prop", Value: "ro.vndk.version"})
458 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.MinApiLevelModuleController", options})
459 }
460 return configs
461}
462
Colin Cross8ff10582023-12-07 13:10:56 -0800463func NewTest(hod android.HostOrDeviceSupported) *Module {
464 module, binary := newBinary(hod)
Colin Crossb916a382016-07-29 17:28:03 -0700465 module.multilib = android.MultilibBoth
Aditya Choudhary4b6eaf42023-11-21 15:38:37 +0000466 module.testModule = true
Dan Willemsen1d577e22016-08-29 15:53:15 -0700467 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700468
469 test := &testBinary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000470 testDecorator: &testDecorator{
471 linker: binary.baseLinker,
472 installer: binary.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700473 },
Colin Crossb916a382016-07-29 17:28:03 -0700474 binaryDecorator: binary,
475 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700476 }
Colin Crossb916a382016-07-29 17:28:03 -0700477 module.compiler = test
478 module.linker = test
479 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700480 return module
481}
482
Colin Crossb916a382016-07-29 17:28:03 -0700483type testLibrary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000484 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700485 *libraryDecorator
486}
487
Jingwen Chen537242c2022-08-24 11:53:27 +0000488func (test *testLibrary) testLibrary() bool {
489 return true
490}
491
Colin Crossb916a382016-07-29 17:28:03 -0700492func (test *testLibrary) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000493 var props []interface{}
494 props = append(props, test.testDecorator.linkerProps()...)
495 return append(props, test.libraryDecorator.linkerProps()...)
Colin Crossb916a382016-07-29 17:28:03 -0700496}
497
Colin Cross37047f12016-12-13 17:06:13 -0800498func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700499 deps = test.testDecorator.linkerDeps(ctx, deps)
500 deps = test.libraryDecorator.linkerDeps(ctx, deps)
501 return deps
502}
503
504func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
505 flags = test.libraryDecorator.linkerFlags(ctx, flags)
506 flags = test.testDecorator.linkerFlags(ctx, flags)
507 return flags
508}
509
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800510func (test *testLibrary) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
511 if len(test.InstallerProperties.Test_suites) > 0 {
512 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, test.InstallerProperties.Test_suites...)
513 }
514
515 test.libraryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
516 test.testDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
517}
518
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000519func (test *testLibrary) installerProps() []interface{} {
520 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
521}
522
Colin Cross4d9c2d12016-07-29 12:48:20 -0700523func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800524 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700525 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700526 test := &testLibrary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000527 testDecorator: &testDecorator{
528 linker: library.baseLinker,
529 installer: library.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700530 },
Colin Crossb916a382016-07-29 17:28:03 -0700531 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700532 }
Colin Crossb916a382016-07-29 17:28:03 -0700533 module.linker = test
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000534 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700535 return module
536}
537
Colin Crosse28f4e22017-04-24 18:10:29 -0700538type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700539 // list of files or filegroup modules that provide data that should be installed alongside
540 // the test
Colin Cross27b922f2019-03-04 22:35:41 -0800541 Data []string `android:"path"`
Anders Lewisb97e8182017-07-14 15:20:13 -0700542
Colin Crosse28f4e22017-04-24 18:10:29 -0700543 // list of compatibility suites (for example "cts", "vts") that the module should be
544 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700545 Test_suites []string `android:"arch_variant"`
546
547 // the name of the test configuration (for example "AndroidTest.xml") that should be
548 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800549 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700550
551 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
552 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800553 Test_config_template *string `android:"path,arch_variant"`
Dan Shi37ee3b82019-06-06 16:23:32 -0700554
555 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
556 // with root permission.
557 Require_root *bool
Dan Shi6ffaaa82019-09-26 11:41:36 -0700558
559 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
560 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
561 // explicitly.
562 Auto_gen_config *bool
Colin Crosse28f4e22017-04-24 18:10:29 -0700563}
564
Colin Crossb916a382016-07-29 17:28:03 -0700565type benchmarkDecorator struct {
566 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700567 Properties BenchmarkProperties
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800568 data []android.DataPath
Colin Cross303e21f2018-08-07 16:49:25 -0700569 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700570}
571
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400572func (benchmark *benchmarkDecorator) benchmarkBinary() bool {
573 return true
574}
575
Colin Crosse28f4e22017-04-24 18:10:29 -0700576func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
577 props := benchmark.binaryDecorator.linkerProps()
578 props = append(props, &benchmark.Properties)
579 return props
580}
581
Colin Cross37047f12016-12-13 17:06:13 -0800582func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700583 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700584 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
585 return deps
586}
587
Colin Crossb916a382016-07-29 17:28:03 -0700588func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800589 for _, d := range android.PathsForModuleSrc(ctx, benchmark.Properties.Data) {
590 benchmark.data = append(benchmark.data, android.DataPath{SrcPath: d})
591 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400592
Dan Shi37ee3b82019-06-06 16:23:32 -0700593 var configs []tradefed.Config
594 if Bool(benchmark.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800595 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shi37ee3b82019-06-06 16:23:32 -0700596 }
Cole Faust21680542022-12-07 18:18:37 -0800597 benchmark.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
598 TestConfigProp: benchmark.Properties.Test_config,
599 TestConfigTemplateProp: benchmark.Properties.Test_config_template,
600 TestSuites: benchmark.Properties.Test_suites,
601 Config: configs,
602 AutoGenConfig: benchmark.Properties.Auto_gen_config,
603 DeviceTemplate: "${NativeBenchmarkTestConfigTemplate}",
604 HostTemplate: "${NativeBenchmarkTestConfigTemplate}",
605 })
Colin Cross303e21f2018-08-07 16:49:25 -0700606
Colin Cross28690e92017-09-08 16:20:30 -0700607 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
608 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800609 benchmark.binaryDecorator.baseInstaller.installTestData(ctx, benchmark.data)
Dan Willemsen1d577e22016-08-29 15:53:15 -0700610 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700611}
612
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800613func (benchmark *benchmarkDecorator) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
614 benchmark.binaryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
615
616 moduleInfoJSON.Class = []string{"NATIVE_TESTS"}
617 if len(benchmark.Properties.Test_suites) > 0 {
618 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, benchmark.Properties.Test_suites...)
619 } else {
620 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "null-suite")
621 }
622
623 if android.PrefixInList(moduleInfoJSON.CompatibilitySuites, "mts-") &&
624 !android.InList("mts", moduleInfoJSON.CompatibilitySuites) {
625 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "mts")
626 }
627
628 if benchmark.testConfig != nil {
629 if _, ok := benchmark.testConfig.(android.WritablePath); ok {
630 moduleInfoJSON.AutoTestConfig = []string{"true"}
631 }
632 moduleInfoJSON.TestConfig = []string{benchmark.testConfig.String()}
633 }
634}
635
Colin Cross4d9c2d12016-07-29 12:48:20 -0700636func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800637 module, binary := newBinary(hod)
Colin Crossb916a382016-07-29 17:28:03 -0700638 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700639 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700640
641 benchmark := &benchmarkDecorator{
642 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700643 }
Colin Crossb916a382016-07-29 17:28:03 -0700644 module.linker = benchmark
645 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700646 return module
647}