Skip to content

Commit a0d8be5

Browse files
hongkongkiwiclaude
andcommitted
feat(llama-cpp-sys-2): add dynamic tools CMakeLists.txt generation for split-model-loading
This commit adds the scalable dynamic tools building system to the split-model-loading branch: - Adds generate_tools_cmake() function to dynamically create tools/CMakeLists.txt - Only builds tools for enabled features (solving PR utilityai#806 issue) - Split model loading doesn't require tools but maintains architecture consistency - Includes tools/CMakeLists.txt in Cargo.toml for build system compatibility - Uses feature-based conditional compilation for future extensibility This creates a merge-friendly architecture where each feature branch can extend tool building without conflicts. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 8f3cb9b commit a0d8be5

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

llama-cpp-sys-2/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ include = [
5151
"/llama.cpp/ggml/CMakeLists.txt",
5252
"/llama.cpp/ggml/src/CMakeLists.txt",
5353
"/llama.cpp/src/CMakeLists.txt",
54+
"/llama.cpp/tools/CMakeLists.txt",
5455

5556
"/llama.cpp/cmake",
5657
"/llama.cpp/ggml/cmake",

llama-cpp-sys-2/build.rs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use cmake::Config;
22
use glob::glob;
33
use std::env;
4+
use std::fs;
45
use std::path::{Path, PathBuf};
56
use std::process::Command;
67
use walkdir::DirEntry;
@@ -197,6 +198,52 @@ fn is_hidden(e: &DirEntry) -> bool {
197198
.unwrap_or_default()
198199
}
199200

201+
/// Generate a dynamic tools CMakeLists.txt based on enabled features
202+
/// This approach allows each feature branch to add their own tool without conflicts
203+
fn generate_tools_cmake() {
204+
let mut cmake_content = String::from(
205+
r#"# Auto-generated tools CMakeLists.txt based on enabled features
206+
# This file is created dynamically to only build tools for enabled features
207+
208+
# dependencies
209+
find_package(Threads REQUIRED)
210+
211+
# third-party
212+
# ...
213+
214+
# flags
215+
llama_add_compile_flags()
216+
217+
# tools - only build what's needed based on enabled features
218+
if (NOT EMSCRIPTEN)
219+
"#);
220+
221+
// Add tools based on enabled features
222+
if cfg!(feature = "mtmd") {
223+
cmake_content.push_str(" add_subdirectory(mtmd)\n");
224+
}
225+
226+
// Future feature branches can add their tools here:
227+
// if cfg!(feature = "rpc") {
228+
// cmake_content.push_str(" add_subdirectory(rpc)\n");
229+
// }
230+
// if cfg!(feature = "server") {
231+
// cmake_content.push_str(" add_subdirectory(server)\n");
232+
// }
233+
// if cfg!(feature = "quantize") {
234+
// cmake_content.push_str(" add_subdirectory(quantize)\n");
235+
// }
236+
237+
// Split model loading doesn't need any tools - it's just a library feature
238+
239+
cmake_content.push_str("endif()\n");
240+
241+
// Write the generated CMakeLists.txt
242+
let tools_cmake_path = Path::new("llama.cpp/tools/CMakeLists.txt");
243+
fs::write(tools_cmake_path, cmake_content)
244+
.expect("Failed to write generated tools CMakeLists.txt");
245+
}
246+
200247
fn main() {
201248
println!("cargo:rerun-if-changed=build.rs");
202249

@@ -448,10 +495,20 @@ fn main() {
448495
config.define("LLAMA_BUILD_TOOLS", "OFF");
449496
config.define("LLAMA_CURL", "OFF");
450497

451-
if cfg!(feature = "mtmd") {
498+
// Generate dynamic tools CMakeLists.txt based on enabled tool features
499+
let any_tool_features = cfg!(feature = "mtmd")
500+
// Future tool features can be added here by other branches:
501+
// || cfg!(feature = "rpc")
502+
// || cfg!(feature = "server")
503+
// || cfg!(feature = "quantize")
504+
;
505+
506+
if any_tool_features {
452507
config.define("LLAMA_BUILD_COMMON", "ON");
453-
// mtmd support in llama-cpp is within the tools directory
454508
config.define("LLAMA_BUILD_TOOLS", "ON");
509+
510+
// Generate the tools CMakeLists.txt with only enabled features
511+
generate_tools_cmake();
455512
}
456513

457514
// Pass CMAKE_ environment variables down to CMake

0 commit comments

Comments
 (0)