Skip to content

Commit

Permalink
add build-script-utils from Substrate (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Mar 25, 2021
1 parent 11175f6 commit d81bd25
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ members = [
"xtokens",
"xcm-support",
"unknown-tokens",
"build-script-utils",
]
resolver = "2"

Expand Down
14 changes: 14 additions & 0 deletions build-script-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "orml-build-script-utils"
description = "Crate with utility functions for `build.rs` scripts."
repository = "https://github.com/open-web3-stack/open-runtime-module-library/tree/master/build-script-utils"
license = "Apache-2.0"
version = "0.4.1-dev"
authors = ["Parity Technologies <[email protected]>", "Laminar Developers <[email protected]>"]
edition = "2018"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
platforms = "1.1"
3 changes: 3 additions & 0 deletions build-script-utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Crate with utility functions for `build.rs` scripts.

License: Apache-2.0
5 changes: 5 additions & 0 deletions build-script-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! Crate with utility functions for `build.rs` scripts.

mod version;

pub use version::*;
55 changes: 55 additions & 0 deletions build-script-utils/src/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use platforms::*;
use std::process::Command;

/// Generate the `cargo:` key output
pub fn generate_cargo_keys() {
println!(
"cargo:rustc-env=SUBSTRATE_CLI_IMPL_VERSION={}",
get_version(get_commit())
)
}

fn get_platform() -> String {
let env_dash = if TARGET_ENV.is_some() { "-" } else { "" };

format!(
"{}-{}{}{}",
TARGET_ARCH.as_str(),
TARGET_OS.as_str(),
env_dash,
TARGET_ENV.map(|x| x.as_str()).unwrap_or(""),
)
}

fn get_version(impl_commit: String) -> String {
let commit_dash = if impl_commit.is_empty() { "" } else { "-" };

format!(
"{}{}{}-{}",
std::env::var("CARGO_PKG_VERSION").unwrap_or_default(),
commit_dash,
impl_commit,
get_platform(),
)
}

fn get_commit() -> String {
let commit = std::env::var("GIT_COMMIT").unwrap_or_default();
if !commit.is_empty() {
return commit;
}

let output = Command::new("git").args(&["rev-parse", "--short", "HEAD"]).output();

match output {
Ok(o) if o.status.success() => String::from_utf8_lossy(&o.stdout).trim().into(),
Ok(o) => {
println!("cargo:warning=Git command failed with status: {}", o.status);
"unknown".into()
}
Err(err) => {
println!("cargo:warning=Failed to execute git command: {}", err);
"unknown".into()
}
}
}

0 comments on commit d81bd25

Please sign in to comment.