Skip to content

Stabilize version output for rustc and rustdoc #13816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -538,7 +538,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 @@ -104,13 +104,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 @@ -238,9 +249,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