Skip to content

Commit 1d61645

Browse files
committed
Apply PR feedback
1 parent 35cfbcd commit 1d61645

File tree

4 files changed

+101
-86
lines changed

4 files changed

+101
-86
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ parallel = ["dep:libc", "dep:jobserver"]
3333
# This is a placeholder feature for people who incorrectly used `cc` with `features = ["jobserver"]`
3434
# so that they aren't broken. This has never enabled `parallel`, so we won't do that.
3535
jobserver = []
36+
# Visual Studio doesn't have clang-cl (Clang/LLVM) installed by default as it's an optional module.
37+
# While GitHub Actions runners have it installed, it might cause some failing tests if you don't
38+
# have it. Run the tests with "--features disable-clang-cl-tests" if you want to disable related tests.
39+
disable-clang-cl-tests = []
3640

3741
[dev-dependencies]
3842
tempfile = "3"

src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3802,7 +3802,12 @@ impl Build {
38023802
self.cargo_output
38033803
.print_metadata(&format_args!("cargo:rerun-if-env-changed={v}"));
38043804
}
3805-
let r = env::var_os(v).map(Arc::from);
3805+
let r = self
3806+
.env
3807+
.iter()
3808+
.find(|(k, _)| k.as_ref() == v)
3809+
.map(|(_, value)| value.clone())
3810+
.or_else(|| env::var_os(v).map(Arc::from));
38063811
self.cargo_output.print_metadata(&format_args!(
38073812
"{} = {}",
38083813
v,

src/windows/find_tools.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,9 +554,10 @@ mod impl_ {
554554
// E.g. C:\...\VC\Tools\LLVM\x64\bin\clang.exe
555555
base_path.push("bin");
556556
base_path.push(tool);
557+
let clang_cl = tool.contains("clang-cl");
557558
base_path
558559
.is_file()
559-
.then(|| Tool::with_family(base_path, ToolFamily::Msvc { clang_cl: true }))
560+
.then(|| Tool::with_family(base_path, ToolFamily::Msvc { clang_cl }))
560561
})
561562
.next()
562563
}
@@ -1139,8 +1140,6 @@ mod impl_ {
11391140
use std::path::Path;
11401141
// Import the find function from the module level
11411142
use crate::windows::find_tools::find;
1142-
// Import StdEnvGetter from the parent module
1143-
use crate::windows::find_tools::StdEnvGetter;
11441143

11451144
fn host_arch_to_string(host_arch_value: u16) -> &'static str {
11461145
match host_arch_value {
@@ -1203,7 +1202,11 @@ mod impl_ {
12031202
}
12041203

12051204
#[test]
1205+
#[cfg(not(feature = "disable-clang-cl-tests"))]
12061206
fn test_find_llvm_tools() {
1207+
// Import StdEnvGetter from the parent module
1208+
use crate::windows::find_tools::StdEnvGetter;
1209+
12071210
// Test the actual find_llvm_tool function with various LLVM tools
12081211
// This test assumes CI environment has Visual Studio + Clang installed
12091212
// We test against x64 target since clang can cross-compile to any target

tests/test.rs

Lines changed: 85 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -852,96 +852,99 @@ fn clang_android() {
852852
}
853853
}
854854

855-
#[test]
856855
#[cfg(windows)]
857-
fn msvc_prefer_clang_cl_over_msvc_disabled_by_default() {
858-
reset_env();
856+
#[cfg(not(feature = "disable-clang-cl-tests"))]
857+
mod msvc_clang_cl_tests {
858+
use super::{reset_env, Test};
859859

860-
let test = Test::msvc_autodetect();
860+
#[test]
861+
fn msvc_prefer_clang_cl_over_msvc_disabled_by_default() {
862+
reset_env();
861863

862-
// When prefer_clang_cl_over_msvc is not called (default false), should use MSVC
863-
let compiler = test
864-
.gcc()
865-
.try_get_compiler()
866-
.expect("Failed to get compiler");
867-
868-
// By default, should be using MSVC (cl.exe) and NOT clang-cl
869-
assert!(compiler.is_like_msvc(), "Should use MSVC by default");
870-
assert!(
871-
!compiler.is_like_clang_cl(),
872-
"Should not use clang-cl by default"
873-
);
874-
}
864+
let test = Test::msvc_autodetect();
875865

876-
#[test]
877-
#[cfg(windows)]
878-
fn msvc_prefer_clang_cl_over_msvc_enabled() {
879-
reset_env();
866+
// When prefer_clang_cl_over_msvc is not called (default false), should use MSVC
867+
let compiler = test
868+
.gcc()
869+
.try_get_compiler()
870+
.expect("Failed to get compiler");
871+
872+
// By default, should be using MSVC (cl.exe) and NOT clang-cl
873+
assert!(compiler.is_like_msvc(), "Should use MSVC by default");
874+
assert!(
875+
!compiler.is_like_clang_cl(),
876+
"Should not use clang-cl by default"
877+
);
878+
}
880879

881-
let test = Test::msvc_autodetect();
880+
#[test]
881+
fn msvc_prefer_clang_cl_over_msvc_enabled() {
882+
reset_env();
882883

883-
let compiler = test
884-
.gcc()
885-
// When prefer_clang_cl_over_msvc is true, should use clang-cl.exe
886-
.prefer_clang_cl_over_msvc(true)
887-
.try_get_compiler()
888-
.expect("Failed to get compiler");
889-
890-
assert!(
891-
compiler.is_like_clang_cl(),
892-
"clang-cl.exe should be identified as clang-cl-like, got {:?}",
893-
compiler
894-
);
895-
assert!(
896-
compiler.is_like_msvc(),
897-
"clang-cl should still be MSVC-like"
898-
);
899-
}
884+
let test = Test::msvc_autodetect();
900885

901-
#[test]
902-
#[cfg(windows)]
903-
fn msvc_prefer_clang_cl_over_msvc_respects_explicit_cc_env() {
904-
reset_env();
886+
let compiler = test
887+
.gcc()
888+
// When prefer_clang_cl_over_msvc is true, should use clang-cl.exe
889+
.prefer_clang_cl_over_msvc(true)
890+
.try_get_compiler()
891+
.expect("Failed to get compiler");
892+
893+
assert!(
894+
compiler.is_like_clang_cl(),
895+
"clang-cl.exe should be identified as clang-cl-like, got {:?}",
896+
compiler
897+
);
898+
assert!(
899+
compiler.is_like_msvc(),
900+
"clang-cl should still be MSVC-like"
901+
);
902+
}
905903

906-
let test = Test::msvc_autodetect();
907-
let compiler = test
908-
.gcc()
909-
// We can't set the CC=cl.exe environment variable directly in the test as it's removed
910-
// in mod.rs, so we simulate it by setting the compiler directly
911-
.compiler("cl.exe")
912-
.prefer_clang_cl_over_msvc(true)
913-
.try_get_compiler()
914-
.expect("Failed to get compiler");
915-
916-
// The preference should not override explicit compiler setting
917-
assert!(compiler.is_like_msvc(), "Should still be MSVC-like");
918-
assert!(
919-
!compiler.is_like_clang_cl(),
920-
"Should NOT use clang-cl when CC is explicitly set to cl.exe, got {:?}",
921-
compiler
922-
);
923-
}
904+
#[test]
905+
fn msvc_prefer_clang_cl_over_msvc_respects_explicit_cc_env() {
906+
reset_env();
924907

925-
#[test]
926-
#[cfg(windows)]
927-
fn msvc_prefer_clang_cl_over_msvc_cpp_mode() {
928-
reset_env();
908+
let test = Test::msvc_autodetect();
929909

930-
let test = Test::msvc_autodetect();
931-
let compiler = test
932-
.gcc()
933-
.cpp(true)
934-
.prefer_clang_cl_over_msvc(true)
935-
.try_get_compiler()
936-
.expect("Failed to get compiler");
937-
938-
// Verify clang-cl.exe works correctly in C++ mode
939-
assert!(
940-
compiler.is_like_clang_cl(),
941-
"clang-cl.exe should be identified as clang-cl-like in C++ mode"
942-
);
943-
assert!(
944-
compiler.is_like_msvc(),
945-
"clang-cl should still be MSVC-like in C++ mode"
946-
);
910+
//std::env::set_var("CC", "cl.exe");
911+
let compiler = test
912+
.gcc()
913+
.__set_env("CC", "cl.exe")
914+
.prefer_clang_cl_over_msvc(true)
915+
.try_get_compiler()
916+
.expect("Failed to get compiler");
917+
918+
// The preference should not override explicit compiler setting
919+
assert!(compiler.is_like_msvc(), "Should still be MSVC-like");
920+
assert!(
921+
!compiler.is_like_clang_cl(),
922+
"Should NOT use clang-cl when CC is explicitly set to cl.exe, got {:?}",
923+
compiler
924+
);
925+
std::env::remove_var("CC"); // Clean up after test
926+
}
927+
928+
#[test]
929+
fn msvc_prefer_clang_cl_over_msvc_cpp_mode() {
930+
reset_env();
931+
932+
let test = Test::msvc_autodetect();
933+
let compiler = test
934+
.gcc()
935+
.cpp(true)
936+
.prefer_clang_cl_over_msvc(true)
937+
.try_get_compiler()
938+
.expect("Failed to get compiler");
939+
940+
// Verify clang-cl.exe works correctly in C++ mode
941+
assert!(
942+
compiler.is_like_clang_cl(),
943+
"clang-cl.exe should be identified as clang-cl-like in C++ mode"
944+
);
945+
assert!(
946+
compiler.is_like_msvc(),
947+
"clang-cl should still be MSVC-like in C++ mode"
948+
);
949+
}
947950
}

0 commit comments

Comments
 (0)