diff --git a/llama-cpp-bindings-build/src/cmake_config.rs b/llama-cpp-bindings-build/src/cmake_config.rs index 66d2f996..37e83494 100644 --- a/llama-cpp-bindings-build/src/cmake_config.rs +++ b/llama-cpp-bindings-build/src/cmake_config.rs @@ -32,6 +32,7 @@ pub fn configure_and_build(context: &BuildContext) -> Result Option<&'static str> { } } +const fn msvc_exception_handling_flag(target_os: TargetOs) -> Option<&'static str> { + if target_os.is_msvc() { + Some("/EHsc") + } else { + None + } +} + +fn configure_msvc_exception_handling(config: &mut Config, target_os: TargetOs) { + let Some(flag) = msvc_exception_handling_flag(target_os) else { + return; + }; + + config.cxxflag(flag); +} + fn msvc_config_flags(target_os: TargetOs, profile: &str) -> Option<&'static str> { if !target_os.is_msvc() { return None; @@ -294,6 +311,31 @@ fn configure_system_ggml(config: &mut Config) -> Result<(), BuildError> { Ok(()) } +#[cfg(test)] +mod msvc_exception_handling_tests { + use crate::target_os::TargetOs; + use crate::windows_variant::WindowsVariant; + + use super::msvc_exception_handling_flag; + + #[test] + fn msvc_targets_compile_llama_cpp_with_unwind_semantics() { + assert_eq!( + msvc_exception_handling_flag(TargetOs::Windows(WindowsVariant::Msvc)), + Some("/EHsc") + ); + } + + #[test] + fn targets_without_msvc_keep_their_toolchain_default_exception_handling() { + assert_eq!(msvc_exception_handling_flag(TargetOs::Linux), None); + assert_eq!( + msvc_exception_handling_flag(TargetOs::Windows(WindowsVariant::Other)), + None + ); + } +} + #[cfg(test)] mod msvc_config_flag_tests { use crate::target_os::TargetOs;