mirror of
https://github.com/Cuprate/cuprate.git
synced 2024-12-22 19:49:28 +00:00
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:
parent
354ac9c2f6
commit
a58d33b95e
4 changed files with 70 additions and 7 deletions
|
@ -10,13 +10,14 @@ 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"]
|
||||||
num = []
|
constants = []
|
||||||
time = ["dep:chrono", "std"]
|
num = []
|
||||||
thread = ["std", "dep:target_os_lib"]
|
time = ["dep:chrono", "std"]
|
||||||
|
thread = ["std", "dep:target_os_lib"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
crossbeam = { workspace = true, optional = true }
|
crossbeam = { workspace = true, optional = true }
|
||||||
|
|
32
helper/build.rs
Normal file
32
helper/build.rs
Normal 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
22
helper/src/constants.rs
Normal 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 {}
|
|
@ -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;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue