diff --git a/Cargo.lock b/Cargo.lock index 3b3e5b24beb641a8f68c3b920c71956d1447bb51..d9ce4fe3f8ac16c20a4ba924e2690c1075bb442e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -508,6 +508,7 @@ dependencies = [ "log", "parity-scale-codec 2.0.1", "parity-wasm 0.42.2", + "platforms", "pretty_assertions", "pwasm-utils", "regex", @@ -517,6 +518,7 @@ dependencies = [ "serde_json", "sp-core", "structopt", + "substrate-build-script-utils", "substrate-subxt", "tempfile", "toml", @@ -2262,6 +2264,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "platforms" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "989d43012e2ca1c4a02507c67282691a0a3207f9dc67cec596b43fe925b3d325" + [[package]] name = "polling" version = "2.0.2" @@ -3474,6 +3482,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "substrate-build-script-utils" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd540ba72520174c2c73ce96bf507eeba3cc8a481f58be92525b69110e1fa645" +dependencies = [ + "platforms", +] + [[package]] name = "substrate-subxt" version = "0.14.0" diff --git a/Cargo.toml b/Cargo.toml index 7e936e0622a697baa66723a0c5146d02a311e9e8..b3bfd1d64ecec0bdcf9448b6dde664697ebc5bd7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,6 +58,8 @@ funty = "=1.1.0" anyhow = "1.0.40" zip = { version = "0.5.11", default-features = false } walkdir = "2.3.2" +substrate-build-script-utils = "3.0.0" +platforms = "1.1" [dev-dependencies] assert_matches = "1.5.0" diff --git a/build.rs b/build.rs index 232f09765497c2d04e227360f9f7404c7548064e..dc79aa3f4c229610682acb84e38d62c14b24d4b7 100644 --- a/build.rs +++ b/build.rs @@ -15,18 +15,23 @@ // along with cargo-contract. If not, see . use std::{ + borrow::Cow, env, ffi::OsStr, fs::File, io::{prelude::*, Write}, iter::Iterator, path::{Path, PathBuf}, + process::Command, }; use anyhow::Result; use walkdir::WalkDir; use zip::{write::FileOptions, CompressionMethod, ZipWriter}; +use platforms::{TARGET_ARCH, TARGET_ENV, TARGET_OS}; +use substrate_build_script_utils::rerun_if_git_head_changed; + const DEFAULT_UNIX_PERMISSIONS: u32 = 0o755; fn main() { @@ -46,6 +51,9 @@ fn main() { dst_file.display() ); + generate_cargo_keys(); + rerun_if_git_head_changed(); + std::process::exit( match zip_dir(&template_dir, &dst_file, CompressionMethod::Stored) { Ok(_) => { @@ -109,3 +117,54 @@ fn zip_dir(src_dir: &Path, dst_file: &Path, method: CompressionMethod) -> Result Ok(()) } + +/// Generate the `cargo:` key output +fn generate_cargo_keys() { + let output = Command::new("git") + .args(&["rev-parse", "--short", "HEAD"]) + .output(); + + let commit = match output { + Ok(o) if o.status.success() => { + let sha = String::from_utf8_lossy(&o.stdout).trim().to_owned(); + Cow::from(sha) + } + Ok(o) => { + println!("cargo:warning=Git command failed with status: {}", o.status); + Cow::from("unknown") + } + Err(err) => { + println!("cargo:warning=Failed to execute git command: {}", err); + Cow::from("unknown") + } + }; + + println!( + "cargo:rustc-env=CARGO_CONTRACT_CLI_IMPL_VERSION={}", + get_version(&commit) + ) +} + +fn get_version(impl_commit: &str) -> 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_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(""), + ) +} diff --git a/src/main.rs b/src/main.rs index eeb90240f4e00fa59b1689b00196deec7f1d9013..3ce2b55d6b7d9ca02ffa007dcd6046b2863afb69 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,9 +41,11 @@ use structopt::{clap, StructOpt}; #[derive(Debug, StructOpt)] #[structopt(bin_name = "cargo")] +#[structopt(version = env!("CARGO_CONTRACT_CLI_IMPL_VERSION"))] pub(crate) enum Opts { /// Utilities to develop Wasm smart contracts. #[structopt(name = "contract")] + #[structopt(version = env!("CARGO_CONTRACT_CLI_IMPL_VERSION"))] #[structopt(setting = clap::AppSettings::UnifiedHelpMessage)] #[structopt(setting = clap::AppSettings::DeriveDisplayOrder)] #[structopt(setting = clap::AppSettings::DontCollapseArgsInUsage)]