blob: 448f089b22ca2da3c862cab2c6108d9eefd367a7 [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"
Colin Crossa929db02017-03-27 16:27:50 -070023
Colin Crossfaeb7aa2017-02-01 14:12:44 -080024 "github.com/google/blueprint"
Colin Cross4d9c2d12016-07-29 12:48:20 -070025)
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 Crossfaeb7aa2017-02-01 14:12:44 -080041
42 // list of files or filegroup modules that provide data that should be installed alongside
43 // the test
44 Data []string
Colin Crossa929db02017-03-27 16:27:50 -070045
46 // list of compatibility suites (for example "cts", "vts") that the module should be
47 // installed into.
48 Test_suites []string
Colin Cross4d9c2d12016-07-29 12:48:20 -070049}
50
51func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070052 android.RegisterModuleType("cc_test", testFactory)
53 android.RegisterModuleType("cc_test_library", testLibraryFactory)
54 android.RegisterModuleType("cc_benchmark", benchmarkFactory)
55 android.RegisterModuleType("cc_test_host", testHostFactory)
56 android.RegisterModuleType("cc_benchmark_host", benchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070057}
58
59// Module factory for tests
60func testFactory() (blueprint.Module, []interface{}) {
61 module := NewTest(android.HostAndDeviceSupported)
62 return module.Init()
63}
64
65// Module factory for test libraries
66func testLibraryFactory() (blueprint.Module, []interface{}) {
67 module := NewTestLibrary(android.HostAndDeviceSupported)
68 return module.Init()
69}
70
71// Module factory for benchmarks
72func benchmarkFactory() (blueprint.Module, []interface{}) {
73 module := NewBenchmark(android.HostAndDeviceSupported)
74 return module.Init()
75}
76
77// Module factory for host tests
78func testHostFactory() (blueprint.Module, []interface{}) {
79 module := NewTest(android.HostSupported)
80 return module.Init()
81}
82
83// Module factory for host benchmarks
84func benchmarkHostFactory() (blueprint.Module, []interface{}) {
85 module := NewBenchmark(android.HostSupported)
86 return module.Init()
87}
88
Colin Crossb916a382016-07-29 17:28:03 -070089type testPerSrc interface {
90 testPerSrc() bool
91 srcs() []string
92 setSrc(string, string)
93}
94
95func (test *testBinary) testPerSrc() bool {
96 return Bool(test.Properties.Test_per_src)
97}
98
99func (test *testBinary) srcs() []string {
100 return test.baseCompiler.Properties.Srcs
101}
102
103func (test *testBinary) setSrc(name, src string) {
104 test.baseCompiler.Properties.Srcs = []string{src}
105 test.binaryDecorator.Properties.Stem = name
106}
107
108var _ testPerSrc = (*testBinary)(nil)
109
Colin Cross4d9c2d12016-07-29 12:48:20 -0700110func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
111 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700112 if test, ok := m.linker.(testPerSrc); ok {
113 if test.testPerSrc() && len(test.srcs()) > 0 {
114 testNames := make([]string, len(test.srcs()))
115 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700116 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
117 }
118 tests := mctx.CreateLocalVariations(testNames...)
Colin Crossb916a382016-07-29 17:28:03 -0700119 for i, src := range test.srcs() {
120 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700121 }
122 }
123 }
124 }
125}
126
Colin Crossb916a382016-07-29 17:28:03 -0700127type testDecorator struct {
128 Properties TestProperties
129 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700130}
131
Colin Cross600c9df2016-09-13 12:26:16 -0700132func (test *testDecorator) gtest() bool {
133 return test.Properties.Gtest == nil || *test.Properties.Gtest == true
134}
135
Colin Crossb916a382016-07-29 17:28:03 -0700136func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700137 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700138 return flags
139 }
140
141 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
142 if ctx.Host() {
143 flags.CFlags = append(flags.CFlags, "-O0", "-g")
144
145 switch ctx.Os() {
146 case android.Windows:
147 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
148 case android.Linux:
149 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
150 flags.LdFlags = append(flags.LdFlags, "-lpthread")
151 case android.Darwin:
152 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
153 flags.LdFlags = append(flags.LdFlags, "-lpthread")
154 }
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() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700164 if ctx.sdk() && ctx.Device() {
165 switch ctx.selectedStl() {
166 case "ndk_libc++_shared", "ndk_libc++_static":
167 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
168 case "ndk_libgnustl_static":
169 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
170 default:
171 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
172 }
173 } else {
174 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
175 }
176 }
Colin Crossb916a382016-07-29 17:28:03 -0700177
Colin Cross4d9c2d12016-07-29 12:48:20 -0700178 return deps
179}
180
Colin Crossb916a382016-07-29 17:28:03 -0700181func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700182 runpath := "../../lib"
183 if ctx.toolchain().Is64Bit() {
184 runpath += "64"
185 }
Colin Crossb916a382016-07-29 17:28:03 -0700186 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700187}
188
Colin Crossb916a382016-07-29 17:28:03 -0700189func (test *testDecorator) linkerProps() []interface{} {
190 return []interface{}{&test.Properties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700191}
192
Colin Crossb916a382016-07-29 17:28:03 -0700193func NewTestInstaller() *baseInstaller {
194 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700195}
196
Colin Crossb916a382016-07-29 17:28:03 -0700197type testBinary struct {
198 testDecorator
199 *binaryDecorator
200 *baseCompiler
Colin Crossb916a382016-07-29 17:28:03 -0700201 Properties TestBinaryProperties
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800202 data android.Paths
Colin Crossb916a382016-07-29 17:28:03 -0700203}
204
205func (test *testBinary) linkerProps() []interface{} {
206 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
207 props = append(props, &test.Properties)
208 return props
209}
210
211func (test *testBinary) linkerInit(ctx BaseModuleContext) {
212 test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker)
213 test.binaryDecorator.linkerInit(ctx)
214}
215
Colin Cross37047f12016-12-13 17:06:13 -0800216func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800217 android.ExtractSourcesDeps(ctx, test.Properties.Data)
218
Colin Crossb916a382016-07-29 17:28:03 -0700219 deps = test.testDecorator.linkerDeps(ctx, deps)
220 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700221 return deps
222}
223
Colin Crossb916a382016-07-29 17:28:03 -0700224func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
225 flags = test.binaryDecorator.linkerFlags(ctx, flags)
226 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700227 return flags
228}
229
Colin Crossb916a382016-07-29 17:28:03 -0700230func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800231 test.data = ctx.ExpandSources(test.Properties.Data, nil)
232
Colin Cross600c9df2016-09-13 12:26:16 -0700233 test.binaryDecorator.baseInstaller.dir = "nativetest"
234 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800235
236 if !Bool(test.Properties.No_named_install_directory) {
237 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
238 } else if test.binaryDecorator.baseInstaller.Properties.Relative_install_path == "" {
239 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
240 }
241
Dan Willemsen1d577e22016-08-29 15:53:15 -0700242 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700243}
244
245func NewTest(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700246 module, binary := NewBinary(hod)
247 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700248 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700249
250 test := &testBinary{
251 testDecorator: testDecorator{
252 linker: binary.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253 },
Colin Crossb916a382016-07-29 17:28:03 -0700254 binaryDecorator: binary,
255 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700256 }
Colin Crossb916a382016-07-29 17:28:03 -0700257 module.compiler = test
258 module.linker = test
259 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700260 return module
261}
262
Colin Crossb916a382016-07-29 17:28:03 -0700263type testLibrary struct {
264 testDecorator
265 *libraryDecorator
266}
267
268func (test *testLibrary) linkerProps() []interface{} {
269 return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...)
270}
271
272func (test *testLibrary) linkerInit(ctx BaseModuleContext) {
273 test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker)
274 test.libraryDecorator.linkerInit(ctx)
275}
276
Colin Cross37047f12016-12-13 17:06:13 -0800277func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700278 deps = test.testDecorator.linkerDeps(ctx, deps)
279 deps = test.libraryDecorator.linkerDeps(ctx, deps)
280 return deps
281}
282
283func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
284 flags = test.libraryDecorator.linkerFlags(ctx, flags)
285 flags = test.testDecorator.linkerFlags(ctx, flags)
286 return flags
287}
288
Colin Cross4d9c2d12016-07-29 12:48:20 -0700289func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800290 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700291 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700292 test := &testLibrary{
293 testDecorator: testDecorator{
294 linker: library.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700295 },
Colin Crossb916a382016-07-29 17:28:03 -0700296 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700297 }
Colin Crossb916a382016-07-29 17:28:03 -0700298 module.linker = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700299 return module
300}
301
Colin Crosse28f4e22017-04-24 18:10:29 -0700302type BenchmarkProperties struct {
303 // list of compatibility suites (for example "cts", "vts") that the module should be
304 // installed into.
305 Test_suites []string
306}
307
Colin Crossb916a382016-07-29 17:28:03 -0700308type benchmarkDecorator struct {
309 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700310 Properties BenchmarkProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700311}
312
Colin Crossb916a382016-07-29 17:28:03 -0700313func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) {
314 runpath := "../../lib"
315 if ctx.toolchain().Is64Bit() {
316 runpath += "64"
317 }
318 benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath)
319 benchmark.binaryDecorator.linkerInit(ctx)
320}
321
Colin Crosse28f4e22017-04-24 18:10:29 -0700322func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
323 props := benchmark.binaryDecorator.linkerProps()
324 props = append(props, &benchmark.Properties)
325 return props
326}
327
Colin Cross37047f12016-12-13 17:06:13 -0800328func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700329 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700330 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
331 return deps
332}
333
Colin Crossb916a382016-07-29 17:28:03 -0700334func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Dan Willemsen1d577e22016-08-29 15:53:15 -0700335 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("nativetest", ctx.ModuleName())
336 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("nativetest64", ctx.ModuleName())
337 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700338}
339
Colin Cross4d9c2d12016-07-29 12:48:20 -0700340func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Dan Willemsen0b24c742016-10-04 15:13:37 -0700341 // Benchmarks aren't supported on Darwin
342 if runtime.GOOS == "darwin" {
343 switch hod {
344 case android.HostAndDeviceSupported:
345 hod = android.DeviceSupported
346 case android.HostSupported:
347 hod = android.NeitherHostNorDeviceSupported
348 }
349 }
350
Colin Crossb916a382016-07-29 17:28:03 -0700351 module, binary := NewBinary(hod)
352 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700353 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700354
355 benchmark := &benchmarkDecorator{
356 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700357 }
Colin Crossb916a382016-07-29 17:28:03 -0700358 module.linker = benchmark
359 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700360 return module
361}