blob: f715a8d702bf572472ab2aff2ec8e518a3450d88 [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
Colin Crossb916a382016-07-29 17:28:03 -070032}
Colin Cross4d9c2d12016-07-29 12:48:20 -070033
yelinhsieh9fc60402018-10-01 19:23:14 +080034// Test option struct.
35type TestOptions struct {
36 // The UID that you want to run the test as on a device.
37 Run_test_as *string
Dan Shi95d19422020-08-15 12:24:26 -070038
David Srbecky519db272020-06-18 18:07:00 +010039 // A list of free-formed strings without spaces that categorize the test.
40 Test_suite_tag []string
Dan Shi95d19422020-08-15 12:24:26 -070041
42 // a list of extra test configuration files that should be installed with the module.
43 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -080044
45 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
46 Unit_test *bool
yelinhsieh9fc60402018-10-01 19:23:14 +080047}
48
Colin Crossb916a382016-07-29 17:28:03 -070049type TestBinaryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070050 // Create a separate binary for each source file. Useful when there is
51 // global state that can not be torn down and reset between each test suite.
52 Test_per_src *bool
Dan Willemsen3340d602016-12-27 14:40:40 -080053
54 // Disables the creation of a test-specific directory when used with
55 // relative_install_path. Useful if several tests need to be in the same
56 // directory, but test_per_src doesn't work.
57 No_named_install_directory *bool
Colin Crossfaeb7aa2017-02-01 14:12:44 -080058
59 // list of files or filegroup modules that provide data that should be installed alongside
60 // the test
Dan Shi67a88342020-02-25 16:34:39 -080061 Data []string `android:"path,arch_variant"`
Colin Crossa929db02017-03-27 16:27:50 -070062
Chris Parsons79d66a52020-06-05 17:26:16 -040063 // list of shared library modules that should be installed alongside the test
64 Data_libs []string `android:"arch_variant"`
65
Colin Crossa929db02017-03-27 16:27:50 -070066 // list of compatibility suites (for example "cts", "vts") that the module should be
67 // installed into.
Dan Willemsen15d54d52017-09-18 16:49:28 -070068 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -070069
70 // the name of the test configuration (for example "AndroidTest.xml") that should be
71 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -080072 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -070073
74 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
75 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -080076 Test_config_template *string `android:"path,arch_variant"`
yelinhsieh9fc60402018-10-01 19:23:14 +080077
78 // Test options.
79 Test_options TestOptions
Dan Shi37ee3b82019-06-06 16:23:32 -070080
81 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
82 // with root permission.
83 Require_root *bool
Dan Shi20ccd212019-08-27 10:37:24 -070084
85 // Add RunCommandTargetPreparer to stop framework before the test and start it after the test.
86 Disable_framework *bool
nelsonli0d7111e2019-09-17 16:35:23 +080087
88 // Add MinApiLevelModuleController to auto generated test config. If the device property of
89 // "ro.product.first_api_level" < Test_min_api_level, then skip this module.
90 Test_min_api_level *int64
91
92 // Add MinApiLevelModuleController to auto generated test config. If the device property of
93 // "ro.build.version.sdk" < Test_min_sdk_version, then skip this module.
94 Test_min_sdk_version *int64
Dan Shi6ffaaa82019-09-26 11:41:36 -070095
96 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
97 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
98 // explicitly.
99 Auto_gen_config *bool
easoncylee1e3fdcd2020-04-30 10:08:33 +0800100
101 // Add parameterized mainline modules to auto generated test config. The options will be
102 // handled by TradeFed to download and install the specified modules on the device.
103 Test_mainline_modules []string
Colin Cross4d9c2d12016-07-29 12:48:20 -0700104}
105
106func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700107 android.RegisterModuleType("cc_test", TestFactory)
108 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
109 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
110 android.RegisterModuleType("cc_test_host", TestHostFactory)
111 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700112}
113
Patrice Arrudac249c712019-03-19 17:00:29 -0700114// cc_test generates a test config file and an executable binary file to test
115// specific functionality on a device. The executable binary gets an implicit
116// static_libs dependency on libgtests unless the gtest flag is set to false.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700117func TestFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700118 module := NewTest(android.HostAndDeviceSupported)
119 return module.Init()
120}
121
Patrice Arrudac249c712019-03-19 17:00:29 -0700122// cc_test_library creates an archive of files (i.e. .o files) which is later
123// referenced by another module (such as cc_test, cc_defaults or cc_test_library)
124// for archiving or linking.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700125func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700126 module := NewTestLibrary(android.HostAndDeviceSupported)
127 return module.Init()
128}
129
Patrice Arrudac249c712019-03-19 17:00:29 -0700130// cc_benchmark compiles an executable binary that performs benchmark testing
131// of a specific component in a device. Additional files such as test suites
132// and test configuration are installed on the side of the compiled executed
133// binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700134func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700135 module := NewBenchmark(android.HostAndDeviceSupported)
136 return module.Init()
137}
138
Patrice Arrudac249c712019-03-19 17:00:29 -0700139// cc_test_host compiles a test host binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700140func TestHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700141 module := NewTest(android.HostSupported)
142 return module.Init()
143}
144
Patrice Arrudac249c712019-03-19 17:00:29 -0700145// cc_benchmark_host compiles an executable binary that performs benchmark
146// testing of a specific component in the host. Additional files such as
147// test suites and test configuration are installed on the side of the
148// compiled executed binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700149func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700150 module := NewBenchmark(android.HostSupported)
151 return module.Init()
152}
153
Colin Crossb916a382016-07-29 17:28:03 -0700154type testPerSrc interface {
155 testPerSrc() bool
156 srcs() []string
Roland Levillainf2fad972019-06-28 15:41:19 +0100157 isAllTestsVariation() bool
Colin Crossb916a382016-07-29 17:28:03 -0700158 setSrc(string, string)
Roland Levillainf2fad972019-06-28 15:41:19 +0100159 unsetSrc()
Colin Crossb916a382016-07-29 17:28:03 -0700160}
161
162func (test *testBinary) testPerSrc() bool {
163 return Bool(test.Properties.Test_per_src)
164}
165
166func (test *testBinary) srcs() []string {
167 return test.baseCompiler.Properties.Srcs
168}
169
Chris Parsons216e10a2020-07-09 17:12:52 -0400170func (test *testBinary) dataPaths() []android.DataPath {
Liz Kammer1c14a212020-05-12 15:26:55 -0700171 return test.data
172}
173
Roland Levillainf2fad972019-06-28 15:41:19 +0100174func (test *testBinary) isAllTestsVariation() bool {
175 stem := test.binaryDecorator.Properties.Stem
176 return stem != nil && *stem == ""
177}
178
Colin Crossb916a382016-07-29 17:28:03 -0700179func (test *testBinary) setSrc(name, src string) {
180 test.baseCompiler.Properties.Srcs = []string{src}
Nan Zhang0007d812017-11-07 10:57:05 -0800181 test.binaryDecorator.Properties.Stem = StringPtr(name)
Colin Crossb916a382016-07-29 17:28:03 -0700182}
183
Roland Levillainf2fad972019-06-28 15:41:19 +0100184func (test *testBinary) unsetSrc() {
185 test.baseCompiler.Properties.Srcs = nil
186 test.binaryDecorator.Properties.Stem = StringPtr("")
187}
188
Colin Crossb916a382016-07-29 17:28:03 -0700189var _ testPerSrc = (*testBinary)(nil)
190
Roland Levillain9b5fde92019-06-28 15:41:19 +0100191func TestPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700192 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700193 if test, ok := m.linker.(testPerSrc); ok {
Roland Levillainf2fad972019-06-28 15:41:19 +0100194 numTests := len(test.srcs())
195 if test.testPerSrc() && numTests > 0 {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700196 if duplicate, found := android.CheckDuplicate(test.srcs()); found {
Jooyung Hana61ff2c2019-02-28 18:06:34 +0900197 mctx.PropertyErrorf("srcs", "found a duplicate entry %q", duplicate)
198 return
199 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100200 testNames := make([]string, numTests)
Colin Crossb916a382016-07-29 17:28:03 -0700201 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700202 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
203 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100204 // In addition to creating one variation per test source file,
205 // create an additional "all tests" variation named "", and have it
206 // depends on all other test_per_src variations. This is useful to
207 // create subsequent dependencies of a given module on all
208 // test_per_src variations created above: by depending on
209 // variation "", that module will transitively depend on all the
210 // other test_per_src variations without the need to know their
211 // name or even their number.
212 testNames = append(testNames, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700213 tests := mctx.CreateLocalVariations(testNames...)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800214 allTests := tests[numTests]
215 allTests.(*Module).linker.(testPerSrc).unsetSrc()
Roland Levillainf2fad972019-06-28 15:41:19 +0100216 // Prevent the "all tests" variation from being installable nor
217 // exporting to Make, as it won't create any output file.
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800218 allTests.(*Module).Properties.PreventInstall = true
219 allTests.(*Module).Properties.HideFromMake = true
Colin Crossb916a382016-07-29 17:28:03 -0700220 for i, src := range test.srcs() {
221 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800222 mctx.AddInterVariantDependency(testPerSrcDepTag, allTests, tests[i])
Colin Cross4d9c2d12016-07-29 12:48:20 -0700223 }
Colin Cross90dab342020-08-21 15:55:50 -0700224 mctx.AliasVariation("")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700225 }
226 }
227 }
228}
229
Colin Crossb916a382016-07-29 17:28:03 -0700230type testDecorator struct {
231 Properties TestProperties
232 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700233}
234
Colin Cross600c9df2016-09-13 12:26:16 -0700235func (test *testDecorator) gtest() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700236 return BoolDefault(test.Properties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700237}
238
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700239func (test *testDecorator) testBinary() bool {
240 return true
241}
242
Colin Crossb916a382016-07-29 17:28:03 -0700243func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700244 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700245 return flags
246 }
247
Colin Cross4af21ed2019-11-04 09:37:55 -0800248 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700249 if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800250 flags.Local.CFlags = append(flags.Local.CFlags, "-O0", "-g")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700251
252 switch ctx.Os() {
253 case android.Windows:
Colin Cross4af21ed2019-11-04 09:37:55 -0800254 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255 case android.Linux:
Colin Cross4af21ed2019-11-04 09:37:55 -0800256 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700257 case android.Darwin:
Colin Cross4af21ed2019-11-04 09:37:55 -0800258 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700259 }
260 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800261 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700262 }
263
264 return flags
265}
266
Colin Crossb916a382016-07-29 17:28:03 -0700267func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700268 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700269 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800270 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Christopher Ferris9df92d62018-08-21 12:40:08 -0700271 } else if BoolDefault(test.Properties.Isolated, false) {
272 deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main")
Christopher Ferris34cbba62019-07-17 15:46:29 -0700273 // The isolated library requires liblog, but adding it
274 // as a static library means unit tests cannot override
275 // liblog functions. Instead make it a shared library
276 // dependency.
277 deps.SharedLibs = append(deps.SharedLibs, "liblog")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700278 } else {
279 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
280 }
281 }
Colin Crossb916a382016-07-29 17:28:03 -0700282
Colin Cross4d9c2d12016-07-29 12:48:20 -0700283 return deps
284}
285
Colin Crossb916a382016-07-29 17:28:03 -0700286func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) {
yangbillb3174d12018-05-07 06:41:20 +0000287 // 1. Add ../../lib[64] to rpath so that out/host/linux-x86/nativetest/<test dir>/<test> can
Colin Crossbd75e1d2017-06-30 17:27:55 -0700288 // find out/host/linux-x86/lib[64]/library.so
yangbillb3174d12018-05-07 06:41:20 +0000289 // 2. Add ../../../lib[64] to rpath so that out/host/linux-x86/testcases/<test dir>/<CPU>/<test> can
290 // also find out/host/linux-x86/lib[64]/library.so
291 runpaths := []string{"../../lib", "../../../lib"}
292 for _, runpath := range runpaths {
293 if ctx.toolchain().Is64Bit() {
294 runpath += "64"
295 }
296 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700297 }
Colin Crossbd75e1d2017-06-30 17:27:55 -0700298
299 // add "" to rpath so that test binaries can find libraries in their own test directory
300 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700301}
302
Colin Crossb916a382016-07-29 17:28:03 -0700303func (test *testDecorator) linkerProps() []interface{} {
304 return []interface{}{&test.Properties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700305}
306
Colin Crossb916a382016-07-29 17:28:03 -0700307func NewTestInstaller() *baseInstaller {
308 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700309}
310
Colin Crossb916a382016-07-29 17:28:03 -0700311type testBinary struct {
312 testDecorator
313 *binaryDecorator
314 *baseCompiler
Dan Shi95d19422020-08-15 12:24:26 -0700315 Properties TestBinaryProperties
316 data []android.DataPath
317 testConfig android.Path
318 extraTestConfigs android.Paths
Colin Crossb916a382016-07-29 17:28:03 -0700319}
320
321func (test *testBinary) linkerProps() []interface{} {
322 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
323 props = append(props, &test.Properties)
324 return props
325}
326
327func (test *testBinary) linkerInit(ctx BaseModuleContext) {
328 test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker)
329 test.binaryDecorator.linkerInit(ctx)
330}
331
Colin Cross37047f12016-12-13 17:06:13 -0800332func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700333 deps = test.testDecorator.linkerDeps(ctx, deps)
334 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Chris Parsons79d66a52020-06-05 17:26:16 -0400335 deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700336 return deps
337}
338
Colin Crossb916a382016-07-29 17:28:03 -0700339func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
340 flags = test.binaryDecorator.linkerFlags(ctx, flags)
341 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700342 return flags
343}
344
Colin Crossb916a382016-07-29 17:28:03 -0700345func (test *testBinary) install(ctx ModuleContext, file android.Path) {
yangbill5ec45552020-08-13 16:16:56 +0800346 // TODO: (b/167308193) Switch to /data/local/tests/unrestricted as the default install base.
347 testInstallBase := "/data/local/tmp"
348 if ctx.inVendor() || ctx.useVndk() {
349 testInstallBase = "/data/local/tests/vendor"
350 }
351
Chris Parsons216e10a2020-07-09 17:12:52 -0400352 dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
353
354 for _, dataSrcPath := range dataSrcPaths {
355 test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
356 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400357
358 ctx.VisitDirectDepsWithTag(dataLibDepTag, func(dep android.Module) {
359 depName := ctx.OtherModuleName(dep)
360 ccDep, ok := dep.(LinkableInterface)
361
362 if !ok {
363 ctx.ModuleErrorf("data_lib %q is not a linkable cc module", depName)
364 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400365 ccModule, ok := dep.(*Module)
366 if !ok {
367 ctx.ModuleErrorf("data_lib %q is not a cc module", depName)
368 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400369 if ccDep.OutputFile().Valid() {
Chris Parsons216e10a2020-07-09 17:12:52 -0400370 test.data = append(test.data,
371 android.DataPath{SrcPath: ccDep.OutputFile().Path(),
372 RelativeInstallPath: ccModule.installer.relativeInstallPath()})
Chris Parsons79d66a52020-06-05 17:26:16 -0400373 }
374 })
375
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800376 var apiLevelProp string
Dan Shi37ee3b82019-06-06 16:23:32 -0700377 var configs []tradefed.Config
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800378 var minLevel string
easoncylee1e3fdcd2020-04-30 10:08:33 +0800379 for _, module := range test.Properties.Test_mainline_modules {
380 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module})
381 }
Dan Shi37ee3b82019-06-06 16:23:32 -0700382 if Bool(test.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800383 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shibc0f2602019-09-17 02:43:50 +0000384 } else {
385 var options []tradefed.Option
easoncylee1e3fdcd2020-04-30 10:08:33 +0800386 options = append(options, tradefed.Option{Name: "force-root", Value: "false"})
nelsonli0d7111e2019-09-17 16:35:23 +0800387 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
Dan Shi20ccd212019-08-27 10:37:24 -0700388 }
389 if Bool(test.Properties.Disable_framework) {
390 var options []tradefed.Option
Dan Shi8aa40102020-05-12 13:50:12 -0700391 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.StopServicesSetup", options})
Julien Desprezeb7398e2019-02-28 08:45:28 -0800392 }
Dan Shi37ee3b82019-06-06 16:23:32 -0700393 if Bool(test.testDecorator.Properties.Isolated) {
easoncylee1e3fdcd2020-04-30 10:08:33 +0800394 configs = append(configs, tradefed.Option{Name: "not-shardable", Value: "true"})
Dan Shi37ee3b82019-06-06 16:23:32 -0700395 }
yelinhsieh9fc60402018-10-01 19:23:14 +0800396 if test.Properties.Test_options.Run_test_as != nil {
easoncylee1e3fdcd2020-04-30 10:08:33 +0800397 configs = append(configs, tradefed.Option{Name: "run-test-as", Value: String(test.Properties.Test_options.Run_test_as)})
yelinhsieh9fc60402018-10-01 19:23:14 +0800398 }
David Srbecky519db272020-06-18 18:07:00 +0100399 for _, tag := range test.Properties.Test_options.Test_suite_tag {
400 configs = append(configs, tradefed.Option{Name: "test-suite-tag", Value: tag})
401 }
nelsonli0d7111e2019-09-17 16:35:23 +0800402 if test.Properties.Test_min_api_level != nil && test.Properties.Test_min_sdk_version != nil {
403 ctx.PropertyErrorf("test_min_api_level", "'test_min_api_level' and 'test_min_sdk_version' should not be set at the same time.")
404 } else if test.Properties.Test_min_api_level != nil {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800405 apiLevelProp = "ro.product.first_api_level"
406 minLevel = strconv.FormatInt(int64(*test.Properties.Test_min_api_level), 10)
nelsonli0d7111e2019-09-17 16:35:23 +0800407 } else if test.Properties.Test_min_sdk_version != nil {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800408 apiLevelProp = "ro.build.version.sdk"
409 minLevel = strconv.FormatInt(int64(*test.Properties.Test_min_sdk_version), 10)
nelsonli0d7111e2019-09-17 16:35:23 +0800410 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800411 if apiLevelProp != "" {
nelsonli0d7111e2019-09-17 16:35:23 +0800412 var options []tradefed.Option
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800413 options = append(options, tradefed.Option{Name: "min-api-level", Value: minLevel})
414 options = append(options, tradefed.Option{Name: "api-level-prop", Value: apiLevelProp})
nelsonli0d7111e2019-09-17 16:35:23 +0800415 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.MinApiLevelModuleController", options})
416 }
yelinhsieh9fc60402018-10-01 19:23:14 +0800417
Jack He33338892018-09-19 02:21:28 -0700418 test.testConfig = tradefed.AutoGenNativeTestConfig(ctx, test.Properties.Test_config,
yangbill5ec45552020-08-13 16:16:56 +0800419 test.Properties.Test_config_template, test.Properties.Test_suites, configs, test.Properties.Auto_gen_config, testInstallBase)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800420
Dan Shi95d19422020-08-15 12:24:26 -0700421 test.extraTestConfigs = android.PathsForModuleSrc(ctx, test.Properties.Test_options.Extra_test_configs)
422
Colin Cross600c9df2016-09-13 12:26:16 -0700423 test.binaryDecorator.baseInstaller.dir = "nativetest"
424 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800425
426 if !Bool(test.Properties.No_named_install_directory) {
427 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800428 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800429 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
430 }
431
Dan Willemsen1d577e22016-08-29 15:53:15 -0700432 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700433}
434
435func NewTest(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700436 module, binary := NewBinary(hod)
437 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700438 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700439
440 test := &testBinary{
441 testDecorator: testDecorator{
442 linker: binary.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700443 },
Colin Crossb916a382016-07-29 17:28:03 -0700444 binaryDecorator: binary,
445 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700446 }
Colin Crossb916a382016-07-29 17:28:03 -0700447 module.compiler = test
448 module.linker = test
449 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700450 return module
451}
452
Colin Crossb916a382016-07-29 17:28:03 -0700453type testLibrary struct {
454 testDecorator
455 *libraryDecorator
456}
457
458func (test *testLibrary) linkerProps() []interface{} {
459 return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...)
460}
461
462func (test *testLibrary) linkerInit(ctx BaseModuleContext) {
463 test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker)
464 test.libraryDecorator.linkerInit(ctx)
465}
466
Colin Cross37047f12016-12-13 17:06:13 -0800467func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700468 deps = test.testDecorator.linkerDeps(ctx, deps)
469 deps = test.libraryDecorator.linkerDeps(ctx, deps)
470 return deps
471}
472
473func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
474 flags = test.libraryDecorator.linkerFlags(ctx, flags)
475 flags = test.testDecorator.linkerFlags(ctx, flags)
476 return flags
477}
478
Colin Cross4d9c2d12016-07-29 12:48:20 -0700479func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800480 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700481 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700482 test := &testLibrary{
483 testDecorator: testDecorator{
484 linker: library.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700485 },
Colin Crossb916a382016-07-29 17:28:03 -0700486 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700487 }
Colin Crossb916a382016-07-29 17:28:03 -0700488 module.linker = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700489 return module
490}
491
Colin Crosse28f4e22017-04-24 18:10:29 -0700492type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700493 // list of files or filegroup modules that provide data that should be installed alongside
494 // the test
Colin Cross27b922f2019-03-04 22:35:41 -0800495 Data []string `android:"path"`
Anders Lewisb97e8182017-07-14 15:20:13 -0700496
Colin Crosse28f4e22017-04-24 18:10:29 -0700497 // list of compatibility suites (for example "cts", "vts") that the module should be
498 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700499 Test_suites []string `android:"arch_variant"`
500
501 // the name of the test configuration (for example "AndroidTest.xml") that should be
502 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800503 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700504
505 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
506 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800507 Test_config_template *string `android:"path,arch_variant"`
Dan Shi37ee3b82019-06-06 16:23:32 -0700508
509 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
510 // with root permission.
511 Require_root *bool
Dan Shi6ffaaa82019-09-26 11:41:36 -0700512
513 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
514 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
515 // explicitly.
516 Auto_gen_config *bool
Colin Crosse28f4e22017-04-24 18:10:29 -0700517}
518
Colin Crossb916a382016-07-29 17:28:03 -0700519type benchmarkDecorator struct {
520 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700521 Properties BenchmarkProperties
Anders Lewisb97e8182017-07-14 15:20:13 -0700522 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700523 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700524}
525
Colin Crossb916a382016-07-29 17:28:03 -0700526func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) {
527 runpath := "../../lib"
528 if ctx.toolchain().Is64Bit() {
529 runpath += "64"
530 }
531 benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath)
532 benchmark.binaryDecorator.linkerInit(ctx)
533}
534
Colin Crosse28f4e22017-04-24 18:10:29 -0700535func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
536 props := benchmark.binaryDecorator.linkerProps()
537 props = append(props, &benchmark.Properties)
538 return props
539}
540
Colin Cross37047f12016-12-13 17:06:13 -0800541func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700542 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700543 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
544 return deps
545}
546
Colin Crossb916a382016-07-29 17:28:03 -0700547func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Colin Cross8a497952019-03-05 22:25:09 -0800548 benchmark.data = android.PathsForModuleSrc(ctx, benchmark.Properties.Data)
Chris Parsons79d66a52020-06-05 17:26:16 -0400549
Dan Shi37ee3b82019-06-06 16:23:32 -0700550 var configs []tradefed.Config
551 if Bool(benchmark.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800552 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shi37ee3b82019-06-06 16:23:32 -0700553 }
Jack He33338892018-09-19 02:21:28 -0700554 benchmark.testConfig = tradefed.AutoGenNativeBenchmarkTestConfig(ctx, benchmark.Properties.Test_config,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700555 benchmark.Properties.Test_config_template, benchmark.Properties.Test_suites, configs, benchmark.Properties.Auto_gen_config)
Colin Cross303e21f2018-08-07 16:49:25 -0700556
Colin Cross28690e92017-09-08 16:20:30 -0700557 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
558 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Dan Willemsen1d577e22016-08-29 15:53:15 -0700559 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700560}
561
Colin Cross4d9c2d12016-07-29 12:48:20 -0700562func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700563 module, binary := NewBinary(hod)
564 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700565 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700566
567 benchmark := &benchmarkDecorator{
568 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700569 }
Colin Crossb916a382016-07-29 17:28:03 -0700570 module.linker = benchmark
571 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700572 return module
573}