blob: f5abc454d257c88cc6734033e90bf96c1f0d925c [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
yelinhsieh9fc60402018-10-01 19:23:14 +080071}
72
Colin Crossb916a382016-07-29 17:28:03 -070073type TestBinaryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070074 // Create a separate binary for each source file. Useful when there is
75 // global state that can not be torn down and reset between each test suite.
76 Test_per_src *bool
Dan Willemsen3340d602016-12-27 14:40:40 -080077
78 // Disables the creation of a test-specific directory when used with
79 // relative_install_path. Useful if several tests need to be in the same
80 // directory, but test_per_src doesn't work.
81 No_named_install_directory *bool
Colin Crossfaeb7aa2017-02-01 14:12:44 -080082
83 // list of files or filegroup modules that provide data that should be installed alongside
84 // the test
Dan Shi67a88342020-02-25 16:34:39 -080085 Data []string `android:"path,arch_variant"`
Colin Crossa929db02017-03-27 16:27:50 -070086
Chris Parsons79d66a52020-06-05 17:26:16 -040087 // list of shared library modules that should be installed alongside the test
88 Data_libs []string `android:"arch_variant"`
89
Colin Crossc8caa062021-09-24 16:50:14 -070090 // list of binary modules that should be installed alongside the test
91 Data_bins []string `android:"arch_variant"`
92
Julien Despreze146e392018-08-02 15:00:46 -070093 // the name of the test configuration (for example "AndroidTest.xml") that should be
94 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -080095 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -070096
97 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
98 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -080099 Test_config_template *string `android:"path,arch_variant"`
yelinhsieh9fc60402018-10-01 19:23:14 +0800100
101 // Test options.
102 Test_options TestOptions
Dan Shi37ee3b82019-06-06 16:23:32 -0700103
104 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
105 // with root permission.
106 Require_root *bool
Dan Shi20ccd212019-08-27 10:37:24 -0700107
108 // Add RunCommandTargetPreparer to stop framework before the test and start it after the test.
109 Disable_framework *bool
nelsonli0d7111e2019-09-17 16:35:23 +0800110
Dan Shi6ffaaa82019-09-26 11:41:36 -0700111 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
112 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
113 // explicitly.
114 Auto_gen_config *bool
easoncylee1e3fdcd2020-04-30 10:08:33 +0800115
116 // Add parameterized mainline modules to auto generated test config. The options will be
117 // handled by TradeFed to download and install the specified modules on the device.
118 Test_mainline_modules []string
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700119
120 // Install the test into a folder named for the module in all test suites.
121 Per_testcase_directory *bool
Colin Cross4d9c2d12016-07-29 12:48:20 -0700122}
123
124func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700125 android.RegisterModuleType("cc_test", TestFactory)
126 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
127 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
128 android.RegisterModuleType("cc_test_host", TestHostFactory)
129 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700130}
131
Patrice Arrudac249c712019-03-19 17:00:29 -0700132// cc_test generates a test config file and an executable binary file to test
133// specific functionality on a device. The executable binary gets an implicit
134// static_libs dependency on libgtests unless the gtest flag is set to false.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700135func TestFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700136 module := NewTest(android.HostAndDeviceSupported)
137 return module.Init()
138}
139
Patrice Arrudac249c712019-03-19 17:00:29 -0700140// cc_test_library creates an archive of files (i.e. .o files) which is later
141// referenced by another module (such as cc_test, cc_defaults or cc_test_library)
142// for archiving or linking.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700143func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700144 module := NewTestLibrary(android.HostAndDeviceSupported)
145 return module.Init()
146}
147
Patrice Arrudac249c712019-03-19 17:00:29 -0700148// cc_benchmark compiles an executable binary that performs benchmark testing
149// of a specific component in a device. Additional files such as test suites
150// and test configuration are installed on the side of the compiled executed
151// binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700152func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700153 module := NewBenchmark(android.HostAndDeviceSupported)
154 return module.Init()
155}
156
Patrice Arrudac249c712019-03-19 17:00:29 -0700157// cc_test_host compiles a test host binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700158func TestHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159 module := NewTest(android.HostSupported)
160 return module.Init()
161}
162
Patrice Arrudac249c712019-03-19 17:00:29 -0700163// cc_benchmark_host compiles an executable binary that performs benchmark
164// testing of a specific component in the host. Additional files such as
165// test suites and test configuration are installed on the side of the
166// compiled executed binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700167func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700168 module := NewBenchmark(android.HostSupported)
169 return module.Init()
170}
171
Colin Crossb916a382016-07-29 17:28:03 -0700172type testPerSrc interface {
173 testPerSrc() bool
174 srcs() []string
Roland Levillainf2fad972019-06-28 15:41:19 +0100175 isAllTestsVariation() bool
Colin Crossb916a382016-07-29 17:28:03 -0700176 setSrc(string, string)
Roland Levillainf2fad972019-06-28 15:41:19 +0100177 unsetSrc()
Colin Crossb916a382016-07-29 17:28:03 -0700178}
179
180func (test *testBinary) testPerSrc() bool {
181 return Bool(test.Properties.Test_per_src)
182}
183
184func (test *testBinary) srcs() []string {
185 return test.baseCompiler.Properties.Srcs
186}
187
Chris Parsons216e10a2020-07-09 17:12:52 -0400188func (test *testBinary) dataPaths() []android.DataPath {
Liz Kammer1c14a212020-05-12 15:26:55 -0700189 return test.data
190}
191
Roland Levillainf2fad972019-06-28 15:41:19 +0100192func (test *testBinary) isAllTestsVariation() bool {
193 stem := test.binaryDecorator.Properties.Stem
194 return stem != nil && *stem == ""
195}
196
Colin Crossb916a382016-07-29 17:28:03 -0700197func (test *testBinary) setSrc(name, src string) {
198 test.baseCompiler.Properties.Srcs = []string{src}
Nan Zhang0007d812017-11-07 10:57:05 -0800199 test.binaryDecorator.Properties.Stem = StringPtr(name)
Colin Crossb916a382016-07-29 17:28:03 -0700200}
201
Roland Levillainf2fad972019-06-28 15:41:19 +0100202func (test *testBinary) unsetSrc() {
203 test.baseCompiler.Properties.Srcs = nil
204 test.binaryDecorator.Properties.Stem = StringPtr("")
205}
206
Colin Crossb916a382016-07-29 17:28:03 -0700207var _ testPerSrc = (*testBinary)(nil)
208
Roland Levillain9b5fde92019-06-28 15:41:19 +0100209func TestPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700210 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700211 if test, ok := m.linker.(testPerSrc); ok {
Roland Levillainf2fad972019-06-28 15:41:19 +0100212 numTests := len(test.srcs())
213 if test.testPerSrc() && numTests > 0 {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700214 if duplicate, found := android.CheckDuplicate(test.srcs()); found {
Jooyung Hana61ff2c2019-02-28 18:06:34 +0900215 mctx.PropertyErrorf("srcs", "found a duplicate entry %q", duplicate)
216 return
217 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100218 testNames := make([]string, numTests)
Colin Crossb916a382016-07-29 17:28:03 -0700219 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
221 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100222 // In addition to creating one variation per test source file,
223 // create an additional "all tests" variation named "", and have it
224 // depends on all other test_per_src variations. This is useful to
225 // create subsequent dependencies of a given module on all
226 // test_per_src variations created above: by depending on
227 // variation "", that module will transitively depend on all the
228 // other test_per_src variations without the need to know their
229 // name or even their number.
230 testNames = append(testNames, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700231 tests := mctx.CreateLocalVariations(testNames...)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800232 allTests := tests[numTests]
233 allTests.(*Module).linker.(testPerSrc).unsetSrc()
Roland Levillainf2fad972019-06-28 15:41:19 +0100234 // Prevent the "all tests" variation from being installable nor
235 // exporting to Make, as it won't create any output file.
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800236 allTests.(*Module).Properties.PreventInstall = true
237 allTests.(*Module).Properties.HideFromMake = true
Colin Crossb916a382016-07-29 17:28:03 -0700238 for i, src := range test.srcs() {
239 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800240 mctx.AddInterVariantDependency(testPerSrcDepTag, allTests, tests[i])
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241 }
Colin Cross90dab342020-08-21 15:55:50 -0700242 mctx.AliasVariation("")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700243 }
244 }
245 }
246}
247
Colin Crossb916a382016-07-29 17:28:03 -0700248type testDecorator struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000249 LinkerProperties TestLinkerProperties
250 InstallerProperties TestInstallerProperties
251 installer *baseInstaller
252 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253}
254
Colin Cross600c9df2016-09-13 12:26:16 -0700255func (test *testDecorator) gtest() bool {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000256 return BoolDefault(test.LinkerProperties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700257}
258
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000259func (test *testDecorator) isolated(ctx BaseModuleContext) bool {
260 if !ctx.Windows() {
Md Shahriar Hossain Sajibd8b58662022-05-20 06:58:48 +0000261 return BoolDefault(test.LinkerProperties.Isolated, false)
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000262 }
263 return BoolDefault(test.LinkerProperties.Isolated, false)
264}
265
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700266func (test *testDecorator) testBinary() bool {
267 return true
268}
269
Colin Crossb916a382016-07-29 17:28:03 -0700270func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700271 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700272 return flags
273 }
274
Colin Cross4af21ed2019-11-04 09:37:55 -0800275 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700276 if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800277 flags.Local.CFlags = append(flags.Local.CFlags, "-O0", "-g")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700278
279 switch ctx.Os() {
280 case android.Windows:
Colin Cross4af21ed2019-11-04 09:37:55 -0800281 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700282 case android.Linux:
Colin Cross4af21ed2019-11-04 09:37:55 -0800283 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700284 case android.Darwin:
Colin Cross4af21ed2019-11-04 09:37:55 -0800285 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700286 }
287 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800288 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700289 }
290
291 return flags
292}
293
Colin Crossb916a382016-07-29 17:28:03 -0700294func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700295 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700296 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800297 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000298 } else if test.isolated(ctx) {
Christopher Ferris9df92d62018-08-21 12:40:08 -0700299 deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main")
Christopher Ferris34cbba62019-07-17 15:46:29 -0700300 // The isolated library requires liblog, but adding it
301 // as a static library means unit tests cannot override
302 // liblog functions. Instead make it a shared library
303 // dependency.
304 deps.SharedLibs = append(deps.SharedLibs, "liblog")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700305 } else {
306 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
307 }
308 }
Colin Crossb916a382016-07-29 17:28:03 -0700309
Colin Cross4d9c2d12016-07-29 12:48:20 -0700310 return deps
311}
312
Colin Crossb916a382016-07-29 17:28:03 -0700313func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) {
yangbillb3174d12018-05-07 06:41:20 +0000314 // 1. Add ../../lib[64] to rpath so that out/host/linux-x86/nativetest/<test dir>/<test> can
Colin Crossbd75e1d2017-06-30 17:27:55 -0700315 // find out/host/linux-x86/lib[64]/library.so
yangbillb3174d12018-05-07 06:41:20 +0000316 // 2. Add ../../../lib[64] to rpath so that out/host/linux-x86/testcases/<test dir>/<CPU>/<test> can
317 // also find out/host/linux-x86/lib[64]/library.so
318 runpaths := []string{"../../lib", "../../../lib"}
319 for _, runpath := range runpaths {
320 if ctx.toolchain().Is64Bit() {
321 runpath += "64"
322 }
323 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700324 }
Colin Crossbd75e1d2017-06-30 17:27:55 -0700325
326 // add "" to rpath so that test binaries can find libraries in their own test directory
327 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700328}
329
Colin Crossb916a382016-07-29 17:28:03 -0700330func (test *testDecorator) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000331 return []interface{}{&test.LinkerProperties}
332}
333
334func (test *testDecorator) installerProps() []interface{} {
335 return []interface{}{&test.InstallerProperties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700336}
337
Colin Crossb916a382016-07-29 17:28:03 -0700338func NewTestInstaller() *baseInstaller {
339 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700340}
341
Colin Crossb916a382016-07-29 17:28:03 -0700342type testBinary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000343 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700344 *binaryDecorator
345 *baseCompiler
Dan Shi95d19422020-08-15 12:24:26 -0700346 Properties TestBinaryProperties
347 data []android.DataPath
348 testConfig android.Path
349 extraTestConfigs android.Paths
Colin Crossb916a382016-07-29 17:28:03 -0700350}
351
352func (test *testBinary) linkerProps() []interface{} {
353 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
354 props = append(props, &test.Properties)
355 return props
356}
357
358func (test *testBinary) linkerInit(ctx BaseModuleContext) {
359 test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker)
360 test.binaryDecorator.linkerInit(ctx)
361}
362
Colin Cross37047f12016-12-13 17:06:13 -0800363func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700364 deps = test.testDecorator.linkerDeps(ctx, deps)
365 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Chris Parsons79d66a52020-06-05 17:26:16 -0400366 deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...)
Colin Crossc8caa062021-09-24 16:50:14 -0700367 deps.DataBins = append(deps.DataBins, test.Properties.Data_bins...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700368 return deps
369}
370
Colin Crossb916a382016-07-29 17:28:03 -0700371func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
372 flags = test.binaryDecorator.linkerFlags(ctx, flags)
373 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700374 return flags
375}
376
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000377func (test *testBinary) installerProps() []interface{} {
378 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
379}
380
Colin Crossb916a382016-07-29 17:28:03 -0700381func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Guang Zhu5c97bc62021-09-22 04:19:19 +0000382 // TODO: (b/167308193) Switch to /data/local/tests/unrestricted as the default install base.
383 testInstallBase := "/data/local/tmp"
yangbill5ec45552020-08-13 16:16:56 +0800384 if ctx.inVendor() || ctx.useVndk() {
385 testInstallBase = "/data/local/tests/vendor"
386 }
387
Chris Parsons216e10a2020-07-09 17:12:52 -0400388 dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
389
390 for _, dataSrcPath := range dataSrcPaths {
391 test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
392 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400393
394 ctx.VisitDirectDepsWithTag(dataLibDepTag, func(dep android.Module) {
395 depName := ctx.OtherModuleName(dep)
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400396 linkableDep, ok := dep.(LinkableInterface)
Chris Parsons79d66a52020-06-05 17:26:16 -0400397 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400398 ctx.ModuleErrorf("data_lib %q is not a LinkableInterface module", depName)
Chris Parsons79d66a52020-06-05 17:26:16 -0400399 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400400 if linkableDep.OutputFile().Valid() {
Chris Parsons216e10a2020-07-09 17:12:52 -0400401 test.data = append(test.data,
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400402 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
403 RelativeInstallPath: linkableDep.RelativeInstallPath()})
Chris Parsons79d66a52020-06-05 17:26:16 -0400404 }
405 })
Colin Crossc8caa062021-09-24 16:50:14 -0700406 ctx.VisitDirectDepsWithTag(dataBinDepTag, func(dep android.Module) {
407 depName := ctx.OtherModuleName(dep)
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400408 linkableDep, ok := dep.(LinkableInterface)
Colin Crossc8caa062021-09-24 16:50:14 -0700409 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400410 ctx.ModuleErrorf("data_bin %q is not a LinkableInterface module", depName)
Colin Crossc8caa062021-09-24 16:50:14 -0700411 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400412 if linkableDep.OutputFile().Valid() {
Colin Crossc8caa062021-09-24 16:50:14 -0700413 test.data = append(test.data,
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400414 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
415 RelativeInstallPath: linkableDep.RelativeInstallPath()})
Colin Crossc8caa062021-09-24 16:50:14 -0700416 }
417 })
Chris Parsons79d66a52020-06-05 17:26:16 -0400418
Dan Shi37ee3b82019-06-06 16:23:32 -0700419 var configs []tradefed.Config
easoncylee1e3fdcd2020-04-30 10:08:33 +0800420 for _, module := range test.Properties.Test_mainline_modules {
421 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module})
422 }
Dan Shi37ee3b82019-06-06 16:23:32 -0700423 if Bool(test.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800424 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shibc0f2602019-09-17 02:43:50 +0000425 } else {
426 var options []tradefed.Option
easoncylee1e3fdcd2020-04-30 10:08:33 +0800427 options = append(options, tradefed.Option{Name: "force-root", Value: "false"})
nelsonli0d7111e2019-09-17 16:35:23 +0800428 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
Dan Shi20ccd212019-08-27 10:37:24 -0700429 }
430 if Bool(test.Properties.Disable_framework) {
431 var options []tradefed.Option
Dan Shi8aa40102020-05-12 13:50:12 -0700432 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.StopServicesSetup", options})
Julien Desprezeb7398e2019-02-28 08:45:28 -0800433 }
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000434 if test.isolated(ctx) {
easoncylee1e3fdcd2020-04-30 10:08:33 +0800435 configs = append(configs, tradefed.Option{Name: "not-shardable", Value: "true"})
Dan Shi37ee3b82019-06-06 16:23:32 -0700436 }
yelinhsieh9fc60402018-10-01 19:23:14 +0800437 if test.Properties.Test_options.Run_test_as != nil {
easoncylee1e3fdcd2020-04-30 10:08:33 +0800438 configs = append(configs, tradefed.Option{Name: "run-test-as", Value: String(test.Properties.Test_options.Run_test_as)})
yelinhsieh9fc60402018-10-01 19:23:14 +0800439 }
David Srbecky519db272020-06-18 18:07:00 +0100440 for _, tag := range test.Properties.Test_options.Test_suite_tag {
441 configs = append(configs, tradefed.Option{Name: "test-suite-tag", Value: tag})
442 }
Justin Yuna364cfb2021-05-13 12:39:42 +0900443 if test.Properties.Test_options.Min_shipping_api_level != nil {
444 if test.Properties.Test_options.Vsr_min_shipping_api_level != nil {
445 ctx.PropertyErrorf("test_options.min_shipping_api_level", "must not be set at the same time as 'vsr_min_shipping_api_level'.")
446 }
Justin Yun46f66052021-05-04 18:42:24 +0900447 var options []tradefed.Option
Justin Yuna364cfb2021-05-13 12:39:42 +0900448 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 +0900449 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
nelsonli0d7111e2019-09-17 16:35:23 +0800450 }
Justin Yuna364cfb2021-05-13 12:39:42 +0900451 if test.Properties.Test_options.Vsr_min_shipping_api_level != nil {
Justin Yun46f66052021-05-04 18:42:24 +0900452 var options []tradefed.Option
Justin Yuna364cfb2021-05-13 12:39:42 +0900453 options = append(options, tradefed.Option{Name: "vsr-min-api-level", Value: strconv.FormatInt(int64(*test.Properties.Test_options.Vsr_min_shipping_api_level), 10)})
454 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
455 }
456 if test.Properties.Test_options.Min_vndk_version != nil {
457 var options []tradefed.Option
458 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 +0900459 options = append(options, tradefed.Option{Name: "api-level-prop", Value: "ro.vndk.version"})
460 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.MinApiLevelModuleController", options})
461 }
yelinhsieh9fc60402018-10-01 19:23:14 +0800462
Jack He33338892018-09-19 02:21:28 -0700463 test.testConfig = tradefed.AutoGenNativeTestConfig(ctx, test.Properties.Test_config,
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000464 test.Properties.Test_config_template, test.testDecorator.InstallerProperties.Test_suites, configs, test.Properties.Auto_gen_config, testInstallBase)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800465
Dan Shi95d19422020-08-15 12:24:26 -0700466 test.extraTestConfigs = android.PathsForModuleSrc(ctx, test.Properties.Test_options.Extra_test_configs)
467
Colin Cross600c9df2016-09-13 12:26:16 -0700468 test.binaryDecorator.baseInstaller.dir = "nativetest"
469 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800470
471 if !Bool(test.Properties.No_named_install_directory) {
472 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800473 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800474 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
475 }
476
Julien Desprez8908b372021-02-04 10:10:16 -0800477 if ctx.Host() && test.gtest() && test.Properties.Test_options.Unit_test == nil {
Julien Desprez3b933d32021-01-14 11:49:15 -0800478 test.Properties.Test_options.Unit_test = proptools.BoolPtr(true)
479 }
Dan Willemsen1d577e22016-08-29 15:53:15 -0700480 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700481}
482
483func NewTest(hod android.HostOrDeviceSupported) *Module {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400484 module, binary := newBinary(hod, false)
Colin Crossb916a382016-07-29 17:28:03 -0700485 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700486 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700487
488 test := &testBinary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000489 testDecorator: &testDecorator{
490 linker: binary.baseLinker,
491 installer: binary.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700492 },
Colin Crossb916a382016-07-29 17:28:03 -0700493 binaryDecorator: binary,
494 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700495 }
Colin Crossb916a382016-07-29 17:28:03 -0700496 module.compiler = test
497 module.linker = test
498 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700499 return module
500}
501
Colin Crossb916a382016-07-29 17:28:03 -0700502type testLibrary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000503 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700504 *libraryDecorator
505}
506
507func (test *testLibrary) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000508 var props []interface{}
509 props = append(props, test.testDecorator.linkerProps()...)
510 return append(props, test.libraryDecorator.linkerProps()...)
Colin Crossb916a382016-07-29 17:28:03 -0700511}
512
513func (test *testLibrary) linkerInit(ctx BaseModuleContext) {
514 test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker)
515 test.libraryDecorator.linkerInit(ctx)
516}
517
Colin Cross37047f12016-12-13 17:06:13 -0800518func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700519 deps = test.testDecorator.linkerDeps(ctx, deps)
520 deps = test.libraryDecorator.linkerDeps(ctx, deps)
521 return deps
522}
523
524func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
525 flags = test.libraryDecorator.linkerFlags(ctx, flags)
526 flags = test.testDecorator.linkerFlags(ctx, flags)
527 return flags
528}
529
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000530func (test *testLibrary) installerProps() []interface{} {
531 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
532}
533
Colin Cross4d9c2d12016-07-29 12:48:20 -0700534func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800535 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700536 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700537 test := &testLibrary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000538 testDecorator: &testDecorator{
539 linker: library.baseLinker,
540 installer: library.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700541 },
Colin Crossb916a382016-07-29 17:28:03 -0700542 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700543 }
Colin Crossb916a382016-07-29 17:28:03 -0700544 module.linker = test
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000545 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700546 return module
547}
548
Colin Crosse28f4e22017-04-24 18:10:29 -0700549type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700550 // list of files or filegroup modules that provide data that should be installed alongside
551 // the test
Colin Cross27b922f2019-03-04 22:35:41 -0800552 Data []string `android:"path"`
Anders Lewisb97e8182017-07-14 15:20:13 -0700553
Colin Crosse28f4e22017-04-24 18:10:29 -0700554 // list of compatibility suites (for example "cts", "vts") that the module should be
555 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700556 Test_suites []string `android:"arch_variant"`
557
558 // the name of the test configuration (for example "AndroidTest.xml") that should be
559 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800560 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700561
562 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
563 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800564 Test_config_template *string `android:"path,arch_variant"`
Dan Shi37ee3b82019-06-06 16:23:32 -0700565
566 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
567 // with root permission.
568 Require_root *bool
Dan Shi6ffaaa82019-09-26 11:41:36 -0700569
570 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
571 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
572 // explicitly.
573 Auto_gen_config *bool
Colin Crosse28f4e22017-04-24 18:10:29 -0700574}
575
Colin Crossb916a382016-07-29 17:28:03 -0700576type benchmarkDecorator struct {
577 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700578 Properties BenchmarkProperties
Anders Lewisb97e8182017-07-14 15:20:13 -0700579 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700580 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700581}
582
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400583func (benchmark *benchmarkDecorator) benchmarkBinary() bool {
584 return true
585}
586
Colin Crossb916a382016-07-29 17:28:03 -0700587func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) {
588 runpath := "../../lib"
589 if ctx.toolchain().Is64Bit() {
590 runpath += "64"
591 }
592 benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath)
593 benchmark.binaryDecorator.linkerInit(ctx)
594}
595
Colin Crosse28f4e22017-04-24 18:10:29 -0700596func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
597 props := benchmark.binaryDecorator.linkerProps()
598 props = append(props, &benchmark.Properties)
599 return props
600}
601
Colin Cross37047f12016-12-13 17:06:13 -0800602func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700603 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700604 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
605 return deps
606}
607
Colin Crossb916a382016-07-29 17:28:03 -0700608func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Colin Cross8a497952019-03-05 22:25:09 -0800609 benchmark.data = android.PathsForModuleSrc(ctx, benchmark.Properties.Data)
Chris Parsons79d66a52020-06-05 17:26:16 -0400610
Dan Shi37ee3b82019-06-06 16:23:32 -0700611 var configs []tradefed.Config
612 if Bool(benchmark.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800613 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shi37ee3b82019-06-06 16:23:32 -0700614 }
Jack He33338892018-09-19 02:21:28 -0700615 benchmark.testConfig = tradefed.AutoGenNativeBenchmarkTestConfig(ctx, benchmark.Properties.Test_config,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700616 benchmark.Properties.Test_config_template, benchmark.Properties.Test_suites, configs, benchmark.Properties.Auto_gen_config)
Colin Cross303e21f2018-08-07 16:49:25 -0700617
Colin Cross28690e92017-09-08 16:20:30 -0700618 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
619 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Dan Willemsen1d577e22016-08-29 15:53:15 -0700620 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700621}
622
Colin Cross4d9c2d12016-07-29 12:48:20 -0700623func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400624 module, binary := newBinary(hod, false)
Colin Crossb916a382016-07-29 17:28:03 -0700625 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700626 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700627
628 benchmark := &benchmarkDecorator{
629 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700630 }
Colin Crossb916a382016-07-29 17:28:03 -0700631 module.linker = benchmark
632 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700633 return module
634}