bootstrap/utils/
build_stamp.rs1use std::path::{Path, PathBuf};
6use std::{fs, io};
7
8use sha2::digest::Digest;
9
10use crate::Mode;
11use crate::core::backend::CodegenBackendKind;
12use crate::core::builder::Builder;
13use crate::core::compiler::Compiler;
14use crate::core::config::TargetSelection;
15use crate::utils::helpers::{self, hex_encode, mtime, t};
16
17#[cfg(test)]
18mod tests;
19
20#[derive(Clone)]
23pub struct BuildStamp {
24 path: PathBuf,
25 stamp: String,
26}
27
28impl BuildStamp {
29 pub fn new(dir: &Path) -> Self {
33 assert!(!dir.is_file(), "can't be a file path");
36 Self { path: dir.join(".stamp"), stamp: String::new() }
37 }
38
39 pub fn path(&self) -> &Path {
41 &self.path
42 }
43
44 pub fn stamp(&self) -> &str {
50 &self.stamp
51 }
52
53 pub fn add_stamp<S: ToString>(mut self, stamp: S) -> Self {
57 self.stamp.push_str(&stamp.to_string());
58 self
59 }
60
61 pub fn with_prefix(mut self, prefix: &str) -> Self {
65 assert!(
66 !prefix.starts_with('.') && !prefix.ends_with('.'),
67 "prefix can not start or end with '.'"
68 );
69
70 let stamp_filename = self.path.file_name().unwrap().to_str().unwrap();
71 let stamp_filename = stamp_filename.strip_prefix('.').unwrap_or(stamp_filename);
72 self.path.set_file_name(format!(".{prefix}-{stamp_filename}"));
73
74 self
75 }
76
77 pub fn remove(&self) -> io::Result<()> {
79 match fs::remove_file(&self.path) {
80 Ok(()) => Ok(()),
81 Err(e) => {
82 if e.kind() == io::ErrorKind::NotFound {
83 Ok(())
84 } else {
85 Err(e)
86 }
87 }
88 }
89 }
90
91 pub fn write(&self) -> io::Result<()> {
93 fs::write(&self.path, &self.stamp)
94 }
95
96 pub fn is_up_to_date(&self) -> bool {
100 match fs::read(&self.path) {
101 Ok(h) => self.stamp.as_bytes() == h.as_slice(),
102 Err(e) if e.kind() == io::ErrorKind::NotFound => false,
103 Err(e) => {
104 panic!("failed to read stamp file `{}`: {}", self.path.display(), e);
105 }
106 }
107 }
108}
109
110pub fn clear_if_dirty(builder: &Builder<'_>, dir: &Path, input: &Path) -> bool {
114 let stamp = BuildStamp::new(dir);
115 let mut cleared = false;
116 if mtime(stamp.path()) < mtime(input) {
117 builder.do_if_verbose(|| println!("Dirty - {}", dir.display()));
118 let _ = fs::remove_dir_all(dir);
119 cleared = true;
120 } else if stamp.path().exists() {
121 return cleared;
122 }
123 t!(fs::create_dir_all(dir));
124 t!(fs::File::create(stamp.path()));
125 cleared
126}
127
128pub fn codegen_backend_stamp(
131 builder: &Builder<'_>,
132 compiler: Compiler,
133 target: TargetSelection,
134 backend: &CodegenBackendKind,
135) -> BuildStamp {
136 BuildStamp::new(&builder.cargo_out(compiler, Mode::Codegen, target))
137 .with_prefix(&format!("lib{}", backend.crate_name()))
138}
139
140pub fn libstd_stamp(
143 builder: &Builder<'_>,
144 build_compiler: Compiler,
145 target: TargetSelection,
146) -> BuildStamp {
147 BuildStamp::new(&builder.cargo_out(build_compiler, Mode::Std, target)).with_prefix("libstd")
148}
149
150pub fn librustc_stamp(
153 builder: &Builder<'_>,
154 build_compiler: Compiler,
155 target: TargetSelection,
156) -> BuildStamp {
157 BuildStamp::new(&builder.cargo_out(build_compiler, Mode::Rustc, target)).with_prefix("librustc")
158}
159
160pub fn generate_smart_stamp_hash(
176 builder: &Builder<'_>,
177 dir: &Path,
178 additional_input: &str,
179) -> String {
180 let diff = helpers::git(Some(dir))
181 .allow_failure()
182 .arg("diff")
183 .arg(".")
184 .run_capture_stdout(builder)
185 .stdout_if_ok()
186 .unwrap_or_default();
187
188 let status = helpers::git(Some(dir))
189 .allow_failure()
190 .arg("status")
191 .arg(".")
192 .arg("--porcelain")
193 .arg("-z")
194 .arg("--untracked-files=normal")
195 .run_capture_stdout(builder)
196 .stdout_if_ok()
197 .unwrap_or_default();
198
199 let mut hasher = sha2::Sha256::new();
200
201 hasher.update(diff);
202 hasher.update(status);
203 hasher.update(additional_input);
204
205 hex_encode(hasher.finalize().as_slice())
206}