blob: 49fc57b58141f34a4c054c4979031fc3bca0ed0b [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"
23)
24
Colin Crossb916a382016-07-29 17:28:03 -070025type TestProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070026 // if set, build against the gtest library. Defaults to true.
Colin Cross600c9df2016-09-13 12:26:16 -070027 Gtest *bool
Colin Crossb916a382016-07-29 17:28:03 -070028}
Colin Cross4d9c2d12016-07-29 12:48:20 -070029
Colin Crossb916a382016-07-29 17:28:03 -070030type TestBinaryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070031 // Create a separate binary for each source file. Useful when there is
32 // global state that can not be torn down and reset between each test suite.
33 Test_per_src *bool
Dan Willemsen3340d602016-12-27 14:40:40 -080034
35 // Disables the creation of a test-specific directory when used with
36 // relative_install_path. Useful if several tests need to be in the same
37 // directory, but test_per_src doesn't work.
38 No_named_install_directory *bool
Colin Crossfaeb7aa2017-02-01 14:12:44 -080039
40 // list of files or filegroup modules that provide data that should be installed alongside
41 // the test
42 Data []string
Colin Crossa929db02017-03-27 16:27:50 -070043
44 // list of compatibility suites (for example "cts", "vts") that the module should be
45 // installed into.
Dan Willemsen15d54d52017-09-18 16:49:28 -070046 Test_suites []string `android:"arch_variant"`
Julien Despreze146e392018-08-02 15:00:46 -070047
48 // the name of the test configuration (for example "AndroidTest.xml") that should be
49 // installed with the module.
50 Test_config *string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070051}
52
53func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -070054 android.RegisterModuleType("cc_test", TestFactory)
55 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
56 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
57 android.RegisterModuleType("cc_test_host", TestHostFactory)
58 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070059}
60
61// Module factory for tests
Steven Moreland87c9d7b2017-11-02 21:38:28 -070062func TestFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070063 module := NewTest(android.HostAndDeviceSupported)
64 return module.Init()
65}
66
67// Module factory for test libraries
Steven Moreland87c9d7b2017-11-02 21:38:28 -070068func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070069 module := NewTestLibrary(android.HostAndDeviceSupported)
70 return module.Init()
71}
72
73// Module factory for benchmarks
Steven Moreland87c9d7b2017-11-02 21:38:28 -070074func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070075 module := NewBenchmark(android.HostAndDeviceSupported)
76 return module.Init()
77}
78
79// Module factory for host tests
Steven Moreland87c9d7b2017-11-02 21:38:28 -070080func TestHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070081 module := NewTest(android.HostSupported)
82 return module.Init()
83}
84
85// Module factory for host benchmarks
Steven Moreland87c9d7b2017-11-02 21:38:28 -070086func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070087 module := NewBenchmark(android.HostSupported)
88 return module.Init()
89}
90
Colin Crossb916a382016-07-29 17:28:03 -070091type testPerSrc interface {
92 testPerSrc() bool
93 srcs() []string
94 setSrc(string, string)
95}
96
97func (test *testBinary) testPerSrc() bool {
98 return Bool(test.Properties.Test_per_src)
99}
100
101func (test *testBinary) srcs() []string {
102 return test.baseCompiler.Properties.Srcs
103}
104
105func (test *testBinary) setSrc(name, src string) {
106 test.baseCompiler.Properties.Srcs = []string{src}
Nan Zhang0007d812017-11-07 10:57:05 -0800107 test.binaryDecorator.Properties.Stem = StringPtr(name)
Colin Crossb916a382016-07-29 17:28:03 -0700108}
109
110var _ testPerSrc = (*testBinary)(nil)
111
Colin Cross4d9c2d12016-07-29 12:48:20 -0700112func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
113 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700114 if test, ok := m.linker.(testPerSrc); ok {
115 if test.testPerSrc() && len(test.srcs()) > 0 {
116 testNames := make([]string, len(test.srcs()))
117 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700118 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
119 }
120 tests := mctx.CreateLocalVariations(testNames...)
Colin Crossb916a382016-07-29 17:28:03 -0700121 for i, src := range test.srcs() {
122 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700123 }
124 }
125 }
126 }
127}
128
Colin Crossb916a382016-07-29 17:28:03 -0700129type testDecorator struct {
130 Properties TestProperties
131 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700132}
133
Colin Cross600c9df2016-09-13 12:26:16 -0700134func (test *testDecorator) gtest() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700135 return BoolDefault(test.Properties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700136}
137
Colin Crossb916a382016-07-29 17:28:03 -0700138func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700139 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700140 return flags
141 }
142
143 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
144 if ctx.Host() {
145 flags.CFlags = append(flags.CFlags, "-O0", "-g")
146
147 switch ctx.Os() {
148 case android.Windows:
149 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
150 case android.Linux:
151 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700152 case android.Darwin:
153 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700154 }
155 } else {
156 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
157 }
158
159 return flags
160}
161
Colin Crossb916a382016-07-29 17:28:03 -0700162func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700163 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700164 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800165 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700166 } else {
167 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
168 }
169 }
Colin Crossb916a382016-07-29 17:28:03 -0700170
Colin Cross4d9c2d12016-07-29 12:48:20 -0700171 return deps
172}
173
Colin Crossb916a382016-07-29 17:28:03 -0700174func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) {
yangbillb3174d12018-05-07 06:41:20 +0000175 // 1. Add ../../lib[64] to rpath so that out/host/linux-x86/nativetest/<test dir>/<test> can
Colin Crossbd75e1d2017-06-30 17:27:55 -0700176 // find out/host/linux-x86/lib[64]/library.so
yangbillb3174d12018-05-07 06:41:20 +0000177 // 2. Add ../../../lib[64] to rpath so that out/host/linux-x86/testcases/<test dir>/<CPU>/<test> can
178 // also find out/host/linux-x86/lib[64]/library.so
179 runpaths := []string{"../../lib", "../../../lib"}
180 for _, runpath := range runpaths {
181 if ctx.toolchain().Is64Bit() {
182 runpath += "64"
183 }
184 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700185 }
Colin Crossbd75e1d2017-06-30 17:27:55 -0700186
187 // add "" to rpath so that test binaries can find libraries in their own test directory
188 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700189}
190
Colin Crossb916a382016-07-29 17:28:03 -0700191func (test *testDecorator) linkerProps() []interface{} {
192 return []interface{}{&test.Properties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700193}
194
Colin Crossb916a382016-07-29 17:28:03 -0700195func NewTestInstaller() *baseInstaller {
196 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700197}
198
Colin Crossb916a382016-07-29 17:28:03 -0700199type testBinary struct {
200 testDecorator
201 *binaryDecorator
202 *baseCompiler
Colin Crossb916a382016-07-29 17:28:03 -0700203 Properties TestBinaryProperties
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800204 data android.Paths
Colin Crossb916a382016-07-29 17:28:03 -0700205}
206
207func (test *testBinary) linkerProps() []interface{} {
208 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
209 props = append(props, &test.Properties)
210 return props
211}
212
213func (test *testBinary) linkerInit(ctx BaseModuleContext) {
214 test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker)
215 test.binaryDecorator.linkerInit(ctx)
216}
217
Colin Cross37047f12016-12-13 17:06:13 -0800218func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800219 android.ExtractSourcesDeps(ctx, test.Properties.Data)
220
Colin Crossb916a382016-07-29 17:28:03 -0700221 deps = test.testDecorator.linkerDeps(ctx, deps)
222 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700223 return deps
224}
225
Colin Crossb916a382016-07-29 17:28:03 -0700226func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
227 flags = test.binaryDecorator.linkerFlags(ctx, flags)
228 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700229 return flags
230}
231
Colin Crossb916a382016-07-29 17:28:03 -0700232func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800233 test.data = ctx.ExpandSources(test.Properties.Data, nil)
234
Colin Cross600c9df2016-09-13 12:26:16 -0700235 test.binaryDecorator.baseInstaller.dir = "nativetest"
236 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800237
238 if !Bool(test.Properties.No_named_install_directory) {
239 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800240 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800241 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
242 }
243
Dan Willemsen1d577e22016-08-29 15:53:15 -0700244 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700245}
246
247func NewTest(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700248 module, binary := NewBinary(hod)
249 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700250 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700251
252 test := &testBinary{
253 testDecorator: testDecorator{
254 linker: binary.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255 },
Colin Crossb916a382016-07-29 17:28:03 -0700256 binaryDecorator: binary,
257 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700258 }
Colin Crossb916a382016-07-29 17:28:03 -0700259 module.compiler = test
260 module.linker = test
261 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700262 return module
263}
264
Colin Crossb916a382016-07-29 17:28:03 -0700265type testLibrary struct {
266 testDecorator
267 *libraryDecorator
268}
269
270func (test *testLibrary) linkerProps() []interface{} {
271 return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...)
272}
273
274func (test *testLibrary) linkerInit(ctx BaseModuleContext) {
275 test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker)
276 test.libraryDecorator.linkerInit(ctx)
277}
278
Colin Cross37047f12016-12-13 17:06:13 -0800279func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700280 deps = test.testDecorator.linkerDeps(ctx, deps)
281 deps = test.libraryDecorator.linkerDeps(ctx, deps)
282 return deps
283}
284
285func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
286 flags = test.libraryDecorator.linkerFlags(ctx, flags)
287 flags = test.testDecorator.linkerFlags(ctx, flags)
288 return flags
289}
290
Colin Cross4d9c2d12016-07-29 12:48:20 -0700291func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800292 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700293 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700294 test := &testLibrary{
295 testDecorator: testDecorator{
296 linker: library.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700297 },
Colin Crossb916a382016-07-29 17:28:03 -0700298 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700299 }
Colin Crossb916a382016-07-29 17:28:03 -0700300 module.linker = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700301 return module
302}
303
Colin Crosse28f4e22017-04-24 18:10:29 -0700304type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700305 // list of files or filegroup modules that provide data that should be installed alongside
306 // the test
307 Data []string
308
Colin Crosse28f4e22017-04-24 18:10:29 -0700309 // list of compatibility suites (for example "cts", "vts") that the module should be
310 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700311 Test_suites []string `android:"arch_variant"`
312
313 // the name of the test configuration (for example "AndroidTest.xml") that should be
314 // installed with the module.
315 Test_config *string `android:"arch_variant"`
Colin Crosse28f4e22017-04-24 18:10:29 -0700316}
317
Colin Crossb916a382016-07-29 17:28:03 -0700318type benchmarkDecorator struct {
319 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700320 Properties BenchmarkProperties
Anders Lewisb97e8182017-07-14 15:20:13 -0700321 data android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700322}
323
Colin Crossb916a382016-07-29 17:28:03 -0700324func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) {
325 runpath := "../../lib"
326 if ctx.toolchain().Is64Bit() {
327 runpath += "64"
328 }
329 benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath)
330 benchmark.binaryDecorator.linkerInit(ctx)
331}
332
Colin Crosse28f4e22017-04-24 18:10:29 -0700333func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
334 props := benchmark.binaryDecorator.linkerProps()
335 props = append(props, &benchmark.Properties)
336 return props
337}
338
Colin Cross37047f12016-12-13 17:06:13 -0800339func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Anders Lewisb97e8182017-07-14 15:20:13 -0700340 android.ExtractSourcesDeps(ctx, benchmark.Properties.Data)
Colin Crossb916a382016-07-29 17:28:03 -0700341 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700342 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
343 return deps
344}
345
Colin Crossb916a382016-07-29 17:28:03 -0700346func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Anders Lewisb97e8182017-07-14 15:20:13 -0700347 benchmark.data = ctx.ExpandSources(benchmark.Properties.Data, nil)
Colin Cross28690e92017-09-08 16:20:30 -0700348 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
349 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Dan Willemsen1d577e22016-08-29 15:53:15 -0700350 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700351}
352
Colin Cross4d9c2d12016-07-29 12:48:20 -0700353func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Dan Willemsen0b24c742016-10-04 15:13:37 -0700354 // Benchmarks aren't supported on Darwin
355 if runtime.GOOS == "darwin" {
356 switch hod {
357 case android.HostAndDeviceSupported:
358 hod = android.DeviceSupported
359 case android.HostSupported:
360 hod = android.NeitherHostNorDeviceSupported
361 }
362 }
363
Colin Crossb916a382016-07-29 17:28:03 -0700364 module, binary := NewBinary(hod)
365 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700366 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700367
368 benchmark := &benchmarkDecorator{
369 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700370 }
Colin Crossb916a382016-07-29 17:28:03 -0700371 module.linker = benchmark
372 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700373 return module
374}