blob: f60996c7321922ddfe20b3e6035c01ef9eec4c45 [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
Dan Willemsen3340d602016-12-27 14:40:40 -080036
37 // Disables the creation of a test-specific directory when used with
38 // relative_install_path. Useful if several tests need to be in the same
39 // directory, but test_per_src doesn't work.
40 No_named_install_directory *bool
Colin Cross4d9c2d12016-07-29 12:48:20 -070041}
42
43func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070044 android.RegisterModuleType("cc_test", testFactory)
45 android.RegisterModuleType("cc_test_library", testLibraryFactory)
46 android.RegisterModuleType("cc_benchmark", benchmarkFactory)
47 android.RegisterModuleType("cc_test_host", testHostFactory)
48 android.RegisterModuleType("cc_benchmark_host", benchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070049}
50
51// Module factory for tests
52func testFactory() (blueprint.Module, []interface{}) {
53 module := NewTest(android.HostAndDeviceSupported)
54 return module.Init()
55}
56
57// Module factory for test libraries
58func testLibraryFactory() (blueprint.Module, []interface{}) {
59 module := NewTestLibrary(android.HostAndDeviceSupported)
60 return module.Init()
61}
62
63// Module factory for benchmarks
64func benchmarkFactory() (blueprint.Module, []interface{}) {
65 module := NewBenchmark(android.HostAndDeviceSupported)
66 return module.Init()
67}
68
69// Module factory for host tests
70func testHostFactory() (blueprint.Module, []interface{}) {
71 module := NewTest(android.HostSupported)
72 return module.Init()
73}
74
75// Module factory for host benchmarks
76func benchmarkHostFactory() (blueprint.Module, []interface{}) {
77 module := NewBenchmark(android.HostSupported)
78 return module.Init()
79}
80
Colin Crossb916a382016-07-29 17:28:03 -070081type testPerSrc interface {
82 testPerSrc() bool
83 srcs() []string
84 setSrc(string, string)
85}
86
87func (test *testBinary) testPerSrc() bool {
88 return Bool(test.Properties.Test_per_src)
89}
90
91func (test *testBinary) srcs() []string {
92 return test.baseCompiler.Properties.Srcs
93}
94
95func (test *testBinary) setSrc(name, src string) {
96 test.baseCompiler.Properties.Srcs = []string{src}
97 test.binaryDecorator.Properties.Stem = name
98}
99
100var _ testPerSrc = (*testBinary)(nil)
101
Colin Cross4d9c2d12016-07-29 12:48:20 -0700102func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
103 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700104 if test, ok := m.linker.(testPerSrc); ok {
105 if test.testPerSrc() && len(test.srcs()) > 0 {
106 testNames := make([]string, len(test.srcs()))
107 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700108 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
109 }
110 tests := mctx.CreateLocalVariations(testNames...)
Colin Crossb916a382016-07-29 17:28:03 -0700111 for i, src := range test.srcs() {
112 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700113 }
114 }
115 }
116 }
117}
118
Colin Crossb916a382016-07-29 17:28:03 -0700119type testDecorator struct {
120 Properties TestProperties
121 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700122}
123
Colin Cross600c9df2016-09-13 12:26:16 -0700124func (test *testDecorator) gtest() bool {
125 return test.Properties.Gtest == nil || *test.Properties.Gtest == true
126}
127
Colin Crossb916a382016-07-29 17:28:03 -0700128func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700129 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700130 return flags
131 }
132
133 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
134 if ctx.Host() {
135 flags.CFlags = append(flags.CFlags, "-O0", "-g")
136
137 switch ctx.Os() {
138 case android.Windows:
139 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
140 case android.Linux:
141 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
142 flags.LdFlags = append(flags.LdFlags, "-lpthread")
143 case android.Darwin:
144 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
145 flags.LdFlags = append(flags.LdFlags, "-lpthread")
146 }
147 } else {
148 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
149 }
150
151 return flags
152}
153
Colin Crossb916a382016-07-29 17:28:03 -0700154func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700155 if test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700156 if ctx.sdk() && ctx.Device() {
157 switch ctx.selectedStl() {
158 case "ndk_libc++_shared", "ndk_libc++_static":
159 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
160 case "ndk_libgnustl_static":
161 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
162 default:
163 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
164 }
165 } else {
166 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
167 }
168 }
Colin Crossb916a382016-07-29 17:28:03 -0700169
Colin Cross4d9c2d12016-07-29 12:48:20 -0700170 return deps
171}
172
Colin Crossb916a382016-07-29 17:28:03 -0700173func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700174 runpath := "../../lib"
175 if ctx.toolchain().Is64Bit() {
176 runpath += "64"
177 }
Colin Crossb916a382016-07-29 17:28:03 -0700178 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700179}
180
Colin Crossb916a382016-07-29 17:28:03 -0700181func (test *testDecorator) linkerProps() []interface{} {
182 return []interface{}{&test.Properties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700183}
184
Colin Crossb916a382016-07-29 17:28:03 -0700185func NewTestInstaller() *baseInstaller {
186 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700187}
188
Colin Crossb916a382016-07-29 17:28:03 -0700189type testBinary struct {
190 testDecorator
191 *binaryDecorator
192 *baseCompiler
Colin Crossb916a382016-07-29 17:28:03 -0700193 Properties TestBinaryProperties
194}
195
196func (test *testBinary) linkerProps() []interface{} {
197 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
198 props = append(props, &test.Properties)
199 return props
200}
201
202func (test *testBinary) linkerInit(ctx BaseModuleContext) {
203 test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker)
204 test.binaryDecorator.linkerInit(ctx)
205}
206
Colin Cross37047f12016-12-13 17:06:13 -0800207func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700208 deps = test.testDecorator.linkerDeps(ctx, deps)
209 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700210 return deps
211}
212
Colin Crossb916a382016-07-29 17:28:03 -0700213func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
214 flags = test.binaryDecorator.linkerFlags(ctx, flags)
215 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700216 return flags
217}
218
Colin Crossb916a382016-07-29 17:28:03 -0700219func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Colin Cross600c9df2016-09-13 12:26:16 -0700220 test.binaryDecorator.baseInstaller.dir = "nativetest"
221 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800222
223 if !Bool(test.Properties.No_named_install_directory) {
224 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
225 } else if test.binaryDecorator.baseInstaller.Properties.Relative_install_path == "" {
226 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
227 }
228
Dan Willemsen1d577e22016-08-29 15:53:15 -0700229 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700230}
231
232func NewTest(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700233 module, binary := NewBinary(hod)
234 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700235 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700236
237 test := &testBinary{
238 testDecorator: testDecorator{
239 linker: binary.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700240 },
Colin Crossb916a382016-07-29 17:28:03 -0700241 binaryDecorator: binary,
242 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700243 }
Colin Crossb916a382016-07-29 17:28:03 -0700244 module.compiler = test
245 module.linker = test
246 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700247 return module
248}
249
Colin Crossb916a382016-07-29 17:28:03 -0700250type testLibrary struct {
251 testDecorator
252 *libraryDecorator
253}
254
255func (test *testLibrary) linkerProps() []interface{} {
256 return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...)
257}
258
259func (test *testLibrary) linkerInit(ctx BaseModuleContext) {
260 test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker)
261 test.libraryDecorator.linkerInit(ctx)
262}
263
Colin Cross37047f12016-12-13 17:06:13 -0800264func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700265 deps = test.testDecorator.linkerDeps(ctx, deps)
266 deps = test.libraryDecorator.linkerDeps(ctx, deps)
267 return deps
268}
269
270func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
271 flags = test.libraryDecorator.linkerFlags(ctx, flags)
272 flags = test.testDecorator.linkerFlags(ctx, flags)
273 return flags
274}
275
Colin Cross4d9c2d12016-07-29 12:48:20 -0700276func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800277 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700278 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700279 test := &testLibrary{
280 testDecorator: testDecorator{
281 linker: library.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700282 },
Colin Crossb916a382016-07-29 17:28:03 -0700283 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700284 }
Colin Crossb916a382016-07-29 17:28:03 -0700285 module.linker = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700286 return module
287}
288
Colin Crossb916a382016-07-29 17:28:03 -0700289type benchmarkDecorator struct {
290 *binaryDecorator
Colin Cross4d9c2d12016-07-29 12:48:20 -0700291}
292
Colin Crossb916a382016-07-29 17:28:03 -0700293func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) {
294 runpath := "../../lib"
295 if ctx.toolchain().Is64Bit() {
296 runpath += "64"
297 }
298 benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath)
299 benchmark.binaryDecorator.linkerInit(ctx)
300}
301
Colin Cross37047f12016-12-13 17:06:13 -0800302func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700303 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700304 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
305 return deps
306}
307
Colin Crossb916a382016-07-29 17:28:03 -0700308func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Dan Willemsen1d577e22016-08-29 15:53:15 -0700309 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("nativetest", ctx.ModuleName())
310 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("nativetest64", ctx.ModuleName())
311 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700312}
313
Colin Cross4d9c2d12016-07-29 12:48:20 -0700314func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Dan Willemsen0b24c742016-10-04 15:13:37 -0700315 // Benchmarks aren't supported on Darwin
316 if runtime.GOOS == "darwin" {
317 switch hod {
318 case android.HostAndDeviceSupported:
319 hod = android.DeviceSupported
320 case android.HostSupported:
321 hod = android.NeitherHostNorDeviceSupported
322 }
323 }
324
Colin Crossb916a382016-07-29 17:28:03 -0700325 module, binary := NewBinary(hod)
326 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700327 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700328
329 benchmark := &benchmarkDecorator{
330 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700331 }
Colin Crossb916a382016-07-29 17:28:03 -0700332 module.linker = benchmark
333 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700334 return module
335}