diff --git a/helper/Cargo.toml b/helper/Cargo.toml index 34feb99..0c7741b 100644 --- a/helper/Cargo.toml +++ b/helper/Cargo.toml @@ -10,13 +10,14 @@ repository = "https://github.com/Cuprate/cuprate/tree/main/consensus" [features] # All features on by default. -default = ["std", "atomic", "asynch", "num", "time", "thread"] -std = [] -atomic = ["dep:crossbeam"] -asynch = ["dep:futures", "dep:rayon"] -num = [] -time = ["dep:chrono", "std"] -thread = ["std", "dep:target_os_lib"] +default = ["std", "atomic", "asynch", "num", "time", "thread", "constants"] +std = [] +atomic = ["dep:crossbeam"] +asynch = ["dep:futures", "dep:rayon"] +constants = [] +num = [] +time = ["dep:chrono", "std"] +thread = ["std", "dep:target_os_lib"] [dependencies] crossbeam = { workspace = true, optional = true } diff --git a/helper/build.rs b/helper/build.rs new file mode 100644 index 0000000..709db42 --- /dev/null +++ b/helper/build.rs @@ -0,0 +1,32 @@ +fn main() { + #[cfg(feature = "constants")] + set_commit_env(); +} + +#[cfg(feature = "constants")] +/// This sets the git `COMMIT` environment variable. +fn set_commit_env() { + const PATH: &str = "../.git/refs/heads/"; + + println!("cargo:rerun-if-changed={PATH}"); + + // FIXME: This could also be `std::fs::read({PATH}/{branch})` + // so the machine building doesn't need `git`, although: + // 1. Having `git` as a build dependency is probably ok + // 2. It causes issues on PRs that aren't the `main` branch + let output = std::process::Command::new("git") + .arg("rev-parse") + .arg("HEAD") + .output() + .unwrap(); + + let commit = std::str::from_utf8(&output.stdout) + .unwrap() + .trim() + .to_lowercase(); + + // Commit hash should always be 40 bytes long. + assert_eq!(commit.as_bytes().len(), 40); + + println!("cargo:rustc-env=COMMIT={commit}"); +} diff --git a/helper/src/constants.rs b/helper/src/constants.rs new file mode 100644 index 0000000..ca8c3dc --- /dev/null +++ b/helper/src/constants.rs @@ -0,0 +1,22 @@ +//! General `const`ants and `static`s. +//! +//! `#[no_std]` compatible. + +//---------------------------------------------------------------------------------------------------- Use +/// The current commit hash of the root Cuprate repository. +/// +/// # Case & length +/// It is guaranteed that `COMMIT` will be: +/// - Lowercase +/// - 40 characters long (no newline) +/// +/// ```rust +/// # use cuprate_helper::constants::*; +/// assert_eq!(COMMIT.as_bytes().len(), 40); +/// assert_eq!(COMMIT.to_lowercase(), COMMIT); +/// ``` +pub const COMMIT: &str = core::env!("COMMIT"); // Set in `helper/build.rs`. + +//---------------------------------------------------------------------------------------------------- Tests +#[cfg(test)] +mod test {} diff --git a/helper/src/lib.rs b/helper/src/lib.rs index fe118bb..1bd87c4 100644 --- a/helper/src/lib.rs +++ b/helper/src/lib.rs @@ -36,13 +36,21 @@ //---------------------------------------------------------------------------------------------------- Public API #[cfg(feature = "asynch")] pub mod asynch; // async collides + #[cfg(feature = "atomic")] pub mod atomic; + +#[cfg(feature = "constants")] +pub mod constants; + pub mod network; + #[cfg(feature = "num")] pub mod num; + #[cfg(feature = "thread")] pub mod thread; + #[cfg(feature = "time")] pub mod time;