blob: d8b7833baccc29ccf496a1de44a2bad7447f1112 [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
Colin Crossb916a382016-07-29 17:28:03 -070028type TestProperties 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
32 // if set, use the isolated gtest runner. Defaults to false.
33 Isolated *bool
Colin Crossb916a382016-07-29 17:28:03 -070034}
Colin Cross4d9c2d12016-07-29 12:48:20 -070035
yelinhsieh9fc60402018-10-01 19:23:14 +080036// Test option struct.
37type TestOptions struct {
38 // The UID that you want to run the test as on a device.
39 Run_test_as *string
Dan Shi95d19422020-08-15 12:24:26 -070040
David Srbecky519db272020-06-18 18:07:00 +010041 // A list of free-formed strings without spaces that categorize the test.
42 Test_suite_tag []string
Dan Shi95d19422020-08-15 12:24:26 -070043
44 // a list of extra test configuration files that should be installed with the module.
45 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -080046
47 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
48 Unit_test *bool
Justin Yun46f66052021-05-04 18:42:24 +090049
50 // Add ShippingApiLevelModuleController to auto generated test config. If the device properties
Justin Yuna364cfb2021-05-13 12:39:42 +090051 // for the shipping api level is less than the min_shipping_api_level, skip this module.
52 Min_shipping_api_level *int64
53
54 // Add ShippingApiLevelModuleController to auto generated test config. If any of the device
55 // shipping api level and vendor api level properties are less than the
56 // vsr_min_shipping_api_level, skip this module.
57 // As this includes the shipping api level check, it is not allowed to define
58 // min_shipping_api_level at the same time with this property.
59 Vsr_min_shipping_api_level *int64
Justin Yun46f66052021-05-04 18:42:24 +090060
61 // Add MinApiLevelModuleController with ro.vndk.version property. If ro.vndk.version has an
Justin Yuna364cfb2021-05-13 12:39:42 +090062 // integer value and the value is less than the min_vndk_version, skip this module.
63 Min_vndk_version *int64
yelinhsieh9fc60402018-10-01 19:23:14 +080064}
65
Colin Crossb916a382016-07-29 17:28:03 -070066type TestBinaryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070067 // Create a separate binary for each source file. Useful when there is
68 // global state that can not be torn down and reset between each test suite.
69 Test_per_src *bool
Dan Willemsen3340d602016-12-27 14:40:40 -080070
71 // Disables the creation of a test-specific directory when used with
72 // relative_install_path. Useful if several tests need to be in the same
73 // directory, but test_per_src doesn't work.
74 No_named_install_directory *bool
Colin Crossfaeb7aa2017-02-01 14:12:44 -080075
76 // list of files or filegroup modules that provide data that should be installed alongside
77 // the test
Dan Shi67a88342020-02-25 16:34:39 -080078 Data []string `android:"path,arch_variant"`
Colin Crossa929db02017-03-27 16:27:50 -070079
Chris Parsons79d66a52020-06-05 17:26:16 -040080 // list of shared library modules that should be installed alongside the test
81 Data_libs []string `android:"arch_variant"`
82
Colin Crossc8caa062021-09-24 16:50:14 -070083 // list of binary modules that should be installed alongside the test
84 Data_bins []string `android:"arch_variant"`
85
Colin Crossa929db02017-03-27 16:27:50 -070086 // list of compatibility suites (for example "cts", "vts") that the module should be
87 // installed into.
Dan Willemsen15d54d52017-09-18 16:49:28 -070088 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -070089
90 // the name of the test configuration (for example "AndroidTest.xml") that should be
91 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -080092 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -070093
94 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
95 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -080096 Test_config_template *string `android:"path,arch_variant"`
yelinhsieh9fc60402018-10-01 19:23:14 +080097
98 // Test options.
99 Test_options TestOptions
Dan Shi37ee3b82019-06-06 16:23:32 -0700100
101 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
102 // with root permission.
103 Require_root *bool
Dan Shi20ccd212019-08-27 10:37:24 -0700104
105 // Add RunCommandTargetPreparer to stop framework before the test and start it after the test.
106 Disable_framework *bool
nelsonli0d7111e2019-09-17 16:35:23 +0800107
Dan Shi6ffaaa82019-09-26 11:41:36 -0700108 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
109 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
110 // explicitly.
111 Auto_gen_config *bool
easoncylee1e3fdcd2020-04-30 10:08:33 +0800112
113 // Add parameterized mainline modules to auto generated test config. The options will be
114 // handled by TradeFed to download and install the specified modules on the device.
115 Test_mainline_modules []string
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700116
117 // Install the test into a folder named for the module in all test suites.
118 Per_testcase_directory *bool
Colin Cross4d9c2d12016-07-29 12:48:20 -0700119}
120
121func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700122 android.RegisterModuleType("cc_test", TestFactory)
123 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
124 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
125 android.RegisterModuleType("cc_test_host", TestHostFactory)
126 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700127}
128
Patrice Arrudac249c712019-03-19 17:00:29 -0700129// cc_test generates a test config file and an executable binary file to test
130// specific functionality on a device. The executable binary gets an implicit
131// static_libs dependency on libgtests unless the gtest flag is set to false.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700132func TestFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700133 module := NewTest(android.HostAndDeviceSupported)
134 return module.Init()
135}
136
Patrice Arrudac249c712019-03-19 17:00:29 -0700137// cc_test_library creates an archive of files (i.e. .o files) which is later
138// referenced by another module (such as cc_test, cc_defaults or cc_test_library)
139// for archiving or linking.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700140func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700141 module := NewTestLibrary(android.HostAndDeviceSupported)
142 return module.Init()
143}
144
Patrice Arrudac249c712019-03-19 17:00:29 -0700145// cc_benchmark compiles an executable binary that performs benchmark testing
146// of a specific component in a device. Additional files such as test suites
147// and test configuration are installed on the side of the compiled executed
148// binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700149func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700150 module := NewBenchmark(android.HostAndDeviceSupported)
151 return module.Init()
152}
153
Patrice Arrudac249c712019-03-19 17:00:29 -0700154// cc_test_host compiles a test host binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700155func TestHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700156 module := NewTest(android.HostSupported)
157 return module.Init()
158}
159
Patrice Arrudac249c712019-03-19 17:00:29 -0700160// cc_benchmark_host compiles an executable binary that performs benchmark
161// testing of a specific component in the host. Additional files such as
162// test suites and test configuration are installed on the side of the
163// compiled executed binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700164func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700165 module := NewBenchmark(android.HostSupported)
166 return module.Init()
167}
168
Colin Crossb916a382016-07-29 17:28:03 -0700169type testPerSrc interface {
170 testPerSrc() bool
171 srcs() []string
Roland Levillainf2fad972019-06-28 15:41:19 +0100172 isAllTestsVariation() bool
Colin Crossb916a382016-07-29 17:28:03 -0700173 setSrc(string, string)
Roland Levillainf2fad972019-06-28 15:41:19 +0100174 unsetSrc()
Colin Crossb916a382016-07-29 17:28:03 -0700175}
176
177func (test *testBinary) testPerSrc() bool {
178 return Bool(test.Properties.Test_per_src)
179}
180
181func (test *testBinary) srcs() []string {
182 return test.baseCompiler.Properties.Srcs
183}
184
Chris Parsons216e10a2020-07-09 17:12:52 -0400185func (test *testBinary) dataPaths() []android.DataPath {
Liz Kammer1c14a212020-05-12 15:26:55 -0700186 return test.data
187}
188
Roland Levillainf2fad972019-06-28 15:41:19 +0100189func (test *testBinary) isAllTestsVariation() bool {
190 stem := test.binaryDecorator.Properties.Stem
191 return stem != nil && *stem == ""
192}
193
Colin Crossb916a382016-07-29 17:28:03 -0700194func (test *testBinary) setSrc(name, src string) {
195 test.baseCompiler.Properties.Srcs = []string{src}
Nan Zhang0007d812017-11-07 10:57:05 -0800196 test.binaryDecorator.Properties.Stem = StringPtr(name)
Colin Crossb916a382016-07-29 17:28:03 -0700197}
198
Roland Levillainf2fad972019-06-28 15:41:19 +0100199func (test *testBinary) unsetSrc() {
200 test.baseCompiler.Properties.Srcs = nil
201 test.binaryDecorator.Properties.Stem = StringPtr("")
202}
203
Colin Crossb916a382016-07-29 17:28:03 -0700204var _ testPerSrc = (*testBinary)(nil)
205
Roland Levillain9b5fde92019-06-28 15:41:19 +0100206func TestPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700207 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700208 if test, ok := m.linker.(testPerSrc); ok {
Roland Levillainf2fad972019-06-28 15:41:19 +0100209 numTests := len(test.srcs())
210 if test.testPerSrc() && numTests > 0 {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700211 if duplicate, found := android.CheckDuplicate(test.srcs()); found {
Jooyung Hana61ff2c2019-02-28 18:06:34 +0900212 mctx.PropertyErrorf("srcs", "found a duplicate entry %q", duplicate)
213 return
214 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100215 testNames := make([]string, numTests)
Colin Crossb916a382016-07-29 17:28:03 -0700216 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
218 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100219 // In addition to creating one variation per test source file,
220 // create an additional "all tests" variation named "", and have it
221 // depends on all other test_per_src variations. This is useful to
222 // create subsequent dependencies of a given module on all
223 // test_per_src variations created above: by depending on
224 // variation "", that module will transitively depend on all the
225 // other test_per_src variations without the need to know their
226 // name or even their number.
227 testNames = append(testNames, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700228 tests := mctx.CreateLocalVariations(testNames...)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800229 allTests := tests[numTests]
230 allTests.(*Module).linker.(testPerSrc).unsetSrc()
Roland Levillainf2fad972019-06-28 15:41:19 +0100231 // Prevent the "all tests" variation from being installable nor
232 // exporting to Make, as it won't create any output file.
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800233 allTests.(*Module).Properties.PreventInstall = true
234 allTests.(*Module).Properties.HideFromMake = true
Colin Crossb916a382016-07-29 17:28:03 -0700235 for i, src := range test.srcs() {
236 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800237 mctx.AddInterVariantDependency(testPerSrcDepTag, allTests, tests[i])
Colin Cross4d9c2d12016-07-29 12:48:20 -0700238 }
Colin Cross90dab342020-08-21 15:55:50 -0700239 mctx.AliasVariation("")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700240 }
241 }
242 }
243}
244
Colin Crossb916a382016-07-29 17:28:03 -0700245type testDecorator struct {
246 Properties TestProperties
247 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700248}
249
Colin Cross600c9df2016-09-13 12:26:16 -0700250func (test *testDecorator) gtest() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700251 return BoolDefault(test.Properties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700252}
253
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700254func (test *testDecorator) testBinary() bool {
255 return true
256}
257
Colin Crossb916a382016-07-29 17:28:03 -0700258func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700259 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700260 return flags
261 }
262
Colin Cross4af21ed2019-11-04 09:37:55 -0800263 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700264 if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800265 flags.Local.CFlags = append(flags.Local.CFlags, "-O0", "-g")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700266
267 switch ctx.Os() {
268 case android.Windows:
Colin Cross4af21ed2019-11-04 09:37:55 -0800269 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700270 case android.Linux:
Colin Cross4af21ed2019-11-04 09:37:55 -0800271 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700272 case android.Darwin:
Colin Cross4af21ed2019-11-04 09:37:55 -0800273 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700274 }
275 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800276 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700277 }
278
279 return flags
280}
281
Colin Crossb916a382016-07-29 17:28:03 -0700282func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700283 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700284 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800285 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Christopher Ferris9df92d62018-08-21 12:40:08 -0700286 } else if BoolDefault(test.Properties.Isolated, false) {
287 deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main")
Christopher Ferris34cbba62019-07-17 15:46:29 -0700288 // The isolated library requires liblog, but adding it
289 // as a static library means unit tests cannot override
290 // liblog functions. Instead make it a shared library
291 // dependency.
292 deps.SharedLibs = append(deps.SharedLibs, "liblog")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700293 } else {
294 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
295 }
296 }
Colin Crossb916a382016-07-29 17:28:03 -0700297
Colin Cross4d9c2d12016-07-29 12:48:20 -0700298 return deps
299}
300
Colin Crossb916a382016-07-29 17:28:03 -0700301func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) {
yangbillb3174d12018-05-07 06:41:20 +0000302 // 1. Add ../../lib[64] to rpath so that out/host/linux-x86/nativetest/<test dir>/<test> can
Colin Crossbd75e1d2017-06-30 17:27:55 -0700303 // find out/host/linux-x86/lib[64]/library.so
yangbillb3174d12018-05-07 06:41:20 +0000304 // 2. Add ../../../lib[64] to rpath so that out/host/linux-x86/testcases/<test dir>/<CPU>/<test> can
305 // also find out/host/linux-x86/lib[64]/library.so
306 runpaths := []string{"../../lib", "../../../lib"}
307 for _, runpath := range runpaths {
308 if ctx.toolchain().Is64Bit() {
309 runpath += "64"
310 }
311 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700312 }
Colin Crossbd75e1d2017-06-30 17:27:55 -0700313
314 // add "" to rpath so that test binaries can find libraries in their own test directory
315 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700316}
317
Colin Crossb916a382016-07-29 17:28:03 -0700318func (test *testDecorator) linkerProps() []interface{} {
319 return []interface{}{&test.Properties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700320}
321
Colin Crossb916a382016-07-29 17:28:03 -0700322func NewTestInstaller() *baseInstaller {
323 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700324}
325
Colin Crossb916a382016-07-29 17:28:03 -0700326type testBinary struct {
327 testDecorator
328 *binaryDecorator
329 *baseCompiler
Dan Shi95d19422020-08-15 12:24:26 -0700330 Properties TestBinaryProperties
331 data []android.DataPath
332 testConfig android.Path
333 extraTestConfigs android.Paths
Colin Crossb916a382016-07-29 17:28:03 -0700334}
335
336func (test *testBinary) linkerProps() []interface{} {
337 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
338 props = append(props, &test.Properties)
339 return props
340}
341
342func (test *testBinary) linkerInit(ctx BaseModuleContext) {
343 test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker)
344 test.binaryDecorator.linkerInit(ctx)
345}
346
Colin Cross37047f12016-12-13 17:06:13 -0800347func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700348 deps = test.testDecorator.linkerDeps(ctx, deps)
349 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Chris Parsons79d66a52020-06-05 17:26:16 -0400350 deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...)
Colin Crossc8caa062021-09-24 16:50:14 -0700351 deps.DataBins = append(deps.DataBins, test.Properties.Data_bins...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700352 return deps
353}
354
Colin Crossb916a382016-07-29 17:28:03 -0700355func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
356 flags = test.binaryDecorator.linkerFlags(ctx, flags)
357 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700358 return flags
359}
360
Colin Crossb916a382016-07-29 17:28:03 -0700361func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Guang Zhu5c97bc62021-09-22 04:19:19 +0000362 // TODO: (b/167308193) Switch to /data/local/tests/unrestricted as the default install base.
363 testInstallBase := "/data/local/tmp"
yangbill5ec45552020-08-13 16:16:56 +0800364 if ctx.inVendor() || ctx.useVndk() {
365 testInstallBase = "/data/local/tests/vendor"
366 }
367
Chris Parsons216e10a2020-07-09 17:12:52 -0400368 dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
369
370 for _, dataSrcPath := range dataSrcPaths {
371 test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
372 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400373
374 ctx.VisitDirectDepsWithTag(dataLibDepTag, func(dep android.Module) {
375 depName := ctx.OtherModuleName(dep)
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400376 linkableDep, ok := dep.(LinkableInterface)
Chris Parsons79d66a52020-06-05 17:26:16 -0400377 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400378 ctx.ModuleErrorf("data_lib %q is not a LinkableInterface module", depName)
Chris Parsons79d66a52020-06-05 17:26:16 -0400379 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400380 if linkableDep.OutputFile().Valid() {
Chris Parsons216e10a2020-07-09 17:12:52 -0400381 test.data = append(test.data,
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400382 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
383 RelativeInstallPath: linkableDep.RelativeInstallPath()})
Chris Parsons79d66a52020-06-05 17:26:16 -0400384 }
385 })
Colin Crossc8caa062021-09-24 16:50:14 -0700386 ctx.VisitDirectDepsWithTag(dataBinDepTag, func(dep android.Module) {
387 depName := ctx.OtherModuleName(dep)
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400388 linkableDep, ok := dep.(LinkableInterface)
Colin Crossc8caa062021-09-24 16:50:14 -0700389 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400390 ctx.ModuleErrorf("data_bin %q is not a LinkableInterface module", depName)
Colin Crossc8caa062021-09-24 16:50:14 -0700391 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400392 if linkableDep.OutputFile().Valid() {
Colin Crossc8caa062021-09-24 16:50:14 -0700393 test.data = append(test.data,
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400394 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
395 RelativeInstallPath: linkableDep.RelativeInstallPath()})
Colin Crossc8caa062021-09-24 16:50:14 -0700396 }
397 })
Chris Parsons79d66a52020-06-05 17:26:16 -0400398
Dan Shi37ee3b82019-06-06 16:23:32 -0700399 var configs []tradefed.Config
easoncylee1e3fdcd2020-04-30 10:08:33 +0800400 for _, module := range test.Properties.Test_mainline_modules {
401 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module})
402 }
Dan Shi37ee3b82019-06-06 16:23:32 -0700403 if Bool(test.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800404 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shibc0f2602019-09-17 02:43:50 +0000405 } else {
406 var options []tradefed.Option
easoncylee1e3fdcd2020-04-30 10:08:33 +0800407 options = append(options, tradefed.Option{Name: "force-root", Value: "false"})
nelsonli0d7111e2019-09-17 16:35:23 +0800408 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
Dan Shi20ccd212019-08-27 10:37:24 -0700409 }
410 if Bool(test.Properties.Disable_framework) {
411 var options []tradefed.Option
Dan Shi8aa40102020-05-12 13:50:12 -0700412 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.StopServicesSetup", options})
Julien Desprezeb7398e2019-02-28 08:45:28 -0800413 }
Dan Shi37ee3b82019-06-06 16:23:32 -0700414 if Bool(test.testDecorator.Properties.Isolated) {
easoncylee1e3fdcd2020-04-30 10:08:33 +0800415 configs = append(configs, tradefed.Option{Name: "not-shardable", Value: "true"})
Dan Shi37ee3b82019-06-06 16:23:32 -0700416 }
yelinhsieh9fc60402018-10-01 19:23:14 +0800417 if test.Properties.Test_options.Run_test_as != nil {
easoncylee1e3fdcd2020-04-30 10:08:33 +0800418 configs = append(configs, tradefed.Option{Name: "run-test-as", Value: String(test.Properties.Test_options.Run_test_as)})
yelinhsieh9fc60402018-10-01 19:23:14 +0800419 }
David Srbecky519db272020-06-18 18:07:00 +0100420 for _, tag := range test.Properties.Test_options.Test_suite_tag {
421 configs = append(configs, tradefed.Option{Name: "test-suite-tag", Value: tag})
422 }
Justin Yuna364cfb2021-05-13 12:39:42 +0900423 if test.Properties.Test_options.Min_shipping_api_level != nil {
424 if test.Properties.Test_options.Vsr_min_shipping_api_level != nil {
425 ctx.PropertyErrorf("test_options.min_shipping_api_level", "must not be set at the same time as 'vsr_min_shipping_api_level'.")
426 }
Justin Yun46f66052021-05-04 18:42:24 +0900427 var options []tradefed.Option
Justin Yuna364cfb2021-05-13 12:39:42 +0900428 options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*test.Properties.Test_options.Min_shipping_api_level), 10)})
Justin Yun46f66052021-05-04 18:42:24 +0900429 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
nelsonli0d7111e2019-09-17 16:35:23 +0800430 }
Justin Yuna364cfb2021-05-13 12:39:42 +0900431 if test.Properties.Test_options.Vsr_min_shipping_api_level != nil {
Justin Yun46f66052021-05-04 18:42:24 +0900432 var options []tradefed.Option
Justin Yuna364cfb2021-05-13 12:39:42 +0900433 options = append(options, tradefed.Option{Name: "vsr-min-api-level", Value: strconv.FormatInt(int64(*test.Properties.Test_options.Vsr_min_shipping_api_level), 10)})
434 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
435 }
436 if test.Properties.Test_options.Min_vndk_version != nil {
437 var options []tradefed.Option
438 options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*test.Properties.Test_options.Min_vndk_version), 10)})
Justin Yun46f66052021-05-04 18:42:24 +0900439 options = append(options, tradefed.Option{Name: "api-level-prop", Value: "ro.vndk.version"})
440 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.MinApiLevelModuleController", options})
441 }
yelinhsieh9fc60402018-10-01 19:23:14 +0800442
Jack He33338892018-09-19 02:21:28 -0700443 test.testConfig = tradefed.AutoGenNativeTestConfig(ctx, test.Properties.Test_config,
yangbill5ec45552020-08-13 16:16:56 +0800444 test.Properties.Test_config_template, test.Properties.Test_suites, configs, test.Properties.Auto_gen_config, testInstallBase)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800445
Dan Shi95d19422020-08-15 12:24:26 -0700446 test.extraTestConfigs = android.PathsForModuleSrc(ctx, test.Properties.Test_options.Extra_test_configs)
447
Colin Cross600c9df2016-09-13 12:26:16 -0700448 test.binaryDecorator.baseInstaller.dir = "nativetest"
449 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800450
451 if !Bool(test.Properties.No_named_install_directory) {
452 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800453 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800454 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
455 }
456
Julien Desprez8908b372021-02-04 10:10:16 -0800457 if ctx.Host() && test.gtest() && test.Properties.Test_options.Unit_test == nil {
Julien Desprez3b933d32021-01-14 11:49:15 -0800458 test.Properties.Test_options.Unit_test = proptools.BoolPtr(true)
459 }
Dan Willemsen1d577e22016-08-29 15:53:15 -0700460 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700461}
462
463func NewTest(hod android.HostOrDeviceSupported) *Module {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400464 module, binary := newBinary(hod, false)
Colin Crossb916a382016-07-29 17:28:03 -0700465 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700466 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700467
468 test := &testBinary{
469 testDecorator: testDecorator{
470 linker: binary.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700471 },
Colin Crossb916a382016-07-29 17:28:03 -0700472 binaryDecorator: binary,
473 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700474 }
Colin Crossb916a382016-07-29 17:28:03 -0700475 module.compiler = test
476 module.linker = test
477 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700478 return module
479}
480
Colin Crossb916a382016-07-29 17:28:03 -0700481type testLibrary struct {
482 testDecorator
483 *libraryDecorator
484}
485
486func (test *testLibrary) linkerProps() []interface{} {
487 return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...)
488}
489
490func (test *testLibrary) linkerInit(ctx BaseModuleContext) {
491 test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker)
492 test.libraryDecorator.linkerInit(ctx)
493}
494
Colin Cross37047f12016-12-13 17:06:13 -0800495func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700496 deps = test.testDecorator.linkerDeps(ctx, deps)
497 deps = test.libraryDecorator.linkerDeps(ctx, deps)
498 return deps
499}
500
501func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
502 flags = test.libraryDecorator.linkerFlags(ctx, flags)
503 flags = test.testDecorator.linkerFlags(ctx, flags)
504 return flags
505}
506
Colin Cross4d9c2d12016-07-29 12:48:20 -0700507func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800508 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700509 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700510 test := &testLibrary{
511 testDecorator: testDecorator{
512 linker: library.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700513 },
Colin Crossb916a382016-07-29 17:28:03 -0700514 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700515 }
Colin Crossb916a382016-07-29 17:28:03 -0700516 module.linker = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700517 return module
518}
519
Colin Crosse28f4e22017-04-24 18:10:29 -0700520type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700521 // list of files or filegroup modules that provide data that should be installed alongside
522 // the test
Colin Cross27b922f2019-03-04 22:35:41 -0800523 Data []string `android:"path"`
Anders Lewisb97e8182017-07-14 15:20:13 -0700524
Colin Crosse28f4e22017-04-24 18:10:29 -0700525 // list of compatibility suites (for example "cts", "vts") that the module should be
526 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700527 Test_suites []string `android:"arch_variant"`
528
529 // the name of the test configuration (for example "AndroidTest.xml") that should be
530 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800531 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700532
533 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
534 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800535 Test_config_template *string `android:"path,arch_variant"`
Dan Shi37ee3b82019-06-06 16:23:32 -0700536
537 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
538 // with root permission.
539 Require_root *bool
Dan Shi6ffaaa82019-09-26 11:41:36 -0700540
541 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
542 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
543 // explicitly.
544 Auto_gen_config *bool
Colin Crosse28f4e22017-04-24 18:10:29 -0700545}
546
Colin Crossb916a382016-07-29 17:28:03 -0700547type benchmarkDecorator struct {
548 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700549 Properties BenchmarkProperties
Anders Lewisb97e8182017-07-14 15:20:13 -0700550 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700551 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700552}
553
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400554func (benchmark *benchmarkDecorator) benchmarkBinary() bool {
555 return true
556}
557
Colin Crossb916a382016-07-29 17:28:03 -0700558func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) {
559 runpath := "../../lib"
560 if ctx.toolchain().Is64Bit() {
561 runpath += "64"
562 }
563 benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath)
564 benchmark.binaryDecorator.linkerInit(ctx)
565}
566
Colin Crosse28f4e22017-04-24 18:10:29 -0700567func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
568 props := benchmark.binaryDecorator.linkerProps()
569 props = append(props, &benchmark.Properties)
570 return props
571}
572
Colin Cross37047f12016-12-13 17:06:13 -0800573func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700574 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700575 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
576 return deps
577}
578
Colin Crossb916a382016-07-29 17:28:03 -0700579func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Colin Cross8a497952019-03-05 22:25:09 -0800580 benchmark.data = android.PathsForModuleSrc(ctx, benchmark.Properties.Data)
Chris Parsons79d66a52020-06-05 17:26:16 -0400581
Dan Shi37ee3b82019-06-06 16:23:32 -0700582 var configs []tradefed.Config
583 if Bool(benchmark.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800584 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shi37ee3b82019-06-06 16:23:32 -0700585 }
Jack He33338892018-09-19 02:21:28 -0700586 benchmark.testConfig = tradefed.AutoGenNativeBenchmarkTestConfig(ctx, benchmark.Properties.Test_config,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700587 benchmark.Properties.Test_config_template, benchmark.Properties.Test_suites, configs, benchmark.Properties.Auto_gen_config)
Colin Cross303e21f2018-08-07 16:49:25 -0700588
Colin Cross28690e92017-09-08 16:20:30 -0700589 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
590 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Dan Willemsen1d577e22016-08-29 15:53:15 -0700591 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700592}
593
Colin Cross4d9c2d12016-07-29 12:48:20 -0700594func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400595 module, binary := newBinary(hod, false)
Colin Crossb916a382016-07-29 17:28:03 -0700596 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700597 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700598
599 benchmark := &benchmarkDecorator{
600 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700601 }
Colin Crossb916a382016-07-29 17:28:03 -0700602 module.linker = benchmark
603 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700604 return module
605}