@@ -80,6 +80,37 @@ fn commit_info_from_git() -> Option<CommitInfo> {
8080 } )
8181}
8282
83+ // The rustc source tarball is meant to contain all the source code to build an exact copy of the
84+ // toolchain, but it doesn't include the git repository itself. It wouldn't thus be possible to
85+ // populate the version information with the commit hash and the commit date.
86+ //
87+ // To work around this, the rustc build process obtains the git information when creating the
88+ // source tarball and writes it to the `git-commit-info` file. The build process actually creates
89+ // at least *two* of those files, one for Rust as a whole (in the root of the tarball) and one
90+ // specifically for Cargo (in src/tools/cargo). This function loads that file.
91+ //
92+ // The file is a newline-separated list of full commit hash, short commit hash, and commit date.
93+ fn commit_info_from_rustc_source_tarball ( ) -> Option < CommitInfo > {
94+ let path = Path :: new ( "git-commit-info" ) ;
95+ if !path. exists ( ) {
96+ return None ;
97+ }
98+
99+ // Dependency tracking is a nice to have for this (git doesn't do it), so if the path is not
100+ // valid UTF-8 just avoid doing it rather than erroring out.
101+ if let Some ( utf8) = path. to_str ( ) {
102+ println ! ( "cargo:rerun-if-changed={utf8}" ) ;
103+ }
104+
105+ let content = std:: fs:: read_to_string ( & path) . ok ( ) ?;
106+ let mut parts = content. split ( '\n' ) . map ( |s| s. to_string ( ) ) ;
107+ Some ( CommitInfo {
108+ hash : parts. next ( ) ?,
109+ short_hash : parts. next ( ) ?,
110+ date : parts. next ( ) ?,
111+ } )
112+ }
113+
83114fn commit_info ( ) {
84115 // Var set by bootstrap whenever omit-git-hash is enabled in rust-lang/rust's config.toml.
85116 println ! ( "cargo:rerun-if-env-changed=CFG_OMIT_GIT_HASH" ) ;
@@ -89,7 +120,7 @@ fn commit_info() {
89120 return ;
90121 }
91122
92- let Some ( git) = commit_info_from_git ( ) else {
123+ let Some ( git) = commit_info_from_git ( ) . or_else ( commit_info_from_rustc_source_tarball ) else {
93124 return ;
94125 } ;
95126
0 commit comments