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" |
| 19 | "strings" |
| 20 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 21 | "android/soong/android" |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 22 | "android/soong/tradefed" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 25 | type TestProperties struct { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 26 | // if set, build against the gtest library. Defaults to true. |
Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 27 | Gtest *bool |
Christopher Ferris | 9df92d6 | 2018-08-21 12:40:08 -0700 | [diff] [blame] | 28 | |
| 29 | // if set, use the isolated gtest runner. Defaults to false. |
| 30 | Isolated *bool |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 31 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 32 | |
yelinhsieh | 9fc6040 | 2018-10-01 19:23:14 +0800 | [diff] [blame] | 33 | // Test option struct. |
| 34 | type TestOptions struct { |
| 35 | // The UID that you want to run the test as on a device. |
| 36 | Run_test_as *string |
| 37 | } |
| 38 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 39 | type TestBinaryProperties struct { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 40 | // Create a separate binary for each source file. Useful when there is |
| 41 | // global state that can not be torn down and reset between each test suite. |
| 42 | Test_per_src *bool |
Dan Willemsen | 3340d60 | 2016-12-27 14:40:40 -0800 | [diff] [blame] | 43 | |
| 44 | // Disables the creation of a test-specific directory when used with |
| 45 | // relative_install_path. Useful if several tests need to be in the same |
| 46 | // directory, but test_per_src doesn't work. |
| 47 | No_named_install_directory *bool |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 48 | |
| 49 | // list of files or filegroup modules that provide data that should be installed alongside |
| 50 | // the test |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 51 | Data []string `android:"path"` |
Colin Cross | a929db0 | 2017-03-27 16:27:50 -0700 | [diff] [blame] | 52 | |
| 53 | // list of compatibility suites (for example "cts", "vts") that the module should be |
| 54 | // installed into. |
Dan Willemsen | 15d54d5 | 2017-09-18 16:49:28 -0700 | [diff] [blame] | 55 | Test_suites []string `android:"arch_variant"` |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 56 | |
| 57 | // the name of the test configuration (for example "AndroidTest.xml") that should be |
| 58 | // installed with the module. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 59 | Test_config *string `android:"path,arch_variant"` |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 60 | |
| 61 | // the name of the test configuration template (for example "AndroidTestTemplate.xml") that |
| 62 | // should be installed with the module. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 63 | Test_config_template *string `android:"path,arch_variant"` |
yelinhsieh | 9fc6040 | 2018-10-01 19:23:14 +0800 | [diff] [blame] | 64 | |
| 65 | // Test options. |
| 66 | Test_options TestOptions |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 67 | |
| 68 | // Add RootTargetPreparer to auto generated test config. This guarantees the test to run |
| 69 | // with root permission. |
| 70 | Require_root *bool |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame^] | 71 | |
| 72 | // Add RunCommandTargetPreparer to stop framework before the test and start it after the test. |
| 73 | Disable_framework *bool |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | func init() { |
Steven Moreland | 87c9d7b | 2017-11-02 21:38:28 -0700 | [diff] [blame] | 77 | android.RegisterModuleType("cc_test", TestFactory) |
| 78 | android.RegisterModuleType("cc_test_library", TestLibraryFactory) |
| 79 | android.RegisterModuleType("cc_benchmark", BenchmarkFactory) |
| 80 | android.RegisterModuleType("cc_test_host", TestHostFactory) |
| 81 | android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 84 | // cc_test generates a test config file and an executable binary file to test |
| 85 | // specific functionality on a device. The executable binary gets an implicit |
| 86 | // static_libs dependency on libgtests unless the gtest flag is set to false. |
Steven Moreland | 87c9d7b | 2017-11-02 21:38:28 -0700 | [diff] [blame] | 87 | func TestFactory() android.Module { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 88 | module := NewTest(android.HostAndDeviceSupported) |
| 89 | return module.Init() |
| 90 | } |
| 91 | |
Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 92 | // cc_test_library creates an archive of files (i.e. .o files) which is later |
| 93 | // referenced by another module (such as cc_test, cc_defaults or cc_test_library) |
| 94 | // for archiving or linking. |
Steven Moreland | 87c9d7b | 2017-11-02 21:38:28 -0700 | [diff] [blame] | 95 | func TestLibraryFactory() android.Module { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 96 | module := NewTestLibrary(android.HostAndDeviceSupported) |
| 97 | return module.Init() |
| 98 | } |
| 99 | |
Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 100 | // cc_benchmark compiles an executable binary that performs benchmark testing |
| 101 | // of a specific component in a device. Additional files such as test suites |
| 102 | // and test configuration are installed on the side of the compiled executed |
| 103 | // binary. |
Steven Moreland | 87c9d7b | 2017-11-02 21:38:28 -0700 | [diff] [blame] | 104 | func BenchmarkFactory() android.Module { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 105 | module := NewBenchmark(android.HostAndDeviceSupported) |
| 106 | return module.Init() |
| 107 | } |
| 108 | |
Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 109 | // cc_test_host compiles a test host binary. |
Steven Moreland | 87c9d7b | 2017-11-02 21:38:28 -0700 | [diff] [blame] | 110 | func TestHostFactory() android.Module { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 111 | module := NewTest(android.HostSupported) |
| 112 | return module.Init() |
| 113 | } |
| 114 | |
Patrice Arruda | c249c71 | 2019-03-19 17:00:29 -0700 | [diff] [blame] | 115 | // cc_benchmark_host compiles an executable binary that performs benchmark |
| 116 | // testing of a specific component in the host. Additional files such as |
| 117 | // test suites and test configuration are installed on the side of the |
| 118 | // compiled executed binary. |
Steven Moreland | 87c9d7b | 2017-11-02 21:38:28 -0700 | [diff] [blame] | 119 | func BenchmarkHostFactory() android.Module { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 120 | module := NewBenchmark(android.HostSupported) |
| 121 | return module.Init() |
| 122 | } |
| 123 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 124 | type testPerSrc interface { |
| 125 | testPerSrc() bool |
| 126 | srcs() []string |
Roland Levillain | f2fad97 | 2019-06-28 15:41:19 +0100 | [diff] [blame] | 127 | isAllTestsVariation() bool |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 128 | setSrc(string, string) |
Roland Levillain | f2fad97 | 2019-06-28 15:41:19 +0100 | [diff] [blame] | 129 | unsetSrc() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | func (test *testBinary) testPerSrc() bool { |
| 133 | return Bool(test.Properties.Test_per_src) |
| 134 | } |
| 135 | |
| 136 | func (test *testBinary) srcs() []string { |
| 137 | return test.baseCompiler.Properties.Srcs |
| 138 | } |
| 139 | |
Roland Levillain | f2fad97 | 2019-06-28 15:41:19 +0100 | [diff] [blame] | 140 | func (test *testBinary) isAllTestsVariation() bool { |
| 141 | stem := test.binaryDecorator.Properties.Stem |
| 142 | return stem != nil && *stem == "" |
| 143 | } |
| 144 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 145 | func (test *testBinary) setSrc(name, src string) { |
| 146 | test.baseCompiler.Properties.Srcs = []string{src} |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 147 | test.binaryDecorator.Properties.Stem = StringPtr(name) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Roland Levillain | f2fad97 | 2019-06-28 15:41:19 +0100 | [diff] [blame] | 150 | func (test *testBinary) unsetSrc() { |
| 151 | test.baseCompiler.Properties.Srcs = nil |
| 152 | test.binaryDecorator.Properties.Stem = StringPtr("") |
| 153 | } |
| 154 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 155 | var _ testPerSrc = (*testBinary)(nil) |
| 156 | |
Roland Levillain | 9b5fde9 | 2019-06-28 15:41:19 +0100 | [diff] [blame] | 157 | func TestPerSrcMutator(mctx android.BottomUpMutatorContext) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 158 | if m, ok := mctx.Module().(*Module); ok { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 159 | if test, ok := m.linker.(testPerSrc); ok { |
Roland Levillain | f2fad97 | 2019-06-28 15:41:19 +0100 | [diff] [blame] | 160 | numTests := len(test.srcs()) |
| 161 | if test.testPerSrc() && numTests > 0 { |
Jooyung Han | a61ff2c | 2019-02-28 18:06:34 +0900 | [diff] [blame] | 162 | if duplicate, found := checkDuplicate(test.srcs()); found { |
| 163 | mctx.PropertyErrorf("srcs", "found a duplicate entry %q", duplicate) |
| 164 | return |
| 165 | } |
Roland Levillain | f2fad97 | 2019-06-28 15:41:19 +0100 | [diff] [blame] | 166 | testNames := make([]string, numTests) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 167 | for i, src := range test.srcs() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 168 | testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src)) |
| 169 | } |
Roland Levillain | f2fad97 | 2019-06-28 15:41:19 +0100 | [diff] [blame] | 170 | // In addition to creating one variation per test source file, |
| 171 | // create an additional "all tests" variation named "", and have it |
| 172 | // depends on all other test_per_src variations. This is useful to |
| 173 | // create subsequent dependencies of a given module on all |
| 174 | // test_per_src variations created above: by depending on |
| 175 | // variation "", that module will transitively depend on all the |
| 176 | // other test_per_src variations without the need to know their |
| 177 | // name or even their number. |
| 178 | testNames = append(testNames, "") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 179 | tests := mctx.CreateLocalVariations(testNames...) |
Roland Levillain | f2fad97 | 2019-06-28 15:41:19 +0100 | [diff] [blame] | 180 | all_tests := tests[numTests] |
| 181 | all_tests.(*Module).linker.(testPerSrc).unsetSrc() |
| 182 | // Prevent the "all tests" variation from being installable nor |
| 183 | // exporting to Make, as it won't create any output file. |
| 184 | all_tests.(*Module).Properties.PreventInstall = true |
| 185 | all_tests.(*Module).Properties.HideFromMake = true |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 186 | for i, src := range test.srcs() { |
| 187 | tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src) |
Roland Levillain | f2fad97 | 2019-06-28 15:41:19 +0100 | [diff] [blame] | 188 | mctx.AddInterVariantDependency(testPerSrcDepTag, all_tests, tests[i]) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
Jooyung Han | a61ff2c | 2019-02-28 18:06:34 +0900 | [diff] [blame] | 195 | func checkDuplicate(values []string) (duplicate string, found bool) { |
| 196 | seen := make(map[string]string) |
| 197 | for _, v := range values { |
| 198 | if duplicate, found = seen[v]; found { |
| 199 | return |
| 200 | } |
| 201 | seen[v] = v |
| 202 | } |
| 203 | return |
| 204 | } |
| 205 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 206 | type testDecorator struct { |
| 207 | Properties TestProperties |
| 208 | linker *baseLinker |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 211 | func (test *testDecorator) gtest() bool { |
Colin Cross | 38b40df | 2018-04-10 16:14:46 -0700 | [diff] [blame] | 212 | return BoolDefault(test.Properties.Gtest, true) |
Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 215 | func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 216 | if !test.gtest() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 217 | return flags |
| 218 | } |
| 219 | |
| 220 | flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING") |
| 221 | if ctx.Host() { |
| 222 | flags.CFlags = append(flags.CFlags, "-O0", "-g") |
| 223 | |
| 224 | switch ctx.Os() { |
| 225 | case android.Windows: |
| 226 | flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS") |
| 227 | case android.Linux: |
| 228 | flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 229 | case android.Darwin: |
| 230 | flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 231 | } |
| 232 | } else { |
| 233 | flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID") |
| 234 | } |
| 235 | |
| 236 | return flags |
| 237 | } |
| 238 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 239 | func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps { |
Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 240 | if test.gtest() { |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 241 | if ctx.useSdk() && ctx.Device() { |
Dan Albert | 7dd5899 | 2018-02-09 15:22:59 -0800 | [diff] [blame] | 242 | deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++") |
Christopher Ferris | 9df92d6 | 2018-08-21 12:40:08 -0700 | [diff] [blame] | 243 | } else if BoolDefault(test.Properties.Isolated, false) { |
| 244 | deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main") |
Christopher Ferris | 34cbba6 | 2019-07-17 15:46:29 -0700 | [diff] [blame] | 245 | // The isolated library requires liblog, but adding it |
| 246 | // as a static library means unit tests cannot override |
| 247 | // liblog functions. Instead make it a shared library |
| 248 | // dependency. |
| 249 | deps.SharedLibs = append(deps.SharedLibs, "liblog") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 250 | } else { |
| 251 | deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest") |
| 252 | } |
| 253 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 254 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 255 | return deps |
| 256 | } |
| 257 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 258 | func (test *testDecorator) linkerInit(ctx BaseModuleContext, linker *baseLinker) { |
yangbill | b3174d1 | 2018-05-07 06:41:20 +0000 | [diff] [blame] | 259 | // 1. Add ../../lib[64] to rpath so that out/host/linux-x86/nativetest/<test dir>/<test> can |
Colin Cross | bd75e1d | 2017-06-30 17:27:55 -0700 | [diff] [blame] | 260 | // find out/host/linux-x86/lib[64]/library.so |
yangbill | b3174d1 | 2018-05-07 06:41:20 +0000 | [diff] [blame] | 261 | // 2. Add ../../../lib[64] to rpath so that out/host/linux-x86/testcases/<test dir>/<CPU>/<test> can |
| 262 | // also find out/host/linux-x86/lib[64]/library.so |
| 263 | runpaths := []string{"../../lib", "../../../lib"} |
| 264 | for _, runpath := range runpaths { |
| 265 | if ctx.toolchain().Is64Bit() { |
| 266 | runpath += "64" |
| 267 | } |
| 268 | linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, runpath) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 269 | } |
Colin Cross | bd75e1d | 2017-06-30 17:27:55 -0700 | [diff] [blame] | 270 | |
| 271 | // add "" to rpath so that test binaries can find libraries in their own test directory |
| 272 | linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 275 | func (test *testDecorator) linkerProps() []interface{} { |
| 276 | return []interface{}{&test.Properties} |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 279 | func NewTestInstaller() *baseInstaller { |
| 280 | return NewBaseInstaller("nativetest", "nativetest64", InstallInData) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 283 | type testBinary struct { |
| 284 | testDecorator |
| 285 | *binaryDecorator |
| 286 | *baseCompiler |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 287 | Properties TestBinaryProperties |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 288 | data android.Paths |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 289 | testConfig android.Path |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | func (test *testBinary) linkerProps() []interface{} { |
| 293 | props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...) |
| 294 | props = append(props, &test.Properties) |
| 295 | return props |
| 296 | } |
| 297 | |
| 298 | func (test *testBinary) linkerInit(ctx BaseModuleContext) { |
| 299 | test.testDecorator.linkerInit(ctx, test.binaryDecorator.baseLinker) |
| 300 | test.binaryDecorator.linkerInit(ctx) |
| 301 | } |
| 302 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 303 | func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 304 | deps = test.testDecorator.linkerDeps(ctx, deps) |
| 305 | deps = test.binaryDecorator.linkerDeps(ctx, deps) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 306 | return deps |
| 307 | } |
| 308 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 309 | func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 310 | flags = test.binaryDecorator.linkerFlags(ctx, flags) |
| 311 | flags = test.testDecorator.linkerFlags(ctx, flags) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 312 | return flags |
| 313 | } |
| 314 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 315 | func (test *testBinary) install(ctx ModuleContext, file android.Path) { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 316 | test.data = android.PathsForModuleSrc(ctx, test.Properties.Data) |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 317 | var configs []tradefed.Config |
| 318 | if Bool(test.Properties.Require_root) { |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame^] | 319 | configs = append(configs, tradefed.Preparer{"com.android.tradefed.targetprep.RootTargetPreparer", nil}) |
| 320 | } |
| 321 | if Bool(test.Properties.Disable_framework) { |
| 322 | var options []tradefed.Option |
| 323 | options = append(options, tradefed.Option{"run-command", "stop"}) |
| 324 | options = append(options, tradefed.Option{"teardown-command", "start"}) |
| 325 | configs = append(configs, tradefed.Preparer{"com.android.tradefed.targetprep.RunCommandTargetPreparer", options}) |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 326 | } |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 327 | if Bool(test.testDecorator.Properties.Isolated) { |
| 328 | configs = append(configs, tradefed.Option{"not-shardable", "true"}) |
| 329 | } |
yelinhsieh | 9fc6040 | 2018-10-01 19:23:14 +0800 | [diff] [blame] | 330 | if test.Properties.Test_options.Run_test_as != nil { |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 331 | configs = append(configs, tradefed.Option{"run-test-as", String(test.Properties.Test_options.Run_test_as)}) |
yelinhsieh | 9fc6040 | 2018-10-01 19:23:14 +0800 | [diff] [blame] | 332 | } |
| 333 | |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 334 | test.testConfig = tradefed.AutoGenNativeTestConfig(ctx, test.Properties.Test_config, |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 335 | test.Properties.Test_config_template, test.Properties.Test_suites, configs) |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 336 | |
Colin Cross | 600c9df | 2016-09-13 12:26:16 -0700 | [diff] [blame] | 337 | test.binaryDecorator.baseInstaller.dir = "nativetest" |
| 338 | test.binaryDecorator.baseInstaller.dir64 = "nativetest64" |
Dan Willemsen | 3340d60 | 2016-12-27 14:40:40 -0800 | [diff] [blame] | 339 | |
| 340 | if !Bool(test.Properties.No_named_install_directory) { |
| 341 | test.binaryDecorator.baseInstaller.relative = ctx.ModuleName() |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 342 | } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" { |
Dan Willemsen | 3340d60 | 2016-12-27 14:40:40 -0800 | [diff] [blame] | 343 | ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set") |
| 344 | } |
| 345 | |
Dan Willemsen | 1d577e2 | 2016-08-29 15:53:15 -0700 | [diff] [blame] | 346 | test.binaryDecorator.baseInstaller.install(ctx, file) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | func NewTest(hod android.HostOrDeviceSupported) *Module { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 350 | module, binary := NewBinary(hod) |
| 351 | module.multilib = android.MultilibBoth |
Dan Willemsen | 1d577e2 | 2016-08-29 15:53:15 -0700 | [diff] [blame] | 352 | binary.baseInstaller = NewTestInstaller() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 353 | |
| 354 | test := &testBinary{ |
| 355 | testDecorator: testDecorator{ |
| 356 | linker: binary.baseLinker, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 357 | }, |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 358 | binaryDecorator: binary, |
| 359 | baseCompiler: NewBaseCompiler(), |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 360 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 361 | module.compiler = test |
| 362 | module.linker = test |
| 363 | module.installer = test |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 364 | return module |
| 365 | } |
| 366 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 367 | type testLibrary struct { |
| 368 | testDecorator |
| 369 | *libraryDecorator |
| 370 | } |
| 371 | |
| 372 | func (test *testLibrary) linkerProps() []interface{} { |
| 373 | return append(test.testDecorator.linkerProps(), test.libraryDecorator.linkerProps()...) |
| 374 | } |
| 375 | |
| 376 | func (test *testLibrary) linkerInit(ctx BaseModuleContext) { |
| 377 | test.testDecorator.linkerInit(ctx, test.libraryDecorator.baseLinker) |
| 378 | test.libraryDecorator.linkerInit(ctx) |
| 379 | } |
| 380 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 381 | func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 382 | deps = test.testDecorator.linkerDeps(ctx, deps) |
| 383 | deps = test.libraryDecorator.linkerDeps(ctx, deps) |
| 384 | return deps |
| 385 | } |
| 386 | |
| 387 | func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
| 388 | flags = test.libraryDecorator.linkerFlags(ctx, flags) |
| 389 | flags = test.testDecorator.linkerFlags(ctx, flags) |
| 390 | return flags |
| 391 | } |
| 392 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 393 | func NewTestLibrary(hod android.HostOrDeviceSupported) *Module { |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 394 | module, library := NewLibrary(android.HostAndDeviceSupported) |
Dan Willemsen | 28bda51 | 2016-08-31 16:32:55 -0700 | [diff] [blame] | 395 | library.baseInstaller = NewTestInstaller() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 396 | test := &testLibrary{ |
| 397 | testDecorator: testDecorator{ |
| 398 | linker: library.baseLinker, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 399 | }, |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 400 | libraryDecorator: library, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 401 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 402 | module.linker = test |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 403 | return module |
| 404 | } |
| 405 | |
Colin Cross | e28f4e2 | 2017-04-24 18:10:29 -0700 | [diff] [blame] | 406 | type BenchmarkProperties struct { |
Anders Lewis | b97e818 | 2017-07-14 15:20:13 -0700 | [diff] [blame] | 407 | // list of files or filegroup modules that provide data that should be installed alongside |
| 408 | // the test |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 409 | Data []string `android:"path"` |
Anders Lewis | b97e818 | 2017-07-14 15:20:13 -0700 | [diff] [blame] | 410 | |
Colin Cross | e28f4e2 | 2017-04-24 18:10:29 -0700 | [diff] [blame] | 411 | // list of compatibility suites (for example "cts", "vts") that the module should be |
| 412 | // installed into. |
Julien Desprez | e146e39 | 2018-08-02 15:00:46 -0700 | [diff] [blame] | 413 | Test_suites []string `android:"arch_variant"` |
| 414 | |
| 415 | // the name of the test configuration (for example "AndroidTest.xml") that should be |
| 416 | // installed with the module. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 417 | Test_config *string `android:"path,arch_variant"` |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 418 | |
| 419 | // the name of the test configuration template (for example "AndroidTestTemplate.xml") that |
| 420 | // should be installed with the module. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 421 | Test_config_template *string `android:"path,arch_variant"` |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 422 | |
| 423 | // Add RootTargetPreparer to auto generated test config. This guarantees the test to run |
| 424 | // with root permission. |
| 425 | Require_root *bool |
Colin Cross | e28f4e2 | 2017-04-24 18:10:29 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 428 | type benchmarkDecorator struct { |
| 429 | *binaryDecorator |
Colin Cross | e28f4e2 | 2017-04-24 18:10:29 -0700 | [diff] [blame] | 430 | Properties BenchmarkProperties |
Anders Lewis | b97e818 | 2017-07-14 15:20:13 -0700 | [diff] [blame] | 431 | data android.Paths |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 432 | testConfig android.Path |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 435 | func (benchmark *benchmarkDecorator) linkerInit(ctx BaseModuleContext) { |
| 436 | runpath := "../../lib" |
| 437 | if ctx.toolchain().Is64Bit() { |
| 438 | runpath += "64" |
| 439 | } |
| 440 | benchmark.baseLinker.dynamicProperties.RunPaths = append(benchmark.baseLinker.dynamicProperties.RunPaths, runpath) |
| 441 | benchmark.binaryDecorator.linkerInit(ctx) |
| 442 | } |
| 443 | |
Colin Cross | e28f4e2 | 2017-04-24 18:10:29 -0700 | [diff] [blame] | 444 | func (benchmark *benchmarkDecorator) linkerProps() []interface{} { |
| 445 | props := benchmark.binaryDecorator.linkerProps() |
| 446 | props = append(props, &benchmark.Properties) |
| 447 | return props |
| 448 | } |
| 449 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 450 | func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 451 | deps = benchmark.binaryDecorator.linkerDeps(ctx, deps) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 452 | deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark") |
| 453 | return deps |
| 454 | } |
| 455 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 456 | func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 457 | benchmark.data = android.PathsForModuleSrc(ctx, benchmark.Properties.Data) |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 458 | var configs []tradefed.Config |
| 459 | if Bool(benchmark.Properties.Require_root) { |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame^] | 460 | configs = append(configs, tradefed.Preparer{"com.android.tradefed.targetprep.RootTargetPreparer", nil}) |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 461 | } |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 462 | benchmark.testConfig = tradefed.AutoGenNativeBenchmarkTestConfig(ctx, benchmark.Properties.Test_config, |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 463 | benchmark.Properties.Test_config_template, benchmark.Properties.Test_suites, configs) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 464 | |
Colin Cross | 28690e9 | 2017-09-08 16:20:30 -0700 | [diff] [blame] | 465 | benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName()) |
| 466 | benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName()) |
Dan Willemsen | 1d577e2 | 2016-08-29 15:53:15 -0700 | [diff] [blame] | 467 | benchmark.binaryDecorator.baseInstaller.install(ctx, file) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 470 | func NewBenchmark(hod android.HostOrDeviceSupported) *Module { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 471 | module, binary := NewBinary(hod) |
| 472 | module.multilib = android.MultilibBoth |
Colin Cross | 28690e9 | 2017-09-08 16:20:30 -0700 | [diff] [blame] | 473 | binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 474 | |
| 475 | benchmark := &benchmarkDecorator{ |
| 476 | binaryDecorator: binary, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 477 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 478 | module.linker = benchmark |
| 479 | module.installer = benchmark |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 480 | return module |
| 481 | } |