2016-07-30 03:48:20 +08:00
|
|
|
// Copyright 2016 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
2016-10-05 06:13:37 +08:00
|
|
|
"runtime"
|
2016-07-30 03:48:20 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"android/soong/android"
|
|
|
|
)
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
type TestProperties struct {
|
2016-07-30 03:48:20 +08:00
|
|
|
// if set, build against the gtest library. Defaults to true.
|
2016-09-14 03:26:16 +08:00
|
|
|
Gtest *bool
|
2016-07-30 08:28:03 +08:00
|
|
|
}
|
2016-07-30 03:48:20 +08:00
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
type TestBinaryProperties struct {
|
2016-07-30 03:48:20 +08:00
|
|
|
// Create a separate binary for each source file. Useful when there is
|
|
|
|
// global state that can not be torn down and reset between each test suite.
|
|
|
|
Test_per_src *bool
|
2016-12-28 06:40:40 +08:00
|
|
|
|
|
|
|
// Disables the creation of a test-specific directory when used with
|
|
|
|
// relative_install_path. Useful if several tests need to be in the same
|
|
|
|
// directory, but test_per_src doesn't work.
|
|
|
|
No_named_install_directory *bool
|
2017-02-02 06:12:44 +08:00
|
|
|
|
|
|
|
// list of files or filegroup modules that provide data that should be installed alongside
|
|
|
|
// the test
|
|
|
|
Data []string
|
2017-03-28 07:27:50 +08:00
|
|
|
|
|
|
|
// list of compatibility suites (for example "cts", "vts") that the module should be
|
|
|
|
// installed into.
|
2017-09-19 07:49:28 +08:00
|
|
|
Test_suites []string `android:"arch_variant"`
|
2016-07-30 03:48:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-10-13 05:28:16 +08:00
|
|
|
android.RegisterModuleType("cc_test", testFactory)
|
|
|
|
android.RegisterModuleType("cc_test_library", testLibraryFactory)
|
|
|
|
android.RegisterModuleType("cc_benchmark", benchmarkFactory)
|
|
|
|
android.RegisterModuleType("cc_test_host", testHostFactory)
|
|
|
|
android.RegisterModuleType("cc_benchmark_host", benchmarkHostFactory)
|
2016-07-30 03:48:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Module factory for tests
|
2017-06-24 06:06:31 +08:00
|
|
|
func testFactory() android.Module {
|
2016-07-30 03:48:20 +08:00
|
|
|
module := NewTest(android.HostAndDeviceSupported)
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Module factory for test libraries
|
2017-06-24 06:06:31 +08:00
|
|
|
func testLibraryFactory() android.Module {
|
2016-07-30 03:48:20 +08:00
|
|
|
module := NewTestLibrary(android.HostAndDeviceSupported)
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Module factory for benchmarks
|
2017-06-24 06:06:31 +08:00
|
|
|
func benchmarkFactory() android.Module {
|
2016-07-30 03:48:20 +08:00
|
|
|
module := NewBenchmark(android.HostAndDeviceSupported)
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Module factory for host tests
|
2017-06-24 06:06:31 +08:00
|
|
|
func testHostFactory() android.Module {
|
2016-07-30 03:48:20 +08:00
|
|
|
module := NewTest(android.HostSupported)
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Module factory for host benchmarks
|
2017-06-24 06:06:31 +08:00
|
|
|
func benchmarkHostFactory() android.Module {
|
2016-07-30 03:48:20 +08:00
|
|
|
module := NewBenchmark(android.HostSupported)
|
|
|
|
return module.Init()
|
|
|
|
}
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
type testPerSrc interface {
|
|
|
|
testPerSrc() bool
|
|
|
|
srcs() []string
|
|
|
|
setSrc(string, string)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (test *testBinary) testPerSrc() bool {
|
|
|
|
return Bool(test.Properties.Test_per_src)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (test *testBinary) srcs() []string {
|
|
|
|
return test.baseCompiler.Properties.Srcs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (test *testBinary) setSrc(name, src string) {
|
|
|
|
test.baseCompiler.Properties.Srcs = []string{src}
|
|
|
|
test.binaryDecorator.Properties.Stem = name
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ testPerSrc = (*testBinary)(nil)
|
|
|
|
|
2016-07-30 03:48:20 +08:00
|
|
|
func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
|
|
|
|
if m, ok := mctx.Module().(*Module); ok {
|
2016-07-30 08:28:03 +08:00
|
|
|
if test, ok := m.linker.(testPerSrc); ok {
|
|
|
|
if test.testPerSrc() && len(test.srcs()) > 0 {
|
|
|
|
testNames := make([]string, len(test.srcs()))
|
|
|
|
for i, src := range test.srcs() {
|
2016-07-30 03:48:20 +08:00
|
|
|
testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
|
|
|
|
}
|
|
|
|
tests := mctx.CreateLocalVariations(testNames...)
|
2016-07-30 08:28:03 +08:00
|
|
|
for i, src := range test.srcs() {
|
|
|
|
tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
|
2016-07-30 03:48:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
type testDecorator struct {
|
|
|
|
Properties TestProperties
|
|
|
|
linker *baseLinker
|
2016-07-30 03:48:20 +08:00
|
|
|
}
|
|
|
|
|
2016-09-14 03:26:16 +08:00
|
|
|
func (test *testDecorator) gtest() bool {
|
|
|
|
return test.Properties.Gtest == nil || *test.Properties.Gtest == true
|
|
|
|
}
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
|
2016-09-14 03:26:16 +08:00
|
|
|
if !test.gtest() {
|
2016-07-30 03:48:20 +08:00
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
|
|
|
flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
|
|
|
|
if ctx.Host() {
|
|
|
|
flags.CFlags = append(flags.CFlags, "-O0", "-g")
|
|
|
|
|
|
|
|
switch ctx.Os() {
|
|
|
|
case android.Windows:
|
|
|
|
flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
|
|
|
|
case android.Linux:
|
|
|
|
flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
|
|
|
|
flags.LdFlags = append(flags.LdFlags, "-lpthread")
|
|
|
|
case android.Darwin:
|
|
|
|
flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
|
|
|
|
flags.LdFlags = append(flags.LdFlags, "-lpthread")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
|
|
|
|
}
|
|
|
|
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
|
2016-09-14 03:26:16 +08:00
|
|
|
if test.gtest() {
|
2016-07-30 03:48:20 +08:00
|
|
|
if ctx.sdk() && ctx.Device() {
|
|
|
|
switch ctx.selectedStl() {
|
|
|
|
case "ndk_libc++_shared", "ndk_libc++_static":
|
|
|
|
deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
|
|
|
|
case "ndk_libgnustl_static":
|
|
|
|
deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
|
|
|
|
default:
|
|
|
|
deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
return deps
|
2016-07-30 03:48:20 +08:00
|
|
|
}
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) {
|
2016-07-30 03:48:20 +08:00
|
|
|
runpath := "../../lib"
|
|
|
|
if ctx.toolchain().Is64Bit() {
|
|
|
|
runpath += "64"
|
|
|
|
}
|
2016-07-30 08:28:03 +08:00
|
|
|
linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath)
|
2016-07-30 03:48:20 +08:00
|
|
|
}
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
func (test *testDecorator) linkerProps() []interface{} {
|
|
|
|
return []interface{}{&test.Properties}
|
2016-07-30 03:48:20 +08:00
|
|
|
}
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
func NewTestInstaller() *baseInstaller {
|
|
|
|
return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
|
2016-07-30 03:48:20 +08:00
|
|
|
}
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
type testBinary struct {
|
|
|
|
testDecorator
|
|
|
|
*binaryDecorator
|
|
|
|
*baseCompiler
|
|
|
|
Properties TestBinaryProperties
|
2017-02-02 06:12:44 +08:00
|
|
|
data android.Paths
|
2016-07-30 03:48:20 +08:00
|
|
|
}
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
func (test *testBinary) linkerProps() []interface{} {
|
|
|
|
props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
|
|
|
|
props = append(props, &test.Properties)
|
|
|
|
return props
|
2016-07-30 03:48:20 +08:00
|
|
|
}
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
func (test *testBinary) linkerInit(ctx BaseModuleContext) {
|
|
|
|
test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker)
|
|
|
|
test.binaryDecorator.linkerInit(ctx)
|
2016-07-30 03:48:20 +08:00
|
|
|
}
|
|
|
|
|
2016-12-14 09:06:13 +08:00
|
|
|
func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
|
2017-02-02 06:12:44 +08:00
|
|
|
android.ExtractSourcesDeps(ctx, test.Properties.Data)
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
deps = test.testDecorator.linkerDeps(ctx, deps)
|
|
|
|
deps = test.binaryDecorator.linkerDeps(ctx, deps)
|
2016-07-30 03:48:20 +08:00
|
|
|
return deps
|
|
|
|
}
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
|
|
|
|
flags = test.binaryDecorator.linkerFlags(ctx, flags)
|
|
|
|
flags = test.testDecorator.linkerFlags(ctx, flags)
|
|
|
|
return flags
|
2016-07-30 03:48:20 +08:00
|
|
|
}
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
func (test *testBinary) install(ctx ModuleContext, file android.Path) {
|
2017-02-02 06:12:44 +08:00
|
|
|
test.data = ctx.ExpandSources(test.Properties.Data, nil)
|
|
|
|
|
2016-09-14 03:26:16 +08:00
|
|
|
test.binaryDecorator.baseInstaller.dir = "nativetest"
|
|
|
|
test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
|
2016-12-28 06:40:40 +08:00
|
|
|
|
|
|
|
if !Bool(test.Properties.No_named_install_directory) {
|
|
|
|
test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
|
|
|
|
} else if test.binaryDecorator.baseInstaller.Properties.Relative_install_path == "" {
|
|
|
|
ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
|
|
|
|
}
|
|
|
|
|
2016-08-30 06:53:15 +08:00
|
|
|
test.binaryDecorator.baseInstaller.install(ctx, file)
|
2016-07-30 03:48:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTest(hod android.HostOrDeviceSupported) *Module {
|
2016-07-30 08:28:03 +08:00
|
|
|
module, binary := NewBinary(hod)
|
|
|
|
module.multilib = android.MultilibBoth
|
2016-08-30 06:53:15 +08:00
|
|
|
binary.baseInstaller = NewTestInstaller()
|
2016-07-30 08:28:03 +08:00
|
|
|
|
|
|
|
test := &testBinary{
|
|
|
|
testDecorator: testDecorator{
|
|
|
|
linker: binary.baseLinker,
|
2016-07-30 03:48:20 +08:00
|
|
|
},
|
2016-07-30 08:28:03 +08:00
|
|
|
binaryDecorator: binary,
|
|
|
|
baseCompiler: NewBaseCompiler(),
|
2016-07-30 03:48:20 +08:00
|
|
|
}
|
2016-07-30 08:28:03 +08:00
|
|
|
module.compiler = test
|
|
|
|
module.linker = test
|
|
|
|
module.installer = test
|
2016-07-30 03:48:20 +08:00
|
|
|
return module
|
|
|
|
}
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
type testLibrary struct {
|
|
|
|
testDecorator
|
|
|
|
*libraryDecorator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (test *testLibrary) linkerProps() []interface{} {
|
|
|
|
return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (test *testLibrary) linkerInit(ctx BaseModuleContext) {
|
|
|
|
test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker)
|
|
|
|
test.libraryDecorator.linkerInit(ctx)
|
|
|
|
}
|
|
|
|
|
2016-12-14 09:06:13 +08:00
|
|
|
func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
|
2016-07-30 08:28:03 +08:00
|
|
|
deps = test.testDecorator.linkerDeps(ctx, deps)
|
|
|
|
deps = test.libraryDecorator.linkerDeps(ctx, deps)
|
|
|
|
return deps
|
|
|
|
}
|
|
|
|
|
|
|
|
func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
|
|
|
|
flags = test.libraryDecorator.linkerFlags(ctx, flags)
|
|
|
|
flags = test.testDecorator.linkerFlags(ctx, flags)
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2016-07-30 03:48:20 +08:00
|
|
|
func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
|
2016-12-10 06:46:15 +08:00
|
|
|
module, library := NewLibrary(android.HostAndDeviceSupported)
|
2016-09-01 07:32:55 +08:00
|
|
|
library.baseInstaller = NewTestInstaller()
|
2016-07-30 08:28:03 +08:00
|
|
|
test := &testLibrary{
|
|
|
|
testDecorator: testDecorator{
|
|
|
|
linker: library.baseLinker,
|
2016-07-30 03:48:20 +08:00
|
|
|
},
|
2016-07-30 08:28:03 +08:00
|
|
|
libraryDecorator: library,
|
2016-07-30 03:48:20 +08:00
|
|
|
}
|
2016-07-30 08:28:03 +08:00
|
|
|
module.linker = test
|
2016-07-30 03:48:20 +08:00
|
|
|
return module
|
|
|
|
}
|
|
|
|
|
2017-04-25 09:10:29 +08:00
|
|
|
type BenchmarkProperties struct {
|
2017-07-15 06:20:13 +08:00
|
|
|
// list of files or filegroup modules that provide data that should be installed alongside
|
|
|
|
// the test
|
|
|
|
Data []string
|
|
|
|
|
2017-04-25 09:10:29 +08:00
|
|
|
// list of compatibility suites (for example "cts", "vts") that the module should be
|
|
|
|
// installed into.
|
|
|
|
Test_suites []string
|
|
|
|
}
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
type benchmarkDecorator struct {
|
|
|
|
*binaryDecorator
|
2017-04-25 09:10:29 +08:00
|
|
|
Properties BenchmarkProperties
|
2017-07-15 06:20:13 +08:00
|
|
|
data android.Paths
|
2016-07-30 03:48:20 +08:00
|
|
|
}
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) {
|
|
|
|
runpath := "../../lib"
|
|
|
|
if ctx.toolchain().Is64Bit() {
|
|
|
|
runpath += "64"
|
|
|
|
}
|
|
|
|
benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath)
|
|
|
|
benchmark.binaryDecorator.linkerInit(ctx)
|
|
|
|
}
|
|
|
|
|
2017-04-25 09:10:29 +08:00
|
|
|
func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
|
|
|
|
props := benchmark.binaryDecorator.linkerProps()
|
|
|
|
props = append(props, &benchmark.Properties)
|
|
|
|
return props
|
|
|
|
}
|
|
|
|
|
2016-12-14 09:06:13 +08:00
|
|
|
func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
|
2017-07-15 06:20:13 +08:00
|
|
|
android.ExtractSourcesDeps(ctx, benchmark.Properties.Data)
|
2016-07-30 08:28:03 +08:00
|
|
|
deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
|
2016-07-30 03:48:20 +08:00
|
|
|
deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
|
|
|
|
return deps
|
|
|
|
}
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
|
2017-07-15 06:20:13 +08:00
|
|
|
benchmark.data = ctx.ExpandSources(benchmark.Properties.Data, nil)
|
2017-09-09 07:20:30 +08:00
|
|
|
benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
|
|
|
|
benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
|
2016-08-30 06:53:15 +08:00
|
|
|
benchmark.binaryDecorator.baseInstaller.install(ctx, file)
|
2016-07-30 08:28:03 +08:00
|
|
|
}
|
|
|
|
|
2016-07-30 03:48:20 +08:00
|
|
|
func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
|
2016-10-05 06:13:37 +08:00
|
|
|
// Benchmarks aren't supported on Darwin
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
switch hod {
|
|
|
|
case android.HostAndDeviceSupported:
|
|
|
|
hod = android.DeviceSupported
|
|
|
|
case android.HostSupported:
|
|
|
|
hod = android.NeitherHostNorDeviceSupported
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-30 08:28:03 +08:00
|
|
|
module, binary := NewBinary(hod)
|
|
|
|
module.multilib = android.MultilibBoth
|
2017-09-09 07:20:30 +08:00
|
|
|
binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
|
2016-07-30 08:28:03 +08:00
|
|
|
|
|
|
|
benchmark := &benchmarkDecorator{
|
|
|
|
binaryDecorator: binary,
|
2016-07-30 03:48:20 +08:00
|
|
|
}
|
2016-07-30 08:28:03 +08:00
|
|
|
module.linker = benchmark
|
|
|
|
module.installer = benchmark
|
2016-07-30 03:48:20 +08:00
|
|
|
return module
|
|
|
|
}
|