| # 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/rust/rustc_artifact.gni") | |
| import("//build/testing/host_test.gni") | |
| import("//build/toolchain/concurrent_jobs.gni") | |
| import("//build/toolchain/rbe.gni") | |
| # Internal template supporting rustc_test. | |
| # | |
| # This templates support rustc_test and the equivalent targets generated by | |
| # other rustc_*** rules when `with_unit_tests=true`. A different rule name than | |
| # `rustc_test` is necessary so that cascaded rules can inherit `configs` while | |
| # we can maintain sensible defaults (through `set_defaults`) for `rustc_test` | |
| # callers. See `BUILDCONFIG.gn` for example `set_defaults` usage with rustc_*** | |
| # rules. | |
| # | |
| # If in doubt, use rustc_test instead. | |
| template("rustc_test_internal") { | |
| not_needed(invoker, | |
| [ | |
| "version", | |
| "testonly", | |
| ]) | |
| # default location for rust libraries | |
| source_root = "src/lib.rs" | |
| if (defined(invoker.source_root)) { | |
| source_root = invoker.source_root | |
| } | |
| if (defined(invoker.original_target_name)) { | |
| _original_target_name = invoker.original_target_name | |
| } else { | |
| _original_target_name = target_name | |
| } | |
| # use the target name unless another name is specified | |
| assert(!(defined(invoker.output_name) && defined(invoker.name)), | |
| "Only one of output_name and name may be specified.") | |
| _package_name = target_name | |
| if (defined(invoker.output_name)) { | |
| _package_name = invoker.output_name | |
| } else if (defined(invoker.name)) { | |
| _package_name = invoker.name | |
| } | |
| # built-in gn rules do not support dashes | |
| if (defined(invoker.crate_name)) { | |
| _crate_name = invoker.crate_name | |
| } else { | |
| _crate_name = _package_name | |
| } | |
| # rustc does not support dashes in crate names | |
| _crate_name = string_replace(_crate_name, "-", "_") | |
| exe_target_name = target_name | |
| if (is_host) { | |
| exe_target_name = "${target_name}_executable" | |
| if (defined(invoker.args)) { | |
| args = invoker.args | |
| } | |
| # Disable using the unstripped binary if this is a builder or toolchain that | |
| # may not actually produce unstripped executables in the known location. | |
| # Note: `select_variant_canonical == []` is needed because whenever any | |
| # variant is active, `BUILDCONFIG.gn` appends `{ variant = "novariant" }` to | |
| # `select_variant_canonical`, causing `variant_target("executable")` in | |
| # `host_x64` to build in `host_x64-novariant` and only copy the stripped | |
| # binary back to `host_x64`. | |
| use_unstripped_binary_for_host_tests = | |
| !defined(invoker.output_dir) && select_variant_canonical == [] && | |
| current_toolchain == toolchain_variant.base && zircon_toolchain == false | |
| # This target copies unstripped binaries from their output directory to the | |
| # root out directory for the host toolchain. It is only used if we decide to | |
| # run the unstripped version below (for better backtraces). | |
| # | |
| # This is needed because many tests assume they are run from a directory | |
| # adjacent to their installed host_test_data files. Running from the nested | |
| # directory would break this assumption, so we run from the expected | |
| # directory using a copy of the test binary. | |
| if (use_unstripped_binary_for_host_tests) { | |
| unstripped_copy_target_name = "${target_name}_copy_unstripped" | |
| copy(unstripped_copy_target_name) { | |
| testonly = true | |
| sources = [ "${root_out_dir}/exe.unstripped/${_crate_name}" ] | |
| outputs = [ "${root_out_dir}/${_crate_name}_unstripped" ] | |
| deps = [ ":${exe_target_name}" ] | |
| } | |
| } | |
| _test_data_target = "${target_name}_rust_test_data" | |
| crate_binary_path = "" | |
| test_args = [] | |
| if (defined(args)) { | |
| test_args = args | |
| } | |
| if (defined(invoker.output_dir)) { | |
| crate_binary_path = "${invoker.output_dir}/${_crate_name}" | |
| } else if (use_unstripped_binary_for_host_tests) { | |
| # Default to using the unstripped binary so that stack traces are easier | |
| # to read. Override the name to still use the old path for consistency | |
| # with previous test results. For instance, if the stripped binary is | |
| # host_x64/foo_test and unstripped is host_x64/foo_test_unstripped, name | |
| # should still be host_x64/foo_test. | |
| # | |
| # Only do this when the invoker does not pass `args`. Otherwise, the | |
| # test name defaults to the path of the wrapper script generated by | |
| # host_test(), which is what builders using the stripped binary (e.g. | |
| # variant builders) report, and the name must be the same on all | |
| # builders. | |
| crate_binary_path = "${root_out_dir}/${_crate_name}_unstripped" | |
| if (!defined(args)) { | |
| test_name = | |
| rebase_path("${root_out_dir}/${_crate_name}", "${root_out_dir}/..") | |
| } | |
| test_args += [ "env RUST_BACKTRACE=1" ] | |
| } else { | |
| # For asan targets use the normal binary path. | |
| crate_binary_path = "${root_out_dir}/${_crate_name}" | |
| } | |
| host_test_data(_test_data_target) { | |
| sources = [ crate_binary_path ] | |
| deps = [ ":${exe_target_name}" ] | |
| if (use_unstripped_binary_for_host_tests) { | |
| deps += [ ":${unstripped_copy_target_name}" ] | |
| } | |
| } | |
| host_test(target_name) { | |
| # LINT.IfChange(rustc_test_invocation) | |
| binary_path = get_label_info("//tools/rust_test_parser", "root_out_dir") + | |
| "/rust_test_parser" | |
| if (defined(test_name)) { | |
| name = test_name | |
| } else if (test_args == []) { | |
| name = rebase_path(crate_binary_path, root_build_dir) | |
| } | |
| args = [] | |
| args = [ rebase_path(crate_binary_path, root_build_dir) ] + test_args | |
| # LINT.ThenChange(//build/bazel/rules/host_tests/host_rustc_test.bzl:rustc_test_invocation) | |
| # The host test should only depend on the _actual_ binary, because only | |
| # the binary is able to properly depend on the toolchain variants | |
| # correctly. | |
| # It shouldn't depend on any of the "deps" as they will be in the wrong | |
| # toolchain when variants are in use, and it will cause the compilation of | |
| # non-variant toolchain artifacts that aren't actually used. | |
| # | |
| # HOWEVER, that bypasses clippy, which currently both uses rlibs and does | |
| # know how to properly switch to the correct variant toolchain. So this | |
| # instead deps on the group that combines both clippy and the binary. But | |
| # it doesn't depend on any other deps. | |
| deps = [ | |
| ":${_test_data_target}", | |
| ":${exe_target_name}", | |
| "//tools/rust_test_parser", | |
| ] | |
| if (use_unstripped_binary_for_host_tests) { | |
| deps += [ ":${unstripped_copy_target_name}" ] | |
| } | |
| # However, if there is host-test-data, then that should be passed via | |
| # data_deps, so include those here | |
| if (defined(invoker.data_deps)) { | |
| data_deps = invoker.data_deps | |
| } | |
| if (defined(invoker.test_environments)) { | |
| environments = invoker.test_environments | |
| } | |
| if (defined(invoker.test_build_only)) { | |
| build_only = invoker.test_build_only | |
| } | |
| forward_variables_from(invoker, | |
| [ | |
| "package_deps", | |
| "visibility", | |
| ]) | |
| } | |
| } else { | |
| # if not a host-side target, we don't need this since it doesn't | |
| # propogate to package.gni | |
| # | |
| # `args` is only read in the is_host branch above. A BUILD.gn file is | |
| # evaluated in both toolchains, so callers that set it unconditionally would | |
| # otherwise trip GN's "Assignment had no effect" check here. | |
| not_needed(invoker, | |
| [ | |
| "args", | |
| "test_environments", | |
| ]) | |
| } | |
| rustflags = [] | |
| if (defined(invoker.rustflags)) { | |
| rustflags = invoker.rustflags | |
| } | |
| rustc_artifact(exe_target_name) { | |
| forward_variables_from(invoker, | |
| [ | |
| "allow_legacy_edition", | |
| "applicable_licenses", | |
| "check_cfgs", | |
| "data_deps", | |
| "disable_clippy", | |
| "edition", | |
| "enforce_source_listing", | |
| "exclude_toolchain_tags", | |
| "features", | |
| "inputs", | |
| "link_deps", | |
| "lint_config", | |
| "output_dir", | |
| "proc_macro_deps", | |
| "public_deps", | |
| "quiet_clippy", | |
| "rustenv", | |
| "sources", | |
| "visibility", | |
| ]) | |
| target_type = "executable" | |
| crate_root = source_root | |
| crate_type = "bin" | |
| original_target_name = _original_target_name | |
| crate_name = _crate_name | |
| output_name = _crate_name | |
| configs = [] | |
| configs = invoker.configs | |
| # Add test-only configs | |
| if (defined(invoker.test_configs)) { | |
| configs += invoker.test_configs | |
| } | |
| if (defined(invoker.test_vars)) { | |
| forward_variables_from(invoker.test_vars, "*") | |
| } | |
| # tell rustc to compile in the test harness | |
| rustflags += [ "--test" ] | |
| # anything defined with rustc_test MUST be a test target | |
| testonly = true | |
| # Exclude tests from tool_paths.json. | |
| emit_tool_path = false | |
| _use_rbe = rust_rbe_enable | |
| if (defined(invoker.disable_rbe)) { | |
| _use_rbe = !invoker.disable_rbe | |
| } | |
| _concurrency = concurrent_jobs.remote | |
| if (!_use_rbe) { | |
| disable_rbe = true | |
| _any_lto = toolchain_variant.tags + [ | |
| "lto", | |
| "thinlto", | |
| ] - | |
| [ | |
| "lto", | |
| "thinlto", | |
| ] != toolchain_variant.tags | |
| if (_any_lto) { | |
| # LTO on tests can be memory-intensive, especially for Rust, | |
| # so reduce concurrency when running locally. | |
| # See https://fxbug.dev/42063215 for more context. | |
| _concurrency = concurrent_jobs.local | |
| } | |
| } | |
| forward_variables_from(_concurrency, "*") # sets a pool | |
| # Allow target to override pool, e.g. "highmem" for exceptionally | |
| # large jobs that should run with even less concurrency. | |
| if (defined(invoker.pool)) { | |
| pool = invoker.pool | |
| } | |
| 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_fuchsia) { | |
| metadata = { | |
| # Used by the fuchsia_test_component_manifest() template. | |
| test_component_manifest_cml = [ | |
| { | |
| include = [ | |
| "//src/sys/test_runners/rust/default.shard.cml", | |
| "//sdk/lib/syslog/use.shard.cml", | |
| ] | |
| }, | |
| ] | |
| disable_rustdoc = [ true ] | |
| } | |
| } | |
| } | |
| } | |
| # Defines a Rust unit test, without also compiling a separate binary or library | |
| # target. | |
| # | |
| # Parameters | |
| # | |
| # output_name (optional) | |
| # name (optional, deprecated) | |
| # 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 | |
| # Edition of the Rust language to be used. See | |
| # https://doc.rust-lang.org/edition-guide/editions/index.html for more info on rust editions. | |
| # | |
| # configs (optional) | |
| # A list of config labels applying to this target. | |
| # | |
| # enforce_source_listing (optional) | |
| # When true, enforces that any source files used by the Rust compiler are | |
| # listed in `sources`. Defaults to true. | |
| # | |
| # sources (optional) | |
| # List of source files which this crate is allowed to compile. Only | |
| # allowed when `enforce_source_listing = true`. | |
| # The Rust compiler discovers source files by following `mod` declarations | |
| # starting at the `source_root`. The discovered source files must match this | |
| # list. | |
| # | |
| # inputs (optional) | |
| # List of additional non-source files read by the compiler. These are typically | |
| # configuration or test-data files included in the build with the `include_str!` | |
| # macro. Only allowed when `enforce_source_listing = true`. | |
| # | |
| # 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. | |
| # OBSOLETE! Please use deps or data_deps instead. | |
| # | |
| # link_deps (optional) | |
| # Native (e.g. C/C++) libraries to link against, has the same semantics | |
| # as deps in GN. Used by Bazel-converted targets, see | |
| # //build/tools/bazel2gn/README.md. | |
| # | |
| # data_deps (optional) | |
| # List of GN targets that are only needed at runtime. Use this for any | |
| # host_test_data() that is needed by the test. | |
| # | |
| # package_deps (optional) | |
| # List of `fuchsia_package` labels needed by the test at runtime. | |
| # See //build/testing/test_spec.gni for more details. | |
| # | |
| # test_environments (optional) | |
| # What environments the unit test should target. Only used here | |
| # for linux host tests, with a default value of a general linux | |
| # environment. | |
| # See environments parameter on //build/testing/test_spec.gni for more | |
| # details. | |
| # | |
| # args (optional) | |
| # List of flags to pass directly to the test binary, e.g. | |
| # ["--nocapture"]. | |
| # | |
| # 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"' | |
| # | |
| # check_cfgs (optional) | |
| # A list of valid configurations for the target. | |
| # Passed to rustc as '--check-cfg=cfg(###)'. | |
| # Not specifying or using an empty list disables configuration checking. | |
| # See https://doc.rust-lang.org/nightly/rustc/check-cfg.html for the syntax. | |
| # | |
| # rustenv (optional) | |
| # A list of environment variables that will be set when running the rust | |
| # compiler. These can be accessed at compile time with | |
| # [`std::env!`](https://doc.rust-lang.org/stable/std/macro.env.html) | |
| # | |
| # output_dir (optional) | |
| # Directory that the resulting test should be placed in. | |
| # See: `gn help output_dir` | |
| # | |
| # pool (optional) | |
| # Override the default concurrency pool when building the Rust test binary. | |
| # e.g. extremely high memory jobs run locally could use | |
| # "//build/config:highmem". | |
| # | |
| # disable_rbe (optional) | |
| # Set to true to force this target to build locally, overriding the global | |
| # `rust_rbe_enable`. | |
| # | |
| # disable_clippy (optional) | |
| # Don't run clippy on this target. | |
| # | |
| # original_target_name (optional) | |
| # The name of the target as it appears in the BUILD file. Enables tooling | |
| # to find the template invocation in a BUILD file where this target was defined. | |
| # | |
| # exclude_toolchain_tags (optional) | |
| # List of toolchain or variant tags to exclude this target from. This can be | |
| # useful if the target is incompatible with a certain toolchain or variant. | |
| # See build/toolchain/toolchain_tags.gni and build/toolchain/variant_tags.gni | |
| # for a list of common tags. | |
| # | |
| # 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", | |
| # ] | |
| # sources = [ "src/lib.rs" ] | |
| # } | |
| template("rustc_test") { | |
| rustc_test_internal(target_name) { | |
| forward_variables_from(invoker, "*") | |
| } | |
| } |