Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
| 18 | "path/filepath" |
Dan Willemsen | 0b24c74 | 2016-10-04 15:13:37 -0700 | [diff] [blame] | 19 | "runtime" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 20 | "strings" |
| 21 | |
| 22 | "github.com/google/blueprint" |
| 23 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 24 | "android/soong/android" |
| 25 | ) |
| 26 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 27 | type TestProperties struct { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 28 | // if set, build against the gtest library. Defaults to true. |
Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 29 | Gtest *bool |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 30 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 31 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 32 | type TestBinaryProperties struct { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 33 | // 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 | |
| 38 | func init() { |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 39 | 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 Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // Module factory for tests |
| 47 | func testFactory() (blueprint.Module, []interface{}) { |
| 48 | module := NewTest(android.HostAndDeviceSupported) |
| 49 | return module.Init() |
| 50 | } |
| 51 | |
| 52 | // Module factory for test libraries |
| 53 | func testLibraryFactory() (blueprint.Module, []interface{}) { |
| 54 | module := NewTestLibrary(android.HostAndDeviceSupported) |
| 55 | return module.Init() |
| 56 | } |
| 57 | |
| 58 | // Module factory for benchmarks |
| 59 | func benchmarkFactory() (blueprint.Module, []interface{}) { |
| 60 | module := NewBenchmark(android.HostAndDeviceSupported) |
| 61 | return module.Init() |
| 62 | } |
| 63 | |
| 64 | // Module factory for host tests |
| 65 | func testHostFactory() (blueprint.Module, []interface{}) { |
| 66 | module := NewTest(android.HostSupported) |
| 67 | return module.Init() |
| 68 | } |
| 69 | |
| 70 | // Module factory for host benchmarks |
| 71 | func benchmarkHostFactory() (blueprint.Module, []interface{}) { |
| 72 | module := NewBenchmark(android.HostSupported) |
| 73 | return module.Init() |
| 74 | } |
| 75 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 76 | type testPerSrc interface { |
| 77 | testPerSrc() bool |
| 78 | srcs() []string |
| 79 | setSrc(string, string) |
| 80 | } |
| 81 | |
| 82 | func (test *testBinary) testPerSrc() bool { |
| 83 | return Bool(test.Properties.Test_per_src) |
| 84 | } |
| 85 | |
| 86 | func (test *testBinary) srcs() []string { |
| 87 | return test.baseCompiler.Properties.Srcs |
| 88 | } |
| 89 | |
| 90 | func (test *testBinary) setSrc(name, src string) { |
| 91 | test.baseCompiler.Properties.Srcs = []string{src} |
| 92 | test.binaryDecorator.Properties.Stem = name |
| 93 | } |
| 94 | |
| 95 | var _ testPerSrc = (*testBinary)(nil) |
| 96 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 97 | func testPerSrcMutator(mctx android.BottomUpMutatorContext) { |
| 98 | if m, ok := mctx.Module().(*Module); ok { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 99 | 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 Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 103 | testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src)) |
| 104 | } |
| 105 | tests := mctx.CreateLocalVariations(testNames...) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 106 | for i, src := range test.srcs() { |
| 107 | tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 114 | type testDecorator struct { |
| 115 | Properties TestProperties |
| 116 | linker *baseLinker |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 119 | func (test *testDecorator) gtest() bool { |
| 120 | return test.Properties.Gtest == nil || *test.Properties.Gtest == true |
| 121 | } |
| 122 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 123 | func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 124 | if !test.gtest() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 125 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 149 | func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps { |
Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 150 | if test.gtest() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 151 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 164 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 165 | return deps |
| 166 | } |
| 167 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 168 | func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 169 | runpath := "../../lib" |
| 170 | if ctx.toolchain().Is64Bit() { |
| 171 | runpath += "64" |
| 172 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 173 | linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 176 | func (test *testDecorator) linkerProps() []interface{} { |
| 177 | return []interface{}{&test.Properties} |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 180 | func NewTestInstaller() *baseInstaller { |
| 181 | return NewBaseInstaller("nativetest", "nativetest64", InstallInData) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 184 | type testBinary struct { |
| 185 | testDecorator |
| 186 | *binaryDecorator |
| 187 | *baseCompiler |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 188 | Properties TestBinaryProperties |
| 189 | } |
| 190 | |
| 191 | func (test *testBinary) linkerProps() []interface{} { |
| 192 | props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...) |
| 193 | props = append(props, &test.Properties) |
| 194 | return props |
| 195 | } |
| 196 | |
| 197 | func (test *testBinary) linkerInit(ctx BaseModuleContext) { |
| 198 | test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker) |
| 199 | test.binaryDecorator.linkerInit(ctx) |
| 200 | } |
| 201 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame^] | 202 | func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 203 | deps = test.testDecorator.linkerDeps(ctx, deps) |
| 204 | deps = test.binaryDecorator.linkerDeps(ctx, deps) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 205 | return deps |
| 206 | } |
| 207 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 208 | func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 209 | flags = test.binaryDecorator.linkerFlags(ctx, flags) |
| 210 | flags = test.testDecorator.linkerFlags(ctx, flags) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 211 | return flags |
| 212 | } |
| 213 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 214 | func (test *testBinary) install(ctx ModuleContext, file android.Path) { |
Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 215 | test.binaryDecorator.baseInstaller.dir = "nativetest" |
| 216 | test.binaryDecorator.baseInstaller.dir64 = "nativetest64" |
| 217 | test.binaryDecorator.baseInstaller.relative = ctx.ModuleName() |
Dan Willemsen | 1d577e2 | 2016-08-29 15:53:15 -0700 | [diff] [blame] | 218 | test.binaryDecorator.baseInstaller.install(ctx, file) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | func NewTest(hod android.HostOrDeviceSupported) *Module { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 222 | module, binary := NewBinary(hod) |
| 223 | module.multilib = android.MultilibBoth |
Dan Willemsen | 1d577e2 | 2016-08-29 15:53:15 -0700 | [diff] [blame] | 224 | binary.baseInstaller = NewTestInstaller() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 225 | |
| 226 | test := &testBinary{ |
| 227 | testDecorator: testDecorator{ |
| 228 | linker: binary.baseLinker, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 229 | }, |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 230 | binaryDecorator: binary, |
| 231 | baseCompiler: NewBaseCompiler(), |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 232 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 233 | module.compiler = test |
| 234 | module.linker = test |
| 235 | module.installer = test |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 236 | return module |
| 237 | } |
| 238 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 239 | type testLibrary struct { |
| 240 | testDecorator |
| 241 | *libraryDecorator |
| 242 | } |
| 243 | |
| 244 | func (test *testLibrary) linkerProps() []interface{} { |
| 245 | return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...) |
| 246 | } |
| 247 | |
| 248 | func (test *testLibrary) linkerInit(ctx BaseModuleContext) { |
| 249 | test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker) |
| 250 | test.libraryDecorator.linkerInit(ctx) |
| 251 | } |
| 252 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame^] | 253 | func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 254 | deps = test.testDecorator.linkerDeps(ctx, deps) |
| 255 | deps = test.libraryDecorator.linkerDeps(ctx, deps) |
| 256 | return deps |
| 257 | } |
| 258 | |
| 259 | func (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 Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 265 | func NewTestLibrary(hod android.HostOrDeviceSupported) *Module { |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 266 | module, library := NewLibrary(android.HostAndDeviceSupported) |
Dan Willemsen | 28bda51 | 2016-08-31 16:32:55 -0700 | [diff] [blame] | 267 | library.baseInstaller = NewTestInstaller() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 268 | test := &testLibrary{ |
| 269 | testDecorator: testDecorator{ |
| 270 | linker: library.baseLinker, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 271 | }, |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 272 | libraryDecorator: library, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 273 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 274 | module.linker = test |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 275 | return module |
| 276 | } |
| 277 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 278 | type benchmarkDecorator struct { |
| 279 | *binaryDecorator |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 282 | func (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 Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame^] | 291 | func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 292 | deps = benchmark.binaryDecorator.linkerDeps(ctx, deps) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 293 | deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark") |
| 294 | return deps |
| 295 | } |
| 296 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 297 | func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) { |
Dan Willemsen | 1d577e2 | 2016-08-29 15:53:15 -0700 | [diff] [blame] | 298 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 303 | func NewBenchmark(hod android.HostOrDeviceSupported) *Module { |
Dan Willemsen | 0b24c74 | 2016-10-04 15:13:37 -0700 | [diff] [blame] | 304 | // 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 314 | module, binary := NewBinary(hod) |
| 315 | module.multilib = android.MultilibBoth |
Dan Willemsen | 1d577e2 | 2016-08-29 15:53:15 -0700 | [diff] [blame] | 316 | binary.baseInstaller = NewTestInstaller() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 317 | |
| 318 | benchmark := &benchmarkDecorator{ |
| 319 | binaryDecorator: binary, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 320 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 321 | module.linker = benchmark |
| 322 | module.installer = benchmark |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 323 | return module |
| 324 | } |