blob: b1f8eec537db829bc7cc6ff6f6b33861397ca1b0 [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
43}
44
Colin Crossb916a382016-07-29 17:28:03 -070045type TestBinaryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070046 // Create a separate binary for each source file. Useful when there is
47 // global state that can not be torn down and reset between each test suite.
48 Test_per_src *bool
Dan Willemsen3340d602016-12-27 14:40:40 -080049
50 // Disables the creation of a test-specific directory when used with
51 // relative_install_path. Useful if several tests need to be in the same
52 // directory, but test_per_src doesn't work.
53 No_named_install_directory *bool
Colin Crossfaeb7aa2017-02-01 14:12:44 -080054
55 // list of files or filegroup modules that provide data that should be installed alongside
56 // the test
Dan Shi67a88342020-02-25 16:34:39 -080057 Data []string `android:"path,arch_variant"`
Colin Crossa929db02017-03-27 16:27:50 -070058
59 // list of compatibility suites (for example "cts", "vts") that the module should be
60 // installed into.
Dan Willemsen15d54d52017-09-18 16:49:28 -070061 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -070062
63 // the name of the test configuration (for example "AndroidTest.xml") that should be
64 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -080065 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -070066
67 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
68 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -080069 Test_config_template *string `android:"path,arch_variant"`
yelinhsieh9fc60402018-10-01 19:23:14 +080070
71 // Test options.
72 Test_options TestOptions
Dan Shi37ee3b82019-06-06 16:23:32 -070073
74 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
75 // with root permission.
76 Require_root *bool
Dan Shi20ccd212019-08-27 10:37:24 -070077
78 // Add RunCommandTargetPreparer to stop framework before the test and start it after the test.
79 Disable_framework *bool
nelsonli0d7111e2019-09-17 16:35:23 +080080
81 // Add MinApiLevelModuleController to auto generated test config. If the device property of
82 // "ro.product.first_api_level" < Test_min_api_level, then skip this module.
83 Test_min_api_level *int64
84
85 // Add MinApiLevelModuleController to auto generated test config. If the device property of
86 // "ro.build.version.sdk" < Test_min_sdk_version, then skip this module.
87 Test_min_sdk_version *int64
Dan Shi6ffaaa82019-09-26 11:41:36 -070088
89 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
90 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
91 // explicitly.
92 Auto_gen_config *bool
easoncylee1e3fdcd2020-04-30 10:08:33 +080093
94 // Add parameterized mainline modules to auto generated test config. The options will be
95 // handled by TradeFed to download and install the specified modules on the device.
96 Test_mainline_modules []string
Colin Cross4d9c2d12016-07-29 12:48:20 -070097}
98
99func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700100 android.RegisterModuleType("cc_test", TestFactory)
101 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
102 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
103 android.RegisterModuleType("cc_test_host", TestHostFactory)
104 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700105}
106
Patrice Arrudac249c712019-03-19 17:00:29 -0700107// cc_test generates a test config file and an executable binary file to test
108// specific functionality on a device. The executable binary gets an implicit
109// static_libs dependency on libgtests unless the gtest flag is set to false.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700110func TestFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700111 module := NewTest(android.HostAndDeviceSupported)
112 return module.Init()
113}
114
Patrice Arrudac249c712019-03-19 17:00:29 -0700115// cc_test_library creates an archive of files (i.e. .o files) which is later
116// referenced by another module (such as cc_test, cc_defaults or cc_test_library)
117// for archiving or linking.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700118func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700119 module := NewTestLibrary(android.HostAndDeviceSupported)
120 return module.Init()
121}
122
Patrice Arrudac249c712019-03-19 17:00:29 -0700123// cc_benchmark compiles an executable binary that performs benchmark testing
124// of a specific component in a device. Additional files such as test suites
125// and test configuration are installed on the side of the compiled executed
126// binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700127func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700128 module := NewBenchmark(android.HostAndDeviceSupported)
129 return module.Init()
130}
131
Patrice Arrudac249c712019-03-19 17:00:29 -0700132// cc_test_host compiles a test host binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700133func TestHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700134 module := NewTest(android.HostSupported)
135 return module.Init()
136}
137
Patrice Arrudac249c712019-03-19 17:00:29 -0700138// cc_benchmark_host compiles an executable binary that performs benchmark
139// testing of a specific component in the host. Additional files such as
140// test suites and test configuration are installed on the side of the
141// compiled executed binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700142func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700143 module := NewBenchmark(android.HostSupported)
144 return module.Init()
145}
146
Colin Crossb916a382016-07-29 17:28:03 -0700147type testPerSrc interface {
148 testPerSrc() bool
149 srcs() []string
Roland Levillainf2fad972019-06-28 15:41:19 +0100150 isAllTestsVariation() bool
Colin Crossb916a382016-07-29 17:28:03 -0700151 setSrc(string, string)
Roland Levillainf2fad972019-06-28 15:41:19 +0100152 unsetSrc()
Colin Crossb916a382016-07-29 17:28:03 -0700153}
154
155func (test *testBinary) testPerSrc() bool {
156 return Bool(test.Properties.Test_per_src)
157}
158
159func (test *testBinary) srcs() []string {
160 return test.baseCompiler.Properties.Srcs
161}
162
Roland Levillainf2fad972019-06-28 15:41:19 +0100163func (test *testBinary) isAllTestsVariation() bool {
164 stem := test.binaryDecorator.Properties.Stem
165 return stem != nil && *stem == ""
166}
167
Colin Crossb916a382016-07-29 17:28:03 -0700168func (test *testBinary) setSrc(name, src string) {
169 test.baseCompiler.Properties.Srcs = []string{src}
Nan Zhang0007d812017-11-07 10:57:05 -0800170 test.binaryDecorator.Properties.Stem = StringPtr(name)
Colin Crossb916a382016-07-29 17:28:03 -0700171}
172
Roland Levillainf2fad972019-06-28 15:41:19 +0100173func (test *testBinary) unsetSrc() {
174 test.baseCompiler.Properties.Srcs = nil
175 test.binaryDecorator.Properties.Stem = StringPtr("")
176}
177
Colin Crossb916a382016-07-29 17:28:03 -0700178var _ testPerSrc = (*testBinary)(nil)
179
Roland Levillain9b5fde92019-06-28 15:41:19 +0100180func TestPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700181 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700182 if test, ok := m.linker.(testPerSrc); ok {
Roland Levillainf2fad972019-06-28 15:41:19 +0100183 numTests := len(test.srcs())
184 if test.testPerSrc() && numTests > 0 {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700185 if duplicate, found := android.CheckDuplicate(test.srcs()); found {
Jooyung Hana61ff2c2019-02-28 18:06:34 +0900186 mctx.PropertyErrorf("srcs", "found a duplicate entry %q", duplicate)
187 return
188 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100189 testNames := make([]string, numTests)
Colin Crossb916a382016-07-29 17:28:03 -0700190 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700191 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
192 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100193 // In addition to creating one variation per test source file,
194 // create an additional "all tests" variation named "", and have it
195 // depends on all other test_per_src variations. This is useful to
196 // create subsequent dependencies of a given module on all
197 // test_per_src variations created above: by depending on
198 // variation "", that module will transitively depend on all the
199 // other test_per_src variations without the need to know their
200 // name or even their number.
201 testNames = append(testNames, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700202 tests := mctx.CreateLocalVariations(testNames...)
Roland Levillainf2fad972019-06-28 15:41:19 +0100203 all_tests := tests[numTests]
204 all_tests.(*Module).linker.(testPerSrc).unsetSrc()
205 // Prevent the "all tests" variation from being installable nor
206 // exporting to Make, as it won't create any output file.
207 all_tests.(*Module).Properties.PreventInstall = true
208 all_tests.(*Module).Properties.HideFromMake = true
Colin Crossb916a382016-07-29 17:28:03 -0700209 for i, src := range test.srcs() {
210 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Roland Levillainf2fad972019-06-28 15:41:19 +0100211 mctx.AddInterVariantDependency(testPerSrcDepTag, all_tests, tests[i])
Colin Cross4d9c2d12016-07-29 12:48:20 -0700212 }
213 }
214 }
215 }
216}
217
Colin Crossb916a382016-07-29 17:28:03 -0700218type testDecorator struct {
219 Properties TestProperties
220 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700221}
222
Colin Cross600c9df2016-09-13 12:26:16 -0700223func (test *testDecorator) gtest() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700224 return BoolDefault(test.Properties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700225}
226
Jiyong Park62304bb2020-04-13 16:19:48 +0900227func (test *testDecorator) testFor() []string {
228 return test.Properties.Test_for
229}
230
Colin Crossb916a382016-07-29 17:28:03 -0700231func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700232 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700233 return flags
234 }
235
Colin Cross4af21ed2019-11-04 09:37:55 -0800236 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700237 if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800238 flags.Local.CFlags = append(flags.Local.CFlags, "-O0", "-g")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700239
240 switch ctx.Os() {
241 case android.Windows:
Colin Cross4af21ed2019-11-04 09:37:55 -0800242 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700243 case android.Linux:
Colin Cross4af21ed2019-11-04 09:37:55 -0800244 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700245 case android.Darwin:
Colin Cross4af21ed2019-11-04 09:37:55 -0800246 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700247 }
248 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800249 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700250 }
251
252 return flags
253}
254
Colin Crossb916a382016-07-29 17:28:03 -0700255func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700256 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700257 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800258 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Christopher Ferris9df92d62018-08-21 12:40:08 -0700259 } else if BoolDefault(test.Properties.Isolated, false) {
260 deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main")
Christopher Ferris34cbba62019-07-17 15:46:29 -0700261 // The isolated library requires liblog, but adding it
262 // as a static library means unit tests cannot override
263 // liblog functions. Instead make it a shared library
264 // dependency.
265 deps.SharedLibs = append(deps.SharedLibs, "liblog")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700266 } else {
267 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
268 }
269 }
Colin Crossb916a382016-07-29 17:28:03 -0700270
Colin Cross4d9c2d12016-07-29 12:48:20 -0700271 return deps
272}
273
Colin Crossb916a382016-07-29 17:28:03 -0700274func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) {
yangbillb3174d12018-05-07 06:41:20 +0000275 // 1. Add ../../lib[64] to rpath so that out/host/linux-x86/nativetest/<test dir>/<test> can
Colin Crossbd75e1d2017-06-30 17:27:55 -0700276 // find out/host/linux-x86/lib[64]/library.so
yangbillb3174d12018-05-07 06:41:20 +0000277 // 2. Add ../../../lib[64] to rpath so that out/host/linux-x86/testcases/<test dir>/<CPU>/<test> can
278 // also find out/host/linux-x86/lib[64]/library.so
279 runpaths := []string{"../../lib", "../../../lib"}
280 for _, runpath := range runpaths {
281 if ctx.toolchain().Is64Bit() {
282 runpath += "64"
283 }
284 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700285 }
Colin Crossbd75e1d2017-06-30 17:27:55 -0700286
287 // add "" to rpath so that test binaries can find libraries in their own test directory
288 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700289}
290
Colin Crossb916a382016-07-29 17:28:03 -0700291func (test *testDecorator) linkerProps() []interface{} {
292 return []interface{}{&test.Properties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700293}
294
Colin Crossb916a382016-07-29 17:28:03 -0700295func NewTestInstaller() *baseInstaller {
296 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700297}
298
Colin Crossb916a382016-07-29 17:28:03 -0700299type testBinary struct {
300 testDecorator
301 *binaryDecorator
302 *baseCompiler
Colin Crossb916a382016-07-29 17:28:03 -0700303 Properties TestBinaryProperties
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800304 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700305 testConfig android.Path
Colin Crossb916a382016-07-29 17:28:03 -0700306}
307
308func (test *testBinary) linkerProps() []interface{} {
309 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
310 props = append(props, &test.Properties)
311 return props
312}
313
314func (test *testBinary) linkerInit(ctx BaseModuleContext) {
315 test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker)
316 test.binaryDecorator.linkerInit(ctx)
317}
318
Colin Cross37047f12016-12-13 17:06:13 -0800319func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700320 deps = test.testDecorator.linkerDeps(ctx, deps)
321 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700322 return deps
323}
324
Colin Crossb916a382016-07-29 17:28:03 -0700325func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
326 flags = test.binaryDecorator.linkerFlags(ctx, flags)
327 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700328 return flags
329}
330
Colin Crossb916a382016-07-29 17:28:03 -0700331func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Colin Cross8a497952019-03-05 22:25:09 -0800332 test.data = android.PathsForModuleSrc(ctx, test.Properties.Data)
nelsonli0d7111e2019-09-17 16:35:23 +0800333 var api_level_prop string
Dan Shi37ee3b82019-06-06 16:23:32 -0700334 var configs []tradefed.Config
nelsonli0d7111e2019-09-17 16:35:23 +0800335 var min_level string
easoncylee1e3fdcd2020-04-30 10:08:33 +0800336 for _, module := range test.Properties.Test_mainline_modules {
337 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module})
338 }
Dan Shi37ee3b82019-06-06 16:23:32 -0700339 if Bool(test.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800340 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shibc0f2602019-09-17 02:43:50 +0000341 } else {
342 var options []tradefed.Option
easoncylee1e3fdcd2020-04-30 10:08:33 +0800343 options = append(options, tradefed.Option{Name: "force-root", Value: "false"})
nelsonli0d7111e2019-09-17 16:35:23 +0800344 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
Dan Shi20ccd212019-08-27 10:37:24 -0700345 }
346 if Bool(test.Properties.Disable_framework) {
347 var options []tradefed.Option
easoncylee1e3fdcd2020-04-30 10:08:33 +0800348 options = append(options, tradefed.Option{Name: "run-command", Value: "stop"})
349 options = append(options, tradefed.Option{Name: "teardown-command", Value: "start"})
nelsonli0d7111e2019-09-17 16:35:23 +0800350 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RunCommandTargetPreparer", options})
Julien Desprezeb7398e2019-02-28 08:45:28 -0800351 }
Dan Shi37ee3b82019-06-06 16:23:32 -0700352 if Bool(test.testDecorator.Properties.Isolated) {
easoncylee1e3fdcd2020-04-30 10:08:33 +0800353 configs = append(configs, tradefed.Option{Name: "not-shardable", Value: "true"})
Dan Shi37ee3b82019-06-06 16:23:32 -0700354 }
yelinhsieh9fc60402018-10-01 19:23:14 +0800355 if test.Properties.Test_options.Run_test_as != nil {
easoncylee1e3fdcd2020-04-30 10:08:33 +0800356 configs = append(configs, tradefed.Option{Name: "run-test-as", Value: String(test.Properties.Test_options.Run_test_as)})
yelinhsieh9fc60402018-10-01 19:23:14 +0800357 }
nelsonli0d7111e2019-09-17 16:35:23 +0800358 if test.Properties.Test_min_api_level != nil && test.Properties.Test_min_sdk_version != nil {
359 ctx.PropertyErrorf("test_min_api_level", "'test_min_api_level' and 'test_min_sdk_version' should not be set at the same time.")
360 } else if test.Properties.Test_min_api_level != nil {
361 api_level_prop = "ro.product.first_api_level"
362 min_level = strconv.FormatInt(int64(*test.Properties.Test_min_api_level), 10)
363 } else if test.Properties.Test_min_sdk_version != nil {
364 api_level_prop = "ro.build.version.sdk"
365 min_level = strconv.FormatInt(int64(*test.Properties.Test_min_sdk_version), 10)
366 }
367 if api_level_prop != "" {
368 var options []tradefed.Option
easoncylee1e3fdcd2020-04-30 10:08:33 +0800369 options = append(options, tradefed.Option{Name: "min-api-level", Value: min_level})
370 options = append(options, tradefed.Option{Name: "api-level-prop", Value: api_level_prop})
nelsonli0d7111e2019-09-17 16:35:23 +0800371 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.MinApiLevelModuleController", options})
372 }
yelinhsieh9fc60402018-10-01 19:23:14 +0800373
Jack He33338892018-09-19 02:21:28 -0700374 test.testConfig = tradefed.AutoGenNativeTestConfig(ctx, test.Properties.Test_config,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700375 test.Properties.Test_config_template, test.Properties.Test_suites, configs, test.Properties.Auto_gen_config)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800376
Colin Cross600c9df2016-09-13 12:26:16 -0700377 test.binaryDecorator.baseInstaller.dir = "nativetest"
378 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800379
380 if !Bool(test.Properties.No_named_install_directory) {
381 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800382 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800383 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
384 }
385
Dan Willemsen1d577e22016-08-29 15:53:15 -0700386 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700387}
388
389func NewTest(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700390 module, binary := NewBinary(hod)
391 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700392 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700393
394 test := &testBinary{
395 testDecorator: testDecorator{
396 linker: binary.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700397 },
Colin Crossb916a382016-07-29 17:28:03 -0700398 binaryDecorator: binary,
399 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700400 }
Colin Crossb916a382016-07-29 17:28:03 -0700401 module.compiler = test
402 module.linker = test
403 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700404 return module
405}
406
Colin Crossb916a382016-07-29 17:28:03 -0700407type testLibrary struct {
408 testDecorator
409 *libraryDecorator
410}
411
412func (test *testLibrary) linkerProps() []interface{} {
413 return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...)
414}
415
416func (test *testLibrary) linkerInit(ctx BaseModuleContext) {
417 test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker)
418 test.libraryDecorator.linkerInit(ctx)
419}
420
Colin Cross37047f12016-12-13 17:06:13 -0800421func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700422 deps = test.testDecorator.linkerDeps(ctx, deps)
423 deps = test.libraryDecorator.linkerDeps(ctx, deps)
424 return deps
425}
426
427func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
428 flags = test.libraryDecorator.linkerFlags(ctx, flags)
429 flags = test.testDecorator.linkerFlags(ctx, flags)
430 return flags
431}
432
Colin Cross4d9c2d12016-07-29 12:48:20 -0700433func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800434 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700435 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700436 test := &testLibrary{
437 testDecorator: testDecorator{
438 linker: library.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700439 },
Colin Crossb916a382016-07-29 17:28:03 -0700440 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700441 }
Colin Crossb916a382016-07-29 17:28:03 -0700442 module.linker = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700443 return module
444}
445
Colin Crosse28f4e22017-04-24 18:10:29 -0700446type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700447 // list of files or filegroup modules that provide data that should be installed alongside
448 // the test
Colin Cross27b922f2019-03-04 22:35:41 -0800449 Data []string `android:"path"`
Anders Lewisb97e8182017-07-14 15:20:13 -0700450
Colin Crosse28f4e22017-04-24 18:10:29 -0700451 // list of compatibility suites (for example "cts", "vts") that the module should be
452 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700453 Test_suites []string `android:"arch_variant"`
454
455 // the name of the test configuration (for example "AndroidTest.xml") that should be
456 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800457 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700458
459 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
460 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800461 Test_config_template *string `android:"path,arch_variant"`
Dan Shi37ee3b82019-06-06 16:23:32 -0700462
463 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
464 // with root permission.
465 Require_root *bool
Dan Shi6ffaaa82019-09-26 11:41:36 -0700466
467 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
468 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
469 // explicitly.
470 Auto_gen_config *bool
Colin Crosse28f4e22017-04-24 18:10:29 -0700471}
472
Colin Crossb916a382016-07-29 17:28:03 -0700473type benchmarkDecorator struct {
474 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700475 Properties BenchmarkProperties
Anders Lewisb97e8182017-07-14 15:20:13 -0700476 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700477 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700478}
479
Colin Crossb916a382016-07-29 17:28:03 -0700480func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) {
481 runpath := "../../lib"
482 if ctx.toolchain().Is64Bit() {
483 runpath += "64"
484 }
485 benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath)
486 benchmark.binaryDecorator.linkerInit(ctx)
487}
488
Colin Crosse28f4e22017-04-24 18:10:29 -0700489func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
490 props := benchmark.binaryDecorator.linkerProps()
491 props = append(props, &benchmark.Properties)
492 return props
493}
494
Colin Cross37047f12016-12-13 17:06:13 -0800495func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700496 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700497 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
498 return deps
499}
500
Colin Crossb916a382016-07-29 17:28:03 -0700501func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Colin Cross8a497952019-03-05 22:25:09 -0800502 benchmark.data = android.PathsForModuleSrc(ctx, benchmark.Properties.Data)
Dan Shi37ee3b82019-06-06 16:23:32 -0700503 var configs []tradefed.Config
504 if Bool(benchmark.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800505 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shi37ee3b82019-06-06 16:23:32 -0700506 }
Jack He33338892018-09-19 02:21:28 -0700507 benchmark.testConfig = tradefed.AutoGenNativeBenchmarkTestConfig(ctx, benchmark.Properties.Test_config,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700508 benchmark.Properties.Test_config_template, benchmark.Properties.Test_suites, configs, benchmark.Properties.Auto_gen_config)
Colin Cross303e21f2018-08-07 16:49:25 -0700509
Colin Cross28690e92017-09-08 16:20:30 -0700510 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
511 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Dan Willemsen1d577e22016-08-29 15:53:15 -0700512 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700513}
514
Colin Cross4d9c2d12016-07-29 12:48:20 -0700515func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700516 module, binary := NewBinary(hod)
517 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700518 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700519
520 benchmark := &benchmarkDecorator{
521 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700522 }
Colin Crossb916a382016-07-29 17:28:03 -0700523 module.linker = benchmark
524 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700525 return module
526}