Skip to content

Commit 6e2b531

Browse files
author
Robert Buonpastore
committed
Stabilize version output for rustc and rustdoc
1 parent 40ca89e commit 6e2b531

File tree

5 files changed

+47
-15
lines changed

5 files changed

+47
-15
lines changed

mk/main.mk

+8-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ SPACE :=
4242
SPACE +=
4343
ifneq ($(wildcard $(subst $(SPACE),\$(SPACE),$(CFG_GIT))),)
4444
ifneq ($(wildcard $(subst $(SPACE),\$(SPACE),$(CFG_GIT_DIR))),)
45-
CFG_VERSION += $(shell git --git-dir='$(CFG_GIT_DIR)' log -1 \
46-
--pretty=format:'(%h %ci)')
45+
CFG_VER_DATE = $(shell git --git-dir='$(CFG_GIT_DIR)' log -1 --pretty=format:'%ci')
4746
CFG_VER_HASH = $(shell git --git-dir='$(CFG_GIT_DIR)' rev-parse HEAD)
47+
CFG_VERSION += ($(CFG_VER_HASH) $(CFG_VER_DATE))
4848
endif
4949
endif
5050

@@ -272,6 +272,12 @@ $(foreach host,$(CFG_HOST), \
272272

273273
export CFG_SRC_DIR
274274
export CFG_BUILD_DIR
275+
ifdef CFG_VER_DATE
276+
export CFG_VER_DATE
277+
endif
278+
ifdef CFG_VER_HASH
279+
export CFG_VER_HASH
280+
endif
275281
export CFG_VERSION
276282
export CFG_VERSION_WIN
277283
export CFG_RELEASE

src/librustc/driver/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ pub fn optgroups() -> Vec<getopts::OptGroup> {
538538
optmulti("F", "forbid", "Set lint forbidden", "OPT"),
539539
optmulti("C", "codegen", "Set a codegen option", "OPT[=VALUE]"),
540540
optmulti("Z", "", "Set internal debugging options", "FLAG"),
541-
optflag("v", "version", "Print version info and exit"),
541+
optflagopt("v", "version", "Print version info and exit", "verbose"),
542542
optopt("", "color", "Configure coloring of output:
543543
auto = colorize, if output goes to a tty (default);
544544
always = always colorize output;

src/librustc/driver/mod.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,24 @@ fn run_compiler(args: &[String]) {
104104
driver::compile_input(sess, cfg, &input, &odir, &ofile);
105105
}
106106

107-
pub fn version(command: &str) {
108-
let vers = match option_env!("CFG_VERSION") {
109-
Some(vers) => vers,
110-
None => "unknown version"
107+
/// Prints version information and returns None on success or an error
108+
/// message on failure.
109+
pub fn version(binary: &str, matches: &getopts::Matches) -> Option<String> {
110+
let verbose = match matches.opt_str("version").as_ref().map(|s| s.as_slice()) {
111+
None => false,
112+
Some("verbose") => true,
113+
Some(s) => return Some(format!("Unrecognized argument: {}", s))
111114
};
112-
println!("{} {}", command, vers);
113-
println!("host: {}", driver::host_triple());
115+
116+
println!("{} {}", binary, env!("CFG_VERSION"));
117+
if verbose {
118+
println!("binary: {}", binary);
119+
println!("commit-hash: {}", option_env!("CFG_VER_HASH").unwrap_or("unknown"));
120+
println!("commit-date: {}", option_env!("CFG_VER_DATE").unwrap_or("unknown"));
121+
println!("host: {}", driver::host_triple());
122+
println!("release: {}", env!("CFG_RELEASE"));
123+
}
124+
None
114125
}
115126

116127
fn usage() {
@@ -238,9 +249,11 @@ pub fn handle_options(mut args: Vec<String>) -> Option<getopts::Matches> {
238249
return None;
239250
}
240251

241-
if matches.opt_present("v") || matches.opt_present("version") {
242-
version("rustc");
243-
return None;
252+
if matches.opt_present("version") {
253+
match version("rustc", &matches) {
254+
Some(err) => early_error(err.as_slice()),
255+
None => return None
256+
}
244257
}
245258

246259
Some(matches)

src/librustdoc/lib.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub fn opts() -> Vec<getopts::OptGroup> {
9595
use getopts::*;
9696
vec!(
9797
optflag("h", "help", "show this help message"),
98-
optflag("", "version", "print rustdoc's version"),
98+
optflagopt("", "version", "print rustdoc's version", "verbose"),
9999
optopt("r", "input-format", "the input type of the specified file",
100100
"[rust|json]"),
101101
optopt("w", "output-format", "the output type to write",
@@ -150,8 +150,13 @@ pub fn main_args(args: &[String]) -> int {
150150
usage(args[0].as_slice());
151151
return 0;
152152
} else if matches.opt_present("version") {
153-
rustc::driver::version("rustdoc");
154-
return 0;
153+
match rustc::driver::version("rustdoc", &matches) {
154+
Some(err) => {
155+
println!("{}", err);
156+
return 1
157+
},
158+
None => return 0
159+
}
155160
}
156161

157162
if matches.free.len() == 0 {

src/test/run-make/version/Makefile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) -v
5+
$(RUSTC) -v verbose
6+
$(RUSTC) -v bad_arg && exit 1 || exit 0
7+
$(RUSTC) --version verbose
8+
$(RUSTC) --version bad_arg && exit 1 || exit 0

0 commit comments

Comments
 (0)