Skip to content

Commit

Permalink
add support for reading git-commit-info
Browse files Browse the repository at this point in the history
  • Loading branch information
pietroalbini committed Apr 30, 2024
1 parent 80f9670 commit bd752e9
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,37 @@ fn commit_info_from_git() -> Option<CommitInfo> {
})
}

// The rustc source tarball is meant to contain all the source code to build an exact copy of the
// toolchain, but it doesn't include the git repository itself. It wouldn't thus be possible to
// populate the version information with the commit hash and the commit date.
//
// To work around this, the rustc build process obtains the git information when creating the
// source tarball and writes it to the `git-commit-info` file. The build process actually creates
// at least *two* of those files, one for Rust as a whole (in the root of the tarball) and one
// specifically for Cargo (in src/tools/cargo). This function loads that file.
//
// The file is a newline-separated list of full commit hash, short commit hash, and commit date.
fn commit_info_from_rustc_source_tarball() -> Option<CommitInfo> {
let path = Path::new("git-commit-info");
if !path.exists() {
return None;
}

// Dependency tracking is a nice to have for this (git doesn't do it), so if the path is not
// valid UTF-8 just avoid doing it rather than erroring out.
if let Some(utf8) = path.to_str() {
println!("cargo:rerun-if-changed={utf8}");
}

let content = std::fs::read_to_string(&path).ok()?;
let mut parts = content.split('\n').map(|s| s.to_string());
Some(CommitInfo {
hash: parts.next()?,
short_hash: parts.next()?,
date: parts.next()?,
})
}

fn commit_info() {
// Var set by bootstrap whenever omit-git-hash is enabled in rust-lang/rust's config.toml.
println!("cargo:rerun-if-env-changed=CFG_OMIT_GIT_HASH");
Expand All @@ -89,7 +120,7 @@ fn commit_info() {
return;
}

let Some(git) = commit_info_from_git() else {
let Some(git) = commit_info_from_git().or_else(commit_info_from_rustc_source_tarball) else {
return;
};

Expand Down

0 comments on commit bd752e9

Please sign in to comment.