blob: 3fffffcd34bd4c3da603f05a924f0b65ad146d57 [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"`
Jack He33338892018-09-19 02:21:28 -070052
53 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
54 // should be installed with the module.
55 Test_config_template *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070056}
57
58func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -070059 android.RegisterModuleType("cc_test", TestFactory)
60 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
61 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
62 android.RegisterModuleType("cc_test_host", TestHostFactory)
63 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070064}
65
66// Module factory for tests
Steven Moreland87c9d7b2017-11-02 21:38:28 -070067func TestFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070068 module := NewTest(android.HostAndDeviceSupported)
69 return module.Init()
70}
71
72// Module factory for test libraries
Steven Moreland87c9d7b2017-11-02 21:38:28 -070073func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070074 module := NewTestLibrary(android.HostAndDeviceSupported)
75 return module.Init()
76}
77
78// Module factory for benchmarks
Steven Moreland87c9d7b2017-11-02 21:38:28 -070079func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070080 module := NewBenchmark(android.HostAndDeviceSupported)
81 return module.Init()
82}
83
84// Module factory for host tests
Steven Moreland87c9d7b2017-11-02 21:38:28 -070085func TestHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070086 module := NewTest(android.HostSupported)
87 return module.Init()
88}
89
90// Module factory for host benchmarks
Steven Moreland87c9d7b2017-11-02 21:38:28 -070091func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070092 module := NewBenchmark(android.HostSupported)
93 return module.Init()
94}
95
Colin Crossb916a382016-07-29 17:28:03 -070096type testPerSrc interface {
97 testPerSrc() bool
98 srcs() []string
99 setSrc(string, string)
100}
101
102func (test *testBinary) testPerSrc() bool {
103 return Bool(test.Properties.Test_per_src)
104}
105
106func (test *testBinary) srcs() []string {
107 return test.baseCompiler.Properties.Srcs
108}
109
110func (test *testBinary) setSrc(name, src string) {
111 test.baseCompiler.Properties.Srcs = []string{src}
Nan Zhang0007d812017-11-07 10:57:05 -0800112 test.binaryDecorator.Properties.Stem = StringPtr(name)
Colin Crossb916a382016-07-29 17:28:03 -0700113}
114
115var _ testPerSrc = (*testBinary)(nil)
116
Colin Cross4d9c2d12016-07-29 12:48:20 -0700117func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
118 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700119 if test, ok := m.linker.(testPerSrc); ok {
120 if test.testPerSrc() && len(test.srcs()) > 0 {
121 testNames := make([]string, len(test.srcs()))
122 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700123 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
124 }
125 tests := mctx.CreateLocalVariations(testNames...)
Colin Crossb916a382016-07-29 17:28:03 -0700126 for i, src := range test.srcs() {
127 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700128 }
129 }
130 }
131 }
132}
133
Colin Crossb916a382016-07-29 17:28:03 -0700134type testDecorator struct {
135 Properties TestProperties
136 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700137}
138
Colin Cross600c9df2016-09-13 12:26:16 -0700139func (test *testDecorator) gtest() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700140 return BoolDefault(test.Properties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700141}
142
Colin Crossb916a382016-07-29 17:28:03 -0700143func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700144 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700145 return flags
146 }
147
148 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
149 if ctx.Host() {
150 flags.CFlags = append(flags.CFlags, "-O0", "-g")
151
152 switch ctx.Os() {
153 case android.Windows:
154 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
155 case android.Linux:
156 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700157 case android.Darwin:
158 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159 }
160 } else {
161 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
162 }
163
164 return flags
165}
166
Colin Crossb916a382016-07-29 17:28:03 -0700167func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700168 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700169 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800170 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700171 } else {
172 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
173 }
174 }
Colin Crossb916a382016-07-29 17:28:03 -0700175
Colin Cross4d9c2d12016-07-29 12:48:20 -0700176 return deps
177}
178
Colin Crossb916a382016-07-29 17:28:03 -0700179func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) {
yangbillb3174d12018-05-07 06:41:20 +0000180 // 1. Add ../../lib[64] to rpath so that out/host/linux-x86/nativetest/<test dir>/<test> can
Colin Crossbd75e1d2017-06-30 17:27:55 -0700181 // find out/host/linux-x86/lib[64]/library.so
yangbillb3174d12018-05-07 06:41:20 +0000182 // 2. Add ../../../lib[64] to rpath so that out/host/linux-x86/testcases/<test dir>/<CPU>/<test> can
183 // also find out/host/linux-x86/lib[64]/library.so
184 runpaths := []string{"../../lib", "../../../lib"}
185 for _, runpath := range runpaths {
186 if ctx.toolchain().Is64Bit() {
187 runpath += "64"
188 }
189 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700190 }
Colin Crossbd75e1d2017-06-30 17:27:55 -0700191
192 // add "" to rpath so that test binaries can find libraries in their own test directory
193 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700194}
195
Colin Crossb916a382016-07-29 17:28:03 -0700196func (test *testDecorator) linkerProps() []interface{} {
197 return []interface{}{&test.Properties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198}
199
Colin Crossb916a382016-07-29 17:28:03 -0700200func NewTestInstaller() *baseInstaller {
201 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700202}
203
Colin Crossb916a382016-07-29 17:28:03 -0700204type testBinary struct {
205 testDecorator
206 *binaryDecorator
207 *baseCompiler
Colin Crossb916a382016-07-29 17:28:03 -0700208 Properties TestBinaryProperties
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800209 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700210 testConfig android.Path
Colin Crossb916a382016-07-29 17:28:03 -0700211}
212
213func (test *testBinary) linkerProps() []interface{} {
214 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
215 props = append(props, &test.Properties)
216 return props
217}
218
219func (test *testBinary) linkerInit(ctx BaseModuleContext) {
220 test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker)
221 test.binaryDecorator.linkerInit(ctx)
222}
223
Colin Cross37047f12016-12-13 17:06:13 -0800224func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800225 android.ExtractSourcesDeps(ctx, test.Properties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700226 android.ExtractSourceDeps(ctx, test.Properties.Test_config)
Jack He33338892018-09-19 02:21:28 -0700227 android.ExtractSourceDeps(ctx, test.Properties.Test_config_template)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800228
Colin Crossb916a382016-07-29 17:28:03 -0700229 deps = test.testDecorator.linkerDeps(ctx, deps)
230 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700231 return deps
232}
233
Colin Crossb916a382016-07-29 17:28:03 -0700234func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
235 flags = test.binaryDecorator.linkerFlags(ctx, flags)
236 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700237 return flags
238}
239
Colin Crossb916a382016-07-29 17:28:03 -0700240func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800241 test.data = ctx.ExpandSources(test.Properties.Data, nil)
Jack He33338892018-09-19 02:21:28 -0700242 test.testConfig = tradefed.AutoGenNativeTestConfig(ctx, test.Properties.Test_config,
243 test.Properties.Test_config_template)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800244
Colin Cross600c9df2016-09-13 12:26:16 -0700245 test.binaryDecorator.baseInstaller.dir = "nativetest"
246 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800247
248 if !Bool(test.Properties.No_named_install_directory) {
249 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800250 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800251 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
252 }
253
Dan Willemsen1d577e22016-08-29 15:53:15 -0700254 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255}
256
257func NewTest(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700258 module, binary := NewBinary(hod)
259 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700260 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700261
262 test := &testBinary{
263 testDecorator: testDecorator{
264 linker: binary.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700265 },
Colin Crossb916a382016-07-29 17:28:03 -0700266 binaryDecorator: binary,
267 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700268 }
Colin Crossb916a382016-07-29 17:28:03 -0700269 module.compiler = test
270 module.linker = test
271 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700272 return module
273}
274
Colin Crossb916a382016-07-29 17:28:03 -0700275type testLibrary struct {
276 testDecorator
277 *libraryDecorator
278}
279
280func (test *testLibrary) linkerProps() []interface{} {
281 return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...)
282}
283
284func (test *testLibrary) linkerInit(ctx BaseModuleContext) {
285 test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker)
286 test.libraryDecorator.linkerInit(ctx)
287}
288
Colin Cross37047f12016-12-13 17:06:13 -0800289func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700290 deps = test.testDecorator.linkerDeps(ctx, deps)
291 deps = test.libraryDecorator.linkerDeps(ctx, deps)
292 return deps
293}
294
295func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
296 flags = test.libraryDecorator.linkerFlags(ctx, flags)
297 flags = test.testDecorator.linkerFlags(ctx, flags)
298 return flags
299}
300
Colin Cross4d9c2d12016-07-29 12:48:20 -0700301func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800302 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700303 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700304 test := &testLibrary{
305 testDecorator: testDecorator{
306 linker: library.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700307 },
Colin Crossb916a382016-07-29 17:28:03 -0700308 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700309 }
Colin Crossb916a382016-07-29 17:28:03 -0700310 module.linker = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700311 return module
312}
313
Colin Crosse28f4e22017-04-24 18:10:29 -0700314type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700315 // list of files or filegroup modules that provide data that should be installed alongside
316 // the test
317 Data []string
318
Colin Crosse28f4e22017-04-24 18:10:29 -0700319 // list of compatibility suites (for example "cts", "vts") that the module should be
320 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700321 Test_suites []string `android:"arch_variant"`
322
323 // the name of the test configuration (for example "AndroidTest.xml") that should be
324 // installed with the module.
325 Test_config *string `android:"arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700326
327 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
328 // should be installed with the module.
329 Test_config_template *string `android:"arch_variant"`
Colin Crosse28f4e22017-04-24 18:10:29 -0700330}
331
Colin Crossb916a382016-07-29 17:28:03 -0700332type benchmarkDecorator struct {
333 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700334 Properties BenchmarkProperties
Anders Lewisb97e8182017-07-14 15:20:13 -0700335 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700336 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700337}
338
Colin Crossb916a382016-07-29 17:28:03 -0700339func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) {
340 runpath := "../../lib"
341 if ctx.toolchain().Is64Bit() {
342 runpath += "64"
343 }
344 benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath)
345 benchmark.binaryDecorator.linkerInit(ctx)
346}
347
Colin Crosse28f4e22017-04-24 18:10:29 -0700348func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
349 props := benchmark.binaryDecorator.linkerProps()
350 props = append(props, &benchmark.Properties)
351 return props
352}
353
Colin Cross37047f12016-12-13 17:06:13 -0800354func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Anders Lewisb97e8182017-07-14 15:20:13 -0700355 android.ExtractSourcesDeps(ctx, benchmark.Properties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700356 android.ExtractSourceDeps(ctx, benchmark.Properties.Test_config)
Jack He33338892018-09-19 02:21:28 -0700357 android.ExtractSourceDeps(ctx, benchmark.Properties.Test_config_template)
Colin Cross303e21f2018-08-07 16:49:25 -0700358
Colin Crossb916a382016-07-29 17:28:03 -0700359 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700360 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
361 return deps
362}
363
Colin Crossb916a382016-07-29 17:28:03 -0700364func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Anders Lewisb97e8182017-07-14 15:20:13 -0700365 benchmark.data = ctx.ExpandSources(benchmark.Properties.Data, nil)
Jack He33338892018-09-19 02:21:28 -0700366 benchmark.testConfig = tradefed.AutoGenNativeBenchmarkTestConfig(ctx, benchmark.Properties.Test_config,
367 benchmark.Properties.Test_config_template)
Colin Cross303e21f2018-08-07 16:49:25 -0700368
Colin Cross28690e92017-09-08 16:20:30 -0700369 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
370 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Dan Willemsen1d577e22016-08-29 15:53:15 -0700371 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700372}
373
Colin Cross4d9c2d12016-07-29 12:48:20 -0700374func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Dan Willemsen0b24c742016-10-04 15:13:37 -0700375 // Benchmarks aren't supported on Darwin
376 if runtime.GOOS == "darwin" {
377 switch hod {
378 case android.HostAndDeviceSupported:
379 hod = android.DeviceSupported
380 case android.HostSupported:
381 hod = android.NeitherHostNorDeviceSupported
382 }
383 }
384
Colin Crossb916a382016-07-29 17:28:03 -0700385 module, binary := NewBinary(hod)
386 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700387 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700388
389 benchmark := &benchmarkDecorator{
390 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700391 }
Colin Crossb916a382016-07-29 17:28:03 -0700392 module.linker = benchmark
393 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700394 return module
395}