diff --git a/src/bin/cargo/commands/bench.rs b/src/bin/cargo/commands/bench.rs index b1398c7a4f8..943ca577590 100644 --- a/src/bin/cargo/commands/bench.rs +++ b/src/bin/cargo/commands/bench.rs @@ -48,6 +48,7 @@ pub fn cli() -> App { )) .arg_unit_graph() .arg_timings() + .arg_rustflags() .after_help("Run `cargo help bench` for more detailed information.\n") } diff --git a/src/bin/cargo/commands/build.rs b/src/bin/cargo/commands/build.rs index 9bcf29fe901..1ce4a8a34a6 100644 --- a/src/bin/cargo/commands/build.rs +++ b/src/bin/cargo/commands/build.rs @@ -45,6 +45,7 @@ pub fn cli() -> App { .arg_unit_graph() .arg_future_incompat_report() .arg_timings() + .arg_rustflags() .after_help("Run `cargo help build` for more detailed information.\n") } diff --git a/src/bin/cargo/commands/check.rs b/src/bin/cargo/commands/check.rs index 0196fb5f652..b52db19f89a 100644 --- a/src/bin/cargo/commands/check.rs +++ b/src/bin/cargo/commands/check.rs @@ -37,6 +37,7 @@ pub fn cli() -> App { .arg_unit_graph() .arg_future_incompat_report() .arg_timings() + .arg_rustflags() .after_help("Run `cargo help check` for more detailed information.\n") } diff --git a/src/bin/cargo/commands/doc.rs b/src/bin/cargo/commands/doc.rs index 429662c5f7f..3ac603e4d38 100644 --- a/src/bin/cargo/commands/doc.rs +++ b/src/bin/cargo/commands/doc.rs @@ -40,6 +40,7 @@ pub fn cli() -> App { .arg_ignore_rust_version() .arg_unit_graph() .arg_timings() + .arg_rustflags() .after_help("Run `cargo help doc` for more detailed information.\n") } diff --git a/src/bin/cargo/commands/fix.rs b/src/bin/cargo/commands/fix.rs index 55bba92c0cf..f36e1494522 100644 --- a/src/bin/cargo/commands/fix.rs +++ b/src/bin/cargo/commands/fix.rs @@ -54,6 +54,7 @@ pub fn cli() -> App { )) .arg_ignore_rust_version() .arg_timings() + .arg_rustflags() .after_help("Run `cargo help fix` for more detailed information.\n") } diff --git a/src/bin/cargo/commands/install.rs b/src/bin/cargo/commands/install.rs index 8416cdfa57c..c50d09bd895 100644 --- a/src/bin/cargo/commands/install.rs +++ b/src/bin/cargo/commands/install.rs @@ -82,6 +82,7 @@ pub fn cli() -> App { ) .arg_message_format() .arg_timings() + .arg_rustflags() .after_help("Run `cargo help install` for more detailed information.\n") } diff --git a/src/bin/cargo/commands/package.rs b/src/bin/cargo/commands/package.rs index d820bc1e050..e3cf623a34b 100644 --- a/src/bin/cargo/commands/package.rs +++ b/src/bin/cargo/commands/package.rs @@ -35,6 +35,7 @@ pub fn cli() -> App { ) .arg_manifest_path() .arg_jobs() + .arg_rustflags() .after_help("Run `cargo help package` for more detailed information.\n") } diff --git a/src/bin/cargo/commands/run.rs b/src/bin/cargo/commands/run.rs index e9e7f253a9c..bf17e809b05 100644 --- a/src/bin/cargo/commands/run.rs +++ b/src/bin/cargo/commands/run.rs @@ -32,6 +32,7 @@ pub fn cli() -> App { .arg_unit_graph() .arg_ignore_rust_version() .arg_timings() + .arg_rustflags() .after_help("Run `cargo help run` for more detailed information.\n") } diff --git a/src/bin/cargo/commands/test.rs b/src/bin/cargo/commands/test.rs index f120c617f9e..04a27a6f466 100644 --- a/src/bin/cargo/commands/test.rs +++ b/src/bin/cargo/commands/test.rs @@ -56,6 +56,7 @@ pub fn cli() -> App { .arg_unit_graph() .arg_future_incompat_report() .arg_timings() + .arg_rustflags() .after_help( "Run `cargo help test` for more detailed information.\n\ Run `cargo test -- --help` for test binary options.\n", diff --git a/src/cargo/core/compiler/fingerprint.rs b/src/cargo/core/compiler/fingerprint.rs index 35a61be13a1..cbf92f822a4 100644 --- a/src/cargo/core/compiler/fingerprint.rs +++ b/src/cargo/core/compiler/fingerprint.rs @@ -1302,13 +1302,16 @@ fn calculate_normal(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult, unit: &Unit) -> CargoResult, unit: &Unit, exec: &Arc) -> Car let dep_info_loc = fingerprint::dep_info_loc(cx, unit); rustc.args(cx.bcx.rustflags_args(unit)); + + // Append any user specified rustflags on the command line + // via the --rustflag argument. + rustc.args(&unit.rustflags); + if cx.bcx.config.cli_unstable().binary_dep_depinfo { rustc.arg("-Z").arg("binary-dep-depinfo"); } + let mut output_options = OutputOptions::new(cx, unit); let package_id = unit.pkg.package_id(); let target = Target::clone(&unit.target); diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs index e0baebd516b..2c36672be80 100644 --- a/src/cargo/core/compiler/standard_lib.rs +++ b/src/cargo/core/compiler/standard_lib.rs @@ -214,6 +214,8 @@ pub fn generate_std_roots( *kind, mode, features.clone(), + /*rustflags*/ + Default::default(), // Setting command line rustflags for the standard library is not supported /*is_std*/ true, /*dep_hash*/ 0, IsArtifact::No, diff --git a/src/cargo/core/compiler/unit.rs b/src/cargo/core/compiler/unit.rs index 32a98cb8cd7..8ef0fa22c2f 100644 --- a/src/cargo/core/compiler/unit.rs +++ b/src/cargo/core/compiler/unit.rs @@ -55,6 +55,8 @@ pub struct UnitInner { /// The `cfg` features to enable for this unit. /// This must be sorted. pub features: Vec, + /// The rustflags to set for this unit. + pub rustflags: Vec, // if `true`, the dependency is an artifact dependency, requiring special handling when // calculating output directories, linkage and environment variables provided to builds. pub artifact: IsArtifact, @@ -181,6 +183,7 @@ impl UnitInterner { kind: CompileKind, mode: CompileMode, features: Vec, + rustflags: Vec, is_std: bool, dep_hash: u64, artifact: IsArtifact, @@ -213,6 +216,7 @@ impl UnitInterner { kind, mode, features, + rustflags, is_std, dep_hash, artifact, diff --git a/src/cargo/core/compiler/unit_dependencies.rs b/src/cargo/core/compiler/unit_dependencies.rs index 4bd8aedf2b2..aaed84c13e8 100644 --- a/src/cargo/core/compiler/unit_dependencies.rs +++ b/src/cargo/core/compiler/unit_dependencies.rs @@ -284,6 +284,7 @@ fn compute_deps( unit, dep_pkg, dep_lib, + /*rustflags*/ Default::default(), dep_unit_for, unit.kind, mode, @@ -295,6 +296,7 @@ fn compute_deps( unit, dep_pkg, dep_lib, + /*rustflags*/ Default::default(), dep_unit_for, CompileKind::Host, mode, @@ -307,6 +309,7 @@ fn compute_deps( unit, dep_pkg, dep_lib, + /*rustflags*/ Default::default(), dep_unit_for, unit.kind.for_target(dep_lib), mode, @@ -377,6 +380,7 @@ fn compute_deps( unit, &unit.pkg, t, + /*rustflags*/ Default::default(), UnitFor::new_normal(unit_for.root_compile_kind()), unit.kind.for_target(t), CompileMode::Build, @@ -479,6 +483,7 @@ fn compute_deps_custom_build( unit, &unit.pkg, &unit.target, + /*rustflags*/ Default::default(), script_unit_for, // Build scripts always compiled for the host. CompileKind::Host, @@ -568,6 +573,7 @@ fn artifact_targets_to_unit_deps( target .clone() .set_kind(TargetKind::Lib(vec![target_kind.clone()])), + /*rustflags*/ Default::default(), parent_unit_for, compile_kind, CompileMode::Build, @@ -580,6 +586,7 @@ fn artifact_targets_to_unit_deps( parent, artifact_pkg, target, + /*rustflags*/ Default::default(), parent_unit_for, compile_kind, CompileMode::Build, @@ -655,6 +662,7 @@ fn compute_deps_doc( unit, dep_pkg, dep_lib, + /*rustflags*/ Default::default(), dep_unit_for, unit.kind.for_target(dep_lib), mode, @@ -669,6 +677,7 @@ fn compute_deps_doc( unit, dep_pkg, dep_lib, + /*rustflags*/ Default::default(), dep_unit_for, unit.kind.for_target(dep_lib), unit.mode, @@ -699,6 +708,7 @@ fn compute_deps_doc( unit, &unit.pkg, lib, + /*rustflags*/ Default::default(), dep_unit_for, unit.kind.for_target(lib), unit.mode, @@ -717,6 +727,7 @@ fn compute_deps_doc( scrape_unit, &scrape_unit.pkg, &scrape_unit.target, + /*rustflags*/ Default::default(), unit_for, scrape_unit.kind, scrape_unit.mode, @@ -740,11 +751,15 @@ fn maybe_lib( .map(|t| { let mode = check_or_build_mode(unit.mode, t); let dep_unit_for = unit_for.with_dependency(unit, t, unit_for.root_compile_kind()); + + // When adding the lib targets for the current unit also pass down the user specified + // rustflags for the package. new_unit_dep( state, unit, &unit.pkg, t, + unit.rustflags.clone(), dep_unit_for, unit.kind.for_target(t), mode, @@ -805,6 +820,7 @@ fn dep_build_script( unit, &unit.pkg, t, + Default::default(), script_unit_for, unit.kind, CompileMode::RunCustomBuild, @@ -839,6 +855,7 @@ fn new_unit_dep( parent: &Unit, pkg: &Package, target: &Target, + rustflags: Vec, unit_for: UnitFor, kind: CompileKind, mode: CompileMode, @@ -853,7 +870,7 @@ fn new_unit_dep( kind, ); new_unit_dep_with_profile( - state, parent, pkg, target, unit_for, kind, mode, profile, artifact, + state, parent, pkg, target, rustflags, unit_for, kind, mode, profile, artifact, ) } @@ -862,6 +879,7 @@ fn new_unit_dep_with_profile( parent: &Unit, pkg: &Package, target: &Target, + rustflags: Vec, unit_for: UnitFor, kind: CompileKind, mode: CompileMode, @@ -885,6 +903,7 @@ fn new_unit_dep_with_profile( kind, mode, features, + rustflags, state.is_std, /*dep_hash*/ 0, artifact.map_or(IsArtifact::No, |_| IsArtifact::Yes), diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 9cca2ee3099..4252b3b62a5 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -62,6 +62,8 @@ pub struct CompileOptions { pub build_config: BuildConfig, /// Feature flags requested by the user. pub cli_features: CliFeatures, + /// Rustflags requested by the user. + pub rustflags: Vec, /// A set of packages to build. pub spec: Packages, /// Filter to apply to the root package to select which targets will be @@ -91,6 +93,7 @@ impl CompileOptions { Ok(CompileOptions { build_config: BuildConfig::new(config, jobs, keep_going, &[], mode)?, cli_features: CliFeatures::new_all(false), + rustflags: Vec::new(), spec: ops::Packages::Packages(Vec::new()), filter: CompileFilter::Default { required_features_filterable: false, @@ -335,6 +338,7 @@ pub fn create_bcx<'a, 'cfg>( ref build_config, ref spec, ref cli_features, + ref rustflags, ref filter, ref target_rustdoc_args, ref target_rustc_args, @@ -492,6 +496,7 @@ pub fn create_bcx<'a, 'cfg>( &resolve, &workspace_resolve, &resolved_features, + &rustflags, &pkg_set, &profiles, interner, @@ -532,6 +537,7 @@ pub fn create_bcx<'a, 'cfg>( &resolve, &workspace_resolve, &resolved_features, + &rustflags, &pkg_set, &profiles, interner, @@ -983,6 +989,7 @@ fn generate_targets( resolve: &Resolve, workspace_resolve: &Option, resolved_features: &features::ResolvedFeatures, + rustflags: &Vec, package_set: &PackageSet<'_>, profiles: &Profiles, interner: &UnitInterner, @@ -1082,6 +1089,15 @@ fn generate_targets( unit_for, *kind, ); + + // Doc units will not set the user specified rustflags since these use + // rustdoc specific flags instead. + let rustflags = if target_mode.is_doc() { + Default::default() + } else { + rustflags.clone() + }; + let unit = interner.intern( pkg, target, @@ -1089,6 +1105,7 @@ fn generate_targets( kind.for_target(target), target_mode, features.clone(), + rustflags.clone(), /*is_std*/ false, /*dep_hash*/ 0, IsArtifact::No, @@ -1672,6 +1689,7 @@ fn traverse_and_share( new_kind, unit.mode, unit.features.clone(), + unit.rustflags.clone(), unit.is_std, new_dep_hash, unit.artifact, @@ -1913,6 +1931,7 @@ fn override_rustc_crate_types( unit.kind, unit.mode, unit.features.clone(), + unit.rustflags.clone(), unit.is_std, unit.dep_hash, unit.artifact, diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index 270e1d70d3a..0e32c473ffb 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -822,6 +822,7 @@ fn run_verify( CompileMode::Build, )?, cli_features: opts.cli_features.clone(), + rustflags: Vec::new(), spec: ops::Packages::Packages(Vec::new()), filter: ops::CompileFilter::Default { required_features_filterable: true, diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 5d197b9f117..5831c4d7999 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -253,6 +253,19 @@ pub trait AppExt: Sized { .require_equals(true), ) } + + fn arg_rustflags(self) -> Self { + self._arg( + multi_opt( + "rustflags", + "RUSTFLAGS", + "Space separated list of RUSTFLAGS (unstable) to enable for the package. Requires a `;` to terminate this option." + ) + .min_values(1) + .allow_hyphen_values(true) + .value_terminator(";") + ) + } } impl AppExt for App { @@ -593,6 +606,7 @@ pub trait ArgMatchesExt { let opts = CompileOptions { build_config, cli_features: self.cli_features()?, + rustflags: self.rustflags(config)?, spec, filter: CompileFilter::from_raw_arguments( self.flag("lib"), @@ -637,6 +651,22 @@ pub trait ArgMatchesExt { ) } + fn rustflags(&self, config: &Config) -> CargoResult> { + let mut rustflags = Vec::new(); + + if self._contains("rustflags") { + config.cli_unstable().fail_if_stable_opt("--rustflags", 0)?; + + rustflags.extend( + self._values_of("rustflags") + .iter() + .map(InternedString::from), + ); + } + + Ok(rustflags) + } + fn compile_options_for_single_package( &self, config: &Config, diff --git a/src/doc/src/reference/unstable.md b/src/doc/src/reference/unstable.md index a04c51fdc76..2bf7d21eda7 100644 --- a/src/doc/src/reference/unstable.md +++ b/src/doc/src/reference/unstable.md @@ -81,6 +81,7 @@ Each new feature described below should explain how to use it. * [panic-abort-tests](#panic-abort-tests) — Allows running tests with the "abort" panic strategy. * [crate-type](#crate-type) — Supports passing crate types to the compiler. * [keep-going](#keep-going) — Build as much as possible rather than aborting on the first error. + * [rustflags](#rustflags) — Supports passing RUSTFLAGS to the compiler for the root crate. * rustdoc * [`doctest-in-workspace`](#doctest-in-workspace) — Fixes workspace-relative paths when running doctests. * [rustdoc-map](#rustdoc-map) — Provides mappings for documentation to link to external sites like [docs.rs](https://docs.rs/). @@ -428,6 +429,26 @@ The `-Z unstable-options` command-line option must be used in order to use cargo check --keep-going -Z unstable-options ``` +### rustflags +* Tracking Issue: [#00000](https://github.com/rust-lang/cargo/issues/00000) + +`cargo build --rustflags` (and similarly for `run`, `test` etc) forward the +rustflags specified to the compiler only for the root crate. Dependencies, +including transitive dependencies, will not have these rustflags enabled. + +For example if the current package depends on dependencies `foo` and `bar`, +`cargo test --rustflags -C instrument-coverage ` will +instrument only the libraries for the current package and not for `foo` or `bar`. + +This option supports multiple values that begin with a leading hypen(`-`), +as such, a value terminator (`;`) is required to indicate the end of these +values. This terminator will allow additional cargo options to be passed via +the command line after the values to the `--rustflags` option. + +```console +cargo test --rustflags -C instrument-coverage ; -Z unstable-options +``` + ### config-include * Tracking Issue: [#7723](https://github.com/rust-lang/cargo/issues/7723) diff --git a/tests/testsuite/rustflags.rs b/tests/testsuite/rustflags.rs index 7d0fd75db59..5e261e4be58 100644 --- a/tests/testsuite/rustflags.rs +++ b/tests/testsuite/rustflags.rs @@ -2,7 +2,7 @@ use cargo_test_support::registry::Package; use cargo_test_support::{ - basic_lib_manifest, basic_manifest, paths, project, project_in_home, rustc_host, + basic_lib_manifest, basic_manifest, cross_compile, paths, project, project_in_home, rustc_host, }; use std::fs; @@ -1671,3 +1671,365 @@ fn host_config_rustflags_with_target() { .arg("host.rustflags=[\"--cfg=foo\"]") .run(); } + +#[cargo_test] +fn command_line_rustflags() { + let p = project().file("src/lib.rs", "").build(); + + p.cargo("build -v") + .masquerade_as_nightly_cargo(&["rustflags"]) + .arg("-Zunstable-options") + .arg("--rustflags") + .arg("-C") + .arg("instrument-coverage") + .arg(";") + .with_stderr( + "\ +[COMPILING] foo v0.0.1 ([..]) +[RUNNING] [..]-C instrument-coverage[..] +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run(); +} + +#[cargo_test] +fn command_line_rustflags_multiple() { + let p = project().file("src/lib.rs", "").build(); + + p.cargo("build -v") + .masquerade_as_nightly_cargo(&["rustflags"]) + .arg("-Zunstable-options") + .arg("--rustflags") + .arg("-C") + .arg("instrument-coverage") + .arg("-C") + .arg("strip=debuginfo") + .arg(";") + .with_stderr( + "\ +[COMPILING] foo v0.0.1 ([..]) +[RUNNING] [..]-C instrument-coverage -C strip=debuginfo[..] +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run(); +} + +#[cargo_test] +fn command_line_rustflags_with_dependency() { + Package::new("bar", "0.1.0") + .file( + "src/lib.rs", + r#"pub fn print_hello_world() { + println!("Hello, World!"); + }"#, + ) + .publish(); + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + + [dependencies] + bar = "0.1" + "#, + ) + .file( + "src/lib.rs", + r#" + pub fn print() { + bar::print_hello_world(); + } + "#, + ) + .build(); + + p.cargo("build -v") + .masquerade_as_nightly_cargo(&["rustflags"]) + .arg("-Zunstable-options") + .arg("--rustflags") + .arg("-C") + .arg("instrument-coverage") + .arg(";") + .with_stderr( + "\ +[UPDATING] `dummy-registry` index[..] +[DOWNLOADING] crates ...[..] +[DOWNLOADED] bar v0.1.0 (registry `dummy-registry`)[..] +[COMPILING] bar v0.1.0[..] +[RUNNING] [..] +[COMPILING] foo v0.1.0 ([..]) +[RUNNING] [..]-C instrument-coverage[..] +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run(); +} + +#[cargo_test] +fn command_line_rustflags_build_script() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + "#, + ) + .file( + "build.rs", + r#" + pub fn main() { + println!(""); + } + "#, + ) + .file( + "src/lib.rs", + r#" + "#, + ) + .build(); + + p.cargo("build -v") + .masquerade_as_nightly_cargo(&["rustflags"]) + .arg("-Zunstable-options") + .arg("--rustflags") + .arg("-C") + .arg("instrument-coverage") + .arg(";") + .with_stderr_line_without( + &["[RUNNING] `rustc --crate-name build_script_build"], + &["-C instrument-coverage"], + ) + .run(); +} + +#[cargo_test] +fn command_line_rustflags_proc_macro() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + + [lib] + proc-macro = true + "#, + ) + .file("src/lib.rs", "") + .build(); + + p.cargo("build -v") + .masquerade_as_nightly_cargo(&["rustflags"]) + .arg("-Zunstable-options") + .arg("--rustflags") + .arg("-C") + .arg("instrument-coverage") + .arg(";") + .with_stderr_contains("[RUNNING] `rustc --crate-name foo [..] -C instrument-coverage[..]") + .run(); +} + +#[cargo_test] +fn command_line_rustflags_proc_macro_dependency() { + Package::new("bar", "0.1.0").proc_macro(true).publish(); + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + + [dependencies] + bar = "0.1" + "#, + ) + .file("src/lib.rs", "") + .build(); + + p.cargo("build -v") + .masquerade_as_nightly_cargo(&["rustflags"]) + .arg("-Zunstable-options") + .arg("--rustflags") + .arg("-C") + .arg("instrument-coverage") + .arg(";") + .with_stderr_line_without( + &["[RUNNING] `rustc --crate-name bar"], + &["-C instrument-coverage"], + ) + .run(); +} + +#[cargo_test] +fn command_line_rustflags_workspace() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "workspace" + version = "0.0.1" + + [workspace] + members = ["foo", "bar"] + "#, + ) + .file("src/lib.rs", "") + .file( + "foo/Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + "#, + ) + .file("foo/src/lib.rs", "") + .file( + "bar/Cargo.toml", + r#" + [package] + name = "bar" + version = "0.0.1" + "#, + ) + .file("bar/src/lib.rs", "") + .build(); + + p.cargo("build -v") + .masquerade_as_nightly_cargo(&["rustflags"]) + .arg("-Zunstable-options") + .arg("--rustflags") + .arg("-C") + .arg("instrument-coverage") + .arg(";") + .with_stderr_unordered( + "\ +[COMPILING] workspace v0.0.1 ([..]) +[RUNNING] `rustc --crate-name workspace [..] -C instrument-coverage[..] +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run(); +} + +#[cargo_test] +fn command_line_rustflags_virtual_workspace() { + let p = project() + .file( + "Cargo.toml", + r#" + [workspace] + members = ["foo", "bar"] + "#, + ) + .file( + "foo/Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + "#, + ) + .file("foo/src/lib.rs", "") + .file( + "bar/Cargo.toml", + r#" + [package] + name = "bar" + version = "0.0.1" + "#, + ) + .file("bar/src/lib.rs", "") + .build(); + + p.cargo("build -v") + .masquerade_as_nightly_cargo(&["rustflags"]) + .arg("-Zunstable-options") + .arg("--rustflags") + .arg("-C") + .arg("instrument-coverage") + .arg(";") + .with_stderr_unordered( + "\ +[COMPILING] foo v0.0.1 ([..]) +[COMPILING] bar v0.0.1 ([..]) +[RUNNING] `rustc --crate-name foo [..] -C instrument-coverage[..] +[RUNNING] `rustc --crate-name bar [..] -C instrument-coverage[..] +[FINISHED] dev [unoptimized + debuginfo] target(s) in [..] +", + ) + .run(); +} + +#[cargo_test] +fn command_line_rustflags_multiple_artifacts() { + let p = project() + .file("examples/example.rs", "pub fn main() { }") + .file("src/lib.rs", "") + .file("src/main.rs", "pub fn main() { }") + .file("src/bin/bin1.rs", "pub fn main() { }") + .build(); + + p.cargo("test -v") + .masquerade_as_nightly_cargo(&["rustflags"]) + .arg("-Zunstable-options") + .arg("--rustflags") + .arg("-C") + .arg("instrument-coverage") + .arg(";") + .with_stderr_unordered( + "\ +[COMPILING] foo v0.0.1 ([..]) +[RUNNING] `rustc --crate-name foo [..] --crate-type lib [..] -C instrument-coverage[..] +[RUNNING] `rustc --crate-name foo src/lib.rs [..] --test [..] -C instrument-coverage[..] +[RUNNING] `rustc --crate-name bin1 [..] --test [..] -C instrument-coverage[..] +[RUNNING] `rustc --crate-name foo src/main.rs [..] --test [..] -C instrument-coverage[..] +[RUNNING] `rustc --crate-name example [..] -C instrument-coverage[..] +[FINISHED] test [..] +[RUNNING] [..] +[RUNNING] [..] +[RUNNING] [..] +[DOCTEST] [..] +[RUNNING] [..] +", + ) + .run(); +} + +#[cargo_test] +fn command_line_rustflags_cross_compile() { + if cross_compile::disabled() { + return; + } + let target = cross_compile::alternate(); + + let p = project().file("src/lib.rs", "").build(); + + p.cargo(&format!("build --target {} -v", target)) + .masquerade_as_nightly_cargo(&["rustflags"]) + .arg("-Zunstable-options") + .arg("--rustflags") + .arg("-C") + .arg("instrument-coverage") + .arg(";") + .with_stderr( + "\ +[COMPILING] foo v0.0.1 ([..]) +[RUNNING] `rustc --crate-name foo [..] -C instrument-coverage[..] +[FINISHED] [..] +", + ) + .run(); +}