blob: 07eb621a6596bda7c99dfb348838b721782d4305 [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
22 "github.com/google/blueprint"
23
Colin Cross4d9c2d12016-07-29 12:48:20 -070024 "android/soong/android"
25)
26
Colin Crossb916a382016-07-29 17:28:03 -070027type TestProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070028 // if set, build against the gtest library. Defaults to true.
Colin Cross600c9df2016-09-13 12:26:16 -070029 Gtest *bool
Colin Crossb916a382016-07-29 17:28:03 -070030}
Colin Cross4d9c2d12016-07-29 12:48:20 -070031
Colin Crossb916a382016-07-29 17:28:03 -070032type TestBinaryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070033 // Create a separate binary for each source file. Useful when there is
34 // global state that can not be torn down and reset between each test suite.
35 Test_per_src *bool
36}
37
38func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070039 android.RegisterModuleType("cc_test", testFactory)
40 android.RegisterModuleType("cc_test_library", testLibraryFactory)
41 android.RegisterModuleType("cc_benchmark", benchmarkFactory)
42 android.RegisterModuleType("cc_test_host", testHostFactory)
43 android.RegisterModuleType("cc_benchmark_host", benchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070044}
45
46// Module factory for tests
47func testFactory() (blueprint.Module, []interface{}) {
48 module := NewTest(android.HostAndDeviceSupported)
49 return module.Init()
50}
51
52// Module factory for test libraries
53func testLibraryFactory() (blueprint.Module, []interface{}) {
54 module := NewTestLibrary(android.HostAndDeviceSupported)
55 return module.Init()
56}
57
58// Module factory for benchmarks
59func benchmarkFactory() (blueprint.Module, []interface{}) {
60 module := NewBenchmark(android.HostAndDeviceSupported)
61 return module.Init()
62}
63
64// Module factory for host tests
65func testHostFactory() (blueprint.Module, []interface{}) {
66 module := NewTest(android.HostSupported)
67 return module.Init()
68}
69
70// Module factory for host benchmarks
71func benchmarkHostFactory() (blueprint.Module, []interface{}) {
72 module := NewBenchmark(android.HostSupported)
73 return module.Init()
74}
75
Colin Crossb916a382016-07-29 17:28:03 -070076type testPerSrc interface {
77 testPerSrc() bool
78 srcs() []string
79 setSrc(string, string)
80}
81
82func (test *testBinary) testPerSrc() bool {
83 return Bool(test.Properties.Test_per_src)
84}
85
86func (test *testBinary) srcs() []string {
87 return test.baseCompiler.Properties.Srcs
88}
89
90func (test *testBinary) setSrc(name, src string) {
91 test.baseCompiler.Properties.Srcs = []string{src}
92 test.binaryDecorator.Properties.Stem = name
93}
94
95var _ testPerSrc = (*testBinary)(nil)
96
Colin Cross4d9c2d12016-07-29 12:48:20 -070097func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
98 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -070099 if test, ok := m.linker.(testPerSrc); ok {
100 if test.testPerSrc() && len(test.srcs()) > 0 {
101 testNames := make([]string, len(test.srcs()))
102 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700103 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
104 }
105 tests := mctx.CreateLocalVariations(testNames...)
Colin Crossb916a382016-07-29 17:28:03 -0700106 for i, src := range test.srcs() {
107 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700108 }
109 }
110 }
111 }
112}
113
Colin Crossb916a382016-07-29 17:28:03 -0700114type testDecorator struct {
115 Properties TestProperties
116 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700117}
118
Colin Cross600c9df2016-09-13 12:26:16 -0700119func (test *testDecorator) gtest() bool {
120 return test.Properties.Gtest == nil || *test.Properties.Gtest == true
121}
122
Colin Crossb916a382016-07-29 17:28:03 -0700123func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700124 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700125 return flags
126 }
127
128 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
129 if ctx.Host() {
130 flags.CFlags = append(flags.CFlags, "-O0", "-g")
131
132 switch ctx.Os() {
133 case android.Windows:
134 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
135 case android.Linux:
136 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
137 flags.LdFlags = append(flags.LdFlags, "-lpthread")
138 case android.Darwin:
139 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
140 flags.LdFlags = append(flags.LdFlags, "-lpthread")
141 }
142 } else {
143 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
144 }
145
146 return flags
147}
148
Colin Crossb916a382016-07-29 17:28:03 -0700149func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700150 if test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700151 if ctx.sdk() && ctx.Device() {
152 switch ctx.selectedStl() {
153 case "ndk_libc++_shared", "ndk_libc++_static":
154 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
155 case "ndk_libgnustl_static":
156 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
157 default:
158 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
159 }
160 } else {
161 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
162 }
163 }
Colin Crossb916a382016-07-29 17:28:03 -0700164
Colin Cross4d9c2d12016-07-29 12:48:20 -0700165 return deps
166}
167
Colin Crossb916a382016-07-29 17:28:03 -0700168func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700169 runpath := "../../lib"
170 if ctx.toolchain().Is64Bit() {
171 runpath += "64"
172 }
Colin Crossb916a382016-07-29 17:28:03 -0700173 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700174}
175
Colin Crossb916a382016-07-29 17:28:03 -0700176func (test *testDecorator) linkerProps() []interface{} {
177 return []interface{}{&test.Properties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700178}
179
Colin Crossb916a382016-07-29 17:28:03 -0700180func NewTestInstaller() *baseInstaller {
181 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700182}
183
Colin Crossb916a382016-07-29 17:28:03 -0700184type testBinary struct {
185 testDecorator
186 *binaryDecorator
187 *baseCompiler
Colin Crossb916a382016-07-29 17:28:03 -0700188 Properties TestBinaryProperties
189}
190
191func (test *testBinary) linkerProps() []interface{} {
192 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
193 props = append(props, &test.Properties)
194 return props
195}
196
197func (test *testBinary) linkerInit(ctx BaseModuleContext) {
198 test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker)
199 test.binaryDecorator.linkerInit(ctx)
200}
201
Colin Cross37047f12016-12-13 17:06:13 -0800202func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700203 deps = test.testDecorator.linkerDeps(ctx, deps)
204 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700205 return deps
206}
207
Colin Crossb916a382016-07-29 17:28:03 -0700208func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
209 flags = test.binaryDecorator.linkerFlags(ctx, flags)
210 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700211 return flags
212}
213
Colin Crossb916a382016-07-29 17:28:03 -0700214func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Colin Cross600c9df2016-09-13 12:26:16 -0700215 test.binaryDecorator.baseInstaller.dir = "nativetest"
216 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
217 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Dan Willemsen1d577e22016-08-29 15:53:15 -0700218 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700219}
220
221func NewTest(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700222 module, binary := NewBinary(hod)
223 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700224 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700225
226 test := &testBinary{
227 testDecorator: testDecorator{
228 linker: binary.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700229 },
Colin Crossb916a382016-07-29 17:28:03 -0700230 binaryDecorator: binary,
231 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700232 }
Colin Crossb916a382016-07-29 17:28:03 -0700233 module.compiler = test
234 module.linker = test
235 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700236 return module
237}
238
Colin Crossb916a382016-07-29 17:28:03 -0700239type testLibrary struct {
240 testDecorator
241 *libraryDecorator
242}
243
244func (test *testLibrary) linkerProps() []interface{} {
245 return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...)
246}
247
248func (test *testLibrary) linkerInit(ctx BaseModuleContext) {
249 test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker)
250 test.libraryDecorator.linkerInit(ctx)
251}
252
Colin Cross37047f12016-12-13 17:06:13 -0800253func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700254 deps = test.testDecorator.linkerDeps(ctx, deps)
255 deps = test.libraryDecorator.linkerDeps(ctx, deps)
256 return deps
257}
258
259func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
260 flags = test.libraryDecorator.linkerFlags(ctx, flags)
261 flags = test.testDecorator.linkerFlags(ctx, flags)
262 return flags
263}
264
Colin Cross4d9c2d12016-07-29 12:48:20 -0700265func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800266 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700267 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700268 test := &testLibrary{
269 testDecorator: testDecorator{
270 linker: library.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700271 },
Colin Crossb916a382016-07-29 17:28:03 -0700272 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700273 }
Colin Crossb916a382016-07-29 17:28:03 -0700274 module.linker = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700275 return module
276}
277
Colin Crossb916a382016-07-29 17:28:03 -0700278type benchmarkDecorator struct {
279 *binaryDecorator
Colin Cross4d9c2d12016-07-29 12:48:20 -0700280}
281
Colin Crossb916a382016-07-29 17:28:03 -0700282func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) {
283 runpath := "../../lib"
284 if ctx.toolchain().Is64Bit() {
285 runpath += "64"
286 }
287 benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath)
288 benchmark.binaryDecorator.linkerInit(ctx)
289}
290
Colin Cross37047f12016-12-13 17:06:13 -0800291func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700292 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700293 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
294 return deps
295}
296
Colin Crossb916a382016-07-29 17:28:03 -0700297func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Dan Willemsen1d577e22016-08-29 15:53:15 -0700298 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("nativetest", ctx.ModuleName())
299 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("nativetest64", ctx.ModuleName())
300 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700301}
302
Colin Cross4d9c2d12016-07-29 12:48:20 -0700303func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Dan Willemsen0b24c742016-10-04 15:13:37 -0700304 // Benchmarks aren't supported on Darwin
305 if runtime.GOOS == "darwin" {
306 switch hod {
307 case android.HostAndDeviceSupported:
308 hod = android.DeviceSupported
309 case android.HostSupported:
310 hod = android.NeitherHostNorDeviceSupported
311 }
312 }
313
Colin Crossb916a382016-07-29 17:28:03 -0700314 module, binary := NewBinary(hod)
315 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700316 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700317
318 benchmark := &benchmarkDecorator{
319 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700320 }
Colin Crossb916a382016-07-29 17:28:03 -0700321 module.linker = benchmark
322 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700323 return module
324}