Skip to content

Commit 5874ff8

Browse files
committed
avoid magical AsRef<Path> implementation
Signed-off-by: onur-ozkan <[email protected]>
1 parent b9ee554 commit 5874ff8

File tree

7 files changed

+37
-29
lines changed

7 files changed

+37
-29
lines changed

src/bootstrap/src/core/build_steps/clean.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ fn clean_default(build: &Build) {
147147
rm_rf(&build.out.join("dist"));
148148
rm_rf(&build.out.join("bootstrap").join(".last-warned-change-id"));
149149
rm_rf(&build.out.join("bootstrap-shims-dump"));
150-
rm_rf(BuildStamp::new(&build.out).with_prefix("rustfmt").as_ref());
150+
rm_rf(BuildStamp::new(&build.out).with_prefix("rustfmt").path());
151151

152152
let mut hosts: Vec<_> = build.hosts.iter().map(|t| build.out.join(t)).collect();
153153
// After cross-compilation, artifacts of the host architecture (which may differ from build.host)

src/bootstrap/src/core/build_steps/compile.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ impl Step for Rustc {
991991
true, // Only ship rustc_driver.so and .rmeta files, not all intermediate .rlib files.
992992
);
993993

994-
let target_root_dir = stamp.as_ref().parent().unwrap();
994+
let target_root_dir = stamp.path().parent().unwrap();
995995
// When building `librustc_driver.so` (like `libLLVM.so`) on linux, it can contain
996996
// unexpected debuginfo from dependencies, for example from the C++ standard library used in
997997
// our LLVM wrapper. Unless we're explicitly requesting `librustc_driver` to be built with
@@ -1478,7 +1478,7 @@ impl Step for CodegenBackend {
14781478
}
14791479
let stamp = build_stamp::codegen_backend_stamp(builder, compiler, target, &backend);
14801480
let codegen_backend = codegen_backend.to_str().unwrap();
1481-
t!(fs::write(stamp, codegen_backend));
1481+
t!(stamp.add_stamp(codegen_backend).write());
14821482
}
14831483
}
14841484

@@ -1516,7 +1516,7 @@ fn copy_codegen_backends_to_sysroot(
15161516
}
15171517

15181518
let stamp = build_stamp::codegen_backend_stamp(builder, compiler, target, backend);
1519-
let dylib = t!(fs::read_to_string(&stamp));
1519+
let dylib = t!(fs::read_to_string(stamp.path()));
15201520
let file = Path::new(&dylib);
15211521
let filename = file.file_name().unwrap().to_str().unwrap();
15221522
// change `librustc_codegen_cranelift-xxxxxx.so` to
@@ -2018,7 +2018,7 @@ pub fn run_cargo(
20182018
rlib_only_metadata: bool,
20192019
) -> Vec<PathBuf> {
20202020
// `target_root_dir` looks like $dir/$target/release
2021-
let target_root_dir = stamp.as_ref().parent().unwrap();
2021+
let target_root_dir = stamp.path().parent().unwrap();
20222022
// `target_deps_dir` looks like $dir/$target/release/deps
20232023
let target_deps_dir = target_root_dir.join("deps");
20242024
// `host_root_dir` looks like $dir/release
@@ -2171,7 +2171,7 @@ pub fn run_cargo(
21712171
new_contents.extend(dep.to_str().unwrap().as_bytes());
21722172
new_contents.extend(b"\0");
21732173
}
2174-
t!(fs::write(stamp, &new_contents));
2174+
t!(fs::write(stamp.path(), &new_contents));
21752175
deps.into_iter().map(|(d, _)| d).collect()
21762176
}
21772177

src/bootstrap/src/core/build_steps/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn update_rustfmt_version(build: &Builder<'_>) {
8282
let Some((version, stamp_file)) = get_rustfmt_version(build) else {
8383
return;
8484
};
85-
t!(std::fs::write(stamp_file, version))
85+
t!(std::fs::write(stamp_file.path(), version))
8686
}
8787

8888
/// Returns the Rust files modified between the `merge-base` of HEAD and

src/bootstrap/src/core/build_steps/gcc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ pub fn prebuilt_gcc_config(builder: &Builder<'_>, target: TargetSelection) -> Gc
5858
let stamp = BuildStamp::new(&out_dir).with_prefix("gcc").add_stamp(smart_stamp_hash);
5959

6060
if stamp.is_up_to_date() {
61-
if stamp.stamp.is_empty() {
61+
if stamp.stamp().is_empty() {
6262
builder.info(
6363
"Could not determine the GCC submodule commit hash. \
6464
Assuming that an GCC rebuild is not necessary.",
6565
);
6666
builder.info(&format!(
6767
"To force GCC to rebuild, remove the file `{}`",
68-
stamp.as_ref().display()
68+
stamp.path().display()
6969
));
7070
}
7171
return GccBuildStatus::AlreadyBuilt;

src/bootstrap/src/core/build_steps/llvm.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ pub fn prebuilt_llvm_config(
138138
let stamp = BuildStamp::new(&out_dir).with_prefix("llvm").add_stamp(smart_stamp_hash);
139139

140140
if stamp.is_up_to_date() {
141-
if stamp.stamp.is_empty() {
141+
if stamp.stamp().is_empty() {
142142
builder.info(
143143
"Could not determine the LLVM submodule commit hash. \
144144
Assuming that an LLVM rebuild is not necessary.",
145145
);
146146
builder.info(&format!(
147147
"To force LLVM to rebuild, remove the file `{}`",
148-
stamp.as_ref().display()
148+
stamp.path().display()
149149
));
150150
}
151151
return LlvmBuildStatus::AlreadyBuilt(res);
@@ -924,14 +924,14 @@ impl Step for Enzyme {
924924
let stamp = BuildStamp::new(&out_dir).with_prefix("enzyme").add_stamp(smart_stamp_hash);
925925

926926
if stamp.is_up_to_date() {
927-
if stamp.stamp.is_empty() {
927+
if stamp.stamp().is_empty() {
928928
builder.info(
929929
"Could not determine the Enzyme submodule commit hash. \
930930
Assuming that an Enzyme rebuild is not necessary.",
931931
);
932932
builder.info(&format!(
933933
"To force Enzyme to rebuild, remove the file `{}`",
934-
stamp.as_ref().display()
934+
stamp.path().display()
935935
));
936936
}
937937
return out_dir;
@@ -1016,7 +1016,7 @@ impl Step for Lld {
10161016
let out_dir = builder.lld_out(target);
10171017

10181018
let done_stamp = BuildStamp::new(&out_dir).with_prefix("lld");
1019-
if done_stamp.as_ref().exists() {
1019+
if done_stamp.path().exists() {
10201020
return out_dir;
10211021
}
10221022

@@ -1140,10 +1140,10 @@ impl Step for Sanitizers {
11401140
let stamp = BuildStamp::new(&out_dir).with_prefix("sanitizers").add_stamp(smart_stamp_hash);
11411141

11421142
if stamp.is_up_to_date() {
1143-
if stamp.stamp.is_empty() {
1143+
if stamp.stamp().is_empty() {
11441144
builder.info(&format!(
11451145
"Rebuild sanitizers by removing the file `{}`",
1146-
stamp.as_ref().display()
1146+
stamp.path().display()
11471147
));
11481148
}
11491149

src/bootstrap/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1607,16 +1607,16 @@ Executed at: {executed_at}"#,
16071607
return Vec::new();
16081608
}
16091609

1610-
if !stamp.as_ref().exists() {
1610+
if !stamp.path().exists() {
16111611
eprintln!(
16121612
"ERROR: Unable to find the stamp file {}, did you try to keep a nonexistent build stage?",
1613-
stamp.as_ref().display()
1613+
stamp.path().display()
16141614
);
16151615
crate::exit!(1);
16161616
}
16171617

16181618
let mut paths = Vec::new();
1619-
let contents = t!(fs::read(stamp.as_ref()), stamp.as_ref());
1619+
let contents = t!(fs::read(stamp.path()), stamp.path());
16201620
// This is the method we use for extracting paths from the stamp file passed to us. See
16211621
// run_cargo for more information (in compile.rs).
16221622
for part in contents.split(|b| *b == 0) {

src/bootstrap/src/utils/build_stamp.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,7 @@ mod tests;
2020
#[derive(Clone)]
2121
pub struct BuildStamp {
2222
path: PathBuf,
23-
pub(crate) stamp: String,
24-
}
25-
26-
impl AsRef<Path> for BuildStamp {
27-
fn as_ref(&self) -> &Path {
28-
&self.path
29-
}
23+
stamp: String,
3024
}
3125

3226
impl BuildStamp {
@@ -40,6 +34,20 @@ impl BuildStamp {
4034
Self { path: dir.join(".stamp"), stamp: String::new() }
4135
}
4236

37+
/// Returns path of the stamp file.
38+
pub fn path(&self) -> &Path {
39+
&self.path
40+
}
41+
42+
/// Returns the value of the stamp.
43+
///
44+
/// Note that this is empty by default and is populated using `BuildStamp::add_stamp`.
45+
/// It is not read from an actual file, but rather it holds the value that will be used
46+
/// when `BuildStamp::write` is called.
47+
pub fn stamp(&self) -> &str {
48+
&self.stamp
49+
}
50+
4351
/// Sets stamp content to the specified value.
4452
pub fn add_stamp<S: ToString>(mut self, stamp: S) -> Self {
4553
self.stamp.push_str(&stamp.to_string());
@@ -101,15 +109,15 @@ impl BuildStamp {
101109
pub fn clear_if_dirty(builder: &Builder<'_>, dir: &Path, input: &Path) -> bool {
102110
let stamp = BuildStamp::new(dir);
103111
let mut cleared = false;
104-
if mtime(stamp.as_ref()) < mtime(input) {
112+
if mtime(stamp.path()) < mtime(input) {
105113
builder.verbose(|| println!("Dirty - {}", dir.display()));
106114
let _ = fs::remove_dir_all(dir);
107115
cleared = true;
108-
} else if stamp.as_ref().exists() {
116+
} else if stamp.path().exists() {
109117
return cleared;
110118
}
111119
t!(fs::create_dir_all(dir));
112-
t!(fs::File::create(stamp));
120+
t!(fs::File::create(stamp.path()));
113121
cleared
114122
}
115123

0 commit comments

Comments
 (0)