Skip to content

Commit 22e4c4d

Browse files
hongkongkiwiclaude
andcommitted
fix(llama-cpp-sys-2): dynamic tools CMakeLists.txt for selective tool building
Replace static tools CMakeLists.txt approach with dynamic generation based on enabled features. This creates a scalable, merge-friendly solution for our feature branches. Key improvements: - Dynamic CMakeLists.txt generation via generate_tools_cmake() function - Only builds tools for enabled features (currently just MTMD) - Designed for easy extension by other feature branches - No static files to conflict during merges - Clear extension points with commented examples Architecture: - generate_tools_cmake() creates tools/CMakeLists.txt at build time - any_tool_features flag determines if tools directory should be built - Each feature branch can add their tool by uncommenting their section This solves the original PR utilityai#806 issue (avoiding building all tools) while providing a foundation for RPC, server, quantize, and other tool features. Future branches can easily add their tools by: 1. Adding their feature to any_tool_features check 2. Uncommenting their add_subdirectory() line in generate_tools_cmake() 3. Including their tool's CMakeLists.txt in Cargo.toml 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent a6c142a commit 22e4c4d

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

llama-cpp-sys-2/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ include = [
5555
"/llama.cpp/cmake",
5656
"/llama.cpp/ggml/cmake",
5757
"/llama.cpp/common/cmake",
58+
59+
# Required for MTMD feature (specific tool only)
60+
"/llama.cpp/tools/mtmd/CMakeLists.txt",
5861
]
5962

6063
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

llama-cpp-sys-2/build.rs

Lines changed: 57 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,50 @@ 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+
cmake_content.push_str("endif()\n");
238+
239+
// Write the generated CMakeLists.txt
240+
let tools_cmake_path = Path::new("llama.cpp/tools/CMakeLists.txt");
241+
fs::write(tools_cmake_path, cmake_content)
242+
.expect("Failed to write generated tools CMakeLists.txt");
243+
}
244+
200245
fn main() {
201246
println!("cargo:rerun-if-changed=build.rs");
202247

@@ -448,10 +493,20 @@ fn main() {
448493
config.define("LLAMA_BUILD_TOOLS", "OFF");
449494
config.define("LLAMA_CURL", "OFF");
450495

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

457512
// Pass CMAKE_ environment variables down to CMake

0 commit comments

Comments
 (0)