Files
servo/components/devtools/build.rs
Jonathan Schwender ee72fca571 devtools: Respect SOURCE_DATE_EPOCH for build_id (#44459)
We are probably not anywhere close to reproducible builds, but we should
try to follow best practices where trivially possible.
See also: https://reproducible-builds.org/docs/source-date-epoch/

Firefox devtools expects the build_id to be provided in a datetime
specific format.
This PR also switches the time to UTC instead of `Local`, since there
seems to be no clear reason to use Local, and SOURCE_DATE_EPOCH is UTC.

Testing: No functional changes, the devtools build_id is not covered by
any tests.
Fixes: #44458

---------

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
2026-04-24 12:09:44 +00:00

34 lines
1.4 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::path::Path;
use std::{env, fs};
use chrono::{TimeZone, Utc};
fn main() {
println!("cargo:rerun-if-env-changed=SOURCE_DATE_EPOCH");
// Parsing as suggested on <https://reproducible-builds.org/docs/source-date-epoch/>
let now = match env::var("SOURCE_DATE_EPOCH") {
Ok(val) => Utc
.timestamp_opt(
val.parse::<i64>()
.expect("SOURCE_DATE_EPOCH should be a valid integer"),
0,
)
.unwrap(),
Err(_) => Utc::now(),
};
let build_id = now.format("%Y%m%d%H%M%S").to_string();
let path = Path::new(&env::var_os("OUT_DIR").unwrap()).join("build_id.rs");
// The build ID is used in Firefox devtools, `getDateFromBuildID` function:
// <https://searchfox.org/firefox-main/rev/be31b3948198286e39a9855e414823cb17b6e94c/devtools/client/shared/remote-debugging/version-checker.js#21-24>
// The expected format is: yyyyMMddHHmmss.
// The date is than later used to check devtools compatibility:
// <https://searchfox.org/firefox-main/rev/be31b3948198286e39a9855e414823cb17b6e94c/devtools/client/shared/remote-debugging/version-checker.js#133-139>
fs::write(path, format!("const BUILD_ID: &str = \"{build_id}\";")).unwrap();
}