Skip to main content

bootstrap/utils/
build_stamp.rs

1//! Module for managing build stamp files.
2//!
3//! Contains the core implementation of how bootstrap utilizes stamp files on build processes.
4
5use 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/// Manages a stamp file to track build state. The file is created in the given
21/// directory and can have custom content and name.
22#[derive(Clone)]
23pub struct BuildStamp {
24    path: PathBuf,
25    stamp: String,
26}
27
28impl BuildStamp {
29    /// Creates a new `BuildStamp` for a given directory.
30    ///
31    /// By default, stamp will be an empty file named `.stamp` within the specified directory.
32    pub fn new(dir: &Path) -> Self {
33        // Avoid using `is_dir()` as the directory may not exist yet.
34        // It is more appropriate to assert that the path is not a file.
35        assert!(!dir.is_file(), "can't be a file path");
36        Self { path: dir.join(".stamp"), stamp: String::new() }
37    }
38
39    /// Returns path of the stamp file.
40    pub fn path(&self) -> &Path {
41        &self.path
42    }
43
44    /// Returns the value of the stamp.
45    ///
46    /// Note that this is empty by default and is populated using `BuildStamp::add_stamp`.
47    /// It is not read from an actual file, but rather it holds the value that will be used
48    /// when `BuildStamp::write` is called.
49    pub fn stamp(&self) -> &str {
50        &self.stamp
51    }
52
53    /// Adds specified stamp content to the current value.
54    ///
55    /// This method can be used incrementally e.g., `add_stamp("x").add_stamp("y").add_stamp("z")`.
56    pub fn add_stamp<S: ToString>(mut self, stamp: S) -> Self {
57        self.stamp.push_str(&stamp.to_string());
58        self
59    }
60
61    /// Adds a prefix to stamp's name.
62    ///
63    /// Prefix cannot start or end with a dot (`.`).
64    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    /// Removes the stamp file if it exists.
78    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    /// Creates the stamp file.
92    pub fn write(&self) -> io::Result<()> {
93        fs::write(&self.path, &self.stamp)
94    }
95
96    /// Checks if the stamp file is up-to-date.
97    ///
98    /// It is considered up-to-date if file content matches with the stamp string.
99    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
110/// Clear out `dir` if `input` is newer.
111///
112/// After this executes, it will also ensure that `dir` exists.
113pub 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
128/// Cargo's output path for librustc_codegen_llvm in a given stage, compiled by a particular
129/// compiler for the specified target and backend.
130pub 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
140/// Cargo's output path for the standard library in a given stage, compiled
141/// by a particular `build_compiler` for the specified `target`.
142pub 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
150/// Cargo's output path for librustc in a given stage, compiled by a particular
151/// `build_compiler` for the specified target.
152pub 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
160/// Computes a hash representing the state of a repository/submodule and additional input.
161///
162/// It uses `git diff` for the actual changes, and `git status` for including the untracked
163/// files in the specified directory. The additional input is also incorporated into the
164/// computation of the hash.
165///
166/// # Parameters
167///
168/// - `dir`: A reference to the directory path of the target repository/submodule.
169/// - `additional_input`: An additional input to be included in the hash.
170///
171/// # Panics
172///
173/// In case of errors during `git` command execution (e.g., in tarball sources), default values
174/// are used to prevent panics.
175pub 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}