Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

boulder: Add SOURCE_DATE_EPOCH #238

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions boulder/data/macros/arch/base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ actions :
set -e
set -x
TERM="dumb"; export TERM
SOURCE_DATE_EPOCH="%(sourcedateepoch)"; export SOURCE_DATE_EPOCH
PKG_CONFIG_PATH="%(pkgconfigpath)"; export PKG_CONFIG_PATH
CFLAGS="%(cflags)"; export CFLAGS
CGO_CFLAGS="%(cflags)"; export CGO_CFLAGS
Expand Down
2 changes: 2 additions & 0 deletions boulder/src/build/job/phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ impl Phase {

parser.add_definition("compiler_cache", "/mason/ccache");

parser.add_definition("sourcedateepoch", recipe.build_time.timestamp());

let path = if ccache {
"/usr/lib/ccache/bin:/usr/bin:/bin"
} else {
Expand Down
46 changes: 43 additions & 3 deletions boulder/src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
// SPDX-License-Identifier: MPL-2.0

use std::{
fs, io,
env, fs, io,
path::{Path, PathBuf},
process::Command,
};

use chrono::{DateTime, Utc};
use thiserror::Error;

use crate::architecture::{self, BuildTarget};
Expand All @@ -18,15 +20,22 @@ pub struct Recipe {
pub path: PathBuf,
pub source: String,
pub parsed: Parsed,
pub build_time: DateTime<Utc>,
}

impl Recipe {
pub fn load(path: impl AsRef<Path>) -> Result<Self, Error> {
let path = resolve_path(path)?;
let source = fs::read_to_string(&path)?;
let parsed = stone_recipe::from_str(&source)?;

Ok(Self { path, source, parsed })
let build_time = resolve_build_time(&path);

Ok(Self {
path,
source,
parsed,
build_time,
})
}

pub fn build_targets(&self) -> Vec<BuildTarget> {
Expand Down Expand Up @@ -101,6 +110,37 @@ pub fn resolve_path(path: impl AsRef<Path>) -> Result<PathBuf, Error> {
fs::canonicalize(&path).map_err(|_| Error::MissingRecipe(path))
}

fn resolve_build_time(path: &Path) -> DateTime<Utc> {
// Propagate SOURCE_DATE_EPOCH if set
if let Ok(epoch_env) = env::var("SOURCE_DATE_EPOCH") {
if let Ok(parsed) = epoch_env.parse::<i64>() {
if let Some(timestamp) = DateTime::from_timestamp(parsed, 0) {
return timestamp;
}
}
}

// If we are building from a git repo and have the git binary available to us then use the last commit timestamp
if let Some(recipe_dir) = path.parent() {
if let Ok(git_log) = Command::new("git")
.args(["log", "-1", "--format=\"%at\""])
.current_dir(recipe_dir)
.output()
{
if let Ok(stdout) = String::from_utf8(git_log.stdout) {
if let Ok(parsed) = stdout.replace(['\n', '"'], "").parse::<i64>() {
if let Some(timestamp) = DateTime::from_timestamp(parsed, 0) {
return timestamp;
}
}
}
}
}

// As a final fallback use the current time
Utc::now()
}

#[derive(Debug, Error)]
pub enum Error {
#[error("recipe file does not exist: {0:?}")]
Expand Down
Loading