blob: 9b77e45da0b91be4fd8abc3757f731002a070b02 [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
Julien Desprez3b933d32021-01-14 11:49:15 -080022 "github.com/google/blueprint/proptools"
23
Colin Cross4d9c2d12016-07-29 12:48:20 -070024 "android/soong/android"
Colin Cross303e21f2018-08-07 16:49:25 -070025 "android/soong/tradefed"
Colin Cross4d9c2d12016-07-29 12:48:20 -070026)
27
Colin Crossb916a382016-07-29 17:28:03 -070028type TestProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070029 // if set, build against the gtest library. Defaults to true.
Colin Cross600c9df2016-09-13 12:26:16 -070030 Gtest *bool
Christopher Ferris9df92d62018-08-21 12:40:08 -070031
32 // if set, use the isolated gtest runner. Defaults to false.
33 Isolated *bool
Colin Crossb916a382016-07-29 17:28:03 -070034}
Colin Cross4d9c2d12016-07-29 12:48:20 -070035
yelinhsieh9fc60402018-10-01 19:23:14 +080036// Test option struct.
37type TestOptions struct {
38 // The UID that you want to run the test as on a device.
39 Run_test_as *string
Dan Shi95d19422020-08-15 12:24:26 -070040
David Srbecky519db272020-06-18 18:07:00 +010041 // A list of free-formed strings without spaces that categorize the test.
42 Test_suite_tag []string
Dan Shi95d19422020-08-15 12:24:26 -070043
44 // a list of extra test configuration files that should be installed with the module.
45 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -080046
47 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
48 Unit_test *bool
yelinhsieh9fc60402018-10-01 19:23:14 +080049}
50
Colin Crossb916a382016-07-29 17:28:03 -070051type TestBinaryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070052 // Create a separate binary for each source file. Useful when there is
53 // global state that can not be torn down and reset between each test suite.
54 Test_per_src *bool
Dan Willemsen3340d602016-12-27 14:40:40 -080055
56 // Disables the creation of a test-specific directory when used with
57 // relative_install_path. Useful if several tests need to be in the same
58 // directory, but test_per_src doesn't work.
59 No_named_install_directory *bool
Colin Crossfaeb7aa2017-02-01 14:12:44 -080060
61 // list of files or filegroup modules that provide data that should be installed alongside
62 // the test
Dan Shi67a88342020-02-25 16:34:39 -080063 Data []string `android:"path,arch_variant"`
Colin Crossa929db02017-03-27 16:27:50 -070064
Chris Parsons79d66a52020-06-05 17:26:16 -040065 // list of shared library modules that should be installed alongside the test
66 Data_libs []string `android:"arch_variant"`
67
Colin Crossa929db02017-03-27 16:27:50 -070068 // list of compatibility suites (for example "cts", "vts") that the module should be
69 // installed into.
Dan Willemsen15d54d52017-09-18 16:49:28 -070070 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -070071
72 // the name of the test configuration (for example "AndroidTest.xml") that should be
73 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -080074 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -070075
76 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
77 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -080078 Test_config_template *string `android:"path,arch_variant"`
yelinhsieh9fc60402018-10-01 19:23:14 +080079
80 // Test options.
81 Test_options TestOptions
Dan Shi37ee3b82019-06-06 16:23:32 -070082
83 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
84 // with root permission.
85 Require_root *bool
Dan Shi20ccd212019-08-27 10:37:24 -070086
87 // Add RunCommandTargetPreparer to stop framework before the test and start it after the test.
88 Disable_framework *bool
nelsonli0d7111e2019-09-17 16:35:23 +080089
Justin Yun107a4cb2021-02-10 13:46:50 +090090 // Add ShippingApiLevelModuleController to auto generated test config. If the device properties
91 // for the shipping api level is less than the test_min_api_level, skip this module.
nelsonli0d7111e2019-09-17 16:35:23 +080092 Test_min_api_level *int64
93
Dan Shi6ffaaa82019-09-26 11:41:36 -070094 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
95 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
96 // explicitly.
97 Auto_gen_config *bool
easoncylee1e3fdcd2020-04-30 10:08:33 +080098
99 // Add parameterized mainline modules to auto generated test config. The options will be
100 // handled by TradeFed to download and install the specified modules on the device.
101 Test_mainline_modules []string
Colin Cross4d9c2d12016-07-29 12:48:20 -0700102}
103
104func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700105 android.RegisterModuleType("cc_test", TestFactory)
106 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
107 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
108 android.RegisterModuleType("cc_test_host", TestHostFactory)
109 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700110}
111
Patrice Arrudac249c712019-03-19 17:00:29 -0700112// cc_test generates a test config file and an executable binary file to test
113// specific functionality on a device. The executable binary gets an implicit
114// static_libs dependency on libgtests unless the gtest flag is set to false.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700115func TestFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700116 module := NewTest(android.HostAndDeviceSupported)
117 return module.Init()
118}
119
Patrice Arrudac249c712019-03-19 17:00:29 -0700120// cc_test_library creates an archive of files (i.e. .o files) which is later
121// referenced by another module (such as cc_test, cc_defaults or cc_test_library)
122// for archiving or linking.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700123func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700124 module := NewTestLibrary(android.HostAndDeviceSupported)
125 return module.Init()
126}
127
Patrice Arrudac249c712019-03-19 17:00:29 -0700128// cc_benchmark compiles an executable binary that performs benchmark testing
129// of a specific component in a device. Additional files such as test suites
130// and test configuration are installed on the side of the compiled executed
131// binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700132func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700133 module := NewBenchmark(android.HostAndDeviceSupported)
134 return module.Init()
135}
136
Patrice Arrudac249c712019-03-19 17:00:29 -0700137// cc_test_host compiles a test host binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700138func TestHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700139 module := NewTest(android.HostSupported)
140 return module.Init()
141}
142
Patrice Arrudac249c712019-03-19 17:00:29 -0700143// cc_benchmark_host compiles an executable binary that performs benchmark
144// testing of a specific component in the host. Additional files such as
145// test suites and test configuration are installed on the side of the
146// compiled executed binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700147func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148 module := NewBenchmark(android.HostSupported)
149 return module.Init()
150}
151
Colin Crossb916a382016-07-29 17:28:03 -0700152type testPerSrc interface {
153 testPerSrc() bool
154 srcs() []string
Roland Levillainf2fad972019-06-28 15:41:19 +0100155 isAllTestsVariation() bool
Colin Crossb916a382016-07-29 17:28:03 -0700156 setSrc(string, string)
Roland Levillainf2fad972019-06-28 15:41:19 +0100157 unsetSrc()
Colin Crossb916a382016-07-29 17:28:03 -0700158}
159
160func (test *testBinary) testPerSrc() bool {
161 return Bool(test.Properties.Test_per_src)
162}
163
164func (test *testBinary) srcs() []string {
165 return test.baseCompiler.Properties.Srcs
166}
167
Chris Parsons216e10a2020-07-09 17:12:52 -0400168func (test *testBinary) dataPaths() []android.DataPath {
Liz Kammer1c14a212020-05-12 15:26:55 -0700169 return test.data
170}
171
Roland Levillainf2fad972019-06-28 15:41:19 +0100172func (test *testBinary) isAllTestsVariation() bool {
173 stem := test.binaryDecorator.Properties.Stem
174 return stem != nil && *stem == ""
175}
176
Colin Crossb916a382016-07-29 17:28:03 -0700177func (test *testBinary) setSrc(name, src string) {
178 test.baseCompiler.Properties.Srcs = []string{src}
Nan Zhang0007d812017-11-07 10:57:05 -0800179 test.binaryDecorator.Properties.Stem = StringPtr(name)
Colin Crossb916a382016-07-29 17:28:03 -0700180}
181
Roland Levillainf2fad972019-06-28 15:41:19 +0100182func (test *testBinary) unsetSrc() {
183 test.baseCompiler.Properties.Srcs = nil
184 test.binaryDecorator.Properties.Stem = StringPtr("")
185}
186
Colin Crossb916a382016-07-29 17:28:03 -0700187var _ testPerSrc = (*testBinary)(nil)
188
Roland Levillain9b5fde92019-06-28 15:41:19 +0100189func TestPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700190 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700191 if test, ok := m.linker.(testPerSrc); ok {
Roland Levillainf2fad972019-06-28 15:41:19 +0100192 numTests := len(test.srcs())
193 if test.testPerSrc() && numTests > 0 {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700194 if duplicate, found := android.CheckDuplicate(test.srcs()); found {
Jooyung Hana61ff2c2019-02-28 18:06:34 +0900195 mctx.PropertyErrorf("srcs", "found a duplicate entry %q", duplicate)
196 return
197 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100198 testNames := make([]string, numTests)
Colin Crossb916a382016-07-29 17:28:03 -0700199 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700200 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
201 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100202 // In addition to creating one variation per test source file,
203 // create an additional "all tests" variation named "", and have it
204 // depends on all other test_per_src variations. This is useful to
205 // create subsequent dependencies of a given module on all
206 // test_per_src variations created above: by depending on
207 // variation "", that module will transitively depend on all the
208 // other test_per_src variations without the need to know their
209 // name or even their number.
210 testNames = append(testNames, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700211 tests := mctx.CreateLocalVariations(testNames...)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800212 allTests := tests[numTests]
213 allTests.(*Module).linker.(testPerSrc).unsetSrc()
Roland Levillainf2fad972019-06-28 15:41:19 +0100214 // Prevent the "all tests" variation from being installable nor
215 // exporting to Make, as it won't create any output file.
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800216 allTests.(*Module).Properties.PreventInstall = true
217 allTests.(*Module).Properties.HideFromMake = true
Colin Crossb916a382016-07-29 17:28:03 -0700218 for i, src := range test.srcs() {
219 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800220 mctx.AddInterVariantDependency(testPerSrcDepTag, allTests, tests[i])
Colin Cross4d9c2d12016-07-29 12:48:20 -0700221 }
Colin Cross90dab342020-08-21 15:55:50 -0700222 mctx.AliasVariation("")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700223 }
224 }
225 }
226}
227
Colin Crossb916a382016-07-29 17:28:03 -0700228type testDecorator struct {
229 Properties TestProperties
230 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700231}
232
Colin Cross600c9df2016-09-13 12:26:16 -0700233func (test *testDecorator) gtest() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700234 return BoolDefault(test.Properties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700235}
236
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -0700237func (test *testDecorator) testBinary() bool {
238 return true
239}
240
Colin Crossb916a382016-07-29 17:28:03 -0700241func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700242 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700243 return flags
244 }
245
Colin Cross4af21ed2019-11-04 09:37:55 -0800246 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700247 if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800248 flags.Local.CFlags = append(flags.Local.CFlags, "-O0", "-g")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700249
250 switch ctx.Os() {
251 case android.Windows:
Colin Cross4af21ed2019-11-04 09:37:55 -0800252 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253 case android.Linux:
Colin Cross4af21ed2019-11-04 09:37:55 -0800254 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255 case android.Darwin:
Colin Cross4af21ed2019-11-04 09:37:55 -0800256 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700257 }
258 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800259 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700260 }
261
262 return flags
263}
264
Colin Crossb916a382016-07-29 17:28:03 -0700265func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700266 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700267 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800268 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Christopher Ferris9df92d62018-08-21 12:40:08 -0700269 } else if BoolDefault(test.Properties.Isolated, false) {
270 deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main")
Christopher Ferris34cbba62019-07-17 15:46:29 -0700271 // The isolated library requires liblog, but adding it
272 // as a static library means unit tests cannot override
273 // liblog functions. Instead make it a shared library
274 // dependency.
275 deps.SharedLibs = append(deps.SharedLibs, "liblog")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700276 } else {
277 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
278 }
279 }
Colin Crossb916a382016-07-29 17:28:03 -0700280
Colin Cross4d9c2d12016-07-29 12:48:20 -0700281 return deps
282}
283
Colin Crossb916a382016-07-29 17:28:03 -0700284func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) {
yangbillb3174d12018-05-07 06:41:20 +0000285 // 1. Add ../../lib[64] to rpath so that out/host/linux-x86/nativetest/<test dir>/<test> can
Colin Crossbd75e1d2017-06-30 17:27:55 -0700286 // find out/host/linux-x86/lib[64]/library.so
yangbillb3174d12018-05-07 06:41:20 +0000287 // 2. Add ../../../lib[64] to rpath so that out/host/linux-x86/testcases/<test dir>/<CPU>/<test> can
288 // also find out/host/linux-x86/lib[64]/library.so
289 runpaths := []string{"../../lib", "../../../lib"}
290 for _, runpath := range runpaths {
291 if ctx.toolchain().Is64Bit() {
292 runpath += "64"
293 }
294 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700295 }
Colin Crossbd75e1d2017-06-30 17:27:55 -0700296
297 // add "" to rpath so that test binaries can find libraries in their own test directory
298 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700299}
300
Colin Crossb916a382016-07-29 17:28:03 -0700301func (test *testDecorator) linkerProps() []interface{} {
302 return []interface{}{&test.Properties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700303}
304
Colin Crossb916a382016-07-29 17:28:03 -0700305func NewTestInstaller() *baseInstaller {
306 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700307}
308
Colin Crossb916a382016-07-29 17:28:03 -0700309type testBinary struct {
310 testDecorator
311 *binaryDecorator
312 *baseCompiler
Dan Shi95d19422020-08-15 12:24:26 -0700313 Properties TestBinaryProperties
314 data []android.DataPath
315 testConfig android.Path
316 extraTestConfigs android.Paths
Colin Crossb916a382016-07-29 17:28:03 -0700317}
318
319func (test *testBinary) linkerProps() []interface{} {
320 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
321 props = append(props, &test.Properties)
322 return props
323}
324
325func (test *testBinary) linkerInit(ctx BaseModuleContext) {
326 test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker)
327 test.binaryDecorator.linkerInit(ctx)
328}
329
Colin Cross37047f12016-12-13 17:06:13 -0800330func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700331 deps = test.testDecorator.linkerDeps(ctx, deps)
332 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Chris Parsons79d66a52020-06-05 17:26:16 -0400333 deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700334 return deps
335}
336
Colin Crossb916a382016-07-29 17:28:03 -0700337func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
338 flags = test.binaryDecorator.linkerFlags(ctx, flags)
339 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700340 return flags
341}
342
Colin Crossb916a382016-07-29 17:28:03 -0700343func (test *testBinary) install(ctx ModuleContext, file android.Path) {
yangbill5ec45552020-08-13 16:16:56 +0800344 // TODO: (b/167308193) Switch to /data/local/tests/unrestricted as the default install base.
345 testInstallBase := "/data/local/tmp"
346 if ctx.inVendor() || ctx.useVndk() {
347 testInstallBase = "/data/local/tests/vendor"
348 }
349
Chris Parsons216e10a2020-07-09 17:12:52 -0400350 dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
351
352 for _, dataSrcPath := range dataSrcPaths {
353 test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
354 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400355
356 ctx.VisitDirectDepsWithTag(dataLibDepTag, func(dep android.Module) {
357 depName := ctx.OtherModuleName(dep)
358 ccDep, ok := dep.(LinkableInterface)
359
360 if !ok {
361 ctx.ModuleErrorf("data_lib %q is not a linkable cc module", depName)
362 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400363 ccModule, ok := dep.(*Module)
364 if !ok {
365 ctx.ModuleErrorf("data_lib %q is not a cc module", depName)
366 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400367 if ccDep.OutputFile().Valid() {
Chris Parsons216e10a2020-07-09 17:12:52 -0400368 test.data = append(test.data,
369 android.DataPath{SrcPath: ccDep.OutputFile().Path(),
370 RelativeInstallPath: ccModule.installer.relativeInstallPath()})
Chris Parsons79d66a52020-06-05 17:26:16 -0400371 }
372 })
373
Dan Shi37ee3b82019-06-06 16:23:32 -0700374 var configs []tradefed.Config
easoncylee1e3fdcd2020-04-30 10:08:33 +0800375 for _, module := range test.Properties.Test_mainline_modules {
376 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module})
377 }
Dan Shi37ee3b82019-06-06 16:23:32 -0700378 if Bool(test.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800379 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shibc0f2602019-09-17 02:43:50 +0000380 } else {
381 var options []tradefed.Option
easoncylee1e3fdcd2020-04-30 10:08:33 +0800382 options = append(options, tradefed.Option{Name: "force-root", Value: "false"})
nelsonli0d7111e2019-09-17 16:35:23 +0800383 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
Dan Shi20ccd212019-08-27 10:37:24 -0700384 }
385 if Bool(test.Properties.Disable_framework) {
386 var options []tradefed.Option
Dan Shi8aa40102020-05-12 13:50:12 -0700387 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.StopServicesSetup", options})
Julien Desprezeb7398e2019-02-28 08:45:28 -0800388 }
Dan Shi37ee3b82019-06-06 16:23:32 -0700389 if Bool(test.testDecorator.Properties.Isolated) {
easoncylee1e3fdcd2020-04-30 10:08:33 +0800390 configs = append(configs, tradefed.Option{Name: "not-shardable", Value: "true"})
Dan Shi37ee3b82019-06-06 16:23:32 -0700391 }
yelinhsieh9fc60402018-10-01 19:23:14 +0800392 if test.Properties.Test_options.Run_test_as != nil {
easoncylee1e3fdcd2020-04-30 10:08:33 +0800393 configs = append(configs, tradefed.Option{Name: "run-test-as", Value: String(test.Properties.Test_options.Run_test_as)})
yelinhsieh9fc60402018-10-01 19:23:14 +0800394 }
David Srbecky519db272020-06-18 18:07:00 +0100395 for _, tag := range test.Properties.Test_options.Test_suite_tag {
396 configs = append(configs, tradefed.Option{Name: "test-suite-tag", Value: tag})
397 }
Justin Yun107a4cb2021-02-10 13:46:50 +0900398 if test.Properties.Test_min_api_level != nil {
nelsonli0d7111e2019-09-17 16:35:23 +0800399 var options []tradefed.Option
Justin Yun107a4cb2021-02-10 13:46:50 +0900400 options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*test.Properties.Test_min_api_level), 10)})
401 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
nelsonli0d7111e2019-09-17 16:35:23 +0800402 }
yelinhsieh9fc60402018-10-01 19:23:14 +0800403
Jack He33338892018-09-19 02:21:28 -0700404 test.testConfig = tradefed.AutoGenNativeTestConfig(ctx, test.Properties.Test_config,
yangbill5ec45552020-08-13 16:16:56 +0800405 test.Properties.Test_config_template, test.Properties.Test_suites, configs, test.Properties.Auto_gen_config, testInstallBase)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800406
Dan Shi95d19422020-08-15 12:24:26 -0700407 test.extraTestConfigs = android.PathsForModuleSrc(ctx, test.Properties.Test_options.Extra_test_configs)
408
Colin Cross600c9df2016-09-13 12:26:16 -0700409 test.binaryDecorator.baseInstaller.dir = "nativetest"
410 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800411
412 if !Bool(test.Properties.No_named_install_directory) {
413 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800414 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800415 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
416 }
417
Julien Desprez8908b372021-02-04 10:10:16 -0800418 if ctx.Host() && test.gtest() && test.Properties.Test_options.Unit_test == nil {
Julien Desprez3b933d32021-01-14 11:49:15 -0800419 test.Properties.Test_options.Unit_test = proptools.BoolPtr(true)
420 }
Dan Willemsen1d577e22016-08-29 15:53:15 -0700421 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700422}
423
424func NewTest(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700425 module, binary := NewBinary(hod)
426 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700427 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700428
429 test := &testBinary{
430 testDecorator: testDecorator{
431 linker: binary.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700432 },
Colin Crossb916a382016-07-29 17:28:03 -0700433 binaryDecorator: binary,
434 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700435 }
Colin Crossb916a382016-07-29 17:28:03 -0700436 module.compiler = test
437 module.linker = test
438 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700439 return module
440}
441
Colin Crossb916a382016-07-29 17:28:03 -0700442type testLibrary struct {
443 testDecorator
444 *libraryDecorator
445}
446
447func (test *testLibrary) linkerProps() []interface{} {
448 return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...)
449}
450
451func (test *testLibrary) linkerInit(ctx BaseModuleContext) {
452 test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker)
453 test.libraryDecorator.linkerInit(ctx)
454}
455
Colin Cross37047f12016-12-13 17:06:13 -0800456func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700457 deps = test.testDecorator.linkerDeps(ctx, deps)
458 deps = test.libraryDecorator.linkerDeps(ctx, deps)
459 return deps
460}
461
462func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
463 flags = test.libraryDecorator.linkerFlags(ctx, flags)
464 flags = test.testDecorator.linkerFlags(ctx, flags)
465 return flags
466}
467
Colin Cross4d9c2d12016-07-29 12:48:20 -0700468func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800469 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700470 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700471 test := &testLibrary{
472 testDecorator: testDecorator{
473 linker: library.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700474 },
Colin Crossb916a382016-07-29 17:28:03 -0700475 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700476 }
Colin Crossb916a382016-07-29 17:28:03 -0700477 module.linker = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700478 return module
479}
480
Colin Crosse28f4e22017-04-24 18:10:29 -0700481type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700482 // list of files or filegroup modules that provide data that should be installed alongside
483 // the test
Colin Cross27b922f2019-03-04 22:35:41 -0800484 Data []string `android:"path"`
Anders Lewisb97e8182017-07-14 15:20:13 -0700485
Colin Crosse28f4e22017-04-24 18:10:29 -0700486 // list of compatibility suites (for example "cts", "vts") that the module should be
487 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700488 Test_suites []string `android:"arch_variant"`
489
490 // the name of the test configuration (for example "AndroidTest.xml") that should be
491 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800492 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700493
494 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
495 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800496 Test_config_template *string `android:"path,arch_variant"`
Dan Shi37ee3b82019-06-06 16:23:32 -0700497
498 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
499 // with root permission.
500 Require_root *bool
Dan Shi6ffaaa82019-09-26 11:41:36 -0700501
502 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
503 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
504 // explicitly.
505 Auto_gen_config *bool
Colin Crosse28f4e22017-04-24 18:10:29 -0700506}
507
Colin Crossb916a382016-07-29 17:28:03 -0700508type benchmarkDecorator struct {
509 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700510 Properties BenchmarkProperties
Anders Lewisb97e8182017-07-14 15:20:13 -0700511 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700512 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700513}
514
Colin Crossb916a382016-07-29 17:28:03 -0700515func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) {
516 runpath := "../../lib"
517 if ctx.toolchain().Is64Bit() {
518 runpath += "64"
519 }
520 benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath)
521 benchmark.binaryDecorator.linkerInit(ctx)
522}
523
Colin Crosse28f4e22017-04-24 18:10:29 -0700524func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
525 props := benchmark.binaryDecorator.linkerProps()
526 props = append(props, &benchmark.Properties)
527 return props
528}
529
Colin Cross37047f12016-12-13 17:06:13 -0800530func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700531 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700532 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
533 return deps
534}
535
Colin Crossb916a382016-07-29 17:28:03 -0700536func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Colin Cross8a497952019-03-05 22:25:09 -0800537 benchmark.data = android.PathsForModuleSrc(ctx, benchmark.Properties.Data)
Chris Parsons79d66a52020-06-05 17:26:16 -0400538
Dan Shi37ee3b82019-06-06 16:23:32 -0700539 var configs []tradefed.Config
540 if Bool(benchmark.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800541 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shi37ee3b82019-06-06 16:23:32 -0700542 }
Jack He33338892018-09-19 02:21:28 -0700543 benchmark.testConfig = tradefed.AutoGenNativeBenchmarkTestConfig(ctx, benchmark.Properties.Test_config,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700544 benchmark.Properties.Test_config_template, benchmark.Properties.Test_suites, configs, benchmark.Properties.Auto_gen_config)
Colin Cross303e21f2018-08-07 16:49:25 -0700545
Colin Cross28690e92017-09-08 16:20:30 -0700546 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
547 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Dan Willemsen1d577e22016-08-29 15:53:15 -0700548 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700549}
550
Colin Cross4d9c2d12016-07-29 12:48:20 -0700551func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700552 module, binary := NewBinary(hod)
553 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700554 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700555
556 benchmark := &benchmarkDecorator{
557 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700558 }
Colin Crossb916a382016-07-29 17:28:03 -0700559 module.linker = benchmark
560 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700561 return module
562}