Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #9002 - volks73:feature-rustc-cfg-argument, r=alexcrichton
Add --cfg and --rustc-cfg flags to output compiler configuration This PR is my attempt to address #8923. I have added the `--cfg` flag to the `cargo rustc` subcommand and the `--rustc-cfg` flag to the `cargo build`, `cargo check`, `cargo test`, and `cargo bench` subcommands, respectively. Both versions of the flag, `--cfg` and `--rustc-cfg`, do the same thing, but I thought it looked weird for the `cargo rustc` subcommand to be `cargo rustc --rustc-cfg`. The following example invocations are possible, once stabilized: - `cargo rustc --cfg` - `cargo build --rustc-cfg` - `cargo check --rustc-cfg` - `cargo test --rustc-cfg` - `cargo bench --rustc-cfg` In each case, compilation is aborted and only compiler configuration is emitted. All of the context creation and configuration is still executed, but execution of the compilation job is aborted. Similar to the `--unit-graph` implementation, the `--cfg/--rustc-cfg` flag is hidden, marked as a "unstable", and requires the `-Z unstable-options` flag at the moment. A complete example invocation with this PR would be: ```bash $ cargo +nightly rustc -Z unstable-options --cfg ``` I am open to alternatives for the flag name. I have thought of `--compiler-cfg`, `--compile-cfg`, and `--target-cfg`, but I went with `--rustc-cfg` because it is the Rust compiler (rustc) configuration (cfg). The `--target-cfg` could be confusing because there are Cargo targets and Compiler targets. A lone `--cfg` for the build, check, test, and bench subcommands would also be ambiguous and configuration that is being displayed. Originally, I was only going to add the `--cfg` flag to the `cargo rustc` subcommand, but build, check, test, and bench all have the `--unit-graph` flag, and I used that flag's implementation and existence as a template for this implementation. I am not opposed to having just the `--cfg` flag for the `cargo rustc` subcommand as originally proposed in #8923. I discovered during my initial investigation to implement the feature and familiarization with the Cargo codebase, that as part of the build context creation and compilation process, Cargo internally calls the `rustc --print cfg` command for all targets and stores the output in the `RustcTargetData` type. It does this for the host and any explicitly defined targets for cross-compilation. Compilation features, such as `+crt-static`, added to the compilation process through Cargo environment variables or TOML configuration directives are added to the internal `rustc --print cfg` command call. So, the `--cfg/--rustc-cfg` just emits the contents of the `RustcTagetData` type as JSON. There is no need to call the `rustc --print cfg` command again. The implementation then becomes nearly identical to the `--unit-graph` implementation. The output is only in JSON, similar to the `--unit-graph` feature, instead of the key-value and name style of the `rustc --print cfg` output because it easily resolves issues related to multi-targets, explicit target (`--target <TRIPLE>`) versus host configurations, and cross compilation. Subcommands and other projects can parse the JSON to recreate the key-value and name style if desired based on a specific target or the host. Here is an example of the JSON output (formatted for humans, the actual output is condensed), which is also available as a comment in the `cargo::core::compiler::rustc_cfg` module: ```javascript { "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" } } } ``` I decided to remove the "target_" prefix from the relevant configurations to reduce "noise" in the output. Again, I am open to alternatives or suggestions on the JSON format.
- Loading branch information