Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e50eac5

Browse files
committedFeb 8, 2016
rustc: Implement a new --print cfg flag
This commit is an implementation of the new compiler flags required by [RFC 1361][rfc]. This specifically adds a new `cfg` option to the `--print` flag to the compiler. This new directive will print the defined `#[cfg]` directives by the compiler for the target in question. [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1361-cargo-cfg-dependencies.md
1 parent 26105b1 commit e50eac5

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed
 

‎src/librustc/session/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ pub enum PrintRequest {
163163
FileNames,
164164
Sysroot,
165165
CrateName,
166+
Cfg,
166167
}
167168

168169
pub enum Input {
@@ -1105,6 +1106,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
11051106
"crate-name" => PrintRequest::CrateName,
11061107
"file-names" => PrintRequest::FileNames,
11071108
"sysroot" => PrintRequest::Sysroot,
1109+
"cfg" => PrintRequest::Cfg,
11081110
req => {
11091111
early_error(error_format, &format!("unknown print request `{}`", req))
11101112
}

‎src/librustc_driver/lib.rs

+19
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,25 @@ impl RustcDefaultCalls {
518518
.to_string_lossy());
519519
}
520520
}
521+
PrintRequest::Cfg => {
522+
for cfg in config::build_configuration(sess) {
523+
match cfg.node {
524+
ast::MetaWord(ref word) => println!("{}", word),
525+
ast::MetaNameValue(ref name, ref value) => {
526+
println!("{}=\"{}\"", name, match value.node {
527+
ast::LitStr(ref s, _) => s,
528+
_ => continue,
529+
});
530+
}
531+
// Right now there are not and should not be any
532+
// MetaList items in the configuration returned by
533+
// `build_configuration`.
534+
ast::MetaList(..) => {
535+
panic!("MetaList encountered in default cfg")
536+
}
537+
}
538+
}
539+
}
521540
}
522541
}
523542
return Compilation::Stop;

‎src/test/run-make/print-cfg/Makefile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-include ../tools.mk
2+
3+
all: default
4+
rustc --target x86_64-pc-windows-gnu --print cfg | grep windows
5+
rustc --target x86_64-pc-windows-gnu --print cfg | grep x86_64
6+
rustc --target i686-pc-windows-msvc --print cfg | grep msvc
7+
rustc --target i686-apple-darwin --print cfg | grep macos
8+
9+
ifdef IS_WINDOWS
10+
default:
11+
rustc --print cfg | grep windows
12+
else
13+
default:
14+
rustc --print cfg | grep unix
15+
endif

0 commit comments

Comments
 (0)
Please sign in to comment.