From 2b861d8b5fc12d073ca8e47fa4e569e54a3b1757 Mon Sep 17 00:00:00 2001 From: Cass Date: Tue, 7 Jul 2026 23:25:20 -0500 Subject: [PATCH] fix(build): link llama-common-base so source builds resolve build-info MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The CUDA/SYCL source builds (v0.17.5) got past the bridge compile and then failed at link with undefined llama_build_number(), llama_commit(), llama_build_info(), llama_compiler(), llama_build_target(). Since b9837, llama.cpp compiles build-info into a dedicated static archive `llama-common-base` that libllama-common depends on but that cmake does not install — so build.rs's install-dir scan linked libllama-common (which references those symbols) but never llama-common-base (which defines them). - build.rs: link libllama-common-base.a explicitly, after the install libs so it follows llama-common on the link line (which references the symbols). - bridge: drop the `#ifndef LLAMA_CPP_PREBUILT` fallback globals — they now collide with the real definitions in llama-common-base. build-info comes from libllama-common in both modes (prebuilt .so / source .a). Verified against a b9837 checkout: llama-common-base.a defines the missing symbols, libllama-common.a references them, and the bridge still compiles in both prebuilt and source modes. Co-Authored-By: Claude Opus 4.8 --- crates/llama-sys/build.rs | 24 +++++++++++++++++++ .../src/autocommit_common_bridge.cpp | 15 ++++-------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/crates/llama-sys/build.rs b/crates/llama-sys/build.rs index af07b31..40ec7dc 100644 --- a/crates/llama-sys/build.rs +++ b/crates/llama-sys/build.rs @@ -672,6 +672,12 @@ fn build_from_source(source_dir: &Path, out_dir: &Path, manifest_dir: &Path) { build_common_bridge(source_dir, manifest_dir, false); emit_common_link_deps(&build_dir); emit_install_link_search_paths(&install_dir); + // b9837 compiles build-info (llama_build_number(), llama_commit(), + // llama_build_info(), and the LLAMA_BUILD_* globals) into a separate + // `llama-common-base` archive that libllama-common depends on but that cmake + // does not install, so the install-dir scan above misses it. Link it + // explicitly — it must come after llama-common, which references the symbols. + emit_build_info_link_deps(&build_dir); if use_cuda { emit_cuda_link_deps(); } @@ -950,6 +956,24 @@ fn has_static_lib(dir: &Path, name: &str) -> bool { dir.join(format!("lib{name}.a")).exists() || dir.join(format!("{name}.lib")).exists() } +/// Link the `llama-common-base` static archive, which holds the generated +/// build-info symbols (`llama_build_number()`, `llama_commit()`, +/// `llama_build_info()`, and the `LLAMA_BUILD_*` globals). Since b9837 these +/// live in their own archive that libllama-common depends on but that cmake does +/// not install, so the install-dir scan misses them. Emitting this after the +/// install libs keeps it after llama-common on the link line, which is required +/// because llama-common references these symbols. +fn emit_build_info_link_deps(build_dir: &Path) { + let common_dir = build_dir.join("common"); + if has_static_lib(&common_dir, "llama-common-base") { + println!( + "cargo:rustc-link-search=native={}", + common_dir.to_string_lossy() + ); + println!("cargo:rustc-link-lib=static=llama-common-base"); + } +} + fn emit_common_link_deps(build_dir: &Path) { let common_dir = build_dir.join("common"); if has_static_lib(&common_dir, "common") { diff --git a/crates/llama-sys/src/autocommit_common_bridge.cpp b/crates/llama-sys/src/autocommit_common_bridge.cpp index 9c9d404..d979298 100644 --- a/crates/llama-sys/src/autocommit_common_bridge.cpp +++ b/crates/llama-sys/src/autocommit_common_bridge.cpp @@ -15,16 +15,11 @@ #include "log.h" #include "sampling.h" -// common.h defines a global `build_info` string derived from these variables. -// In this integration path libcommon.a is linked directly from the build tree -// (where build_info object may not be installed), so provide safe fallbacks. -// When using prebuilt binary releases, libllama-common already provides these. -#ifndef LLAMA_CPP_PREBUILT -int LLAMA_BUILD_NUMBER = 0; -const char * LLAMA_COMMIT = "unknown"; -const char * LLAMA_COMPILER = "unknown"; -const char * LLAMA_BUILD_TARGET = "unknown"; -#endif +// build-info symbols (llama_build_number(), llama_commit(), the LLAMA_BUILD_* +// globals, ...) are provided by libllama-common at link time: from the prebuilt +// libllama-common.so for binary releases, and from the static llama-common-base +// archive for source builds (linked explicitly in build.rs). Defining fallbacks +// here would collide with those real definitions, so we don't. struct autocommit_common_config { common_params params;