blob: 007932eb51fa57d519864e5847ad7338817be7a6 [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"
Colin Cross303e21f2018-08-07 16:49:25 -070025 "android/soong/tradefed"
Colin Cross4d9c2d12016-07-29 12:48:20 -070026)
27
Trevor Radcliffef389cb42022-03-24 21:06:14 +000028// TestLinkerProperties properties to be registered via the linker
29type TestLinkerProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070030 // if set, build against the gtest library. Defaults to true.
Colin Cross600c9df2016-09-13 12:26:16 -070031 Gtest *bool
Christopher Ferris9df92d62018-08-21 12:40:08 -070032
Trevor Radcliffecd8fd672022-05-03 14:51:16 +000033 // if set, use the isolated gtest runner. Defaults to true if gtest is also true and the arch is Windows, false
34 // otherwise.
Christopher Ferris9df92d62018-08-21 12:40:08 -070035 Isolated *bool
Colin Crossb916a382016-07-29 17:28:03 -070036}
Colin Cross4d9c2d12016-07-29 12:48:20 -070037
Trevor Radcliffef389cb42022-03-24 21:06:14 +000038// TestInstallerProperties properties to be registered via the installer
39type TestInstallerProperties struct {
40 // list of compatibility suites (for example "cts", "vts") that the module should be installed into.
41 Test_suites []string `android:"arch_variant"`
42}
43
yelinhsieh9fc60402018-10-01 19:23:14 +080044// Test option struct.
45type TestOptions struct {
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +080046 android.CommonTestOptions
47
yelinhsieh9fc60402018-10-01 19:23:14 +080048 // The UID that you want to run the test as on a device.
49 Run_test_as *string
Dan Shi95d19422020-08-15 12:24:26 -070050
David Srbecky519db272020-06-18 18:07:00 +010051 // A list of free-formed strings without spaces that categorize the test.
52 Test_suite_tag []string
Dan Shi95d19422020-08-15 12:24:26 -070053
54 // a list of extra test configuration files that should be installed with the module.
55 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -080056
Justin Yun46f66052021-05-04 18:42:24 +090057 // Add ShippingApiLevelModuleController to auto generated test config. If the device properties
Justin Yuna364cfb2021-05-13 12:39:42 +090058 // for the shipping api level is less than the min_shipping_api_level, skip this module.
59 Min_shipping_api_level *int64
60
61 // Add ShippingApiLevelModuleController to auto generated test config. If any of the device
62 // shipping api level and vendor api level properties are less than the
63 // vsr_min_shipping_api_level, skip this module.
64 // As this includes the shipping api level check, it is not allowed to define
65 // min_shipping_api_level at the same time with this property.
66 Vsr_min_shipping_api_level *int64
Justin Yun46f66052021-05-04 18:42:24 +090067
68 // Add MinApiLevelModuleController with ro.vndk.version property. If ro.vndk.version has an
Justin Yuna364cfb2021-05-13 12:39:42 +090069 // integer value and the value is less than the min_vndk_version, skip this module.
70 Min_vndk_version *int64
Dan Shiec731432023-05-26 04:21:44 +000071
72 // Extra <option> tags to add to the auto generated test xml file under the test runner, e.g., GTest.
73 // The "key" is optional in each of these.
74 Test_runner_options []tradefed.Option
yelinhsieh9fc60402018-10-01 19:23:14 +080075}
76
Colin Crossb916a382016-07-29 17:28:03 -070077type TestBinaryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070078 // Create a separate binary for each source file. Useful when there is
79 // global state that can not be torn down and reset between each test suite.
80 Test_per_src *bool
Dan Willemsen3340d602016-12-27 14:40:40 -080081
82 // Disables the creation of a test-specific directory when used with
83 // relative_install_path. Useful if several tests need to be in the same
84 // directory, but test_per_src doesn't work.
85 No_named_install_directory *bool
Colin Crossfaeb7aa2017-02-01 14:12:44 -080086
87 // list of files or filegroup modules that provide data that should be installed alongside
88 // the test
Dan Shi67a88342020-02-25 16:34:39 -080089 Data []string `android:"path,arch_variant"`
Colin Crossa929db02017-03-27 16:27:50 -070090
Chris Parsons79d66a52020-06-05 17:26:16 -040091 // list of shared library modules that should be installed alongside the test
92 Data_libs []string `android:"arch_variant"`
93
Colin Crossc8caa062021-09-24 16:50:14 -070094 // list of binary modules that should be installed alongside the test
95 Data_bins []string `android:"arch_variant"`
96
Julien Despreze146e392018-08-02 15:00:46 -070097 // the name of the test configuration (for example "AndroidTest.xml") that should be
98 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -080099 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700100
101 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
102 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800103 Test_config_template *string `android:"path,arch_variant"`
yelinhsieh9fc60402018-10-01 19:23:14 +0800104
105 // Test options.
106 Test_options TestOptions
Dan Shi37ee3b82019-06-06 16:23:32 -0700107
108 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
109 // with root permission.
110 Require_root *bool
Dan Shi20ccd212019-08-27 10:37:24 -0700111
112 // Add RunCommandTargetPreparer to stop framework before the test and start it after the test.
113 Disable_framework *bool
nelsonli0d7111e2019-09-17 16:35:23 +0800114
Dan Shi6ffaaa82019-09-26 11:41:36 -0700115 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
116 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
117 // explicitly.
118 Auto_gen_config *bool
easoncylee1e3fdcd2020-04-30 10:08:33 +0800119
120 // Add parameterized mainline modules to auto generated test config. The options will be
121 // handled by TradeFed to download and install the specified modules on the device.
122 Test_mainline_modules []string
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700123
124 // Install the test into a folder named for the module in all test suites.
125 Per_testcase_directory *bool
Colin Cross4d9c2d12016-07-29 12:48:20 -0700126}
127
128func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700129 android.RegisterModuleType("cc_test", TestFactory)
130 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
131 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
132 android.RegisterModuleType("cc_test_host", TestHostFactory)
133 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700134}
135
Patrice Arrudac249c712019-03-19 17:00:29 -0700136// cc_test generates a test config file and an executable binary file to test
137// specific functionality on a device. The executable binary gets an implicit
138// static_libs dependency on libgtests unless the gtest flag is set to false.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700139func TestFactory() android.Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800140 module := NewTest(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700141 return module.Init()
142}
143
Patrice Arrudac249c712019-03-19 17:00:29 -0700144// cc_test_library creates an archive of files (i.e. .o files) which is later
145// referenced by another module (such as cc_test, cc_defaults or cc_test_library)
146// for archiving or linking.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700147func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148 module := NewTestLibrary(android.HostAndDeviceSupported)
149 return module.Init()
150}
151
Patrice Arrudac249c712019-03-19 17:00:29 -0700152// cc_benchmark compiles an executable binary that performs benchmark testing
153// of a specific component in a device. Additional files such as test suites
154// and test configuration are installed on the side of the compiled executed
155// binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700156func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700157 module := NewBenchmark(android.HostAndDeviceSupported)
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000158 module.testModule = true
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159 return module.Init()
160}
161
Patrice Arrudac249c712019-03-19 17:00:29 -0700162// cc_test_host compiles a test host binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700163func TestHostFactory() android.Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800164 module := NewTest(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700165 return module.Init()
166}
167
Patrice Arrudac249c712019-03-19 17:00:29 -0700168// cc_benchmark_host compiles an executable binary that performs benchmark
169// testing of a specific component in the host. Additional files such as
170// test suites and test configuration are installed on the side of the
171// compiled executed binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700172func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700173 module := NewBenchmark(android.HostSupported)
174 return module.Init()
175}
176
Colin Crossb916a382016-07-29 17:28:03 -0700177type testPerSrc interface {
178 testPerSrc() bool
179 srcs() []string
Roland Levillainf2fad972019-06-28 15:41:19 +0100180 isAllTestsVariation() bool
Colin Crossb916a382016-07-29 17:28:03 -0700181 setSrc(string, string)
Roland Levillainf2fad972019-06-28 15:41:19 +0100182 unsetSrc()
Colin Crossb916a382016-07-29 17:28:03 -0700183}
184
185func (test *testBinary) testPerSrc() bool {
186 return Bool(test.Properties.Test_per_src)
187}
188
189func (test *testBinary) srcs() []string {
190 return test.baseCompiler.Properties.Srcs
191}
192
Chris Parsons216e10a2020-07-09 17:12:52 -0400193func (test *testBinary) dataPaths() []android.DataPath {
Liz Kammer1c14a212020-05-12 15:26:55 -0700194 return test.data
195}
196
Roland Levillainf2fad972019-06-28 15:41:19 +0100197func (test *testBinary) isAllTestsVariation() bool {
198 stem := test.binaryDecorator.Properties.Stem
199 return stem != nil && *stem == ""
200}
201
Colin Crossb916a382016-07-29 17:28:03 -0700202func (test *testBinary) setSrc(name, src string) {
203 test.baseCompiler.Properties.Srcs = []string{src}
Nan Zhang0007d812017-11-07 10:57:05 -0800204 test.binaryDecorator.Properties.Stem = StringPtr(name)
Colin Crossb916a382016-07-29 17:28:03 -0700205}
206
Roland Levillainf2fad972019-06-28 15:41:19 +0100207func (test *testBinary) unsetSrc() {
208 test.baseCompiler.Properties.Srcs = nil
209 test.binaryDecorator.Properties.Stem = StringPtr("")
210}
211
Jingwen Chen537242c2022-08-24 11:53:27 +0000212func (test *testBinary) testBinary() bool {
213 return true
214}
215
Colin Crossb916a382016-07-29 17:28:03 -0700216var _ testPerSrc = (*testBinary)(nil)
217
Roland Levillain9b5fde92019-06-28 15:41:19 +0100218func TestPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700219 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700220 if test, ok := m.linker.(testPerSrc); ok {
Roland Levillainf2fad972019-06-28 15:41:19 +0100221 numTests := len(test.srcs())
222 if test.testPerSrc() && numTests > 0 {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700223 if duplicate, found := android.CheckDuplicate(test.srcs()); found {
Jooyung Hana61ff2c2019-02-28 18:06:34 +0900224 mctx.PropertyErrorf("srcs", "found a duplicate entry %q", duplicate)
225 return
226 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100227 testNames := make([]string, numTests)
Colin Crossb916a382016-07-29 17:28:03 -0700228 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700229 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
230 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100231 // In addition to creating one variation per test source file,
232 // create an additional "all tests" variation named "", and have it
233 // depends on all other test_per_src variations. This is useful to
234 // create subsequent dependencies of a given module on all
235 // test_per_src variations created above: by depending on
236 // variation "", that module will transitively depend on all the
237 // other test_per_src variations without the need to know their
238 // name or even their number.
239 testNames = append(testNames, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700240 tests := mctx.CreateLocalVariations(testNames...)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800241 allTests := tests[numTests]
242 allTests.(*Module).linker.(testPerSrc).unsetSrc()
Roland Levillainf2fad972019-06-28 15:41:19 +0100243 // Prevent the "all tests" variation from being installable nor
244 // exporting to Make, as it won't create any output file.
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800245 allTests.(*Module).Properties.PreventInstall = true
246 allTests.(*Module).Properties.HideFromMake = true
Colin Crossb916a382016-07-29 17:28:03 -0700247 for i, src := range test.srcs() {
248 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800249 mctx.AddInterVariantDependency(testPerSrcDepTag, allTests, tests[i])
Colin Cross4d9c2d12016-07-29 12:48:20 -0700250 }
Colin Cross90dab342020-08-21 15:55:50 -0700251 mctx.AliasVariation("")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700252 }
253 }
254 }
255}
256
Colin Crossb916a382016-07-29 17:28:03 -0700257type testDecorator struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000258 LinkerProperties TestLinkerProperties
259 InstallerProperties TestInstallerProperties
260 installer *baseInstaller
261 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700262}
263
Colin Cross600c9df2016-09-13 12:26:16 -0700264func (test *testDecorator) gtest() bool {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000265 return BoolDefault(test.LinkerProperties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700266}
267
Spandan Dasf5a86552023-07-22 03:16:53 +0000268func (test *testDecorator) isolated(ctx android.EarlyModuleContext) bool {
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000269 return BoolDefault(test.LinkerProperties.Isolated, false)
270}
271
Jingwen Chen537242c2022-08-24 11:53:27 +0000272// NOTE: Keep this in sync with cc/cc_test.bzl#gtest_copts
Colin Crossb916a382016-07-29 17:28:03 -0700273func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700274 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700275 return flags
276 }
277
Colin Cross4af21ed2019-11-04 09:37:55 -0800278 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700279 if ctx.Host() {
Yi Kong12bebf92023-08-07 07:41:39 +0000280 flags.Local.CFlags = append([]string{"-Og", "-g"}, flags.Local.CFlags...)
281
282 // Turn off unused-result warning since it is not important for tests.
283 flags.Local.CFlags = append(flags.Local.CFlags, "-Wno-error=unused-result")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700284
285 switch ctx.Os() {
286 case android.Windows:
Colin Cross4af21ed2019-11-04 09:37:55 -0800287 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700288 case android.Linux:
Colin Cross4af21ed2019-11-04 09:37:55 -0800289 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700290 case android.Darwin:
Colin Cross4af21ed2019-11-04 09:37:55 -0800291 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700292 }
293 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800294 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700295 }
296
297 return flags
298}
299
Colin Crossb916a382016-07-29 17:28:03 -0700300func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700301 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700302 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800303 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000304 } else if test.isolated(ctx) {
Christopher Ferris9df92d62018-08-21 12:40:08 -0700305 deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main")
Christopher Ferris34cbba62019-07-17 15:46:29 -0700306 // The isolated library requires liblog, but adding it
307 // as a static library means unit tests cannot override
308 // liblog functions. Instead make it a shared library
309 // dependency.
310 deps.SharedLibs = append(deps.SharedLibs, "liblog")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700311 } else {
312 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
313 }
314 }
Colin Crossb916a382016-07-29 17:28:03 -0700315
Colin Cross4d9c2d12016-07-29 12:48:20 -0700316 return deps
317}
318
Colin Crossb916a382016-07-29 17:28:03 -0700319func (test *testDecorator) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000320 return []interface{}{&test.LinkerProperties}
321}
322
323func (test *testDecorator) installerProps() []interface{} {
324 return []interface{}{&test.InstallerProperties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700325}
326
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800327func (test *testDecorator) moduleInfoJSON(ctx android.ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
328 if android.PrefixInList(moduleInfoJSON.CompatibilitySuites, "mts-") &&
329 !android.InList("mts", moduleInfoJSON.CompatibilitySuites) {
330 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "mts")
331 }
332}
333
Colin Crossb916a382016-07-29 17:28:03 -0700334func NewTestInstaller() *baseInstaller {
335 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700336}
337
Colin Crossb916a382016-07-29 17:28:03 -0700338type testBinary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000339 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700340 *binaryDecorator
341 *baseCompiler
Dan Shi95d19422020-08-15 12:24:26 -0700342 Properties TestBinaryProperties
343 data []android.DataPath
344 testConfig android.Path
345 extraTestConfigs android.Paths
Colin Crossb916a382016-07-29 17:28:03 -0700346}
347
348func (test *testBinary) linkerProps() []interface{} {
349 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
350 props = append(props, &test.Properties)
351 return props
352}
353
Colin Cross37047f12016-12-13 17:06:13 -0800354func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700355 deps = test.testDecorator.linkerDeps(ctx, deps)
356 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Chris Parsons79d66a52020-06-05 17:26:16 -0400357 deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...)
Colin Crossc8caa062021-09-24 16:50:14 -0700358 deps.DataBins = append(deps.DataBins, test.Properties.Data_bins...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700359 return deps
360}
361
Colin Crossb916a382016-07-29 17:28:03 -0700362func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
363 flags = test.binaryDecorator.linkerFlags(ctx, flags)
364 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Crossb881e322024-05-10 13:24:17 -0700365
366 // Add a default rpath to allow tests to dlopen libraries specified in data_libs.
367 // Host modules already get an rpath specified in linker.go.
368 if !ctx.Host() {
369 flags.Global.LdFlags = append(flags.Global.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
370 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700371 return flags
372}
373
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800374func (test *testBinary) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
375 if ctx.Host() && Bool(test.Properties.Test_options.Unit_test) {
376 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "host-unit-tests")
377 }
378 moduleInfoJSON.TestOptionsTags = append(moduleInfoJSON.TestOptionsTags, test.Properties.Test_options.Tags...)
379 moduleInfoJSON.TestMainlineModules = append(moduleInfoJSON.TestMainlineModules, test.Properties.Test_mainline_modules...)
380 if test.testConfig != nil {
381 if _, ok := test.testConfig.(android.WritablePath); ok {
382 moduleInfoJSON.AutoTestConfig = []string{"true"}
383 }
384 moduleInfoJSON.TestConfig = append(moduleInfoJSON.TestConfig, test.testConfig.String())
385 }
386 moduleInfoJSON.TestConfig = append(moduleInfoJSON.TestConfig, test.extraTestConfigs.Strings()...)
387
388 if Bool(test.Properties.Test_per_src) {
389 moduleInfoJSON.SubName = "_" + String(test.binaryDecorator.Properties.Stem)
390 }
391
392 moduleInfoJSON.DataDependencies = append(moduleInfoJSON.DataDependencies, test.Properties.Data_bins...)
393
394 if len(test.InstallerProperties.Test_suites) > 0 {
395 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, test.InstallerProperties.Test_suites...)
396 } else {
397 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "null-suite")
398 }
399
400 test.binaryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
401 test.testDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
402 moduleInfoJSON.Class = []string{"NATIVE_TESTS"}
403
404}
405
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000406func (test *testBinary) installerProps() []interface{} {
407 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
408}
409
Colin Crossb916a382016-07-29 17:28:03 -0700410func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Chris Parsons216e10a2020-07-09 17:12:52 -0400411 dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
412
413 for _, dataSrcPath := range dataSrcPaths {
414 test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
415 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400416
417 ctx.VisitDirectDepsWithTag(dataLibDepTag, func(dep android.Module) {
418 depName := ctx.OtherModuleName(dep)
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400419 linkableDep, ok := dep.(LinkableInterface)
Chris Parsons79d66a52020-06-05 17:26:16 -0400420 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400421 ctx.ModuleErrorf("data_lib %q is not a LinkableInterface module", depName)
Chris Parsons79d66a52020-06-05 17:26:16 -0400422 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400423 if linkableDep.OutputFile().Valid() {
Chris Parsons216e10a2020-07-09 17:12:52 -0400424 test.data = append(test.data,
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400425 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
426 RelativeInstallPath: linkableDep.RelativeInstallPath()})
Chris Parsons79d66a52020-06-05 17:26:16 -0400427 }
428 })
Colin Crossc8caa062021-09-24 16:50:14 -0700429 ctx.VisitDirectDepsWithTag(dataBinDepTag, func(dep android.Module) {
430 depName := ctx.OtherModuleName(dep)
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400431 linkableDep, ok := dep.(LinkableInterface)
Colin Crossc8caa062021-09-24 16:50:14 -0700432 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400433 ctx.ModuleErrorf("data_bin %q is not a LinkableInterface module", depName)
Colin Crossc8caa062021-09-24 16:50:14 -0700434 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400435 if linkableDep.OutputFile().Valid() {
Colin Crossc8caa062021-09-24 16:50:14 -0700436 test.data = append(test.data,
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400437 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
438 RelativeInstallPath: linkableDep.RelativeInstallPath()})
Colin Crossc8caa062021-09-24 16:50:14 -0700439 }
440 })
Chris Parsons79d66a52020-06-05 17:26:16 -0400441
Kiyoung Kimaa394802024-01-08 12:55:45 +0900442 testInstallBase := getTestInstallBase(ctx.InVendorOrProduct())
Dan Shiad042502023-08-02 13:53:00 -0700443 configs := getTradefedConfigOptions(ctx, &test.Properties, test.isolated(ctx), ctx.Device())
yelinhsieh9fc60402018-10-01 19:23:14 +0800444
Cole Faust21680542022-12-07 18:18:37 -0800445 test.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
446 TestConfigProp: test.Properties.Test_config,
447 TestConfigTemplateProp: test.Properties.Test_config_template,
448 TestSuites: test.testDecorator.InstallerProperties.Test_suites,
449 Config: configs,
Dan Shiec731432023-05-26 04:21:44 +0000450 TestRunnerOptions: test.Properties.Test_options.Test_runner_options,
Cole Faust21680542022-12-07 18:18:37 -0800451 AutoGenConfig: test.Properties.Auto_gen_config,
452 TestInstallBase: testInstallBase,
453 DeviceTemplate: "${NativeTestConfigTemplate}",
454 HostTemplate: "${NativeHostTestConfigTemplate}",
455 })
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800456
Dan Shi95d19422020-08-15 12:24:26 -0700457 test.extraTestConfigs = android.PathsForModuleSrc(ctx, test.Properties.Test_options.Extra_test_configs)
458
Colin Cross600c9df2016-09-13 12:26:16 -0700459 test.binaryDecorator.baseInstaller.dir = "nativetest"
460 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800461
462 if !Bool(test.Properties.No_named_install_directory) {
463 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800464 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800465 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
466 }
467
Julien Desprez8908b372021-02-04 10:10:16 -0800468 if ctx.Host() && test.gtest() && test.Properties.Test_options.Unit_test == nil {
Julien Desprez3b933d32021-01-14 11:49:15 -0800469 test.Properties.Test_options.Unit_test = proptools.BoolPtr(true)
470 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800471
472 test.binaryDecorator.baseInstaller.installTestData(ctx, test.data)
Dan Willemsen1d577e22016-08-29 15:53:15 -0700473 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700474}
475
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000476func getTestInstallBase(useVendor bool) string {
477 // TODO: (b/167308193) Switch to /data/local/tests/unrestricted as the default install base.
478 testInstallBase := "/data/local/tmp"
479 if useVendor {
480 testInstallBase = "/data/local/tests/vendor"
481 }
482 return testInstallBase
483}
484
Dan Shiad042502023-08-02 13:53:00 -0700485func getTradefedConfigOptions(ctx android.EarlyModuleContext, properties *TestBinaryProperties, isolated bool, device bool) []tradefed.Config {
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000486 var configs []tradefed.Config
487
488 for _, module := range properties.Test_mainline_modules {
489 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module})
490 }
Dan Shiad042502023-08-02 13:53:00 -0700491 if device {
492 if Bool(properties.Require_root) {
493 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
494 } else {
495 var options []tradefed.Option
496 options = append(options, tradefed.Option{Name: "force-root", Value: "false"})
497 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
498 }
499 if Bool(properties.Disable_framework) {
500 var options []tradefed.Option
501 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.StopServicesSetup", options})
502 }
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000503 }
504 if isolated {
505 configs = append(configs, tradefed.Option{Name: "not-shardable", Value: "true"})
506 }
507 if properties.Test_options.Run_test_as != nil {
508 configs = append(configs, tradefed.Option{Name: "run-test-as", Value: String(properties.Test_options.Run_test_as)})
509 }
510 for _, tag := range properties.Test_options.Test_suite_tag {
511 configs = append(configs, tradefed.Option{Name: "test-suite-tag", Value: tag})
512 }
513 if properties.Test_options.Min_shipping_api_level != nil {
514 if properties.Test_options.Vsr_min_shipping_api_level != nil {
515 ctx.PropertyErrorf("test_options.min_shipping_api_level", "must not be set at the same time as 'vsr_min_shipping_api_level'.")
516 }
517 var options []tradefed.Option
518 options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_shipping_api_level), 10)})
519 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
520 }
521 if properties.Test_options.Vsr_min_shipping_api_level != nil {
522 var options []tradefed.Option
523 options = append(options, tradefed.Option{Name: "vsr-min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Vsr_min_shipping_api_level), 10)})
524 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
525 }
526 if properties.Test_options.Min_vndk_version != nil {
527 var options []tradefed.Option
528 options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_vndk_version), 10)})
529 options = append(options, tradefed.Option{Name: "api-level-prop", Value: "ro.vndk.version"})
530 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.MinApiLevelModuleController", options})
531 }
532 return configs
533}
534
Colin Cross8ff10582023-12-07 13:10:56 -0800535func NewTest(hod android.HostOrDeviceSupported) *Module {
536 module, binary := newBinary(hod)
Colin Crossb916a382016-07-29 17:28:03 -0700537 module.multilib = android.MultilibBoth
Aditya Choudhary4b6eaf42023-11-21 15:38:37 +0000538 module.testModule = true
Dan Willemsen1d577e22016-08-29 15:53:15 -0700539 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700540
541 test := &testBinary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000542 testDecorator: &testDecorator{
543 linker: binary.baseLinker,
544 installer: binary.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700545 },
Colin Crossb916a382016-07-29 17:28:03 -0700546 binaryDecorator: binary,
547 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700548 }
Colin Crossb916a382016-07-29 17:28:03 -0700549 module.compiler = test
550 module.linker = test
551 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700552 return module
553}
554
Colin Crossb916a382016-07-29 17:28:03 -0700555type testLibrary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000556 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700557 *libraryDecorator
558}
559
Jingwen Chen537242c2022-08-24 11:53:27 +0000560func (test *testLibrary) testLibrary() bool {
561 return true
562}
563
Colin Crossb916a382016-07-29 17:28:03 -0700564func (test *testLibrary) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000565 var props []interface{}
566 props = append(props, test.testDecorator.linkerProps()...)
567 return append(props, test.libraryDecorator.linkerProps()...)
Colin Crossb916a382016-07-29 17:28:03 -0700568}
569
Colin Cross37047f12016-12-13 17:06:13 -0800570func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700571 deps = test.testDecorator.linkerDeps(ctx, deps)
572 deps = test.libraryDecorator.linkerDeps(ctx, deps)
573 return deps
574}
575
576func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
577 flags = test.libraryDecorator.linkerFlags(ctx, flags)
578 flags = test.testDecorator.linkerFlags(ctx, flags)
579 return flags
580}
581
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800582func (test *testLibrary) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
583 if len(test.InstallerProperties.Test_suites) > 0 {
584 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, test.InstallerProperties.Test_suites...)
585 }
586
587 test.libraryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
588 test.testDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
589}
590
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000591func (test *testLibrary) installerProps() []interface{} {
592 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
593}
594
Colin Cross4d9c2d12016-07-29 12:48:20 -0700595func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800596 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700597 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700598 test := &testLibrary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000599 testDecorator: &testDecorator{
600 linker: library.baseLinker,
601 installer: library.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700602 },
Colin Crossb916a382016-07-29 17:28:03 -0700603 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700604 }
Colin Crossb916a382016-07-29 17:28:03 -0700605 module.linker = test
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000606 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700607 return module
608}
609
Colin Crosse28f4e22017-04-24 18:10:29 -0700610type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700611 // list of files or filegroup modules that provide data that should be installed alongside
612 // the test
Colin Cross27b922f2019-03-04 22:35:41 -0800613 Data []string `android:"path"`
Anders Lewisb97e8182017-07-14 15:20:13 -0700614
Colin Crosse28f4e22017-04-24 18:10:29 -0700615 // list of compatibility suites (for example "cts", "vts") that the module should be
616 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700617 Test_suites []string `android:"arch_variant"`
618
619 // the name of the test configuration (for example "AndroidTest.xml") that should be
620 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800621 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700622
623 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
624 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800625 Test_config_template *string `android:"path,arch_variant"`
Dan Shi37ee3b82019-06-06 16:23:32 -0700626
627 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
628 // with root permission.
629 Require_root *bool
Dan Shi6ffaaa82019-09-26 11:41:36 -0700630
631 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
632 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
633 // explicitly.
634 Auto_gen_config *bool
Colin Crosse28f4e22017-04-24 18:10:29 -0700635}
636
Colin Crossb916a382016-07-29 17:28:03 -0700637type benchmarkDecorator struct {
638 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700639 Properties BenchmarkProperties
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800640 data []android.DataPath
Colin Cross303e21f2018-08-07 16:49:25 -0700641 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700642}
643
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400644func (benchmark *benchmarkDecorator) benchmarkBinary() bool {
645 return true
646}
647
Colin Crosse28f4e22017-04-24 18:10:29 -0700648func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
649 props := benchmark.binaryDecorator.linkerProps()
650 props = append(props, &benchmark.Properties)
651 return props
652}
653
Colin Cross37047f12016-12-13 17:06:13 -0800654func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700655 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700656 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
657 return deps
658}
659
Colin Crossb916a382016-07-29 17:28:03 -0700660func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800661 for _, d := range android.PathsForModuleSrc(ctx, benchmark.Properties.Data) {
662 benchmark.data = append(benchmark.data, android.DataPath{SrcPath: d})
663 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400664
Dan Shi37ee3b82019-06-06 16:23:32 -0700665 var configs []tradefed.Config
666 if Bool(benchmark.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800667 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shi37ee3b82019-06-06 16:23:32 -0700668 }
Cole Faust21680542022-12-07 18:18:37 -0800669 benchmark.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
670 TestConfigProp: benchmark.Properties.Test_config,
671 TestConfigTemplateProp: benchmark.Properties.Test_config_template,
672 TestSuites: benchmark.Properties.Test_suites,
673 Config: configs,
674 AutoGenConfig: benchmark.Properties.Auto_gen_config,
675 DeviceTemplate: "${NativeBenchmarkTestConfigTemplate}",
676 HostTemplate: "${NativeBenchmarkTestConfigTemplate}",
677 })
Colin Cross303e21f2018-08-07 16:49:25 -0700678
Colin Cross28690e92017-09-08 16:20:30 -0700679 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
680 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800681 benchmark.binaryDecorator.baseInstaller.installTestData(ctx, benchmark.data)
Dan Willemsen1d577e22016-08-29 15:53:15 -0700682 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700683}
684
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800685func (benchmark *benchmarkDecorator) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
686 benchmark.binaryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
687
688 moduleInfoJSON.Class = []string{"NATIVE_TESTS"}
689 if len(benchmark.Properties.Test_suites) > 0 {
690 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, benchmark.Properties.Test_suites...)
691 } else {
692 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "null-suite")
693 }
694
695 if android.PrefixInList(moduleInfoJSON.CompatibilitySuites, "mts-") &&
696 !android.InList("mts", moduleInfoJSON.CompatibilitySuites) {
697 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "mts")
698 }
699
700 if benchmark.testConfig != nil {
701 if _, ok := benchmark.testConfig.(android.WritablePath); ok {
702 moduleInfoJSON.AutoTestConfig = []string{"true"}
703 }
704 moduleInfoJSON.TestConfig = []string{benchmark.testConfig.String()}
705 }
706}
707
Colin Cross4d9c2d12016-07-29 12:48:20 -0700708func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800709 module, binary := newBinary(hod)
Colin Crossb916a382016-07-29 17:28:03 -0700710 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700711 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700712
713 benchmark := &benchmarkDecorator{
714 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700715 }
Colin Crossb916a382016-07-29 17:28:03 -0700716 module.linker = benchmark
717 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700718 return module
719}