blob: ee103ffbe1561b9371f57e28028392f6d2d1cff5 [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
Colin Cross4d9c2d12016-07-29 12:48:20 -070022 "android/soong/android"
Colin Cross303e21f2018-08-07 16:49:25 -070023 "android/soong/tradefed"
Colin Cross4d9c2d12016-07-29 12:48:20 -070024)
25
Colin Crossb916a382016-07-29 17:28:03 -070026type TestProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070027 // if set, build against the gtest library. Defaults to true.
Colin Cross600c9df2016-09-13 12:26:16 -070028 Gtest *bool
Christopher Ferris9df92d62018-08-21 12:40:08 -070029
30 // if set, use the isolated gtest runner. Defaults to false.
31 Isolated *bool
Jiyong Park62304bb2020-04-13 16:19:48 +090032
33 // List of APEXes that this module tests. The module has access to
34 // the private part of the listed APEXes even when it is not included in the
35 // APEXes.
36 Test_for []string
Colin Crossb916a382016-07-29 17:28:03 -070037}
Colin Cross4d9c2d12016-07-29 12:48:20 -070038
yelinhsieh9fc60402018-10-01 19:23:14 +080039// Test option struct.
40type TestOptions struct {
41 // The UID that you want to run the test as on a device.
42 Run_test_as *string
Dan Shi95d19422020-08-15 12:24:26 -070043
David Srbecky519db272020-06-18 18:07:00 +010044 // A list of free-formed strings without spaces that categorize the test.
45 Test_suite_tag []string
Dan Shi95d19422020-08-15 12:24:26 -070046
47 // a list of extra test configuration files that should be installed with the module.
48 Extra_test_configs []string `android:"path,arch_variant"`
yelinhsieh9fc60402018-10-01 19:23:14 +080049}
50
Colin Crossb916a382016-07-29 17:28:03 -070051type TestBinaryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070052 // Create a separate binary for each source file. Useful when there is
53 // global state that can not be torn down and reset between each test suite.
54 Test_per_src *bool
Dan Willemsen3340d602016-12-27 14:40:40 -080055
56 // Disables the creation of a test-specific directory when used with
57 // relative_install_path. Useful if several tests need to be in the same
58 // directory, but test_per_src doesn't work.
59 No_named_install_directory *bool
Colin Crossfaeb7aa2017-02-01 14:12:44 -080060
61 // list of files or filegroup modules that provide data that should be installed alongside
62 // the test
Dan Shi67a88342020-02-25 16:34:39 -080063 Data []string `android:"path,arch_variant"`
Colin Crossa929db02017-03-27 16:27:50 -070064
Chris Parsons79d66a52020-06-05 17:26:16 -040065 // list of shared library modules that should be installed alongside the test
66 Data_libs []string `android:"arch_variant"`
67
Colin Crossa929db02017-03-27 16:27:50 -070068 // list of compatibility suites (for example "cts", "vts") that the module should be
69 // installed into.
Dan Willemsen15d54d52017-09-18 16:49:28 -070070 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -070071
72 // the name of the test configuration (for example "AndroidTest.xml") that should be
73 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -080074 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -070075
76 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
77 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -080078 Test_config_template *string `android:"path,arch_variant"`
yelinhsieh9fc60402018-10-01 19:23:14 +080079
80 // Test options.
81 Test_options TestOptions
Dan Shi37ee3b82019-06-06 16:23:32 -070082
83 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
84 // with root permission.
85 Require_root *bool
Dan Shi20ccd212019-08-27 10:37:24 -070086
87 // Add RunCommandTargetPreparer to stop framework before the test and start it after the test.
88 Disable_framework *bool
nelsonli0d7111e2019-09-17 16:35:23 +080089
90 // Add MinApiLevelModuleController to auto generated test config. If the device property of
91 // "ro.product.first_api_level" < Test_min_api_level, then skip this module.
92 Test_min_api_level *int64
93
94 // Add MinApiLevelModuleController to auto generated test config. If the device property of
95 // "ro.build.version.sdk" < Test_min_sdk_version, then skip this module.
96 Test_min_sdk_version *int64
Dan Shi6ffaaa82019-09-26 11:41:36 -070097
98 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
99 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
100 // explicitly.
101 Auto_gen_config *bool
easoncylee1e3fdcd2020-04-30 10:08:33 +0800102
103 // Add parameterized mainline modules to auto generated test config. The options will be
104 // handled by TradeFed to download and install the specified modules on the device.
105 Test_mainline_modules []string
Colin Cross4d9c2d12016-07-29 12:48:20 -0700106}
107
108func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700109 android.RegisterModuleType("cc_test", TestFactory)
110 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
111 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
112 android.RegisterModuleType("cc_test_host", TestHostFactory)
113 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700114}
115
Patrice Arrudac249c712019-03-19 17:00:29 -0700116// cc_test generates a test config file and an executable binary file to test
117// specific functionality on a device. The executable binary gets an implicit
118// static_libs dependency on libgtests unless the gtest flag is set to false.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700119func TestFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700120 module := NewTest(android.HostAndDeviceSupported)
121 return module.Init()
122}
123
Patrice Arrudac249c712019-03-19 17:00:29 -0700124// cc_test_library creates an archive of files (i.e. .o files) which is later
125// referenced by another module (such as cc_test, cc_defaults or cc_test_library)
126// for archiving or linking.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700127func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700128 module := NewTestLibrary(android.HostAndDeviceSupported)
129 return module.Init()
130}
131
Patrice Arrudac249c712019-03-19 17:00:29 -0700132// cc_benchmark compiles an executable binary that performs benchmark testing
133// of a specific component in a device. Additional files such as test suites
134// and test configuration are installed on the side of the compiled executed
135// binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700136func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700137 module := NewBenchmark(android.HostAndDeviceSupported)
138 return module.Init()
139}
140
Patrice Arrudac249c712019-03-19 17:00:29 -0700141// cc_test_host compiles a test host binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700142func TestHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700143 module := NewTest(android.HostSupported)
144 return module.Init()
145}
146
Patrice Arrudac249c712019-03-19 17:00:29 -0700147// cc_benchmark_host compiles an executable binary that performs benchmark
148// testing of a specific component in the host. Additional files such as
149// test suites and test configuration are installed on the side of the
150// compiled executed binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700151func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700152 module := NewBenchmark(android.HostSupported)
153 return module.Init()
154}
155
Colin Crossb916a382016-07-29 17:28:03 -0700156type testPerSrc interface {
157 testPerSrc() bool
158 srcs() []string
Roland Levillainf2fad972019-06-28 15:41:19 +0100159 isAllTestsVariation() bool
Colin Crossb916a382016-07-29 17:28:03 -0700160 setSrc(string, string)
Roland Levillainf2fad972019-06-28 15:41:19 +0100161 unsetSrc()
Colin Crossb916a382016-07-29 17:28:03 -0700162}
163
164func (test *testBinary) testPerSrc() bool {
165 return Bool(test.Properties.Test_per_src)
166}
167
168func (test *testBinary) srcs() []string {
169 return test.baseCompiler.Properties.Srcs
170}
171
Chris Parsons216e10a2020-07-09 17:12:52 -0400172func (test *testBinary) dataPaths() []android.DataPath {
Liz Kammer1c14a212020-05-12 15:26:55 -0700173 return test.data
174}
175
Roland Levillainf2fad972019-06-28 15:41:19 +0100176func (test *testBinary) isAllTestsVariation() bool {
177 stem := test.binaryDecorator.Properties.Stem
178 return stem != nil && *stem == ""
179}
180
Colin Crossb916a382016-07-29 17:28:03 -0700181func (test *testBinary) setSrc(name, src string) {
182 test.baseCompiler.Properties.Srcs = []string{src}
Nan Zhang0007d812017-11-07 10:57:05 -0800183 test.binaryDecorator.Properties.Stem = StringPtr(name)
Colin Crossb916a382016-07-29 17:28:03 -0700184}
185
Roland Levillainf2fad972019-06-28 15:41:19 +0100186func (test *testBinary) unsetSrc() {
187 test.baseCompiler.Properties.Srcs = nil
188 test.binaryDecorator.Properties.Stem = StringPtr("")
189}
190
Colin Crossb916a382016-07-29 17:28:03 -0700191var _ testPerSrc = (*testBinary)(nil)
192
Roland Levillain9b5fde92019-06-28 15:41:19 +0100193func TestPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700194 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700195 if test, ok := m.linker.(testPerSrc); ok {
Roland Levillainf2fad972019-06-28 15:41:19 +0100196 numTests := len(test.srcs())
197 if test.testPerSrc() && numTests > 0 {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700198 if duplicate, found := android.CheckDuplicate(test.srcs()); found {
Jooyung Hana61ff2c2019-02-28 18:06:34 +0900199 mctx.PropertyErrorf("srcs", "found a duplicate entry %q", duplicate)
200 return
201 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100202 testNames := make([]string, numTests)
Colin Crossb916a382016-07-29 17:28:03 -0700203 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700204 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
205 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100206 // In addition to creating one variation per test source file,
207 // create an additional "all tests" variation named "", and have it
208 // depends on all other test_per_src variations. This is useful to
209 // create subsequent dependencies of a given module on all
210 // test_per_src variations created above: by depending on
211 // variation "", that module will transitively depend on all the
212 // other test_per_src variations without the need to know their
213 // name or even their number.
214 testNames = append(testNames, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700215 tests := mctx.CreateLocalVariations(testNames...)
Roland Levillainf2fad972019-06-28 15:41:19 +0100216 all_tests := tests[numTests]
217 all_tests.(*Module).linker.(testPerSrc).unsetSrc()
218 // Prevent the "all tests" variation from being installable nor
219 // exporting to Make, as it won't create any output file.
220 all_tests.(*Module).Properties.PreventInstall = true
221 all_tests.(*Module).Properties.HideFromMake = true
Colin Crossb916a382016-07-29 17:28:03 -0700222 for i, src := range test.srcs() {
223 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Roland Levillainf2fad972019-06-28 15:41:19 +0100224 mctx.AddInterVariantDependency(testPerSrcDepTag, all_tests, tests[i])
Colin Cross4d9c2d12016-07-29 12:48:20 -0700225 }
226 }
227 }
228 }
229}
230
Colin Crossb916a382016-07-29 17:28:03 -0700231type testDecorator struct {
232 Properties TestProperties
233 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700234}
235
Colin Cross600c9df2016-09-13 12:26:16 -0700236func (test *testDecorator) gtest() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700237 return BoolDefault(test.Properties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700238}
239
Jiyong Park62304bb2020-04-13 16:19:48 +0900240func (test *testDecorator) testFor() []string {
241 return test.Properties.Test_for
242}
243
Colin Crossb916a382016-07-29 17:28:03 -0700244func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700245 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700246 return flags
247 }
248
Colin Cross4af21ed2019-11-04 09:37:55 -0800249 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700250 if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800251 flags.Local.CFlags = append(flags.Local.CFlags, "-O0", "-g")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700252
253 switch ctx.Os() {
254 case android.Windows:
Colin Cross4af21ed2019-11-04 09:37:55 -0800255 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700256 case android.Linux:
Colin Cross4af21ed2019-11-04 09:37:55 -0800257 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700258 case android.Darwin:
Colin Cross4af21ed2019-11-04 09:37:55 -0800259 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700260 }
261 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800262 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700263 }
264
265 return flags
266}
267
Colin Crossb916a382016-07-29 17:28:03 -0700268func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700269 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700270 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800271 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Christopher Ferris9df92d62018-08-21 12:40:08 -0700272 } else if BoolDefault(test.Properties.Isolated, false) {
273 deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main")
Christopher Ferris34cbba62019-07-17 15:46:29 -0700274 // The isolated library requires liblog, but adding it
275 // as a static library means unit tests cannot override
276 // liblog functions. Instead make it a shared library
277 // dependency.
278 deps.SharedLibs = append(deps.SharedLibs, "liblog")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700279 } else {
280 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
281 }
282 }
Colin Crossb916a382016-07-29 17:28:03 -0700283
Colin Cross4d9c2d12016-07-29 12:48:20 -0700284 return deps
285}
286
Colin Crossb916a382016-07-29 17:28:03 -0700287func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) {
yangbillb3174d12018-05-07 06:41:20 +0000288 // 1. Add ../../lib[64] to rpath so that out/host/linux-x86/nativetest/<test dir>/<test> can
Colin Crossbd75e1d2017-06-30 17:27:55 -0700289 // find out/host/linux-x86/lib[64]/library.so
yangbillb3174d12018-05-07 06:41:20 +0000290 // 2. Add ../../../lib[64] to rpath so that out/host/linux-x86/testcases/<test dir>/<CPU>/<test> can
291 // also find out/host/linux-x86/lib[64]/library.so
292 runpaths := []string{"../../lib", "../../../lib"}
293 for _, runpath := range runpaths {
294 if ctx.toolchain().Is64Bit() {
295 runpath += "64"
296 }
297 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700298 }
Colin Crossbd75e1d2017-06-30 17:27:55 -0700299
300 // add "" to rpath so that test binaries can find libraries in their own test directory
301 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700302}
303
Colin Crossb916a382016-07-29 17:28:03 -0700304func (test *testDecorator) linkerProps() []interface{} {
305 return []interface{}{&test.Properties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700306}
307
Colin Crossb916a382016-07-29 17:28:03 -0700308func NewTestInstaller() *baseInstaller {
309 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700310}
311
Colin Crossb916a382016-07-29 17:28:03 -0700312type testBinary struct {
313 testDecorator
314 *binaryDecorator
315 *baseCompiler
Dan Shi95d19422020-08-15 12:24:26 -0700316 Properties TestBinaryProperties
317 data []android.DataPath
318 testConfig android.Path
319 extraTestConfigs android.Paths
Colin Crossb916a382016-07-29 17:28:03 -0700320}
321
322func (test *testBinary) linkerProps() []interface{} {
323 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
324 props = append(props, &test.Properties)
325 return props
326}
327
328func (test *testBinary) linkerInit(ctx BaseModuleContext) {
329 test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker)
330 test.binaryDecorator.linkerInit(ctx)
331}
332
Colin Cross37047f12016-12-13 17:06:13 -0800333func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700334 deps = test.testDecorator.linkerDeps(ctx, deps)
335 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Chris Parsons79d66a52020-06-05 17:26:16 -0400336 deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700337 return deps
338}
339
Colin Crossb916a382016-07-29 17:28:03 -0700340func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
341 flags = test.binaryDecorator.linkerFlags(ctx, flags)
342 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700343 return flags
344}
345
Colin Crossb916a382016-07-29 17:28:03 -0700346func (test *testBinary) install(ctx ModuleContext, file android.Path) {
yangbill5ec45552020-08-13 16:16:56 +0800347 // TODO: (b/167308193) Switch to /data/local/tests/unrestricted as the default install base.
348 testInstallBase := "/data/local/tmp"
349 if ctx.inVendor() || ctx.useVndk() {
350 testInstallBase = "/data/local/tests/vendor"
351 }
352
Chris Parsons216e10a2020-07-09 17:12:52 -0400353 dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
354
355 for _, dataSrcPath := range dataSrcPaths {
356 test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
357 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400358
359 ctx.VisitDirectDepsWithTag(dataLibDepTag, func(dep android.Module) {
360 depName := ctx.OtherModuleName(dep)
361 ccDep, ok := dep.(LinkableInterface)
362
363 if !ok {
364 ctx.ModuleErrorf("data_lib %q is not a linkable cc module", depName)
365 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400366 ccModule, ok := dep.(*Module)
367 if !ok {
368 ctx.ModuleErrorf("data_lib %q is not a cc module", depName)
369 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400370 if ccDep.OutputFile().Valid() {
Chris Parsons216e10a2020-07-09 17:12:52 -0400371 test.data = append(test.data,
372 android.DataPath{SrcPath: ccDep.OutputFile().Path(),
373 RelativeInstallPath: ccModule.installer.relativeInstallPath()})
Chris Parsons79d66a52020-06-05 17:26:16 -0400374 }
375 })
376
nelsonli0d7111e2019-09-17 16:35:23 +0800377 var api_level_prop string
Dan Shi37ee3b82019-06-06 16:23:32 -0700378 var configs []tradefed.Config
nelsonli0d7111e2019-09-17 16:35:23 +0800379 var min_level string
easoncylee1e3fdcd2020-04-30 10:08:33 +0800380 for _, module := range test.Properties.Test_mainline_modules {
381 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module})
382 }
Dan Shi37ee3b82019-06-06 16:23:32 -0700383 if Bool(test.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800384 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shibc0f2602019-09-17 02:43:50 +0000385 } else {
386 var options []tradefed.Option
easoncylee1e3fdcd2020-04-30 10:08:33 +0800387 options = append(options, tradefed.Option{Name: "force-root", Value: "false"})
nelsonli0d7111e2019-09-17 16:35:23 +0800388 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
Dan Shi20ccd212019-08-27 10:37:24 -0700389 }
390 if Bool(test.Properties.Disable_framework) {
391 var options []tradefed.Option
Dan Shi8aa40102020-05-12 13:50:12 -0700392 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.StopServicesSetup", options})
Julien Desprezeb7398e2019-02-28 08:45:28 -0800393 }
Dan Shi37ee3b82019-06-06 16:23:32 -0700394 if Bool(test.testDecorator.Properties.Isolated) {
easoncylee1e3fdcd2020-04-30 10:08:33 +0800395 configs = append(configs, tradefed.Option{Name: "not-shardable", Value: "true"})
Dan Shi37ee3b82019-06-06 16:23:32 -0700396 }
yelinhsieh9fc60402018-10-01 19:23:14 +0800397 if test.Properties.Test_options.Run_test_as != nil {
easoncylee1e3fdcd2020-04-30 10:08:33 +0800398 configs = append(configs, tradefed.Option{Name: "run-test-as", Value: String(test.Properties.Test_options.Run_test_as)})
yelinhsieh9fc60402018-10-01 19:23:14 +0800399 }
David Srbecky519db272020-06-18 18:07:00 +0100400 for _, tag := range test.Properties.Test_options.Test_suite_tag {
401 configs = append(configs, tradefed.Option{Name: "test-suite-tag", Value: tag})
402 }
nelsonli0d7111e2019-09-17 16:35:23 +0800403 if test.Properties.Test_min_api_level != nil && test.Properties.Test_min_sdk_version != nil {
404 ctx.PropertyErrorf("test_min_api_level", "'test_min_api_level' and 'test_min_sdk_version' should not be set at the same time.")
405 } else if test.Properties.Test_min_api_level != nil {
406 api_level_prop = "ro.product.first_api_level"
407 min_level = strconv.FormatInt(int64(*test.Properties.Test_min_api_level), 10)
408 } else if test.Properties.Test_min_sdk_version != nil {
409 api_level_prop = "ro.build.version.sdk"
410 min_level = strconv.FormatInt(int64(*test.Properties.Test_min_sdk_version), 10)
411 }
412 if api_level_prop != "" {
413 var options []tradefed.Option
easoncylee1e3fdcd2020-04-30 10:08:33 +0800414 options = append(options, tradefed.Option{Name: "min-api-level", Value: min_level})
415 options = append(options, tradefed.Option{Name: "api-level-prop", Value: api_level_prop})
nelsonli0d7111e2019-09-17 16:35:23 +0800416 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.MinApiLevelModuleController", options})
417 }
yelinhsieh9fc60402018-10-01 19:23:14 +0800418
Jack He33338892018-09-19 02:21:28 -0700419 test.testConfig = tradefed.AutoGenNativeTestConfig(ctx, test.Properties.Test_config,
yangbill5ec45552020-08-13 16:16:56 +0800420 test.Properties.Test_config_template, test.Properties.Test_suites, configs, test.Properties.Auto_gen_config, testInstallBase)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800421
Dan Shi95d19422020-08-15 12:24:26 -0700422 test.extraTestConfigs = android.PathsForModuleSrc(ctx, test.Properties.Test_options.Extra_test_configs)
423
Colin Cross600c9df2016-09-13 12:26:16 -0700424 test.binaryDecorator.baseInstaller.dir = "nativetest"
425 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800426
427 if !Bool(test.Properties.No_named_install_directory) {
428 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800429 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800430 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
431 }
432
Dan Willemsen1d577e22016-08-29 15:53:15 -0700433 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700434}
435
436func NewTest(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700437 module, binary := NewBinary(hod)
438 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700439 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700440
441 test := &testBinary{
442 testDecorator: testDecorator{
443 linker: binary.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700444 },
Colin Crossb916a382016-07-29 17:28:03 -0700445 binaryDecorator: binary,
446 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700447 }
Colin Crossb916a382016-07-29 17:28:03 -0700448 module.compiler = test
449 module.linker = test
450 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700451 return module
452}
453
Colin Crossb916a382016-07-29 17:28:03 -0700454type testLibrary struct {
455 testDecorator
456 *libraryDecorator
457}
458
459func (test *testLibrary) linkerProps() []interface{} {
460 return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...)
461}
462
463func (test *testLibrary) linkerInit(ctx BaseModuleContext) {
464 test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker)
465 test.libraryDecorator.linkerInit(ctx)
466}
467
Colin Cross37047f12016-12-13 17:06:13 -0800468func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700469 deps = test.testDecorator.linkerDeps(ctx, deps)
470 deps = test.libraryDecorator.linkerDeps(ctx, deps)
471 return deps
472}
473
474func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
475 flags = test.libraryDecorator.linkerFlags(ctx, flags)
476 flags = test.testDecorator.linkerFlags(ctx, flags)
477 return flags
478}
479
Colin Cross4d9c2d12016-07-29 12:48:20 -0700480func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800481 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700482 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700483 test := &testLibrary{
484 testDecorator: testDecorator{
485 linker: library.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700486 },
Colin Crossb916a382016-07-29 17:28:03 -0700487 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700488 }
Colin Crossb916a382016-07-29 17:28:03 -0700489 module.linker = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700490 return module
491}
492
Colin Crosse28f4e22017-04-24 18:10:29 -0700493type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700494 // list of files or filegroup modules that provide data that should be installed alongside
495 // the test
Colin Cross27b922f2019-03-04 22:35:41 -0800496 Data []string `android:"path"`
Anders Lewisb97e8182017-07-14 15:20:13 -0700497
Colin Crosse28f4e22017-04-24 18:10:29 -0700498 // list of compatibility suites (for example "cts", "vts") that the module should be
499 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700500 Test_suites []string `android:"arch_variant"`
501
502 // the name of the test configuration (for example "AndroidTest.xml") that should be
503 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800504 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700505
506 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
507 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800508 Test_config_template *string `android:"path,arch_variant"`
Dan Shi37ee3b82019-06-06 16:23:32 -0700509
510 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
511 // with root permission.
512 Require_root *bool
Dan Shi6ffaaa82019-09-26 11:41:36 -0700513
514 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
515 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
516 // explicitly.
517 Auto_gen_config *bool
Colin Crosse28f4e22017-04-24 18:10:29 -0700518}
519
Colin Crossb916a382016-07-29 17:28:03 -0700520type benchmarkDecorator struct {
521 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700522 Properties BenchmarkProperties
Anders Lewisb97e8182017-07-14 15:20:13 -0700523 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700524 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700525}
526
Colin Crossb916a382016-07-29 17:28:03 -0700527func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) {
528 runpath := "../../lib"
529 if ctx.toolchain().Is64Bit() {
530 runpath += "64"
531 }
532 benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath)
533 benchmark.binaryDecorator.linkerInit(ctx)
534}
535
Colin Crosse28f4e22017-04-24 18:10:29 -0700536func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
537 props := benchmark.binaryDecorator.linkerProps()
538 props = append(props, &benchmark.Properties)
539 return props
540}
541
Colin Cross37047f12016-12-13 17:06:13 -0800542func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700543 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700544 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
545 return deps
546}
547
Colin Crossb916a382016-07-29 17:28:03 -0700548func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Colin Cross8a497952019-03-05 22:25:09 -0800549 benchmark.data = android.PathsForModuleSrc(ctx, benchmark.Properties.Data)
Chris Parsons79d66a52020-06-05 17:26:16 -0400550
Dan Shi37ee3b82019-06-06 16:23:32 -0700551 var configs []tradefed.Config
552 if Bool(benchmark.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800553 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shi37ee3b82019-06-06 16:23:32 -0700554 }
Jack He33338892018-09-19 02:21:28 -0700555 benchmark.testConfig = tradefed.AutoGenNativeBenchmarkTestConfig(ctx, benchmark.Properties.Test_config,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700556 benchmark.Properties.Test_config_template, benchmark.Properties.Test_suites, configs, benchmark.Properties.Auto_gen_config)
Colin Cross303e21f2018-08-07 16:49:25 -0700557
Colin Cross28690e92017-09-08 16:20:30 -0700558 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
559 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Dan Willemsen1d577e22016-08-29 15:53:15 -0700560 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700561}
562
Colin Cross4d9c2d12016-07-29 12:48:20 -0700563func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700564 module, binary := NewBinary(hod)
565 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700566 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700567
568 benchmark := &benchmarkDecorator{
569 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700570 }
Colin Crossb916a382016-07-29 17:28:03 -0700571 module.linker = benchmark
572 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700573 return module
574}