Skip to content
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

Add code to generate build info code (in build.rs) #53

Merged
merged 5 commits into from
Apr 3, 2018
Merged
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
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ cache:
timeout: 600
directories:
- $HOME/.cargo
- $TRAVIS_BUILD_DIR/target/debug/deps
services:
- docker
jobs:
Expand All @@ -13,8 +12,6 @@ jobs:
script:
- docker pull cita/cita-build:latest
- bash '.ci-scripts/format'
- bash '.ci-scripts/test'
- bash '.ci-scripts/coverage'
- bash '.ci-scripts/test' && bash '.ci-scripts/coverage'
before_cache:
- sudo chown -R travis:travis $HOME/.cargo
- sudo chown -R travis:travis $TRAVIS_BUILD_DIR/target/debug/deps
2 changes: 2 additions & 0 deletions util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ panic_hook = {path = "../panic_hook"}
serde = "1.0"
serde_derive = "1.0"
toml = "0.4"
git2 = "0.7"
rustc_version = "0.2.0"

[features]
default = []
Expand Down
157 changes: 157 additions & 0 deletions util/src/build_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
use std::fs::File;
use std::io::Write;
use std::path::Path;

use git2::{DescribeFormatOptions, DescribeOptions, ErrorCode, Repository};
use rustc_version;

const ASCII_LOGO: &'static str = r#"
..-nnmmmmnn-..
.-nndNNNNNNmddddmmmho.
.smNNMMMNnn- :nnmhn.
.dNMMMMMNs--:nosnno-. ..dNn.. ....
.hNMMMMMMMNmmNNNMMMMMNmn ..nMNNmmmmmmmmdnn.
.mMMMMMMMMMMMMMMMMMMMMMMN: ..ohmNN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MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMd:. nMMMNo.
nNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNdo:...sMMMMNo
nNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNmmNMMMMMm
NMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMd
:mNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNms
mMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMm
._____. ._____. _. ._ ._____. ._____. ._. ._____. ._____.
| .___| |___. | | | | | |___. | |_____| |_| |___. | |_____|
| | ._. | | | |_| | ._. | | ._. ._____. ._. | | ._____.
| | | | |_| \_____/ | | |_/ | | | ,_, | | | |_/ |_____|
| |___. | | ._. ._. | | | | | | | | | | ._____.
|_____| |_| |_| |_| |_| |_| |_| |_| |_| |_____|
"#;

/// Get the commit ID of this repository
fn get_commit_id(repo: &Repository) -> Option<String> {
repo.revparse("HEAD")
.map(|revspec| revspec.from().map(|obj| format!("{}", obj.id())))
.unwrap_or(None)
}

/// Get the branch name of this repository
fn get_branch(repo: &Repository) -> Option<String> {
let head = match repo.head() {
Ok(head) => Some(head),
Err(ref e) if e.code() == ErrorCode::UnbornBranch || e.code() == ErrorCode::NotFound => None,
Err(_) => return None,
};
head.as_ref()
.and_then(|h| h.shorthand())
.map(|v| v.to_owned())
}

/// [Command]:
/// * git describe --abbrev=0 --tags
/// * git describe --dirty=-dev
fn get_describe(repo: &Repository, dirty: Option<&str>) -> Option<String> {
let mut describe_opt = DescribeOptions::new();
describe_opt.describe_tags();
repo.describe(&describe_opt)
.map(|describe| {
let mut format_opt = DescribeFormatOptions::new();
if let Some(dirty) = dirty {
format_opt.dirty_suffix(dirty);
} else {
format_opt.abbreviated_size(0);
}
describe.format(Some(&format_opt)).ok()
})
.ok()
.unwrap_or(None)
}

fn get_latest_tag(repo: &Repository) -> Option<String> {
get_describe(repo, None)
}

/// Generate the build info functions (The file will be used by `include!` macro)
pub fn gen_build_info(out_dir: &str, dest_name: &str) {
let dest_path = Path::new(&out_dir).join(dest_name);
let mut f = File::create(&dest_path).unwrap();

let (descr_dirty, tag, branch, commit_id) = match Repository::discover(".") {
Ok(repo) => (
get_describe(&repo, Some("-dev")),
get_latest_tag(&repo),
get_branch(&repo),
get_commit_id(&repo),
),
Err(_) => (None, None, None, None),
};

let (version, pre, commit_date) = {
let ver_meta = rustc_version::version_meta().unwrap();
let ver = &ver_meta.semver;
let pre = ver.pre.get(0).map(|id| format!("{}", id));
((ver.major, ver.minor, ver.patch), pre, ver_meta.commit_date)
};

let version_string = descr_dirty.clone().unwrap_or_else(|| {
let branch_string = branch.clone().unwrap_or_else(|| "unknown".to_owned());
let commit_id_string = commit_id.clone().unwrap_or_else(|| "unknown".to_owned());
format!("{}-{:.8}", branch_string, commit_id_string)
});
let pre_str = pre.as_ref().map(|x| &**x).unwrap_or("unknown");
let commit_date_str = commit_date.as_ref().map(|x| &**x).unwrap_or("unknown");
let rustc_str = format!(
"rustc {major}.{minor}.{patch}-{pre}-{commit_date}",
major = version.0,
minor = version.1,
patch = version.2,
pre = pre_str,
commit_date = commit_date_str,
);
let info_str = format!(
"{version}\n({rustc})\n{logo}",
version = version_string,
rustc = rustc_str,
logo = ASCII_LOGO,
).replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\n", "\\n");
let code = format!(
"
pub fn get_build_info_str(short: bool) -> &'static str {{
if short {{ \"{}\" }} else {{ \"{}\" }}
}}

pub fn get_build_info() -> (
&'static str, // ASCII Logo
Option<&'static str>, // git: describe --dirty=-dev
Option<&'static str>, // git: latest tag
Option<&'static str>, // git: current branch
Option<&'static str>, // git: current commit id
(u64, u64, u64), // rustc: version(major, minor, patch)
Option<&'static str>, // rustc: pre-release
Option<&'static str>, // rustc: commit_date
) {{
({:?}, {:?}, {:?}, {:?}, {:?}, {:?}, {:?}, {:?})
}}
",
version_string, info_str, ASCII_LOGO, descr_dirty, tag, branch, commit_id, version, pre, commit_date
);
f.write_all(code.as_bytes()).unwrap();
}
3 changes: 3 additions & 0 deletions util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern crate bigint;
#[cfg(feature = "blake2bhash")]
extern crate blake2b;
extern crate elastic_array;
extern crate git2;
extern crate heapsize;
extern crate itertools;
extern crate libc;
Expand All @@ -33,6 +34,7 @@ extern crate rlp;
extern crate rlp_derive;
extern crate rocksdb;
extern crate rustc_hex;
extern crate rustc_version;
extern crate sha3;
#[cfg(feature = "sm3hash")]
extern crate sm3;
Expand All @@ -46,6 +48,7 @@ extern crate toml;
extern crate uuid;

pub mod avl;
pub mod build_info;
pub mod merklehash;
pub mod hashable;
pub mod common;
Expand Down