blob: b67341e03086c1baf36dc0175f1bff810d9cd42a [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"
Dan Willemsen0b24c742016-10-04 15:13:37 -070019 "runtime"
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
Colin Crossb916a382016-07-29 17:28:03 -070029}
Colin Cross4d9c2d12016-07-29 12:48:20 -070030
Colin Crossb916a382016-07-29 17:28:03 -070031type TestBinaryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070032 // Create a separate binary for each source file. Useful when there is
33 // global state that can not be torn down and reset between each test suite.
34 Test_per_src *bool
Dan Willemsen3340d602016-12-27 14:40:40 -080035
36 // Disables the creation of a test-specific directory when used with
37 // relative_install_path. Useful if several tests need to be in the same
38 // directory, but test_per_src doesn't work.
39 No_named_install_directory *bool
Colin Crossfaeb7aa2017-02-01 14:12:44 -080040
41 // list of files or filegroup modules that provide data that should be installed alongside
42 // the test
43 Data []string
Colin Crossa929db02017-03-27 16:27:50 -070044
45 // list of compatibility suites (for example "cts", "vts") that the module should be
46 // installed into.
Dan Willemsen15d54d52017-09-18 16:49:28 -070047 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -070048
49 // the name of the test configuration (for example "AndroidTest.xml") that should be
50 // installed with the module.
51 Test_config *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070052}
53
54func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -070055 android.RegisterModuleType("cc_test", TestFactory)
56 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
57 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
58 android.RegisterModuleType("cc_test_host", TestHostFactory)
59 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070060}
61
62// Module factory for tests
Steven Moreland87c9d7b2017-11-02 21:38:28 -070063func TestFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070064 module := NewTest(android.HostAndDeviceSupported)
65 return module.Init()
66}
67
68// Module factory for test libraries
Steven Moreland87c9d7b2017-11-02 21:38:28 -070069func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070070 module := NewTestLibrary(android.HostAndDeviceSupported)
71 return module.Init()
72}
73
74// Module factory for benchmarks
Steven Moreland87c9d7b2017-11-02 21:38:28 -070075func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070076 module := NewBenchmark(android.HostAndDeviceSupported)
77 return module.Init()
78}
79
80// Module factory for host tests
Steven Moreland87c9d7b2017-11-02 21:38:28 -070081func TestHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070082 module := NewTest(android.HostSupported)
83 return module.Init()
84}
85
86// Module factory for host benchmarks
Steven Moreland87c9d7b2017-11-02 21:38:28 -070087func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070088 module := NewBenchmark(android.HostSupported)
89 return module.Init()
90}
91
Colin Crossb916a382016-07-29 17:28:03 -070092type testPerSrc interface {
93 testPerSrc() bool
94 srcs() []string
95 setSrc(string, string)
96}
97
98func (test *testBinary) testPerSrc() bool {
99 return Bool(test.Properties.Test_per_src)
100}
101
102func (test *testBinary) srcs() []string {
103 return test.baseCompiler.Properties.Srcs
104}
105
106func (test *testBinary) setSrc(name, src string) {
107 test.baseCompiler.Properties.Srcs = []string{src}
Nan Zhang0007d812017-11-07 10:57:05 -0800108 test.binaryDecorator.Properties.Stem = StringPtr(name)
Colin Crossb916a382016-07-29 17:28:03 -0700109}
110
111var _ testPerSrc = (*testBinary)(nil)
112
Colin Cross4d9c2d12016-07-29 12:48:20 -0700113func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
114 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700115 if test, ok := m.linker.(testPerSrc); ok {
116 if test.testPerSrc() && len(test.srcs()) > 0 {
117 testNames := make([]string, len(test.srcs()))
118 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700119 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
120 }
121 tests := mctx.CreateLocalVariations(testNames...)
Colin Crossb916a382016-07-29 17:28:03 -0700122 for i, src := range test.srcs() {
123 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700124 }
125 }
126 }
127 }
128}
129
Colin Crossb916a382016-07-29 17:28:03 -0700130type testDecorator struct {
131 Properties TestProperties
132 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700133}
134
Colin Cross600c9df2016-09-13 12:26:16 -0700135func (test *testDecorator) gtest() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700136 return BoolDefault(test.Properties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700137}
138
Colin Crossb916a382016-07-29 17:28:03 -0700139func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700140 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700141 return flags
142 }
143
144 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
145 if ctx.Host() {
146 flags.CFlags = append(flags.CFlags, "-O0", "-g")
147
148 switch ctx.Os() {
149 case android.Windows:
150 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
151 case android.Linux:
152 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700153 case android.Darwin:
154 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700155 }
156 } else {
157 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
158 }
159
160 return flags
161}
162
Colin Crossb916a382016-07-29 17:28:03 -0700163func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700164 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700165 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800166 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700167 } else {
168 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
169 }
170 }
Colin Crossb916a382016-07-29 17:28:03 -0700171
Colin Cross4d9c2d12016-07-29 12:48:20 -0700172 return deps
173}
174
Colin Crossb916a382016-07-29 17:28:03 -0700175func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) {
yangbillb3174d12018-05-07 06:41:20 +0000176 // 1. Add ../../lib[64] to rpath so that out/host/linux-x86/nativetest/<test dir>/<test> can
Colin Crossbd75e1d2017-06-30 17:27:55 -0700177 // find out/host/linux-x86/lib[64]/library.so
yangbillb3174d12018-05-07 06:41:20 +0000178 // 2. Add ../../../lib[64] to rpath so that out/host/linux-x86/testcases/<test dir>/<CPU>/<test> can
179 // also find out/host/linux-x86/lib[64]/library.so
180 runpaths := []string{"../../lib", "../../../lib"}
181 for _, runpath := range runpaths {
182 if ctx.toolchain().Is64Bit() {
183 runpath += "64"
184 }
185 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700186 }
Colin Crossbd75e1d2017-06-30 17:27:55 -0700187
188 // add "" to rpath so that test binaries can find libraries in their own test directory
189 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700190}
191
Colin Crossb916a382016-07-29 17:28:03 -0700192func (test *testDecorator) linkerProps() []interface{} {
193 return []interface{}{&test.Properties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700194}
195
Colin Crossb916a382016-07-29 17:28:03 -0700196func NewTestInstaller() *baseInstaller {
197 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198}
199
Colin Crossb916a382016-07-29 17:28:03 -0700200type testBinary struct {
201 testDecorator
202 *binaryDecorator
203 *baseCompiler
Colin Crossb916a382016-07-29 17:28:03 -0700204 Properties TestBinaryProperties
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800205 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700206 testConfig android.Path
Colin Crossb916a382016-07-29 17:28:03 -0700207}
208
209func (test *testBinary) linkerProps() []interface{} {
210 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
211 props = append(props, &test.Properties)
212 return props
213}
214
215func (test *testBinary) linkerInit(ctx BaseModuleContext) {
216 test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker)
217 test.binaryDecorator.linkerInit(ctx)
218}
219
Colin Cross37047f12016-12-13 17:06:13 -0800220func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800221 android.ExtractSourcesDeps(ctx, test.Properties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700222 android.ExtractSourceDeps(ctx, test.Properties.Test_config)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800223
Colin Crossb916a382016-07-29 17:28:03 -0700224 deps = test.testDecorator.linkerDeps(ctx, deps)
225 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700226 return deps
227}
228
Colin Crossb916a382016-07-29 17:28:03 -0700229func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
230 flags = test.binaryDecorator.linkerFlags(ctx, flags)
231 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700232 return flags
233}
234
Colin Crossb916a382016-07-29 17:28:03 -0700235func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800236 test.data = ctx.ExpandSources(test.Properties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -0700237 test.testConfig = tradefed.AutoGenNativeTestConfig(ctx, test.Properties.Test_config)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800238
Colin Cross600c9df2016-09-13 12:26:16 -0700239 test.binaryDecorator.baseInstaller.dir = "nativetest"
240 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800241
242 if !Bool(test.Properties.No_named_install_directory) {
243 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800244 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800245 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
246 }
247
Dan Willemsen1d577e22016-08-29 15:53:15 -0700248 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700249}
250
251func NewTest(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700252 module, binary := NewBinary(hod)
253 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700254 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700255
256 test := &testBinary{
257 testDecorator: testDecorator{
258 linker: binary.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700259 },
Colin Crossb916a382016-07-29 17:28:03 -0700260 binaryDecorator: binary,
261 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700262 }
Colin Crossb916a382016-07-29 17:28:03 -0700263 module.compiler = test
264 module.linker = test
265 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700266 return module
267}
268
Colin Crossb916a382016-07-29 17:28:03 -0700269type testLibrary struct {
270 testDecorator
271 *libraryDecorator
272}
273
274func (test *testLibrary) linkerProps() []interface{} {
275 return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...)
276}
277
278func (test *testLibrary) linkerInit(ctx BaseModuleContext) {
279 test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker)
280 test.libraryDecorator.linkerInit(ctx)
281}
282
Colin Cross37047f12016-12-13 17:06:13 -0800283func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700284 deps = test.testDecorator.linkerDeps(ctx, deps)
285 deps = test.libraryDecorator.linkerDeps(ctx, deps)
286 return deps
287}
288
289func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
290 flags = test.libraryDecorator.linkerFlags(ctx, flags)
291 flags = test.testDecorator.linkerFlags(ctx, flags)
292 return flags
293}
294
Colin Cross4d9c2d12016-07-29 12:48:20 -0700295func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800296 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700297 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700298 test := &testLibrary{
299 testDecorator: testDecorator{
300 linker: library.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700301 },
Colin Crossb916a382016-07-29 17:28:03 -0700302 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700303 }
Colin Crossb916a382016-07-29 17:28:03 -0700304 module.linker = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700305 return module
306}
307
Colin Crosse28f4e22017-04-24 18:10:29 -0700308type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700309 // list of files or filegroup modules that provide data that should be installed alongside
310 // the test
311 Data []string
312
Colin Crosse28f4e22017-04-24 18:10:29 -0700313 // list of compatibility suites (for example "cts", "vts") that the module should be
314 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700315 Test_suites []string `android:"arch_variant"`
316
317 // the name of the test configuration (for example "AndroidTest.xml") that should be
318 // installed with the module.
319 Test_config *string `android:"arch_variant"`
Colin Crosse28f4e22017-04-24 18:10:29 -0700320}
321
Colin Crossb916a382016-07-29 17:28:03 -0700322type benchmarkDecorator struct {
323 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700324 Properties BenchmarkProperties
Anders Lewisb97e8182017-07-14 15:20:13 -0700325 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700326 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700327}
328
Colin Crossb916a382016-07-29 17:28:03 -0700329func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) {
330 runpath := "../../lib"
331 if ctx.toolchain().Is64Bit() {
332 runpath += "64"
333 }
334 benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath)
335 benchmark.binaryDecorator.linkerInit(ctx)
336}
337
Colin Crosse28f4e22017-04-24 18:10:29 -0700338func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
339 props := benchmark.binaryDecorator.linkerProps()
340 props = append(props, &benchmark.Properties)
341 return props
342}
343
Colin Cross37047f12016-12-13 17:06:13 -0800344func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Anders Lewisb97e8182017-07-14 15:20:13 -0700345 android.ExtractSourcesDeps(ctx, benchmark.Properties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700346 android.ExtractSourceDeps(ctx, benchmark.Properties.Test_config)
347
Colin Crossb916a382016-07-29 17:28:03 -0700348 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700349 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
350 return deps
351}
352
Colin Crossb916a382016-07-29 17:28:03 -0700353func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Anders Lewisb97e8182017-07-14 15:20:13 -0700354 benchmark.data = ctx.ExpandSources(benchmark.Properties.Data, nil)
Colin Cross303e21f2018-08-07 16:49:25 -0700355 benchmark.testConfig = tradefed.AutoGenNativeBenchmarkTestConfig(ctx, benchmark.Properties.Test_config)
356
Colin Cross28690e92017-09-08 16:20:30 -0700357 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
358 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Dan Willemsen1d577e22016-08-29 15:53:15 -0700359 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700360}
361
Colin Cross4d9c2d12016-07-29 12:48:20 -0700362func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Dan Willemsen0b24c742016-10-04 15:13:37 -0700363 // Benchmarks aren't supported on Darwin
364 if runtime.GOOS == "darwin" {
365 switch hod {
366 case android.HostAndDeviceSupported:
367 hod = android.DeviceSupported
368 case android.HostSupported:
369 hod = android.NeitherHostNorDeviceSupported
370 }
371 }
372
Colin Crossb916a382016-07-29 17:28:03 -0700373 module, binary := NewBinary(hod)
374 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700375 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700376
377 benchmark := &benchmarkDecorator{
378 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700379 }
Colin Crossb916a382016-07-29 17:28:03 -0700380 module.linker = benchmark
381 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700382 return module
383}