|
1 | 1 | use cmake::Config;
|
2 | 2 | use glob::glob;
|
3 | 3 | use std::env;
|
| 4 | +use std::fs; |
4 | 5 | use std::path::{Path, PathBuf};
|
5 | 6 | use std::process::Command;
|
6 | 7 | use walkdir::DirEntry;
|
@@ -197,6 +198,50 @@ fn is_hidden(e: &DirEntry) -> bool {
|
197 | 198 | .unwrap_or_default()
|
198 | 199 | }
|
199 | 200 |
|
| 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 | + |
200 | 245 | fn main() {
|
201 | 246 | println!("cargo:rerun-if-changed=build.rs");
|
202 | 247 |
|
@@ -448,10 +493,20 @@ fn main() {
|
448 | 493 | config.define("LLAMA_BUILD_TOOLS", "OFF");
|
449 | 494 | config.define("LLAMA_CURL", "OFF");
|
450 | 495 |
|
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 { |
452 | 505 | config.define("LLAMA_BUILD_COMMON", "ON");
|
453 |
| - // mtmd support in llama-cpp is within the tools directory |
454 | 506 | config.define("LLAMA_BUILD_TOOLS", "ON");
|
| 507 | + |
| 508 | + // Generate the tools CMakeLists.txt with only enabled features |
| 509 | + generate_tools_cmake(); |
455 | 510 | }
|
456 | 511 |
|
457 | 512 | // Pass CMAKE_ environment variables down to CMake
|
|
0 commit comments