| # Copyright 2018 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| import("//build/testing/test_spec.gni") |
| |
| # Defines a Rust unit test, without also compiling a separate binary or library |
| # target. |
| # |
| # Parameters |
| # |
| # name |
| # Name of the test binary, also used as the crate name as defined in its |
| # manifest file. If not specified, it is assumed to be the same as the |
| # target name. |
| # |
| # version |
| # Semver version of the crate as seen on crates.io. |
| # |
| # edition (optional) |
| # Edition of the Rust language to be used. |
| # Options are "2015" and "2018". Defaults to "2018". |
| # |
| # configs (optional) |
| # A list of config labels applying to this target. |
| # |
| # deps (optional) |
| # List of rust_library GN targets on which this crate depends. |
| # Third party crates can be included through paths like |
| # "//third_party/rust_crates:<cratename>", |
| # |
| # non_rust_deps (optional) |
| # List of non-rust_library GN targets on which this crate depends. |
| # |
| # test_environments (optional) |
| # What environments the unit test should target. Only used here |
| # for linux and mac tests, with a default value of a general linux/mac |
| # environment (as a function of $current_os). |
| # See environments parameter on //build/testing/test_spec.gni for more |
| # details. |
| # |
| # source_root (optional) |
| # Location of the crate root (e.g. `src/main.rs` or `src/lib.rs`). |
| # This should usually be 'src/lib.rs/ for libraries (the default) |
| # and `src/main.rs` for binaries. |
| # |
| # features (optional) |
| # A list of conditional compilation flags to enable. This can be used to set features for crates |
| # built in-tree which are also published to crates.io. This would be passed to rustc as |
| # '--cfg feature=XXX' |
| # |
| # Example of usage: |
| # |
| # rustc_test("foo_test") { |
| # source_root = "src/foo_test.rs", |
| # deps = [ |
| # "//garnet/public/rust/bar", |
| # "//third_party/rust_crates:clap", |
| # "//third_party/rust_crates:serde", |
| # "//third_party/rust_crates:slab", |
| # ] |
| # } |
| template("rustc_test") { |
| not_needed(invoker, |
| [ |
| "version", |
| "edition", |
| "force_opt", |
| "testonly", |
| ]) |
| |
| # default location for rust libraries |
| source_root = "src/lib.rs" |
| if (defined(invoker.source_root)) { |
| source_root = invoker.source_root |
| } |
| |
| # use the target name unless another name is specified |
| package_name = target_name |
| if (defined(invoker.name)) { |
| package_name = invoker.name |
| } else { |
| package_name = "${target_name}" |
| } |
| |
| # built-in gn rules do not support dashes |
| crate_name = string_replace(package_name, "-", "_") |
| |
| if (!is_fuchsia) { |
| test_output_file = "${root_out_dir}/${crate_name}" |
| test_spec_target_name = "${target_name}_spec" |
| test_spec(test_spec_target_name) { |
| name = invoker.target_name |
| target = invoker.target_name |
| path = test_output_file |
| deps = [] |
| if (defined(invoker.deps)) { |
| deps += invoker.deps |
| } |
| if (defined(invoker.non_rust_deps)) { |
| deps += invoker.non_rust_deps |
| } |
| if (defined(invoker.test_environments)) { |
| environments = invoker.test_environments |
| } |
| } |
| } else { |
| # if not a host-side target, we don't need this since it doesn't |
| # propogate to package.gni |
| not_needed(invoker, [ "test_environments" ]) |
| } |
| |
| # enable these features for the target |
| features = [] |
| if (defined(invoker.features)) { |
| foreach(i, invoker.features) { |
| features += [ "--cfg=feature=\"${i}\"" ] |
| } |
| } |
| |
| executable(target_name) { |
| # anything defined with rustc_test MUST be a test target |
| testonly = true |
| |
| configs = [] |
| configs = invoker.configs |
| |
| crate_root = source_root |
| output_name = crate_name |
| crate_name = crate_name |
| |
| # tell rustc to compile in the test harness |
| rustflags = features + [ "--test" ] |
| |
| deps = [] |
| if (defined(invoker.deps)) { |
| deps = invoker.deps |
| } |
| if (defined(invoker.non_rust_deps)) { |
| deps += invoker.non_rust_deps |
| } |
| |
| # test_deps specified in the parent template (if not used directly) |
| # should also be treated as normal dependencies |
| if (defined(invoker.test_deps)) { |
| deps += invoker.test_deps |
| } |
| |
| if (is_linux || is_mac) { |
| # if this test is being built on host, depend on the test_spec |
| deps += [ ":$test_spec_target_name" ] |
| } |
| |
| if (is_fuchsia) { |
| deps += [ "//build/test:rust_test_metadata" ] |
| metadata = { |
| test_barrier = [ "//build/test:rust_test_metadata" ] |
| } |
| } |
| |
| if (defined(invoker.edition) && invoker.edition == "2015") { |
| configs -= [ "//build/config:rust_edition_2018" ] |
| configs += [ "//build/config:rust_edition_2015" ] |
| } |
| |
| forward_variables_from(invoker, [ "visibility" ]) |
| } |
| } |