helper: add constants & COMMIT (#64)

* helper: add `build.rs`

* helper: add `constants` feature

* helper: add `constants.rs`

* helper: use `.as_bytes()` for commit hash length check

* helper: `to_lowercase()` and `trim()` to `COMMIT`
This commit is contained in:
hinto-janai 2024-02-15 16:44:43 -05:00 committed by GitHub
parent 354ac9c2f6
commit a58d33b95e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 70 additions and 7 deletions

View file

@ -10,10 +10,11 @@ repository = "https://github.com/Cuprate/cuprate/tree/main/consensus"
[features] [features]
# All features on by default. # All features on by default.
default = ["std", "atomic", "asynch", "num", "time", "thread"] default = ["std", "atomic", "asynch", "num", "time", "thread", "constants"]
std = [] std = []
atomic = ["dep:crossbeam"] atomic = ["dep:crossbeam"]
asynch = ["dep:futures", "dep:rayon"] asynch = ["dep:futures", "dep:rayon"]
constants = []
num = [] num = []
time = ["dep:chrono", "std"] time = ["dep:chrono", "std"]
thread = ["std", "dep:target_os_lib"] thread = ["std", "dep:target_os_lib"]

32
helper/build.rs Normal file
View file

@ -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}");
}

22
helper/src/constants.rs Normal file
View file

@ -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 {}

View file

@ -36,13 +36,21 @@
//---------------------------------------------------------------------------------------------------- Public API //---------------------------------------------------------------------------------------------------- Public API
#[cfg(feature = "asynch")] #[cfg(feature = "asynch")]
pub mod asynch; // async collides pub mod asynch; // async collides
#[cfg(feature = "atomic")] #[cfg(feature = "atomic")]
pub mod atomic; pub mod atomic;
#[cfg(feature = "constants")]
pub mod constants;
pub mod network; pub mod network;
#[cfg(feature = "num")] #[cfg(feature = "num")]
pub mod num; pub mod num;
#[cfg(feature = "thread")] #[cfg(feature = "thread")]
pub mod thread; pub mod thread;
#[cfg(feature = "time")] #[cfg(feature = "time")]
pub mod time; pub mod time;