blob: 0e66e288e07fa4713a30d63b4b0ea69a914c0341 [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
Dan Shi6ffaaa82019-09-26 11:41:36 -070083
84 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
85 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
86 // explicitly.
87 Auto_gen_config *bool
Colin Cross4d9c2d12016-07-29 12:48:20 -070088}
89
90func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -070091 android.RegisterModuleType("cc_test", TestFactory)
92 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
93 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
94 android.RegisterModuleType("cc_test_host", TestHostFactory)
95 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070096}
97
Patrice Arrudac249c712019-03-19 17:00:29 -070098// cc_test generates a test config file and an executable binary file to test
99// specific functionality on a device. The executable binary gets an implicit
100// static_libs dependency on libgtests unless the gtest flag is set to false.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700101func TestFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700102 module := NewTest(android.HostAndDeviceSupported)
103 return module.Init()
104}
105
Patrice Arrudac249c712019-03-19 17:00:29 -0700106// cc_test_library creates an archive of files (i.e. .o files) which is later
107// referenced by another module (such as cc_test, cc_defaults or cc_test_library)
108// for archiving or linking.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700109func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700110 module := NewTestLibrary(android.HostAndDeviceSupported)
111 return module.Init()
112}
113
Patrice Arrudac249c712019-03-19 17:00:29 -0700114// cc_benchmark compiles an executable binary that performs benchmark testing
115// of a specific component in a device. Additional files such as test suites
116// and test configuration are installed on the side of the compiled executed
117// binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700118func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700119 module := NewBenchmark(android.HostAndDeviceSupported)
120 return module.Init()
121}
122
Patrice Arrudac249c712019-03-19 17:00:29 -0700123// cc_test_host compiles a test host binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700124func TestHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700125 module := NewTest(android.HostSupported)
126 return module.Init()
127}
128
Patrice Arrudac249c712019-03-19 17:00:29 -0700129// cc_benchmark_host compiles an executable binary that performs benchmark
130// testing of a specific component in the host. Additional files such as
131// test suites and test configuration are installed on the side of the
132// compiled executed binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700133func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700134 module := NewBenchmark(android.HostSupported)
135 return module.Init()
136}
137
Colin Crossb916a382016-07-29 17:28:03 -0700138type testPerSrc interface {
139 testPerSrc() bool
140 srcs() []string
Roland Levillainf2fad972019-06-28 15:41:19 +0100141 isAllTestsVariation() bool
Colin Crossb916a382016-07-29 17:28:03 -0700142 setSrc(string, string)
Roland Levillainf2fad972019-06-28 15:41:19 +0100143 unsetSrc()
Colin Crossb916a382016-07-29 17:28:03 -0700144}
145
146func (test *testBinary) testPerSrc() bool {
147 return Bool(test.Properties.Test_per_src)
148}
149
150func (test *testBinary) srcs() []string {
151 return test.baseCompiler.Properties.Srcs
152}
153
Roland Levillainf2fad972019-06-28 15:41:19 +0100154func (test *testBinary) isAllTestsVariation() bool {
155 stem := test.binaryDecorator.Properties.Stem
156 return stem != nil && *stem == ""
157}
158
Colin Crossb916a382016-07-29 17:28:03 -0700159func (test *testBinary) setSrc(name, src string) {
160 test.baseCompiler.Properties.Srcs = []string{src}
Nan Zhang0007d812017-11-07 10:57:05 -0800161 test.binaryDecorator.Properties.Stem = StringPtr(name)
Colin Crossb916a382016-07-29 17:28:03 -0700162}
163
Roland Levillainf2fad972019-06-28 15:41:19 +0100164func (test *testBinary) unsetSrc() {
165 test.baseCompiler.Properties.Srcs = nil
166 test.binaryDecorator.Properties.Stem = StringPtr("")
167}
168
Colin Crossb916a382016-07-29 17:28:03 -0700169var _ testPerSrc = (*testBinary)(nil)
170
Roland Levillain9b5fde92019-06-28 15:41:19 +0100171func TestPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700172 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700173 if test, ok := m.linker.(testPerSrc); ok {
Roland Levillainf2fad972019-06-28 15:41:19 +0100174 numTests := len(test.srcs())
175 if test.testPerSrc() && numTests > 0 {
Jooyung Hana61ff2c2019-02-28 18:06:34 +0900176 if duplicate, found := checkDuplicate(test.srcs()); found {
177 mctx.PropertyErrorf("srcs", "found a duplicate entry %q", duplicate)
178 return
179 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100180 testNames := make([]string, numTests)
Colin Crossb916a382016-07-29 17:28:03 -0700181 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700182 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
183 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100184 // In addition to creating one variation per test source file,
185 // create an additional "all tests" variation named "", and have it
186 // depends on all other test_per_src variations. This is useful to
187 // create subsequent dependencies of a given module on all
188 // test_per_src variations created above: by depending on
189 // variation "", that module will transitively depend on all the
190 // other test_per_src variations without the need to know their
191 // name or even their number.
192 testNames = append(testNames, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700193 tests := mctx.CreateLocalVariations(testNames...)
Roland Levillainf2fad972019-06-28 15:41:19 +0100194 all_tests := tests[numTests]
195 all_tests.(*Module).linker.(testPerSrc).unsetSrc()
196 // Prevent the "all tests" variation from being installable nor
197 // exporting to Make, as it won't create any output file.
198 all_tests.(*Module).Properties.PreventInstall = true
199 all_tests.(*Module).Properties.HideFromMake = true
Colin Crossb916a382016-07-29 17:28:03 -0700200 for i, src := range test.srcs() {
201 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Roland Levillainf2fad972019-06-28 15:41:19 +0100202 mctx.AddInterVariantDependency(testPerSrcDepTag, all_tests, tests[i])
Colin Cross4d9c2d12016-07-29 12:48:20 -0700203 }
204 }
205 }
206 }
207}
208
Jooyung Hana61ff2c2019-02-28 18:06:34 +0900209func checkDuplicate(values []string) (duplicate string, found bool) {
210 seen := make(map[string]string)
211 for _, v := range values {
212 if duplicate, found = seen[v]; found {
213 return
214 }
215 seen[v] = v
216 }
217 return
218}
219
Colin Crossb916a382016-07-29 17:28:03 -0700220type testDecorator struct {
221 Properties TestProperties
222 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700223}
224
Colin Cross600c9df2016-09-13 12:26:16 -0700225func (test *testDecorator) gtest() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700226 return BoolDefault(test.Properties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700227}
228
Colin Crossb916a382016-07-29 17:28:03 -0700229func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700230 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700231 return flags
232 }
233
234 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
235 if ctx.Host() {
236 flags.CFlags = append(flags.CFlags, "-O0", "-g")
237
238 switch ctx.Os() {
239 case android.Windows:
240 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
241 case android.Linux:
242 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700243 case android.Darwin:
244 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700245 }
246 } else {
247 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
248 }
249
250 return flags
251}
252
Colin Crossb916a382016-07-29 17:28:03 -0700253func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700254 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700255 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800256 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Christopher Ferris9df92d62018-08-21 12:40:08 -0700257 } else if BoolDefault(test.Properties.Isolated, false) {
258 deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main")
Christopher Ferris34cbba62019-07-17 15:46:29 -0700259 // The isolated library requires liblog, but adding it
260 // as a static library means unit tests cannot override
261 // liblog functions. Instead make it a shared library
262 // dependency.
263 deps.SharedLibs = append(deps.SharedLibs, "liblog")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700264 } else {
265 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
266 }
267 }
Colin Crossb916a382016-07-29 17:28:03 -0700268
Colin Cross4d9c2d12016-07-29 12:48:20 -0700269 return deps
270}
271
Colin Crossb916a382016-07-29 17:28:03 -0700272func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) {
yangbillb3174d12018-05-07 06:41:20 +0000273 // 1. Add ../../lib[64] to rpath so that out/host/linux-x86/nativetest/<test dir>/<test> can
Colin Crossbd75e1d2017-06-30 17:27:55 -0700274 // find out/host/linux-x86/lib[64]/library.so
yangbillb3174d12018-05-07 06:41:20 +0000275 // 2. Add ../../../lib[64] to rpath so that out/host/linux-x86/testcases/<test dir>/<CPU>/<test> can
276 // also find out/host/linux-x86/lib[64]/library.so
277 runpaths := []string{"../../lib", "../../../lib"}
278 for _, runpath := range runpaths {
279 if ctx.toolchain().Is64Bit() {
280 runpath += "64"
281 }
282 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700283 }
Colin Crossbd75e1d2017-06-30 17:27:55 -0700284
285 // add "" to rpath so that test binaries can find libraries in their own test directory
286 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700287}
288
Colin Crossb916a382016-07-29 17:28:03 -0700289func (test *testDecorator) linkerProps() []interface{} {
290 return []interface{}{&test.Properties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700291}
292
Colin Crossb916a382016-07-29 17:28:03 -0700293func NewTestInstaller() *baseInstaller {
294 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700295}
296
Colin Crossb916a382016-07-29 17:28:03 -0700297type testBinary struct {
298 testDecorator
299 *binaryDecorator
300 *baseCompiler
Colin Crossb916a382016-07-29 17:28:03 -0700301 Properties TestBinaryProperties
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800302 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700303 testConfig android.Path
Colin Crossb916a382016-07-29 17:28:03 -0700304}
305
306func (test *testBinary) linkerProps() []interface{} {
307 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
308 props = append(props, &test.Properties)
309 return props
310}
311
312func (test *testBinary) linkerInit(ctx BaseModuleContext) {
313 test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker)
314 test.binaryDecorator.linkerInit(ctx)
315}
316
Colin Cross37047f12016-12-13 17:06:13 -0800317func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700318 deps = test.testDecorator.linkerDeps(ctx, deps)
319 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700320 return deps
321}
322
Colin Crossb916a382016-07-29 17:28:03 -0700323func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
324 flags = test.binaryDecorator.linkerFlags(ctx, flags)
325 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700326 return flags
327}
328
Colin Crossb916a382016-07-29 17:28:03 -0700329func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Colin Cross8a497952019-03-05 22:25:09 -0800330 test.data = android.PathsForModuleSrc(ctx, test.Properties.Data)
nelsonli0d7111e2019-09-17 16:35:23 +0800331 var api_level_prop string
Dan Shi37ee3b82019-06-06 16:23:32 -0700332 var configs []tradefed.Config
nelsonli0d7111e2019-09-17 16:35:23 +0800333 var min_level string
Dan Shi37ee3b82019-06-06 16:23:32 -0700334 if Bool(test.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800335 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shibc0f2602019-09-17 02:43:50 +0000336 } else {
337 var options []tradefed.Option
338 options = append(options, tradefed.Option{"force-root", "false"})
nelsonli0d7111e2019-09-17 16:35:23 +0800339 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
Dan Shi20ccd212019-08-27 10:37:24 -0700340 }
341 if Bool(test.Properties.Disable_framework) {
342 var options []tradefed.Option
343 options = append(options, tradefed.Option{"run-command", "stop"})
344 options = append(options, tradefed.Option{"teardown-command", "start"})
nelsonli0d7111e2019-09-17 16:35:23 +0800345 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RunCommandTargetPreparer", options})
Julien Desprezeb7398e2019-02-28 08:45:28 -0800346 }
Dan Shi37ee3b82019-06-06 16:23:32 -0700347 if Bool(test.testDecorator.Properties.Isolated) {
348 configs = append(configs, tradefed.Option{"not-shardable", "true"})
349 }
yelinhsieh9fc60402018-10-01 19:23:14 +0800350 if test.Properties.Test_options.Run_test_as != nil {
Dan Shi37ee3b82019-06-06 16:23:32 -0700351 configs = append(configs, tradefed.Option{"run-test-as", String(test.Properties.Test_options.Run_test_as)})
yelinhsieh9fc60402018-10-01 19:23:14 +0800352 }
nelsonli0d7111e2019-09-17 16:35:23 +0800353 if test.Properties.Test_min_api_level != nil && test.Properties.Test_min_sdk_version != nil {
354 ctx.PropertyErrorf("test_min_api_level", "'test_min_api_level' and 'test_min_sdk_version' should not be set at the same time.")
355 } else if test.Properties.Test_min_api_level != nil {
356 api_level_prop = "ro.product.first_api_level"
357 min_level = strconv.FormatInt(int64(*test.Properties.Test_min_api_level), 10)
358 } else if test.Properties.Test_min_sdk_version != nil {
359 api_level_prop = "ro.build.version.sdk"
360 min_level = strconv.FormatInt(int64(*test.Properties.Test_min_sdk_version), 10)
361 }
362 if api_level_prop != "" {
363 var options []tradefed.Option
364 options = append(options, tradefed.Option{"min-api-level", min_level})
365 options = append(options, tradefed.Option{"api-level-prop", api_level_prop})
366 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.MinApiLevelModuleController", options})
367 }
yelinhsieh9fc60402018-10-01 19:23:14 +0800368
Jack He33338892018-09-19 02:21:28 -0700369 test.testConfig = tradefed.AutoGenNativeTestConfig(ctx, test.Properties.Test_config,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700370 test.Properties.Test_config_template, test.Properties.Test_suites, configs, test.Properties.Auto_gen_config)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800371
Colin Cross600c9df2016-09-13 12:26:16 -0700372 test.binaryDecorator.baseInstaller.dir = "nativetest"
373 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800374
375 if !Bool(test.Properties.No_named_install_directory) {
376 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800377 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800378 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
379 }
380
Dan Willemsen1d577e22016-08-29 15:53:15 -0700381 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700382}
383
384func NewTest(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700385 module, binary := NewBinary(hod)
386 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700387 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700388
389 test := &testBinary{
390 testDecorator: testDecorator{
391 linker: binary.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700392 },
Colin Crossb916a382016-07-29 17:28:03 -0700393 binaryDecorator: binary,
394 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700395 }
Colin Crossb916a382016-07-29 17:28:03 -0700396 module.compiler = test
397 module.linker = test
398 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700399 return module
400}
401
Colin Crossb916a382016-07-29 17:28:03 -0700402type testLibrary struct {
403 testDecorator
404 *libraryDecorator
405}
406
407func (test *testLibrary) linkerProps() []interface{} {
408 return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...)
409}
410
411func (test *testLibrary) linkerInit(ctx BaseModuleContext) {
412 test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker)
413 test.libraryDecorator.linkerInit(ctx)
414}
415
Colin Cross37047f12016-12-13 17:06:13 -0800416func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700417 deps = test.testDecorator.linkerDeps(ctx, deps)
418 deps = test.libraryDecorator.linkerDeps(ctx, deps)
419 return deps
420}
421
422func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
423 flags = test.libraryDecorator.linkerFlags(ctx, flags)
424 flags = test.testDecorator.linkerFlags(ctx, flags)
425 return flags
426}
427
Colin Cross4d9c2d12016-07-29 12:48:20 -0700428func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800429 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700430 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700431 test := &testLibrary{
432 testDecorator: testDecorator{
433 linker: library.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700434 },
Colin Crossb916a382016-07-29 17:28:03 -0700435 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700436 }
Colin Crossb916a382016-07-29 17:28:03 -0700437 module.linker = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700438 return module
439}
440
Colin Crosse28f4e22017-04-24 18:10:29 -0700441type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700442 // list of files or filegroup modules that provide data that should be installed alongside
443 // the test
Colin Cross27b922f2019-03-04 22:35:41 -0800444 Data []string `android:"path"`
Anders Lewisb97e8182017-07-14 15:20:13 -0700445
Colin Crosse28f4e22017-04-24 18:10:29 -0700446 // list of compatibility suites (for example "cts", "vts") that the module should be
447 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700448 Test_suites []string `android:"arch_variant"`
449
450 // the name of the test configuration (for example "AndroidTest.xml") that should be
451 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800452 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700453
454 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
455 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800456 Test_config_template *string `android:"path,arch_variant"`
Dan Shi37ee3b82019-06-06 16:23:32 -0700457
458 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
459 // with root permission.
460 Require_root *bool
Dan Shi6ffaaa82019-09-26 11:41:36 -0700461
462 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
463 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
464 // explicitly.
465 Auto_gen_config *bool
Colin Crosse28f4e22017-04-24 18:10:29 -0700466}
467
Colin Crossb916a382016-07-29 17:28:03 -0700468type benchmarkDecorator struct {
469 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700470 Properties BenchmarkProperties
Anders Lewisb97e8182017-07-14 15:20:13 -0700471 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700472 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700473}
474
Colin Crossb916a382016-07-29 17:28:03 -0700475func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) {
476 runpath := "../../lib"
477 if ctx.toolchain().Is64Bit() {
478 runpath += "64"
479 }
480 benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath)
481 benchmark.binaryDecorator.linkerInit(ctx)
482}
483
Colin Crosse28f4e22017-04-24 18:10:29 -0700484func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
485 props := benchmark.binaryDecorator.linkerProps()
486 props = append(props, &benchmark.Properties)
487 return props
488}
489
Colin Cross37047f12016-12-13 17:06:13 -0800490func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700491 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700492 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
493 return deps
494}
495
Colin Crossb916a382016-07-29 17:28:03 -0700496func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Colin Cross8a497952019-03-05 22:25:09 -0800497 benchmark.data = android.PathsForModuleSrc(ctx, benchmark.Properties.Data)
Dan Shi37ee3b82019-06-06 16:23:32 -0700498 var configs []tradefed.Config
499 if Bool(benchmark.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800500 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shi37ee3b82019-06-06 16:23:32 -0700501 }
Jack He33338892018-09-19 02:21:28 -0700502 benchmark.testConfig = tradefed.AutoGenNativeBenchmarkTestConfig(ctx, benchmark.Properties.Test_config,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700503 benchmark.Properties.Test_config_template, benchmark.Properties.Test_suites, configs, benchmark.Properties.Auto_gen_config)
Colin Cross303e21f2018-08-07 16:49:25 -0700504
Colin Cross28690e92017-09-08 16:20:30 -0700505 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
506 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Dan Willemsen1d577e22016-08-29 15:53:15 -0700507 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700508}
509
Colin Cross4d9c2d12016-07-29 12:48:20 -0700510func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700511 module, binary := NewBinary(hod)
512 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700513 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700514
515 benchmark := &benchmarkDecorator{
516 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700517 }
Colin Crossb916a382016-07-29 17:28:03 -0700518 module.linker = benchmark
519 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700520 return module
521}