From 0018ef0698e2a4bfa6da77dcac76664e57feed9a Mon Sep 17 00:00:00 2001 From: Chris Field Date: Tue, 15 Dec 2020 17:24:30 -0500 Subject: [PATCH 01/43] Add minimum working implementation The `--cfg` flag is added to the `cargo rustc` subcommand. The implementation generally follows the `--unit-graph` implementation in that it aborts compilation after the build context is created. I discovered that cargo runs the `rustc --print cfg` every time it builds/compiles a package. It stores this information for all compiler targets and the host in the `RustcTargetData` struct, which is further populated when the build context is created. When the `rustc --print cfg` command is ran internally, all of the Cargo configurations and environment variables are applied to the command. This means that the command does not need to be re-run for the `--cfg` flag. Instead, I just needed to print what was already populated into the `RustcTargetData` struct for the build context and abort before executing the build/compile job. The existence of the `rustc --print cfg` command being executed internally to Cargo also meant that multi-target and cross-compilation are naturally supported. However, the output kind of has to be JSON because it is not possible to select a single compiler target to print. It gets messy very quickly if multiple targets are specified and which one to use for the "human" output similar to the `rustc --print cfg` command. The solution is to output in JSON like the `--unit-graph` flag and include the data for the host and any specified targets. A downstream parser can then pull out/extract the target data that is needed. The `--cfg` flag needs to be added to the `cargo build` subcommand, too. The `--unit-graph` flag is available in both subcommands and so should the `--cfg` flag for consistency. Ultimately, the `cargo build` subcommand "calls" the `cargo rustc` subcommand. In other words, both subcommands use the same implementation. The flag does not appear in the help text, but the `--unit-graph` does not either. I need to work on this as well. --- src/bin/cargo/commands/rustc.rs | 1 + src/cargo/core/compiler/build_config.rs | 3 + src/cargo/core/compiler/cfg.rs | 85 +++++++++++++++++++++++++ src/cargo/core/compiler/mod.rs | 1 + src/cargo/ops/cargo_compile.rs | 6 +- src/cargo/util/command_prelude.rs | 5 ++ 6 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 src/cargo/core/compiler/cfg.rs diff --git a/src/bin/cargo/commands/rustc.rs b/src/bin/cargo/commands/rustc.rs index aa8fb6d9d9f..c801f0ffe9b 100644 --- a/src/bin/cargo/commands/rustc.rs +++ b/src/bin/cargo/commands/rustc.rs @@ -30,6 +30,7 @@ pub fn cli() -> App { .arg_manifest_path() .arg_message_format() .arg_unit_graph() + .arg_rustc_cfg() .after_help("Run `cargo help rustc` for more detailed information.\n") } diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs index d0f5431a47e..1eb427cbbe1 100644 --- a/src/cargo/core/compiler/build_config.rs +++ b/src/cargo/core/compiler/build_config.rs @@ -26,6 +26,8 @@ pub struct BuildConfig { pub build_plan: bool, /// Output the unit graph to stdout instead of actually compiling. pub unit_graph: bool, + /// Output the rustc configuration to stdout instead of actually compiling. + pub cfg: bool, /// An optional override of the rustc process for primary units pub primary_unit_rustc: Option, /// A thread used by `cargo fix` to receive messages on a socket regarding @@ -77,6 +79,7 @@ impl BuildConfig { force_rebuild: false, build_plan: false, unit_graph: false, + cfg: false, primary_unit_rustc: None, rustfix_diagnostic_server: RefCell::new(None), export_dir: None, diff --git a/src/cargo/core/compiler/cfg.rs b/src/cargo/core/compiler/cfg.rs new file mode 100644 index 00000000000..2a406cb246a --- /dev/null +++ b/src/cargo/core/compiler/cfg.rs @@ -0,0 +1,85 @@ +use cargo_platform::Cfg; + +use crate::core::compiler::{CompileKind, RustcTargetData}; +use crate::util::CargoResult; + +use std::collections::HashMap; +use std::io::Write; + +const VERSION: u32 = 1; + +// { +// "version": 1, +// "host": { +// "names": ["windows", "debug_assertions"], +// "key_pairs": [ +// {"target_os": "windows"}, +// {"target_vendor": "pc"} +// ] +// }, +// "targets": [ +// "x86_64-pc-windows-msvc": { +// "names": ["windows", "debug_assertions"], +// "key_pairs": [ +// {"target_os": "windows"}, +// {"target_vendor": "pc"} +// ] +// } +// ] +// } + +#[derive(serde::Serialize)] +struct SerializedRustcCfg<'a> { + version: u32, + host: SerializedCfg<'a>, + targets: HashMap<&'a str, SerializedCfg<'a>>, +} + +#[derive(serde::Serialize)] +struct SerializedCfg<'a> { + names: Vec<&'a str>, + key_pairs: Vec>, +} + +impl<'a> SerializedCfg<'a> { + fn new(rtd: &'a RustcTargetData, kind: CompileKind) -> Self { + Self { + names: rtd.cfg(kind).iter().filter_map(|c| { + match c { + Cfg::Name(s) => Some(s.as_str()), + Cfg::KeyPair(..) => None, + } + }).collect(), + key_pairs: rtd.cfg(kind).iter().filter_map(|c| { + match c { + Cfg::Name(..) => None, + Cfg::KeyPair(k, v) => { + let mut pair = HashMap::with_capacity(1); + pair.insert(k.as_str(), v.as_str()); + Some(pair) + } + } + }).collect() + } + } +} + +pub fn emit_serialized_rustc_cfg(rtd: &RustcTargetData, kinds: &[CompileKind]) -> CargoResult<()> { + let host = SerializedCfg::new(rtd, CompileKind::Host); + let targets = kinds.iter().filter_map(|k| { + match k { + CompileKind::Host => None, + CompileKind::Target(ct) => Some((ct.short_name(), SerializedCfg::new(rtd, *k))), + } + }).collect(); + let s = SerializedRustcCfg { + version: VERSION, + host, + targets + }; + let stdout = std::io::stdout(); + let mut lock = stdout.lock(); + serde_json::to_writer(&mut lock, &s)?; + drop(writeln!(lock)); + Ok(()) +} diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 7ac3842f4df..9d96b0f033e 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -19,6 +19,7 @@ mod timings; mod unit; pub mod unit_dependencies; pub mod unit_graph; +pub mod cfg; use std::env; use std::ffi::{OsStr, OsString}; diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 68ed8625e91..13d2abf62de 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -29,6 +29,7 @@ use std::sync::Arc; use crate::core::compiler::standard_lib; use crate::core::compiler::unit_dependencies::build_unit_dependencies; use crate::core::compiler::unit_graph::{self, UnitDep, UnitGraph}; +use crate::core::compiler::cfg; use crate::core::compiler::{BuildConfig, BuildContext, Compilation, Context}; use crate::core::compiler::{CompileKind, CompileMode, CompileTarget, RustcTargetData, Unit}; use crate::core::compiler::{DefaultExecutor, Executor, UnitInterner}; @@ -284,7 +285,10 @@ pub fn compile_ws<'a>( unit_graph::emit_serialized_unit_graph(&bcx.roots, &bcx.unit_graph)?; return Ok(Compilation::new(&bcx)?); } - + if options.build_config.cfg { + cfg::emit_serialized_rustc_cfg(&bcx.target_data, &bcx.build_config.requested_kinds)?; + return Ok(Compilation::new(&bcx)?); + } let _p = profile::start("compiling"); let cx = Context::new(&bcx)?; cx.compile(exec) diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 5f2a0ad50dc..134da802286 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -172,6 +172,10 @@ pub trait AppExt: Sized { self._arg(opt("unit-graph", "Output build graph in JSON (unstable)").hidden(true)) } + fn arg_rustc_cfg(self) -> Self { + self._arg(opt("cfg", "Output the ructc configuration to STDOUT").hidden(true)) + } + fn arg_new_opts(self) -> Self { self._arg( opt( @@ -469,6 +473,7 @@ pub trait ArgMatchesExt { build_config.requested_profile = self.get_profile_name(config, "dev", profile_checking)?; build_config.build_plan = self._is_present("build-plan"); build_config.unit_graph = self._is_present("unit-graph"); + build_config.cfg = self._is_present("cfg"); if build_config.build_plan { config .cli_unstable() From 7397f319ffa67af76424f2a4556806b868c3cacc Mon Sep 17 00:00:00 2001 From: Chris Field Date: Tue, 15 Dec 2020 20:04:32 -0500 Subject: [PATCH 02/43] Add `--cfg` to build and check subcommands --- src/bin/cargo/commands/build.rs | 1 + src/bin/cargo/commands/check.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/bin/cargo/commands/build.rs b/src/bin/cargo/commands/build.rs index e165b250d69..5fd61f5e7a4 100644 --- a/src/bin/cargo/commands/build.rs +++ b/src/bin/cargo/commands/build.rs @@ -42,6 +42,7 @@ pub fn cli() -> App { .arg_message_format() .arg_build_plan() .arg_unit_graph() + .arg_rustc_cfg() .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 6d26ef2d765..05750641dbd 100644 --- a/src/bin/cargo/commands/check.rs +++ b/src/bin/cargo/commands/check.rs @@ -34,6 +34,7 @@ pub fn cli() -> App { .arg_manifest_path() .arg_message_format() .arg_unit_graph() + .arg_rustc_cfg() .after_help("Run `cargo help check` for more detailed information.\n") } From 3a6850f41792450d55f5e75d5e6044c62c3af0e6 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Tue, 15 Dec 2020 20:07:20 -0500 Subject: [PATCH 03/43] Add `--cfg` to bench and test subcommands --- src/bin/cargo/commands/bench.rs | 1 + src/bin/cargo/commands/test.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/bin/cargo/commands/bench.rs b/src/bin/cargo/commands/bench.rs index 700dca258bf..28f404b0196 100644 --- a/src/bin/cargo/commands/bench.rs +++ b/src/bin/cargo/commands/bench.rs @@ -45,6 +45,7 @@ pub fn cli() -> App { "Run all benchmarks regardless of failure", )) .arg_unit_graph() + .arg_rustc_cfg() .after_help("Run `cargo help bench` for more detailed information.\n") } diff --git a/src/bin/cargo/commands/test.rs b/src/bin/cargo/commands/test.rs index 3d4dd5b2f02..bb39569eba2 100644 --- a/src/bin/cargo/commands/test.rs +++ b/src/bin/cargo/commands/test.rs @@ -55,6 +55,7 @@ pub fn cli() -> App { .arg_manifest_path() .arg_message_format() .arg_unit_graph() + .arg_rustc_cfg() .after_help("Run `cargo help test` for more detailed information.\n") } From e9e4593f40ec362c27d94a4f427a0567e3186ed3 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Wed, 16 Dec 2020 14:53:18 -0500 Subject: [PATCH 04/43] Change option name for subcommands The `--cfg` option is changed to `--rustc-cfg` for the build, bench, test, and check subcommands but left as `--cfg` for the rustc subcommand. The `--rustc-cfg` flag is more descriptive of configuration (cfg) that will be printed as JSON, but it looks weird if `--rustc-cfg` is used with the rustc subcommand, i.e.: ```pwsh PS C:\>cargo rustc --rustc-cfg ``` versus ``` PS C:\>cargo rustc --cfg ``` By using the rustc subcommad, the type of output and configuration is known, but wiht the other subcommands it is ambiguous what a lone `--cfg` would output. --- src/bin/cargo/commands/rustc.rs | 2 +- src/cargo/util/command_prelude.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bin/cargo/commands/rustc.rs b/src/bin/cargo/commands/rustc.rs index c801f0ffe9b..22c9fccb33a 100644 --- a/src/bin/cargo/commands/rustc.rs +++ b/src/bin/cargo/commands/rustc.rs @@ -30,7 +30,7 @@ pub fn cli() -> App { .arg_manifest_path() .arg_message_format() .arg_unit_graph() - .arg_rustc_cfg() + .arg(opt("cfg", "Output the compiler configuration in JSON (unstable)")) .after_help("Run `cargo help rustc` for more detailed information.\n") } diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 134da802286..d93af913f71 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -173,7 +173,7 @@ pub trait AppExt: Sized { } fn arg_rustc_cfg(self) -> Self { - self._arg(opt("cfg", "Output the ructc configuration to STDOUT").hidden(true)) + self._arg(opt("rustc-cfg", "Output the rustc configuration in JSON (unstable)")) } fn arg_new_opts(self) -> Self { From d662cc35f4132c726bec9e4db96010ee9041d11a Mon Sep 17 00:00:00 2001 From: Chris Field Date: Wed, 16 Dec 2020 14:59:42 -0500 Subject: [PATCH 05/43] Fix `--rustc-cfg` not working The name of the flag changed for some subcommands from `--cfg` to `--rustc-cfg` but the parsing of the arguments was not updated to account for the different names. --- src/cargo/util/command_prelude.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index d93af913f71..ee5ca052a61 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -473,7 +473,7 @@ pub trait ArgMatchesExt { build_config.requested_profile = self.get_profile_name(config, "dev", profile_checking)?; build_config.build_plan = self._is_present("build-plan"); build_config.unit_graph = self._is_present("unit-graph"); - build_config.cfg = self._is_present("cfg"); + build_config.cfg = self._is_present("cfg") || self._is_present("rustc-cfg"); if build_config.build_plan { config .cli_unstable() From 6562f6afce255ec74b342040e804251ed9508759 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Wed, 16 Dec 2020 15:08:53 -0500 Subject: [PATCH 06/43] Add `--cfg` or `--rustc-cfg` behind unstable flag Following the template of the `--unit-graph` option, the `--cfg` and `--rustc-cfg` flags are placed behind the `-Z unstable-options` flag. Now, if the `--cfg` or `--rustc-cfg` flags are used without the `-Z unstable-option` flag, an error message will appear. Once stablized, I believe this requirement can be removed. --- src/cargo/util/command_prelude.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index ee5ca052a61..a51067fb75e 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -484,6 +484,16 @@ pub trait ArgMatchesExt { .cli_unstable() .fail_if_stable_opt("--unit-graph", 8002)?; } + if self._is_present("cfg") { + config + .cli_unstable() + .fail_if_stable_opt("--cfg", 8923)?; + } + if self._is_present("rustc-cfg") { + config + .cli_unstable() + .fail_if_stable_opt("--rustc-cfg", 8923)?; + } let opts = CompileOptions { build_config, From d49a32fd8fc08ce0e0ff6cebcedba406672a9a49 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Wed, 16 Dec 2020 15:14:36 -0500 Subject: [PATCH 07/43] Change field name The `BuildConfig.cfg` is changed to `BuildConfig.rustc_cfg`. I am afraid of a name collision or confusion with just a `cfg` field. The `rustc_cfg` field indicates this is for printing the compiler (rustc) configuration from the `rustc --print cfg` like process, instead of some higher level or more generic "configuration" type. --- src/cargo/core/compiler/build_config.rs | 4 ++-- src/cargo/ops/cargo_compile.rs | 2 +- src/cargo/util/command_prelude.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs index 1eb427cbbe1..e4523c582ba 100644 --- a/src/cargo/core/compiler/build_config.rs +++ b/src/cargo/core/compiler/build_config.rs @@ -27,7 +27,7 @@ pub struct BuildConfig { /// Output the unit graph to stdout instead of actually compiling. pub unit_graph: bool, /// Output the rustc configuration to stdout instead of actually compiling. - pub cfg: bool, + pub rustc_cfg: bool, /// An optional override of the rustc process for primary units pub primary_unit_rustc: Option, /// A thread used by `cargo fix` to receive messages on a socket regarding @@ -79,7 +79,7 @@ impl BuildConfig { force_rebuild: false, build_plan: false, unit_graph: false, - cfg: false, + rustc_cfg: false, primary_unit_rustc: None, rustfix_diagnostic_server: RefCell::new(None), export_dir: None, diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 13d2abf62de..8f5f580bfc5 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -285,7 +285,7 @@ pub fn compile_ws<'a>( unit_graph::emit_serialized_unit_graph(&bcx.roots, &bcx.unit_graph)?; return Ok(Compilation::new(&bcx)?); } - if options.build_config.cfg { + if options.build_config.rustc_cfg { cfg::emit_serialized_rustc_cfg(&bcx.target_data, &bcx.build_config.requested_kinds)?; return Ok(Compilation::new(&bcx)?); } diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index a51067fb75e..50683ac55bd 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -473,7 +473,7 @@ pub trait ArgMatchesExt { build_config.requested_profile = self.get_profile_name(config, "dev", profile_checking)?; build_config.build_plan = self._is_present("build-plan"); build_config.unit_graph = self._is_present("unit-graph"); - build_config.cfg = self._is_present("cfg") || self._is_present("rustc-cfg"); + build_config.rustc_cfg = self._is_present("cfg") || self._is_present("rustc-cfg"); if build_config.build_plan { config .cli_unstable() From efdf6477ed86d9cdd88283eb11d135a974ac00f5 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Wed, 16 Dec 2020 15:17:30 -0500 Subject: [PATCH 08/43] Change module name Here again, the `cfg` name is too generic and could be confusing. It is changed to `rustc_cfg` in similar convention to the `unit_graph` module and to relate it to the `--rustc-cfg` flag. --- src/cargo/core/compiler/{cfg.rs => rustc_cfg.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/cargo/core/compiler/{cfg.rs => rustc_cfg.rs} (100%) diff --git a/src/cargo/core/compiler/cfg.rs b/src/cargo/core/compiler/rustc_cfg.rs similarity index 100% rename from src/cargo/core/compiler/cfg.rs rename to src/cargo/core/compiler/rustc_cfg.rs From 6789a4cc26909030af618eaadc389c2421be9989 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Wed, 16 Dec 2020 15:23:18 -0500 Subject: [PATCH 09/43] Fix compile-time errors from module rename --- src/cargo/core/compiler/mod.rs | 2 +- src/cargo/core/compiler/rustc_cfg.rs | 15 +++++++++++++++ src/cargo/ops/cargo_compile.rs | 4 ++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 41f824eefc4..5f3bd7a1412 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -19,7 +19,7 @@ mod timings; mod unit; pub mod unit_dependencies; pub mod unit_graph; -pub mod cfg; +pub mod rustc_cfg; use std::env; use std::ffi::{OsStr, OsString}; diff --git a/src/cargo/core/compiler/rustc_cfg.rs b/src/cargo/core/compiler/rustc_cfg.rs index 2a406cb246a..88dbf0335c1 100644 --- a/src/cargo/core/compiler/rustc_cfg.rs +++ b/src/cargo/core/compiler/rustc_cfg.rs @@ -28,6 +28,21 @@ const VERSION: u32 = 1; // ] // } +// { +// "version": 1, +// "host": { +// "names": ["windows", "debug_assertions"], +// "arch":"x86_64", +// "endian":"little", +// "env":"msvc", +// "family":"windows", +// "features":["fxsr","sse","sse2"], +// "os":"windows", +// "pointer_width":"64", +// "vendor":"pc" +// } +// } + #[derive(serde::Serialize)] struct SerializedRustcCfg<'a> { version: u32, diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 8f5f580bfc5..1bb12124954 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -29,7 +29,7 @@ use std::sync::Arc; use crate::core::compiler::standard_lib; use crate::core::compiler::unit_dependencies::build_unit_dependencies; use crate::core::compiler::unit_graph::{self, UnitDep, UnitGraph}; -use crate::core::compiler::cfg; +use crate::core::compiler::rustc_cfg; use crate::core::compiler::{BuildConfig, BuildContext, Compilation, Context}; use crate::core::compiler::{CompileKind, CompileMode, CompileTarget, RustcTargetData, Unit}; use crate::core::compiler::{DefaultExecutor, Executor, UnitInterner}; @@ -286,7 +286,7 @@ pub fn compile_ws<'a>( return Ok(Compilation::new(&bcx)?); } if options.build_config.rustc_cfg { - cfg::emit_serialized_rustc_cfg(&bcx.target_data, &bcx.build_config.requested_kinds)?; + rustc_cfg::emit_serialized_rustc_cfg(&bcx.target_data, &bcx.build_config.requested_kinds)?; return Ok(Compilation::new(&bcx)?); } let _p = profile::start("compiling"); From 234089ffbe73e666e1985c93f11d64c4db7ffd1a Mon Sep 17 00:00:00 2001 From: Chris Field Date: Wed, 16 Dec 2020 15:55:48 -0500 Subject: [PATCH 10/43] Change JSON format The original format was quick-n-dirty and more like a "raw dump" of the compiler configurations. The quick-n-dirty format would probably work just find for 99% of the use cases but it was kind of noisy and verbose. The target features were not great. The new format is less noisy (no "target_" everywhere) and provides an array of the target features. It is just more clean and elegant. Note, there is no implementation or CLI argument to select a JSON format version. I am not sure such an implementation is even needed. --- src/cargo/core/compiler/rustc_cfg.rs | 99 +++++++++++++++++----------- 1 file changed, 62 insertions(+), 37 deletions(-) diff --git a/src/cargo/core/compiler/rustc_cfg.rs b/src/cargo/core/compiler/rustc_cfg.rs index 88dbf0335c1..dda217ef367 100644 --- a/src/cargo/core/compiler/rustc_cfg.rs +++ b/src/cargo/core/compiler/rustc_cfg.rs @@ -8,26 +8,6 @@ use std::io::Write; const VERSION: u32 = 1; -// { -// "version": 1, -// "host": { -// "names": ["windows", "debug_assertions"], -// "key_pairs": [ -// {"target_os": "windows"}, -// {"target_vendor": "pc"} -// ] -// }, -// "targets": [ -// "x86_64-pc-windows-msvc": { -// "names": ["windows", "debug_assertions"], -// "key_pairs": [ -// {"target_os": "windows"}, -// {"target_vendor": "pc"} -// ] -// } -// ] -// } - // { // "version": 1, // "host": { @@ -40,7 +20,20 @@ const VERSION: u32 = 1; // "os":"windows", // "pointer_width":"64", // "vendor":"pc" -// } +// }, +// "targets": [ +// "x86_64-unknown-linux-gnu": { +// "names": ["debug_assertions", "unix"], +// "arch":"x86_64", +// "endian":"little", +// "env":"gnu", +// "family":"unix", +// "features": ["fxsr","sse","sse2"], +// "os":"linux", +// "pointer_width":"64", +// "vendor":"unknown" +// } +// ] // } #[derive(serde::Serialize)] @@ -52,31 +45,63 @@ struct SerializedRustcCfg<'a> { #[derive(serde::Serialize)] struct SerializedCfg<'a> { + arch: Option<&'a str>, + endian: Option<&'a str>, + env: Option<&'a str>, + family: Option<&'a str>, + features: Vec<&'a str>, names: Vec<&'a str>, - key_pairs: Vec>, + os: Option<&'a str>, + pointer_width: Option<&'a str>, + vendor: Option<&'a str>, } impl<'a> SerializedCfg<'a> { fn new(rtd: &'a RustcTargetData, kind: CompileKind) -> Self { - Self { - names: rtd.cfg(kind).iter().filter_map(|c| { - match c { - Cfg::Name(s) => Some(s.as_str()), - Cfg::KeyPair(..) => None, - } - }).collect(), - key_pairs: rtd.cfg(kind).iter().filter_map(|c| { - match c { - Cfg::Name(..) => None, - Cfg::KeyPair(k, v) => { - let mut pair = HashMap::with_capacity(1); - pair.insert(k.as_str(), v.as_str()); - Some(pair) + let features = rtd.cfg(kind).iter().filter_map(|c| { + match c { + Cfg::Name(..) => None, + Cfg::KeyPair(k, v) => { + if k == "target_feature" { + Some(v.as_str()) + } else { + None } } - }).collect() + } + }).collect(); + let names = rtd.cfg(kind).iter().filter_map(|c| { + match c { + Cfg::Name(s) => Some(s.as_str()), + Cfg::KeyPair(..) => None, + } + }).collect(); + Self { + arch: Self::find(rtd, kind, "target_arch"), + endian: Self::find(rtd, kind, "target_endian"), + env: Self::find(rtd, kind, "target_env"), + family: Self::find(rtd, kind, "target_family"), + features, + names, + os: Self::find(rtd, kind, "target_os"), + pointer_width: Self::find(rtd, kind, "target_pointer_width"), + vendor: Self::find(rtd, kind, "target_vendor"), } } + + fn find(rtd: &'a RustcTargetData, kind: CompileKind, key: &str) -> Option<&'a str> { + rtd.cfg(kind).iter().find_map(|c| { + match c { + Cfg::Name(..) => None, + Cfg::KeyPair(k, v) => + if k == key { + Some(v.as_str()) + } else { + None + } + } + }) + } } pub fn emit_serialized_rustc_cfg(rtd: &RustcTargetData, kinds: &[CompileKind]) -> CargoResult<()> { From 3d0fcbefebe74165277500c20a8866ec8b7c5df0 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Wed, 16 Dec 2020 16:33:40 -0500 Subject: [PATCH 11/43] Add better example JSON to internal comment --- src/cargo/core/compiler/rustc_cfg.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/cargo/core/compiler/rustc_cfg.rs b/src/cargo/core/compiler/rustc_cfg.rs index dda217ef367..78208ce39a3 100644 --- a/src/cargo/core/compiler/rustc_cfg.rs +++ b/src/cargo/core/compiler/rustc_cfg.rs @@ -21,7 +21,7 @@ const VERSION: u32 = 1; // "pointer_width":"64", // "vendor":"pc" // }, -// "targets": [ +// "targets": { // "x86_64-unknown-linux-gnu": { // "names": ["debug_assertions", "unix"], // "arch":"x86_64", @@ -32,8 +32,19 @@ const VERSION: u32 = 1; // "os":"linux", // "pointer_width":"64", // "vendor":"unknown" +// }, +// "i686-pc-windows-msvc": { +// "names": ["windows", "debug_assertions"], +// "arch":"x86", +// "endian":"little", +// "env":"msvc", +// "family":"windows", +// "features":["fxsr","sse","sse2"], +// "os":"windows", +// "pointer_width":"32", +// "vendor":"pc" // } -// ] +// } // } #[derive(serde::Serialize)] From af3a843c5e715637ee4fd01ab173244afd1144d9 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Sat, 19 Dec 2020 11:33:34 -0500 Subject: [PATCH 12/43] Change the `--cfg` and `--rustc-cfg` to hidden Similar to the `--unit-graph` flag, which is also hidden, the `--cfg` and `--rustc-cfg` are hidden. I think they will be made visible once the feature is stablized. --- src/bin/cargo/commands/rustc.rs | 2 +- src/cargo/util/command_prelude.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bin/cargo/commands/rustc.rs b/src/bin/cargo/commands/rustc.rs index 22c9fccb33a..538261bb207 100644 --- a/src/bin/cargo/commands/rustc.rs +++ b/src/bin/cargo/commands/rustc.rs @@ -30,7 +30,7 @@ pub fn cli() -> App { .arg_manifest_path() .arg_message_format() .arg_unit_graph() - .arg(opt("cfg", "Output the compiler configuration in JSON (unstable)")) + .arg(opt("cfg", "Output the compiler configuration in JSON (unstable)").hidden(true)) .after_help("Run `cargo help rustc` for more detailed information.\n") } diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 50683ac55bd..447901128f5 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -173,7 +173,7 @@ pub trait AppExt: Sized { } fn arg_rustc_cfg(self) -> Self { - self._arg(opt("rustc-cfg", "Output the rustc configuration in JSON (unstable)")) + self._arg(opt("rustc-cfg", "Output the rustc configuration in JSON (unstable)").hidden(true)) } fn arg_new_opts(self) -> Self { From 9be620a770e389f92db78acd6143871515724c49 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Sat, 19 Dec 2020 13:08:38 -0500 Subject: [PATCH 13/43] Fix formatting --- src/bin/cargo/commands/rustc.rs | 8 +- src/cargo/core/compiler/mod.rs | 2 +- src/cargo/core/compiler/rustc_cfg.rs | 276 ++++++++++++++------------- src/cargo/ops/cargo_compile.rs | 2 +- src/cargo/util/command_prelude.rs | 12 +- 5 files changed, 157 insertions(+), 143 deletions(-) diff --git a/src/bin/cargo/commands/rustc.rs b/src/bin/cargo/commands/rustc.rs index 538261bb207..08d38e11ddf 100644 --- a/src/bin/cargo/commands/rustc.rs +++ b/src/bin/cargo/commands/rustc.rs @@ -30,7 +30,13 @@ pub fn cli() -> App { .arg_manifest_path() .arg_message_format() .arg_unit_graph() - .arg(opt("cfg", "Output the compiler configuration in JSON (unstable)").hidden(true)) + .arg( + opt( + "cfg", + "Output the compiler configuration in JSON (unstable)", + ) + .hidden(true), + ) .after_help("Run `cargo help rustc` for more detailed information.\n") } diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 71a2c0876d9..bb6a8b26968 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -13,13 +13,13 @@ mod layout; mod links; mod lto; mod output_depinfo; +pub mod rustc_cfg; pub mod rustdoc; pub mod standard_lib; mod timings; mod unit; pub mod unit_dependencies; pub mod unit_graph; -pub mod rustc_cfg; use std::env; use std::ffi::{OsStr, OsString}; diff --git a/src/cargo/core/compiler/rustc_cfg.rs b/src/cargo/core/compiler/rustc_cfg.rs index 78208ce39a3..f4459785fe7 100644 --- a/src/cargo/core/compiler/rustc_cfg.rs +++ b/src/cargo/core/compiler/rustc_cfg.rs @@ -1,136 +1,140 @@ -use cargo_platform::Cfg; - -use crate::core::compiler::{CompileKind, RustcTargetData}; -use crate::util::CargoResult; - -use std::collections::HashMap; -use std::io::Write; - -const VERSION: u32 = 1; - -// { -// "version": 1, -// "host": { -// "names": ["windows", "debug_assertions"], -// "arch":"x86_64", -// "endian":"little", -// "env":"msvc", -// "family":"windows", -// "features":["fxsr","sse","sse2"], -// "os":"windows", -// "pointer_width":"64", -// "vendor":"pc" -// }, -// "targets": { -// "x86_64-unknown-linux-gnu": { -// "names": ["debug_assertions", "unix"], -// "arch":"x86_64", -// "endian":"little", -// "env":"gnu", -// "family":"unix", -// "features": ["fxsr","sse","sse2"], -// "os":"linux", -// "pointer_width":"64", -// "vendor":"unknown" -// }, -// "i686-pc-windows-msvc": { -// "names": ["windows", "debug_assertions"], -// "arch":"x86", -// "endian":"little", -// "env":"msvc", -// "family":"windows", -// "features":["fxsr","sse","sse2"], -// "os":"windows", -// "pointer_width":"32", -// "vendor":"pc" -// } -// } -// } - -#[derive(serde::Serialize)] -struct SerializedRustcCfg<'a> { - version: u32, - host: SerializedCfg<'a>, - targets: HashMap<&'a str, SerializedCfg<'a>>, -} - -#[derive(serde::Serialize)] -struct SerializedCfg<'a> { - arch: Option<&'a str>, - endian: Option<&'a str>, - env: Option<&'a str>, - family: Option<&'a str>, - features: Vec<&'a str>, - names: Vec<&'a str>, - os: Option<&'a str>, - pointer_width: Option<&'a str>, - vendor: Option<&'a str>, -} - -impl<'a> SerializedCfg<'a> { - fn new(rtd: &'a RustcTargetData, kind: CompileKind) -> Self { - let features = rtd.cfg(kind).iter().filter_map(|c| { - match c { - Cfg::Name(..) => None, - Cfg::KeyPair(k, v) => { - if k == "target_feature" { - Some(v.as_str()) - } else { - None - } - } - } - }).collect(); - let names = rtd.cfg(kind).iter().filter_map(|c| { - match c { - Cfg::Name(s) => Some(s.as_str()), - Cfg::KeyPair(..) => None, - } - }).collect(); - Self { - arch: Self::find(rtd, kind, "target_arch"), - endian: Self::find(rtd, kind, "target_endian"), - env: Self::find(rtd, kind, "target_env"), - family: Self::find(rtd, kind, "target_family"), - features, - names, - os: Self::find(rtd, kind, "target_os"), - pointer_width: Self::find(rtd, kind, "target_pointer_width"), - vendor: Self::find(rtd, kind, "target_vendor"), - } - } - - fn find(rtd: &'a RustcTargetData, kind: CompileKind, key: &str) -> Option<&'a str> { - rtd.cfg(kind).iter().find_map(|c| { - match c { - Cfg::Name(..) => None, - Cfg::KeyPair(k, v) => - if k == key { - Some(v.as_str()) - } else { - None - } - } - }) - } -} - -pub fn emit_serialized_rustc_cfg(rtd: &RustcTargetData, kinds: &[CompileKind]) -> CargoResult<()> { - let host = SerializedCfg::new(rtd, CompileKind::Host); - let targets = kinds.iter().filter_map(|k| { - match k { - CompileKind::Host => None, - CompileKind::Target(ct) => Some((ct.short_name(), SerializedCfg::new(rtd, *k))), - } - }).collect(); - let s = SerializedRustcCfg { - version: VERSION, - host, - targets - }; - let stdout = std::io::stdout(); - let mut lock = stdout.lock(); - serde_json::to_writer(&mut lock, &s)?; - drop(writeln!(lock)); - Ok(()) -} +use cargo_platform::Cfg; + +use crate::core::compiler::{CompileKind, RustcTargetData}; +use crate::util::CargoResult; + +use std::collections::HashMap; +use std::io::Write; + +const VERSION: u32 = 1; + +// { +// "version": 1, +// "host": { +// "names": ["windows", "debug_assertions"], +// "arch":"x86_64", +// "endian":"little", +// "env":"msvc", +// "family":"windows", +// "features":["fxsr","sse","sse2"], +// "os":"windows", +// "pointer_width":"64", +// "vendor":"pc" +// }, +// "targets": { +// "x86_64-unknown-linux-gnu": { +// "names": ["debug_assertions", "unix"], +// "arch":"x86_64", +// "endian":"little", +// "env":"gnu", +// "family":"unix", +// "features": ["fxsr","sse","sse2"], +// "os":"linux", +// "pointer_width":"64", +// "vendor":"unknown" +// }, +// "i686-pc-windows-msvc": { +// "names": ["windows", "debug_assertions"], +// "arch":"x86", +// "endian":"little", +// "env":"msvc", +// "family":"windows", +// "features":["fxsr","sse","sse2"], +// "os":"windows", +// "pointer_width":"32", +// "vendor":"pc" +// } +// } +// } + +#[derive(serde::Serialize)] +struct SerializedRustcCfg<'a> { + version: u32, + host: SerializedCfg<'a>, + targets: HashMap<&'a str, SerializedCfg<'a>>, +} + +#[derive(serde::Serialize)] +struct SerializedCfg<'a> { + arch: Option<&'a str>, + endian: Option<&'a str>, + env: Option<&'a str>, + family: Option<&'a str>, + features: Vec<&'a str>, + names: Vec<&'a str>, + os: Option<&'a str>, + pointer_width: Option<&'a str>, + vendor: Option<&'a str>, +} + +impl<'a> SerializedCfg<'a> { + fn new(rtd: &'a RustcTargetData, kind: CompileKind) -> Self { + let features = rtd + .cfg(kind) + .iter() + .filter_map(|c| match c { + Cfg::Name(..) => None, + Cfg::KeyPair(k, v) => { + if k == "target_feature" { + Some(v.as_str()) + } else { + None + } + } + }) + .collect(); + let names = rtd + .cfg(kind) + .iter() + .filter_map(|c| match c { + Cfg::Name(s) => Some(s.as_str()), + Cfg::KeyPair(..) => None, + }) + .collect(); + Self { + arch: Self::find(rtd, kind, "target_arch"), + endian: Self::find(rtd, kind, "target_endian"), + env: Self::find(rtd, kind, "target_env"), + family: Self::find(rtd, kind, "target_family"), + features, + names, + os: Self::find(rtd, kind, "target_os"), + pointer_width: Self::find(rtd, kind, "target_pointer_width"), + vendor: Self::find(rtd, kind, "target_vendor"), + } + } + + fn find(rtd: &'a RustcTargetData, kind: CompileKind, key: &str) -> Option<&'a str> { + rtd.cfg(kind).iter().find_map(|c| match c { + Cfg::Name(..) => None, + Cfg::KeyPair(k, v) => { + if k == key { + Some(v.as_str()) + } else { + None + } + } + }) + } +} + +pub fn emit_serialized_rustc_cfg(rtd: &RustcTargetData, kinds: &[CompileKind]) -> CargoResult<()> { + let host = SerializedCfg::new(rtd, CompileKind::Host); + let targets = kinds + .iter() + .filter_map(|k| match k { + CompileKind::Host => None, + CompileKind::Target(ct) => Some((ct.short_name(), SerializedCfg::new(rtd, *k))), + }) + .collect(); + let s = SerializedRustcCfg { + version: VERSION, + host, + targets, + }; + let stdout = std::io::stdout(); + let mut lock = stdout.lock(); + serde_json::to_writer(&mut lock, &s)?; + drop(writeln!(lock)); + Ok(()) +} diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 42908ae1473..a921521c689 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -26,10 +26,10 @@ use std::collections::{BTreeSet, HashMap, HashSet}; use std::hash::{Hash, Hasher}; use std::sync::Arc; +use crate::core::compiler::rustc_cfg; use crate::core::compiler::standard_lib; use crate::core::compiler::unit_dependencies::build_unit_dependencies; use crate::core::compiler::unit_graph::{self, UnitDep, UnitGraph}; -use crate::core::compiler::rustc_cfg; use crate::core::compiler::{BuildConfig, BuildContext, Compilation, Context}; use crate::core::compiler::{CompileKind, CompileMode, CompileTarget, RustcTargetData, Unit}; use crate::core::compiler::{DefaultExecutor, Executor, UnitInterner}; diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index 447901128f5..f2f20e51e16 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -173,7 +173,13 @@ pub trait AppExt: Sized { } fn arg_rustc_cfg(self) -> Self { - self._arg(opt("rustc-cfg", "Output the rustc configuration in JSON (unstable)").hidden(true)) + self._arg( + opt( + "rustc-cfg", + "Output the rustc configuration in JSON (unstable)", + ) + .hidden(true), + ) } fn arg_new_opts(self) -> Self { @@ -485,9 +491,7 @@ pub trait ArgMatchesExt { .fail_if_stable_opt("--unit-graph", 8002)?; } if self._is_present("cfg") { - config - .cli_unstable() - .fail_if_stable_opt("--cfg", 8923)?; + config.cli_unstable().fail_if_stable_opt("--cfg", 8923)?; } if self._is_present("rustc-cfg") { config From 105a8bdefd7fc2b1922cfb303bfd36787cbe4f09 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Fri, 8 Jan 2021 12:30:48 -0500 Subject: [PATCH 14/43] Remove `--rustc-cfg` argument Only the `cargo rustc --cfg` argument remains. Having the `--rustc-cfg` flag for all other subcommands as too much and deemed not really necessary or useful. --- src/bin/cargo/commands/bench.rs | 1 - src/bin/cargo/commands/build.rs | 1 - src/bin/cargo/commands/check.rs | 1 - src/bin/cargo/commands/test.rs | 1 - src/cargo/util/command_prelude.rs | 17 +---------------- 5 files changed, 1 insertion(+), 20 deletions(-) diff --git a/src/bin/cargo/commands/bench.rs b/src/bin/cargo/commands/bench.rs index 28f404b0196..700dca258bf 100644 --- a/src/bin/cargo/commands/bench.rs +++ b/src/bin/cargo/commands/bench.rs @@ -45,7 +45,6 @@ pub fn cli() -> App { "Run all benchmarks regardless of failure", )) .arg_unit_graph() - .arg_rustc_cfg() .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 5fd61f5e7a4..e165b250d69 100644 --- a/src/bin/cargo/commands/build.rs +++ b/src/bin/cargo/commands/build.rs @@ -42,7 +42,6 @@ pub fn cli() -> App { .arg_message_format() .arg_build_plan() .arg_unit_graph() - .arg_rustc_cfg() .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 05750641dbd..6d26ef2d765 100644 --- a/src/bin/cargo/commands/check.rs +++ b/src/bin/cargo/commands/check.rs @@ -34,7 +34,6 @@ pub fn cli() -> App { .arg_manifest_path() .arg_message_format() .arg_unit_graph() - .arg_rustc_cfg() .after_help("Run `cargo help check` for more detailed information.\n") } diff --git a/src/bin/cargo/commands/test.rs b/src/bin/cargo/commands/test.rs index bb39569eba2..3d4dd5b2f02 100644 --- a/src/bin/cargo/commands/test.rs +++ b/src/bin/cargo/commands/test.rs @@ -55,7 +55,6 @@ pub fn cli() -> App { .arg_manifest_path() .arg_message_format() .arg_unit_graph() - .arg_rustc_cfg() .after_help("Run `cargo help test` for more detailed information.\n") } diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index f2f20e51e16..a2c2dae0998 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -172,16 +172,6 @@ pub trait AppExt: Sized { self._arg(opt("unit-graph", "Output build graph in JSON (unstable)").hidden(true)) } - fn arg_rustc_cfg(self) -> Self { - self._arg( - opt( - "rustc-cfg", - "Output the rustc configuration in JSON (unstable)", - ) - .hidden(true), - ) - } - fn arg_new_opts(self) -> Self { self._arg( opt( @@ -479,7 +469,7 @@ pub trait ArgMatchesExt { build_config.requested_profile = self.get_profile_name(config, "dev", profile_checking)?; build_config.build_plan = self._is_present("build-plan"); build_config.unit_graph = self._is_present("unit-graph"); - build_config.rustc_cfg = self._is_present("cfg") || self._is_present("rustc-cfg"); + build_config.rustc_cfg = self._is_present("cfg"); if build_config.build_plan { config .cli_unstable() @@ -493,11 +483,6 @@ pub trait ArgMatchesExt { if self._is_present("cfg") { config.cli_unstable().fail_if_stable_opt("--cfg", 8923)?; } - if self._is_present("rustc-cfg") { - config - .cli_unstable() - .fail_if_stable_opt("--rustc-cfg", 8923)?; - } let opts = CompileOptions { build_config, From e078a6c9ce92c6ca5387ce801324a7447ea8ddce Mon Sep 17 00:00:00 2001 From: Chris Field Date: Fri, 8 Jan 2021 12:46:16 -0500 Subject: [PATCH 15/43] Change to lighter weight `--print-cfg` The `--cfg` flag is changed to `--print-cfg`, so it is a little more obvious what the flag will do. At the same time, the implementation is changed to only affect the `cargo rustc` subcommand. It skips downloading the entire crate graph and compiling/building the project. Instead, it just obtains the rustc target data similar to the `ops::compile` function and then returns. The `ops::print_cfg` function is added, but this could also be moved to just the `cargo rustc` subcommand. --- src/bin/cargo/commands/rustc.rs | 9 +++++++-- src/cargo/core/compiler/build_config.rs | 2 -- src/cargo/ops/cargo_compile.rs | 9 +++++++++ src/cargo/util/command_prelude.rs | 5 ----- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/bin/cargo/commands/rustc.rs b/src/bin/cargo/commands/rustc.rs index 08d38e11ddf..28d1d003f1d 100644 --- a/src/bin/cargo/commands/rustc.rs +++ b/src/bin/cargo/commands/rustc.rs @@ -32,7 +32,7 @@ pub fn cli() -> App { .arg_unit_graph() .arg( opt( - "cfg", + "--print-cfg", "Output the compiler configuration in JSON (unstable)", ) .hidden(true), @@ -68,6 +68,11 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { } else { Some(target_args) }; - ops::compile(&ws, &compile_opts)?; + if args.is_present("--print-cfg") { + config.cli_unstable().fail_if_stable_opt("--print-cfg", 8923)?; + ops::print_cfg(&ws, &comile_opts)?; + } else { + ops::compile(&ws, &compile_opts)?; + } Ok(()) } diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs index e4523c582ba..12c2d13e663 100644 --- a/src/cargo/core/compiler/build_config.rs +++ b/src/cargo/core/compiler/build_config.rs @@ -26,8 +26,6 @@ pub struct BuildConfig { pub build_plan: bool, /// Output the unit graph to stdout instead of actually compiling. pub unit_graph: bool, - /// Output the rustc configuration to stdout instead of actually compiling. - pub rustc_cfg: bool, /// An optional override of the rustc process for primary units pub primary_unit_rustc: Option, /// A thread used by `cargo fix` to receive messages on a socket regarding diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index a921521c689..f1f272835fd 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -294,6 +294,15 @@ pub fn compile_ws<'a>( cx.compile(exec) } +pub fn print_cfg<'a>( + ws: &Workspace<'a>, + options: &CompileOptions, +) -> CargoResult<()> { + let target_data = RustcTargetData::new(ws, &options.build_config.requested_kinds)?; + rustc_cfg::emit_serialized_rustc_cfg(&target_data, &options.build_config.requested_kinds)?; + Ok(()) +} + pub fn create_bcx<'a, 'cfg>( ws: &'a Workspace<'cfg>, options: &'a CompileOptions, diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index a2c2dae0998..2ea5fc5368c 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -469,7 +469,6 @@ pub trait ArgMatchesExt { build_config.requested_profile = self.get_profile_name(config, "dev", profile_checking)?; build_config.build_plan = self._is_present("build-plan"); build_config.unit_graph = self._is_present("unit-graph"); - build_config.rustc_cfg = self._is_present("cfg"); if build_config.build_plan { config .cli_unstable() @@ -480,10 +479,6 @@ pub trait ArgMatchesExt { .cli_unstable() .fail_if_stable_opt("--unit-graph", 8002)?; } - if self._is_present("cfg") { - config.cli_unstable().fail_if_stable_opt("--cfg", 8923)?; - } - let opts = CompileOptions { build_config, features: self._values_of("features"), From 7f1c268ab7287cad14aa028c5cbba62a508d023c Mon Sep 17 00:00:00 2001 From: Chris Field Date: Fri, 8 Jan 2021 12:50:02 -0500 Subject: [PATCH 16/43] Fix typo --- src/bin/cargo/commands/rustc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/cargo/commands/rustc.rs b/src/bin/cargo/commands/rustc.rs index 28d1d003f1d..7e8f6c10835 100644 --- a/src/bin/cargo/commands/rustc.rs +++ b/src/bin/cargo/commands/rustc.rs @@ -70,7 +70,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { }; if args.is_present("--print-cfg") { config.cli_unstable().fail_if_stable_opt("--print-cfg", 8923)?; - ops::print_cfg(&ws, &comile_opts)?; + ops::print_cfg(&ws, &compile_opts)?; } else { ops::compile(&ws, &compile_opts)?; } From fc83cbedbb3bcd56a559ecfc5652406f22c7fb4c Mon Sep 17 00:00:00 2001 From: Chris Field Date: Fri, 8 Jan 2021 12:53:01 -0500 Subject: [PATCH 17/43] Refactor to use constant for arg name A static constant is used to define the arg name since this may change after review but the string is used as an ID in processing arguments. --- src/bin/cargo/commands/rustc.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/bin/cargo/commands/rustc.rs b/src/bin/cargo/commands/rustc.rs index 7e8f6c10835..2060fb0affc 100644 --- a/src/bin/cargo/commands/rustc.rs +++ b/src/bin/cargo/commands/rustc.rs @@ -2,6 +2,8 @@ use crate::command_prelude::*; use cargo::ops; +const PRINT_CFG_ARG_NAME: &str = "print-cfg"; + pub fn cli() -> App { subcommand("rustc") .setting(AppSettings::TrailingVarArg) @@ -32,7 +34,7 @@ pub fn cli() -> App { .arg_unit_graph() .arg( opt( - "--print-cfg", + PRINT_CFG_ARG_NAME, "Output the compiler configuration in JSON (unstable)", ) .hidden(true), @@ -68,8 +70,8 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { } else { Some(target_args) }; - if args.is_present("--print-cfg") { - config.cli_unstable().fail_if_stable_opt("--print-cfg", 8923)?; + if args.is_present(PRINT_CFG_ARG_NAME) { + config.cli_unstable().fail_if_stable_opt(PRINT_CFG_ARG_NAME, 8923)?; ops::print_cfg(&ws, &compile_opts)?; } else { ops::compile(&ws, &compile_opts)?; From 97a821d4f2beb8b3e36e29fa5d2add34e843598a Mon Sep 17 00:00:00 2001 From: Chris Field Date: Fri, 8 Jan 2021 13:28:17 -0500 Subject: [PATCH 18/43] Change JSON format The JSON format does not parse or group or drop prefixes for configurations. For each compiler target (host, etc.), the configurations are just strings in an array. The targets is changed to an array of maps as well. --- src/cargo/core/compiler/rustc_cfg.rs | 155 +++++++++++---------------- 1 file changed, 64 insertions(+), 91 deletions(-) diff --git a/src/cargo/core/compiler/rustc_cfg.rs b/src/cargo/core/compiler/rustc_cfg.rs index f4459785fe7..30a2561af1d 100644 --- a/src/cargo/core/compiler/rustc_cfg.rs +++ b/src/cargo/core/compiler/rustc_cfg.rs @@ -10,39 +10,52 @@ const VERSION: u32 = 1; // { // "version": 1, -// "host": { -// "names": ["windows", "debug_assertions"], -// "arch":"x86_64", -// "endian":"little", -// "env":"msvc", -// "family":"windows", -// "features":["fxsr","sse","sse2"], -// "os":"windows", -// "pointer_width":"64", -// "vendor":"pc" -// }, -// "targets": { -// "x86_64-unknown-linux-gnu": { -// "names": ["debug_assertions", "unix"], -// "arch":"x86_64", -// "endian":"little", -// "env":"gnu", -// "family":"unix", -// "features": ["fxsr","sse","sse2"], -// "os":"linux", -// "pointer_width":"64", -// "vendor":"unknown" +// "host": [ +// "windows", +// "debug_assertions", +// "target_arch='x86_64'", +// "target_endian='little'", +// "target_env='msvc'", +// "target_family='windows'", +// "target_feature='fxsr'", +// "target_feature='sse'", +// "target_feature='sse2'", +// "target_os='windows'", +// "target_pointer_width='64'", +// "target_vendor='pc'" +// ], +// "targets": [ +// { +// "x86_64-unknown-linux-gnu": [ +// "unix", +// "debug_assertions", +// "target_arch='x86_64'", +// "target_endian='little'", +// "target_env='gnu'", +// "target_family='unix'", +// "target_feature='fxsr'", +// "target_feature='sse'", +// "target_feature='sse2'", +// "target_os='linux'", +// "target_pointer_width='64'", +// "target_vendor='unknown'" +// ] // }, -// "i686-pc-windows-msvc": { -// "names": ["windows", "debug_assertions"], -// "arch":"x86", -// "endian":"little", -// "env":"msvc", -// "family":"windows", -// "features":["fxsr","sse","sse2"], -// "os":"windows", -// "pointer_width":"32", -// "vendor":"pc" +// { +// "i686-pc-windows-msvc": [ +// "windows", +// "debug_assertions", +// "target_arch='x86'", +// "target_endian='little'", +// "target_env='msvc'", +// "target_family='windows'", +// "target_feature='fxsr'", +// "target_feature='sse'", +// "target_feature='sse2'", +// "target_os='windows'", +// "target_pointer_width='32'", +// "target_vendor='pc'" +// ] // } // } // } @@ -50,81 +63,41 @@ const VERSION: u32 = 1; #[derive(serde::Serialize)] struct SerializedRustcCfg<'a> { version: u32, - host: SerializedCfg<'a>, - targets: HashMap<&'a str, SerializedCfg<'a>>, + host: SerializedTargetData<'a>, + targets: Vec>>, } #[derive(serde::Serialize)] -struct SerializedCfg<'a> { - arch: Option<&'a str>, - endian: Option<&'a str>, - env: Option<&'a str>, - family: Option<&'a str>, - features: Vec<&'a str>, - names: Vec<&'a str>, - os: Option<&'a str>, - pointer_width: Option<&'a str>, - vendor: Option<&'a str>, +struct SerializedTargetData<'a> { + cfgs: Vec<&'a str>, } -impl<'a> SerializedCfg<'a> { +impl<'a> SerializedTargetData<'a> { fn new(rtd: &'a RustcTargetData, kind: CompileKind) -> Self { - let features = rtd - .cfg(kind) - .iter() - .filter_map(|c| match c { - Cfg::Name(..) => None, - Cfg::KeyPair(k, v) => { - if k == "target_feature" { - Some(v.as_str()) - } else { - None - } - } - }) - .collect(); - let names = rtd - .cfg(kind) - .iter() - .filter_map(|c| match c { - Cfg::Name(s) => Some(s.as_str()), - Cfg::KeyPair(..) => None, - }) - .collect(); Self { - arch: Self::find(rtd, kind, "target_arch"), - endian: Self::find(rtd, kind, "target_endian"), - env: Self::find(rtd, kind, "target_env"), - family: Self::find(rtd, kind, "target_family"), - features, - names, - os: Self::find(rtd, kind, "target_os"), - pointer_width: Self::find(rtd, kind, "target_pointer_width"), - vendor: Self::find(rtd, kind, "target_vendor"), + cfgs: rtd + .cfg(kind) + .iter() + .map(|c| match c { + Cfg::Name(n) => n.as_str(), + Cfg::KeyPair(k, v) => format!("{}='{}'", k, v).as_str() + }) + .collect() } } - - fn find(rtd: &'a RustcTargetData, kind: CompileKind, key: &str) -> Option<&'a str> { - rtd.cfg(kind).iter().find_map(|c| match c { - Cfg::Name(..) => None, - Cfg::KeyPair(k, v) => { - if k == key { - Some(v.as_str()) - } else { - None - } - } - }) - } } pub fn emit_serialized_rustc_cfg(rtd: &RustcTargetData, kinds: &[CompileKind]) -> CargoResult<()> { - let host = SerializedCfg::new(rtd, CompileKind::Host); + let host = SerializedTargetData::new(rtd, CompileKind::Host); let targets = kinds .iter() .filter_map(|k| match k { CompileKind::Host => None, - CompileKind::Target(ct) => Some((ct.short_name(), SerializedCfg::new(rtd, *k))), + CompileKind::Target(ct) => { + let mut target = HashMap::with_capacity(1); + target.insert(ct.short_name(), SerializedTargetData::new(rtd, *k)); + Some(target) + }, }) .collect(); let s = SerializedRustcCfg { From 698fe7083338d67981338fae910b37cf8293ef0b Mon Sep 17 00:00:00 2001 From: Chris Field Date: Fri, 8 Jan 2021 13:31:19 -0500 Subject: [PATCH 19/43] Fix formatting --- src/bin/cargo/commands/rustc.rs | 4 +++- src/cargo/core/compiler/rustc_cfg.rs | 6 +++--- src/cargo/ops/cargo_compile.rs | 5 +---- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/bin/cargo/commands/rustc.rs b/src/bin/cargo/commands/rustc.rs index 2060fb0affc..d6ee205e7a8 100644 --- a/src/bin/cargo/commands/rustc.rs +++ b/src/bin/cargo/commands/rustc.rs @@ -71,7 +71,9 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { Some(target_args) }; if args.is_present(PRINT_CFG_ARG_NAME) { - config.cli_unstable().fail_if_stable_opt(PRINT_CFG_ARG_NAME, 8923)?; + config + .cli_unstable() + .fail_if_stable_opt(PRINT_CFG_ARG_NAME, 8923)?; ops::print_cfg(&ws, &compile_opts)?; } else { ops::compile(&ws, &compile_opts)?; diff --git a/src/cargo/core/compiler/rustc_cfg.rs b/src/cargo/core/compiler/rustc_cfg.rs index 30a2561af1d..115b8ada24f 100644 --- a/src/cargo/core/compiler/rustc_cfg.rs +++ b/src/cargo/core/compiler/rustc_cfg.rs @@ -80,9 +80,9 @@ impl<'a> SerializedTargetData<'a> { .iter() .map(|c| match c { Cfg::Name(n) => n.as_str(), - Cfg::KeyPair(k, v) => format!("{}='{}'", k, v).as_str() + Cfg::KeyPair(k, v) => format!("{}='{}'", k, v).as_str(), }) - .collect() + .collect(), } } } @@ -97,7 +97,7 @@ pub fn emit_serialized_rustc_cfg(rtd: &RustcTargetData, kinds: &[CompileKind]) - let mut target = HashMap::with_capacity(1); target.insert(ct.short_name(), SerializedTargetData::new(rtd, *k)); Some(target) - }, + } }) .collect(); let s = SerializedRustcCfg { diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index f1f272835fd..4f2de8d693d 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -294,10 +294,7 @@ pub fn compile_ws<'a>( cx.compile(exec) } -pub fn print_cfg<'a>( - ws: &Workspace<'a>, - options: &CompileOptions, -) -> CargoResult<()> { +pub fn print_cfg<'a>(ws: &Workspace<'a>, options: &CompileOptions) -> CargoResult<()> { let target_data = RustcTargetData::new(ws, &options.build_config.requested_kinds)?; rustc_cfg::emit_serialized_rustc_cfg(&target_data, &options.build_config.requested_kinds)?; Ok(()) From 0c133f35578e3c1c9939c3e4a84ae00e136ee354 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Fri, 8 Jan 2021 14:51:34 -0500 Subject: [PATCH 20/43] Fix compile-time errors --- src/cargo/core/compiler/build_config.rs | 1 - src/cargo/core/compiler/rustc_cfg.rs | 14 +++++++------- src/cargo/ops/cargo_compile.rs | 4 ---- src/cargo/ops/mod.rs | 2 +- 4 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/cargo/core/compiler/build_config.rs b/src/cargo/core/compiler/build_config.rs index 12c2d13e663..d0f5431a47e 100644 --- a/src/cargo/core/compiler/build_config.rs +++ b/src/cargo/core/compiler/build_config.rs @@ -77,7 +77,6 @@ impl BuildConfig { force_rebuild: false, build_plan: false, unit_graph: false, - rustc_cfg: false, primary_unit_rustc: None, rustfix_diagnostic_server: RefCell::new(None), export_dir: None, diff --git a/src/cargo/core/compiler/rustc_cfg.rs b/src/cargo/core/compiler/rustc_cfg.rs index 115b8ada24f..414e165c51e 100644 --- a/src/cargo/core/compiler/rustc_cfg.rs +++ b/src/cargo/core/compiler/rustc_cfg.rs @@ -63,24 +63,24 @@ const VERSION: u32 = 1; #[derive(serde::Serialize)] struct SerializedRustcCfg<'a> { version: u32, - host: SerializedTargetData<'a>, - targets: Vec>>, + host: SerializedTargetData, + targets: Vec>, } #[derive(serde::Serialize)] -struct SerializedTargetData<'a> { - cfgs: Vec<&'a str>, +struct SerializedTargetData { + cfgs: Vec, } -impl<'a> SerializedTargetData<'a> { +impl<'a> SerializedTargetData { fn new(rtd: &'a RustcTargetData, kind: CompileKind) -> Self { Self { cfgs: rtd .cfg(kind) .iter() .map(|c| match c { - Cfg::Name(n) => n.as_str(), - Cfg::KeyPair(k, v) => format!("{}='{}'", k, v).as_str(), + Cfg::Name(n) => n.to_owned(), + Cfg::KeyPair(k, v) => format!("{}='{}'", k, v), }) .collect(), } diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 4f2de8d693d..5ce18b90514 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -285,10 +285,6 @@ pub fn compile_ws<'a>( unit_graph::emit_serialized_unit_graph(&bcx.roots, &bcx.unit_graph)?; return Ok(Compilation::new(&bcx)?); } - if options.build_config.rustc_cfg { - rustc_cfg::emit_serialized_rustc_cfg(&bcx.target_data, &bcx.build_config.requested_kinds)?; - return Ok(Compilation::new(&bcx)?); - } let _p = profile::start("compiling"); let cx = Context::new(&bcx)?; cx.compile(exec) diff --git a/src/cargo/ops/mod.rs b/src/cargo/ops/mod.rs index 4853993e4a5..0607a7b7e76 100644 --- a/src/cargo/ops/mod.rs +++ b/src/cargo/ops/mod.rs @@ -1,6 +1,6 @@ pub use self::cargo_clean::{clean, CleanOptions}; pub use self::cargo_compile::{ - compile, compile_with_exec, compile_ws, create_bcx, resolve_all_features, CompileOptions, + compile, compile_with_exec, compile_ws, create_bcx, print_cfg, resolve_all_features, CompileOptions, }; pub use self::cargo_compile::{CompileFilter, FilterRule, LibRule, Packages}; pub use self::cargo_doc::{doc, DocOptions}; From 6d48e500f87b23ee2dff18b5eac859508a2283c6 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Fri, 8 Jan 2021 14:52:34 -0500 Subject: [PATCH 21/43] Fix formatting --- src/cargo/ops/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cargo/ops/mod.rs b/src/cargo/ops/mod.rs index 0607a7b7e76..bf673ba48af 100644 --- a/src/cargo/ops/mod.rs +++ b/src/cargo/ops/mod.rs @@ -1,6 +1,7 @@ pub use self::cargo_clean::{clean, CleanOptions}; pub use self::cargo_compile::{ - compile, compile_with_exec, compile_ws, create_bcx, print_cfg, resolve_all_features, CompileOptions, + compile, compile_with_exec, compile_ws, create_bcx, print_cfg, resolve_all_features, + CompileOptions, }; pub use self::cargo_compile::{CompileFilter, FilterRule, LibRule, Packages}; pub use self::cargo_doc::{doc, DocOptions}; From afa1905e7507412ea64065fc99119076247d2be2 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Sat, 30 Jan 2021 11:46:23 -0500 Subject: [PATCH 22/43] Remove `--print-cfg` argument The `--print-cfg` flag will be replaced with the `--print foo` option and passed to `rustc --print foo`. The output will be the "raw" output from the `rustc --print foo` command instead of a serialized JSON of the RustcTarget Data. This is the first step in changing the implementation. The serialization code is no longer needed. --- src/bin/cargo/commands/rustc.rs | 16 ++-- src/cargo/core/compiler/rustc_cfg.rs | 113 --------------------------- src/cargo/ops/cargo_compile.rs | 4 +- 3 files changed, 6 insertions(+), 127 deletions(-) delete mode 100644 src/cargo/core/compiler/rustc_cfg.rs diff --git a/src/bin/cargo/commands/rustc.rs b/src/bin/cargo/commands/rustc.rs index d6ee205e7a8..d92937931af 100644 --- a/src/bin/cargo/commands/rustc.rs +++ b/src/bin/cargo/commands/rustc.rs @@ -2,7 +2,7 @@ use crate::command_prelude::*; use cargo::ops; -const PRINT_CFG_ARG_NAME: &str = "print-cfg"; +const PRINT_ARG_NAME: &str = "print"; pub fn cli() -> App { subcommand("rustc") @@ -32,13 +32,7 @@ pub fn cli() -> App { .arg_manifest_path() .arg_message_format() .arg_unit_graph() - .arg( - opt( - PRINT_CFG_ARG_NAME, - "Output the compiler configuration in JSON (unstable)", - ) - .hidden(true), - ) + .arg(Arg::with_name(PRINT_ARG_NAME).takes_value(true).help("Output the compiler configuration").hidden(true)) .after_help("Run `cargo help rustc` for more detailed information.\n") } @@ -70,11 +64,11 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { } else { Some(target_args) }; - if args.is_present(PRINT_CFG_ARG_NAME) { + if args.is_present(PRINT_ARG_NAME) { config .cli_unstable() - .fail_if_stable_opt(PRINT_CFG_ARG_NAME, 8923)?; - ops::print_cfg(&ws, &compile_opts)?; + .fail_if_stable_opt(PRINT_ARG_NAME, 8923)?; + ops::print(&ws, &compile_opts)?; } else { ops::compile(&ws, &compile_opts)?; } diff --git a/src/cargo/core/compiler/rustc_cfg.rs b/src/cargo/core/compiler/rustc_cfg.rs deleted file mode 100644 index 414e165c51e..00000000000 --- a/src/cargo/core/compiler/rustc_cfg.rs +++ /dev/null @@ -1,113 +0,0 @@ -use cargo_platform::Cfg; - -use crate::core::compiler::{CompileKind, RustcTargetData}; -use crate::util::CargoResult; - -use std::collections::HashMap; -use std::io::Write; - -const VERSION: u32 = 1; - -// { -// "version": 1, -// "host": [ -// "windows", -// "debug_assertions", -// "target_arch='x86_64'", -// "target_endian='little'", -// "target_env='msvc'", -// "target_family='windows'", -// "target_feature='fxsr'", -// "target_feature='sse'", -// "target_feature='sse2'", -// "target_os='windows'", -// "target_pointer_width='64'", -// "target_vendor='pc'" -// ], -// "targets": [ -// { -// "x86_64-unknown-linux-gnu": [ -// "unix", -// "debug_assertions", -// "target_arch='x86_64'", -// "target_endian='little'", -// "target_env='gnu'", -// "target_family='unix'", -// "target_feature='fxsr'", -// "target_feature='sse'", -// "target_feature='sse2'", -// "target_os='linux'", -// "target_pointer_width='64'", -// "target_vendor='unknown'" -// ] -// }, -// { -// "i686-pc-windows-msvc": [ -// "windows", -// "debug_assertions", -// "target_arch='x86'", -// "target_endian='little'", -// "target_env='msvc'", -// "target_family='windows'", -// "target_feature='fxsr'", -// "target_feature='sse'", -// "target_feature='sse2'", -// "target_os='windows'", -// "target_pointer_width='32'", -// "target_vendor='pc'" -// ] -// } -// } -// } - -#[derive(serde::Serialize)] -struct SerializedRustcCfg<'a> { - version: u32, - host: SerializedTargetData, - targets: Vec>, -} - -#[derive(serde::Serialize)] -struct SerializedTargetData { - cfgs: Vec, -} - -impl<'a> SerializedTargetData { - fn new(rtd: &'a RustcTargetData, kind: CompileKind) -> Self { - Self { - cfgs: rtd - .cfg(kind) - .iter() - .map(|c| match c { - Cfg::Name(n) => n.to_owned(), - Cfg::KeyPair(k, v) => format!("{}='{}'", k, v), - }) - .collect(), - } - } -} - -pub fn emit_serialized_rustc_cfg(rtd: &RustcTargetData, kinds: &[CompileKind]) -> CargoResult<()> { - let host = SerializedTargetData::new(rtd, CompileKind::Host); - let targets = kinds - .iter() - .filter_map(|k| match k { - CompileKind::Host => None, - CompileKind::Target(ct) => { - let mut target = HashMap::with_capacity(1); - target.insert(ct.short_name(), SerializedTargetData::new(rtd, *k)); - Some(target) - } - }) - .collect(); - let s = SerializedRustcCfg { - version: VERSION, - host, - targets, - }; - let stdout = std::io::stdout(); - let mut lock = stdout.lock(); - serde_json::to_writer(&mut lock, &s)?; - drop(writeln!(lock)); - Ok(()) -} diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 5ce18b90514..0aa3f02e8f7 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -290,9 +290,7 @@ pub fn compile_ws<'a>( cx.compile(exec) } -pub fn print_cfg<'a>(ws: &Workspace<'a>, options: &CompileOptions) -> CargoResult<()> { - let target_data = RustcTargetData::new(ws, &options.build_config.requested_kinds)?; - rustc_cfg::emit_serialized_rustc_cfg(&target_data, &options.build_config.requested_kinds)?; +pub fn print<'a>(ws: &Workspace<'a>, options: &CompileOptions) -> CargoResult<()> { Ok(()) } From 13807557cd178be2271282b79c33994455729cca Mon Sep 17 00:00:00 2001 From: Chris Field Date: Sat, 30 Jan 2021 14:09:01 -0500 Subject: [PATCH 23/43] Change implementation The implementation now follows `rustc --print ` and adds the `--print ` option to the `cargo rustc` subcommand. When the `cargo rustc --print ` command is executed, all extra arguments are passed to the `rustc --print ` command including the Cargo-specific (compiler) target flags and options from Cargo's configuration and environment variables. If multiple `--target ` options are used, then the list is of (compiler) targets are iterated and outputed with an empty line delimiting the output for each compiler target. If no `--target ` is used, then the host compiler target is used. None of the output is altered. It is just a straight dump of the `rustc --print ` command. This implementation works with any of the `` values for the `rustc --print ` command, i.e. `cfg`, `target-list`, `sysroot`, etc. Most of the arguments for either `cargo rustc` are ignored unles recognized by the `rustc --print ` argument. For example, `--target `. Cargo configuration is target dependent, so the `--target ` must be recognized. --- src/bin/cargo/commands/rustc.rs | 12 +++- src/cargo/core/compiler/build_context/mod.rs | 4 +- .../compiler/build_context/target_info.rs | 4 +- src/cargo/core/compiler/mod.rs | 5 +- src/cargo/ops/cargo_compile.rs | 70 ++++++++++++++++++- src/cargo/ops/mod.rs | 3 +- 6 files changed, 85 insertions(+), 13 deletions(-) diff --git a/src/bin/cargo/commands/rustc.rs b/src/bin/cargo/commands/rustc.rs index d92937931af..a4a84e0183f 100644 --- a/src/bin/cargo/commands/rustc.rs +++ b/src/bin/cargo/commands/rustc.rs @@ -28,11 +28,17 @@ pub fn cli() -> App { .arg_profile("Build artifacts with the specified profile") .arg_features() .arg_target_triple("Target triple which compiles will be for") + .arg( + opt( + PRINT_ARG_NAME, + "Output compiler information without compiling", + ) + .value_name("INFO"), + ) .arg_target_dir() .arg_manifest_path() .arg_message_format() .arg_unit_graph() - .arg(Arg::with_name(PRINT_ARG_NAME).takes_value(true).help("Output the compiler configuration").hidden(true)) .after_help("Run `cargo help rustc` for more detailed information.\n") } @@ -64,11 +70,11 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { } else { Some(target_args) }; - if args.is_present(PRINT_ARG_NAME) { + if let Some(opt_value) = args.value_of(PRINT_ARG_NAME) { config .cli_unstable() .fail_if_stable_opt(PRINT_ARG_NAME, 8923)?; - ops::print(&ws, &compile_opts)?; + ops::print(&ws, &compile_opts, opt_value)?; } else { ops::compile(&ws, &compile_opts)?; } diff --git a/src/cargo/core/compiler/build_context/mod.rs b/src/cargo/core/compiler/build_context/mod.rs index 47efdc77de8..fc4f6a73597 100644 --- a/src/cargo/core/compiler/build_context/mod.rs +++ b/src/cargo/core/compiler/build_context/mod.rs @@ -11,7 +11,9 @@ use std::collections::HashMap; use std::path::PathBuf; mod target_info; -pub use self::target_info::{FileFlavor, FileType, RustcTargetData, TargetInfo}; +pub use self::target_info::{ + env_args, output_err_info, FileFlavor, FileType, RustcTargetData, TargetInfo, +}; /// The build context, containing all information about a build task. /// diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index 53fe197707c..efb929248b3 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -519,7 +519,7 @@ fn parse_crate_type( } /// Helper for creating an error message when parsing rustc output fails. -fn output_err_info(cmd: &ProcessBuilder, stdout: &str, stderr: &str) -> String { +pub fn output_err_info(cmd: &ProcessBuilder, stdout: &str, stderr: &str) -> String { let mut result = format!("command was: {}\n", cmd); if !stdout.is_empty() { result.push_str("\n--- stdout\n"); @@ -552,7 +552,7 @@ fn output_err_info(cmd: &ProcessBuilder, stdout: &str, stderr: &str) -> String { /// /// Note that if a `target` is specified, no args will be passed to host code (plugins, build /// scripts, ...), even if it is the same as the target. -fn env_args( +pub fn env_args( config: &Config, requested_kinds: &[CompileKind], host_triple: &str, diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index bb6a8b26968..666a25475e4 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -13,7 +13,6 @@ mod layout; mod links; mod lto; mod output_depinfo; -pub mod rustc_cfg; pub mod rustdoc; pub mod standard_lib; mod timings; @@ -33,7 +32,9 @@ use lazycell::LazyCell; use log::debug; pub use self::build_config::{BuildConfig, CompileMode, MessageFormat}; -pub use self::build_context::{BuildContext, FileFlavor, FileType, RustcTargetData, TargetInfo}; +pub use self::build_context::{ + env_args, output_err_info, BuildContext, FileFlavor, FileType, RustcTargetData, TargetInfo, +}; use self::build_plan::BuildPlan; pub use self::compilation::{Compilation, Doctest}; pub use self::compile_kind::{CompileKind, CompileTarget}; diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 0aa3f02e8f7..5b277912c9f 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -24,14 +24,16 @@ use std::collections::{BTreeSet, HashMap, HashSet}; use std::hash::{Hash, Hasher}; +use std::io::Write; use std::sync::Arc; -use crate::core::compiler::rustc_cfg; use crate::core::compiler::standard_lib; use crate::core::compiler::unit_dependencies::build_unit_dependencies; use crate::core::compiler::unit_graph::{self, UnitDep, UnitGraph}; +use crate::core::compiler::{ + env_args, output_err_info, CompileKind, CompileMode, CompileTarget, RustcTargetData, Unit, +}; use crate::core::compiler::{BuildConfig, BuildContext, Compilation, Context}; -use crate::core::compiler::{CompileKind, CompileMode, CompileTarget, RustcTargetData, Unit}; use crate::core::compiler::{DefaultExecutor, Executor, UnitInterner}; use crate::core::profiles::{Profiles, UnitFor}; use crate::core::resolver::features::{self, FeaturesFor, RequestedFeatures}; @@ -42,6 +44,7 @@ use crate::ops; use crate::ops::resolve::WorkspaceResolve; use crate::util::config::Config; use crate::util::restricted_names::is_glob_pattern; +use crate::util::CargoResultExt; use crate::util::{closest_msg, profile, CargoResult, StableHasher}; use anyhow::Context as _; @@ -290,7 +293,68 @@ pub fn compile_ws<'a>( cx.compile(exec) } -pub fn print<'a>(ws: &Workspace<'a>, options: &CompileOptions) -> CargoResult<()> { +pub fn print<'a>( + ws: &Workspace<'a>, + options: &CompileOptions, + print_opt_value: &str, +) -> CargoResult<()> { + let CompileOptions { + ref build_config, + ref target_rustc_args, + .. + } = *options; + let config = ws.config(); + let rustc = config.load_global_rustc(Some(ws))?; + let mut kinds = build_config.requested_kinds.clone(); + if kinds.is_empty() { + kinds.push(CompileKind::Host); + } + let mut print_empty_line = false; + for kind in kinds { + let rustflags = env_args( + config, + &build_config.requested_kinds, + &rustc.host, + None, + kind, + "RUSTFLAGS", + )?; + let mut process = rustc.process(); + process + .arg("-") + .arg("--crate-name") + .arg("___") + .args(&rustflags); + if let Some(args) = target_rustc_args { + process.args(args); + } + if let CompileKind::Target(t) = kind { + process.arg("--target").arg(t.short_name()); + } + process + .arg("--print") + .arg(print_opt_value) + .env_remove("RUSTC_LOG"); + let (output, error) = rustc + .cached_output(&process) + .chain_err(|| "failed to run `rustc --print=`")?; + if output.is_empty() { + anyhow::bail!( + "output of --print={} missing from rustc\n{}", + print_opt_value, + output_err_info(&process, &output, &error) + ); + } + let stdout = std::io::stdout(); + let mut lock = stdout.lock(); + if print_empty_line { + writeln!(lock)?; + } else { + print_empty_line = true; + } + lock.write_all(output.as_bytes())?; + drop(lock); + } Ok(()) } diff --git a/src/cargo/ops/mod.rs b/src/cargo/ops/mod.rs index bf673ba48af..5b5894f991b 100644 --- a/src/cargo/ops/mod.rs +++ b/src/cargo/ops/mod.rs @@ -1,7 +1,6 @@ pub use self::cargo_clean::{clean, CleanOptions}; pub use self::cargo_compile::{ - compile, compile_with_exec, compile_ws, create_bcx, print_cfg, resolve_all_features, - CompileOptions, + compile, compile_with_exec, compile_ws, create_bcx, print, resolve_all_features, CompileOptions, }; pub use self::cargo_compile::{CompileFilter, FilterRule, LibRule, Packages}; pub use self::cargo_doc::{doc, DocOptions}; From bf3ed17fca3fbcebbd47393827b205f0a193297b Mon Sep 17 00:00:00 2001 From: Chris Field Date: Mon, 8 Feb 2021 21:14:57 -0500 Subject: [PATCH 24/43] Remove cache output and error handling Error handling is done by rustc and the stderr is simply printed. The `rustc --print ` command is always executed instead of using the cache. --- src/cargo/ops/cargo_compile.rs | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 5b277912c9f..d4c8ced3101 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -335,16 +335,7 @@ pub fn print<'a>( .arg("--print") .arg(print_opt_value) .env_remove("RUSTC_LOG"); - let (output, error) = rustc - .cached_output(&process) - .chain_err(|| "failed to run `rustc --print=`")?; - if output.is_empty() { - anyhow::bail!( - "output of --print={} missing from rustc\n{}", - print_opt_value, - output_err_info(&process, &output, &error) - ); - } + let output = process.exec_with_output()?; let stdout = std::io::stdout(); let mut lock = stdout.lock(); if print_empty_line { @@ -352,7 +343,7 @@ pub fn print<'a>( } else { print_empty_line = true; } - lock.write_all(output.as_bytes())?; + lock.write_all(&output.stdout)?; drop(lock); } Ok(()) From db77c3165ad8e2fbf04f5bca90114acba8485dc1 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Mon, 8 Feb 2021 21:18:07 -0500 Subject: [PATCH 25/43] Fix warnings --- src/cargo/ops/cargo_compile.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index d4c8ced3101..3ec43dc49f6 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -31,7 +31,7 @@ use crate::core::compiler::standard_lib; use crate::core::compiler::unit_dependencies::build_unit_dependencies; use crate::core::compiler::unit_graph::{self, UnitDep, UnitGraph}; use crate::core::compiler::{ - env_args, output_err_info, CompileKind, CompileMode, CompileTarget, RustcTargetData, Unit, + env_args, CompileKind, CompileMode, CompileTarget, RustcTargetData, Unit, }; use crate::core::compiler::{BuildConfig, BuildContext, Compilation, Context}; use crate::core::compiler::{DefaultExecutor, Executor, UnitInterner}; @@ -44,7 +44,6 @@ use crate::ops; use crate::ops::resolve::WorkspaceResolve; use crate::util::config::Config; use crate::util::restricted_names::is_glob_pattern; -use crate::util::CargoResultExt; use crate::util::{closest_msg, profile, CargoResult, StableHasher}; use anyhow::Context as _; @@ -333,8 +332,7 @@ pub fn print<'a>( } process .arg("--print") - .arg(print_opt_value) - .env_remove("RUSTC_LOG"); + .arg(print_opt_value); let output = process.exec_with_output()?; let stdout = std::io::stdout(); let mut lock = stdout.lock(); From 2f985aec30fbfefa0198afd682dc9c133e29ab91 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Mon, 8 Feb 2021 21:20:32 -0500 Subject: [PATCH 26/43] Remove unnecessary arguments and env removal --- src/cargo/ops/cargo_compile.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 3ec43dc49f6..32ff02a7e07 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -319,11 +319,7 @@ pub fn print<'a>( "RUSTFLAGS", )?; let mut process = rustc.process(); - process - .arg("-") - .arg("--crate-name") - .arg("___") - .args(&rustflags); + process.args(&rustflags); if let Some(args) = target_rustc_args { process.args(args); } From 2cd5d9dfe1d49ede2521364848076dc3a6c6317a Mon Sep 17 00:00:00 2001 From: Chris Field Date: Mon, 8 Feb 2021 21:35:13 -0500 Subject: [PATCH 27/43] Change to enumerate implementation Only printing an empty line after the first target output is changed to use the `enumerate` method on an Iterator with a non-zero index. I could not get the `drop_println` macro to compile. Something about the `shell` missing. The standard library `println` macro seems work fine. --- src/cargo/ops/cargo_compile.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 32ff02a7e07..717c7cb95da 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -308,14 +308,16 @@ pub fn print<'a>( if kinds.is_empty() { kinds.push(CompileKind::Host); } - let mut print_empty_line = false; - for kind in kinds { + for (index, kind) in kinds.iter().enumerate() { + if index != 0 { + println!(); + } let rustflags = env_args( config, &build_config.requested_kinds, &rustc.host, None, - kind, + *kind, "RUSTFLAGS", )?; let mut process = rustc.process(); @@ -332,11 +334,6 @@ pub fn print<'a>( let output = process.exec_with_output()?; let stdout = std::io::stdout(); let mut lock = stdout.lock(); - if print_empty_line { - writeln!(lock)?; - } else { - print_empty_line = true; - } lock.write_all(&output.stdout)?; drop(lock); } From a05dac35cbb74e0bc701b2e5c6304ad3cd79da1c Mon Sep 17 00:00:00 2001 From: Chris Field Date: Mon, 8 Feb 2021 21:42:21 -0500 Subject: [PATCH 28/43] Remove duplicate check for empty kinds --- src/cargo/ops/cargo_compile.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 717c7cb95da..15200432868 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -304,11 +304,7 @@ pub fn print<'a>( } = *options; let config = ws.config(); let rustc = config.load_global_rustc(Some(ws))?; - let mut kinds = build_config.requested_kinds.clone(); - if kinds.is_empty() { - kinds.push(CompileKind::Host); - } - for (index, kind) in kinds.iter().enumerate() { + for (index, kind) in build_config.requested_kinds.iter().enumerate() { if index != 0 { println!(); } @@ -328,9 +324,7 @@ pub fn print<'a>( if let CompileKind::Target(t) = kind { process.arg("--target").arg(t.short_name()); } - process - .arg("--print") - .arg(print_opt_value); + process.arg("--print").arg(print_opt_value); let output = process.exec_with_output()?; let stdout = std::io::stdout(); let mut lock = stdout.lock(); From 5a24ad1732475bdb83513b7e1106de321a10587e Mon Sep 17 00:00:00 2001 From: Chris Field Date: Tue, 9 Feb 2021 13:06:02 -0500 Subject: [PATCH 29/43] Change println macro to drop_println I figured out the error was me improperly using the macro. CI build failed because `println` macro is not allowed in Cargo codebase. --- src/cargo/ops/cargo_compile.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 15200432868..e9254ef0bcd 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -27,6 +27,7 @@ use std::hash::{Hash, Hasher}; use std::io::Write; use std::sync::Arc; +use crate::drop_println; use crate::core::compiler::standard_lib; use crate::core::compiler::unit_dependencies::build_unit_dependencies; use crate::core::compiler::unit_graph::{self, UnitDep, UnitGraph}; @@ -306,7 +307,7 @@ pub fn print<'a>( let rustc = config.load_global_rustc(Some(ws))?; for (index, kind) in build_config.requested_kinds.iter().enumerate() { if index != 0 { - println!(); + drop_println!(config); } let rustflags = env_args( config, From d7034c6021007a4c6a342956ecee0525570f3584 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Tue, 9 Feb 2021 13:07:37 -0500 Subject: [PATCH 30/43] Fix formatting --- src/cargo/ops/cargo_compile.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index e9254ef0bcd..536c8b21c46 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -27,7 +27,6 @@ use std::hash::{Hash, Hasher}; use std::io::Write; use std::sync::Arc; -use crate::drop_println; use crate::core::compiler::standard_lib; use crate::core::compiler::unit_dependencies::build_unit_dependencies; use crate::core::compiler::unit_graph::{self, UnitDep, UnitGraph}; @@ -41,6 +40,7 @@ use crate::core::resolver::features::{self, FeaturesFor, RequestedFeatures}; use crate::core::resolver::{HasDevUnits, Resolve, ResolveOpts}; use crate::core::{FeatureValue, Package, PackageSet, Shell, Summary, Target}; use crate::core::{PackageId, PackageIdSpec, TargetKind, Workspace}; +use crate::drop_println; use crate::ops; use crate::ops::resolve::WorkspaceResolve; use crate::util::config::Config; From c86864ba2f582a6be5cba04b0ea3547249fa3fb8 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Tue, 9 Feb 2021 13:26:46 -0500 Subject: [PATCH 31/43] Fix missing import after merge --- src/cargo/ops/cargo_compile.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 7f2fec354e0..34a25b48b2a 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -39,7 +39,7 @@ use crate::core::profiles::{Profiles, UnitFor}; use crate::core::resolver::features::{self, FeaturesFor, RequestedFeatures}; use crate::core::resolver::{HasDevUnits, Resolve, ResolveOpts}; use crate::core::{FeatureValue, Package, PackageSet, Shell, Summary, Target}; -use crate::core::{PackageId, PackageIdSpec, TargetKind, Workspace}; +use crate::core::{PackageId, PackageIdSpec, SourceId, TargetKind, Workspace}; use crate::drop_println; use crate::ops; use crate::ops::resolve::WorkspaceResolve; From f32f72f45629e0b206bfba4285b1b538b6300bb7 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Tue, 9 Feb 2021 20:31:41 -0500 Subject: [PATCH 32/43] Fix missing import --- src/cargo/ops/cargo_compile.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 7f7d2db42ca..f6142aba3b5 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -39,7 +39,7 @@ use crate::core::profiles::{Profiles, UnitFor}; use crate::core::resolver::features::{self, FeaturesFor, RequestedFeatures}; use crate::core::resolver::{HasDevUnits, Resolve, ResolveOpts}; use crate::core::{FeatureValue, Package, PackageSet, Shell, Summary, Target}; -use crate::core::{PackageId, PackageIdSpec, TargetKind, Workspace}; +use crate::core::{PackageId, PackageIdSpec, SourceId, TargetKind, Workspace}; use crate::drop_println; use crate::ops; use crate::ops::resolve::WorkspaceResolve; From 2aa4fba8200863af3b6aa8bc11401d303eedc75d Mon Sep 17 00:00:00 2001 From: Chris Field Date: Wed, 10 Feb 2021 19:10:14 -0500 Subject: [PATCH 33/43] Change scope of function The `output_err_info` function can be private now that the implementation for the `cargo rustc --print ` command has changed to not capture the output. --- src/cargo/core/compiler/build_context/mod.rs | 2 +- src/cargo/core/compiler/build_context/target_info.rs | 2 +- src/cargo/core/compiler/mod.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cargo/core/compiler/build_context/mod.rs b/src/cargo/core/compiler/build_context/mod.rs index 19fa1f4672e..759d3b44a12 100644 --- a/src/cargo/core/compiler/build_context/mod.rs +++ b/src/cargo/core/compiler/build_context/mod.rs @@ -12,7 +12,7 @@ use std::path::PathBuf; mod target_info; pub use self::target_info::{ - env_args, output_err_info, FileFlavor, FileType, RustcTargetData, TargetInfo, + env_args, FileFlavor, FileType, RustcTargetData, TargetInfo, }; /// The build context, containing all information about a build task. diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index b4498aa4d44..67a44cbf002 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -519,7 +519,7 @@ fn parse_crate_type( } /// Helper for creating an error message when parsing rustc output fails. -pub fn output_err_info(cmd: &ProcessBuilder, stdout: &str, stderr: &str) -> String { +fn output_err_info(cmd: &ProcessBuilder, stdout: &str, stderr: &str) -> String { let mut result = format!("command was: {}\n", cmd); if !stdout.is_empty() { result.push_str("\n--- stdout\n"); diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 18b2b5152ff..01e78bfe7d4 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -33,7 +33,7 @@ use log::debug; pub use self::build_config::{BuildConfig, CompileMode, MessageFormat}; pub use self::build_context::{ - env_args, output_err_info, BuildContext, FileFlavor, FileType, RustcTargetData, TargetInfo, + env_args, BuildContext, FileFlavor, FileType, RustcTargetData, TargetInfo, }; use self::build_plan::BuildPlan; pub use self::compilation::{Compilation, Doctest, UnitOutput}; From 39fb01c2a2d04d49e3c11f73f08d7fbf31084050 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Wed, 10 Feb 2021 19:30:21 -0500 Subject: [PATCH 34/43] Change to use TargetInfo type The `TargetInfo` type, but more specifically the `TargetInfo::new` method is used instead of the making the `env_args` function public and using it. This means the `env_args` function can go back to being private, too. --- src/cargo/core/compiler/build_context/mod.rs | 4 +--- .../core/compiler/build_context/target_info.rs | 2 +- src/cargo/core/compiler/mod.rs | 4 +--- src/cargo/ops/cargo_compile.rs | 17 ++++------------- 4 files changed, 7 insertions(+), 20 deletions(-) diff --git a/src/cargo/core/compiler/build_context/mod.rs b/src/cargo/core/compiler/build_context/mod.rs index 759d3b44a12..303bf6a873c 100644 --- a/src/cargo/core/compiler/build_context/mod.rs +++ b/src/cargo/core/compiler/build_context/mod.rs @@ -11,9 +11,7 @@ use std::collections::{HashMap, HashSet}; use std::path::PathBuf; mod target_info; -pub use self::target_info::{ - env_args, FileFlavor, FileType, RustcTargetData, TargetInfo, -}; +pub use self::target_info::{FileFlavor, FileType, RustcTargetData, TargetInfo}; /// The build context, containing all information about a build task. /// diff --git a/src/cargo/core/compiler/build_context/target_info.rs b/src/cargo/core/compiler/build_context/target_info.rs index 67a44cbf002..9d205d0e212 100644 --- a/src/cargo/core/compiler/build_context/target_info.rs +++ b/src/cargo/core/compiler/build_context/target_info.rs @@ -552,7 +552,7 @@ fn output_err_info(cmd: &ProcessBuilder, stdout: &str, stderr: &str) -> String { /// /// Note that if a `target` is specified, no args will be passed to host code (plugins, build /// scripts, ...), even if it is the same as the target. -pub fn env_args( +fn env_args( config: &Config, requested_kinds: &[CompileKind], host_triple: &str, diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 01e78bfe7d4..464ae20fe48 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -32,9 +32,7 @@ use lazycell::LazyCell; use log::debug; pub use self::build_config::{BuildConfig, CompileMode, MessageFormat}; -pub use self::build_context::{ - env_args, BuildContext, FileFlavor, FileType, RustcTargetData, TargetInfo, -}; +pub use self::build_context::{BuildContext, FileFlavor, FileType, RustcTargetData, TargetInfo}; use self::build_plan::BuildPlan; pub use self::compilation::{Compilation, Doctest, UnitOutput}; pub use self::compile_kind::{CompileKind, CompileTarget}; diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index f6142aba3b5..83899e1d525 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -27,13 +27,11 @@ use std::hash::{Hash, Hasher}; use std::io::Write; use std::sync::Arc; -use crate::core::compiler::standard_lib; use crate::core::compiler::unit_dependencies::build_unit_dependencies; use crate::core::compiler::unit_graph::{self, UnitDep, UnitGraph}; -use crate::core::compiler::{ - env_args, CompileKind, CompileMode, CompileTarget, RustcTargetData, Unit, -}; +use crate::core::compiler::{standard_lib, TargetInfo}; use crate::core::compiler::{BuildConfig, BuildContext, Compilation, Context}; +use crate::core::compiler::{CompileKind, CompileMode, CompileTarget, RustcTargetData, Unit}; use crate::core::compiler::{DefaultExecutor, Executor, UnitInterner}; use crate::core::profiles::{Profiles, UnitFor}; use crate::core::resolver::features::{self, FeaturesFor, RequestedFeatures}; @@ -314,16 +312,9 @@ pub fn print<'a>( if index != 0 { drop_println!(config); } - let rustflags = env_args( - config, - &build_config.requested_kinds, - &rustc.host, - None, - *kind, - "RUSTFLAGS", - )?; + let target_info = TargetInfo::new(config, &build_config.requested_kinds, &rustc, *kind)?; let mut process = rustc.process(); - process.args(&rustflags); + process.args(&target_info.rustflags); if let Some(args) = target_rustc_args { process.args(args); } From fa8e9ae93172a981bac9a89035af7656c987eb3e Mon Sep 17 00:00:00 2001 From: Chris Field Date: Sat, 20 Feb 2021 10:35:27 -0500 Subject: [PATCH 35/43] Change to forwarding rustc stdio to cargo The `exec_with_output` function and subsequent writing to stdout and stderr is replaced with the `exec` function that inherits the stdio streams from Cargo is used. Weirdly, I originally tried to use the `exec` function but I go no output. The `cargo run -- rustc --print cfg` appear to just hang. So, I switched to using the `exec_with_output` function. Now, the `exec` function appears to work and it is much simplier. There must have been something else going on with my system at the time. --- src/cargo/ops/cargo_compile.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 83899e1d525..ce7db762745 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -322,11 +322,7 @@ pub fn print<'a>( process.arg("--target").arg(t.short_name()); } process.arg("--print").arg(print_opt_value); - let output = process.exec_with_output()?; - let stdout = std::io::stdout(); - let mut lock = stdout.lock(); - lock.write_all(&output.stdout)?; - drop(lock); + process.exec()?; } Ok(()) } From 8ce7633a6548a445430fb96195c8796a3d09585e Mon Sep 17 00:00:00 2001 From: Chris Field Date: Sat, 20 Feb 2021 10:40:08 -0500 Subject: [PATCH 36/43] Fix warning with unused import --- src/cargo/ops/cargo_compile.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index ce7db762745..3d4cf8c96a1 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -24,7 +24,6 @@ use std::collections::{BTreeSet, HashMap, HashSet}; use std::hash::{Hash, Hasher}; -use std::io::Write; use std::sync::Arc; use crate::core::compiler::unit_dependencies::build_unit_dependencies; From de340a2ef6a5e8649e2076024ea0011c51a2533b Mon Sep 17 00:00:00 2001 From: Chris Field Date: Sun, 21 Feb 2021 19:35:33 -0500 Subject: [PATCH 37/43] Add first test Not sure how to handle running the test on different hosts, so the first test is explicit about the compile target to print the configuration. --- tests/testsuite/rustc.rs | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/testsuite/rustc.rs b/tests/testsuite/rustc.rs index 48f6859f02d..029f2c8bcc3 100644 --- a/tests/testsuite/rustc.rs +++ b/tests/testsuite/rustc.rs @@ -458,3 +458,47 @@ fn rustc_test_with_implicit_bin() { ) .run(); } + +#[cargo_test] +fn rustc_with_print_cfg_single_target() { + let p = project() + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file("src/main.rs", r#"fn main() {} "#) + .build(); + + p.cargo("rustc -Z unstable-options --target x86_64-pc-windows-msvc --print cfg") + .masquerade_as_nightly_cargo() + .with_stdout_contains( + "\ +debug_assertions +target_arch=\"x86_64\" +target_endian=\"little\" +target_env=\"msvc\" +target_family=\"windows\" +target_feature=\"fxsr\" +target_feature=\"sse\" +target_feature=\"sse2\" +target_has_atomic=\"16\" +target_has_atomic=\"32\" +target_has_atomic=\"64\" +target_has_atomic=\"8\" +target_has_atomic=\"ptr\" +target_has_atomic_equal_alignment=\"16\" +target_has_atomic_equal_alignment=\"32\" +target_has_atomic_equal_alignment=\"64\" +target_has_atomic_equal_alignment=\"8\" +target_has_atomic_equal_alignment=\"ptr\" +target_has_atomic_load_store=\"16\" +target_has_atomic_load_store=\"32\" +target_has_atomic_load_store=\"64\" +target_has_atomic_load_store=\"8\" +target_has_atomic_load_store=\"ptr\" +target_os=\"windows\" +target_pointer_width=\"64\" +target_thread_local +target_vendor=\"pc\" +windows +", + ) + .run(); +} From 8b80e52ca80f0dca6f9b2e644c467a2e89397f45 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Sun, 21 Feb 2021 19:50:21 -0500 Subject: [PATCH 38/43] Add multitarget test Using the multitarget feature to print the configuration of multiple compiler target configurations, a test is created. --- tests/testsuite/rustc.rs | 71 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/tests/testsuite/rustc.rs b/tests/testsuite/rustc.rs index 029f2c8bcc3..cd2cbcb1727 100644 --- a/tests/testsuite/rustc.rs +++ b/tests/testsuite/rustc.rs @@ -502,3 +502,74 @@ windows ) .run(); } + +#[cargo_test] +fn rustc_with_print_cfg_multiple_targets() { + let p = project() + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file("src/main.rs", r#"fn main() {} "#) + .build(); + + p.cargo("rustc -Z unstable-options -Z multitarget --target x86_64-pc-windows-msvc --target i686-unknown-linux-gnu --print cfg") + .masquerade_as_nightly_cargo() + .with_stdout_contains( + "\ +debug_assertions +target_arch=\"x86\" +target_endian=\"little\" +target_env=\"gnu\" +target_family=\"unix\" +target_feature=\"fxsr\" +target_feature=\"sse\" +target_feature=\"sse2\" +target_has_atomic=\"16\" +target_has_atomic=\"32\" +target_has_atomic=\"64\" +target_has_atomic=\"8\" +target_has_atomic=\"ptr\" +target_has_atomic_equal_alignment=\"16\" +target_has_atomic_equal_alignment=\"32\" +target_has_atomic_equal_alignment=\"8\" +target_has_atomic_equal_alignment=\"ptr\" +target_has_atomic_load_store=\"16\" +target_has_atomic_load_store=\"32\" +target_has_atomic_load_store=\"64\" +target_has_atomic_load_store=\"8\" +target_has_atomic_load_store=\"ptr\" +target_os=\"linux\" +target_pointer_width=\"32\" +target_thread_local +target_vendor=\"unknown\" +unix + +debug_assertions +target_arch=\"x86_64\" +target_endian=\"little\" +target_env=\"msvc\" +target_family=\"windows\" +target_feature=\"fxsr\" +target_feature=\"sse\" +target_feature=\"sse2\" +target_has_atomic=\"16\" +target_has_atomic=\"32\" +target_has_atomic=\"64\" +target_has_atomic=\"8\" +target_has_atomic=\"ptr\" +target_has_atomic_equal_alignment=\"16\" +target_has_atomic_equal_alignment=\"32\" +target_has_atomic_equal_alignment=\"64\" +target_has_atomic_equal_alignment=\"8\" +target_has_atomic_equal_alignment=\"ptr\" +target_has_atomic_load_store=\"16\" +target_has_atomic_load_store=\"32\" +target_has_atomic_load_store=\"64\" +target_has_atomic_load_store=\"8\" +target_has_atomic_load_store=\"ptr\" +target_os=\"windows\" +target_pointer_width=\"64\" +target_thread_local +target_vendor=\"pc\" +windows", + ) + .run(); +} From c7038b22cad77b94140c75de38207674b5fd71dc Mon Sep 17 00:00:00 2001 From: Chris Field Date: Sun, 21 Feb 2021 19:54:08 -0500 Subject: [PATCH 39/43] Add test using RUSTFLAGS env var The `RUSTFLAGS` environment variable is used to add the "crt-static" target feature and test printing the target compiler configuration contains the `target_feature="crt-static"` line. --- tests/testsuite/rustc.rs | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tests/testsuite/rustc.rs b/tests/testsuite/rustc.rs index cd2cbcb1727..1a2bd9e8b1a 100644 --- a/tests/testsuite/rustc.rs +++ b/tests/testsuite/rustc.rs @@ -573,3 +573,49 @@ windows", ) .run(); } + +#[cargo_test] +fn rustc_with_print_cfg_rustflags_env_var() { + let p = project() + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file("src/main.rs", r#"fn main() {} "#) + .build(); + + p.cargo("rustc -Z unstable-options --target x86_64-pc-windows-msvc --print cfg") + .masquerade_as_nightly_cargo() + .env("RUSTFLAGS", "-C target-feature=+crt-static") + .with_stdout_contains( + "\ +debug_assertions +target_arch=\"x86_64\" +target_endian=\"little\" +target_env=\"msvc\" +target_family=\"windows\" +target_feature=\"crt-static\" +target_feature=\"fxsr\" +target_feature=\"sse\" +target_feature=\"sse2\" +target_has_atomic=\"16\" +target_has_atomic=\"32\" +target_has_atomic=\"64\" +target_has_atomic=\"8\" +target_has_atomic=\"ptr\" +target_has_atomic_equal_alignment=\"16\" +target_has_atomic_equal_alignment=\"32\" +target_has_atomic_equal_alignment=\"64\" +target_has_atomic_equal_alignment=\"8\" +target_has_atomic_equal_alignment=\"ptr\" +target_has_atomic_load_store=\"16\" +target_has_atomic_load_store=\"32\" +target_has_atomic_load_store=\"64\" +target_has_atomic_load_store=\"8\" +target_has_atomic_load_store=\"ptr\" +target_os=\"windows\" +target_pointer_width=\"64\" +target_thread_local +target_vendor=\"pc\" +windows +", + ) + .run(); +} From 1f057303b83f4a6998c20217ef79e02e04822ede Mon Sep 17 00:00:00 2001 From: Chris Field Date: Sun, 21 Feb 2021 19:58:19 -0500 Subject: [PATCH 40/43] Add test with cargo configuration file A `.cargo/config.toml` file is used to add the "crt-static" target feature and test printing the compiler target configuration contains the `target_feature="crt-static"` line. --- tests/testsuite/rustc.rs | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/testsuite/rustc.rs b/tests/testsuite/rustc.rs index 1a2bd9e8b1a..5eddb3c89d1 100644 --- a/tests/testsuite/rustc.rs +++ b/tests/testsuite/rustc.rs @@ -619,3 +619,53 @@ windows ) .run(); } + +#[cargo_test] +fn rustc_with_print_cfg_config_toml() { + let p = project() + .file("Cargo.toml", &basic_bin_manifest("foo")) + .file(".cargo/config.toml", r#" +[target.x86_64-pc-windows-msvc] +rustflags = ["-C", "target-feature=+crt-static"] +"#) + .file("src/main.rs", r#"fn main() {} "#) + .build(); + + p.cargo("rustc -Z unstable-options --target x86_64-pc-windows-msvc --print cfg") + .masquerade_as_nightly_cargo() + .env("RUSTFLAGS", "-C target-feature=+crt-static") + .with_stdout_contains( + "\ +debug_assertions +target_arch=\"x86_64\" +target_endian=\"little\" +target_env=\"msvc\" +target_family=\"windows\" +target_feature=\"crt-static\" +target_feature=\"fxsr\" +target_feature=\"sse\" +target_feature=\"sse2\" +target_has_atomic=\"16\" +target_has_atomic=\"32\" +target_has_atomic=\"64\" +target_has_atomic=\"8\" +target_has_atomic=\"ptr\" +target_has_atomic_equal_alignment=\"16\" +target_has_atomic_equal_alignment=\"32\" +target_has_atomic_equal_alignment=\"64\" +target_has_atomic_equal_alignment=\"8\" +target_has_atomic_equal_alignment=\"ptr\" +target_has_atomic_load_store=\"16\" +target_has_atomic_load_store=\"32\" +target_has_atomic_load_store=\"64\" +target_has_atomic_load_store=\"8\" +target_has_atomic_load_store=\"ptr\" +target_os=\"windows\" +target_pointer_width=\"64\" +target_thread_local +target_vendor=\"pc\" +windows +", + ) + .run(); +} From 70a423ebf8afd1c0002f0f25494cbb44b934a2b7 Mon Sep 17 00:00:00 2001 From: Chris Field Date: Sun, 21 Feb 2021 20:00:28 -0500 Subject: [PATCH 41/43] Fix formatting --- tests/testsuite/rustc.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/testsuite/rustc.rs b/tests/testsuite/rustc.rs index 5eddb3c89d1..3a43bc92c12 100644 --- a/tests/testsuite/rustc.rs +++ b/tests/testsuite/rustc.rs @@ -582,7 +582,7 @@ fn rustc_with_print_cfg_rustflags_env_var() { .build(); p.cargo("rustc -Z unstable-options --target x86_64-pc-windows-msvc --print cfg") - .masquerade_as_nightly_cargo() + .masquerade_as_nightly_cargo() .env("RUSTFLAGS", "-C target-feature=+crt-static") .with_stdout_contains( "\ @@ -624,15 +624,18 @@ windows fn rustc_with_print_cfg_config_toml() { let p = project() .file("Cargo.toml", &basic_bin_manifest("foo")) - .file(".cargo/config.toml", r#" + .file( + ".cargo/config.toml", + r#" [target.x86_64-pc-windows-msvc] rustflags = ["-C", "target-feature=+crt-static"] -"#) +"#, + ) .file("src/main.rs", r#"fn main() {} "#) .build(); p.cargo("rustc -Z unstable-options --target x86_64-pc-windows-msvc --print cfg") - .masquerade_as_nightly_cargo() + .masquerade_as_nightly_cargo() .env("RUSTFLAGS", "-C target-feature=+crt-static") .with_stdout_contains( "\ From 9b02dd41e4afcf163a65fab7940b53e45736b9bc Mon Sep 17 00:00:00 2001 From: Chris Field Date: Sun, 21 Feb 2021 21:45:55 -0500 Subject: [PATCH 42/43] Fix tests for CI environment The `panic="unwind"` appears in the output for the CI tests, but not in my local tests. I need to investigate the origin of this configuration but it causes the CI builds to fail. --- tests/testsuite/rustc.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/testsuite/rustc.rs b/tests/testsuite/rustc.rs index 3a43bc92c12..ced40183053 100644 --- a/tests/testsuite/rustc.rs +++ b/tests/testsuite/rustc.rs @@ -471,6 +471,7 @@ fn rustc_with_print_cfg_single_target() { .with_stdout_contains( "\ debug_assertions +panic=\"unwind\" target_arch=\"x86_64\" target_endian=\"little\" target_env=\"msvc\" @@ -543,6 +544,7 @@ target_vendor=\"unknown\" unix debug_assertions +panic=\"unwind\" target_arch=\"x86_64\" target_endian=\"little\" target_env=\"msvc\" @@ -587,6 +589,7 @@ fn rustc_with_print_cfg_rustflags_env_var() { .with_stdout_contains( "\ debug_assertions +panic=\"unwind\" target_arch=\"x86_64\" target_endian=\"little\" target_env=\"msvc\" @@ -640,6 +643,7 @@ rustflags = ["-C", "target-feature=+crt-static"] .with_stdout_contains( "\ debug_assertions +panic=\"unwind\" target_arch=\"x86_64\" target_endian=\"little\" target_env=\"msvc\" From 2a5355f9b3f2962286cd912245e9a3a0893c438f Mon Sep 17 00:00:00 2001 From: Chris Field Date: Sun, 21 Feb 2021 22:58:31 -0500 Subject: [PATCH 43/43] Fix usage of assert methods The `with_stdout_contains` was mis-used. Since some lines may or may not appear for some compiler targets and environments (nightly, beta, stable, etc.) the tests would fail because the output was not identical. Instead of a using raw strings, each line with the arch, endian, env, family, vendor, pointer_width, etc. that are known to always be present (at least of the CI builds) are included. This should work for the CI environments and my local environment. --- tests/testsuite/rustc.rs | 205 +++++++++------------------------------ 1 file changed, 44 insertions(+), 161 deletions(-) diff --git a/tests/testsuite/rustc.rs b/tests/testsuite/rustc.rs index ced40183053..53b5c6fb19a 100644 --- a/tests/testsuite/rustc.rs +++ b/tests/testsuite/rustc.rs @@ -468,39 +468,15 @@ fn rustc_with_print_cfg_single_target() { p.cargo("rustc -Z unstable-options --target x86_64-pc-windows-msvc --print cfg") .masquerade_as_nightly_cargo() - .with_stdout_contains( - "\ -debug_assertions -panic=\"unwind\" -target_arch=\"x86_64\" -target_endian=\"little\" -target_env=\"msvc\" -target_family=\"windows\" -target_feature=\"fxsr\" -target_feature=\"sse\" -target_feature=\"sse2\" -target_has_atomic=\"16\" -target_has_atomic=\"32\" -target_has_atomic=\"64\" -target_has_atomic=\"8\" -target_has_atomic=\"ptr\" -target_has_atomic_equal_alignment=\"16\" -target_has_atomic_equal_alignment=\"32\" -target_has_atomic_equal_alignment=\"64\" -target_has_atomic_equal_alignment=\"8\" -target_has_atomic_equal_alignment=\"ptr\" -target_has_atomic_load_store=\"16\" -target_has_atomic_load_store=\"32\" -target_has_atomic_load_store=\"64\" -target_has_atomic_load_store=\"8\" -target_has_atomic_load_store=\"ptr\" -target_os=\"windows\" -target_pointer_width=\"64\" -target_thread_local -target_vendor=\"pc\" -windows -", - ) + .with_stdout_contains("debug_assertions") + .with_stdout_contains("target_arch=\"x86_64\"") + .with_stdout_contains("target_endian=\"little\"") + .with_stdout_contains("target_env=\"msvc\"") + .with_stdout_contains("target_family=\"windows\"") + .with_stdout_contains("target_os=\"windows\"") + .with_stdout_contains("target_pointer_width=\"64\"") + .with_stdout_contains("target_vendor=\"pc\"") + .with_stdout_contains("windows") .run(); } @@ -513,66 +489,21 @@ fn rustc_with_print_cfg_multiple_targets() { p.cargo("rustc -Z unstable-options -Z multitarget --target x86_64-pc-windows-msvc --target i686-unknown-linux-gnu --print cfg") .masquerade_as_nightly_cargo() - .with_stdout_contains( - "\ -debug_assertions -target_arch=\"x86\" -target_endian=\"little\" -target_env=\"gnu\" -target_family=\"unix\" -target_feature=\"fxsr\" -target_feature=\"sse\" -target_feature=\"sse2\" -target_has_atomic=\"16\" -target_has_atomic=\"32\" -target_has_atomic=\"64\" -target_has_atomic=\"8\" -target_has_atomic=\"ptr\" -target_has_atomic_equal_alignment=\"16\" -target_has_atomic_equal_alignment=\"32\" -target_has_atomic_equal_alignment=\"8\" -target_has_atomic_equal_alignment=\"ptr\" -target_has_atomic_load_store=\"16\" -target_has_atomic_load_store=\"32\" -target_has_atomic_load_store=\"64\" -target_has_atomic_load_store=\"8\" -target_has_atomic_load_store=\"ptr\" -target_os=\"linux\" -target_pointer_width=\"32\" -target_thread_local -target_vendor=\"unknown\" -unix - -debug_assertions -panic=\"unwind\" -target_arch=\"x86_64\" -target_endian=\"little\" -target_env=\"msvc\" -target_family=\"windows\" -target_feature=\"fxsr\" -target_feature=\"sse\" -target_feature=\"sse2\" -target_has_atomic=\"16\" -target_has_atomic=\"32\" -target_has_atomic=\"64\" -target_has_atomic=\"8\" -target_has_atomic=\"ptr\" -target_has_atomic_equal_alignment=\"16\" -target_has_atomic_equal_alignment=\"32\" -target_has_atomic_equal_alignment=\"64\" -target_has_atomic_equal_alignment=\"8\" -target_has_atomic_equal_alignment=\"ptr\" -target_has_atomic_load_store=\"16\" -target_has_atomic_load_store=\"32\" -target_has_atomic_load_store=\"64\" -target_has_atomic_load_store=\"8\" -target_has_atomic_load_store=\"ptr\" -target_os=\"windows\" -target_pointer_width=\"64\" -target_thread_local -target_vendor=\"pc\" -windows", - ) + .with_stdout_contains("debug_assertions") + .with_stdout_contains("target_arch=\"x86_64\"") + .with_stdout_contains("target_endian=\"little\"") + .with_stdout_contains("target_env=\"msvc\"") + .with_stdout_contains("target_family=\"windows\"") + .with_stdout_contains("target_os=\"windows\"") + .with_stdout_contains("target_pointer_width=\"64\"") + .with_stdout_contains("target_vendor=\"pc\"") + .with_stdout_contains("windows") + .with_stdout_contains("target_env=\"gnu\"") + .with_stdout_contains("target_family=\"unix\"") + .with_stdout_contains("target_pointer_width=\"32\"") + .with_stdout_contains("target_vendor=\"unknown\"") + .with_stdout_contains("target_os=\"linux\"") + .with_stdout_contains("unix") .run(); } @@ -586,40 +517,16 @@ fn rustc_with_print_cfg_rustflags_env_var() { p.cargo("rustc -Z unstable-options --target x86_64-pc-windows-msvc --print cfg") .masquerade_as_nightly_cargo() .env("RUSTFLAGS", "-C target-feature=+crt-static") - .with_stdout_contains( - "\ -debug_assertions -panic=\"unwind\" -target_arch=\"x86_64\" -target_endian=\"little\" -target_env=\"msvc\" -target_family=\"windows\" -target_feature=\"crt-static\" -target_feature=\"fxsr\" -target_feature=\"sse\" -target_feature=\"sse2\" -target_has_atomic=\"16\" -target_has_atomic=\"32\" -target_has_atomic=\"64\" -target_has_atomic=\"8\" -target_has_atomic=\"ptr\" -target_has_atomic_equal_alignment=\"16\" -target_has_atomic_equal_alignment=\"32\" -target_has_atomic_equal_alignment=\"64\" -target_has_atomic_equal_alignment=\"8\" -target_has_atomic_equal_alignment=\"ptr\" -target_has_atomic_load_store=\"16\" -target_has_atomic_load_store=\"32\" -target_has_atomic_load_store=\"64\" -target_has_atomic_load_store=\"8\" -target_has_atomic_load_store=\"ptr\" -target_os=\"windows\" -target_pointer_width=\"64\" -target_thread_local -target_vendor=\"pc\" -windows -", - ) + .with_stdout_contains("debug_assertions") + .with_stdout_contains("target_arch=\"x86_64\"") + .with_stdout_contains("target_endian=\"little\"") + .with_stdout_contains("target_env=\"msvc\"") + .with_stdout_contains("target_family=\"windows\"") + .with_stdout_contains("target_feature=\"crt-static\"") + .with_stdout_contains("target_os=\"windows\"") + .with_stdout_contains("target_pointer_width=\"64\"") + .with_stdout_contains("target_vendor=\"pc\"") + .with_stdout_contains("windows") .run(); } @@ -640,39 +547,15 @@ rustflags = ["-C", "target-feature=+crt-static"] p.cargo("rustc -Z unstable-options --target x86_64-pc-windows-msvc --print cfg") .masquerade_as_nightly_cargo() .env("RUSTFLAGS", "-C target-feature=+crt-static") - .with_stdout_contains( - "\ -debug_assertions -panic=\"unwind\" -target_arch=\"x86_64\" -target_endian=\"little\" -target_env=\"msvc\" -target_family=\"windows\" -target_feature=\"crt-static\" -target_feature=\"fxsr\" -target_feature=\"sse\" -target_feature=\"sse2\" -target_has_atomic=\"16\" -target_has_atomic=\"32\" -target_has_atomic=\"64\" -target_has_atomic=\"8\" -target_has_atomic=\"ptr\" -target_has_atomic_equal_alignment=\"16\" -target_has_atomic_equal_alignment=\"32\" -target_has_atomic_equal_alignment=\"64\" -target_has_atomic_equal_alignment=\"8\" -target_has_atomic_equal_alignment=\"ptr\" -target_has_atomic_load_store=\"16\" -target_has_atomic_load_store=\"32\" -target_has_atomic_load_store=\"64\" -target_has_atomic_load_store=\"8\" -target_has_atomic_load_store=\"ptr\" -target_os=\"windows\" -target_pointer_width=\"64\" -target_thread_local -target_vendor=\"pc\" -windows -", - ) + .with_stdout_contains("debug_assertions") + .with_stdout_contains("target_arch=\"x86_64\"") + .with_stdout_contains("target_endian=\"little\"") + .with_stdout_contains("target_env=\"msvc\"") + .with_stdout_contains("target_family=\"windows\"") + .with_stdout_contains("target_feature=\"crt-static\"") + .with_stdout_contains("target_os=\"windows\"") + .with_stdout_contains("target_pointer_width=\"64\"") + .with_stdout_contains("target_vendor=\"pc\"") + .with_stdout_contains("windows") .run(); }