Skip to content

Commit

Permalink
Stabilize version output for rustc and rustdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Buonpastore authored and alexcrichton committed Jun 25, 2014
1 parent fb296b8 commit d6a4c43
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
10 changes: 8 additions & 2 deletions mk/main.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/driver/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ pub fn optgroups() -> Vec<getopts::OptGroup> {
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;
Expand Down
31 changes: 22 additions & 9 deletions src/librustc/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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() {
Expand Down Expand Up @@ -268,9 +279,11 @@ pub fn handle_options(mut args: Vec<String>) -> Option<getopts::Matches> {
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)
Expand Down
11 changes: 8 additions & 3 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn opts() -> Vec<getopts::OptGroup> {
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",
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions src/test/run-make/version/Makefile
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d6a4c43

Please sign in to comment.