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