Skip to content

Commit b08e6ec

Browse files
committed
un-invert target_cpu check on linux/aarch64 builds, clean up code
1 parent 9d39309 commit b08e6ec

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

llama-cpp-sys-2/build.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -508,17 +508,7 @@ fn main() {
508508
}
509509
}
510510

511-
// in this next bit, we select which cpu-specific features to compile for
512-
// first check for target-cpu=native
513-
let has_native_target_cpu = std::env::var("CARGO_ENCODED_RUSTFLAGS")
514-
.map(|rustflags| {
515-
rustflags
516-
.split('\x1f')
517-
.any(|f| f.contains("target-cpu=native"))
518-
})
519-
.unwrap_or(false);
520-
521-
// Also extract the target-cpu value if specified (e.g., x86-64, x86-64-v2, etc.)
511+
// extract the target-cpu config value, if specified
522512
let target_cpu = std::env::var("CARGO_ENCODED_RUSTFLAGS")
523513
.ok()
524514
.and_then(|rustflags| {
@@ -529,25 +519,27 @@ fn main() {
529519
.map(|s| s.to_string())
530520
});
531521

532-
if has_native_target_cpu {
522+
if target_cpu == Some("native".into()) {
533523
debug_log!("Detected target-cpu=native, compiling with GGML_NATIVE");
534524
config.define("GGML_NATIVE", "ON");
535525
}
536-
// if native isn't specified, enable specific features for ggml
537-
// Get the target features as a comma-separated string
538-
else if let Ok(features) = std::env::var("CARGO_CFG_TARGET_FEATURE") {
539-
debug_log!("Compiling with target features: {}", features);
526+
// if native isn't specified, enable specific features for ggml instead
527+
else {
528+
// rust code isn't using `target-cpu=native`, so llama.cpp shouldn't use GGML_NATIVE either
540529
config.define("GGML_NATIVE", "OFF");
541530

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
531+
// if `target-cpu` is set set, also set -march for llama.cpp to the same value
544532
if let Some(ref cpu) = target_cpu {
545533
debug_log!("Setting baseline architecture: -march={}", cpu);
546-
// Pass the baseline architecture to CMake's C and CXX compilers
547534
config.cflag(&format!("-march={}", cpu));
548535
config.cxxflag(&format!("-march={}", cpu));
549536
}
550537

538+
// I expect this env var to always be present
539+
let features = std::env::var("CARGO_CFG_TARGET_FEATURE")
540+
.expect("Env var CARGO_CFG_TARGET_FEATURE not found.");
541+
debug_log!("Compiling with target features: {}", features);
542+
551543
// list of rust target_features here:
552544
// https://doc.rust-lang.org/reference/attributes/codegen.html#the-target_feature-attribute
553545
// GGML config flags have been found by looking at:
@@ -714,7 +706,7 @@ fn main() {
714706

715707
if matches!(target_os, TargetOs::Linux)
716708
&& target_triple.contains("aarch64")
717-
&& has_native_target_cpu
709+
&& target_cpu != Some("native".into())
718710
{
719711
// If the target-cpu is not specified as native, we take off the native ARM64 support.
720712
// It is useful in docker environments where the native feature is not enabled.

0 commit comments

Comments
 (0)