feat: initialize Kurdistan SDK - independent fork of Polkadot SDK
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "substrate-build-script-utils"
|
||||
version = "11.0.0"
|
||||
authors.workspace = true
|
||||
edition.workspace = true
|
||||
license = "Apache-2.0"
|
||||
homepage.workspace = true
|
||||
repository.workspace = true
|
||||
description = "Crate with utility functions for `build.rs` scripts."
|
||||
readme = "README.md"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
targets = ["x86_64-unknown-linux-gnu"]
|
||||
@@ -0,0 +1,3 @@
|
||||
Crate with utility functions for `build.rs` scripts.
|
||||
|
||||
License: Apache-2.0
|
||||
@@ -0,0 +1,131 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use std::{
|
||||
env, fs,
|
||||
fs::File,
|
||||
io,
|
||||
io::Read,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
/// Make sure the calling `build.rs` script is rerun when `.git/HEAD` or the ref of `.git/HEAD`
|
||||
/// changed.
|
||||
///
|
||||
/// The file is searched from the `CARGO_MANIFEST_DIR` upwards. If the file can not be found,
|
||||
/// a warning is generated.
|
||||
pub fn rerun_if_git_head_changed() {
|
||||
let mut manifest_dir = PathBuf::from(
|
||||
env::var("CARGO_MANIFEST_DIR").expect("`CARGO_MANIFEST_DIR` is always set by cargo."),
|
||||
);
|
||||
let manifest_dir_copy = manifest_dir.clone();
|
||||
|
||||
while manifest_dir.parent().is_some() {
|
||||
match get_git_paths(&manifest_dir) {
|
||||
Err(err) => {
|
||||
eprintln!("cargo:warning=Unable to read the Git repository: {}", err);
|
||||
|
||||
return;
|
||||
},
|
||||
Ok(None) => {},
|
||||
Ok(Some(paths)) => {
|
||||
for p in paths {
|
||||
println!("cargo:rerun-if-changed={}", p.display());
|
||||
}
|
||||
|
||||
return;
|
||||
},
|
||||
}
|
||||
|
||||
manifest_dir.pop();
|
||||
}
|
||||
|
||||
println!(
|
||||
"cargo:warning=Could not find `.git/HEAD` searching from `{}` upwards!",
|
||||
manifest_dir_copy.display(),
|
||||
);
|
||||
}
|
||||
|
||||
// Code taken from https://github.com/rustyhorde/vergen/blob/8d522db8c8e16e26c0fc9ea8e6b0247cbf5cca84/src/output/envvar.rs
|
||||
fn get_git_paths(path: &Path) -> Result<Option<Vec<PathBuf>>, io::Error> {
|
||||
let git_dir_or_file = path.join(".git");
|
||||
|
||||
if let Ok(metadata) = fs::metadata(&git_dir_or_file) {
|
||||
if metadata.is_dir() {
|
||||
// Echo the HEAD path
|
||||
let git_head_path = git_dir_or_file.join("HEAD");
|
||||
|
||||
// Determine where HEAD points and echo that path also.
|
||||
let mut f = File::open(&git_head_path)?;
|
||||
let mut git_head_contents = String::new();
|
||||
f.read_to_string(&mut git_head_contents)?;
|
||||
let ref_vec: Vec<&str> = git_head_contents.split(": ").collect();
|
||||
|
||||
if ref_vec.len() == 2 {
|
||||
let current_head_file = ref_vec[1];
|
||||
let git_refs_path = git_dir_or_file.join(current_head_file);
|
||||
|
||||
Ok(Some(vec![git_head_path, git_refs_path]))
|
||||
} else {
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"You are most likely in a detached HEAD state",
|
||||
))
|
||||
}
|
||||
} else if metadata.is_file() {
|
||||
// We are in a worktree, so find out where the actual worktrees/<name>/HEAD file is.
|
||||
let mut git_file = File::open(&git_dir_or_file)?;
|
||||
let mut git_contents = String::new();
|
||||
git_file.read_to_string(&mut git_contents)?;
|
||||
let dir_vec: Vec<&str> = git_contents.split(": ").collect();
|
||||
let git_path = dir_vec[1].trim();
|
||||
|
||||
// Echo the HEAD psth
|
||||
let git_head_path = PathBuf::from(git_path).join("HEAD");
|
||||
|
||||
// Find out what the full path to the .git dir is.
|
||||
let mut actual_git_dir = PathBuf::from(git_path);
|
||||
actual_git_dir.pop();
|
||||
actual_git_dir.pop();
|
||||
|
||||
// Determine where HEAD points and echo that path also.
|
||||
let mut f = File::open(&git_head_path)?;
|
||||
let mut git_head_contents = String::new();
|
||||
f.read_to_string(&mut git_head_contents)?;
|
||||
let ref_vec: Vec<&str> = git_head_contents.split(": ").collect();
|
||||
|
||||
if ref_vec.len() == 2 {
|
||||
let current_head_file = ref_vec[1];
|
||||
let git_refs_path = actual_git_dir.join(current_head_file);
|
||||
|
||||
Ok(Some(vec![git_head_path, git_refs_path]))
|
||||
} else {
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"You are most likely in a detached HEAD state",
|
||||
))
|
||||
}
|
||||
} else {
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
"Invalid .git format (Not a directory or a file)",
|
||||
))
|
||||
}
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Crate with utility functions for `build.rs` scripts.
|
||||
|
||||
mod git;
|
||||
mod version;
|
||||
|
||||
pub use git::*;
|
||||
pub use version::*;
|
||||
@@ -0,0 +1,61 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use std::{borrow::Cow, process::Command};
|
||||
|
||||
/// Generate the `cargo:` key output
|
||||
pub fn generate_cargo_keys() {
|
||||
let commit = if let Ok(hash) = std::env::var("SUBSTRATE_CLI_GIT_COMMIT_HASH") {
|
||||
Cow::from(hash.trim().to_owned())
|
||||
} else {
|
||||
// We deliberately set the length here to `11` to ensure that
|
||||
// the emitted hash is always of the same length; otherwise
|
||||
// it can (and will!) vary between different build environments.
|
||||
match Command::new("git").args(["rev-parse", "--short=11", "HEAD"]).output() {
|
||||
Ok(o) if o.status.success() => {
|
||||
let sha = String::from_utf8_lossy(&o.stdout).trim().to_owned();
|
||||
Cow::from(sha)
|
||||
},
|
||||
Ok(o) => {
|
||||
let stderr = String::from_utf8_lossy(&o.stderr).trim().to_owned();
|
||||
println!(
|
||||
"cargo:warning=Git command failed with status '{}' with message: '{}'",
|
||||
o.status, stderr,
|
||||
);
|
||||
Cow::from("unknown")
|
||||
},
|
||||
Err(err) => {
|
||||
println!("cargo:warning=Failed to execute git command: {}", err);
|
||||
Cow::from("unknown")
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
println!("cargo:rustc-env=SUBSTRATE_CLI_COMMIT_HASH={commit}");
|
||||
println!("cargo:rustc-env=SUBSTRATE_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
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user