blob: 4e6506a3fe6be20c77fb6d72589b3c4108d06de9 [file] [log] [blame]
Avi Drissmandfd880852022-09-15 20:11:091# Copyright 2015 The Chromium Authors
qsrc6c612c2015-01-13 22:07:482# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
qsrfb5251d12015-01-21 15:57:224
5# ==============================================================================
6# TEST SETUP
7# ==============================================================================
8
Yuke Liao2a9b2f0e2021-04-16 00:40:119import("//build/config/chromeos/args.gni")
Yuke Liaoe703384b2020-07-16 01:05:2410import("//build/config/chromeos/ui_mode.gni")
Kevin Marshall36c602c2021-11-04 16:16:2111import("//build/config/devtools.gni")
Dirk Prankeb404c3b2021-06-14 19:57:5012import("//build/config/gclient_args.gni")
Tony Seaward37c51bc2024-10-28 18:22:5713import("//build/config/rts.gni")
danakj482580a2022-11-18 18:00:5914import("//build/rust/rust_static_library.gni")
Mirko Bonadei4a0df432020-09-08 19:06:0215import("//build_overrides/build.gni")
Yuke Liaoe703384b2020-07-16 01:05:2416
Greg Guterman6963dc02021-04-07 05:20:5917declare_args() {
Dirk Prankeb404c3b2021-06-14 19:57:5018 # Some component repos (e.g. ANGLE) import //testing but do not have
19 # "location_tags.json", and so we don't want to try and upload the tags
20 # for their tests.
21 # And, some build configs may simply turn off generation altogether.
22 tests_have_location_tags = generate_location_tags
Greg Guterman6963dc02021-04-07 05:20:5923}
24
David Dorwin621c5072022-03-30 00:32:5325# On Fuchsia, the test executable has a suffix and is a dependency of the
26# common |target_name| target. For `visibility`, the executable must be
27# specified. Cross-platform targets that include `test` targets in their
28# visibility lists, add `${exec_target_suffix}` immediately after the test
29# target name. This is not necessary when the target is a `source_set`.
30if (is_fuchsia) {
31 exec_target_suffix = "__exec"
32} else {
33 exec_target_suffix = ""
34}
35
jcivellif4462a352017-01-10 04:45:5936if (is_android) {
Ben Pastene1e985542024-09-25 00:41:4237 import("//build/android/test_wrapper/logdog_wrapper.gni")
jcivellif4462a352017-01-10 04:45:5938 import("//build/config/android/config.gni")
Charlie Hud98dc692021-12-08 01:01:0239 import("//build/config/android/create_unwind_table.gni")
James Cook209256f2018-12-07 18:40:5040 import("//build/config/android/extract_unwind_tables.gni")
jcivellif4462a352017-01-10 04:45:5941 import("//build/config/android/rules.gni")
Abhishek Arya2f5f7342018-06-13 16:59:4442 import("//build/config/sanitizers/sanitizers.gni")
Dirk Prankedd4ff742020-11-18 19:57:3243} else if (is_fuchsia) {
Kevin Marshall55fd8522019-10-04 22:47:0144 import("//build/config/fuchsia/generate_runner_scripts.gni")
Wez691dde42023-10-19 17:47:2945 import("//third_party/fuchsia-gn-sdk/src/cmc.gni")
46 import("//third_party/fuchsia-gn-sdk/src/component.gni")
47 import("//third_party/fuchsia-gn-sdk/src/package.gni")
Nico Weberd73c90382022-03-30 20:37:5048} else if (is_chromeos && is_chromeos_device) {
Ben Pastene4c35c572018-04-30 23:21:4849 import("//build/config/chromeos/rules.gni")
Dirk Prankedd4ff742020-11-18 19:57:3250 import("//build/config/sanitizers/sanitizers.gni")
Dirk Pranke6188075b2020-10-01 19:31:2851 import("//build/util/generate_wrapper.gni")
Dirk Prankedd4ff742020-11-18 19:57:3252} else if (is_ios) {
Jeff Yoonf7f4eb42020-03-06 18:55:3653 import("//build/config/ios/ios_sdk.gni")
54 import("//build/config/ios/ios_test_runner_wrapper.gni")
55 import("//build/config/ios/rules.gni")
Dirk Prankedd4ff742020-11-18 19:57:3256} else {
Dirk Pranke31e346e2020-07-15 00:54:0657 import("//build/config/sanitizers/sanitizers.gni")
58 import("//build/util/generate_wrapper.gni")
59}
60
Paul Semel2c1673cc02024-01-31 15:30:4961# This template represents the core common functionality of a test target
62# on each platform. It includes:
63# * the ability to generate a rust library that includes all .rs files found
64# in sources and depends on that from the test target.
65# * the ability to recognize any declare fuzztests and build runners for them.
danakjebb9cc4d2022-03-04 21:30:1166template("mixed_test") {
67 assert(defined(invoker.target_type) && invoker.target_type != "")
danakjebb9cc4d2022-03-04 21:30:1168
danakjca3cae62023-04-12 15:20:5769 # The crate_root variable would transform the target into a Rust binary
70 # which is incorrect. To not use a generated crate root set:
71 # ```
72 # test_crate_root = "path/to/root.rs"
73 # ```
74 assert(!defined(invoker.crate_root))
75
danakj482580a2022-11-18 18:00:5976 _rs_vars = [
77 "sources", # We split this list into two.
78 "crate_name", # Android test template overrides the crate name.
79 ]
danakjaa85aad2022-03-10 18:45:1080
danakj482580a2022-11-18 18:00:5981 if (defined(invoker.sources)) {
82 _rs_sources = filter_include(invoker.sources, [ "*.rs" ])
83 _cc_sources = filter_exclude(invoker.sources, [ "*.rs" ])
84 } else {
85 _rs_sources = []
86 _cc_sources = []
87 }
88
89 if (_rs_sources != []) {
Daniel Cheng8629dd7e2023-11-22 10:56:4590 # Note: as a weak convention, __ is usually used before a suffix for
91 # internally-generated targets. However, rust_target requires a strict
92 # snake_case name.
danakjc858ce72022-12-20 21:39:5193 if (defined(invoker.crate_name)) {
94 _rust_target_name = "${invoker.crate_name}_rust_objects"
95 } else {
96 _rust_target_name = "${target_name}_rust_objects"
97 }
danakj482580a2022-11-18 18:00:5998
99 # We could automatically add `deps += [ "//testing/rust_gtest_interop" ]`
danakjebb9cc4d2022-03-04 21:30:11100 # if `rs_sources` is non-empty. But we don't automatically provide
101 # //testing/gtest either so it would be asymmetric and could break in that
danakj482580a2022-11-18 18:00:59102 # case. So, we act instead as if //testing/rust_gtest_interop is part of
103 # the //testing/gtest dependency. If you add one, and have `rs_sources`
104 # listed, you get both.
danakjebb9cc4d2022-03-04 21:30:11105 _gtest_is_in_deps = false
danakj482580a2022-11-18 18:00:59106 if (defined(invoker.deps) && invoker.deps != []) {
107 foreach(dep, invoker.deps) {
danakjc1f000c2022-11-18 19:31:06108 if (get_label_info(dep, "label_no_toolchain") ==
109 "//testing/gtest:gtest") {
danakjebb9cc4d2022-03-04 21:30:11110 _gtest_is_in_deps = true
111 }
112 }
113 }
danakj482580a2022-11-18 18:00:59114
115 # TODO(danakj): This could be a rust_source_set perhaps, the point being
116 # that we need to link in all the .o object files inside the library,
117 # instead of dropping unreachable ones during linking (which would drop the
118 # tests). Alternatively we could use a special name suffix or other similar
119 # trick perhaps to ensure that all object files are linked in here.
120 rust_static_library(_rust_target_name) {
121 forward_variables_from(invoker,
122 TESTONLY_AND_VISIBILITY + [
danakjca3cae62023-04-12 15:20:57123 "allow_unsafe",
danakj482580a2022-11-18 18:00:59124 "deps",
danakjca3cae62023-04-12 15:20:57125 "generate_crate_root",
danakj482580a2022-11-18 18:00:59126 "public_deps",
127 ])
128 configs += [ "//build/rust:test" ]
danakjca3cae62023-04-12 15:20:57129 if (defined(invoker.test_crate_root)) {
130 crate_root = invoker.test_crate_root
131 } else {
132 generate_crate_root = true
133 }
danakj482580a2022-11-18 18:00:59134 sources = _rs_sources
Daniel Cheng8629dd7e2023-11-22 10:56:45135 is_gtest_unittests = true
danakjca3cae62023-04-12 15:20:57136
danakj482580a2022-11-18 18:00:59137 if (_gtest_is_in_deps) {
138 deps += [ "//testing/rust_gtest_interop" ]
danakjebb9cc4d2022-03-04 21:30:11139 }
danakj482580a2022-11-18 18:00:59140 }
141 } else {
142 not_needed(invoker, _rs_vars)
143 }
144
Sam Maierb63502c2023-09-15 16:24:41145 if (invoker.target_type == "shared_library_with_jni") {
146 # Needed for shared_library_with_jni. Keeping this import guarded so
147 # that projects who import //testing but not //third_party/jni_zero
148 # don't have issues.
149 import("//third_party/jni_zero/jni_zero.gni")
150 }
danakjca3cae62023-04-12 15:20:57151
Paul Semelffe54d42024-04-16 10:01:30152 _building_fuzztest_fuzzer =
Adrian Taylor519e6b82024-10-22 13:44:59153 defined(invoker.fuzztests) && use_fuzzing_engine && (is_linux || is_mac)
Paul Semelffe54d42024-04-16 10:01:30154
155 # Fuzz tests are small fuzzers that do not require particularly-powerful
156 # machines to run, so we do not build them when `high_end_fuzzer_targets`
157 # is true and we are building fuzztests in fuzzing mode.
158 if (_building_fuzztest_fuzzer && high_end_fuzzer_targets) {
159 not_needed(invoker, "*")
160 not_needed("*")
161
162 # We still want a reachable target, so make it a no-op empty group. This
163 # will let the fuzzer builders crawl the build graph and invoke ninja in
164 # the same way regardless of GN args.
165 group(target_name) {
danakjebb9cc4d2022-03-04 21:30:11166 }
Paul Semelffe54d42024-04-16 10:01:30167 } else {
168 target(invoker.target_type, target_name) {
Titouan Rigoudybacddcc92025-02-05 10:39:51169 forward_variables_from(invoker,
170 "*",
171 TESTONLY_AND_VISIBILITY + _rs_vars + [
172 "fuzztests",
173 "fuzztest_stack_limit_kb",
174 ])
Paul Semelffe54d42024-04-16 10:01:30175 forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
176 sources = _cc_sources
177 if (!defined(deps)) {
178 deps = []
179 }
180 if (!defined(ldflags)) {
181 ldflags = []
182 }
183
184 if (_rs_sources != []) {
185 deps += [ ":${_rust_target_name}" ]
186 }
187 if (defined(invoker.fuzztests)) {
Adrian Taylor6337f562024-06-06 06:18:23188 deps += [
189 "//third_party/fuzztest",
190
191 # The following target contains a static initializer which
192 # will ensure that the code in base/test:test_support
193 # ends up calling through to fuzztest::InitFuzzTest.
Adrian Taylorde84dff2024-11-01 18:00:22194 "//testing/libfuzzer:confirm_fuzztest_init",
Adrian Taylor6337f562024-06-06 06:18:23195 ]
Paul Semelffe54d42024-04-16 10:01:30196 }
Paul Semel2c1673cc02024-01-31 15:30:49197 }
198 }
199
Paul Semelffe54d42024-04-16 10:01:30200 if (_building_fuzztest_fuzzer && !high_end_fuzzer_targets) {
Paul Semel2c1673cc02024-01-31 15:30:49201 # This test contains fuzztests. We want to package them up in a way
202 # which ClusterFuzz knows how to extract. We need to:
203 # 1) make an executable for each individual fuzz test;
Paul Semelffe54d42024-04-16 10:01:30204 # 2) check that the fuzztests variable is correct.
Paul Semel2c1673cc02024-01-31 15:30:49205 # At present, all this is likely to work only if invoker.target_type
206 # is 'executable', since we generate a wrapper script that assumes so.
207 # At the moment, we aim to fuzz these fuzztests only on Linux so that's
208 # fine. In future we may need to broaden this.
209 if (defined(invoker.output_name)) {
210 _output_name = invoker.output_name
211 } else {
212 _output_name = target_name
213 }
214
Paul Semel2c1673cc02024-01-31 15:30:49215 _fuzzer_binary_extension = ""
216 if (is_win) {
217 _fuzzer_binary_extension = ".exe"
218 }
Paul Semel57d8b1392024-03-28 16:53:02219
220 # This will be the actual name of the fuzzer binary generated by
221 # `target_name`.
222 _fuzzer_binary_name = _output_name + _fuzzer_binary_extension
223 _fuzztest_target_name = target_name
Paul Semel2c1673cc02024-01-31 15:30:49224
Titouan Rigoudybacddcc92025-02-05 10:39:51225 # Additional command-line arguments passed to fuzzer executables, common to
226 # all fuzz tests in this executable.
227 _common_fuzzer_args = [ "--corpus_database=" ]
228
229 if (use_centipede && defined(invoker.fuzztest_stack_limit_kb)) {
230 _common_fuzzer_args +=
231 [ "--stack_limit_kb=${invoker.fuzztest_stack_limit_kb}" ]
232 }
233
Paul Semel2c1673cc02024-01-31 15:30:49234 # Confirming that the "fuzztests =" directive is correct can only
235 # be done on builds where we can confidently run the fuzzing binary.
236 # Let's be conservative about that -- so long as any failures are
237 # spotted by at least one CI bot we should be good.
238 confirm_fuzztest_contents = is_asan || !using_sanitizer
239
240 if (confirm_fuzztest_contents) {
241 # Confirm that the fuzztests GN variable matches with the
242 # actual fuzztests in the binary. The output of this action is unused.
243 # It just exists to fail the build if there's an error.
244 # We only do this on Linux, and not for any sanitizers other than
245 # ASAN, because that's specific for CI to show problems and there
246 # may be unknown problems running the fuzztest binary on other
247 # platforms.
248 _fuzztest_check_action = target_name + "__fuzztest_check"
249 action(_fuzztest_check_action) {
250 deps = [ ":" + _fuzztest_target_name ]
251 testonly = true
252 script = "//testing/libfuzzer/confirm_fuzztests.py"
253 _output_name = "$target_gen_dir/${target_name}__checked.txt"
254 outputs = [ _output_name ]
255
256 args = [
257 "--executable",
258 rebase_path(
259 get_label_info(_fuzztest_target_name, "root_out_dir") +
260 "/" + _fuzzer_binary_name),
261 "--output",
262 rebase_path(_output_name),
263 "--fuzztests",
264 ] + invoker.fuzztests
265 }
266 }
267
268 # Make a wrapper executable for each individual fuzz test
269 foreach(fuzztest_unit, invoker.fuzztests) {
270 _fuzzer_name = target_name + "_" +
271 string_replace(fuzztest_unit, ".", "_") + "_fuzzer"
272
273 # We generate an actual executable because currently our fuzz
274 # builder recipes use `gn refs --type=executable` to find things
275 # to build. Otherwise we could use generate_wrapper or equivalent
276 # to make a python script. We could alter the recipe, or rearrange
277 # deps arragenements so that some other executable depends on these
278 # scripts, but that seems worse. The executable might be more cross-
279 # platform too.
280 _fuzztest_generate_fuzzer = _fuzzer_name + "__generate"
281
282 generated_file(_fuzztest_generate_fuzzer + "_constants") {
283 outputs = [ "$target_gen_dir/$target_name/constants.cpp" ]
Adrian Taylor8c585712024-02-06 18:46:30284
Titouan Rigoudybacddcc92025-02-05 10:39:51285 _fuzzer_args = _common_fuzzer_args + [ "--fuzz=${fuzztest_unit}" ]
Dan McArdle23b6a6fe2024-12-16 15:46:09286
Titouan Rigoudybacddcc92025-02-05 10:39:51287 fuzzer_args_joined = string_join(" ", _fuzzer_args)
Dan McArdle23b6a6fe2024-12-16 15:46:09288
289 NEWLINE = "$0x0A"
290
291 contents =
292 "const char* kFuzzerArgs = \"${fuzzer_args_joined}\";" + NEWLINE +
293 "const char* kFuzzerBinary = \"${_fuzzer_binary_name}\";" + NEWLINE
Paul Semel2c1673cc02024-01-31 15:30:49294 }
295
Paul Semel1e37ba62024-03-27 20:48:12296 _fuzzer_target_name = target_name
Paul Semel2c1673cc02024-01-31 15:30:49297 executable(_fuzztest_generate_fuzzer) {
298 testonly = true
Paul Semel1e37ba62024-03-27 20:48:12299 data_deps = [
300 ":" + _fuzztest_target_name,
301 ":" + _fuzzer_target_name,
302 ]
Paul Semel2c1673cc02024-01-31 15:30:49303 deps = [
Paul Semel2c1673cc02024-01-31 15:30:49304 "//testing/libfuzzer:individual_fuzztest_wrapper",
305 ":" + _fuzztest_generate_fuzzer + "_constants",
306 ]
307 if (confirm_fuzztest_contents) {
308 deps += [ ":" + _fuzztest_check_action ]
309 }
310 output_name = _fuzzer_name
311 sources =
312 get_target_outputs(":" + _fuzztest_generate_fuzzer + "_constants")
313 write_runtime_deps = "$root_build_dir/${_fuzzer_name}.runtime_deps"
314 }
315 }
danakjebb9cc4d2022-03-04 21:30:11316 }
317}
318
qsrfb5251d12015-01-21 15:57:22319# Define a test as an executable (or apk on Android) with the "testonly" flag
320# set.
agrieve62ab00282016-04-05 02:03:45321# Variable:
Dirk Pranke79d065d2020-08-29 03:28:30322# use_xvfb: (optional) whether to run the executable under Xvfb.
danakj482580a2022-11-18 18:00:59323# use_raw_android_executable: Use executable() rather than android_apk().
ynovikov389d9e442016-05-27 02:34:56324# use_native_activity: Test implements ANativeActivity_onCreate().
Greg Thompson318cd692022-03-28 08:12:06325# test_runner_shard: (Fuchsia, optional): for CFv2 tests, use the given test
Greg Thompsonfd269652022-10-28 12:06:55326# runner shard rather than the default shard for the ELF runner when
327# assembling the test component. This is useful, for example, to use the
328# elf_test_ambient_exec_runner for tests that require
329# job_policy_ambient_mark_vmo_exec.
330# fuchsia_package_deps: (Fuchsia, optional) List of fuchsia_component()
331# targets that this test package contains.
Mirko Bonadei15522bc2020-09-16 20:38:39332# is_xctest: (iOS, optional) whether to build the executable as XCTest.
333# Similar to the GN arg 'enable_run_ios_unittests_with_xctest' but
334# for build targets.
Stefano Duo4128b6b2021-08-02 21:24:43335# allow_cleartext_traffic: (Android, optional) whether to allow cleartext
336# network requests during the test.
Adrian Taylora20bc5a2024-01-29 23:22:26337# fuzztests: a list of instances of the FUZZ_TEST macro to
Adrian Taylor62dbea52023-10-25 20:29:16338# include fuzzing tests alongside unit tests. This introduces an
339# extra dependency and also creates additional metadata so that our
340# fuzzing infrastructure can find and run such tests.
Adrian Taylora20bc5a2024-01-29 23:22:26341# This should be a list of the test names, for example
342# fuzztests = [ "MyTestClass.MyTestName" ]
Titouan Rigoudybacddcc92025-02-05 10:39:51343# fuzztest_stack_limit_kb: soft limit on the size of the stack in KB, enforced
344# for fuzz tests in fuzzing mode only.
qsrfb5251d12015-01-21 15:57:22345template("test") {
Tony Seaward37c51bc2024-10-28 18:22:57346 # Ensures a test filter file exists and if not, creates a dummy file.
347 # The Regression Test Selection (rts) flag is passed in mb.py and used
348 # to filter out test cases. Flag ensures that a file exists.
349 if (use_rts) {
350 action("${target_name}__rts_filters") {
351 script = "//build/add_rts_filters.py"
352 rts_file = "${root_build_dir}/gen/rts/${invoker.target_name}.filter"
353 args = [ rebase_path(rts_file, root_build_dir) ]
354 outputs = [ rts_file ]
355 }
356 }
Andrew Grieve1b290e4a22020-11-24 20:07:01357 testonly = true
Mirko Bonadei15522bc2020-09-16 20:38:39358 if (!is_ios) {
359 assert(!defined(invoker.is_xctest) || !invoker.is_xctest,
360 "is_xctest can be set only for iOS builds")
361 }
Stefano Duo4128b6b2021-08-02 21:24:43362 if (!is_android) {
363 assert(!defined(invoker.allow_cleartext_traffic),
364 "allow_cleartext_traffic can be set only for Android tests")
365 }
366
qsrfb5251d12015-01-21 15:57:22367 if (is_android) {
Dirk Pranke79d065d2020-08-29 03:28:30368 assert(!defined(invoker.use_xvfb) || !invoker.use_xvfb)
369
Peter Kotwicz10742f82021-04-15 22:32:50370 _use_default_launcher =
371 !defined(invoker.use_default_launcher) || invoker.use_default_launcher
372 if (!defined(invoker.use_raw_android_executable)) {
373 # Checkouts where build_with_chromium == false often have a custom GN
374 # template wrapper around test() which sets use_default_launcher == false.
375 # Set the _use_raw_android_executable default so that test() targets which
376 # do not use the custom wrapper
377 # (1) Do not cause "gn gen" to fail
378 # (2) Do not need to be moved into if(build_with_chromium) block.
379 _use_raw_android_executable =
380 !build_with_chromium && _use_default_launcher
381 } else {
382 not_needed([ "_use_default_launcher" ])
383 _use_raw_android_executable = invoker.use_raw_android_executable
384 }
qsrfb5251d12015-01-21 15:57:22385
agrieve67855de2016-03-30 14:46:01386 # output_name is used to allow targets with the same name but in different
387 # packages to still produce unique runner scripts.
388 _output_name = invoker.target_name
mikecase56d80d72015-06-03 00:57:26389 if (defined(invoker.output_name)) {
agrieve67855de2016-03-30 14:46:01390 _output_name = invoker.output_name
mikecase56d80d72015-06-03 00:57:26391 }
agrieve62ab00282016-04-05 02:03:45392
agrieveb355ad152016-04-19 03:45:23393 _test_runner_target = "${_output_name}__test_runner_script"
jbudorickd29ecfa72016-11-18 22:45:42394 _wrapper_script_vars = [
Andrew Grievea9183012025-02-12 15:05:58395 "additional_apks",
Jamie Madill73b9af332022-08-03 19:27:47396 "android_test_runner_script",
Haiyang Pan88eaf7502024-03-21 22:08:11397 "extra_args",
agrievee41ae190d2016-04-25 14:12:51398 "ignore_all_data_deps",
jbudorickd29ecfa72016-11-18 22:45:42399 "shard_timeout",
agrievee41ae190d2016-04-25 14:12:51400 ]
agrieve3ac557f02016-04-12 15:52:00401
jbudorickced2a252016-06-09 16:38:54402 assert(_use_raw_android_executable || enable_java_templates)
403
agrieve62ab00282016-04-05 02:03:45404 if (_use_raw_android_executable) {
Peter Kotwicz13a827a62021-04-22 22:34:49405 not_needed(invoker, [ "add_unwind_tables_in_apk" ])
406
agrieve62ab00282016-04-05 02:03:45407 _exec_target = "${target_name}__exec"
408 _dist_target = "${target_name}__dist"
409 _exec_output =
410 "$target_out_dir/${invoker.target_name}/${invoker.target_name}"
danakj505b7f062023-07-05 19:02:02411 _crate_name = "${target_name}"
agrieve62ab00282016-04-05 02:03:45412
danakjebb9cc4d2022-03-04 21:30:11413 mixed_test(_exec_target) {
414 target_type = "executable"
415
danakj505b7f062023-07-05 19:02:02416 # Use a crate name that avoids creating a warning due to double
417 # underscore (ie. `__`).
418 crate_name = _crate_name
419
danakje94f40d2022-02-16 18:13:53420 # Configs will always be defined since we set_defaults in
421 # BUILDCONFIG.gn.
agrieve62ab00282016-04-05 02:03:45422 configs = []
Dirk Pranke19a58732021-03-24 22:26:22423 forward_variables_from(
424 invoker,
425 "*",
426 TESTONLY_AND_VISIBILITY + _wrapper_script_vars + [
427 "data_deps",
428 "extra_dist_files",
429 ])
agrieve62ab00282016-04-05 02:03:45430
431 # Thanks to the set_defaults() for test(), configs are initialized with
432 # the default shared_library configs rather than executable configs.
Thomas Anderson11c1d9822019-01-15 06:32:59433 configs -= [
434 "//build/config:shared_library_config",
435 "//build/config/android:hide_all_but_jni",
436 ]
agrieve62ab00282016-04-05 02:03:45437 configs += [ "//build/config:executable_config" ]
438
Dirk Pranke19a58732021-03-24 22:26:22439 if (defined(invoker.data_deps)) {
440 data_deps = invoker.data_deps
441 } else {
442 data_deps = []
443 }
444 if (!defined(data)) {
445 data = []
446 }
Jamie Madilldd60ee62021-04-13 19:25:52447 if (tests_have_location_tags) {
448 data += [ "//testing/location_tags.json" ]
449 }
Adrian Taylor62dbea52023-10-25 20:29:16450 if (!defined(deps)) {
451 deps = []
452 }
Dirk Pranke19a58732021-03-24 22:26:22453
agrieve62ab00282016-04-05 02:03:45454 # Don't output to the root or else conflict with the group() below.
455 output_name = rebase_path(_exec_output, root_out_dir)
Tony Seaward37c51bc2024-10-28 18:22:57456
457 if (use_rts) {
458 data_deps += [ ":${invoker.target_name}__rts_filters" ]
459 }
agrieve62ab00282016-04-05 02:03:45460 }
461
462 create_native_executable_dist(_dist_target) {
agrieve62ab00282016-04-05 02:03:45463 dist_dir = "$root_out_dir/$target_name"
464 binary = _exec_output
Nico Weber852532f2020-01-28 18:17:22465 deps = [ ":$_exec_target" ]
agrieve62ab00282016-04-05 02:03:45466 if (defined(invoker.extra_dist_files)) {
467 extra_files = invoker.extra_dist_files
468 }
Tony Seaward37c51bc2024-10-28 18:22:57469 if (use_rts) {
470 if (!defined(data_deps)) {
471 data_deps = []
472 }
473 data_deps += [ ":${invoker.target_name}__rts_filters" ]
474 }
agrieve62ab00282016-04-05 02:03:45475 }
476 } else {
Andrew Grieveee8aa44d2022-09-23 17:14:38477 _library_target_name = "${target_name}__library"
danakj98e073722022-02-24 21:01:49478 _library_crate_name = "${target_name}_library"
Andrew Grieveee8aa44d2022-09-23 17:14:38479 _apk_target_name = "${target_name}__apk"
agrieve62ab00282016-04-05 02:03:45480 _apk_specific_vars = [
Stefano Duo4128b6b2021-08-02 21:24:43481 "allow_cleartext_traffic",
agrieve62ab00282016-04-05 02:03:45482 "android_manifest",
agrievec6811b422016-06-23 02:25:09483 "android_manifest_dep",
Peter Kotwiczab1b5422021-03-30 22:58:27484 "android_manifest_template",
Clark DuVall1bee5322020-06-10 05:51:55485 "app_as_shared_lib",
Haiyang Pan5c01df52024-03-11 16:47:23486 "keystore_name",
487 "keystore_password",
488 "keystore_path",
Alexander Cooper791f06f2025-01-22 21:17:54489 "library_always_compress",
Andrew Grieve43f24fd02022-04-06 23:04:04490 "loadable_module_deps",
Haiyang Pan5c01df52024-03-11 16:47:23491 "loadable_modules",
Tibor Goldschwendt95db95d2019-06-17 20:32:02492 "min_sdk_version",
Haiyang Pan5c01df52024-03-11 16:47:23493 "product_config_java_packages",
huapenglc35ba6e2016-05-25 23:08:07494 "proguard_configs",
495 "proguard_enabled",
Fred Mello0cc91c62019-08-24 01:53:45496 "srcjar_deps",
Tibor Goldschwendt95db95d2019-06-17 20:32:02497 "target_sdk_version",
agrieve62ab00282016-04-05 02:03:45498 "use_default_launcher",
ynovikov389d9e442016-05-27 02:34:56499 "use_native_activity",
agrieve62ab00282016-04-05 02:03:45500 ]
Siddhartha764226b2018-03-13 02:32:55501
Andrew Grieveee8aa44d2022-09-23 17:14:38502 _add_unwind_tables_in_apk =
503 defined(invoker.add_unwind_tables_in_apk) &&
504 invoker.add_unwind_tables_in_apk && target_cpu == "arm"
Andrew Grieved5fdf2af2022-08-23 07:47:55505
Siddhartha764226b2018-03-13 02:32:55506 # Adds the unwind tables from unstripped binary as an asset file in the
507 # apk, if |add_unwind_tables_in_apk| is specified by the test.
Andrew Grieved5fdf2af2022-08-23 07:47:55508 if (_add_unwind_tables_in_apk) {
Alison Gale47d1537d2024-04-19 21:31:46509 # TODO(crbug.com/40833600): Remove generation of v1 unwind asset when
Tushar Agarwaldcafb622022-11-30 17:32:27510 # `CFIBacktraceAndroid` is replaced with `ChromeUnwinderAndroid`.
Andrew Grieveee8aa44d2022-09-23 17:14:38511 _unwind_table_name = "${_library_target_name}_unwind_v1"
512 unwind_table_v1(_unwind_table_name) {
513 library_target = ":$_library_target_name"
514 }
515
Arthur Sonzogni54424e92022-09-23 13:30:45516 if (use_android_unwinder_v2) {
Andrew Grieveee8aa44d2022-09-23 17:14:38517 _unwind_table_v2_name = "${_library_target_name}_unwind_v2"
518 unwind_table_v2(_unwind_table_v2_name) {
519 library_target = ":$_library_target_name"
Arthur Sonzogni54424e92022-09-23 13:30:45520 }
521 }
522
Andrew Grieveee8aa44d2022-09-23 17:14:38523 _unwind_table_asset_name = "${target_name}__unwind_assets"
524 android_assets(_unwind_table_asset_name) {
525 sources = [ "$target_out_dir/$_unwind_table_name/$unwind_table_asset_v1_filename" ]
526 disable_compression = true
527 deps = [ ":$_unwind_table_name" ]
528 if (use_android_unwinder_v2) {
529 sources += [ "$target_out_dir/$_unwind_table_v2_name/$unwind_table_asset_v2_filename" ]
530 deps += [ ":$_unwind_table_v2_name" ]
531 }
Siddhartha764226b2018-03-13 02:32:55532 }
533 }
534
Sam Maierbc320a9482023-05-17 19:44:16535 _generate_final_jni =
536 !defined(invoker.generate_final_jni) || invoker.generate_final_jni
Andrew Grieveee8aa44d2022-09-23 17:14:38537 mixed_test(_library_target_name) {
Sam Maierbc320a9482023-05-17 19:44:16538 if (_generate_final_jni) {
539 target_type = "shared_library_with_jni"
540 java_targets = [ ":$_apk_target_name" ]
541 } else {
542 target_type = "shared_library"
543 }
danakjebb9cc4d2022-03-04 21:30:11544
danakj98e073722022-02-24 21:01:49545 # Configs will always be defined since we set_defaults in
546 # BUILDCONFIG.gn.
agrieve62ab00282016-04-05 02:03:45547 configs = [] # Prevent list overwriting warning.
548 configs = invoker.configs
agrieve62ab00282016-04-05 02:03:45549
jbudorickd29ecfa72016-11-18 22:45:42550 forward_variables_from(
551 invoker,
552 "*",
Andrew Grieved5fdf2af2022-08-23 07:47:55553 [
554 "configs",
555 "deps",
556 ] + _apk_specific_vars + _wrapper_script_vars +
Peter Wen2052bd12020-12-03 20:15:07557 TESTONLY_AND_VISIBILITY)
558
danakj482580a2022-11-18 18:00:59559 # Use a crate name that avoids creating a warning due to double
560 # underscore (ie. `__`).
561 crate_name = _library_crate_name
562
Peter Wen2052bd12020-12-03 20:15:07563 # Native targets do not need to depend on java targets. Filter them out
564 # so that the shared library can be built without needing to wait for
565 # dependent java targets.
Paul Semel2c1673cc02024-01-31 15:30:49566 if (!defined(deps)) {
567 deps = []
568 }
Peter Wen2052bd12020-12-03 20:15:07569 if (defined(invoker.deps)) {
Adrian Taylor62dbea52023-10-25 20:29:16570 deps += filter_exclude(invoker.deps, java_target_patterns)
Peter Wen2052bd12020-12-03 20:15:07571 }
agrieve62ab00282016-04-05 02:03:45572
Peter Kotwiczb9957d62021-04-12 21:09:43573 if (_use_default_launcher) {
Tom Andersonce772fa2018-05-30 22:20:37574 deps += [ "//testing/android/native_test:native_test_native_code" ]
agrieve62ab00282016-04-05 02:03:45575 }
576 }
Andrew Grieveee8aa44d2022-09-23 17:14:38577 unittest_apk(_apk_target_name) {
Daniel Bratellfdda4652019-01-31 15:45:54578 forward_variables_from(invoker, _apk_specific_vars)
Andrew Grieveee8aa44d2022-09-23 17:14:38579 shared_library = ":$_library_target_name"
Sam Maierbc320a9482023-05-17 19:44:16580 if (_generate_final_jni) {
581 srcjar_deps = [ "${shared_library}__jni_registration" ]
Sam Maierbc320a9482023-05-17 19:44:16582 }
agrieve62ab00282016-04-05 02:03:45583 apk_name = invoker.target_name
584 if (defined(invoker.output_name)) {
585 apk_name = invoker.output_name
agrieve62ab00282016-04-05 02:03:45586 }
agrieveb355ad152016-04-19 03:45:23587
Daniel Bratellfdda4652019-01-31 15:45:54588 if (defined(invoker.deps)) {
589 deps = invoker.deps
590 } else {
591 deps = []
592 }
Andrew Grieve43f24fd02022-04-06 23:04:04593 if (defined(loadable_module_deps)) {
594 deps += loadable_module_deps
595 }
Daniel Bratellfdda4652019-01-31 15:45:54596
jcivellif4462a352017-01-10 04:45:59597 # Add the Java classes so that each target does not have to do it.
Peter Kotwiczb9957d62021-04-12 21:09:43598 if (_use_default_launcher) {
599 deps += [ "//base/test:test_support_java" ]
600 }
jcivellif4462a352017-01-10 04:45:59601
Siddhartha764226b2018-03-13 02:32:55602 if (defined(_unwind_table_asset_name)) {
603 deps += [ ":${_unwind_table_asset_name}" ]
604 }
Tony Seaward37c51bc2024-10-28 18:22:57605
606 if (use_rts) {
607 data_deps = [ ":${invoker.target_name}__rts_filters" ]
608 }
agrieve62ab00282016-04-05 02:03:45609 }
Andrew Grievee1dc23f2019-10-22 16:26:36610 }
agrieve62ab00282016-04-05 02:03:45611
Andrew Grievee1dc23f2019-10-22 16:26:36612 test_runner_script(_test_runner_target) {
613 forward_variables_from(invoker, _wrapper_script_vars)
estevensonce8443922016-12-15 19:57:57614
Andrew Grievee1dc23f2019-10-22 16:26:36615 if (_use_raw_android_executable) {
616 executable_dist_dir = "$root_out_dir/$_dist_target"
John Budorickd5dccb22020-02-01 02:16:34617 data_deps = [ ":$_exec_target" ]
Andrew Grievee1dc23f2019-10-22 16:26:36618 } else {
Andrew Grieveee8aa44d2022-09-23 17:14:38619 apk_target = ":$_apk_target_name"
Andrew Grievee1dc23f2019-10-22 16:26:36620 incremental_apk = incremental_install
Andrew Grieve7ca6de32019-10-18 03:57:47621
622 # Dep needed for the test runner .runtime_deps file to pick up data
623 # deps from the forward_variables_from(invoker, "*") on the library.
Andrew Grieveee8aa44d2022-09-23 17:14:38624 data_deps = [ ":$_library_target_name" ]
agrieve62ab00282016-04-05 02:03:45625 }
Andrew Grievee1dc23f2019-10-22 16:26:36626 test_name = _output_name
627 test_suite = _output_name
628 test_type = "gtest"
Tony Seaward37c51bc2024-10-28 18:22:57629 if (use_rts) {
630 data_deps += [ ":${invoker.target_name}__rts_filters" ]
631 }
mikecase56d80d72015-06-03 00:57:26632 }
633
Andrew Grieve7ca6de32019-10-18 03:57:47634 # Create a wrapper script rather than using a group() in order to ensure
635 # "ninja $target_name" always works. If this was a group(), then GN would
636 # not create a top-level alias for it if a target exists in another
637 # directory with the same $target_name.
638 # Also - bots run this script directly for "components_perftests".
639 generate_wrapper(target_name) {
Andrew Grieve1b290e4a22020-11-24 20:07:01640 forward_variables_from(invoker, [ "visibility" ])
Andrew Grieve7ca6de32019-10-18 03:57:47641 executable = "$root_build_dir/bin/run_$_output_name"
642 wrapper_script = "$root_build_dir/$_output_name"
Nico Weber852532f2020-01-28 18:17:22643 deps = [ ":$_test_runner_target" ]
jbudorick686094f62017-05-04 21:52:40644 if (_use_raw_android_executable) {
Andrew Grieve7ca6de32019-10-18 03:57:47645 deps += [ ":$_dist_target" ]
jbudorick686094f62017-05-04 21:52:40646 } else {
Andrew Grieve7ca6de32019-10-18 03:57:47647 # Dep needed for the swarming .isolate file to pick up data
648 # deps from the forward_variables_from(invoker, "*") on the library.
649 deps += [
Andrew Grieveee8aa44d2022-09-23 17:14:38650 ":$_apk_target_name",
651 ":$_library_target_name",
Andrew Grieve7ca6de32019-10-18 03:57:47652 ]
agrieve62ab00282016-04-05 02:03:45653 }
Dirk Pranke19a58732021-03-24 22:26:22654
655 if (defined(invoker.data_deps)) {
656 data_deps = invoker.data_deps
657 } else {
658 data_deps = []
659 }
Kevin Marshall23529362022-02-23 16:50:36660
661 data_deps += [ "//testing:test_scripts_shared" ]
662
Jamie Madilldd60ee62021-04-13 19:25:52663 if (tests_have_location_tags) {
664 data = [ "//testing/location_tags.json" ]
665 }
Tony Seaward37c51bc2024-10-28 18:22:57666 if (use_rts) {
667 data_deps += [ ":${invoker.target_name}__rts_filters" ]
668 }
agrieve1a02e582015-10-15 21:35:39669 }
Scott Graham4c4cdc52017-05-29 20:45:03670 } else if (is_fuchsia) {
Dirk Pranke79d065d2020-08-29 03:28:30671 assert(!defined(invoker.use_xvfb) || !invoker.use_xvfb)
672
Scott Graham4c4cdc52017-05-29 20:45:03673 _output_name = invoker.target_name
Kevin Marshallf35fa5f2018-01-29 19:24:42674 _pkg_target = "${_output_name}_pkg"
Kevin Marshallf35fa5f2018-01-29 19:24:42675 _exec_target = "${_output_name}__exec"
Greg Thompson9765eeb42022-04-05 12:30:35676 _program_name = get_label_info(":${_exec_target}", "name")
danakj505b7f062023-07-05 19:02:02677 _crate_name = _output_name
Scott Graham4c4cdc52017-05-29 20:45:03678
Greg Thompson2f1e3762022-10-17 19:53:44679 # Generate a CML fragment that provides the program name.
680 _test_program_fragment_target = "${target_name}_program-fragment"
681 _test_program_fragment = "${target_out_dir}/${target_name}_program.test-cml"
682 generated_file(_test_program_fragment_target) {
683 contents = {
684 program = {
685 binary = _program_name
Kevin Marshall2ec60032022-05-09 17:38:28686 }
Greg Thompson26516592021-12-16 08:34:45687 }
Greg Thompson2f1e3762022-10-17 19:53:44688 outputs = [ _test_program_fragment ]
689 output_conversion = "json"
Greg Thompson9765eeb42022-04-05 12:30:35690 }
691
Greg Thompson2f1e3762022-10-17 19:53:44692 _test_runner_shard =
693 "//build/config/fuchsia/test/elf_test_runner.shard.test-cml"
694 if (defined(invoker.test_runner_shard)) {
695 _test_runner_shard = invoker.test_runner_shard
Wez6879f8a2021-09-07 20:27:02696 }
697
Greg Thompson2f1e3762022-10-17 19:53:44698 # Collate the complete set of elements to include in the test component's
699 # manifest.
David Dorwin2c4872c2023-02-22 20:00:56700
Greg Thompson2f1e3762022-10-17 19:53:44701 _manifest_fragments = [
Greg Thompson2f1e3762022-10-17 19:53:44702 _test_program_fragment,
703 _test_runner_shard,
704 ]
705
John Wittrockf8b72822025-01-15 19:03:50706 _subpackages = []
707
David Dorwin2c4872c2023-02-22 20:00:56708 # Select the Fuchsia test realm in which to run the test.
709 if (defined(invoker.run_as_chromium_system_test) &&
710 invoker.run_as_chromium_system_test) {
711 _manifest_fragments += [
712 "//build/config/fuchsia/test/chromium_system_test_facet.shard.test-cml",
713 "//build/config/fuchsia/test/system_test_minimum.shard.test-cml",
714 ]
715 } else {
John Wittrockf8b72822025-01-15 19:03:50716 _subpackages += [
717 "//third_party/fuchsia-sdk/sdk/packages/fake-build-info:fake-build-info",
718 "//third_party/fuchsia-sdk/sdk/packages/intl_property_manager:intl_property_manager",
719 ]
David Dorwin2c4872c2023-02-22 20:00:56720 _manifest_fragments += [
721 "//build/config/fuchsia/test/chromium_test_facet.shard.test-cml",
722 "//build/config/fuchsia/test/minimum.shard.test-cml",
723 ]
724 }
725
Zijie He78d978e2023-07-19 21:46:42726 if (is_asan) {
Alison Gale59c007a2024-04-20 03:05:40727 # TODO(crbug.com/40276216): Remove the extra cml segment for asan.
Zijie He78d978e2023-07-19 21:46:42728 _manifest_fragments +=
729 [ "//build/config/fuchsia/test/asan_options.shard.test-cml" ]
730 }
731
Greg Thompson2f1e3762022-10-17 19:53:44732 _test_component_manifest = "${target_out_dir}/${target_name}.cml"
733 _merged_manifest_name = "${_output_name}.cml"
734
735 if (defined(invoker.additional_manifest_fragments)) {
736 _manifest_fragments += invoker.additional_manifest_fragments
737 }
738
739 # Generate the test component manifest from the specified elements.
740 _test_component_manifest_target = "${target_name}_component-manifest"
741 cmc_merge(_test_component_manifest_target) {
742 sources = _manifest_fragments
743 output_name = "${_merged_manifest_name}"
744 deps = [ ":${_test_program_fragment_target}" ]
745 }
746
747 # Define the test component, dependent on the generated manifest, and the
748 # test executable target.
749 _test_component_target = "${target_name}_component"
750 fuchsia_component(_test_component_target) {
751 deps = [ ":$_test_component_manifest_target" ]
752 data_deps = [ ":$_exec_target" ]
753 manifest = _test_component_manifest
754 visibility = [ ":*" ]
755 }
756
757 _test_component_targets = [ ":${_test_component_target}" ]
758
Wez6879f8a2021-09-07 20:27:02759 # Define components for each entry in |additional_manifests|, if any. Since
760 # manifests may themselves depend-on the outputs of |deps|, these components
761 # must each individually depend on |invoker.deps|.
Wez6879f8a2021-09-07 20:27:02762 if (defined(invoker.additional_manifests)) {
763 foreach(filename, invoker.additional_manifests) {
764 _additional_component_target =
765 target_name + "_" + get_path_info(filename, "name")
766 _test_component_targets += [ ":${_additional_component_target}" ]
767 fuchsia_component(_additional_component_target) {
768 forward_variables_from(invoker, [ "testonly" ])
769 data_deps = [ ":$_exec_target" ]
770 visibility = [ ":*" ]
771 manifest = filename
772
773 # Depend on |invoker.deps|, in case it includes a dependency that
774 # creates this additional component's manifest.
775 if (defined(invoker.deps)) {
776 deps = invoker.deps
777 }
778 }
779 }
780 }
781
782 # Define the package target that will bundle the test and additional
783 # components and their data.
784 fuchsia_package(_pkg_target) {
Kevin Marshall36c602c2021-11-04 16:16:21785 forward_variables_from(invoker,
786 [
787 "excluded_files",
788 "excluded_dirs",
Bryant Chandlerc40f2672023-01-27 23:33:30789 "excluded_paths",
Kevin Marshall36c602c2021-11-04 16:16:21790 ])
Wez6879f8a2021-09-07 20:27:02791 package_name = _output_name
792 deps = _test_component_targets
Kevin Marshall36c602c2021-11-04 16:16:21793
Greg Thompsonfd269652022-10-28 12:06:55794 if (defined(invoker.fuchsia_package_deps)) {
795 deps += invoker.fuchsia_package_deps
796 }
Bryant Chandlerc40f2672023-01-27 23:33:30797 if (!defined(excluded_paths)) {
798 excluded_paths = []
Kevin Marshall36c602c2021-11-04 16:16:21799 }
Bryant Chandlerc40f2672023-01-27 23:33:30800 excluded_paths += [
801 "${devtools_root_location}/*",
802 "*.git/*",
803 "*.svn/*",
804 "*.hg/*",
805 ]
Sarah Pham80972efc2022-05-31 17:40:15806 if (devtools_root_location != "") {
Bryant Chandlerc40f2672023-01-27 23:33:30807 excluded_paths += [ "${devtools_root_location}/*" ]
Sarah Pham80972efc2022-05-31 17:40:15808 }
John Wittrockf8b72822025-01-15 19:03:50809
810 subpackages = _subpackages
Wez6879f8a2021-09-07 20:27:02811 }
812
813 # |target_name| refers to the package-runner rule, so that building
814 # "base_unittests" will build not only the executable, component, and
815 # package, but also the script required to run them.
Kevin Marshall5fadadd2021-10-16 00:08:25816 fuchsia_test_runner(target_name) {
Fabrice de Gans-Riberi041a6522018-12-11 00:20:53817 forward_variables_from(invoker,
818 [
Kevin Marshall5fadadd2021-10-16 00:08:25819 "data",
820 "data_deps",
Fabrice de Gans-Riberi041a6522018-12-11 00:20:53821 "package_deps",
Kevin Marshall5fadadd2021-10-16 00:08:25822 "use_test_server",
Fabrice de Gans-Riberi041a6522018-12-11 00:20:53823 ])
Kevin Marshall5fadadd2021-10-16 00:08:25824
Chong Guc6bfdf62021-10-20 23:37:00825 is_test_exe = true
Kevin Marshall39b4aa82018-06-23 00:12:15826 package = ":$_pkg_target"
Kevin Marshall5fadadd2021-10-16 00:08:25827 package_name = _output_name
Dimitri Glazkovc95e6dd2018-08-24 23:39:42828
Kevin Marshall5fadadd2021-10-16 00:08:25829 if (!defined(deps)) {
830 deps = []
Jamie Madilldd60ee62021-04-13 19:25:52831 }
Kevin Marshall5fadadd2021-10-16 00:08:25832 if (defined(invoker.deps)) {
833 deps += invoker.deps
834 }
Greg Guterman6963dc02021-04-07 05:20:59835
Kevin Marshall5fadadd2021-10-16 00:08:25836 if (!defined(data)) {
837 data = []
838 }
839 if (tests_have_location_tags) {
840 data += [ "//testing/location_tags.json" ]
841 }
842
843 if (!defined(data_deps)) {
844 data_deps = []
845 }
Benjamin Lerman5e3cb3362022-01-25 16:46:28846
Kevin Marshall23529362022-02-23 16:50:36847 data_deps += [ "//testing:test_scripts_shared" ]
Kevin Marshallf35fa5f2018-01-29 19:24:42848 }
849
danakjebb9cc4d2022-03-04 21:30:11850 mixed_test(_exec_target) {
851 target_type = "executable"
Andrew Grieve1b290e4a22020-11-24 20:07:01852 forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
Kevin Marshallf35fa5f2018-01-29 19:24:42853 output_name = _exec_target
danakj505b7f062023-07-05 19:02:02854
Adrian Taylor62dbea52023-10-25 20:29:16855 if (!defined(deps)) {
856 deps = []
857 }
Adrian Taylor62dbea52023-10-25 20:29:16858
danakj505b7f062023-07-05 19:02:02859 # Use a crate name that avoids creating a warning due to double
860 # underscore (ie. `__`).
861 crate_name = _crate_name
Scott Graham4c4cdc52017-05-29 20:45:03862 }
dpranke2a294622015-08-07 05:23:01863 } else if (is_ios) {
Dirk Pranke79d065d2020-08-29 03:28:30864 assert(!defined(invoker.use_xvfb) || !invoker.use_xvfb)
Mirko Bonadei50e251d2020-09-14 18:05:46865 _runtime_deps_file = "$root_out_dir/${target_name}.runtime_deps"
Dirk Pranke79d065d2020-08-29 03:28:30866
Rohit Raof9b096d2019-09-09 22:26:23867 declare_args() {
Zhaoyang Li34fb05e2023-07-25 18:02:00868 # Keep the unittest-as-xctest functionality defaulted to off for
869 # local builds and test executions.
Rohit Raof9b096d2019-09-09 22:26:23870 enable_run_ios_unittests_with_xctest = false
871 }
872
sdefresne012857872016-03-16 10:55:37873 _test_target = target_name
Sylvain Defresne3f48aedc2021-10-07 10:17:27874
Jeff Yoonf7f4eb42020-03-06 18:55:36875 _wrapper_output_name = "run_${target_name}"
876 ios_test_runner_wrapper(_wrapper_output_name) {
877 forward_variables_from(invoker,
878 [
Cameron Higgins69d21be2023-12-04 17:57:21879 "clones",
Jeff Yoonf7f4eb42020-03-06 18:55:36880 "data",
Jeff Yoonf7f4eb42020-03-06 18:55:36881 "deps",
882 "executable_args",
883 "retries",
Jeff Yoonf7f4eb42020-03-06 18:55:36884 ])
885
886 _root_build_dir = rebase_path("${root_build_dir}", root_build_dir)
887
888 if (!defined(executable_args)) {
889 executable_args = []
890 }
891 executable_args += [
892 "--app",
893 "@WrappedPath(${_root_build_dir}/${_test_target}.app)",
894 ]
895
896 wrapper_output_name = "${_wrapper_output_name}"
Dirk Pranke19a58732021-03-24 22:26:22897
898 if (!defined(data)) {
899 data = []
900 }
Jamie Madilldd60ee62021-04-13 19:25:52901 if (tests_have_location_tags) {
902 data += [ "//testing/location_tags.json" ]
903 }
Jeff Yoonf7f4eb42020-03-06 18:55:36904 }
905
sdefresne012857872016-03-16 10:55:37906 _resources_bundle_data = target_name + "_resources_bundle_data"
907
908 bundle_data(_resources_bundle_data) {
sdefresne047490e2016-07-22 08:49:34909 visibility = [ ":$_test_target" ]
Nico Weber852532f2020-01-28 18:17:22910 sources = [ "//testing/gtest_ios/Default.png" ]
911 outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ]
dpranke2a294622015-08-07 05:23:01912 }
913
Mirko Bonadei15522bc2020-09-16 20:38:39914 force_xctest = enable_run_ios_unittests_with_xctest ||
915 (defined(invoker.is_xctest) && invoker.is_xctest)
916
danakjfae603fc602022-11-18 18:40:22917 mixed_test(_test_target) {
918 if (force_xctest) {
919 target_type = "ios_xctest_test"
920 } else {
921 target_type = "ios_app_bundle"
922 }
dpranke2a294622015-08-07 05:23:01923 testonly = true
sdefresnea828c282016-05-30 18:04:20924
Mirko Bonadei15522bc2020-09-16 20:38:39925 if (force_xctest && build_with_chromium) {
Rohit Raof9b096d2019-09-09 22:26:23926 xctest_module_target = "//base/test:google_test_runner"
927 }
928
Andrew Grieve1b290e4a22020-11-24 20:07:01929 forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
sdefresne9e147e02016-06-07 00:10:13930
931 # Provide sensible defaults in case invoker did not define any of those
932 # required variables.
sdefresne05b97ca2016-06-08 07:19:46933 if (!defined(info_plist) && !defined(info_plist_target)) {
sdefresne9e147e02016-06-07 00:10:13934 info_plist = "//testing/gtest_ios/unittest-Info.plist"
935 }
sdefresne9e147e02016-06-07 00:10:13936
Sylvain Defresne8c0fc9a2023-10-16 17:26:13937 bundle_identifier = shared_bundle_id_for_test_apps
dpranke2a294622015-08-07 05:23:01938
sdefresne047490e2016-07-22 08:49:34939 if (!defined(bundle_deps)) {
940 bundle_deps = []
941 }
942 bundle_deps += [ ":$_resources_bundle_data" ]
Jeff Yoonf7f4eb42020-03-06 18:55:36943
944 if (!defined(data_deps)) {
945 data_deps = []
946 }
947
Kevin Marshall23529362022-02-23 16:50:36948 data_deps += [ "//testing:test_scripts_shared" ]
949
Jeff Yoonf7f4eb42020-03-06 18:55:36950 # Include the generate_wrapper as part of data_deps
951 data_deps += [ ":${_wrapper_output_name}" ]
Mirko Bonadei50e251d2020-09-14 18:05:46952 write_runtime_deps = _runtime_deps_file
Tony Seaward37c51bc2024-10-28 18:22:57953 if (use_rts) {
954 data_deps += [ ":${invoker.target_name}__rts_filters" ]
955 }
Adrian Taylor62dbea52023-10-25 20:29:16956 if (!defined(deps)) {
957 deps = []
958 }
dpranke2a294622015-08-07 05:23:01959 }
Georg Neis11a59652024-10-09 06:20:04960 } else if (is_chromeos && cros_board != "") {
Dirk Pranke79d065d2020-08-29 03:28:30961 assert(!defined(invoker.use_xvfb) || !invoker.use_xvfb)
962
Georg Neis11a59652024-10-09 06:20:04963 # Building for a cros board (ie: not linux-chromeos).
Benjamin Pastene3bce864e2018-04-14 01:16:32964
Benjamin Pastene3bce864e2018-04-14 01:16:32965 _gen_runner_target = "${target_name}__runner"
966 _runtime_deps_file =
967 "$root_out_dir/gen.runtime/" + get_label_info(target_name, "dir") +
968 "/" + get_label_info(target_name, "name") + ".runtime_deps"
969
Xinan Linf3f5aa52023-08-24 22:07:28970 if (is_skylab && (defined(tast_attr_expr) || defined(tast_tests) ||
971 defined(tast_disabled_tests))) {
972 generate_skylab_tast_filter(_gen_runner_target) {
Yuke Liaoacb74b12021-04-23 23:37:34973 }
Xinan Lin6be01252021-06-25 23:07:36974 } else {
975 generate_runner_script(_gen_runner_target) {
Xinan Linf3f5aa52023-08-24 22:07:28976 generated_script = "$root_build_dir/bin/run_" + invoker.target_name
Xinan Lin6be01252021-06-25 23:07:36977 test_exe = invoker.target_name
978 runtime_deps_file = _runtime_deps_file
Yuke Liaoacb74b12021-04-23 23:37:34979
Xinan Lin6be01252021-06-25 23:07:36980 if (tests_have_location_tags) {
981 data = [ "//testing/location_tags.json" ]
982 }
Tony Seaward37c51bc2024-10-28 18:22:57983 if (use_rts) {
984 data_deps = [ ":${invoker.target_name}__rts_filters" ]
985 }
Greg Guterman6963dc02021-04-07 05:20:59986 }
Benjamin Pastene3bce864e2018-04-14 01:16:32987 }
988
danakjebb9cc4d2022-03-04 21:30:11989 mixed_test(target_name) {
990 target_type = "executable"
Andrew Grieve1b290e4a22020-11-24 20:07:01991 forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
992 forward_variables_from(invoker, [ "visibility" ])
Benjamin Pastene3bce864e2018-04-14 01:16:32993 if (!defined(deps)) {
994 deps = []
995 }
996 if (!defined(data)) {
997 data = []
998 }
999
Ben Pastene41041782019-02-16 04:21:581000 # We use a special trigger script for CrOS hardware tests.
1001 data += [ "//testing/trigger_scripts/chromeos_device_trigger.py" ]
1002
Benjamin Pastene3bce864e2018-04-14 01:16:321003 write_runtime_deps = _runtime_deps_file
1004 data += [ _runtime_deps_file ]
Tom Andersonce772fa2018-05-30 22:20:371005 deps += [ ":$_gen_runner_target" ]
Greg Guterman6963dc02021-04-07 05:20:591006
Kevin Marshall23529362022-02-23 16:50:361007 if (!defined(data_deps)) {
1008 data_deps = []
1009 }
1010
1011 data_deps += [ "//testing:test_scripts_shared" ]
Tony Seaward37c51bc2024-10-28 18:22:571012 if (use_rts) {
1013 data_deps += [ ":${invoker.target_name}__rts_filters" ]
1014 }
Benjamin Pastene3bce864e2018-04-14 01:16:321015 }
Dirk Prankedd4ff742020-11-18 19:57:321016 } else if (!is_nacl) {
Dirk Pranke79d065d2020-08-29 03:28:301017 if (is_mac || is_win) {
1018 assert(!defined(invoker.use_xvfb) || !invoker.use_xvfb)
1019 }
1020
Dirk Prankedd4ff742020-11-18 19:57:321021 _runtime_deps_file = "$root_out_dir/${target_name}.runtime_deps"
1022 _executable = target_name
1023 _gen_runner_target = "${target_name}__runner"
Dirk Pranke31e346e2020-07-15 00:54:061024
Dirk Prankedd4ff742020-11-18 19:57:321025 if ((is_linux || is_chromeos) && defined(invoker.use_xvfb)) {
1026 _use_xvfb = invoker.use_xvfb
1027 } else {
1028 _use_xvfb = false
1029 }
1030
1031 generate_wrapper(_gen_runner_target) {
Dirk Prankedd4ff742020-11-18 19:57:321032 wrapper_script = "$root_build_dir/bin/run_" + _executable
1033
1034 data = []
Will Harrisd35e2c92021-04-07 01:42:021035 data_deps = [ "//testing:test_scripts_shared" ]
1036
Dirk Prankedd4ff742020-11-18 19:57:321037 if (_use_xvfb) {
1038 executable = "//testing/xvfb.py"
Dirk Pranke31e346e2020-07-15 00:54:061039 } else {
Dirk Prankedd4ff742020-11-18 19:57:321040 executable = "//testing/test_env.py"
Dirk Pranke31e346e2020-07-15 00:54:061041 }
Jamie Madilldd60ee62021-04-13 19:25:521042 if (tests_have_location_tags) {
1043 data += [ "//testing/location_tags.json" ]
1044 }
Dirk Pranke31e346e2020-07-15 00:54:061045
Dirk Prankedd4ff742020-11-18 19:57:321046 executable_args = [
1047 "@WrappedPath(./${_executable})",
1048 "--test-launcher-bot-mode",
1049 ]
1050 if (is_asan) {
Sven Zhenga006f4f2022-10-26 18:38:001051 executable_args += [ "--asan=1" ]
Dirk Pranke31e346e2020-07-15 00:54:061052 }
Dirk Prankedd4ff742020-11-18 19:57:321053 if (is_msan) {
Sven Zhenga006f4f2022-10-26 18:38:001054 executable_args += [ "--msan=1" ]
Dirk Prankedd4ff742020-11-18 19:57:321055 }
1056 if (is_tsan) {
Sven Zhenga006f4f2022-10-26 18:38:001057 executable_args += [ "--tsan=1" ]
Dirk Prankedd4ff742020-11-18 19:57:321058 }
1059 if (use_cfi_diag) {
Sven Zhenga006f4f2022-10-26 18:38:001060 executable_args += [ "--cfi-diag=1" ]
Dirk Prankedd4ff742020-11-18 19:57:321061 }
Ian Struiksma2ebc3222023-05-24 00:28:461062 if (fail_on_san_warnings) {
1063 executable_args += [ "--fail-san=1" ]
1064 }
Tony Seaward37c51bc2024-10-28 18:22:571065 if (use_rts) {
1066 data_deps += [ ":${invoker.target_name}__rts_filters" ]
1067 }
Dirk Pranke31e346e2020-07-15 00:54:061068 }
1069
danakjebb9cc4d2022-03-04 21:30:111070 mixed_test(target_name) {
1071 target_type = "executable"
Andrew Grieve1b290e4a22020-11-24 20:07:011072 forward_variables_from(invoker,
1073 "*",
1074 TESTONLY_AND_VISIBILITY + [ "use_xvfb" ])
1075 forward_variables_from(invoker, [ "visibility" ])
Dirk Pranke31e346e2020-07-15 00:54:061076 if (!defined(deps)) {
1077 deps = []
1078 }
1079
Dirk Pranke31e346e2020-07-15 00:54:061080 deps += [
1081 # Give tests the default manifest on Windows (a no-op elsewhere).
1082 "//build/win:default_exe_manifest",
1083 ]
1084
Dirk Prankedd4ff742020-11-18 19:57:321085 write_runtime_deps = _runtime_deps_file
1086 deps += [ ":$_gen_runner_target" ]
Greg Guterman50ed4b42021-04-08 01:15:111087
Kevin Marshall23529362022-02-23 16:50:361088 if (!defined(data_deps)) {
1089 data_deps = []
1090 }
1091
1092 data_deps += [ "//testing:test_scripts_shared" ]
Tony Seaward37c51bc2024-10-28 18:22:571093 if (use_rts) {
1094 data_deps += [ ":${invoker.target_name}__rts_filters" ]
1095 }
Dirk Prankedd4ff742020-11-18 19:57:321096 }
1097 } else {
1098 # This is a catch-all clause for NaCl toolchains and other random
1099 # configurations that might define tests; test() in these configs
1100 # will just define the underlying executables.
1101 assert(!defined(invoker.use_xvfb) || !invoker.use_xvfb,
1102 "use_xvfb should not be defined for a non-linux configuration")
danakjebb9cc4d2022-03-04 21:30:111103 mixed_test(target_name) {
1104 target_type = "executable"
Andrew Grieve1b290e4a22020-11-24 20:07:011105 forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
1106 forward_variables_from(invoker, [ "visibility" ])
Dirk Prankedd4ff742020-11-18 19:57:321107 if (!defined(deps)) {
1108 deps = []
Dirk Pranke31e346e2020-07-15 00:54:061109 }
Greg Guterman6963dc02021-04-07 05:20:591110
Kevin Marshall23529362022-02-23 16:50:361111 if (!defined(data_deps)) {
1112 data_deps = []
1113 }
1114
1115 data_deps += [ "//testing:test_scripts_shared" ]
Tony Seaward37c51bc2024-10-28 18:22:571116 if (use_rts) {
1117 data_deps += [ ":${invoker.target_name}__rts_filters" ]
1118 }
Dirk Pranke31e346e2020-07-15 00:54:061119 }
qsrfb5251d12015-01-21 15:57:221120 }
1121}
brettwedb6ecc2016-07-14 23:37:031122
Dirk Pranke6188075b2020-10-01 19:31:281123# Defines a type of test that invokes a script to run, rather than
1124# invoking an executable.
1125#
1126# The script must implement the
1127# [test executable API](//docs/testing/test_executable_api.md).
1128#
Andrew Grievea16222d42023-02-10 15:31:101129# The template must be passed the `script` parameter, which specifies the path
1130# to the script to run. It may optionally be passed a `args` parameter, which
1131# can be used to include a list of args to be specified by default. The
1132# template will produce a `$root_build_dir/run_$target_name` wrapper and write
1133# the runtime_deps for the target to
1134# $root_build_dir/${target_name}.runtime_deps, as per the conventions listed in
1135# the [test wrapper API](//docs/testing/test_wrapper_api.md).
Ben Pastene1e985542024-09-25 00:41:421136#
1137# Variables:
1138# script: Path to underlying test script.
1139# args: Args to the test script.
1140# enable_android_wrapper: (optional) If true, the test will get wrapped in the
1141# logdog_wrapper on Android, which will stream logcats from the device.
1142# enable_cros_wrapper: (optional) If true, the test will get wrapped by the
1143# cros_test_wrapper on ChromeOS, which will prep the device for testing.
Dirk Pranke6188075b2020-10-01 19:31:281144template("script_test") {
Ben Pastene1e985542024-09-25 00:41:421145 _enable_logdog_wrapper = defined(invoker.enable_android_wrapper) &&
1146 invoker.enable_android_wrapper && is_android
1147 _enable_cros_wrapper =
1148 defined(invoker.enable_cros_wrapper) && invoker.enable_cros_wrapper &&
Georg Neis11a59652024-10-09 06:20:041149 is_chromeos && is_chromeos_device
Ben Pastene1e985542024-09-25 00:41:421150 assert(!(_enable_logdog_wrapper && _enable_cros_wrapper),
1151 "The logdog and cros_test wrappers are mutually exclusive")
Dirk Pranke6188075b2020-10-01 19:31:281152
Tony Seaward37c51bc2024-10-28 18:22:571153 if (use_rts) {
1154 action("${target_name}__rts_filters") {
1155 script = "//build/add_rts_filters.py"
1156 rts_file = "${root_build_dir}/gen/rts/${invoker.target_name}.filter"
1157 args = [ rebase_path(rts_file, root_build_dir) ]
1158 outputs = [ rts_file ]
1159 }
1160 }
1161
Ben Pastene1e985542024-09-25 00:41:421162 _shared_data = [ invoker.script ]
1163 if (defined(invoker.data)) {
1164 _shared_data += invoker.data
1165 }
1166 if (tests_have_location_tags) {
1167 _shared_data += [ "//testing/location_tags.json" ]
1168 }
Dirk Pranke6188075b2020-10-01 19:31:281169
Ben Pastene1e985542024-09-25 00:41:421170 _shared_data_deps = [ "//testing:test_scripts_shared" ]
1171 if (defined(invoker.data_deps)) {
1172 _shared_data_deps += invoker.data_deps
1173 }
Struan Shrimpton6084bfe2024-12-11 21:35:331174 if (use_rts) {
1175 _shared_data_deps += [ ":${invoker.target_name}__rts_filters" ]
1176 }
Ben Pastene1e985542024-09-25 00:41:421177
1178 _wrapped_script =
1179 "@WrappedPath(" + rebase_path(invoker.script, root_build_dir) + ")"
1180 if (_enable_logdog_wrapper) {
1181 logdog_wrapper_script_test(target_name) {
1182 testonly = true
1183 args = [ _wrapped_script ]
1184 if (defined(invoker.args)) {
1185 args += invoker.args
1186 }
1187 data = _shared_data
1188 data_deps = _shared_data_deps
1189
1190 forward_variables_from(invoker,
1191 "*",
1192 TESTONLY_AND_VISIBILITY + [
1193 "args",
1194 "data",
1195 "data_deps",
1196 "script",
1197 ])
Dirk Pranke6188075b2020-10-01 19:31:281198 }
Ben Pastene1e985542024-09-25 00:41:421199 } else if (_enable_cros_wrapper) {
1200 cros_test_wrapper_script_test(target_name) {
1201 args = [ _wrapped_script ]
1202 if (defined(invoker.args)) {
1203 args += invoker.args
1204 }
1205 data = _shared_data
1206 data_deps = _shared_data_deps
Dirk Pranke6188075b2020-10-01 19:31:281207
Ben Pastene1e985542024-09-25 00:41:421208 forward_variables_from(invoker,
1209 "*",
1210 TESTONLY_AND_VISIBILITY + [
1211 "args",
1212 "data",
1213 "data_deps",
1214 "script",
1215 ])
Dirk Pranke6188075b2020-10-01 19:31:281216 }
Ben Pastene1e985542024-09-25 00:41:421217 } else {
1218 generate_wrapper(target_name) {
1219 testonly = true
1220 wrapper_script = "${root_build_dir}/bin/run_${target_name}"
1221
1222 executable = "//testing/test_env.py"
1223
1224 executable_args = [ _wrapped_script ]
1225 if (defined(invoker.args)) {
1226 executable_args += invoker.args
1227 }
1228
1229 data = _shared_data
1230 data_deps = _shared_data_deps
1231
1232 forward_variables_from(invoker,
1233 "*",
1234 TESTONLY_AND_VISIBILITY + [
1235 "args",
1236 "data",
1237 "data_deps",
1238 "script",
1239 ])
1240 forward_variables_from(invoker, [ "visibility" ])
1241
1242 write_runtime_deps = "${root_build_dir}/${target_name}.runtime_deps"
Jamie Madilldd60ee62021-04-13 19:25:521243 }
Dirk Pranke6188075b2020-10-01 19:31:281244 }
1245}
1246
Andrew Grievea16222d42023-02-10 15:31:101247# Defines a test target that uses exit code for pass/fail.
1248template("isolated_script_test") {
1249 script_test(target_name) {
1250 forward_variables_from(invoker,
1251 "*",
1252 TESTONLY_AND_VISIBILITY + [
1253 "args",
Peter Wena9f19582024-10-08 18:16:161254 "data",
Andrew Grievea16222d42023-02-10 15:31:101255 "deps",
1256 "script",
1257 ])
1258 forward_variables_from(invoker, [ "visibility" ])
1259 deps = [ "//testing:run_isolated_script_test" ]
1260 if (defined(invoker.deps)) {
1261 deps += invoker.deps
1262 }
1263 script = "//testing/scripts/run_isolated_script_test.py"
1264 data = [ invoker.script ]
Peter Wena9f19582024-10-08 18:16:161265 if (defined(invoker.data)) {
1266 data += invoker.data
1267 }
Andrew Grievea16222d42023-02-10 15:31:101268 args = [
1269 rebase_path(invoker.script, root_build_dir),
1270 "--script-type=bare",
1271 ]
1272 if (defined(invoker.args)) {
1273 args += invoker.args
1274 }
1275 }
1276}
1277
brettwedb6ecc2016-07-14 23:37:031278# Test defaults.
1279set_defaults("test") {
1280 if (is_android) {
Mohamed Heikaldd52b452024-09-10 17:10:501281 # Should be kept in sync with set_defaults("cronet_test") in
1282 # //components/cronet/android/cronet_test_templates.gni
1283 # LINT.IfChange
brettwedb6ecc2016-07-14 23:37:031284 configs = default_shared_library_configs
Yipeng Wang158dbc5c2017-06-30 18:16:411285 configs -= [ "//build/config/android:hide_all_but_jni_onload" ]
Thomas Anderson11c1d9822019-01-15 06:32:591286 configs += [ "//build/config/android:hide_all_but_jni" ]
Mohamed Heikaldd52b452024-09-10 17:10:501287
1288 # LINT.ThenChange(/components/cronet/android/cronet_test_templates.gni)
brettwedb6ecc2016-07-14 23:37:031289 } else {
1290 configs = default_executable_configs
1291 }
1292}