blob: 9c7c0de4ad4c6513c795656e6388af771465316d [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
24 "android/soong"
25 "android/soong/android"
26)
27
Colin Crossb916a382016-07-29 17:28:03 -070028type TestProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070029 // if set, build against the gtest library. Defaults to true.
Colin Cross600c9df2016-09-13 12:26:16 -070030 Gtest *bool
Colin Crossb916a382016-07-29 17:28:03 -070031}
Colin Cross4d9c2d12016-07-29 12:48:20 -070032
Colin Crossb916a382016-07-29 17:28:03 -070033type TestBinaryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070034 // Create a separate binary for each source file. Useful when there is
35 // global state that can not be torn down and reset between each test suite.
36 Test_per_src *bool
37}
38
39func init() {
40 soong.RegisterModuleType("cc_test", testFactory)
41 soong.RegisterModuleType("cc_test_library", testLibraryFactory)
42 soong.RegisterModuleType("cc_benchmark", benchmarkFactory)
43 soong.RegisterModuleType("cc_test_host", testHostFactory)
44 soong.RegisterModuleType("cc_benchmark_host", benchmarkHostFactory)
45}
46
47// Module factory for tests
48func testFactory() (blueprint.Module, []interface{}) {
49 module := NewTest(android.HostAndDeviceSupported)
50 return module.Init()
51}
52
53// Module factory for test libraries
54func testLibraryFactory() (blueprint.Module, []interface{}) {
55 module := NewTestLibrary(android.HostAndDeviceSupported)
56 return module.Init()
57}
58
59// Module factory for benchmarks
60func benchmarkFactory() (blueprint.Module, []interface{}) {
61 module := NewBenchmark(android.HostAndDeviceSupported)
62 return module.Init()
63}
64
65// Module factory for host tests
66func testHostFactory() (blueprint.Module, []interface{}) {
67 module := NewTest(android.HostSupported)
68 return module.Init()
69}
70
71// Module factory for host benchmarks
72func benchmarkHostFactory() (blueprint.Module, []interface{}) {
73 module := NewBenchmark(android.HostSupported)
74 return module.Init()
75}
76
Colin Crossb916a382016-07-29 17:28:03 -070077type testPerSrc interface {
78 testPerSrc() bool
79 srcs() []string
80 setSrc(string, string)
81}
82
83func (test *testBinary) testPerSrc() bool {
84 return Bool(test.Properties.Test_per_src)
85}
86
87func (test *testBinary) srcs() []string {
88 return test.baseCompiler.Properties.Srcs
89}
90
91func (test *testBinary) setSrc(name, src string) {
92 test.baseCompiler.Properties.Srcs = []string{src}
93 test.binaryDecorator.Properties.Stem = name
94}
95
96var _ testPerSrc = (*testBinary)(nil)
97
Colin Cross4d9c2d12016-07-29 12:48:20 -070098func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
99 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700100 if test, ok := m.linker.(testPerSrc); ok {
101 if test.testPerSrc() && len(test.srcs()) > 0 {
102 testNames := make([]string, len(test.srcs()))
103 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700104 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
105 }
106 tests := mctx.CreateLocalVariations(testNames...)
Colin Crossb916a382016-07-29 17:28:03 -0700107 for i, src := range test.srcs() {
108 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700109 }
110 }
111 }
112 }
113}
114
Colin Crossb916a382016-07-29 17:28:03 -0700115type testDecorator struct {
116 Properties TestProperties
117 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700118}
119
Colin Cross600c9df2016-09-13 12:26:16 -0700120func (test *testDecorator) gtest() bool {
121 return test.Properties.Gtest == nil || *test.Properties.Gtest == true
122}
123
Colin Crossb916a382016-07-29 17:28:03 -0700124func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700125 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700126 return flags
127 }
128
129 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
130 if ctx.Host() {
131 flags.CFlags = append(flags.CFlags, "-O0", "-g")
132
133 switch ctx.Os() {
134 case android.Windows:
135 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
136 case android.Linux:
137 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
138 flags.LdFlags = append(flags.LdFlags, "-lpthread")
139 case android.Darwin:
140 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
141 flags.LdFlags = append(flags.LdFlags, "-lpthread")
142 }
143 } else {
144 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
145 }
146
147 return flags
148}
149
Colin Crossb916a382016-07-29 17:28:03 -0700150func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700151 if test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700152 if ctx.sdk() && ctx.Device() {
153 switch ctx.selectedStl() {
154 case "ndk_libc++_shared", "ndk_libc++_static":
155 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
156 case "ndk_libgnustl_static":
157 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
158 default:
159 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
160 }
161 } else {
162 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
163 }
164 }
Colin Crossb916a382016-07-29 17:28:03 -0700165
Colin Cross4d9c2d12016-07-29 12:48:20 -0700166 return deps
167}
168
Colin Crossb916a382016-07-29 17:28:03 -0700169func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700170 runpath := "../../lib"
171 if ctx.toolchain().Is64Bit() {
172 runpath += "64"
173 }
Colin Crossb916a382016-07-29 17:28:03 -0700174 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700175}
176
Colin Crossb916a382016-07-29 17:28:03 -0700177func (test *testDecorator) linkerProps() []interface{} {
178 return []interface{}{&test.Properties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700179}
180
Colin Crossb916a382016-07-29 17:28:03 -0700181func NewTestInstaller() *baseInstaller {
182 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700183}
184
Colin Crossb916a382016-07-29 17:28:03 -0700185type testBinary struct {
186 testDecorator
187 *binaryDecorator
188 *baseCompiler
Colin Crossb916a382016-07-29 17:28:03 -0700189 Properties TestBinaryProperties
190}
191
192func (test *testBinary) linkerProps() []interface{} {
193 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
194 props = append(props, &test.Properties)
195 return props
196}
197
198func (test *testBinary) linkerInit(ctx BaseModuleContext) {
199 test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker)
200 test.binaryDecorator.linkerInit(ctx)
201}
202
203func (test *testBinary) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
204 deps = test.testDecorator.linkerDeps(ctx, deps)
205 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206 return deps
207}
208
Colin Crossb916a382016-07-29 17:28:03 -0700209func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
210 flags = test.binaryDecorator.linkerFlags(ctx, flags)
211 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700212 return flags
213}
214
Colin Crossb916a382016-07-29 17:28:03 -0700215func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Colin Cross600c9df2016-09-13 12:26:16 -0700216 test.binaryDecorator.baseInstaller.dir = "nativetest"
217 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
218 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Dan Willemsen1d577e22016-08-29 15:53:15 -0700219 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220}
221
222func NewTest(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700223 module, binary := NewBinary(hod)
224 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700225 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700226
227 test := &testBinary{
228 testDecorator: testDecorator{
229 linker: binary.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700230 },
Colin Crossb916a382016-07-29 17:28:03 -0700231 binaryDecorator: binary,
232 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700233 }
Colin Crossb916a382016-07-29 17:28:03 -0700234 module.compiler = test
235 module.linker = test
236 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700237 return module
238}
239
Colin Crossb916a382016-07-29 17:28:03 -0700240type testLibrary struct {
241 testDecorator
242 *libraryDecorator
243}
244
245func (test *testLibrary) linkerProps() []interface{} {
246 return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...)
247}
248
249func (test *testLibrary) linkerInit(ctx BaseModuleContext) {
250 test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker)
251 test.libraryDecorator.linkerInit(ctx)
252}
253
254func (test *testLibrary) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
255 deps = test.testDecorator.linkerDeps(ctx, deps)
256 deps = test.libraryDecorator.linkerDeps(ctx, deps)
257 return deps
258}
259
260func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
261 flags = test.libraryDecorator.linkerFlags(ctx, flags)
262 flags = test.testDecorator.linkerFlags(ctx, flags)
263 return flags
264}
265
Colin Cross4d9c2d12016-07-29 12:48:20 -0700266func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Dan Willemsen28bda512016-08-31 16:32:55 -0700267 module, library := NewLibrary(android.HostAndDeviceSupported, true, true)
268 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700269 test := &testLibrary{
270 testDecorator: testDecorator{
271 linker: library.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700272 },
Colin Crossb916a382016-07-29 17:28:03 -0700273 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700274 }
Colin Crossb916a382016-07-29 17:28:03 -0700275 module.linker = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700276 return module
277}
278
Colin Crossb916a382016-07-29 17:28:03 -0700279type benchmarkDecorator struct {
280 *binaryDecorator
Colin Cross4d9c2d12016-07-29 12:48:20 -0700281}
282
Colin Crossb916a382016-07-29 17:28:03 -0700283func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) {
284 runpath := "../../lib"
285 if ctx.toolchain().Is64Bit() {
286 runpath += "64"
287 }
288 benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath)
289 benchmark.binaryDecorator.linkerInit(ctx)
290}
291
292func (benchmark *benchmarkDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
293 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700294 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
295 return deps
296}
297
Colin Crossb916a382016-07-29 17:28:03 -0700298func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Dan Willemsen1d577e22016-08-29 15:53:15 -0700299 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("nativetest", ctx.ModuleName())
300 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("nativetest64", ctx.ModuleName())
301 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700302}
303
Colin Cross4d9c2d12016-07-29 12:48:20 -0700304func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Dan Willemsen0b24c742016-10-04 15:13:37 -0700305 // Benchmarks aren't supported on Darwin
306 if runtime.GOOS == "darwin" {
307 switch hod {
308 case android.HostAndDeviceSupported:
309 hod = android.DeviceSupported
310 case android.HostSupported:
311 hod = android.NeitherHostNorDeviceSupported
312 }
313 }
314
Colin Crossb916a382016-07-29 17:28:03 -0700315 module, binary := NewBinary(hod)
316 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700317 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700318
319 benchmark := &benchmarkDecorator{
320 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700321 }
Colin Crossb916a382016-07-29 17:28:03 -0700322 module.linker = benchmark
323 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700324 return module
325}