blob: c9351e97e34ec2c8b50b436130954544e2d27d93 [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 (
Colin Cross3a02c7b2024-05-21 13:46:22 -070018 "github.com/google/blueprint/proptools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070019 "path/filepath"
nelsonli0d7111e2019-09-17 16:35:23 +080020 "strconv"
Julien Desprez3b933d32021-01-14 11:49:15 -080021
Colin Cross4d9c2d12016-07-29 12:48:20 -070022 "android/soong/android"
Colin Cross303e21f2018-08-07 16:49:25 -070023 "android/soong/tradefed"
Colin Cross4d9c2d12016-07-29 12:48:20 -070024)
25
Trevor Radcliffef389cb42022-03-24 21:06:14 +000026// TestLinkerProperties properties to be registered via the linker
27type TestLinkerProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070028 // if set, build against the gtest library. Defaults to true.
Colin Cross600c9df2016-09-13 12:26:16 -070029 Gtest *bool
Christopher Ferris9df92d62018-08-21 12:40:08 -070030
Trevor Radcliffecd8fd672022-05-03 14:51:16 +000031 // if set, use the isolated gtest runner. Defaults to true if gtest is also true and the arch is Windows, false
32 // otherwise.
Christopher Ferris9df92d62018-08-21 12:40:08 -070033 Isolated *bool
Colin Crossb916a382016-07-29 17:28:03 -070034}
Colin Cross4d9c2d12016-07-29 12:48:20 -070035
Trevor Radcliffef389cb42022-03-24 21:06:14 +000036// TestInstallerProperties properties to be registered via the installer
37type TestInstallerProperties struct {
38 // list of compatibility suites (for example "cts", "vts") that the module should be installed into.
39 Test_suites []string `android:"arch_variant"`
40}
41
yelinhsieh9fc60402018-10-01 19:23:14 +080042// Test option struct.
43type TestOptions struct {
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +080044 android.CommonTestOptions
45
yelinhsieh9fc60402018-10-01 19:23:14 +080046 // The UID that you want to run the test as on a device.
47 Run_test_as *string
Dan Shi95d19422020-08-15 12:24:26 -070048
David Srbecky519db272020-06-18 18:07:00 +010049 // A list of free-formed strings without spaces that categorize the test.
50 Test_suite_tag []string
Dan Shi95d19422020-08-15 12:24:26 -070051
52 // a list of extra test configuration files that should be installed with the module.
53 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -080054
Justin Yun46f66052021-05-04 18:42:24 +090055 // Add ShippingApiLevelModuleController to auto generated test config. If the device properties
Justin Yuna364cfb2021-05-13 12:39:42 +090056 // for the shipping api level is less than the min_shipping_api_level, skip this module.
57 Min_shipping_api_level *int64
58
59 // Add ShippingApiLevelModuleController to auto generated test config. If any of the device
60 // shipping api level and vendor api level properties are less than the
61 // vsr_min_shipping_api_level, skip this module.
62 // As this includes the shipping api level check, it is not allowed to define
63 // min_shipping_api_level at the same time with this property.
64 Vsr_min_shipping_api_level *int64
Justin Yun46f66052021-05-04 18:42:24 +090065
66 // Add MinApiLevelModuleController with ro.vndk.version property. If ro.vndk.version has an
Justin Yuna364cfb2021-05-13 12:39:42 +090067 // integer value and the value is less than the min_vndk_version, skip this module.
68 Min_vndk_version *int64
Dan Shiec731432023-05-26 04:21:44 +000069
70 // Extra <option> tags to add to the auto generated test xml file under the test runner, e.g., GTest.
71 // The "key" is optional in each of these.
72 Test_runner_options []tradefed.Option
yelinhsieh9fc60402018-10-01 19:23:14 +080073}
74
Colin Crossb916a382016-07-29 17:28:03 -070075type TestBinaryProperties struct {
Dan Willemsen3340d602016-12-27 14:40:40 -080076 // Disables the creation of a test-specific directory when used with
77 // relative_install_path. Useful if several tests need to be in the same
Colin Cross3a02c7b2024-05-21 13:46:22 -070078 // directory.
Dan Willemsen3340d602016-12-27 14:40:40 -080079 No_named_install_directory *bool
Colin Crossfaeb7aa2017-02-01 14:12:44 -080080
81 // list of files or filegroup modules that provide data that should be installed alongside
82 // the test
Dan Shi67a88342020-02-25 16:34:39 -080083 Data []string `android:"path,arch_variant"`
Colin Crossa929db02017-03-27 16:27:50 -070084
Chris Parsons79d66a52020-06-05 17:26:16 -040085 // list of shared library modules that should be installed alongside the test
86 Data_libs []string `android:"arch_variant"`
87
Colin Crossc8caa062021-09-24 16:50:14 -070088 // list of binary modules that should be installed alongside the test
89 Data_bins []string `android:"arch_variant"`
90
Julien Despreze146e392018-08-02 15:00:46 -070091 // the name of the test configuration (for example "AndroidTest.xml") that should be
92 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -080093 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -070094
95 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
96 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -080097 Test_config_template *string `android:"path,arch_variant"`
yelinhsieh9fc60402018-10-01 19:23:14 +080098
99 // Test options.
100 Test_options TestOptions
Dan Shi37ee3b82019-06-06 16:23:32 -0700101
102 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
103 // with root permission.
104 Require_root *bool
Dan Shi20ccd212019-08-27 10:37:24 -0700105
106 // Add RunCommandTargetPreparer to stop framework before the test and start it after the test.
107 Disable_framework *bool
nelsonli0d7111e2019-09-17 16:35:23 +0800108
Dan Shi6ffaaa82019-09-26 11:41:36 -0700109 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
110 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
111 // explicitly.
112 Auto_gen_config *bool
easoncylee1e3fdcd2020-04-30 10:08:33 +0800113
114 // Add parameterized mainline modules to auto generated test config. The options will be
115 // handled by TradeFed to download and install the specified modules on the device.
116 Test_mainline_modules []string
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700117
118 // Install the test into a folder named for the module in all test suites.
119 Per_testcase_directory *bool
Colin Cross4d9c2d12016-07-29 12:48:20 -0700120}
121
122func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700123 android.RegisterModuleType("cc_test", TestFactory)
124 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
125 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
126 android.RegisterModuleType("cc_test_host", TestHostFactory)
127 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700128}
129
Patrice Arrudac249c712019-03-19 17:00:29 -0700130// cc_test generates a test config file and an executable binary file to test
131// specific functionality on a device. The executable binary gets an implicit
132// static_libs dependency on libgtests unless the gtest flag is set to false.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700133func TestFactory() android.Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800134 module := NewTest(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700135 return module.Init()
136}
137
Patrice Arrudac249c712019-03-19 17:00:29 -0700138// cc_test_library creates an archive of files (i.e. .o files) which is later
139// referenced by another module (such as cc_test, cc_defaults or cc_test_library)
140// for archiving or linking.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700141func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700142 module := NewTestLibrary(android.HostAndDeviceSupported)
143 return module.Init()
144}
145
Patrice Arrudac249c712019-03-19 17:00:29 -0700146// cc_benchmark compiles an executable binary that performs benchmark testing
147// of a specific component in a device. Additional files such as test suites
148// and test configuration are installed on the side of the compiled executed
149// binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700150func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700151 module := NewBenchmark(android.HostAndDeviceSupported)
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000152 module.testModule = true
Colin Cross4d9c2d12016-07-29 12:48:20 -0700153 return module.Init()
154}
155
Patrice Arrudac249c712019-03-19 17:00:29 -0700156// cc_test_host compiles a test host binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700157func TestHostFactory() android.Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800158 module := NewTest(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159 return module.Init()
160}
161
Patrice Arrudac249c712019-03-19 17:00:29 -0700162// cc_benchmark_host compiles an executable binary that performs benchmark
163// testing of a specific component in the host. Additional files such as
164// test suites and test configuration are installed on the side of the
165// compiled executed binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700166func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700167 module := NewBenchmark(android.HostSupported)
168 return module.Init()
169}
170
Chris Parsons216e10a2020-07-09 17:12:52 -0400171func (test *testBinary) dataPaths() []android.DataPath {
Liz Kammer1c14a212020-05-12 15:26:55 -0700172 return test.data
173}
174
Jingwen Chen537242c2022-08-24 11:53:27 +0000175func (test *testBinary) testBinary() bool {
176 return true
177}
178
Colin Crossb916a382016-07-29 17:28:03 -0700179type testDecorator struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000180 LinkerProperties TestLinkerProperties
181 InstallerProperties TestInstallerProperties
182 installer *baseInstaller
183 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700184}
185
Colin Cross600c9df2016-09-13 12:26:16 -0700186func (test *testDecorator) gtest() bool {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000187 return BoolDefault(test.LinkerProperties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700188}
189
Spandan Dasf5a86552023-07-22 03:16:53 +0000190func (test *testDecorator) isolated(ctx android.EarlyModuleContext) bool {
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000191 return BoolDefault(test.LinkerProperties.Isolated, false)
192}
193
Jingwen Chen537242c2022-08-24 11:53:27 +0000194// NOTE: Keep this in sync with cc/cc_test.bzl#gtest_copts
Colin Crossb916a382016-07-29 17:28:03 -0700195func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700196 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700197 return flags
198 }
199
Colin Cross4af21ed2019-11-04 09:37:55 -0800200 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700201 if ctx.Host() {
Yi Kong12bebf92023-08-07 07:41:39 +0000202 flags.Local.CFlags = append([]string{"-Og", "-g"}, flags.Local.CFlags...)
203
204 // Turn off unused-result warning since it is not important for tests.
205 flags.Local.CFlags = append(flags.Local.CFlags, "-Wno-error=unused-result")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206
207 switch ctx.Os() {
208 case android.Windows:
Colin Cross4af21ed2019-11-04 09:37:55 -0800209 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700210 case android.Linux:
Colin Cross4af21ed2019-11-04 09:37:55 -0800211 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700212 case android.Darwin:
Colin Cross4af21ed2019-11-04 09:37:55 -0800213 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700214 }
215 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800216 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217 }
218
219 return flags
220}
221
Colin Crossb916a382016-07-29 17:28:03 -0700222func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700223 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700224 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800225 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000226 } else if test.isolated(ctx) {
Christopher Ferris9df92d62018-08-21 12:40:08 -0700227 deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main")
Christopher Ferris34cbba62019-07-17 15:46:29 -0700228 // The isolated library requires liblog, but adding it
229 // as a static library means unit tests cannot override
230 // liblog functions. Instead make it a shared library
231 // dependency.
232 deps.SharedLibs = append(deps.SharedLibs, "liblog")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700233 } else {
234 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
235 }
236 }
Colin Crossb916a382016-07-29 17:28:03 -0700237
Colin Cross4d9c2d12016-07-29 12:48:20 -0700238 return deps
239}
240
Colin Crossb916a382016-07-29 17:28:03 -0700241func (test *testDecorator) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000242 return []interface{}{&test.LinkerProperties}
243}
244
245func (test *testDecorator) installerProps() []interface{} {
246 return []interface{}{&test.InstallerProperties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700247}
248
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800249func (test *testDecorator) moduleInfoJSON(ctx android.ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
250 if android.PrefixInList(moduleInfoJSON.CompatibilitySuites, "mts-") &&
251 !android.InList("mts", moduleInfoJSON.CompatibilitySuites) {
252 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "mts")
253 }
254}
255
Colin Crossb916a382016-07-29 17:28:03 -0700256func NewTestInstaller() *baseInstaller {
257 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700258}
259
Colin Crossb916a382016-07-29 17:28:03 -0700260type testBinary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000261 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700262 *binaryDecorator
263 *baseCompiler
Dan Shi95d19422020-08-15 12:24:26 -0700264 Properties TestBinaryProperties
265 data []android.DataPath
266 testConfig android.Path
267 extraTestConfigs android.Paths
Colin Crossb916a382016-07-29 17:28:03 -0700268}
269
270func (test *testBinary) linkerProps() []interface{} {
271 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
272 props = append(props, &test.Properties)
273 return props
274}
275
Colin Cross37047f12016-12-13 17:06:13 -0800276func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700277 deps = test.testDecorator.linkerDeps(ctx, deps)
278 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Chris Parsons79d66a52020-06-05 17:26:16 -0400279 deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...)
Colin Crossc8caa062021-09-24 16:50:14 -0700280 deps.DataBins = append(deps.DataBins, test.Properties.Data_bins...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700281 return deps
282}
283
Colin Crossb916a382016-07-29 17:28:03 -0700284func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
285 flags = test.binaryDecorator.linkerFlags(ctx, flags)
286 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Crossb881e322024-05-10 13:24:17 -0700287
288 // Add a default rpath to allow tests to dlopen libraries specified in data_libs.
289 // Host modules already get an rpath specified in linker.go.
290 if !ctx.Host() {
291 flags.Global.LdFlags = append(flags.Global.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
292 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700293 return flags
294}
295
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800296func (test *testBinary) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
297 if ctx.Host() && Bool(test.Properties.Test_options.Unit_test) {
298 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "host-unit-tests")
299 }
300 moduleInfoJSON.TestOptionsTags = append(moduleInfoJSON.TestOptionsTags, test.Properties.Test_options.Tags...)
301 moduleInfoJSON.TestMainlineModules = append(moduleInfoJSON.TestMainlineModules, test.Properties.Test_mainline_modules...)
302 if test.testConfig != nil {
303 if _, ok := test.testConfig.(android.WritablePath); ok {
304 moduleInfoJSON.AutoTestConfig = []string{"true"}
305 }
306 moduleInfoJSON.TestConfig = append(moduleInfoJSON.TestConfig, test.testConfig.String())
307 }
308 moduleInfoJSON.TestConfig = append(moduleInfoJSON.TestConfig, test.extraTestConfigs.Strings()...)
309
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800310 moduleInfoJSON.DataDependencies = append(moduleInfoJSON.DataDependencies, test.Properties.Data_bins...)
311
312 if len(test.InstallerProperties.Test_suites) > 0 {
313 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, test.InstallerProperties.Test_suites...)
314 } else {
315 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "null-suite")
316 }
317
318 test.binaryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
319 test.testDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
320 moduleInfoJSON.Class = []string{"NATIVE_TESTS"}
321
322}
323
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000324func (test *testBinary) installerProps() []interface{} {
325 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
326}
327
Colin Crossb916a382016-07-29 17:28:03 -0700328func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Chris Parsons216e10a2020-07-09 17:12:52 -0400329 dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
330
331 for _, dataSrcPath := range dataSrcPaths {
332 test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
333 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400334
335 ctx.VisitDirectDepsWithTag(dataLibDepTag, func(dep android.Module) {
336 depName := ctx.OtherModuleName(dep)
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400337 linkableDep, ok := dep.(LinkableInterface)
Chris Parsons79d66a52020-06-05 17:26:16 -0400338 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400339 ctx.ModuleErrorf("data_lib %q is not a LinkableInterface module", depName)
Chris Parsons79d66a52020-06-05 17:26:16 -0400340 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400341 if linkableDep.OutputFile().Valid() {
Chris Parsons216e10a2020-07-09 17:12:52 -0400342 test.data = append(test.data,
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400343 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
344 RelativeInstallPath: linkableDep.RelativeInstallPath()})
Chris Parsons79d66a52020-06-05 17:26:16 -0400345 }
346 })
Colin Crossc8caa062021-09-24 16:50:14 -0700347 ctx.VisitDirectDepsWithTag(dataBinDepTag, func(dep android.Module) {
348 depName := ctx.OtherModuleName(dep)
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400349 linkableDep, ok := dep.(LinkableInterface)
Colin Crossc8caa062021-09-24 16:50:14 -0700350 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400351 ctx.ModuleErrorf("data_bin %q is not a LinkableInterface module", depName)
Colin Crossc8caa062021-09-24 16:50:14 -0700352 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400353 if linkableDep.OutputFile().Valid() {
Colin Crossc8caa062021-09-24 16:50:14 -0700354 test.data = append(test.data,
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400355 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
356 RelativeInstallPath: linkableDep.RelativeInstallPath()})
Colin Crossc8caa062021-09-24 16:50:14 -0700357 }
358 })
Chris Parsons79d66a52020-06-05 17:26:16 -0400359
Kiyoung Kimaa394802024-01-08 12:55:45 +0900360 testInstallBase := getTestInstallBase(ctx.InVendorOrProduct())
Dan Shiad042502023-08-02 13:53:00 -0700361 configs := getTradefedConfigOptions(ctx, &test.Properties, test.isolated(ctx), ctx.Device())
yelinhsieh9fc60402018-10-01 19:23:14 +0800362
Cole Faust21680542022-12-07 18:18:37 -0800363 test.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
364 TestConfigProp: test.Properties.Test_config,
365 TestConfigTemplateProp: test.Properties.Test_config_template,
366 TestSuites: test.testDecorator.InstallerProperties.Test_suites,
367 Config: configs,
Dan Shiec731432023-05-26 04:21:44 +0000368 TestRunnerOptions: test.Properties.Test_options.Test_runner_options,
Cole Faust21680542022-12-07 18:18:37 -0800369 AutoGenConfig: test.Properties.Auto_gen_config,
370 TestInstallBase: testInstallBase,
371 DeviceTemplate: "${NativeTestConfigTemplate}",
372 HostTemplate: "${NativeHostTestConfigTemplate}",
373 })
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800374
Dan Shi95d19422020-08-15 12:24:26 -0700375 test.extraTestConfigs = android.PathsForModuleSrc(ctx, test.Properties.Test_options.Extra_test_configs)
376
Colin Cross600c9df2016-09-13 12:26:16 -0700377 test.binaryDecorator.baseInstaller.dir = "nativetest"
378 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800379
380 if !Bool(test.Properties.No_named_install_directory) {
381 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800382 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800383 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
384 }
385
Julien Desprez8908b372021-02-04 10:10:16 -0800386 if ctx.Host() && test.gtest() && test.Properties.Test_options.Unit_test == nil {
Julien Desprez3b933d32021-01-14 11:49:15 -0800387 test.Properties.Test_options.Unit_test = proptools.BoolPtr(true)
388 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800389
390 test.binaryDecorator.baseInstaller.installTestData(ctx, test.data)
Dan Willemsen1d577e22016-08-29 15:53:15 -0700391 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700392}
393
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000394func getTestInstallBase(useVendor bool) string {
395 // TODO: (b/167308193) Switch to /data/local/tests/unrestricted as the default install base.
396 testInstallBase := "/data/local/tmp"
397 if useVendor {
398 testInstallBase = "/data/local/tests/vendor"
399 }
400 return testInstallBase
401}
402
Dan Shiad042502023-08-02 13:53:00 -0700403func getTradefedConfigOptions(ctx android.EarlyModuleContext, properties *TestBinaryProperties, isolated bool, device bool) []tradefed.Config {
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000404 var configs []tradefed.Config
405
406 for _, module := range properties.Test_mainline_modules {
407 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module})
408 }
Dan Shiad042502023-08-02 13:53:00 -0700409 if device {
410 if Bool(properties.Require_root) {
411 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
412 } else {
413 var options []tradefed.Option
414 options = append(options, tradefed.Option{Name: "force-root", Value: "false"})
415 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
416 }
417 if Bool(properties.Disable_framework) {
418 var options []tradefed.Option
419 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.StopServicesSetup", options})
420 }
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000421 }
422 if isolated {
423 configs = append(configs, tradefed.Option{Name: "not-shardable", Value: "true"})
424 }
425 if properties.Test_options.Run_test_as != nil {
426 configs = append(configs, tradefed.Option{Name: "run-test-as", Value: String(properties.Test_options.Run_test_as)})
427 }
428 for _, tag := range properties.Test_options.Test_suite_tag {
429 configs = append(configs, tradefed.Option{Name: "test-suite-tag", Value: tag})
430 }
431 if properties.Test_options.Min_shipping_api_level != nil {
432 if properties.Test_options.Vsr_min_shipping_api_level != nil {
433 ctx.PropertyErrorf("test_options.min_shipping_api_level", "must not be set at the same time as 'vsr_min_shipping_api_level'.")
434 }
435 var options []tradefed.Option
436 options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_shipping_api_level), 10)})
437 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
438 }
439 if properties.Test_options.Vsr_min_shipping_api_level != nil {
440 var options []tradefed.Option
441 options = append(options, tradefed.Option{Name: "vsr-min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Vsr_min_shipping_api_level), 10)})
442 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
443 }
444 if properties.Test_options.Min_vndk_version != nil {
445 var options []tradefed.Option
446 options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_vndk_version), 10)})
447 options = append(options, tradefed.Option{Name: "api-level-prop", Value: "ro.vndk.version"})
448 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.MinApiLevelModuleController", options})
449 }
450 return configs
451}
452
Colin Cross8ff10582023-12-07 13:10:56 -0800453func NewTest(hod android.HostOrDeviceSupported) *Module {
454 module, binary := newBinary(hod)
Colin Crossb916a382016-07-29 17:28:03 -0700455 module.multilib = android.MultilibBoth
Aditya Choudhary4b6eaf42023-11-21 15:38:37 +0000456 module.testModule = true
Dan Willemsen1d577e22016-08-29 15:53:15 -0700457 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700458
459 test := &testBinary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000460 testDecorator: &testDecorator{
461 linker: binary.baseLinker,
462 installer: binary.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700463 },
Colin Crossb916a382016-07-29 17:28:03 -0700464 binaryDecorator: binary,
465 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700466 }
Colin Crossb916a382016-07-29 17:28:03 -0700467 module.compiler = test
468 module.linker = test
469 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700470 return module
471}
472
Colin Crossb916a382016-07-29 17:28:03 -0700473type testLibrary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000474 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700475 *libraryDecorator
476}
477
Jingwen Chen537242c2022-08-24 11:53:27 +0000478func (test *testLibrary) testLibrary() bool {
479 return true
480}
481
Colin Crossb916a382016-07-29 17:28:03 -0700482func (test *testLibrary) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000483 var props []interface{}
484 props = append(props, test.testDecorator.linkerProps()...)
485 return append(props, test.libraryDecorator.linkerProps()...)
Colin Crossb916a382016-07-29 17:28:03 -0700486}
487
Colin Cross37047f12016-12-13 17:06:13 -0800488func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700489 deps = test.testDecorator.linkerDeps(ctx, deps)
490 deps = test.libraryDecorator.linkerDeps(ctx, deps)
491 return deps
492}
493
494func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
495 flags = test.libraryDecorator.linkerFlags(ctx, flags)
496 flags = test.testDecorator.linkerFlags(ctx, flags)
497 return flags
498}
499
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800500func (test *testLibrary) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
501 if len(test.InstallerProperties.Test_suites) > 0 {
502 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, test.InstallerProperties.Test_suites...)
503 }
504
505 test.libraryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
506 test.testDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
507}
508
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000509func (test *testLibrary) installerProps() []interface{} {
510 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
511}
512
Colin Cross4d9c2d12016-07-29 12:48:20 -0700513func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800514 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700515 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700516 test := &testLibrary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000517 testDecorator: &testDecorator{
518 linker: library.baseLinker,
519 installer: library.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700520 },
Colin Crossb916a382016-07-29 17:28:03 -0700521 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700522 }
Colin Crossb916a382016-07-29 17:28:03 -0700523 module.linker = test
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000524 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700525 return module
526}
527
Colin Crosse28f4e22017-04-24 18:10:29 -0700528type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700529 // list of files or filegroup modules that provide data that should be installed alongside
530 // the test
Colin Cross27b922f2019-03-04 22:35:41 -0800531 Data []string `android:"path"`
Anders Lewisb97e8182017-07-14 15:20:13 -0700532
Colin Crosse28f4e22017-04-24 18:10:29 -0700533 // list of compatibility suites (for example "cts", "vts") that the module should be
534 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700535 Test_suites []string `android:"arch_variant"`
536
537 // the name of the test configuration (for example "AndroidTest.xml") that should be
538 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800539 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700540
541 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
542 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800543 Test_config_template *string `android:"path,arch_variant"`
Dan Shi37ee3b82019-06-06 16:23:32 -0700544
545 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
546 // with root permission.
547 Require_root *bool
Dan Shi6ffaaa82019-09-26 11:41:36 -0700548
549 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
550 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
551 // explicitly.
552 Auto_gen_config *bool
Colin Crosse28f4e22017-04-24 18:10:29 -0700553}
554
Colin Crossb916a382016-07-29 17:28:03 -0700555type benchmarkDecorator struct {
556 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700557 Properties BenchmarkProperties
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800558 data []android.DataPath
Colin Cross303e21f2018-08-07 16:49:25 -0700559 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700560}
561
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400562func (benchmark *benchmarkDecorator) benchmarkBinary() bool {
563 return true
564}
565
Colin Crosse28f4e22017-04-24 18:10:29 -0700566func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
567 props := benchmark.binaryDecorator.linkerProps()
568 props = append(props, &benchmark.Properties)
569 return props
570}
571
Colin Cross37047f12016-12-13 17:06:13 -0800572func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700573 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700574 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
575 return deps
576}
577
Colin Crossb916a382016-07-29 17:28:03 -0700578func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800579 for _, d := range android.PathsForModuleSrc(ctx, benchmark.Properties.Data) {
580 benchmark.data = append(benchmark.data, android.DataPath{SrcPath: d})
581 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400582
Dan Shi37ee3b82019-06-06 16:23:32 -0700583 var configs []tradefed.Config
584 if Bool(benchmark.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800585 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shi37ee3b82019-06-06 16:23:32 -0700586 }
Cole Faust21680542022-12-07 18:18:37 -0800587 benchmark.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
588 TestConfigProp: benchmark.Properties.Test_config,
589 TestConfigTemplateProp: benchmark.Properties.Test_config_template,
590 TestSuites: benchmark.Properties.Test_suites,
591 Config: configs,
592 AutoGenConfig: benchmark.Properties.Auto_gen_config,
593 DeviceTemplate: "${NativeBenchmarkTestConfigTemplate}",
594 HostTemplate: "${NativeBenchmarkTestConfigTemplate}",
595 })
Colin Cross303e21f2018-08-07 16:49:25 -0700596
Colin Cross28690e92017-09-08 16:20:30 -0700597 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
598 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800599 benchmark.binaryDecorator.baseInstaller.installTestData(ctx, benchmark.data)
Dan Willemsen1d577e22016-08-29 15:53:15 -0700600 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700601}
602
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800603func (benchmark *benchmarkDecorator) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
604 benchmark.binaryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
605
606 moduleInfoJSON.Class = []string{"NATIVE_TESTS"}
607 if len(benchmark.Properties.Test_suites) > 0 {
608 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, benchmark.Properties.Test_suites...)
609 } else {
610 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "null-suite")
611 }
612
613 if android.PrefixInList(moduleInfoJSON.CompatibilitySuites, "mts-") &&
614 !android.InList("mts", moduleInfoJSON.CompatibilitySuites) {
615 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "mts")
616 }
617
618 if benchmark.testConfig != nil {
619 if _, ok := benchmark.testConfig.(android.WritablePath); ok {
620 moduleInfoJSON.AutoTestConfig = []string{"true"}
621 }
622 moduleInfoJSON.TestConfig = []string{benchmark.testConfig.String()}
623 }
624}
625
Colin Cross4d9c2d12016-07-29 12:48:20 -0700626func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800627 module, binary := newBinary(hod)
Colin Crossb916a382016-07-29 17:28:03 -0700628 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700629 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700630
631 benchmark := &benchmarkDecorator{
632 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700633 }
Colin Crossb916a382016-07-29 17:28:03 -0700634 module.linker = benchmark
635 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700636 return module
637}