Skip to content

Commit 8540188

Browse files
committed
Remove RefCell from cc/cxx/ar/ranlib
It wasn't really needed there.
1 parent 2591571 commit 8540188

File tree

7 files changed

+44
-49
lines changed

7 files changed

+44
-49
lines changed

src/bootstrap/src/core/build_steps/test.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,7 +2009,7 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the
20092009
// Note that if we encounter `PATH` we make sure to append to our own `PATH`
20102010
// rather than stomp over it.
20112011
if !builder.config.dry_run() && target.is_msvc() {
2012-
for (k, v) in builder.cc.borrow()[&target].env() {
2012+
for (k, v) in builder.cc[&target].env() {
20132013
if k != "PATH" {
20142014
cmd.env(k, v);
20152015
}
@@ -2026,8 +2026,7 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the
20262026
// address sanitizer enabled (e.g., ntdll.dll).
20272027
cmd.env("ASAN_WIN_CONTINUE_ON_INTERCEPTION_FAILURE", "1");
20282028
// Add the address sanitizer runtime to the PATH - it is located next to cl.exe.
2029-
let asan_runtime_path =
2030-
builder.cc.borrow()[&target].path().parent().unwrap().to_path_buf();
2029+
let asan_runtime_path = builder.cc[&target].path().parent().unwrap().to_path_buf();
20312030
let old_path = cmd
20322031
.get_envs()
20332032
.find_map(|(k, v)| (k == "PATH").then_some(v))

src/bootstrap/src/core/build_steps/tool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ impl Builder<'_> {
13211321
if compiler.host.is_msvc() {
13221322
let curpaths = env::var_os("PATH").unwrap_or_default();
13231323
let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
1324-
for (k, v) in self.cc.borrow()[&compiler.host].env() {
1324+
for (k, v) in self.cc[&compiler.host].env() {
13251325
if k != "PATH" {
13261326
continue;
13271327
}

src/bootstrap/src/core/builder/cargo.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,7 @@ impl Cargo {
278278
self.rustdocflags.arg(&arg);
279279
}
280280

281-
if !builder.config.dry_run()
282-
&& builder.cc.borrow()[&target].args().iter().any(|arg| arg == "-gz")
283-
{
281+
if !builder.config.dry_run() && builder.cc[&target].args().iter().any(|arg| arg == "-gz") {
284282
self.rustflags.arg("-Clink-arg=-gz");
285283
}
286284

src/bootstrap/src/lib.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! also check out the `src/bootstrap/README.md` file for more information.
1818
#![cfg_attr(test, allow(unused))]
1919

20-
use std::cell::{Cell, RefCell};
20+
use std::cell::Cell;
2121
use std::collections::{BTreeSet, HashMap, HashSet};
2222
use std::fmt::Display;
2323
use std::path::{Path, PathBuf};
@@ -190,10 +190,10 @@ pub struct Build {
190190

191191
// Runtime state filled in later on
192192
// C/C++ compilers and archiver for all targets
193-
cc: RefCell<HashMap<TargetSelection, cc::Tool>>,
194-
cxx: RefCell<HashMap<TargetSelection, cc::Tool>>,
195-
ar: RefCell<HashMap<TargetSelection, PathBuf>>,
196-
ranlib: RefCell<HashMap<TargetSelection, PathBuf>>,
193+
cc: HashMap<TargetSelection, cc::Tool>,
194+
cxx: HashMap<TargetSelection, cc::Tool>,
195+
ar: HashMap<TargetSelection, PathBuf>,
196+
ranlib: HashMap<TargetSelection, PathBuf>,
197197
// Miscellaneous
198198
// allow bidirectional lookups: both name -> path and path -> name
199199
crates: HashMap<String, Crate>,
@@ -464,10 +464,10 @@ impl Build {
464464
enzyme_info,
465465
in_tree_llvm_info,
466466
in_tree_gcc_info,
467-
cc: RefCell::new(HashMap::new()),
468-
cxx: RefCell::new(HashMap::new()),
469-
ar: RefCell::new(HashMap::new()),
470-
ranlib: RefCell::new(HashMap::new()),
467+
cc: HashMap::new(),
468+
cxx: HashMap::new(),
469+
ar: HashMap::new(),
470+
ranlib: HashMap::new(),
471471
crates: HashMap::new(),
472472
crate_paths: HashMap::new(),
473473
is_sudo,
@@ -493,7 +493,7 @@ impl Build {
493493
}
494494

495495
build.verbose(|| println!("finding compilers"));
496-
utils::cc_detect::find(&build);
496+
utils::cc_detect::fill_compilers(&mut build);
497497
// When running `setup`, the profile is about to change, so any requirements we have now may
498498
// be different on the next invocation. Don't check for them until the next time x.py is
499499
// run. This is ok because `setup` never runs any build commands, so it won't fail if commands are missing.
@@ -1133,17 +1133,17 @@ impl Build {
11331133
if self.config.dry_run() {
11341134
return PathBuf::new();
11351135
}
1136-
self.cc.borrow()[&target].path().into()
1136+
self.cc[&target].path().into()
11371137
}
11381138

11391139
/// Returns the internal `cc::Tool` for the C compiler.
11401140
fn cc_tool(&self, target: TargetSelection) -> Tool {
1141-
self.cc.borrow()[&target].clone()
1141+
self.cc[&target].clone()
11421142
}
11431143

11441144
/// Returns the internal `cc::Tool` for the C++ compiler.
11451145
fn cxx_tool(&self, target: TargetSelection) -> Tool {
1146-
self.cxx.borrow()[&target].clone()
1146+
self.cxx[&target].clone()
11471147
}
11481148

11491149
/// Returns C flags that `cc-rs` thinks should be enabled for the
@@ -1153,8 +1153,8 @@ impl Build {
11531153
return Vec::new();
11541154
}
11551155
let base = match c {
1156-
CLang::C => self.cc.borrow()[&target].clone(),
1157-
CLang::Cxx => self.cxx.borrow()[&target].clone(),
1156+
CLang::C => self.cc[&target].clone(),
1157+
CLang::Cxx => self.cxx[&target].clone(),
11581158
};
11591159

11601160
// Filter out -O and /O (the optimization flags) that we picked up
@@ -1207,23 +1207,23 @@ impl Build {
12071207
if self.config.dry_run() {
12081208
return None;
12091209
}
1210-
self.ar.borrow().get(&target).cloned()
1210+
self.ar.get(&target).cloned()
12111211
}
12121212

12131213
/// Returns the path to the `ranlib` utility for the target specified.
12141214
fn ranlib(&self, target: TargetSelection) -> Option<PathBuf> {
12151215
if self.config.dry_run() {
12161216
return None;
12171217
}
1218-
self.ranlib.borrow().get(&target).cloned()
1218+
self.ranlib.get(&target).cloned()
12191219
}
12201220

12211221
/// Returns the path to the C++ compiler for the target specified.
12221222
fn cxx(&self, target: TargetSelection) -> Result<PathBuf, String> {
12231223
if self.config.dry_run() {
12241224
return Ok(PathBuf::new());
12251225
}
1226-
match self.cxx.borrow().get(&target) {
1226+
match self.cxx.get(&target) {
12271227
Some(p) => Ok(p.path().into()),
12281228
None => Err(format!("target `{target}` is not configured as a host, only as a target")),
12291229
}
@@ -1240,7 +1240,7 @@ impl Build {
12401240
} else if target.contains("vxworks") {
12411241
// need to use CXX compiler as linker to resolve the exception functions
12421242
// that are only existed in CXX libraries
1243-
Some(self.cxx.borrow()[&target].path().into())
1243+
Some(self.cxx[&target].path().into())
12441244
} else if !self.config.is_host_target(target)
12451245
&& helpers::use_host_linker(target)
12461246
&& !target.is_msvc()

src/bootstrap/src/utils/cc_detect.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ fn new_cc_build(build: &Build, target: TargetSelection) -> cc::Build {
6161
///
6262
/// This function determines which targets need a C compiler (and, if needed, a C++ compiler)
6363
/// by combining the primary build target, host targets, and any additional targets. For
64-
/// each target, it calls [`find_target`] to configure the necessary compiler tools.
65-
pub fn find(build: &Build) {
64+
/// each target, it calls [`fill_target_compiler`] to configure the necessary compiler tools.
65+
pub fn fill_compilers(build: &mut Build) {
6666
let targets: HashSet<_> = match build.config.cmd {
6767
// We don't need to check cross targets for these commands.
6868
crate::Subcommand::Clean { .. }
@@ -87,7 +87,7 @@ pub fn find(build: &Build) {
8787
};
8888

8989
for target in targets.into_iter() {
90-
find_target(build, target);
90+
fill_target_compiler(build, target);
9191
}
9292
}
9393

@@ -96,7 +96,7 @@ pub fn find(build: &Build) {
9696
/// This function uses both user-specified configuration (from `bootstrap.toml`) and auto-detection
9797
/// logic to determine the correct C/C++ compilers for the target. It also determines the appropriate
9898
/// archiver (`ar`) and sets up additional compilation flags (both handled and unhandled).
99-
pub fn find_target(build: &Build, target: TargetSelection) {
99+
pub fn fill_target_compiler(build: &mut Build, target: TargetSelection) {
100100
let mut cfg = new_cc_build(build, target);
101101
let config = build.config.target_config.get(&target);
102102
if let Some(cc) = config
@@ -113,7 +113,7 @@ pub fn find_target(build: &Build, target: TargetSelection) {
113113
cfg.try_get_archiver().map(|c| PathBuf::from(c.get_program())).ok()
114114
};
115115

116-
build.cc.borrow_mut().insert(target, compiler.clone());
116+
build.cc.insert(target, compiler.clone());
117117
let mut cflags = build.cc_handled_clags(target, CLang::C);
118118
cflags.extend(build.cc_unhandled_cflags(target, GitRepo::Rustc, CLang::C));
119119

@@ -135,7 +135,7 @@ pub fn find_target(build: &Build, target: TargetSelection) {
135135
// for VxWorks, record CXX compiler which will be used in lib.rs:linker()
136136
if cxx_configured || target.contains("vxworks") {
137137
let compiler = cfg.get_compiler();
138-
build.cxx.borrow_mut().insert(target, compiler);
138+
build.cxx.insert(target, compiler);
139139
}
140140

141141
build.verbose(|| println!("CC_{} = {:?}", target.triple, build.cc(target)));
@@ -148,11 +148,11 @@ pub fn find_target(build: &Build, target: TargetSelection) {
148148
}
149149
if let Some(ar) = ar {
150150
build.verbose(|| println!("AR_{} = {ar:?}", target.triple));
151-
build.ar.borrow_mut().insert(target, ar);
151+
build.ar.insert(target, ar);
152152
}
153153

154154
if let Some(ranlib) = config.and_then(|c| c.ranlib.clone()) {
155-
build.ranlib.borrow_mut().insert(target, ranlib);
155+
build.ranlib.insert(target, ranlib);
156156
}
157157
}
158158

src/bootstrap/src/utils/cc_detect/tests.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,14 @@ fn test_find_target_with_config() {
119119
target_config.ar = Some(PathBuf::from("dummy-ar"));
120120
target_config.ranlib = Some(PathBuf::from("dummy-ranlib"));
121121
build.config.target_config.insert(target.clone(), target_config);
122-
find_target(&build, target.clone());
123-
let binding = build.cc.borrow();
124-
let cc_tool = binding.get(&target).unwrap();
122+
fill_target_compiler(&mut build, target.clone());
123+
let cc_tool = build.cc.get(&target).unwrap();
125124
assert_eq!(cc_tool.path(), &PathBuf::from("dummy-cc"));
126-
let binding = build.cxx.borrow();
127-
let cxx_tool = binding.get(&target).unwrap();
125+
let cxx_tool = build.cxx.get(&target).unwrap();
128126
assert_eq!(cxx_tool.path(), &PathBuf::from("dummy-cxx"));
129-
let binding = build.ar.borrow();
130-
let ar = binding.get(&target).unwrap();
127+
let ar = build.ar.get(&target).unwrap();
131128
assert_eq!(ar, &PathBuf::from("dummy-ar"));
132-
let binding = build.ranlib.borrow();
133-
let ranlib = binding.get(&target).unwrap();
129+
let ranlib = build.ranlib.get(&target).unwrap();
134130
assert_eq!(ranlib, &PathBuf::from("dummy-ranlib"));
135131
}
136132

@@ -139,12 +135,12 @@ fn test_find_target_without_config() {
139135
let mut build = Build::new(Config { ..Config::parse(Flags::parse(&["build".to_owned()])) });
140136
let target = TargetSelection::from_user("x86_64-unknown-linux-gnu");
141137
build.config.target_config.clear();
142-
find_target(&build, target.clone());
143-
assert!(build.cc.borrow().contains_key(&target));
138+
fill_target_compiler(&mut build, target.clone());
139+
assert!(build.cc.contains_key(&target));
144140
if !target.triple.contains("vxworks") {
145-
assert!(build.cxx.borrow().contains_key(&target));
141+
assert!(build.cxx.contains_key(&target));
146142
}
147-
assert!(build.ar.borrow().contains_key(&target));
143+
assert!(build.ar.contains_key(&target));
148144
}
149145

150146
#[test]
@@ -154,8 +150,8 @@ fn test_find() {
154150
let target2 = TargetSelection::from_user("x86_64-unknown-openbsd");
155151
build.targets.push(target1.clone());
156152
build.hosts.push(target2.clone());
157-
find(&build);
153+
fill_compilers(&mut build);
158154
for t in build.hosts.iter().chain(build.targets.iter()).chain(iter::once(&build.host_target)) {
159-
assert!(build.cc.borrow().contains_key(t), "CC not set for target {}", t.triple);
155+
assert!(build.cc.contains_key(t), "CC not set for target {}", t.triple);
160156
}
161157
}

src/bootstrap/src/utils/tests/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
//! This module contains shared utilities for bootstrap tests.
2+
13
pub mod git;

0 commit comments

Comments
 (0)