blob: ba0b7e482888a9838dc20513752d1393c49def8b [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
38}
39
Colin Crossb916a382016-07-29 17:28:03 -070040type TestBinaryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070041 // Create a separate binary for each source file. Useful when there is
42 // global state that can not be torn down and reset between each test suite.
43 Test_per_src *bool
Dan Willemsen3340d602016-12-27 14:40:40 -080044
45 // Disables the creation of a test-specific directory when used with
46 // relative_install_path. Useful if several tests need to be in the same
47 // directory, but test_per_src doesn't work.
48 No_named_install_directory *bool
Colin Crossfaeb7aa2017-02-01 14:12:44 -080049
50 // list of files or filegroup modules that provide data that should be installed alongside
51 // the test
Colin Cross27b922f2019-03-04 22:35:41 -080052 Data []string `android:"path"`
Colin Crossa929db02017-03-27 16:27:50 -070053
54 // list of compatibility suites (for example "cts", "vts") that the module should be
55 // installed into.
Dan Willemsen15d54d52017-09-18 16:49:28 -070056 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -070057
58 // the name of the test configuration (for example "AndroidTest.xml") that should be
59 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -080060 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -070061
62 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
63 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -080064 Test_config_template *string `android:"path,arch_variant"`
yelinhsieh9fc60402018-10-01 19:23:14 +080065
66 // Test options.
67 Test_options TestOptions
Dan Shi37ee3b82019-06-06 16:23:32 -070068
69 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
70 // with root permission.
71 Require_root *bool
Dan Shi20ccd212019-08-27 10:37:24 -070072
73 // Add RunCommandTargetPreparer to stop framework before the test and start it after the test.
74 Disable_framework *bool
nelsonli0d7111e2019-09-17 16:35:23 +080075
76 // Add MinApiLevelModuleController to auto generated test config. If the device property of
77 // "ro.product.first_api_level" < Test_min_api_level, then skip this module.
78 Test_min_api_level *int64
79
80 // Add MinApiLevelModuleController to auto generated test config. If the device property of
81 // "ro.build.version.sdk" < Test_min_sdk_version, then skip this module.
82 Test_min_sdk_version *int64
Colin Cross4d9c2d12016-07-29 12:48:20 -070083}
84
85func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -070086 android.RegisterModuleType("cc_test", TestFactory)
87 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
88 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
89 android.RegisterModuleType("cc_test_host", TestHostFactory)
90 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070091}
92
Patrice Arrudac249c712019-03-19 17:00:29 -070093// cc_test generates a test config file and an executable binary file to test
94// specific functionality on a device. The executable binary gets an implicit
95// static_libs dependency on libgtests unless the gtest flag is set to false.
Steven Moreland87c9d7b2017-11-02 21:38:28 -070096func TestFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070097 module := NewTest(android.HostAndDeviceSupported)
98 return module.Init()
99}
100
Patrice Arrudac249c712019-03-19 17:00:29 -0700101// cc_test_library creates an archive of files (i.e. .o files) which is later
102// referenced by another module (such as cc_test, cc_defaults or cc_test_library)
103// for archiving or linking.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700104func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700105 module := NewTestLibrary(android.HostAndDeviceSupported)
106 return module.Init()
107}
108
Patrice Arrudac249c712019-03-19 17:00:29 -0700109// cc_benchmark compiles an executable binary that performs benchmark testing
110// of a specific component in a device. Additional files such as test suites
111// and test configuration are installed on the side of the compiled executed
112// binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700113func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700114 module := NewBenchmark(android.HostAndDeviceSupported)
115 return module.Init()
116}
117
Patrice Arrudac249c712019-03-19 17:00:29 -0700118// cc_test_host compiles a test host binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700119func TestHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700120 module := NewTest(android.HostSupported)
121 return module.Init()
122}
123
Patrice Arrudac249c712019-03-19 17:00:29 -0700124// cc_benchmark_host compiles an executable binary that performs benchmark
125// testing of a specific component in the host. Additional files such as
126// test suites and test configuration are installed on the side of the
127// compiled executed binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700128func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700129 module := NewBenchmark(android.HostSupported)
130 return module.Init()
131}
132
Colin Crossb916a382016-07-29 17:28:03 -0700133type testPerSrc interface {
134 testPerSrc() bool
135 srcs() []string
Roland Levillainf2fad972019-06-28 15:41:19 +0100136 isAllTestsVariation() bool
Colin Crossb916a382016-07-29 17:28:03 -0700137 setSrc(string, string)
Roland Levillainf2fad972019-06-28 15:41:19 +0100138 unsetSrc()
Colin Crossb916a382016-07-29 17:28:03 -0700139}
140
141func (test *testBinary) testPerSrc() bool {
142 return Bool(test.Properties.Test_per_src)
143}
144
145func (test *testBinary) srcs() []string {
146 return test.baseCompiler.Properties.Srcs
147}
148
Roland Levillainf2fad972019-06-28 15:41:19 +0100149func (test *testBinary) isAllTestsVariation() bool {
150 stem := test.binaryDecorator.Properties.Stem
151 return stem != nil && *stem == ""
152}
153
Colin Crossb916a382016-07-29 17:28:03 -0700154func (test *testBinary) setSrc(name, src string) {
155 test.baseCompiler.Properties.Srcs = []string{src}
Nan Zhang0007d812017-11-07 10:57:05 -0800156 test.binaryDecorator.Properties.Stem = StringPtr(name)
Colin Crossb916a382016-07-29 17:28:03 -0700157}
158
Roland Levillainf2fad972019-06-28 15:41:19 +0100159func (test *testBinary) unsetSrc() {
160 test.baseCompiler.Properties.Srcs = nil
161 test.binaryDecorator.Properties.Stem = StringPtr("")
162}
163
Colin Crossb916a382016-07-29 17:28:03 -0700164var _ testPerSrc = (*testBinary)(nil)
165
Roland Levillain9b5fde92019-06-28 15:41:19 +0100166func TestPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700167 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700168 if test, ok := m.linker.(testPerSrc); ok {
Roland Levillainf2fad972019-06-28 15:41:19 +0100169 numTests := len(test.srcs())
170 if test.testPerSrc() && numTests > 0 {
Jooyung Hana61ff2c2019-02-28 18:06:34 +0900171 if duplicate, found := checkDuplicate(test.srcs()); found {
172 mctx.PropertyErrorf("srcs", "found a duplicate entry %q", duplicate)
173 return
174 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100175 testNames := make([]string, numTests)
Colin Crossb916a382016-07-29 17:28:03 -0700176 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700177 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
178 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100179 // In addition to creating one variation per test source file,
180 // create an additional "all tests" variation named "", and have it
181 // depends on all other test_per_src variations. This is useful to
182 // create subsequent dependencies of a given module on all
183 // test_per_src variations created above: by depending on
184 // variation "", that module will transitively depend on all the
185 // other test_per_src variations without the need to know their
186 // name or even their number.
187 testNames = append(testNames, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700188 tests := mctx.CreateLocalVariations(testNames...)
Roland Levillainf2fad972019-06-28 15:41:19 +0100189 all_tests := tests[numTests]
190 all_tests.(*Module).linker.(testPerSrc).unsetSrc()
191 // Prevent the "all tests" variation from being installable nor
192 // exporting to Make, as it won't create any output file.
193 all_tests.(*Module).Properties.PreventInstall = true
194 all_tests.(*Module).Properties.HideFromMake = true
Colin Crossb916a382016-07-29 17:28:03 -0700195 for i, src := range test.srcs() {
196 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Roland Levillainf2fad972019-06-28 15:41:19 +0100197 mctx.AddInterVariantDependency(testPerSrcDepTag, all_tests, tests[i])
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198 }
199 }
200 }
201 }
202}
203
Jooyung Hana61ff2c2019-02-28 18:06:34 +0900204func checkDuplicate(values []string) (duplicate string, found bool) {
205 seen := make(map[string]string)
206 for _, v := range values {
207 if duplicate, found = seen[v]; found {
208 return
209 }
210 seen[v] = v
211 }
212 return
213}
214
Colin Crossb916a382016-07-29 17:28:03 -0700215type testDecorator struct {
216 Properties TestProperties
217 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700218}
219
Colin Cross600c9df2016-09-13 12:26:16 -0700220func (test *testDecorator) gtest() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700221 return BoolDefault(test.Properties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700222}
223
Colin Crossb916a382016-07-29 17:28:03 -0700224func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700225 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700226 return flags
227 }
228
229 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
230 if ctx.Host() {
231 flags.CFlags = append(flags.CFlags, "-O0", "-g")
232
233 switch ctx.Os() {
234 case android.Windows:
235 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
236 case android.Linux:
237 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700238 case android.Darwin:
239 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700240 }
241 } else {
242 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
243 }
244
245 return flags
246}
247
Colin Crossb916a382016-07-29 17:28:03 -0700248func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700249 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700250 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800251 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Christopher Ferris9df92d62018-08-21 12:40:08 -0700252 } else if BoolDefault(test.Properties.Isolated, false) {
253 deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main")
Christopher Ferris34cbba62019-07-17 15:46:29 -0700254 // The isolated library requires liblog, but adding it
255 // as a static library means unit tests cannot override
256 // liblog functions. Instead make it a shared library
257 // dependency.
258 deps.SharedLibs = append(deps.SharedLibs, "liblog")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700259 } else {
260 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
261 }
262 }
Colin Crossb916a382016-07-29 17:28:03 -0700263
Colin Cross4d9c2d12016-07-29 12:48:20 -0700264 return deps
265}
266
Colin Crossb916a382016-07-29 17:28:03 -0700267func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) {
yangbillb3174d12018-05-07 06:41:20 +0000268 // 1. Add ../../lib[64] to rpath so that out/host/linux-x86/nativetest/<test dir>/<test> can
Colin Crossbd75e1d2017-06-30 17:27:55 -0700269 // find out/host/linux-x86/lib[64]/library.so
yangbillb3174d12018-05-07 06:41:20 +0000270 // 2. Add ../../../lib[64] to rpath so that out/host/linux-x86/testcases/<test dir>/<CPU>/<test> can
271 // also find out/host/linux-x86/lib[64]/library.so
272 runpaths := []string{"../../lib", "../../../lib"}
273 for _, runpath := range runpaths {
274 if ctx.toolchain().Is64Bit() {
275 runpath += "64"
276 }
277 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700278 }
Colin Crossbd75e1d2017-06-30 17:27:55 -0700279
280 // add "" to rpath so that test binaries can find libraries in their own test directory
281 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700282}
283
Colin Crossb916a382016-07-29 17:28:03 -0700284func (test *testDecorator) linkerProps() []interface{} {
285 return []interface{}{&test.Properties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700286}
287
Colin Crossb916a382016-07-29 17:28:03 -0700288func NewTestInstaller() *baseInstaller {
289 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700290}
291
Colin Crossb916a382016-07-29 17:28:03 -0700292type testBinary struct {
293 testDecorator
294 *binaryDecorator
295 *baseCompiler
Colin Crossb916a382016-07-29 17:28:03 -0700296 Properties TestBinaryProperties
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800297 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700298 testConfig android.Path
Colin Crossb916a382016-07-29 17:28:03 -0700299}
300
301func (test *testBinary) linkerProps() []interface{} {
302 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
303 props = append(props, &test.Properties)
304 return props
305}
306
307func (test *testBinary) linkerInit(ctx BaseModuleContext) {
308 test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker)
309 test.binaryDecorator.linkerInit(ctx)
310}
311
Colin Cross37047f12016-12-13 17:06:13 -0800312func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700313 deps = test.testDecorator.linkerDeps(ctx, deps)
314 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700315 return deps
316}
317
Colin Crossb916a382016-07-29 17:28:03 -0700318func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
319 flags = test.binaryDecorator.linkerFlags(ctx, flags)
320 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700321 return flags
322}
323
Colin Crossb916a382016-07-29 17:28:03 -0700324func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Colin Cross8a497952019-03-05 22:25:09 -0800325 test.data = android.PathsForModuleSrc(ctx, test.Properties.Data)
nelsonli0d7111e2019-09-17 16:35:23 +0800326 var api_level_prop string
Dan Shi37ee3b82019-06-06 16:23:32 -0700327 var configs []tradefed.Config
nelsonli0d7111e2019-09-17 16:35:23 +0800328 var min_level string
Dan Shi37ee3b82019-06-06 16:23:32 -0700329 if Bool(test.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800330 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shibc0f2602019-09-17 02:43:50 +0000331 } else {
332 var options []tradefed.Option
333 options = append(options, tradefed.Option{"force-root", "false"})
nelsonli0d7111e2019-09-17 16:35:23 +0800334 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
Dan Shi20ccd212019-08-27 10:37:24 -0700335 }
336 if Bool(test.Properties.Disable_framework) {
337 var options []tradefed.Option
338 options = append(options, tradefed.Option{"run-command", "stop"})
339 options = append(options, tradefed.Option{"teardown-command", "start"})
nelsonli0d7111e2019-09-17 16:35:23 +0800340 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RunCommandTargetPreparer", options})
Julien Desprezeb7398e2019-02-28 08:45:28 -0800341 }
Dan Shi37ee3b82019-06-06 16:23:32 -0700342 if Bool(test.testDecorator.Properties.Isolated) {
343 configs = append(configs, tradefed.Option{"not-shardable", "true"})
344 }
yelinhsieh9fc60402018-10-01 19:23:14 +0800345 if test.Properties.Test_options.Run_test_as != nil {
Dan Shi37ee3b82019-06-06 16:23:32 -0700346 configs = append(configs, tradefed.Option{"run-test-as", String(test.Properties.Test_options.Run_test_as)})
yelinhsieh9fc60402018-10-01 19:23:14 +0800347 }
nelsonli0d7111e2019-09-17 16:35:23 +0800348 if test.Properties.Test_min_api_level != nil && test.Properties.Test_min_sdk_version != nil {
349 ctx.PropertyErrorf("test_min_api_level", "'test_min_api_level' and 'test_min_sdk_version' should not be set at the same time.")
350 } else if test.Properties.Test_min_api_level != nil {
351 api_level_prop = "ro.product.first_api_level"
352 min_level = strconv.FormatInt(int64(*test.Properties.Test_min_api_level), 10)
353 } else if test.Properties.Test_min_sdk_version != nil {
354 api_level_prop = "ro.build.version.sdk"
355 min_level = strconv.FormatInt(int64(*test.Properties.Test_min_sdk_version), 10)
356 }
357 if api_level_prop != "" {
358 var options []tradefed.Option
359 options = append(options, tradefed.Option{"min-api-level", min_level})
360 options = append(options, tradefed.Option{"api-level-prop", api_level_prop})
361 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.MinApiLevelModuleController", options})
362 }
yelinhsieh9fc60402018-10-01 19:23:14 +0800363
Jack He33338892018-09-19 02:21:28 -0700364 test.testConfig = tradefed.AutoGenNativeTestConfig(ctx, test.Properties.Test_config,
Dan Shi37ee3b82019-06-06 16:23:32 -0700365 test.Properties.Test_config_template, test.Properties.Test_suites, configs)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800366
Colin Cross600c9df2016-09-13 12:26:16 -0700367 test.binaryDecorator.baseInstaller.dir = "nativetest"
368 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800369
370 if !Bool(test.Properties.No_named_install_directory) {
371 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800372 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800373 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
374 }
375
Dan Willemsen1d577e22016-08-29 15:53:15 -0700376 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700377}
378
379func NewTest(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700380 module, binary := NewBinary(hod)
381 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700382 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700383
384 test := &testBinary{
385 testDecorator: testDecorator{
386 linker: binary.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700387 },
Colin Crossb916a382016-07-29 17:28:03 -0700388 binaryDecorator: binary,
389 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700390 }
Colin Crossb916a382016-07-29 17:28:03 -0700391 module.compiler = test
392 module.linker = test
393 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700394 return module
395}
396
Colin Crossb916a382016-07-29 17:28:03 -0700397type testLibrary struct {
398 testDecorator
399 *libraryDecorator
400}
401
402func (test *testLibrary) linkerProps() []interface{} {
403 return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...)
404}
405
406func (test *testLibrary) linkerInit(ctx BaseModuleContext) {
407 test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker)
408 test.libraryDecorator.linkerInit(ctx)
409}
410
Colin Cross37047f12016-12-13 17:06:13 -0800411func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700412 deps = test.testDecorator.linkerDeps(ctx, deps)
413 deps = test.libraryDecorator.linkerDeps(ctx, deps)
414 return deps
415}
416
417func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
418 flags = test.libraryDecorator.linkerFlags(ctx, flags)
419 flags = test.testDecorator.linkerFlags(ctx, flags)
420 return flags
421}
422
Colin Cross4d9c2d12016-07-29 12:48:20 -0700423func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800424 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700425 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700426 test := &testLibrary{
427 testDecorator: testDecorator{
428 linker: library.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700429 },
Colin Crossb916a382016-07-29 17:28:03 -0700430 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700431 }
Colin Crossb916a382016-07-29 17:28:03 -0700432 module.linker = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700433 return module
434}
435
Colin Crosse28f4e22017-04-24 18:10:29 -0700436type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700437 // list of files or filegroup modules that provide data that should be installed alongside
438 // the test
Colin Cross27b922f2019-03-04 22:35:41 -0800439 Data []string `android:"path"`
Anders Lewisb97e8182017-07-14 15:20:13 -0700440
Colin Crosse28f4e22017-04-24 18:10:29 -0700441 // list of compatibility suites (for example "cts", "vts") that the module should be
442 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700443 Test_suites []string `android:"arch_variant"`
444
445 // the name of the test configuration (for example "AndroidTest.xml") that should be
446 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800447 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700448
449 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
450 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800451 Test_config_template *string `android:"path,arch_variant"`
Dan Shi37ee3b82019-06-06 16:23:32 -0700452
453 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
454 // with root permission.
455 Require_root *bool
Colin Crosse28f4e22017-04-24 18:10:29 -0700456}
457
Colin Crossb916a382016-07-29 17:28:03 -0700458type benchmarkDecorator struct {
459 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700460 Properties BenchmarkProperties
Anders Lewisb97e8182017-07-14 15:20:13 -0700461 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700462 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700463}
464
Colin Crossb916a382016-07-29 17:28:03 -0700465func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) {
466 runpath := "../../lib"
467 if ctx.toolchain().Is64Bit() {
468 runpath += "64"
469 }
470 benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath)
471 benchmark.binaryDecorator.linkerInit(ctx)
472}
473
Colin Crosse28f4e22017-04-24 18:10:29 -0700474func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
475 props := benchmark.binaryDecorator.linkerProps()
476 props = append(props, &benchmark.Properties)
477 return props
478}
479
Colin Cross37047f12016-12-13 17:06:13 -0800480func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700481 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700482 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
483 return deps
484}
485
Colin Crossb916a382016-07-29 17:28:03 -0700486func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Colin Cross8a497952019-03-05 22:25:09 -0800487 benchmark.data = android.PathsForModuleSrc(ctx, benchmark.Properties.Data)
Dan Shi37ee3b82019-06-06 16:23:32 -0700488 var configs []tradefed.Config
489 if Bool(benchmark.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800490 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shi37ee3b82019-06-06 16:23:32 -0700491 }
Jack He33338892018-09-19 02:21:28 -0700492 benchmark.testConfig = tradefed.AutoGenNativeBenchmarkTestConfig(ctx, benchmark.Properties.Test_config,
Dan Shi37ee3b82019-06-06 16:23:32 -0700493 benchmark.Properties.Test_config_template, benchmark.Properties.Test_suites, configs)
Colin Cross303e21f2018-08-07 16:49:25 -0700494
Colin Cross28690e92017-09-08 16:20:30 -0700495 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
496 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Dan Willemsen1d577e22016-08-29 15:53:15 -0700497 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700498}
499
Colin Cross4d9c2d12016-07-29 12:48:20 -0700500func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700501 module, binary := NewBinary(hod)
502 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700503 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700504
505 benchmark := &benchmarkDecorator{
506 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700507 }
Colin Crossb916a382016-07-29 17:28:03 -0700508 module.linker = benchmark
509 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700510 return module
511}