blob: 2fb3a35f0099669a253dcd9c137d91428b6fc1df [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"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070047}
48
49func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -070050 android.RegisterModuleType("cc_test", TestFactory)
51 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
52 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
53 android.RegisterModuleType("cc_test_host", TestHostFactory)
54 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070055}
56
57// Module factory for tests
Steven Moreland87c9d7b2017-11-02 21:38:28 -070058func TestFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070059 module := NewTest(android.HostAndDeviceSupported)
60 return module.Init()
61}
62
63// Module factory for test libraries
Steven Moreland87c9d7b2017-11-02 21:38:28 -070064func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070065 module := NewTestLibrary(android.HostAndDeviceSupported)
66 return module.Init()
67}
68
69// Module factory for benchmarks
Steven Moreland87c9d7b2017-11-02 21:38:28 -070070func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070071 module := NewBenchmark(android.HostAndDeviceSupported)
72 return module.Init()
73}
74
75// Module factory for host tests
Steven Moreland87c9d7b2017-11-02 21:38:28 -070076func TestHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070077 module := NewTest(android.HostSupported)
78 return module.Init()
79}
80
81// Module factory for host benchmarks
Steven Moreland87c9d7b2017-11-02 21:38:28 -070082func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070083 module := NewBenchmark(android.HostSupported)
84 return module.Init()
85}
86
Colin Crossb916a382016-07-29 17:28:03 -070087type testPerSrc interface {
88 testPerSrc() bool
89 srcs() []string
90 setSrc(string, string)
91}
92
93func (test *testBinary) testPerSrc() bool {
94 return Bool(test.Properties.Test_per_src)
95}
96
97func (test *testBinary) srcs() []string {
98 return test.baseCompiler.Properties.Srcs
99}
100
101func (test *testBinary) setSrc(name, src string) {
102 test.baseCompiler.Properties.Srcs = []string{src}
Nan Zhang0007d812017-11-07 10:57:05 -0800103 test.binaryDecorator.Properties.Stem = StringPtr(name)
Colin Crossb916a382016-07-29 17:28:03 -0700104}
105
106var _ testPerSrc = (*testBinary)(nil)
107
Colin Cross4d9c2d12016-07-29 12:48:20 -0700108func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
109 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700110 if test, ok := m.linker.(testPerSrc); ok {
111 if test.testPerSrc() && len(test.srcs()) > 0 {
112 testNames := make([]string, len(test.srcs()))
113 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700114 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
115 }
116 tests := mctx.CreateLocalVariations(testNames...)
Colin Crossb916a382016-07-29 17:28:03 -0700117 for i, src := range test.srcs() {
118 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700119 }
120 }
121 }
122 }
123}
124
Colin Crossb916a382016-07-29 17:28:03 -0700125type testDecorator struct {
126 Properties TestProperties
127 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700128}
129
Colin Cross600c9df2016-09-13 12:26:16 -0700130func (test *testDecorator) gtest() bool {
131 return test.Properties.Gtest == nil || *test.Properties.Gtest == true
132}
133
Colin Crossb916a382016-07-29 17:28:03 -0700134func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700135 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700136 return flags
137 }
138
139 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
140 if ctx.Host() {
141 flags.CFlags = append(flags.CFlags, "-O0", "-g")
142
143 switch ctx.Os() {
144 case android.Windows:
145 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
146 case android.Linux:
147 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148 case android.Darwin:
149 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700150 }
151 } else {
152 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
153 }
154
155 return flags
156}
157
Colin Crossb916a382016-07-29 17:28:03 -0700158func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700159 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700160 if ctx.useSdk() && ctx.Device() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700161 switch ctx.selectedStl() {
162 case "ndk_libc++_shared", "ndk_libc++_static":
163 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700164 default:
165 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
166 }
167 } 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) {
Colin Crossbd75e1d2017-06-30 17:27:55 -0700176 // add ../../lib[64] to rpath so that out/host/linux-x86/nativetest/<test dir>/<test> can
177 // find out/host/linux-x86/lib[64]/library.so
Colin Cross4d9c2d12016-07-29 12:48:20 -0700178 runpath := "../../lib"
179 if ctx.toolchain().Is64Bit() {
180 runpath += "64"
181 }
Colin Crossb916a382016-07-29 17:28:03 -0700182 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath)
Colin Crossbd75e1d2017-06-30 17:27:55 -0700183
184 // add "" to rpath so that test binaries can find libraries in their own test directory
185 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700186}
187
Colin Crossb916a382016-07-29 17:28:03 -0700188func (test *testDecorator) linkerProps() []interface{} {
189 return []interface{}{&test.Properties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700190}
191
Colin Crossb916a382016-07-29 17:28:03 -0700192func NewTestInstaller() *baseInstaller {
193 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700194}
195
Colin Crossb916a382016-07-29 17:28:03 -0700196type testBinary struct {
197 testDecorator
198 *binaryDecorator
199 *baseCompiler
Colin Crossb916a382016-07-29 17:28:03 -0700200 Properties TestBinaryProperties
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800201 data android.Paths
Colin Crossb916a382016-07-29 17:28:03 -0700202}
203
204func (test *testBinary) linkerProps() []interface{} {
205 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
206 props = append(props, &test.Properties)
207 return props
208}
209
210func (test *testBinary) linkerInit(ctx BaseModuleContext) {
211 test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker)
212 test.binaryDecorator.linkerInit(ctx)
213}
214
Colin Cross37047f12016-12-13 17:06:13 -0800215func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800216 android.ExtractSourcesDeps(ctx, test.Properties.Data)
217
Colin Crossb916a382016-07-29 17:28:03 -0700218 deps = test.testDecorator.linkerDeps(ctx, deps)
219 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220 return deps
221}
222
Colin Crossb916a382016-07-29 17:28:03 -0700223func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
224 flags = test.binaryDecorator.linkerFlags(ctx, flags)
225 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700226 return flags
227}
228
Colin Crossb916a382016-07-29 17:28:03 -0700229func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800230 test.data = ctx.ExpandSources(test.Properties.Data, nil)
231
Colin Cross600c9df2016-09-13 12:26:16 -0700232 test.binaryDecorator.baseInstaller.dir = "nativetest"
233 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800234
235 if !Bool(test.Properties.No_named_install_directory) {
236 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800237 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800238 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
239 }
240
Dan Willemsen1d577e22016-08-29 15:53:15 -0700241 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242}
243
244func NewTest(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700245 module, binary := NewBinary(hod)
246 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700247 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700248
249 test := &testBinary{
250 testDecorator: testDecorator{
251 linker: binary.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700252 },
Colin Crossb916a382016-07-29 17:28:03 -0700253 binaryDecorator: binary,
254 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255 }
Colin Crossb916a382016-07-29 17:28:03 -0700256 module.compiler = test
257 module.linker = test
258 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700259 return module
260}
261
Colin Crossb916a382016-07-29 17:28:03 -0700262type testLibrary struct {
263 testDecorator
264 *libraryDecorator
265}
266
267func (test *testLibrary) linkerProps() []interface{} {
268 return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...)
269}
270
271func (test *testLibrary) linkerInit(ctx BaseModuleContext) {
272 test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker)
273 test.libraryDecorator.linkerInit(ctx)
274}
275
Colin Cross37047f12016-12-13 17:06:13 -0800276func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700277 deps = test.testDecorator.linkerDeps(ctx, deps)
278 deps = test.libraryDecorator.linkerDeps(ctx, deps)
279 return deps
280}
281
282func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
283 flags = test.libraryDecorator.linkerFlags(ctx, flags)
284 flags = test.testDecorator.linkerFlags(ctx, flags)
285 return flags
286}
287
Colin Cross4d9c2d12016-07-29 12:48:20 -0700288func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800289 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700290 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700291 test := &testLibrary{
292 testDecorator: testDecorator{
293 linker: library.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700294 },
Colin Crossb916a382016-07-29 17:28:03 -0700295 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700296 }
Colin Crossb916a382016-07-29 17:28:03 -0700297 module.linker = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700298 return module
299}
300
Colin Crosse28f4e22017-04-24 18:10:29 -0700301type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700302 // list of files or filegroup modules that provide data that should be installed alongside
303 // the test
304 Data []string
305
Colin Crosse28f4e22017-04-24 18:10:29 -0700306 // list of compatibility suites (for example "cts", "vts") that the module should be
307 // installed into.
308 Test_suites []string
309}
310
Colin Crossb916a382016-07-29 17:28:03 -0700311type benchmarkDecorator struct {
312 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700313 Properties BenchmarkProperties
Anders Lewisb97e8182017-07-14 15:20:13 -0700314 data android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700315}
316
Colin Crossb916a382016-07-29 17:28:03 -0700317func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) {
318 runpath := "../../lib"
319 if ctx.toolchain().Is64Bit() {
320 runpath += "64"
321 }
322 benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath)
323 benchmark.binaryDecorator.linkerInit(ctx)
324}
325
Colin Crosse28f4e22017-04-24 18:10:29 -0700326func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
327 props := benchmark.binaryDecorator.linkerProps()
328 props = append(props, &benchmark.Properties)
329 return props
330}
331
Colin Cross37047f12016-12-13 17:06:13 -0800332func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Anders Lewisb97e8182017-07-14 15:20:13 -0700333 android.ExtractSourcesDeps(ctx, benchmark.Properties.Data)
Colin Crossb916a382016-07-29 17:28:03 -0700334 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700335 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
336 return deps
337}
338
Colin Crossb916a382016-07-29 17:28:03 -0700339func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Anders Lewisb97e8182017-07-14 15:20:13 -0700340 benchmark.data = ctx.ExpandSources(benchmark.Properties.Data, nil)
Colin Cross28690e92017-09-08 16:20:30 -0700341 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
342 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Dan Willemsen1d577e22016-08-29 15:53:15 -0700343 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700344}
345
Colin Cross4d9c2d12016-07-29 12:48:20 -0700346func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Dan Willemsen0b24c742016-10-04 15:13:37 -0700347 // Benchmarks aren't supported on Darwin
348 if runtime.GOOS == "darwin" {
349 switch hod {
350 case android.HostAndDeviceSupported:
351 hod = android.DeviceSupported
352 case android.HostSupported:
353 hod = android.NeitherHostNorDeviceSupported
354 }
355 }
356
Colin Crossb916a382016-07-29 17:28:03 -0700357 module, binary := NewBinary(hod)
358 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700359 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700360
361 benchmark := &benchmarkDecorator{
362 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700363 }
Colin Crossb916a382016-07-29 17:28:03 -0700364 module.linker = benchmark
365 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700366 return module
367}