Skip to content

Commit 25e13fd

Browse files
committed
Bootstrap: Don't duplicate cc-rs flags
Commands that end up invoking cc-rs, i.e. Cargo (through build scripts) and cmake-rs don't need the CFLAGS from cc-rs itself, as they will just end up as duplicates.
1 parent f00f682 commit 25e13fd

File tree

6 files changed

+29
-17
lines changed

6 files changed

+29
-17
lines changed

src/bootstrap/src/core/build_steps/compile.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1564,7 +1564,8 @@ pub fn compiler_file(
15641564
return PathBuf::new();
15651565
}
15661566
let mut cmd = command(compiler);
1567-
cmd.args(builder.cflags(target, GitRepo::Rustc, c));
1567+
cmd.args(builder.default_cflags(target, c));
1568+
cmd.args(builder.extra_cflags(target, GitRepo::Rustc, c));
15681569
cmd.arg(format!("-print-file-name={file}"));
15691570
let out = cmd.run_capture_stdout(builder).stdout();
15701571
PathBuf::from(out.trim())

src/bootstrap/src/core/build_steps/llvm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ fn configure_cmake(
758758

759759
cfg.build_arg("-j").build_arg(builder.jobs().to_string());
760760
let mut cflags: OsString = builder
761-
.cflags(target, GitRepo::Llvm, CLang::C)
761+
.extra_cflags(target, GitRepo::Llvm, CLang::C)
762762
.into_iter()
763763
.filter(|flag| {
764764
!suppressed_compiler_flag_prefixes
@@ -778,7 +778,7 @@ fn configure_cmake(
778778
}
779779
cfg.define("CMAKE_C_FLAGS", cflags);
780780
let mut cxxflags: OsString = builder
781-
.cflags(target, GitRepo::Llvm, CLang::Cxx)
781+
.extra_cflags(target, GitRepo::Llvm, CLang::Cxx)
782782
.into_iter()
783783
.filter(|flag| {
784784
!suppressed_compiler_flag_prefixes

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -2012,14 +2012,18 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the
20122012
// Only pass correct values for these flags for the `run-make` suite as it
20132013
// requires that a C++ compiler was configured which isn't always the case.
20142014
if !builder.config.dry_run() && mode == "run-make" {
2015+
let mut cflags = builder.default_cflags(target, CLang::C);
2016+
cflags.extend(builder.extra_cflags(target, GitRepo::Rustc, CLang::C));
2017+
let mut cxxflags = builder.default_cflags(target, CLang::Cxx);
2018+
cxxflags.extend(builder.extra_cflags(target, GitRepo::Rustc, CLang::Cxx));
20152019
cmd.arg("--cc")
20162020
.arg(builder.cc(target))
20172021
.arg("--cxx")
20182022
.arg(builder.cxx(target).unwrap())
20192023
.arg("--cflags")
2020-
.arg(builder.cflags(target, GitRepo::Rustc, CLang::C).join(" "))
2024+
.arg(cflags.join(" "))
20212025
.arg("--cxxflags")
2022-
.arg(builder.cflags(target, GitRepo::Rustc, CLang::Cxx).join(" "));
2026+
.arg(cxxflags.join(" "));
20232027
copts_passed = true;
20242028
if let Some(ar) = builder.ar(target) {
20252029
cmd.arg("--ar").arg(ar);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ impl Cargo {
307307
let cc = ccacheify(&builder.cc(target));
308308
self.command.env(format!("CC_{triple_underscored}"), &cc);
309309

310-
let cflags = builder.cflags(target, GitRepo::Rustc, CLang::C).join(" ");
310+
let cflags = builder.extra_cflags(target, GitRepo::Rustc, CLang::C).join(" ");
311311
self.command.env(format!("CFLAGS_{triple_underscored}"), &cflags);
312312

313313
if let Some(ar) = builder.ar(target) {
@@ -319,7 +319,7 @@ impl Cargo {
319319

320320
if let Ok(cxx) = builder.cxx(target) {
321321
let cxx = ccacheify(&cxx);
322-
let cxxflags = builder.cflags(target, GitRepo::Rustc, CLang::Cxx).join(" ");
322+
let cxxflags = builder.extra_cflags(target, GitRepo::Rustc, CLang::Cxx).join(" ");
323323
self.command
324324
.env(format!("CXX_{triple_underscored}"), &cxx)
325325
.env(format!("CXXFLAGS_{triple_underscored}"), cxxflags);

src/bootstrap/src/lib.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ impl Mode {
250250
}
251251
}
252252

253+
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
253254
pub enum CLang {
254255
C,
255256
Cxx,
@@ -1187,9 +1188,9 @@ Executed at: {executed_at}"#,
11871188
self.cc.borrow()[&target].path().into()
11881189
}
11891190

1190-
/// Returns a list of flags to pass to the C compiler for the target
1191-
/// specified.
1192-
fn cflags(&self, target: TargetSelection, which: GitRepo, c: CLang) -> Vec<String> {
1191+
/// Returns C flags that `cc-rs` thinks should be enabled for the
1192+
/// specified target by default.
1193+
fn default_cflags(&self, target: TargetSelection, c: CLang) -> Vec<String> {
11931194
if self.config.dry_run() {
11941195
return Vec::new();
11951196
}
@@ -1198,14 +1199,18 @@ Executed at: {executed_at}"#,
11981199
CLang::Cxx => self.cxx.borrow()[&target].clone(),
11991200
};
12001201

1201-
// Filter out -O and /O (the optimization flags) that we picked up from
1202-
// cc-rs because the build scripts will determine that for themselves.
1203-
let mut base = base
1204-
.args()
1202+
// Filter out -O and /O (the optimization flags) that we picked up
1203+
// from cc-rs, that's up to the caller to figure out.
1204+
base.args()
12051205
.iter()
12061206
.map(|s| s.to_string_lossy().into_owned())
12071207
.filter(|s| !s.starts_with("-O") && !s.starts_with("/O"))
1208-
.collect::<Vec<String>>();
1208+
.collect::<Vec<String>>()
1209+
}
1210+
1211+
/// Returns extra C flags that `cc-rs` doesn't handle.
1212+
fn extra_cflags(&self, target: TargetSelection, which: GitRepo, c: CLang) -> Vec<String> {
1213+
let mut base = Vec::new();
12091214

12101215
// If we're compiling C++ on macOS then we add a flag indicating that
12111216
// we want libc++ (more filled out than libstdc++), ensuring that

src/bootstrap/src/utils/cc_detect.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ pub fn find_target(build: &Build, target: TargetSelection) {
132132
};
133133

134134
build.cc.borrow_mut().insert(target, compiler.clone());
135-
let cflags = build.cflags(target, GitRepo::Rustc, CLang::C);
135+
let mut cflags = build.default_cflags(target, CLang::C);
136+
cflags.extend(build.extra_cflags(target, GitRepo::Rustc, CLang::C));
136137

137138
// If we use llvm-libunwind, we will need a C++ compiler as well for all targets
138139
// We'll need one anyways if the target triple is also a host triple
@@ -158,7 +159,8 @@ pub fn find_target(build: &Build, target: TargetSelection) {
158159
build.verbose(|| println!("CC_{} = {:?}", target.triple, build.cc(target)));
159160
build.verbose(|| println!("CFLAGS_{} = {cflags:?}", target.triple));
160161
if let Ok(cxx) = build.cxx(target) {
161-
let cxxflags = build.cflags(target, GitRepo::Rustc, CLang::Cxx);
162+
let mut cxxflags = build.default_cflags(target, CLang::Cxx);
163+
cxxflags.extend(build.extra_cflags(target, GitRepo::Rustc, CLang::Cxx));
162164
build.verbose(|| println!("CXX_{} = {cxx:?}", target.triple));
163165
build.verbose(|| println!("CXXFLAGS_{} = {cxxflags:?}", target.triple));
164166
}

0 commit comments

Comments
 (0)