Skip to content

Commit 711d3e8

Browse files
committed
pass target cpu to cmake cflags march
1 parent e01e0d7 commit 711d3e8

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

llama-cpp-sys-2/build.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,18 @@ fn main() {
517517
.any(|f| f.contains("target-cpu=native"))
518518
})
519519
.unwrap_or(false);
520+
521+
// Also extract the target-cpu value if specified (e.g., x86-64, x86-64-v2, etc.)
522+
let target_cpu = std::env::var("CARGO_ENCODED_RUSTFLAGS")
523+
.ok()
524+
.and_then(|rustflags| {
525+
rustflags
526+
.split('\x1f')
527+
.find(|f| f.contains("target-cpu=") && !f.contains("target-cpu=native"))
528+
.and_then(|f| f.split("target-cpu=").nth(1))
529+
.map(|s| s.to_string())
530+
});
531+
520532
if has_native_target_cpu {
521533
debug_log!("Detected target-cpu=native, compiling with GGML_NATIVE");
522534
config.define("GGML_NATIVE", "ON");
@@ -525,6 +537,17 @@ fn main() {
525537
// Get the target features as a comma-separated string
526538
else if let Ok(features) = std::env::var("CARGO_CFG_TARGET_FEATURE") {
527539
debug_log!("Compiling with target features: {}", features);
540+
config.define("GGML_NATIVE", "OFF");
541+
542+
// Set baseline architecture from target-cpu if specified
543+
// This is critical to prevent the compiler from auto-vectorizing to the build host's capabilities
544+
if let Some(ref cpu) = target_cpu {
545+
debug_log!("Setting baseline architecture: -march={}", cpu);
546+
// Pass the baseline architecture to CMake's C and CXX compilers
547+
config.cflag(&format!("-march={}", cpu));
548+
config.cxxflag(&format!("-march={}", cpu));
549+
}
550+
528551
// list of rust target_features here:
529552
// https://doc.rust-lang.org/reference/attributes/codegen.html#the-target_feature-attribute
530553
// GGML config flags have been found by looking at:

0 commit comments

Comments
 (0)