Skip to content

Commit 1dfb23f

Browse files
committed
librustc/driver: expose build information of rustc.
CFG_RELEASE, CFG_VER_HASH and CFG_VER_DATE were only available as an output to stdout from the driver::version() function that had an inconvenient signature.
1 parent d2f8d4c commit 1dfb23f

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/librustc/driver/mod.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ fn run_compiler(args: &[String]) {
128128
driver::compile_input(sess, cfg, &input, &odir, &ofile, None);
129129
}
130130

131+
/// Returns a version string such as "0.12.0-dev".
132+
pub fn release_str() -> Option<&'static str> {
133+
option_env!("CFG_RELEASE")
134+
}
135+
136+
/// Returns the full SHA1 hash of HEAD of the Git repo from which rustc was built.
137+
pub fn commit_hash_str() -> Option<&'static str> {
138+
option_env!("CFG_VER_HASH")
139+
}
140+
141+
/// Returns the "commit date" of HEAD of the Git repo from which rustc was built as a static string.
142+
pub fn commit_date_str() -> Option<&'static str> {
143+
option_env!("CFG_VER_DATE")
144+
}
145+
131146
/// Prints version information and returns None on success or an error
132147
/// message on failure.
133148
pub fn version(binary: &str, matches: &getopts::Matches) -> Option<String> {
@@ -137,13 +152,14 @@ pub fn version(binary: &str, matches: &getopts::Matches) -> Option<String> {
137152
Some(s) => return Some(format!("Unrecognized argument: {}", s))
138153
};
139154

140-
println!("{} {}", binary, env!("CFG_VERSION"));
155+
println!("{} {}", binary, option_env!("CFG_VERSION").unwrap_or("unknown version"));
141156
if verbose {
157+
fn unw(x: Option<&str>) -> &str { x.unwrap_or("unknown") }
142158
println!("binary: {}", binary);
143-
println!("commit-hash: {}", option_env!("CFG_VER_HASH").unwrap_or("unknown"));
144-
println!("commit-date: {}", option_env!("CFG_VER_DATE").unwrap_or("unknown"));
159+
println!("commit-hash: {}", unw(commit_hash_str()));
160+
println!("commit-date: {}", unw(commit_date_str()));
145161
println!("host: {}", driver::host_triple());
146-
println!("release: {}", env!("CFG_RELEASE"));
162+
println!("release: {}", unw(release_str()));
147163
}
148164
None
149165
}

0 commit comments

Comments
 (0)