blob: 96049db8d73a7b3e8f1cf64b77567a88519a14e8 [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
Christopher Ferris9df92d62018-08-21 12:40:08 -070029
30 // if set, use the isolated gtest runner. Defaults to false.
31 Isolated *bool
Colin Crossb916a382016-07-29 17:28:03 -070032}
Colin Cross4d9c2d12016-07-29 12:48:20 -070033
Colin Crossb916a382016-07-29 17:28:03 -070034type TestBinaryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070035 // Create a separate binary for each source file. Useful when there is
36 // global state that can not be torn down and reset between each test suite.
37 Test_per_src *bool
Dan Willemsen3340d602016-12-27 14:40:40 -080038
39 // Disables the creation of a test-specific directory when used with
40 // relative_install_path. Useful if several tests need to be in the same
41 // directory, but test_per_src doesn't work.
42 No_named_install_directory *bool
Colin Crossfaeb7aa2017-02-01 14:12:44 -080043
44 // list of files or filegroup modules that provide data that should be installed alongside
45 // the test
46 Data []string
Colin Crossa929db02017-03-27 16:27:50 -070047
48 // list of compatibility suites (for example "cts", "vts") that the module should be
49 // installed into.
Dan Willemsen15d54d52017-09-18 16:49:28 -070050 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -070051
52 // the name of the test configuration (for example "AndroidTest.xml") that should be
53 // installed with the module.
54 Test_config *string `android:"arch_variant"`
Jack He33338892018-09-19 02:21:28 -070055
56 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
57 // should be installed with the module.
58 Test_config_template *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070059}
60
61func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -070062 android.RegisterModuleType("cc_test", TestFactory)
63 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
64 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
65 android.RegisterModuleType("cc_test_host", TestHostFactory)
66 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070067}
68
69// Module factory for tests
Steven Moreland87c9d7b2017-11-02 21:38:28 -070070func TestFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070071 module := NewTest(android.HostAndDeviceSupported)
72 return module.Init()
73}
74
75// Module factory for test libraries
Steven Moreland87c9d7b2017-11-02 21:38:28 -070076func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070077 module := NewTestLibrary(android.HostAndDeviceSupported)
78 return module.Init()
79}
80
81// Module factory for benchmarks
Steven Moreland87c9d7b2017-11-02 21:38:28 -070082func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070083 module := NewBenchmark(android.HostAndDeviceSupported)
84 return module.Init()
85}
86
87// Module factory for host tests
Steven Moreland87c9d7b2017-11-02 21:38:28 -070088func TestHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070089 module := NewTest(android.HostSupported)
90 return module.Init()
91}
92
93// Module factory for host benchmarks
Steven Moreland87c9d7b2017-11-02 21:38:28 -070094func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070095 module := NewBenchmark(android.HostSupported)
96 return module.Init()
97}
98
Colin Crossb916a382016-07-29 17:28:03 -070099type testPerSrc interface {
100 testPerSrc() bool
101 srcs() []string
102 setSrc(string, string)
103}
104
105func (test *testBinary) testPerSrc() bool {
106 return Bool(test.Properties.Test_per_src)
107}
108
109func (test *testBinary) srcs() []string {
110 return test.baseCompiler.Properties.Srcs
111}
112
113func (test *testBinary) setSrc(name, src string) {
114 test.baseCompiler.Properties.Srcs = []string{src}
Nan Zhang0007d812017-11-07 10:57:05 -0800115 test.binaryDecorator.Properties.Stem = StringPtr(name)
Colin Crossb916a382016-07-29 17:28:03 -0700116}
117
118var _ testPerSrc = (*testBinary)(nil)
119
Colin Cross4d9c2d12016-07-29 12:48:20 -0700120func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
121 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700122 if test, ok := m.linker.(testPerSrc); ok {
123 if test.testPerSrc() && len(test.srcs()) > 0 {
124 testNames := make([]string, len(test.srcs()))
125 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700126 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
127 }
128 tests := mctx.CreateLocalVariations(testNames...)
Colin Crossb916a382016-07-29 17:28:03 -0700129 for i, src := range test.srcs() {
130 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700131 }
132 }
133 }
134 }
135}
136
Colin Crossb916a382016-07-29 17:28:03 -0700137type testDecorator struct {
138 Properties TestProperties
139 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700140}
141
Colin Cross600c9df2016-09-13 12:26:16 -0700142func (test *testDecorator) gtest() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700143 return BoolDefault(test.Properties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700144}
145
Colin Crossb916a382016-07-29 17:28:03 -0700146func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700147 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148 return flags
149 }
150
151 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
152 if ctx.Host() {
153 flags.CFlags = append(flags.CFlags, "-O0", "-g")
154
155 switch ctx.Os() {
156 case android.Windows:
157 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
158 case android.Linux:
159 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700160 case android.Darwin:
161 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162 }
163 } else {
164 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
165 }
166
167 return flags
168}
169
Colin Crossb916a382016-07-29 17:28:03 -0700170func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700171 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700172 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800173 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Christopher Ferris9df92d62018-08-21 12:40:08 -0700174 } else if BoolDefault(test.Properties.Isolated, false) {
175 deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700176 } else {
177 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
178 }
179 }
Colin Crossb916a382016-07-29 17:28:03 -0700180
Colin Cross4d9c2d12016-07-29 12:48:20 -0700181 return deps
182}
183
Colin Crossb916a382016-07-29 17:28:03 -0700184func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) {
yangbillb3174d12018-05-07 06:41:20 +0000185 // 1. Add ../../lib[64] to rpath so that out/host/linux-x86/nativetest/<test dir>/<test> can
Colin Crossbd75e1d2017-06-30 17:27:55 -0700186 // find out/host/linux-x86/lib[64]/library.so
yangbillb3174d12018-05-07 06:41:20 +0000187 // 2. Add ../../../lib[64] to rpath so that out/host/linux-x86/testcases/<test dir>/<CPU>/<test> can
188 // also find out/host/linux-x86/lib[64]/library.so
189 runpaths := []string{"../../lib", "../../../lib"}
190 for _, runpath := range runpaths {
191 if ctx.toolchain().Is64Bit() {
192 runpath += "64"
193 }
194 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700195 }
Colin Crossbd75e1d2017-06-30 17:27:55 -0700196
197 // add "" to rpath so that test binaries can find libraries in their own test directory
198 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700199}
200
Colin Crossb916a382016-07-29 17:28:03 -0700201func (test *testDecorator) linkerProps() []interface{} {
202 return []interface{}{&test.Properties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700203}
204
Colin Crossb916a382016-07-29 17:28:03 -0700205func NewTestInstaller() *baseInstaller {
206 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700207}
208
Colin Crossb916a382016-07-29 17:28:03 -0700209type testBinary struct {
210 testDecorator
211 *binaryDecorator
212 *baseCompiler
Colin Crossb916a382016-07-29 17:28:03 -0700213 Properties TestBinaryProperties
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800214 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700215 testConfig android.Path
Colin Crossb916a382016-07-29 17:28:03 -0700216}
217
218func (test *testBinary) linkerProps() []interface{} {
219 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
220 props = append(props, &test.Properties)
221 return props
222}
223
224func (test *testBinary) linkerInit(ctx BaseModuleContext) {
225 test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker)
226 test.binaryDecorator.linkerInit(ctx)
227}
228
Colin Cross37047f12016-12-13 17:06:13 -0800229func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800230 android.ExtractSourcesDeps(ctx, test.Properties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700231 android.ExtractSourceDeps(ctx, test.Properties.Test_config)
Jack He33338892018-09-19 02:21:28 -0700232 android.ExtractSourceDeps(ctx, test.Properties.Test_config_template)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800233
Colin Crossb916a382016-07-29 17:28:03 -0700234 deps = test.testDecorator.linkerDeps(ctx, deps)
235 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700236 return deps
237}
238
Colin Crossb916a382016-07-29 17:28:03 -0700239func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
240 flags = test.binaryDecorator.linkerFlags(ctx, flags)
241 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242 return flags
243}
244
Colin Crossb916a382016-07-29 17:28:03 -0700245func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800246 test.data = ctx.ExpandSources(test.Properties.Data, nil)
Jack He33338892018-09-19 02:21:28 -0700247 test.testConfig = tradefed.AutoGenNativeTestConfig(ctx, test.Properties.Test_config,
Bill Yangfd18c422018-10-25 05:36:13 +0000248 test.Properties.Test_config_template)
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800249
Colin Cross600c9df2016-09-13 12:26:16 -0700250 test.binaryDecorator.baseInstaller.dir = "nativetest"
251 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800252
253 if !Bool(test.Properties.No_named_install_directory) {
254 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800255 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800256 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
257 }
258
Dan Willemsen1d577e22016-08-29 15:53:15 -0700259 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700260}
261
262func NewTest(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700263 module, binary := NewBinary(hod)
264 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700265 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700266
267 test := &testBinary{
268 testDecorator: testDecorator{
269 linker: binary.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700270 },
Colin Crossb916a382016-07-29 17:28:03 -0700271 binaryDecorator: binary,
272 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700273 }
Colin Crossb916a382016-07-29 17:28:03 -0700274 module.compiler = test
275 module.linker = test
276 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700277 return module
278}
279
Colin Crossb916a382016-07-29 17:28:03 -0700280type testLibrary struct {
281 testDecorator
282 *libraryDecorator
283}
284
285func (test *testLibrary) linkerProps() []interface{} {
286 return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...)
287}
288
289func (test *testLibrary) linkerInit(ctx BaseModuleContext) {
290 test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker)
291 test.libraryDecorator.linkerInit(ctx)
292}
293
Colin Cross37047f12016-12-13 17:06:13 -0800294func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700295 deps = test.testDecorator.linkerDeps(ctx, deps)
296 deps = test.libraryDecorator.linkerDeps(ctx, deps)
297 return deps
298}
299
300func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
301 flags = test.libraryDecorator.linkerFlags(ctx, flags)
302 flags = test.testDecorator.linkerFlags(ctx, flags)
303 return flags
304}
305
Colin Cross4d9c2d12016-07-29 12:48:20 -0700306func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800307 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700308 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700309 test := &testLibrary{
310 testDecorator: testDecorator{
311 linker: library.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700312 },
Colin Crossb916a382016-07-29 17:28:03 -0700313 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700314 }
Colin Crossb916a382016-07-29 17:28:03 -0700315 module.linker = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700316 return module
317}
318
Colin Crosse28f4e22017-04-24 18:10:29 -0700319type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700320 // list of files or filegroup modules that provide data that should be installed alongside
321 // the test
322 Data []string
323
Colin Crosse28f4e22017-04-24 18:10:29 -0700324 // list of compatibility suites (for example "cts", "vts") that the module should be
325 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700326 Test_suites []string `android:"arch_variant"`
327
328 // the name of the test configuration (for example "AndroidTest.xml") that should be
329 // installed with the module.
330 Test_config *string `android:"arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700331
332 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
333 // should be installed with the module.
334 Test_config_template *string `android:"arch_variant"`
Colin Crosse28f4e22017-04-24 18:10:29 -0700335}
336
Colin Crossb916a382016-07-29 17:28:03 -0700337type benchmarkDecorator struct {
338 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700339 Properties BenchmarkProperties
Anders Lewisb97e8182017-07-14 15:20:13 -0700340 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700341 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700342}
343
Colin Crossb916a382016-07-29 17:28:03 -0700344func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) {
345 runpath := "../../lib"
346 if ctx.toolchain().Is64Bit() {
347 runpath += "64"
348 }
349 benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath)
350 benchmark.binaryDecorator.linkerInit(ctx)
351}
352
Colin Crosse28f4e22017-04-24 18:10:29 -0700353func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
354 props := benchmark.binaryDecorator.linkerProps()
355 props = append(props, &benchmark.Properties)
356 return props
357}
358
Colin Cross37047f12016-12-13 17:06:13 -0800359func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Anders Lewisb97e8182017-07-14 15:20:13 -0700360 android.ExtractSourcesDeps(ctx, benchmark.Properties.Data)
Colin Cross303e21f2018-08-07 16:49:25 -0700361 android.ExtractSourceDeps(ctx, benchmark.Properties.Test_config)
Jack He33338892018-09-19 02:21:28 -0700362 android.ExtractSourceDeps(ctx, benchmark.Properties.Test_config_template)
Colin Cross303e21f2018-08-07 16:49:25 -0700363
Colin Crossb916a382016-07-29 17:28:03 -0700364 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700365 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
366 return deps
367}
368
Colin Crossb916a382016-07-29 17:28:03 -0700369func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Anders Lewisb97e8182017-07-14 15:20:13 -0700370 benchmark.data = ctx.ExpandSources(benchmark.Properties.Data, nil)
Jack He33338892018-09-19 02:21:28 -0700371 benchmark.testConfig = tradefed.AutoGenNativeBenchmarkTestConfig(ctx, benchmark.Properties.Test_config,
372 benchmark.Properties.Test_config_template)
Colin Cross303e21f2018-08-07 16:49:25 -0700373
Colin Cross28690e92017-09-08 16:20:30 -0700374 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
375 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Dan Willemsen1d577e22016-08-29 15:53:15 -0700376 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700377}
378
Colin Cross4d9c2d12016-07-29 12:48:20 -0700379func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Dan Willemsen0b24c742016-10-04 15:13:37 -0700380 // Benchmarks aren't supported on Darwin
381 if runtime.GOOS == "darwin" {
382 switch hod {
383 case android.HostAndDeviceSupported:
384 hod = android.DeviceSupported
385 case android.HostSupported:
386 hod = android.NeitherHostNorDeviceSupported
387 }
388 }
389
Colin Crossb916a382016-07-29 17:28:03 -0700390 module, binary := NewBinary(hod)
391 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700392 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700393
394 benchmark := &benchmarkDecorator{
395 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700396 }
Colin Crossb916a382016-07-29 17:28:03 -0700397 module.linker = benchmark
398 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700399 return module
400}