Skip to content

Commit d6a4c43

Browse files
Robert Buonpastorealexcrichton
Robert Buonpastore
authored andcommitted
Stabilize version output for rustc and rustdoc
1 parent fb296b8 commit d6a4c43

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
@@ -544,7 +544,7 @@ pub fn optgroups() -> Vec<getopts::OptGroup> {
544544
optmulti("F", "forbid", "Set lint forbidden", "OPT"),
545545
optmulti("C", "codegen", "Set a codegen option", "OPT[=VALUE]"),
546546
optmulti("Z", "", "Set internal debugging options", "FLAG"),
547-
optflag("v", "version", "Print version info and exit"),
547+
optflagopt("v", "version", "Print version info and exit", "verbose"),
548548
optopt("", "color", "Configure coloring of output:
549549
auto = colorize, if output goes to a tty (default);
550550
always = always colorize output;

src/librustc/driver/mod.rs

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

115-
pub fn version(command: &str) {
116-
let vers = match option_env!("CFG_VERSION") {
117-
Some(vers) => vers,
118-
None => "unknown version"
115+
/// Prints version information and returns None on success or an error
116+
/// message on failure.
117+
pub fn version(binary: &str, matches: &getopts::Matches) -> Option<String> {
118+
let verbose = match matches.opt_str("version").as_ref().map(|s| s.as_slice()) {
119+
None => false,
120+
Some("verbose") => true,
121+
Some(s) => return Some(format!("Unrecognized argument: {}", s))
119122
};
120-
println!("{} {}", command, vers);
121-
println!("host: {}", driver::host_triple());
123+
124+
println!("{} {}", binary, env!("CFG_VERSION"));
125+
if verbose {
126+
println!("binary: {}", binary);
127+
println!("commit-hash: {}", option_env!("CFG_VER_HASH").unwrap_or("unknown"));
128+
println!("commit-date: {}", option_env!("CFG_VER_DATE").unwrap_or("unknown"));
129+
println!("host: {}", driver::host_triple());
130+
println!("release: {}", env!("CFG_RELEASE"));
131+
}
132+
None
122133
}
123134

124135
fn usage() {
@@ -268,9 +279,11 @@ pub fn handle_options(mut args: Vec<String>) -> Option<getopts::Matches> {
268279
return None;
269280
}
270281

271-
if matches.opt_present("v") || matches.opt_present("version") {
272-
version("rustc");
273-
return None;
282+
if matches.opt_present("version") {
283+
match version("rustc", &matches) {
284+
Some(err) => early_error(err.as_slice()),
285+
None => return None
286+
}
274287
}
275288

276289
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)