From d251d57d6782bff33c3ca438b2f83463a5d4b13f Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 14 Sep 2024 02:54:37 -0700 Subject: [PATCH] Fix bootstrap on Windows --- patches/fix_bootstrap_windows.patch | 77 +++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 patches/fix_bootstrap_windows.patch diff --git a/patches/fix_bootstrap_windows.patch b/patches/fix_bootstrap_windows.patch new file mode 100644 index 0000000..a9d91fa --- /dev/null +++ b/patches/fix_bootstrap_windows.patch @@ -0,0 +1,77 @@ +diff --git a/src/bootstrap/src/core/download.rs b/src/bootstrap/src/core/download.rs +index 56a8528d..b8e862da 100644 +--- a/src/bootstrap/src/core/download.rs ++++ b/src/bootstrap/src/core/download.rs +@@ -706,9 +706,7 @@ pub(crate) fn maybe_download_ci_llvm(&self) { + let file_times = fs::FileTimes::new().set_accessed(now).set_modified(now); + + let llvm_config = llvm_root.join("bin").join(exe("llvm-config", self.build)); +- let llvm_config_file = t!(File::options().write(true).open(llvm_config)); +- +- t!(llvm_config_file.set_times(file_times)); ++ t!(crate::utils::helpers::set_file_times(llvm_config, file_times)); + + if self.should_fix_bins_and_dylibs() { + let llvm_lib = llvm_root.join("lib"); +diff --git a/src/bootstrap/src/lib.rs b/src/bootstrap/src/lib.rs +index a8555b2c..7b55463a 100644 +--- a/src/bootstrap/src/lib.rs ++++ b/src/bootstrap/src/lib.rs +@@ -41,7 +41,9 @@ + use crate::core::config::{DryRun, Target}; + use crate::core::config::{LlvmLibunwind, TargetSelection}; + use crate::utils::exec::{command, BehaviorOnFailure, BootstrapCommand, CommandOutput}; +-use crate::utils::helpers::{self, dir_is_empty, exe, libdir, mtime, output, symlink_dir}; ++use crate::utils::helpers::{ ++ self, dir_is_empty, exe, libdir, mtime, output, set_file_times, symlink_dir, ++}; + + mod core; + mod utils; +@@ -1725,21 +1727,20 @@ fn copy_link_internal(&self, src: &Path, dst: &Path, dereference_symlinks: bool) + } + } + if let Ok(()) = fs::hard_link(&src, dst) { +- // Attempt to "easy copy" by creating a hard link +- // (symlinks don't work on windows), but if that fails +- // just fall back to a slow `copy` operation. ++ // Attempt to "easy copy" by creating a hard link (symlinks are priviledged on windows), ++ // but if that fails just fall back to a slow `copy` operation. + } else { + if let Err(e) = fs::copy(&src, dst) { + panic!("failed to copy `{}` to `{}`: {}", src.display(), dst.display(), e) + } + t!(fs::set_permissions(dst, metadata.permissions())); + ++ // Restore file times because changing permissions on e.g. Linux using `chmod` can cause ++ // file access time to change. + let file_times = fs::FileTimes::new() + .set_accessed(t!(metadata.accessed())) + .set_modified(t!(metadata.modified())); +- +- let dst_file = t!(fs::File::open(dst)); +- t!(dst_file.set_times(file_times)); ++ t!(set_file_times(dst, file_times)); + } + } + +diff --git a/src/bootstrap/src/utils/helpers.rs b/src/bootstrap/src/utils/helpers.rs +index 773a873e..72ec05e6 100644 +--- a/src/bootstrap/src/utils/helpers.rs ++++ b/src/bootstrap/src/utils/helpers.rs +@@ -545,3 +545,15 @@ pub fn get_closest_merge_base_commit( + + Ok(output_result(git.as_command_mut())?.trim().to_owned()) + } ++ ++/// Sets the file times for a given file at `path`. ++pub fn set_file_times>(path: P, times: fs::FileTimes) -> io::Result<()> { ++ // Windows requires file to be writable to modify file times. But on Linux CI the file does not ++ // need to be writable to modify file times and might be read-only. ++ let f = if cfg!(windows) { ++ fs::File::options().write(true).open(path)? ++ } else { ++ fs::File::open(path)? ++ }; ++ f.set_times(times) ++}