roughtime 0.1.0

A no_std-capable Roughtime secure time-sync client with pluggable crypto backends
Documentation
//! Computes the build-time anti-rollback floor.
//!
//! By default (the `build-time-floor` feature disabled) this writes a no-op `BUILD_FLOOR_SECS`
//! constant so the crate's embedded floor depends solely on the hardcoded constant in
//! `src/floor.rs`, keeping builds reproducible. With `build-time-floor` enabled, this queries
//! the build host's wall clock, ratchets the floor to the start of the current UTC day, and
//! refuses to compile if the host clock is impossibly behind the hardcoded floor.
//!
//! This file is a separate build-script compilation unit from the library crate. `rustc`'s
//! `[lints.rust]` table does not apply to it, but `cargo clippy` lints every target in the
//! package by default, including build scripts — so `expect`/`panic` are explicitly allowed
//! here (scoped to this file only) since a build script legitimately must abort on
//! unrecoverable setup failure. This does not weaken the library crate's own lint posture.

#![allow(clippy::expect_used, clippy::panic)]

use std::env;
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};

/// Must match `HARDCODED_FLOOR_SECS` in `src/floor.rs`: July 1, 2026 00:00:00 UTC.
const HARDCODED_FLOOR_SECS: u64 = 1_782_864_000;

const SECS_PER_DAY: u64 = 86_400;

fn main() {
    println!("cargo:rerun-if-env-changed=CARGO_FEATURE_BUILD_TIME_FLOOR");

    let build_floor_secs = if env::var_os("CARGO_FEATURE_BUILD_TIME_FLOOR").is_some() {
        let now_secs = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("build.rs: system clock is before the Unix epoch")
            .as_secs();
        let day_truncated = (now_secs / SECS_PER_DAY) * SECS_PER_DAY;

        assert!(
            day_truncated >= HARDCODED_FLOOR_SECS,
            "roughtime build.rs: build machine clock ({day_truncated}s, day-truncated UTC) is \
             before the hardcoded anti-rollback floor ({HARDCODED_FLOOR_SECS}s / 2026-07-01). \
             This indicates a broken or tampered build-host clock. Refusing to compile — fix \
             the build machine's clock and retry."
        );

        day_truncated
    } else {
        0
    };

    let out_dir = env::var_os("OUT_DIR").expect("build.rs: OUT_DIR not set by Cargo");
    let dest = Path::new(&out_dir).join("build_floor.rs");
    std::fs::write(
        &dest,
        format!("pub(crate) const BUILD_FLOOR_SECS: u64 = {build_floor_secs};\n"),
    )
    .expect("build.rs: failed to write build_floor.rs to OUT_DIR");
}