Skip to content

Commit fdecfe4

Browse files
committed
rustbuild: don't set cxx if not configured
Signed-off-by: Marc-Antoine Perennou <[email protected]>
1 parent 00b6015 commit fdecfe4

File tree

7 files changed

+40
-20
lines changed

7 files changed

+40
-20
lines changed

src/bootstrap/builder.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,10 +1135,12 @@ impl<'a> Builder<'a> {
11351135
.env(format!("RANLIB_{}", target), ranlib);
11361136
}
11371137

1138-
let cxx = ccacheify(&self.cxx(target));
1139-
cargo
1140-
.env(format!("CXX_{}", target), &cxx)
1141-
.env(format!("CXXFLAGS_{}", target), cflags);
1138+
if let Ok(cxx) = self.cxx(target) {
1139+
let cxx = ccacheify(&cxx);
1140+
cargo
1141+
.env(format!("CXX_{}", target), &cxx)
1142+
.env(format!("CXXFLAGS_{}", target), cflags);
1143+
}
11421144
}
11431145

11441146
if (cmd == "build" || cmd == "rustc")

src/bootstrap/cc_detect.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,25 @@ pub fn find(build: &mut Build) {
102102
let mut cfg = cc::Build::new();
103103
cfg.cargo_metadata(false).opt_level(2).warnings(false).debug(false).cpp(true)
104104
.target(&target).host(&build.build);
105-
if let Some(cxx) = config.and_then(|c| c.cxx.as_ref()) {
105+
106+
let cxx_configured = if let Some(cxx) = config.and_then(|c| c.cxx.as_ref()) {
106107
cfg.compiler(cxx);
108+
true
107109
} else {
108-
set_compiler(&mut cfg, Language::CPlusPlus, target, config, build);
110+
set_compiler(&mut cfg, Language::CPlusPlus, target, config, build)
111+
};
112+
113+
if cxx_configured {
114+
let compiler = cfg.get_compiler();
115+
build.cxx.insert(target, compiler);
109116
}
110-
let compiler = cfg.get_compiler();
111-
build.cxx.insert(target, compiler);
112117

113118
build.verbose(&format!("CC_{} = {:?}", &target, build.cc(target)));
114119
build.verbose(&format!("CFLAGS_{} = {:?}", &target, cflags));
115-
build.verbose(&format!("CXX_{} = {:?}", &target, build.cxx(target)));
116-
build.verbose(&format!("CXXFLAGS_{} = {:?}", &target, cflags));
120+
if let Ok(cxx) = build.cxx(target) {
121+
build.verbose(&format!("CXX_{} = {:?}", &target, cxx));
122+
build.verbose(&format!("CXXFLAGS_{} = {:?}", &target, cflags));
123+
}
117124
if let Some(ar) = ar {
118125
build.verbose(&format!("AR_{} = {:?}", &target, ar));
119126
build.ar.insert(target, ar);
@@ -125,7 +132,7 @@ fn set_compiler(cfg: &mut cc::Build,
125132
compiler: Language,
126133
target: Interned<String>,
127134
config: Option<&Target>,
128-
build: &Build) {
135+
build: &Build) -> bool {
129136
match &*target {
130137
// When compiling for android we may have the NDK configured in the
131138
// config.toml in which case we look there. Otherwise the default
@@ -138,6 +145,7 @@ fn set_compiler(cfg: &mut cc::Build,
138145
.replace("thumbv7", "arm");
139146
let compiler = format!("{}-{}", target, compiler.clang());
140147
cfg.compiler(ndk.join("bin").join(compiler));
148+
return true;
141149
}
142150
}
143151

@@ -147,32 +155,35 @@ fn set_compiler(cfg: &mut cc::Build,
147155
let c = cfg.get_compiler();
148156
let gnu_compiler = compiler.gcc();
149157
if !c.path().ends_with(gnu_compiler) {
150-
return
158+
return false;
151159
}
152160

153161
let output = output(c.to_command().arg("--version"));
154162
let i = match output.find(" 4.") {
155163
Some(i) => i,
156-
None => return,
164+
None => return false,
157165
};
158166
match output[i + 3..].chars().next().unwrap() {
159167
'0' ..= '6' => {}
160-
_ => return,
168+
_ => return false,
161169
}
162170
let alternative = format!("e{}", gnu_compiler);
163171
if Command::new(&alternative).output().is_ok() {
164172
cfg.compiler(alternative);
173+
return true;
165174
}
166175
}
167176

168177
"mips-unknown-linux-musl" => {
169178
if cfg.get_compiler().path().to_str() == Some("gcc") {
170179
cfg.compiler("mips-linux-musl-gcc");
180+
return true;
171181
}
172182
}
173183
"mipsel-unknown-linux-musl" => {
174184
if cfg.get_compiler().path().to_str() == Some("gcc") {
175185
cfg.compiler("mipsel-linux-musl-gcc");
186+
return true;
176187
}
177188
}
178189

@@ -181,12 +192,14 @@ fn set_compiler(cfg: &mut cc::Build,
181192
let guess = root.join("bin/musl-gcc");
182193
if guess.exists() {
183194
cfg.compiler(guess);
195+
return true;
184196
}
185197
}
186198
}
187199

188200
_ => {}
189201
}
202+
false
190203
}
191204

192205
/// The target programming language for a native compiler.

src/bootstrap/compile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ pub fn build_codegen_backend(builder: &Builder<'_>,
782782
!target.contains("windows") &&
783783
!target.contains("apple") {
784784
let file = compiler_file(builder,
785-
builder.cxx(target),
785+
builder.cxx(target).unwrap(),
786786
target,
787787
"libstdc++.a");
788788
cargo.env("LLVM_STATIC_STDCPP", file);

src/bootstrap/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,8 +815,13 @@ impl Build {
815815
}
816816

817817
/// Returns the path to the C++ compiler for the target specified.
818-
fn cxx(&self, target: Interned<String>) -> &Path {
819-
self.cxx[&target].path()
818+
fn cxx(&self, target: Interned<String>) -> Result<&Path, String> {
819+
match self.cxx.get(&target) {
820+
Some(p) => Ok(p.path()),
821+
None => Err(format!(
822+
"target `{}` is not configured as a host, only as a target",
823+
target))
824+
}
820825
}
821826

822827
/// Returns the path to the linker for the given target if it needs to be overridden.

src/bootstrap/native.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ fn configure_cmake(builder: &Builder<'_>,
358358

359359
let (cc, cxx) = match builder.config.llvm_clang_cl {
360360
Some(ref cl) => (cl.as_ref(), cl.as_ref()),
361-
None => (builder.cc(target), builder.cxx(target)),
361+
None => (builder.cc(target), builder.cxx(target).unwrap()),
362362
};
363363

364364
// Handle msvc + ninja + ccache specially (this is what the bots use)

src/bootstrap/sanity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ pub fn check(build: &mut Build) {
146146

147147
for host in &build.hosts {
148148
if !build.config.dry_run {
149-
cmd_finder.must_have(build.cxx(*host));
149+
cmd_finder.must_have(build.cxx(*host).unwrap());
150150
}
151151
}
152152

src/bootstrap/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ impl Step for Compiletest {
12111211
cmd.arg("--cc")
12121212
.arg(builder.cc(target))
12131213
.arg("--cxx")
1214-
.arg(builder.cxx(target))
1214+
.arg(builder.cxx(target).unwrap())
12151215
.arg("--cflags")
12161216
.arg(builder.cflags(target, GitRepo::Rustc).join(" "))
12171217
.arg("--llvm-components")

0 commit comments

Comments
 (0)