about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPietro Albini <pietro.albini@ferrous-systems.com>2023-06-06 12:37:54 +0200
committerPietro Albini <pietro.albini@ferrous-systems.com>2023-06-12 09:33:33 +0200
commit68d458bb402b873b9ae80423710c2672967479df (patch)
tree13450f59f911d5c3b3bb3534460cf3f09ce15f41
parent1b5143ae13c50aeb3d8a84d838e4db54a57e8a5b (diff)
downloadrust-68d458bb402b873b9ae80423710c2672967479df.tar.gz
rust-68d458bb402b873b9ae80423710c2672967479df.zip
allow mutating the c compilers detected by bootstrap
This will be needed to create synthetic targets in future commits.
-rw-r--r--src/bootstrap/builder.rs2
-rw-r--r--src/bootstrap/cc_detect.rs10
-rw-r--r--src/bootstrap/compile.rs11
-rw-r--r--src/bootstrap/lib.rs48
-rw-r--r--src/bootstrap/llvm.rs12
-rw-r--r--src/bootstrap/test.rs2
-rw-r--r--src/bootstrap/tool.rs2
7 files changed, 46 insertions, 41 deletions
diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs
index 51d94c48f7f..fb0d1811f2d 100644
--- a/src/bootstrap/builder.rs
+++ b/src/bootstrap/builder.rs
@@ -1650,7 +1650,7 @@ impl<'a> Builder<'a> {
             }
         };
         cargo.env(profile_var("DEBUG"), debuginfo_level.to_string());
-        if self.cc[&target].args().iter().any(|arg| arg == "-gz") {
+        if self.cc.borrow()[&target].args().iter().any(|arg| arg == "-gz") {
             rustflags.arg("-Clink-arg=-gz");
         }
         cargo.env(
diff --git a/src/bootstrap/cc_detect.rs b/src/bootstrap/cc_detect.rs
index db3b69d18c8..ec06e75ac4a 100644
--- a/src/bootstrap/cc_detect.rs
+++ b/src/bootstrap/cc_detect.rs
@@ -89,7 +89,7 @@ fn new_cc_build(build: &Build, target: TargetSelection) -> cc::Build {
     cfg
 }
 
-pub fn find(build: &mut Build) {
+pub fn find(build: &Build) {
     // For all targets we're going to need a C compiler for building some shims
     // and such as well as for being a linker for Rust code.
     let targets = build
@@ -115,7 +115,7 @@ pub fn find(build: &mut Build) {
             cc2ar(compiler.path(), target)
         };
 
-        build.cc.insert(target, compiler.clone());
+        build.cc.borrow_mut().insert(target, compiler.clone());
         let cflags = build.cflags(target, GitRepo::Rustc, CLang::C);
 
         // If we use llvm-libunwind, we will need a C++ compiler as well for all targets
@@ -136,7 +136,7 @@ pub fn find(build: &mut Build) {
         // for VxWorks, record CXX compiler which will be used in lib.rs:linker()
         if cxx_configured || target.contains("vxworks") {
             let compiler = cfg.get_compiler();
-            build.cxx.insert(target, compiler);
+            build.cxx.borrow_mut().insert(target, compiler);
         }
 
         build.verbose(&format!("CC_{} = {:?}", &target.triple, build.cc(target)));
@@ -148,11 +148,11 @@ pub fn find(build: &mut Build) {
         }
         if let Some(ar) = ar {
             build.verbose(&format!("AR_{} = {:?}", &target.triple, ar));
-            build.ar.insert(target, ar);
+            build.ar.borrow_mut().insert(target, ar);
         }
 
         if let Some(ranlib) = config.and_then(|c| c.ranlib.clone()) {
-            build.ranlib.insert(target, ranlib);
+            build.ranlib.borrow_mut().insert(target, ranlib);
         }
     }
 }
diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs
index c28fe9022ec..12ca6c79b34 100644
--- a/src/bootstrap/compile.rs
+++ b/src/bootstrap/compile.rs
@@ -314,7 +314,7 @@ fn copy_self_contained_objects(
         }
     } else if target.ends_with("windows-gnu") {
         for obj in ["crt2.o", "dllcrt2.o"].iter() {
-            let src = compiler_file(builder, builder.cc(target), target, CLang::C, obj);
+            let src = compiler_file(builder, &builder.cc(target), target, CLang::C, obj);
             let target = libdir_self_contained.join(obj);
             builder.copy(&src, &target);
             target_deps.push((target, DependencyType::TargetSelfContained));
@@ -995,8 +995,13 @@ fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelect
         && !target.contains("apple")
         && !target.contains("solaris")
     {
-        let file =
-            compiler_file(builder, builder.cxx(target).unwrap(), target, CLang::Cxx, "libstdc++.a");
+        let file = compiler_file(
+            builder,
+            &builder.cxx(target).unwrap(),
+            target,
+            CLang::Cxx,
+            "libstdc++.a",
+        );
         cargo.env("LLVM_STATIC_STDCPP", file);
     }
     if builder.llvm_link_shared() {
diff --git a/src/bootstrap/lib.rs b/src/bootstrap/lib.rs
index 7a16189926b..b0026aedca1 100644
--- a/src/bootstrap/lib.rs
+++ b/src/bootstrap/lib.rs
@@ -226,10 +226,10 @@ pub struct Build {
 
     // Runtime state filled in later on
     // C/C++ compilers and archiver for all targets
-    cc: HashMap<TargetSelection, cc::Tool>,
-    cxx: HashMap<TargetSelection, cc::Tool>,
-    ar: HashMap<TargetSelection, PathBuf>,
-    ranlib: HashMap<TargetSelection, PathBuf>,
+    cc: RefCell<HashMap<TargetSelection, cc::Tool>>,
+    cxx: RefCell<HashMap<TargetSelection, cc::Tool>>,
+    ar: RefCell<HashMap<TargetSelection, PathBuf>>,
+    ranlib: RefCell<HashMap<TargetSelection, PathBuf>>,
     // Miscellaneous
     // allow bidirectional lookups: both name -> path and path -> name
     crates: HashMap<Interned<String>, Crate>,
@@ -451,10 +451,10 @@ impl Build {
             miri_info,
             rustfmt_info,
             in_tree_llvm_info,
-            cc: HashMap::new(),
-            cxx: HashMap::new(),
-            ar: HashMap::new(),
-            ranlib: HashMap::new(),
+            cc: RefCell::new(HashMap::new()),
+            cxx: RefCell::new(HashMap::new()),
+            ar: RefCell::new(HashMap::new()),
+            ranlib: RefCell::new(HashMap::new()),
             crates: HashMap::new(),
             crate_paths: HashMap::new(),
             is_sudo,
@@ -482,7 +482,7 @@ impl Build {
         }
 
         build.verbose("finding compilers");
-        cc_detect::find(&mut build);
+        cc_detect::find(&build);
         // When running `setup`, the profile is about to change, so any requirements we have now may
         // be different on the next invocation. Don't check for them until the next time x.py is
         // run. This is ok because `setup` never runs any build commands, so it won't fail if commands are missing.
@@ -1103,16 +1103,16 @@ impl Build {
     }
 
     /// Returns the path to the C compiler for the target specified.
-    fn cc(&self, target: TargetSelection) -> &Path {
-        self.cc[&target].path()
+    fn cc(&self, target: TargetSelection) -> PathBuf {
+        self.cc.borrow()[&target].path().into()
     }
 
     /// Returns a list of flags to pass to the C compiler for the target
     /// specified.
     fn cflags(&self, target: TargetSelection, which: GitRepo, c: CLang) -> Vec<String> {
         let base = match c {
-            CLang::C => &self.cc[&target],
-            CLang::Cxx => &self.cxx[&target],
+            CLang::C => self.cc.borrow()[&target].clone(),
+            CLang::Cxx => self.cxx.borrow()[&target].clone(),
         };
 
         // Filter out -O and /O (the optimization flags) that we picked up from
@@ -1153,19 +1153,19 @@ impl Build {
     }
 
     /// Returns the path to the `ar` archive utility for the target specified.
-    fn ar(&self, target: TargetSelection) -> Option<&Path> {
-        self.ar.get(&target).map(|p| &**p)
+    fn ar(&self, target: TargetSelection) -> Option<PathBuf> {
+        self.ar.borrow().get(&target).cloned()
     }
 
     /// Returns the path to the `ranlib` utility for the target specified.
-    fn ranlib(&self, target: TargetSelection) -> Option<&Path> {
-        self.ranlib.get(&target).map(|p| &**p)
+    fn ranlib(&self, target: TargetSelection) -> Option<PathBuf> {
+        self.ranlib.borrow().get(&target).cloned()
     }
 
     /// Returns the path to the C++ compiler for the target specified.
-    fn cxx(&self, target: TargetSelection) -> Result<&Path, String> {
-        match self.cxx.get(&target) {
-            Some(p) => Ok(p.path()),
+    fn cxx(&self, target: TargetSelection) -> Result<PathBuf, String> {
+        match self.cxx.borrow().get(&target) {
+            Some(p) => Ok(p.path().into()),
             None => {
                 Err(format!("target `{}` is not configured as a host, only as a target", target))
             }
@@ -1173,21 +1173,21 @@ impl Build {
     }
 
     /// Returns the path to the linker for the given target if it needs to be overridden.
-    fn linker(&self, target: TargetSelection) -> Option<&Path> {
-        if let Some(linker) = self.config.target_config.get(&target).and_then(|c| c.linker.as_ref())
+    fn linker(&self, target: TargetSelection) -> Option<PathBuf> {
+        if let Some(linker) = self.config.target_config.get(&target).and_then(|c| c.linker.clone())
         {
             Some(linker)
         } else if target.contains("vxworks") {
             // need to use CXX compiler as linker to resolve the exception functions
             // that are only existed in CXX libraries
-            Some(self.cxx[&target].path())
+            Some(self.cxx.borrow()[&target].path().into())
         } else if target != self.config.build
             && util::use_host_linker(target)
             && !target.contains("msvc")
         {
             Some(self.cc(target))
         } else if self.config.use_lld && !self.is_fuse_ld_lld(target) && self.build == target {
-            Some(&self.initial_lld)
+            Some(self.initial_lld.clone())
         } else {
             None
         }
diff --git a/src/bootstrap/llvm.rs b/src/bootstrap/llvm.rs
index 19e595650cf..4752b1f7ea1 100644
--- a/src/bootstrap/llvm.rs
+++ b/src/bootstrap/llvm.rs
@@ -605,7 +605,7 @@ fn configure_cmake(
     }
 
     let (cc, cxx) = match builder.config.llvm_clang_cl {
-        Some(ref cl) => (cl.as_ref(), cl.as_ref()),
+        Some(ref cl) => (cl.into(), cl.into()),
         None => (builder.cc(target), builder.cxx(target).unwrap()),
     };
 
@@ -656,9 +656,9 @@ fn configure_cmake(
                     .define("CMAKE_CXX_COMPILER_LAUNCHER", ccache);
             }
         }
-        cfg.define("CMAKE_C_COMPILER", sanitize_cc(cc))
-            .define("CMAKE_CXX_COMPILER", sanitize_cc(cxx))
-            .define("CMAKE_ASM_COMPILER", sanitize_cc(cc));
+        cfg.define("CMAKE_C_COMPILER", sanitize_cc(&cc))
+            .define("CMAKE_CXX_COMPILER", sanitize_cc(&cxx))
+            .define("CMAKE_ASM_COMPILER", sanitize_cc(&cc));
     }
 
     cfg.build_arg("-j").build_arg(builder.jobs().to_string());
@@ -698,7 +698,7 @@ fn configure_cmake(
         if ar.is_absolute() {
             // LLVM build breaks if `CMAKE_AR` is a relative path, for some reason it
             // tries to resolve this path in the LLVM build directory.
-            cfg.define("CMAKE_AR", sanitize_cc(ar));
+            cfg.define("CMAKE_AR", sanitize_cc(&ar));
         }
     }
 
@@ -706,7 +706,7 @@ fn configure_cmake(
         if ranlib.is_absolute() {
             // LLVM build breaks if `CMAKE_RANLIB` is a relative path, for some reason it
             // tries to resolve this path in the LLVM build directory.
-            cfg.define("CMAKE_RANLIB", sanitize_cc(ranlib));
+            cfg.define("CMAKE_RANLIB", sanitize_cc(&ranlib));
         }
     }
 
diff --git a/src/bootstrap/test.rs b/src/bootstrap/test.rs
index 699b7f0be85..c04567f92b2 100644
--- a/src/bootstrap/test.rs
+++ b/src/bootstrap/test.rs
@@ -1698,7 +1698,7 @@ note: if you're sure you want to do this, please open an issue as to why. In the
         // Note that if we encounter `PATH` we make sure to append to our own `PATH`
         // rather than stomp over it.
         if target.contains("msvc") {
-            for &(ref k, ref v) in builder.cc[&target].env() {
+            for &(ref k, ref v) in builder.cc.borrow()[&target].env() {
                 if k != "PATH" {
                     cmd.env(k, v);
                 }
diff --git a/src/bootstrap/tool.rs b/src/bootstrap/tool.rs
index 962cbf758d4..96341b69df0 100644
--- a/src/bootstrap/tool.rs
+++ b/src/bootstrap/tool.rs
@@ -855,7 +855,7 @@ impl<'a> Builder<'a> {
         if compiler.host.contains("msvc") {
             let curpaths = env::var_os("PATH").unwrap_or_default();
             let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
-            for &(ref k, ref v) in self.cc[&compiler.host].env() {
+            for &(ref k, ref v) in self.cc.borrow()[&compiler.host].env() {
                 if k != "PATH" {
                     continue;
                 }