blob: 1501a2636f5089fddf5c3b79f0469633a55ad9eb [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.
46 Test_suites []string
Colin Cross4d9c2d12016-07-29 12:48:20 -070047}
48
49func init() {
Colin Cross798bfce2016-10-12 14:28:16 -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
Colin Cross36242852017-06-23 15:06:31 -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
Colin Cross36242852017-06-23 15:06:31 -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
Colin Cross36242852017-06-23 15:06:31 -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
Colin Cross36242852017-06-23 15:06:31 -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
Colin Cross36242852017-06-23 15:06:31 -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}
103 test.binaryDecorator.Properties.Stem = name
104}
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")
148 flags.LdFlags = append(flags.LdFlags, "-lpthread")
149 case android.Darwin:
150 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
151 flags.LdFlags = append(flags.LdFlags, "-lpthread")
152 }
153 } else {
154 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
155 }
156
157 return flags
158}
159
Colin Crossb916a382016-07-29 17:28:03 -0700160func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700161 if test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162 if ctx.sdk() && ctx.Device() {
163 switch ctx.selectedStl() {
164 case "ndk_libc++_shared", "ndk_libc++_static":
165 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
166 case "ndk_libgnustl_static":
167 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
168 default:
169 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
170 }
171 } else {
172 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
173 }
174 }
Colin Crossb916a382016-07-29 17:28:03 -0700175
Colin Cross4d9c2d12016-07-29 12:48:20 -0700176 return deps
177}
178
Colin Crossb916a382016-07-29 17:28:03 -0700179func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700180 runpath := "../../lib"
181 if ctx.toolchain().Is64Bit() {
182 runpath += "64"
183 }
Colin Crossb916a382016-07-29 17:28:03 -0700184 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700185}
186
Colin Crossb916a382016-07-29 17:28:03 -0700187func (test *testDecorator) linkerProps() []interface{} {
188 return []interface{}{&test.Properties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700189}
190
Colin Crossb916a382016-07-29 17:28:03 -0700191func NewTestInstaller() *baseInstaller {
192 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700193}
194
Colin Crossb916a382016-07-29 17:28:03 -0700195type testBinary struct {
196 testDecorator
197 *binaryDecorator
198 *baseCompiler
Colin Crossb916a382016-07-29 17:28:03 -0700199 Properties TestBinaryProperties
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800200 data android.Paths
Colin Crossb916a382016-07-29 17:28:03 -0700201}
202
203func (test *testBinary) linkerProps() []interface{} {
204 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
205 props = append(props, &test.Properties)
206 return props
207}
208
209func (test *testBinary) linkerInit(ctx BaseModuleContext) {
210 test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker)
211 test.binaryDecorator.linkerInit(ctx)
212}
213
Colin Cross37047f12016-12-13 17:06:13 -0800214func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800215 android.ExtractSourcesDeps(ctx, test.Properties.Data)
216
Colin Crossb916a382016-07-29 17:28:03 -0700217 deps = test.testDecorator.linkerDeps(ctx, deps)
218 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700219 return deps
220}
221
Colin Crossb916a382016-07-29 17:28:03 -0700222func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
223 flags = test.binaryDecorator.linkerFlags(ctx, flags)
224 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700225 return flags
226}
227
Colin Crossb916a382016-07-29 17:28:03 -0700228func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800229 test.data = ctx.ExpandSources(test.Properties.Data, nil)
230
Colin Cross600c9df2016-09-13 12:26:16 -0700231 test.binaryDecorator.baseInstaller.dir = "nativetest"
232 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800233
234 if !Bool(test.Properties.No_named_install_directory) {
235 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
236 } else if test.binaryDecorator.baseInstaller.Properties.Relative_install_path == "" {
237 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
238 }
239
Dan Willemsen1d577e22016-08-29 15:53:15 -0700240 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241}
242
243func NewTest(hod android.HostOrDeviceSupported) *Module {
Colin Crossb916a382016-07-29 17:28:03 -0700244 module, binary := NewBinary(hod)
245 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700246 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700247
248 test := &testBinary{
249 testDecorator: testDecorator{
250 linker: binary.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700251 },
Colin Crossb916a382016-07-29 17:28:03 -0700252 binaryDecorator: binary,
253 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700254 }
Colin Crossb916a382016-07-29 17:28:03 -0700255 module.compiler = test
256 module.linker = test
257 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700258 return module
259}
260
Colin Crossb916a382016-07-29 17:28:03 -0700261type testLibrary struct {
262 testDecorator
263 *libraryDecorator
264}
265
266func (test *testLibrary) linkerProps() []interface{} {
267 return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...)
268}
269
270func (test *testLibrary) linkerInit(ctx BaseModuleContext) {
271 test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker)
272 test.libraryDecorator.linkerInit(ctx)
273}
274
Colin Cross37047f12016-12-13 17:06:13 -0800275func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700276 deps = test.testDecorator.linkerDeps(ctx, deps)
277 deps = test.libraryDecorator.linkerDeps(ctx, deps)
278 return deps
279}
280
281func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
282 flags = test.libraryDecorator.linkerFlags(ctx, flags)
283 flags = test.testDecorator.linkerFlags(ctx, flags)
284 return flags
285}
286
Colin Cross4d9c2d12016-07-29 12:48:20 -0700287func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800288 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700289 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700290 test := &testLibrary{
291 testDecorator: testDecorator{
292 linker: library.baseLinker,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700293 },
Colin Crossb916a382016-07-29 17:28:03 -0700294 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700295 }
Colin Crossb916a382016-07-29 17:28:03 -0700296 module.linker = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700297 return module
298}
299
Colin Crosse28f4e22017-04-24 18:10:29 -0700300type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700301 // list of files or filegroup modules that provide data that should be installed alongside
302 // the test
303 Data []string
304
Colin Crosse28f4e22017-04-24 18:10:29 -0700305 // list of compatibility suites (for example "cts", "vts") that the module should be
306 // installed into.
307 Test_suites []string
308}
309
Colin Crossb916a382016-07-29 17:28:03 -0700310type benchmarkDecorator struct {
311 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700312 Properties BenchmarkProperties
Anders Lewisb97e8182017-07-14 15:20:13 -0700313 data android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700314}
315
Colin Crossb916a382016-07-29 17:28:03 -0700316func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) {
317 runpath := "../../lib"
318 if ctx.toolchain().Is64Bit() {
319 runpath += "64"
320 }
321 benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath)
322 benchmark.binaryDecorator.linkerInit(ctx)
323}
324
Colin Crosse28f4e22017-04-24 18:10:29 -0700325func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
326 props := benchmark.binaryDecorator.linkerProps()
327 props = append(props, &benchmark.Properties)
328 return props
329}
330
Colin Cross37047f12016-12-13 17:06:13 -0800331func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Anders Lewisb97e8182017-07-14 15:20:13 -0700332 android.ExtractSourcesDeps(ctx, benchmark.Properties.Data)
Colin Crossb916a382016-07-29 17:28:03 -0700333 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700334 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
335 return deps
336}
337
Colin Crossb916a382016-07-29 17:28:03 -0700338func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Anders Lewisb97e8182017-07-14 15:20:13 -0700339 benchmark.data = ctx.ExpandSources(benchmark.Properties.Data, nil)
Colin Cross28690e92017-09-08 16:20:30 -0700340 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
341 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Dan Willemsen1d577e22016-08-29 15:53:15 -0700342 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700343}
344
Colin Cross4d9c2d12016-07-29 12:48:20 -0700345func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Dan Willemsen0b24c742016-10-04 15:13:37 -0700346 // Benchmarks aren't supported on Darwin
347 if runtime.GOOS == "darwin" {
348 switch hod {
349 case android.HostAndDeviceSupported:
350 hod = android.DeviceSupported
351 case android.HostSupported:
352 hod = android.NeitherHostNorDeviceSupported
353 }
354 }
355
Colin Crossb916a382016-07-29 17:28:03 -0700356 module, binary := NewBinary(hod)
357 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700358 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700359
360 benchmark := &benchmarkDecorator{
361 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700362 }
Colin Crossb916a382016-07-29 17:28:03 -0700363 module.linker = benchmark
364 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700365 return module
366}