gecko-dev/testing/geckodriver/build.rs
Andreas Tolfsen 4a968ad679 Bug 1375207 - Display hash from whence geckodriver came in --version; r=jgraham
Because we no longer can tag geckodriver releases in mozilla-central,
we need to include build information in the program itself.  In the
version information message displayed when passing the --version flag,
we now include the current tip's SHA1 and build date following the
version number.

This patch could be made simpler by dumping this information correctly
formatted into a text file in the output directory, but it was requested
in https://bugzilla.mozilla.org/show_bug.cgi?id=1374977 to also include
the version information in the log output, which means we need to access
it differently and in different places.

MozReview-Commit-ID: CbFQn7IV8ew

--HG--
extra : rebase_source : dcc38ba7b5f209e9878755d5d75b611e22b5253d
2017-06-22 16:28:18 +01:00

74 lines
1.9 KiB
Rust

/// Writes build information to ${OUT_DIR}/build-info.rs which is included in
/// the program during compilation:
///
/// ```no_run
/// const COMMIT_HASH: Option<&'static str> = Some("c31a366");
/// const COMMIT_DATE: Option<&'static str> = Some("1988-05-10");
/// ```
///
/// The values are `None` if running hg failed, e.g. if it is not installed or
/// if we are not in an hg repo.
use std::env;
use std::ffi::OsStr;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;
fn main() {
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let mut fh = File::create(out_dir.join("build-info.rs")).unwrap();
writeln!(
fh,
"const COMMIT_HASH: Option<&'static str> = {:?};",
commit_hash()
).unwrap();
writeln!(
fh,
"const COMMIT_DATE: Option<&'static str> = {:?};",
commit_date()
).unwrap();
}
fn commit_hash() -> Option<String> {
exec(&"hg", &["log", "-r.", "-T '{node|short}'"]).or_else(
|| {
exec(&"git", &["rev-parse", "HEAD"]).and_then(hg2git_sha)
},
)
}
fn commit_date() -> Option<String> {
exec(&"hg", &["log", "-r.", "-T '{date|isodate}'"]).or_else(|| {
exec(
&"git",
&["log", "-1", "--date=short", "--pretty=format:%cd"],
)
})
}
fn exec<S, I>(program: S, args: I) -> Option<String>
where
S: AsRef<OsStr>,
I: IntoIterator<Item = S>,
{
let mut cmd = Command::new(program);
for arg in args {
cmd.arg(arg.as_ref());
}
cmd.output()
.ok()
.and_then(|r| if r.status.success() {
Some(r.stdout)
} else {
None
})
.and_then(|o| String::from_utf8(o).ok())
.map(|s| s.trim_right().into())
}
fn hg2git_sha(hg_sha: String) -> Option<String> {
exec(&"git", &["cinnabar", "git2hg", &hg_sha])
}