Skip to content

Commit b53fb47

Browse files
committed
Store LLVM options in a separate struct
To make it easier to compare two LLVM configurations.
1 parent 55be59d commit b53fb47

File tree

8 files changed

+129
-124
lines changed

8 files changed

+129
-124
lines changed

src/bootstrap/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ impl<'a> Builder<'a> {
11041104
let mut dylib_dirs = vec![self.rustc_libdir(compiler)];
11051105

11061106
// Ensure that the downloaded LLVM libraries can be found.
1107-
if self.config.llvm_from_ci {
1107+
if self.config.llvm.from_ci {
11081108
let ci_llvm_lib = self.out.join(&*compiler.host.triple).join("ci-llvm").join("lib");
11091109
dylib_dirs.push(ci_llvm_lib);
11101110
}
@@ -1872,7 +1872,7 @@ impl<'a> Builder<'a> {
18721872
//
18731873
// FIXME: the guard against msvc shouldn't need to be here
18741874
if target.contains("msvc") {
1875-
if let Some(ref cl) = self.config.llvm_clang_cl {
1875+
if let Some(ref cl) = self.config.llvm.clang_cl {
18761876
cargo.env("CC", cl).env("CXX", cl);
18771877
}
18781878
} else {

src/bootstrap/compile.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ impl Step for Rustc {
809809
// is already on by default in MSVC optimized builds, which is interpreted as --icf=all:
810810
// https://github.com/llvm/llvm-project/blob/3329cec2f79185bafd678f310fafadba2a8c76d2/lld/COFF/Driver.cpp#L1746
811811
// https://github.com/rust-lang/rust/blob/f22819bcce4abaff7d1246a56eec493418f9f4ee/compiler/rustc_codegen_ssa/src/back/linker.rs#L827
812-
if builder.config.use_lld && !compiler.host.contains("msvc") {
812+
if builder.config.llvm.use_lld && !compiler.host.contains("msvc") {
813813
cargo.rustflag("-Clink-args=-Wl,--icf=all");
814814
}
815815

@@ -1002,15 +1002,15 @@ fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelect
10021002
// `__llvm_profile_instrument_memop` when linking `rustc_driver`.
10031003
let mut llvm_linker_flags = String::new();
10041004
if builder.config.llvm_profile_generate && target.contains("msvc") {
1005-
if let Some(ref clang_cl_path) = builder.config.llvm_clang_cl {
1005+
if let Some(ref clang_cl_path) = builder.config.llvm.clang_cl {
10061006
// Add clang's runtime library directory to the search path
10071007
let clang_rt_dir = get_clang_cl_resource_dir(clang_cl_path);
10081008
llvm_linker_flags.push_str(&format!("-L{}", clang_rt_dir.display()));
10091009
}
10101010
}
10111011

10121012
// The config can also specify its own llvm linker flags.
1013-
if let Some(ref s) = builder.config.llvm_ldflags {
1013+
if let Some(ref s) = builder.config.llvm.ldflags {
10141014
if !llvm_linker_flags.is_empty() {
10151015
llvm_linker_flags.push_str(" ");
10161016
}
@@ -1024,7 +1024,7 @@ fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelect
10241024

10251025
// Building with a static libstdc++ is only supported on linux right now,
10261026
// not for MSVC or macOS
1027-
if builder.config.llvm_static_stdcpp
1027+
if builder.config.llvm.static_stdcpp
10281028
&& !target.contains("freebsd")
10291029
&& !target.contains("msvc")
10301030
&& !target.contains("apple")
@@ -1042,10 +1042,10 @@ fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelect
10421042
if builder.llvm_link_shared() {
10431043
cargo.env("LLVM_LINK_SHARED", "1");
10441044
}
1045-
if builder.config.llvm_use_libcxx {
1045+
if builder.config.llvm.use_libcxx {
10461046
cargo.env("LLVM_USE_LIBCXX", "1");
10471047
}
1048-
if builder.config.llvm_optimize && !builder.config.llvm_release_debuginfo {
1048+
if builder.config.llvm.optimize && !builder.config.llvm.release_debuginfo {
10491049
cargo.env("LLVM_NDEBUG", "1");
10501050
}
10511051
}
@@ -1564,7 +1564,7 @@ impl Step for Assemble {
15641564
});
15651565
}
15661566

1567-
let lld_install = if builder.config.lld_enabled {
1567+
let lld_install = if builder.config.llvm.lld_enabled {
15681568
Some(builder.ensure(llvm::Lld { target: target_compiler.host }))
15691569
} else {
15701570
None

src/bootstrap/config.rs

Lines changed: 77 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,43 @@ impl Display for DebuginfoLevel {
101101
}
102102
}
103103

104+
#[derive(Default, Clone)]
105+
pub struct LLVMConfig {
106+
pub assertions: bool,
107+
pub tests: bool,
108+
pub plugins: bool,
109+
pub optimize: bool,
110+
pub thin_lto: bool,
111+
pub release_debuginfo: bool,
112+
pub static_stdcpp: bool,
113+
/// `None` if `llvm_from_ci` is true and we haven't yet downloaded llvm.
114+
#[cfg(not(test))]
115+
link_shared: Cell<Option<bool>>,
116+
#[cfg(test)]
117+
pub link_shared: Cell<Option<bool>>,
118+
pub clang_cl: Option<String>,
119+
pub targets: Option<String>,
120+
pub experimental_targets: Option<String>,
121+
pub link_jobs: Option<u32>,
122+
pub version_suffix: Option<String>,
123+
pub use_linker: Option<String>,
124+
pub allow_old_toolchain: bool,
125+
pub polly: bool,
126+
pub clang: bool,
127+
pub enable_warnings: bool,
128+
pub from_ci: bool,
129+
pub build_config: HashMap<String, String>,
130+
131+
pub use_lld: bool,
132+
pub lld_enabled: bool,
133+
pub tools_enabled: bool,
134+
135+
pub cflags: Option<String>,
136+
pub cxxflags: Option<String>,
137+
pub ldflags: Option<String>,
138+
pub use_libcxx: bool,
139+
}
140+
104141
/// Global configuration for the entire build and/or bootstrap.
105142
///
106143
/// This structure is parsed from `config.toml`, and some of the fields are inferred from `git` or build-time parameters.
@@ -167,39 +204,7 @@ pub struct Config {
167204
pub backtrace_on_ice: bool,
168205

169206
// llvm codegen options
170-
pub llvm_assertions: bool,
171-
pub llvm_tests: bool,
172-
pub llvm_plugins: bool,
173-
pub llvm_optimize: bool,
174-
pub llvm_thin_lto: bool,
175-
pub llvm_release_debuginfo: bool,
176-
pub llvm_static_stdcpp: bool,
177-
/// `None` if `llvm_from_ci` is true and we haven't yet downloaded llvm.
178-
#[cfg(not(test))]
179-
llvm_link_shared: Cell<Option<bool>>,
180-
#[cfg(test)]
181-
pub llvm_link_shared: Cell<Option<bool>>,
182-
pub llvm_clang_cl: Option<String>,
183-
pub llvm_targets: Option<String>,
184-
pub llvm_experimental_targets: Option<String>,
185-
pub llvm_link_jobs: Option<u32>,
186-
pub llvm_version_suffix: Option<String>,
187-
pub llvm_use_linker: Option<String>,
188-
pub llvm_allow_old_toolchain: bool,
189-
pub llvm_polly: bool,
190-
pub llvm_clang: bool,
191-
pub llvm_enable_warnings: bool,
192-
pub llvm_from_ci: bool,
193-
pub llvm_build_config: HashMap<String, String>,
194-
195-
pub use_lld: bool,
196-
pub lld_enabled: bool,
197-
pub llvm_tools_enabled: bool,
198-
199-
pub llvm_cflags: Option<String>,
200-
pub llvm_cxxflags: Option<String>,
201-
pub llvm_ldflags: Option<String>,
202-
pub llvm_use_libcxx: bool,
207+
pub llvm: LLVMConfig,
203208

204209
// rust codegen options
205210
pub rust_optimize: RustOptimize,
@@ -1052,9 +1057,9 @@ define_config! {
10521057
impl Config {
10531058
pub fn default_opts() -> Config {
10541059
let mut config = Config::default();
1055-
config.llvm_optimize = true;
1060+
config.llvm.optimize = true;
10561061
config.ninja_in_file = true;
1057-
config.llvm_static_stdcpp = false;
1062+
config.llvm.static_stdcpp = false;
10581063
config.backtrace = true;
10591064
config.rust_optimize = RustOptimize::Bool(true);
10601065
config.rust_optimize_tests = true;
@@ -1428,9 +1433,9 @@ impl Config {
14281433
if let Some(true) = rust.incremental {
14291434
config.incremental = true;
14301435
}
1431-
set(&mut config.use_lld, rust.use_lld);
1432-
set(&mut config.lld_enabled, rust.lld);
1433-
set(&mut config.llvm_tools_enabled, rust.llvm_tools);
1436+
set(&mut config.llvm.use_lld, rust.use_lld);
1437+
set(&mut config.llvm.lld_enabled, rust.lld);
1438+
set(&mut config.llvm.tools_enabled, rust.llvm_tools);
14341439
config.rustc_parallel = rust.parallel_compiler.unwrap_or(false);
14351440
config.rustc_default_linker = rust.default_linker;
14361441
config.musl_root = rust.musl_root.map(PathBuf::from);
@@ -1489,32 +1494,32 @@ impl Config {
14891494
llvm_assertions = llvm.assertions;
14901495
llvm_tests = llvm.tests;
14911496
llvm_plugins = llvm.plugins;
1492-
set(&mut config.llvm_optimize, llvm.optimize);
1493-
set(&mut config.llvm_thin_lto, llvm.thin_lto);
1494-
set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
1495-
set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
1497+
set(&mut config.llvm.optimize, llvm.optimize);
1498+
set(&mut config.llvm.thin_lto, llvm.thin_lto);
1499+
set(&mut config.llvm.release_debuginfo, llvm.release_debuginfo);
1500+
set(&mut config.llvm.static_stdcpp, llvm.static_libstdcpp);
14961501
if let Some(v) = llvm.link_shared {
1497-
config.llvm_link_shared.set(Some(v));
1502+
config.llvm.link_shared.set(Some(v));
14981503
}
1499-
config.llvm_targets = llvm.targets.clone();
1500-
config.llvm_experimental_targets = llvm.experimental_targets.clone();
1501-
config.llvm_link_jobs = llvm.link_jobs;
1502-
config.llvm_version_suffix = llvm.version_suffix.clone();
1503-
config.llvm_clang_cl = llvm.clang_cl.clone();
1504-
1505-
config.llvm_cflags = llvm.cflags.clone();
1506-
config.llvm_cxxflags = llvm.cxxflags.clone();
1507-
config.llvm_ldflags = llvm.ldflags.clone();
1508-
set(&mut config.llvm_use_libcxx, llvm.use_libcxx);
1509-
config.llvm_use_linker = llvm.use_linker.clone();
1510-
config.llvm_allow_old_toolchain = llvm.allow_old_toolchain.unwrap_or(false);
1511-
config.llvm_polly = llvm.polly.unwrap_or(false);
1512-
config.llvm_clang = llvm.clang.unwrap_or(false);
1513-
config.llvm_enable_warnings = llvm.enable_warnings.unwrap_or(false);
1514-
config.llvm_build_config = llvm.build_config.clone().unwrap_or(Default::default());
1504+
config.llvm.targets = llvm.targets.clone();
1505+
config.llvm.experimental_targets = llvm.experimental_targets.clone();
1506+
config.llvm.link_jobs = llvm.link_jobs;
1507+
config.llvm.version_suffix = llvm.version_suffix.clone();
1508+
config.llvm.clang_cl = llvm.clang_cl.clone();
1509+
1510+
config.llvm.cflags = llvm.cflags.clone();
1511+
config.llvm.cxxflags = llvm.cxxflags.clone();
1512+
config.llvm.ldflags = llvm.ldflags.clone();
1513+
set(&mut config.llvm.use_libcxx, llvm.use_libcxx);
1514+
config.llvm.use_linker = llvm.use_linker.clone();
1515+
config.llvm.allow_old_toolchain = llvm.allow_old_toolchain.unwrap_or(false);
1516+
config.llvm.polly = llvm.polly.unwrap_or(false);
1517+
config.llvm.clang = llvm.clang.unwrap_or(false);
1518+
config.llvm.enable_warnings = llvm.enable_warnings.unwrap_or(false);
1519+
config.llvm.build_config = llvm.build_config.clone().unwrap_or(Default::default());
15151520

15161521
let asserts = llvm_assertions.unwrap_or(false);
1517-
config.llvm_from_ci = match llvm.download_ci_llvm {
1522+
config.llvm.from_ci = match llvm.download_ci_llvm {
15181523
Some(StringOrBool::String(s)) => {
15191524
assert!(s == "if-available", "unknown option `{}` for download-ci-llvm", s);
15201525
crate::llvm::is_ci_llvm_available(&config, asserts)
@@ -1525,7 +1530,7 @@ impl Config {
15251530
}
15261531
};
15271532

1528-
if config.llvm_from_ci {
1533+
if config.llvm.from_ci {
15291534
// None of the LLVM options, except assertions, are supported
15301535
// when using downloaded LLVM. We could just ignore these but
15311536
// that's potentially confusing, so force them to not be
@@ -1556,14 +1561,14 @@ impl Config {
15561561
}
15571562

15581563
// NOTE: can never be hit when downloading from CI, since we call `check_ci_llvm!(thin_lto)` above.
1559-
if config.llvm_thin_lto && llvm.link_shared.is_none() {
1564+
if config.llvm.thin_lto && llvm.link_shared.is_none() {
15601565
// If we're building with ThinLTO on, by default we want to link
15611566
// to LLVM shared, to avoid re-doing ThinLTO (which happens in
15621567
// the link step) with each stage.
1563-
config.llvm_link_shared.set(Some(true));
1568+
config.llvm.link_shared.set(Some(true));
15641569
}
15651570
} else {
1566-
config.llvm_from_ci =
1571+
config.llvm.from_ci =
15671572
config.channel == "dev" && crate::llvm::is_ci_llvm_available(&config, false);
15681573
}
15691574

@@ -1615,7 +1620,7 @@ impl Config {
16151620
}
16161621
}
16171622

1618-
if config.llvm_from_ci {
1623+
if config.llvm.from_ci {
16191624
let triple = &config.build.triple;
16201625
let ci_llvm_bin = config.ci_llvm_root().join("bin");
16211626
let build_target = config
@@ -1650,9 +1655,9 @@ impl Config {
16501655
// Now that we've reached the end of our configuration, infer the
16511656
// default values for all options that we haven't otherwise stored yet.
16521657

1653-
config.llvm_assertions = llvm_assertions.unwrap_or(false);
1654-
config.llvm_tests = llvm_tests.unwrap_or(false);
1655-
config.llvm_plugins = llvm_plugins.unwrap_or(false);
1658+
config.llvm.assertions = llvm_assertions.unwrap_or(false);
1659+
config.llvm.tests = llvm_tests.unwrap_or(false);
1660+
config.llvm.plugins = llvm_plugins.unwrap_or(false);
16561661
config.rust_optimize = optimize.unwrap_or(RustOptimize::Bool(true));
16571662

16581663
let default = debug == Some(true);
@@ -1855,7 +1860,7 @@ impl Config {
18551860

18561861
/// The absolute path to the downloaded LLVM artifacts.
18571862
pub(crate) fn ci_llvm_root(&self) -> PathBuf {
1858-
assert!(self.llvm_from_ci);
1863+
assert!(self.llvm.from_ci);
18591864
self.out.join(&*self.build.triple).join("ci-llvm")
18601865
}
18611866

@@ -1870,14 +1875,14 @@ impl Config {
18701875
/// If `false`, llvm should be linked statically.
18711876
/// This is computed on demand since LLVM might have to first be downloaded from CI.
18721877
pub(crate) fn llvm_link_shared(&self) -> bool {
1873-
let mut opt = self.llvm_link_shared.get();
1878+
let mut opt = self.llvm.link_shared.get();
18741879
if opt.is_none() && self.dry_run() {
18751880
// just assume static for now - dynamic linking isn't supported on all platforms
18761881
return false;
18771882
}
18781883

18791884
let llvm_link_shared = *opt.get_or_insert_with(|| {
1880-
if self.llvm_from_ci {
1885+
if self.llvm.from_ci {
18811886
self.maybe_download_ci_llvm();
18821887
let ci_llvm = self.ci_llvm_root();
18831888
let link_type = t!(
@@ -1891,7 +1896,7 @@ impl Config {
18911896
false
18921897
}
18931898
});
1894-
self.llvm_link_shared.set(opt);
1899+
self.llvm.link_shared.set(opt);
18951900
llvm_link_shared
18961901
}
18971902

src/bootstrap/dist.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ impl Step for Rustc {
469469
t!(fs::create_dir_all(&dst_dir));
470470

471471
// Copy over lld if it's there
472-
if builder.config.lld_enabled {
472+
if builder.config.llvm.lld_enabled {
473473
let src_dir = builder.sysroot_libdir(compiler, host).parent().unwrap().join("bin");
474474
let rust_lld = exe("rust-lld", compiler.host);
475475
builder.copy(&src_dir.join(&rust_lld), &dst_dir.join(&rust_lld));
@@ -1968,7 +1968,7 @@ fn install_llvm_file(builder: &Builder<'_>, source: &Path, destination: &Path) {
19681968
/// Returns whether the files were actually copied.
19691969
fn maybe_install_llvm(builder: &Builder<'_>, target: TargetSelection, dst_libdir: &Path) -> bool {
19701970
if let Some(config) = builder.config.target_config.get(&target) {
1971-
if config.llvm_config.is_some() && !builder.config.llvm_from_ci {
1971+
if config.llvm_config.is_some() && !builder.config.llvm.from_ci {
19721972
// If the LLVM was externally provided, then we don't currently copy
19731973
// artifacts into the sysroot. This is not necessarily the right
19741974
// choice (in particular, it will require the LLVM dylib to be in
@@ -2090,7 +2090,7 @@ impl Step for BoltInstrument {
20902090
return self.file.clone();
20912091
}
20922092

2093-
if builder.build.config.llvm_from_ci {
2093+
if builder.build.config.llvm.from_ci {
20942094
println!("warning: trying to use BOLT with LLVM from CI, this will probably not work");
20952095
}
20962096

@@ -2142,7 +2142,7 @@ impl Step for BoltOptimize {
21422142
return self.file.clone();
21432143
}
21442144

2145-
if builder.build.config.llvm_from_ci {
2145+
if builder.build.config.llvm.from_ci {
21462146
println!("warning: trying to use BOLT with LLVM from CI, this will probably not work");
21472147
}
21482148

0 commit comments

Comments
 (0)