add new configure_cmake option to let projects set cc/cxx flags

This commit is contained in:
Manuel Drehwald 2025-12-22 11:18:35 -08:00
parent 68f11a11b6
commit e8a6312474

View file

@ -63,6 +63,25 @@ impl LlvmBuildStatus {
} }
} }
/// Allows each step to add C/Cxx flags which are only used for a specific cmake invocation.
#[derive(Debug, Clone, Default)]
struct CcFlags {
/// Additional values for CMAKE_CC_FLAGS, to be added before all other values.
cflags: OsString,
/// Additional values for CMAKE_CXX_FLAGS, to be added before all other values.
cxxflags: OsString,
}
impl CcFlags {
fn push_all(&mut self, s: impl AsRef<OsStr>) {
let s = s.as_ref();
self.cflags.push(" ");
self.cflags.push(s);
self.cxxflags.push(" ");
self.cxxflags.push(s);
}
}
/// Linker flags to pass to LLVM's CMake invocation. /// Linker flags to pass to LLVM's CMake invocation.
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
struct LdFlags { struct LdFlags {
@ -527,7 +546,7 @@ impl Step for Llvm {
cfg.define("LLVM_VERSION_SUFFIX", suffix); cfg.define("LLVM_VERSION_SUFFIX", suffix);
} }
configure_cmake(builder, target, &mut cfg, true, ldflags, &[]); configure_cmake(builder, target, &mut cfg, true, ldflags, CcFlags::default(), &[]);
configure_llvm(builder, target, &mut cfg); configure_llvm(builder, target, &mut cfg);
for (key, val) in &builder.config.llvm_build_config { for (key, val) in &builder.config.llvm_build_config {
@ -633,6 +652,7 @@ fn configure_cmake(
cfg: &mut cmake::Config, cfg: &mut cmake::Config,
use_compiler_launcher: bool, use_compiler_launcher: bool,
mut ldflags: LdFlags, mut ldflags: LdFlags,
ccflags: CcFlags,
suppressed_compiler_flag_prefixes: &[&str], suppressed_compiler_flag_prefixes: &[&str],
) { ) {
// Do not print installation messages for up-to-date files. // Do not print installation messages for up-to-date files.
@ -761,23 +781,21 @@ fn configure_cmake(
.define("CMAKE_ASM_COMPILER", sanitize_cc(&cc)); .define("CMAKE_ASM_COMPILER", sanitize_cc(&cc));
cfg.build_arg("-j").build_arg(builder.jobs().to_string()); cfg.build_arg("-j").build_arg(builder.jobs().to_string());
let mut cflags = ccflags.cflags.clone();
// FIXME(madsmtm): Allow `cmake-rs` to select flags by itself by passing // FIXME(madsmtm): Allow `cmake-rs` to select flags by itself by passing
// our flags via `.cflag`/`.cxxflag` instead. // our flags via `.cflag`/`.cxxflag` instead.
// //
// Needs `suppressed_compiler_flag_prefixes` to be gone, and hence // Needs `suppressed_compiler_flag_prefixes` to be gone, and hence
// https://github.com/llvm/llvm-project/issues/88780 to be fixed. // https://github.com/llvm/llvm-project/issues/88780 to be fixed.
let mut cflags: OsString = builder for flag in builder
.cc_handled_clags(target, CLang::C) .cc_handled_clags(target, CLang::C)
.into_iter() .into_iter()
.chain(builder.cc_unhandled_cflags(target, GitRepo::Llvm, CLang::C)) .chain(builder.cc_unhandled_cflags(target, GitRepo::Llvm, CLang::C))
.filter(|flag| { .filter(|flag| !suppressed_compiler_flag_prefixes.iter().any(|p| flag.starts_with(p)))
!suppressed_compiler_flag_prefixes {
.iter() cflags.push(" ");
.any(|suppressed_prefix| flag.starts_with(suppressed_prefix)) cflags.push(flag);
}) }
.collect::<Vec<String>>()
.join(" ")
.into();
if let Some(ref s) = builder.config.llvm_cflags { if let Some(ref s) = builder.config.llvm_cflags {
cflags.push(" "); cflags.push(" ");
cflags.push(s); cflags.push(s);
@ -789,7 +807,8 @@ fn configure_cmake(
cflags.push(format!(" --target={target}")); cflags.push(format!(" --target={target}"));
} }
cfg.define("CMAKE_C_FLAGS", cflags); cfg.define("CMAKE_C_FLAGS", cflags);
let mut cxxflags: OsString = builder let mut cxxflags = ccflags.cxxflags.clone();
for flag in builder
.cc_handled_clags(target, CLang::Cxx) .cc_handled_clags(target, CLang::Cxx)
.into_iter() .into_iter()
.chain(builder.cc_unhandled_cflags(target, GitRepo::Llvm, CLang::Cxx)) .chain(builder.cc_unhandled_cflags(target, GitRepo::Llvm, CLang::Cxx))
@ -798,9 +817,10 @@ fn configure_cmake(
.iter() .iter()
.any(|suppressed_prefix| flag.starts_with(suppressed_prefix)) .any(|suppressed_prefix| flag.starts_with(suppressed_prefix))
}) })
.collect::<Vec<String>>() {
.join(" ") cxxflags.push(" ");
.into(); cxxflags.push(flag);
}
if let Some(ref s) = builder.config.llvm_cxxflags { if let Some(ref s) = builder.config.llvm_cxxflags {
cxxflags.push(" "); cxxflags.push(" ");
cxxflags.push(s); cxxflags.push(s);
@ -811,6 +831,7 @@ fn configure_cmake(
if builder.config.llvm_clang_cl.is_some() { if builder.config.llvm_clang_cl.is_some() {
cxxflags.push(format!(" --target={target}")); cxxflags.push(format!(" --target={target}"));
} }
cfg.define("CMAKE_CXX_FLAGS", cxxflags); cfg.define("CMAKE_CXX_FLAGS", cxxflags);
if let Some(ar) = builder.ar(target) if let Some(ar) = builder.ar(target)
&& ar.is_absolute() && ar.is_absolute()
@ -970,7 +991,13 @@ impl Step for Enzyme {
builder.config.update_submodule("src/tools/enzyme"); builder.config.update_submodule("src/tools/enzyme");
let mut cfg = cmake::Config::new(builder.src.join("src/tools/enzyme/enzyme/")); let mut cfg = cmake::Config::new(builder.src.join("src/tools/enzyme/enzyme/"));
configure_cmake(builder, target, &mut cfg, true, LdFlags::default(), &[]);
let mut cflags = CcFlags::default();
// Enzyme devs maintain upstream compability, but only fix deprecations when they are about
// to turn into a hard error. As such, Enzyme generates various warnings which could make it
// hard to spot more relevant issues.
cflags.push_all("-Wno-deprecated");
configure_cmake(builder, target, &mut cfg, true, LdFlags::default(), cflags, &[]);
// Re-use the same flags as llvm to control the level of debug information // Re-use the same flags as llvm to control the level of debug information
// generated by Enzyme. // generated by Enzyme.
@ -1090,7 +1117,7 @@ impl Step for Lld {
ldflags.push_all("-Wl,-rpath,'$ORIGIN/../../../'"); ldflags.push_all("-Wl,-rpath,'$ORIGIN/../../../'");
} }
configure_cmake(builder, target, &mut cfg, true, ldflags, &[]); configure_cmake(builder, target, &mut cfg, true, ldflags, CcFlags::default(), &[]);
configure_llvm(builder, target, &mut cfg); configure_llvm(builder, target, &mut cfg);
// Re-use the same flags as llvm to control the level of debug information // Re-use the same flags as llvm to control the level of debug information
@ -1213,6 +1240,7 @@ impl Step for Sanitizers {
&mut cfg, &mut cfg,
use_compiler_launcher, use_compiler_launcher,
LdFlags::default(), LdFlags::default(),
CcFlags::default(),
suppressed_compiler_flag_prefixes, suppressed_compiler_flag_prefixes,
); );