Skip to content

Commit e790b92

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. We do still choose to pass them in certain places, but now it's at least clear which flags are default, and which flags are extra flags added on.
1 parent f85c6de commit e790b92

File tree

6 files changed

+36
-17
lines changed

6 files changed

+36
-17
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,8 @@ pub fn compiler_file(
15411541
return PathBuf::new();
15421542
}
15431543
let mut cmd = command(compiler);
1544-
cmd.args(builder.cflags(target, GitRepo::Rustc, c));
1544+
cmd.args(builder.default_cflags(target, c));
1545+
cmd.args(builder.extra_cflags(target, GitRepo::Rustc, c));
15451546
cmd.arg(format!("-print-file-name={file}"));
15461547
let out = cmd.run_capture_stdout(builder).stdout();
15471548
PathBuf::from(out.trim())

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -757,9 +757,15 @@ fn configure_cmake(
757757
}
758758

759759
cfg.build_arg("-j").build_arg(builder.jobs().to_string());
760+
// FIXME(madsmtm): Allow `cmake-rs` to select flags by itself by passing
761+
// our flags via `.cflag`/`.cxxflag` instead.
762+
//
763+
// Needs `suppressed_compiler_flag_prefixes` to be gone, and hence
764+
// https://github.com/llvm/llvm-project/issues/88780 to be fixed.
760765
let mut cflags: OsString = builder
761-
.cflags(target, GitRepo::Llvm, CLang::C)
766+
.default_cflags(target, CLang::C)
762767
.into_iter()
768+
.chain(builder.extra_cflags(target, GitRepo::Llvm, CLang::C))
763769
.filter(|flag| {
764770
!suppressed_compiler_flag_prefixes
765771
.iter()
@@ -778,8 +784,9 @@ fn configure_cmake(
778784
}
779785
cfg.define("CMAKE_C_FLAGS", cflags);
780786
let mut cxxflags: OsString = builder
781-
.cflags(target, GitRepo::Llvm, CLang::Cxx)
787+
.default_cflags(target, CLang::Cxx)
782788
.into_iter()
789+
.chain(builder.extra_cflags(target, GitRepo::Llvm, CLang::Cxx))
783790
.filter(|flag| {
784791
!suppressed_compiler_flag_prefixes
785792
.iter()

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -2005,14 +2005,18 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the
20052005
// Only pass correct values for these flags for the `run-make` suite as it
20062006
// requires that a C++ compiler was configured which isn't always the case.
20072007
if !builder.config.dry_run() && mode == "run-make" {
2008+
let mut cflags = builder.default_cflags(target, CLang::C);
2009+
cflags.extend(builder.extra_cflags(target, GitRepo::Rustc, CLang::C));
2010+
let mut cxxflags = builder.default_cflags(target, CLang::Cxx);
2011+
cxxflags.extend(builder.extra_cflags(target, GitRepo::Rustc, CLang::Cxx));
20082012
cmd.arg("--cc")
20092013
.arg(builder.cc(target))
20102014
.arg("--cxx")
20112015
.arg(builder.cxx(target).unwrap())
20122016
.arg("--cflags")
2013-
.arg(builder.cflags(target, GitRepo::Rustc, CLang::C).join(" "))
2017+
.arg(cflags.join(" "))
20142018
.arg("--cxxflags")
2015-
.arg(builder.cflags(target, GitRepo::Rustc, CLang::Cxx).join(" "));
2019+
.arg(cxxflags.join(" "));
20162020
copts_passed = true;
20172021
if let Some(ar) = builder.ar(target) {
20182022
cmd.arg("--ar").arg(ar);

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

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

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

323323
if let Some(ar) = builder.ar(target) {
@@ -329,7 +329,7 @@ impl Cargo {
329329

330330
if let Ok(cxx) = builder.cxx(target) {
331331
let cxx = ccacheify(&cxx);
332-
let cxxflags = builder.cflags(target, GitRepo::Rustc, CLang::Cxx).join(" ");
332+
let cxxflags = builder.extra_cflags(target, GitRepo::Rustc, CLang::Cxx).join(" ");
333333
self.command
334334
.env(format!("CXX_{triple_underscored}"), &cxx)
335335
.env(format!("CXXFLAGS_{triple_underscored}"), cxxflags);

src/bootstrap/src/lib.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ impl Mode {
246246
}
247247
}
248248

249+
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
249250
pub enum CLang {
250251
C,
251252
Cxx,
@@ -1139,9 +1140,9 @@ Executed at: {executed_at}"#,
11391140
self.cc.borrow()[&target].path().into()
11401141
}
11411142

1142-
/// Returns a list of flags to pass to the C compiler for the target
1143-
/// specified.
1144-
fn cflags(&self, target: TargetSelection, which: GitRepo, c: CLang) -> Vec<String> {
1143+
/// Returns C flags that `cc-rs` thinks should be enabled for the
1144+
/// specified target by default.
1145+
fn default_cflags(&self, target: TargetSelection, c: CLang) -> Vec<String> {
11451146
if self.config.dry_run() {
11461147
return Vec::new();
11471148
}
@@ -1150,14 +1151,18 @@ Executed at: {executed_at}"#,
11501151
CLang::Cxx => self.cxx.borrow()[&target].clone(),
11511152
};
11521153

1153-
// Filter out -O and /O (the optimization flags) that we picked up from
1154-
// cc-rs because the build scripts will determine that for themselves.
1155-
let mut base = base
1156-
.args()
1154+
// Filter out -O and /O (the optimization flags) that we picked up
1155+
// from cc-rs, that's up to the caller to figure out.
1156+
base.args()
11571157
.iter()
11581158
.map(|s| s.to_string_lossy().into_owned())
11591159
.filter(|s| !s.starts_with("-O") && !s.starts_with("/O"))
1160-
.collect::<Vec<String>>();
1160+
.collect::<Vec<String>>()
1161+
}
1162+
1163+
/// Returns extra C flags that `cc-rs` doesn't handle.
1164+
fn extra_cflags(&self, target: TargetSelection, which: GitRepo, c: CLang) -> Vec<String> {
1165+
let mut base = Vec::new();
11611166

11621167
// If we're compiling C++ on macOS then we add a flag indicating that
11631168
// 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
@@ -142,7 +142,8 @@ pub fn find_target(build: &Build, target: TargetSelection) {
142142
};
143143

144144
build.cc.borrow_mut().insert(target, compiler.clone());
145-
let cflags = build.cflags(target, GitRepo::Rustc, CLang::C);
145+
let mut cflags = build.default_cflags(target, CLang::C);
146+
cflags.extend(build.extra_cflags(target, GitRepo::Rustc, CLang::C));
146147

147148
// If we use llvm-libunwind, we will need a C++ compiler as well for all targets
148149
// We'll need one anyways if the target triple is also a host triple
@@ -168,7 +169,8 @@ pub fn find_target(build: &Build, target: TargetSelection) {
168169
build.verbose(|| println!("CC_{} = {:?}", target.triple, build.cc(target)));
169170
build.verbose(|| println!("CFLAGS_{} = {cflags:?}", target.triple));
170171
if let Ok(cxx) = build.cxx(target) {
171-
let cxxflags = build.cflags(target, GitRepo::Rustc, CLang::Cxx);
172+
let mut cxxflags = build.default_cflags(target, CLang::Cxx);
173+
cxxflags.extend(build.extra_cflags(target, GitRepo::Rustc, CLang::Cxx));
172174
build.verbose(|| println!("CXX_{} = {cxx:?}", target.triple));
173175
build.verbose(|| println!("CXXFLAGS_{} = {cxxflags:?}", target.triple));
174176
}

0 commit comments

Comments
 (0)