Skip to content

Commit fb92796

Browse files
committed
Separate out a rustc_llvm_env function
1 parent f2d9a3d commit fb92796

File tree

1 file changed

+67
-61
lines changed

1 file changed

+67
-61
lines changed

Diff for: src/bootstrap/compile.rs

+67-61
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,6 @@ pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetS
867867
cargo.env("RUSTC_VERIFY_LLVM_IR", "1");
868868
}
869869

870-
// Pass down configuration from the LLVM build into the build of
871-
// rustc_llvm and rustc_codegen_llvm.
872870
//
873871
// Note that this is disabled if LLVM itself is disabled or we're in a check
874872
// build. If we are in a check build we still go ahead here presuming we've
@@ -878,72 +876,80 @@ pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetS
878876
&& (builder.kind != Kind::Check
879877
|| crate::llvm::prebuilt_llvm_config(builder, target).is_ok())
880878
{
881-
if builder.is_rust_llvm(target) {
882-
cargo.env("LLVM_RUSTLLVM", "1");
883-
}
884-
let llvm::LlvmResult { llvm_config, .. } = builder.ensure(llvm::Llvm { target });
885-
cargo.env("LLVM_CONFIG", &llvm_config);
886-
if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) {
887-
cargo.env("CFG_LLVM_ROOT", s);
888-
}
879+
rustc_llvm_env(builder, cargo, target)
880+
}
881+
}
889882

890-
// Some LLVM linker flags (-L and -l) may be needed to link `rustc_llvm`. Its build script
891-
// expects these to be passed via the `LLVM_LINKER_FLAGS` env variable, separated by
892-
// whitespace.
893-
//
894-
// For example:
895-
// - on windows, when `clang-cl` is used with instrumentation, we need to manually add
896-
// clang's runtime library resource directory so that the profiler runtime library can be
897-
// found. This is to avoid the linker errors about undefined references to
898-
// `__llvm_profile_instrument_memop` when linking `rustc_driver`.
899-
let mut llvm_linker_flags = String::new();
900-
if builder.config.llvm_profile_generate && target.contains("msvc") {
901-
if let Some(ref clang_cl_path) = builder.config.llvm_clang_cl {
902-
// Add clang's runtime library directory to the search path
903-
let clang_rt_dir = get_clang_cl_resource_dir(clang_cl_path);
904-
llvm_linker_flags.push_str(&format!("-L{}", clang_rt_dir.display()));
905-
}
906-
}
883+
/// Pass down configuration from the LLVM build into the build of
884+
/// rustc_llvm and rustc_codegen_llvm.
885+
fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelection) {
886+
let target_config = builder.config.target_config.get(&target);
907887

908-
// The config can also specify its own llvm linker flags.
909-
if let Some(ref s) = builder.config.llvm_ldflags {
910-
if !llvm_linker_flags.is_empty() {
911-
llvm_linker_flags.push_str(" ");
912-
}
913-
llvm_linker_flags.push_str(s);
888+
if builder.is_rust_llvm(target) {
889+
cargo.env("LLVM_RUSTLLVM", "1");
890+
}
891+
let llvm::LlvmResult { llvm_config, .. } = builder.ensure(llvm::Llvm { target });
892+
cargo.env("LLVM_CONFIG", &llvm_config);
893+
if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) {
894+
cargo.env("CFG_LLVM_ROOT", s);
895+
}
896+
897+
// Some LLVM linker flags (-L and -l) may be needed to link `rustc_llvm`. Its build script
898+
// expects these to be passed via the `LLVM_LINKER_FLAGS` env variable, separated by
899+
// whitespace.
900+
//
901+
// For example:
902+
// - on windows, when `clang-cl` is used with instrumentation, we need to manually add
903+
// clang's runtime library resource directory so that the profiler runtime library can be
904+
// found. This is to avoid the linker errors about undefined references to
905+
// `__llvm_profile_instrument_memop` when linking `rustc_driver`.
906+
let mut llvm_linker_flags = String::new();
907+
if builder.config.llvm_profile_generate && target.contains("msvc") {
908+
if let Some(ref clang_cl_path) = builder.config.llvm_clang_cl {
909+
// Add clang's runtime library directory to the search path
910+
let clang_rt_dir = get_clang_cl_resource_dir(clang_cl_path);
911+
llvm_linker_flags.push_str(&format!("-L{}", clang_rt_dir.display()));
914912
}
913+
}
915914

916-
// Set the linker flags via the env var that `rustc_llvm`'s build script will read.
915+
// The config can also specify its own llvm linker flags.
916+
if let Some(ref s) = builder.config.llvm_ldflags {
917917
if !llvm_linker_flags.is_empty() {
918-
cargo.env("LLVM_LINKER_FLAGS", llvm_linker_flags);
918+
llvm_linker_flags.push_str(" ");
919919
}
920+
llvm_linker_flags.push_str(s);
921+
}
920922

921-
// Building with a static libstdc++ is only supported on linux right now,
922-
// not for MSVC or macOS
923-
if builder.config.llvm_static_stdcpp
924-
&& !target.contains("freebsd")
925-
&& !target.contains("msvc")
926-
&& !target.contains("apple")
927-
&& !target.contains("solaris")
928-
{
929-
let file = compiler_file(
930-
builder,
931-
builder.cxx(target).unwrap(),
932-
target,
933-
CLang::Cxx,
934-
"libstdc++.a",
935-
);
936-
cargo.env("LLVM_STATIC_STDCPP", file);
937-
}
938-
if builder.llvm_link_shared() {
939-
cargo.env("LLVM_LINK_SHARED", "1");
940-
}
941-
if builder.config.llvm_use_libcxx {
942-
cargo.env("LLVM_USE_LIBCXX", "1");
943-
}
944-
if builder.config.llvm_optimize && !builder.config.llvm_release_debuginfo {
945-
cargo.env("LLVM_NDEBUG", "1");
946-
}
923+
// Set the linker flags via the env var that `rustc_llvm`'s build script will read.
924+
if !llvm_linker_flags.is_empty() {
925+
cargo.env("LLVM_LINKER_FLAGS", llvm_linker_flags);
926+
}
927+
928+
// Building with a static libstdc++ is only supported on linux right now,
929+
// not for MSVC or macOS
930+
if builder.config.llvm_static_stdcpp
931+
&& !target.contains("freebsd")
932+
&& !target.contains("msvc")
933+
&& !target.contains("apple")
934+
&& !target.contains("solaris")
935+
{
936+
let file = compiler_file(
937+
builder,
938+
builder.cxx(target).unwrap(),
939+
target,
940+
CLang::Cxx,
941+
"libstdc++.a",
942+
);
943+
cargo.env("LLVM_STATIC_STDCPP", file);
944+
}
945+
if builder.llvm_link_shared() {
946+
cargo.env("LLVM_LINK_SHARED", "1");
947+
}
948+
if builder.config.llvm_use_libcxx {
949+
cargo.env("LLVM_USE_LIBCXX", "1");
950+
}
951+
if builder.config.llvm_optimize && !builder.config.llvm_release_debuginfo {
952+
cargo.env("LLVM_NDEBUG", "1");
947953
}
948954
}
949955

0 commit comments

Comments
 (0)