From d6a4c431f4ea819ffffed987167319f14e809955 Mon Sep 17 00:00:00 2001 From: Robert Buonpastore Date: Sun, 18 May 2014 23:37:07 -0400 Subject: [PATCH] Stabilize version output for rustc and rustdoc --- mk/main.mk | 10 ++++++++-- src/librustc/driver/config.rs | 2 +- src/librustc/driver/mod.rs | 31 +++++++++++++++++++++--------- src/librustdoc/lib.rs | 11 ++++++++--- src/test/run-make/version/Makefile | 8 ++++++++ 5 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 src/test/run-make/version/Makefile diff --git a/mk/main.mk b/mk/main.mk index b56f4d7a25f..3f6103bafa5 100644 --- a/mk/main.mk +++ b/mk/main.mk @@ -42,9 +42,9 @@ SPACE := SPACE += ifneq ($(wildcard $(subst $(SPACE),\$(SPACE),$(CFG_GIT))),) ifneq ($(wildcard $(subst $(SPACE),\$(SPACE),$(CFG_GIT_DIR))),) - CFG_VERSION += $(shell git --git-dir='$(CFG_GIT_DIR)' log -1 \ - --pretty=format:'(%h %ci)') + CFG_VER_DATE = $(shell git --git-dir='$(CFG_GIT_DIR)' log -1 --pretty=format:'%ci') CFG_VER_HASH = $(shell git --git-dir='$(CFG_GIT_DIR)' rev-parse HEAD) + CFG_VERSION += ($(CFG_VER_HASH) $(CFG_VER_DATE)) endif endif @@ -272,6 +272,12 @@ $(foreach host,$(CFG_HOST), \ export CFG_SRC_DIR export CFG_BUILD_DIR +ifdef CFG_VER_DATE +export CFG_VER_DATE +endif +ifdef CFG_VER_HASH +export CFG_VER_HASH +endif export CFG_VERSION export CFG_VERSION_WIN export CFG_RELEASE diff --git a/src/librustc/driver/config.rs b/src/librustc/driver/config.rs index d815e3d8a86..f14efb696c5 100644 --- a/src/librustc/driver/config.rs +++ b/src/librustc/driver/config.rs @@ -544,7 +544,7 @@ pub fn optgroups() -> Vec { optmulti("F", "forbid", "Set lint forbidden", "OPT"), optmulti("C", "codegen", "Set a codegen option", "OPT[=VALUE]"), optmulti("Z", "", "Set internal debugging options", "FLAG"), - optflag("v", "version", "Print version info and exit"), + optflagopt("v", "version", "Print version info and exit", "verbose"), optopt("", "color", "Configure coloring of output: auto = colorize, if output goes to a tty (default); always = always colorize output; diff --git a/src/librustc/driver/mod.rs b/src/librustc/driver/mod.rs index cfde4ad52af..87c0a270839 100644 --- a/src/librustc/driver/mod.rs +++ b/src/librustc/driver/mod.rs @@ -112,13 +112,24 @@ fn run_compiler(args: &[String]) { driver::compile_input(sess, cfg, &input, &odir, &ofile); } -pub fn version(command: &str) { - let vers = match option_env!("CFG_VERSION") { - Some(vers) => vers, - None => "unknown version" +/// Prints version information and returns None on success or an error +/// message on failure. +pub fn version(binary: &str, matches: &getopts::Matches) -> Option { + let verbose = match matches.opt_str("version").as_ref().map(|s| s.as_slice()) { + None => false, + Some("verbose") => true, + Some(s) => return Some(format!("Unrecognized argument: {}", s)) }; - println!("{} {}", command, vers); - println!("host: {}", driver::host_triple()); + + println!("{} {}", binary, env!("CFG_VERSION")); + if verbose { + println!("binary: {}", binary); + println!("commit-hash: {}", option_env!("CFG_VER_HASH").unwrap_or("unknown")); + println!("commit-date: {}", option_env!("CFG_VER_DATE").unwrap_or("unknown")); + println!("host: {}", driver::host_triple()); + println!("release: {}", env!("CFG_RELEASE")); + } + None } fn usage() { @@ -268,9 +279,11 @@ pub fn handle_options(mut args: Vec) -> Option { return None; } - if matches.opt_present("v") || matches.opt_present("version") { - version("rustc"); - return None; + if matches.opt_present("version") { + match version("rustc", &matches) { + Some(err) => early_error(err.as_slice()), + None => return None + } } Some(matches) diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index fdf38dc335c..00c5c2a218d 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -95,7 +95,7 @@ pub fn opts() -> Vec { use getopts::*; vec!( optflag("h", "help", "show this help message"), - optflag("", "version", "print rustdoc's version"), + optflagopt("", "version", "print rustdoc's version", "verbose"), optopt("r", "input-format", "the input type of the specified file", "[rust|json]"), optopt("w", "output-format", "the output type to write", @@ -150,8 +150,13 @@ pub fn main_args(args: &[String]) -> int { usage(args[0].as_slice()); return 0; } else if matches.opt_present("version") { - rustc::driver::version("rustdoc"); - return 0; + match rustc::driver::version("rustdoc", &matches) { + Some(err) => { + println!("{}", err); + return 1 + }, + None => return 0 + } } if matches.free.len() == 0 { diff --git a/src/test/run-make/version/Makefile b/src/test/run-make/version/Makefile new file mode 100644 index 00000000000..4950fe7572a --- /dev/null +++ b/src/test/run-make/version/Makefile @@ -0,0 +1,8 @@ +-include ../tools.mk + +all: + $(RUSTC) -v + $(RUSTC) -v verbose + $(RUSTC) -v bad_arg && exit 1 || exit 0 + $(RUSTC) --version verbose + $(RUSTC) --version bad_arg && exit 1 || exit 0