1 # Copyright 2021 The Chromium Authors
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import("//build/config/rust.gni")
6 import("//build/rust/rust_unit_tests_group.gni")
8 # Defines a Rust unit test.
10 # This generates an executable + a script that can be run on Chromium bots.
11 # Future iterations of this template may do something smarter with the test
12 # code in order to automatically contribute it to test steps on the bots.
18 # allow_unsafe (optional)
21 # crate_root (optional)
23 # rustflags (optional)
25 # All as in rust_static_library.
29 # rust_unit_test("foo_tests") {
31 # "//third_party/rust/test_utils/v1:lib",
33 # sources = [ "src/lib.rs" ]
36 # Implementation note: you might assume it makes sense to implement this
37 # in terms of rust_target in order to avoid the duplication of logic around
38 # features and editions. We don't do that because rust_target actually
39 # depends on this template in order to build embedded unit tests
40 # (and therefore depending on rust_target here would lead to an infinite
43 template("rust_unit_test") {
44 assert(can_build_rust_unit_tests)
45 if (defined(invoker.crate_name)) {
46 _crate_name = invoker.crate_name
48 _crate_name = target_name
50 if (defined(invoker.crate_root)) {
51 _crate_root = invoker.crate_root
53 _crate_root = "src/lib.rs"
55 _rustflags = invoker.rustflags
56 if (defined(invoker.features)) {
57 foreach(i, invoker.features) {
58 _rustflags += [ "--cfg=feature=\"${i}\"" ]
61 _configs = invoker.configs
63 if (defined(invoker.edition)) {
64 _edition = invoker.edition
66 _configs += [ "//build/rust:edition_${_edition}" ]
68 # We require that all source files are listed, even though this is
69 # not a requirement for rustc. The reason is to ensure that tools
70 # such as `gn deps` give the correct answer, and thus we trigger
71 # the right test suites etc. on code change.
72 # TODO(crbug.com/1256930) - verify this is correct
73 assert(defined(invoker.sources), "sources must be listed")
75 _exe_target_name = target_name + "_exe"
76 rust_unit_tests_group(target_name) {
77 deps = [ ":$_exe_target_name" ]
80 # The OUT_DIR for a crate's tests should point to the same OUT_DIR that the
81 # library it's testing used. The `env_out_dir` variable can be used to specify
83 if (defined(invoker.env_out_dir)) {
84 _env_out_dir = invoker.env_out_dir
86 _env_out_dir = target_gen_dir
89 # TODO(crbug.com/1229320): Arrange to run test executables on try bots.
90 # TODO(crbug.com/gn/146): Allow Rust executables to depend on C/C++ source
92 # This is important in cases where Rust tests may depend upon C/C++
94 executable(_exe_target_name) {
96 forward_variables_from(invoker,
108 if (!defined(output_name) || output_name == "") {
109 output_name = _crate_name
117 rustflags += _rustflags
120 crate_name = _crate_name
121 crate_root = _crate_root
122 if (!defined(rustenv)) {
126 rustenv += [ "OUT_DIR=" +
127 rebase_path(_env_out_dir, get_path_info(_crate_root, "dir")) ]
129 # Consumed by "rust_unit_tests_group" gni template.
130 rust_unit_test_executables = [ _crate_name ]
135 set_defaults("rust_unit_test") {
136 configs = default_executable_configs