Skip to content

Commit 68d458b

Browse files
committed
allow mutating the c compilers detected by bootstrap
This will be needed to create synthetic targets in future commits.
1 parent 1b5143a commit 68d458b

File tree

7 files changed

+46
-41
lines changed

7 files changed

+46
-41
lines changed

src/bootstrap/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,7 @@ impl<'a> Builder<'a> {
16501650
}
16511651
};
16521652
cargo.env(profile_var("DEBUG"), debuginfo_level.to_string());
1653-
if self.cc[&target].args().iter().any(|arg| arg == "-gz") {
1653+
if self.cc.borrow()[&target].args().iter().any(|arg| arg == "-gz") {
16541654
rustflags.arg("-Clink-arg=-gz");
16551655
}
16561656
cargo.env(

src/bootstrap/cc_detect.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ fn new_cc_build(build: &Build, target: TargetSelection) -> cc::Build {
8989
cfg
9090
}
9191

92-
pub fn find(build: &mut Build) {
92+
pub fn find(build: &Build) {
9393
// For all targets we're going to need a C compiler for building some shims
9494
// and such as well as for being a linker for Rust code.
9595
let targets = build
@@ -115,7 +115,7 @@ pub fn find(build: &mut Build) {
115115
cc2ar(compiler.path(), target)
116116
};
117117

118-
build.cc.insert(target, compiler.clone());
118+
build.cc.borrow_mut().insert(target, compiler.clone());
119119
let cflags = build.cflags(target, GitRepo::Rustc, CLang::C);
120120

121121
// 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) {
136136
// for VxWorks, record CXX compiler which will be used in lib.rs:linker()
137137
if cxx_configured || target.contains("vxworks") {
138138
let compiler = cfg.get_compiler();
139-
build.cxx.insert(target, compiler);
139+
build.cxx.borrow_mut().insert(target, compiler);
140140
}
141141

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

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

src/bootstrap/compile.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ fn copy_self_contained_objects(
314314
}
315315
} else if target.ends_with("windows-gnu") {
316316
for obj in ["crt2.o", "dllcrt2.o"].iter() {
317-
let src = compiler_file(builder, builder.cc(target), target, CLang::C, obj);
317+
let src = compiler_file(builder, &builder.cc(target), target, CLang::C, obj);
318318
let target = libdir_self_contained.join(obj);
319319
builder.copy(&src, &target);
320320
target_deps.push((target, DependencyType::TargetSelfContained));
@@ -995,8 +995,13 @@ fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelect
995995
&& !target.contains("apple")
996996
&& !target.contains("solaris")
997997
{
998-
let file =
999-
compiler_file(builder, builder.cxx(target).unwrap(), target, CLang::Cxx, "libstdc++.a");
998+
let file = compiler_file(
999+
builder,
1000+
&builder.cxx(target).unwrap(),
1001+
target,
1002+
CLang::Cxx,
1003+
"libstdc++.a",
1004+
);
10001005
cargo.env("LLVM_STATIC_STDCPP", file);
10011006
}
10021007
if builder.llvm_link_shared() {

src/bootstrap/lib.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,10 @@ pub struct Build {
226226

227227
// Runtime state filled in later on
228228
// C/C++ compilers and archiver for all targets
229-
cc: HashMap<TargetSelection, cc::Tool>,
230-
cxx: HashMap<TargetSelection, cc::Tool>,
231-
ar: HashMap<TargetSelection, PathBuf>,
232-
ranlib: HashMap<TargetSelection, PathBuf>,
229+
cc: RefCell<HashMap<TargetSelection, cc::Tool>>,
230+
cxx: RefCell<HashMap<TargetSelection, cc::Tool>>,
231+
ar: RefCell<HashMap<TargetSelection, PathBuf>>,
232+
ranlib: RefCell<HashMap<TargetSelection, PathBuf>>,
233233
// Miscellaneous
234234
// allow bidirectional lookups: both name -> path and path -> name
235235
crates: HashMap<Interned<String>, Crate>,
@@ -451,10 +451,10 @@ impl Build {
451451
miri_info,
452452
rustfmt_info,
453453
in_tree_llvm_info,
454-
cc: HashMap::new(),
455-
cxx: HashMap::new(),
456-
ar: HashMap::new(),
457-
ranlib: HashMap::new(),
454+
cc: RefCell::new(HashMap::new()),
455+
cxx: RefCell::new(HashMap::new()),
456+
ar: RefCell::new(HashMap::new()),
457+
ranlib: RefCell::new(HashMap::new()),
458458
crates: HashMap::new(),
459459
crate_paths: HashMap::new(),
460460
is_sudo,
@@ -482,7 +482,7 @@ impl Build {
482482
}
483483

484484
build.verbose("finding compilers");
485-
cc_detect::find(&mut build);
485+
cc_detect::find(&build);
486486
// When running `setup`, the profile is about to change, so any requirements we have now may
487487
// be different on the next invocation. Don't check for them until the next time x.py is
488488
// 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 {
11031103
}
11041104

11051105
/// Returns the path to the C compiler for the target specified.
1106-
fn cc(&self, target: TargetSelection) -> &Path {
1107-
self.cc[&target].path()
1106+
fn cc(&self, target: TargetSelection) -> PathBuf {
1107+
self.cc.borrow()[&target].path().into()
11081108
}
11091109

11101110
/// Returns a list of flags to pass to the C compiler for the target
11111111
/// specified.
11121112
fn cflags(&self, target: TargetSelection, which: GitRepo, c: CLang) -> Vec<String> {
11131113
let base = match c {
1114-
CLang::C => &self.cc[&target],
1115-
CLang::Cxx => &self.cxx[&target],
1114+
CLang::C => self.cc.borrow()[&target].clone(),
1115+
CLang::Cxx => self.cxx.borrow()[&target].clone(),
11161116
};
11171117

11181118
// Filter out -O and /O (the optimization flags) that we picked up from
@@ -1153,41 +1153,41 @@ impl Build {
11531153
}
11541154

11551155
/// Returns the path to the `ar` archive utility for the target specified.
1156-
fn ar(&self, target: TargetSelection) -> Option<&Path> {
1157-
self.ar.get(&target).map(|p| &**p)
1156+
fn ar(&self, target: TargetSelection) -> Option<PathBuf> {
1157+
self.ar.borrow().get(&target).cloned()
11581158
}
11591159

11601160
/// Returns the path to the `ranlib` utility for the target specified.
1161-
fn ranlib(&self, target: TargetSelection) -> Option<&Path> {
1162-
self.ranlib.get(&target).map(|p| &**p)
1161+
fn ranlib(&self, target: TargetSelection) -> Option<PathBuf> {
1162+
self.ranlib.borrow().get(&target).cloned()
11631163
}
11641164

11651165
/// Returns the path to the C++ compiler for the target specified.
1166-
fn cxx(&self, target: TargetSelection) -> Result<&Path, String> {
1167-
match self.cxx.get(&target) {
1168-
Some(p) => Ok(p.path()),
1166+
fn cxx(&self, target: TargetSelection) -> Result<PathBuf, String> {
1167+
match self.cxx.borrow().get(&target) {
1168+
Some(p) => Ok(p.path().into()),
11691169
None => {
11701170
Err(format!("target `{}` is not configured as a host, only as a target", target))
11711171
}
11721172
}
11731173
}
11741174

11751175
/// Returns the path to the linker for the given target if it needs to be overridden.
1176-
fn linker(&self, target: TargetSelection) -> Option<&Path> {
1177-
if let Some(linker) = self.config.target_config.get(&target).and_then(|c| c.linker.as_ref())
1176+
fn linker(&self, target: TargetSelection) -> Option<PathBuf> {
1177+
if let Some(linker) = self.config.target_config.get(&target).and_then(|c| c.linker.clone())
11781178
{
11791179
Some(linker)
11801180
} else if target.contains("vxworks") {
11811181
// need to use CXX compiler as linker to resolve the exception functions
11821182
// that are only existed in CXX libraries
1183-
Some(self.cxx[&target].path())
1183+
Some(self.cxx.borrow()[&target].path().into())
11841184
} else if target != self.config.build
11851185
&& util::use_host_linker(target)
11861186
&& !target.contains("msvc")
11871187
{
11881188
Some(self.cc(target))
11891189
} else if self.config.use_lld && !self.is_fuse_ld_lld(target) && self.build == target {
1190-
Some(&self.initial_lld)
1190+
Some(self.initial_lld.clone())
11911191
} else {
11921192
None
11931193
}

src/bootstrap/llvm.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ fn configure_cmake(
605605
}
606606

607607
let (cc, cxx) = match builder.config.llvm_clang_cl {
608-
Some(ref cl) => (cl.as_ref(), cl.as_ref()),
608+
Some(ref cl) => (cl.into(), cl.into()),
609609
None => (builder.cc(target), builder.cxx(target).unwrap()),
610610
};
611611

@@ -656,9 +656,9 @@ fn configure_cmake(
656656
.define("CMAKE_CXX_COMPILER_LAUNCHER", ccache);
657657
}
658658
}
659-
cfg.define("CMAKE_C_COMPILER", sanitize_cc(cc))
660-
.define("CMAKE_CXX_COMPILER", sanitize_cc(cxx))
661-
.define("CMAKE_ASM_COMPILER", sanitize_cc(cc));
659+
cfg.define("CMAKE_C_COMPILER", sanitize_cc(&cc))
660+
.define("CMAKE_CXX_COMPILER", sanitize_cc(&cxx))
661+
.define("CMAKE_ASM_COMPILER", sanitize_cc(&cc));
662662
}
663663

664664
cfg.build_arg("-j").build_arg(builder.jobs().to_string());
@@ -698,15 +698,15 @@ fn configure_cmake(
698698
if ar.is_absolute() {
699699
// LLVM build breaks if `CMAKE_AR` is a relative path, for some reason it
700700
// tries to resolve this path in the LLVM build directory.
701-
cfg.define("CMAKE_AR", sanitize_cc(ar));
701+
cfg.define("CMAKE_AR", sanitize_cc(&ar));
702702
}
703703
}
704704

705705
if let Some(ranlib) = builder.ranlib(target) {
706706
if ranlib.is_absolute() {
707707
// LLVM build breaks if `CMAKE_RANLIB` is a relative path, for some reason it
708708
// tries to resolve this path in the LLVM build directory.
709-
cfg.define("CMAKE_RANLIB", sanitize_cc(ranlib));
709+
cfg.define("CMAKE_RANLIB", sanitize_cc(&ranlib));
710710
}
711711
}
712712

src/bootstrap/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1698,7 +1698,7 @@ note: if you're sure you want to do this, please open an issue as to why. In the
16981698
// Note that if we encounter `PATH` we make sure to append to our own `PATH`
16991699
// rather than stomp over it.
17001700
if target.contains("msvc") {
1701-
for &(ref k, ref v) in builder.cc[&target].env() {
1701+
for &(ref k, ref v) in builder.cc.borrow()[&target].env() {
17021702
if k != "PATH" {
17031703
cmd.env(k, v);
17041704
}

src/bootstrap/tool.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ impl<'a> Builder<'a> {
855855
if compiler.host.contains("msvc") {
856856
let curpaths = env::var_os("PATH").unwrap_or_default();
857857
let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
858-
for &(ref k, ref v) in self.cc[&compiler.host].env() {
858+
for &(ref k, ref v) in self.cc.borrow()[&compiler.host].env() {
859859
if k != "PATH" {
860860
continue;
861861
}

0 commit comments

Comments
 (0)