-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
348 lines (310 loc) · 13.7 KB
/
build.rs
File metadata and controls
348 lines (310 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Build script for transmutation
// Handles C++ compilation and linking for docling-parse FFI
use std::process::Command;
fn main() {
// Embed icon and metadata into Windows executable (only for CLI binary builds)
// When used as a library dependency, skip resource compilation to avoid
// duplicate resource errors (see: https://github.com/hivellm/transmutation/issues/3)
#[cfg(all(target_os = "windows", feature = "cli"))]
{
let mut res = winres::WindowsResource::new();
res.set_icon("assets/icon.ico");
res.set("ProductName", "Transmutation");
res.set(
"FileDescription",
"High-performance document conversion engine for AI/LLM embeddings",
);
res.set("CompanyName", "HiveLLM Team");
res.set("LegalCopyright", "Copyright (c) 2025 HiveLLM Team");
res.set("OriginalFilename", "transmutation.exe");
if let Err(e) = res.compile() {
eprintln!("Warning: Failed to compile Windows resources: {e}");
}
}
// Check for optional external dependencies
check_external_dependencies();
// Only build C++ if docling-ffi feature is enabled
#[cfg(feature = "docling-ffi")]
{
println!("cargo:rerun-if-changed=cpp/docling_ffi.cpp");
println!("cargo:rerun-if-changed=cpp/docling_ffi.h");
println!("cargo:rerun-if-changed=cpp/CMakeLists.txt");
// Platform-specific library paths and names
#[cfg(target_os = "windows")]
{
use std::path::Path;
// Detect architecture
let arch = if cfg!(target_arch = "x86_64") {
"x86"
} else {
"ARM"
};
let build_dir = format!("build_windows_{arch}");
let lib_path_str = format!("{build_dir}/Release/docling_ffi.lib");
let libs_path_str = format!("libs/docling-ffi-windows_{arch}.lib");
let lib_path = Path::new(&lib_path_str);
let libs_path = Path::new(&libs_path_str);
// Check if library exists in either location
if !lib_path.exists() && !libs_path.exists() {
eprintln!("========================================");
eprintln!("ERROR: docling_ffi.lib not found!");
eprintln!("========================================");
eprintln!("Please run: .\\build_cpp.ps1");
eprintln!("This will build the C++ library for Windows.");
eprintln!("Or use WSL for full functionality:");
eprintln!(
" wsl -d Ubuntu-24.04 -- bash -c 'cd /mnt/f/Node/hivellm/transmutation && ./build_cpp.sh'"
);
eprintln!("========================================");
panic!("C++ library not built");
}
println!("cargo:rustc-link-search=native=libs");
println!("cargo:rustc-link-search=native={build_dir}/Release");
println!(
"cargo:rustc-link-search=native=docling-parse/build_windows_{arch}_docling/Release"
);
println!("cargo:rustc-link-lib=dylib=docling_ffi");
// Copy DLL to target directory for runtime
let dll_libs_path = format!("libs/docling-ffi-windows_{arch}.dll");
let dll_build_path = format!("{build_dir}/Release/docling_ffi.dll");
let dll_src_str = if Path::new(&dll_libs_path).exists() {
dll_libs_path
} else {
dll_build_path
};
let dll_src = Path::new(&dll_src_str);
let target_dir = std::env::var("OUT_DIR").unwrap();
let dll_dst = Path::new(&target_dir).join("../../../docling_ffi.dll");
if dll_src.exists() {
let _ = std::fs::copy(dll_src, dll_dst);
}
}
#[cfg(target_os = "linux")]
{
use std::path::Path;
// Detect architecture
let arch = if cfg!(target_arch = "x86_64") {
"x86"
} else if cfg!(target_arch = "aarch64") {
"ARM"
} else {
"x86"
};
let build_dir = format!("build_linux_{arch}");
// Check if library exists in any known location
let lib_build = format!("{build_dir}/libdocling_ffi.so");
let lib_libs = format!("libs/libdocling_ffi.so");
let lib_legacy = "cpp/build/libdocling_ffi.so";
let lib_docling = format!("docling-parse/build_linux_{arch}_docling/libdocling_ffi.so");
if !Path::new(&lib_build).exists()
&& !Path::new(&lib_libs).exists()
&& !Path::new(lib_legacy).exists()
&& !Path::new(&lib_docling).exists()
{
eprintln!("========================================");
eprintln!("ERROR: libdocling_ffi.so not found!");
eprintln!("========================================");
eprintln!("The 'docling-ffi' feature requires a pre-built C++ library.");
eprintln!("Please build it first: ./scripts/build_cpp.sh");
eprintln!();
eprintln!("If installing via 'cargo install', note that docling-ffi");
eprintln!("requires cloning the repo and building the C++ library locally.");
eprintln!("See: https://github.com/hivellm/transmutation/blob/main/docs/FFI.md");
eprintln!("========================================");
panic!("C++ library not built - see instructions above");
}
// Library search paths
println!("cargo:rustc-link-search=native=libs"); // Pre-built library location
println!("cargo:rustc-link-search=native={build_dir}");
println!("cargo:rustc-link-search=native=cpp/build"); // Legacy location
println!("cargo:rustc-link-search=native=docling-parse/build_linux_{arch}_docling");
println!("cargo:rustc-link-lib=dylib=docling_ffi");
// Add rpath for runtime library loading
println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN");
println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN/../libs");
println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN/../{build_dir}");
}
#[cfg(target_os = "macos")]
{
use std::path::Path;
// Detect architecture
let arch = if cfg!(target_arch = "x86_64") {
"x86"
} else if cfg!(target_arch = "aarch64") {
"ARM"
} else {
"ARM"
}; // Default to ARM for Apple Silicon
let build_dir = format!("build_macos_{arch}");
// Check if library exists in any known location
let lib_build = format!("{build_dir}/libdocling_ffi.dylib");
let lib_libs = format!("libs/libdocling_ffi.dylib");
let lib_legacy = "cpp/build/libdocling_ffi.dylib";
let lib_docling =
format!("docling-parse/build_macos_{arch}_docling/libdocling_ffi.dylib");
if !Path::new(&lib_build).exists()
&& !Path::new(&lib_libs).exists()
&& !Path::new(lib_legacy).exists()
&& !Path::new(&lib_docling).exists()
{
eprintln!("========================================");
eprintln!("ERROR: libdocling_ffi.dylib not found!");
eprintln!("========================================");
eprintln!("The 'docling-ffi' feature requires a pre-built C++ library.");
eprintln!("Please build it first: ./scripts/build_cpp.sh");
eprintln!();
eprintln!("If installing via 'cargo install', note that docling-ffi");
eprintln!("requires cloning the repo and building the C++ library locally.");
eprintln!("See: https://github.com/hivellm/transmutation/blob/main/docs/FFI.md");
eprintln!("========================================");
panic!("C++ library not built - see instructions above");
}
println!("cargo:rustc-link-search=native=libs");
println!("cargo:rustc-link-search=native={build_dir}");
println!("cargo:rustc-link-search=native=cpp/build"); // Legacy location
println!("cargo:rustc-link-search=native=docling-parse/build_macos_{arch}_docling");
println!("cargo:rustc-link-lib=dylib=docling_ffi");
// Add rpath for runtime library loading on macOS
println!("cargo:rustc-link-arg=-Wl,-rpath,@loader_path");
println!("cargo:rustc-link-arg=-Wl,-rpath,@loader_path/../libs");
println!("cargo:rustc-link-arg=-Wl,-rpath,@loader_path/../{build_dir}");
}
// Link against standard C++ library
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
println!("cargo:rustc-link-lib=dylib=stdc++");
}
}
}
/// Check for optional external dependencies and provide helpful messages
fn check_external_dependencies() {
#[allow(unused_mut)]
let mut warnings: Vec<(&str, &str, String)> = Vec::new();
// Check pdf-to-image feature dependencies
#[cfg(feature = "pdf-to-image")]
{
if !command_exists("pdftoppm") {
warnings.push((
"pdftoppm (poppler-utils)",
"PDF → Image conversion",
get_install_command("poppler-utils"),
));
}
}
// Check office feature dependencies for image conversion
#[cfg(all(feature = "office", feature = "pdf-to-image"))]
{
if !command_exists("libreoffice") && !command_exists("soffice") {
warnings.push((
"LibreOffice",
"DOCX/PPTX → Image conversion",
get_install_command("libreoffice"),
));
}
}
// Check tesseract feature dependencies
#[cfg(feature = "tesseract")]
{
if !command_exists("tesseract") {
warnings.push((
"Tesseract OCR",
"Image → Text (OCR)",
get_install_command("tesseract"),
));
}
}
// Check ffmpeg feature dependencies
#[cfg(any(feature = "audio", feature = "video"))]
{
if !command_exists("ffmpeg") {
warnings.push((
"FFmpeg",
"Audio/Video processing",
get_install_command("ffmpeg"),
));
}
}
// Print warnings if any dependencies are missing
if !warnings.is_empty() {
eprintln!();
eprintln!("╔════════════════════════════════════════════════════════════╗");
eprintln!("║ ⚠️ Optional External Dependencies Missing ║");
eprintln!("╚════════════════════════════════════════════════════════════╝");
eprintln!();
eprintln!("Transmutation will compile, but some features won't work:");
eprintln!();
for (tool, feature, install_cmd) in &warnings {
eprintln!(" ❌ {tool}: {feature}");
eprintln!(" Install: {install_cmd}");
}
eprintln!();
eprintln!("📖 For detailed installation instructions:");
eprintln!(" https://github.com/yourusername/transmutation/blob/main/install/README.md");
eprintln!();
eprintln!("💡 Quick install (all dependencies):");
eprintln!("{}", get_quick_install_all());
eprintln!();
}
}
/// Check if a command exists in PATH
#[allow(dead_code)]
fn command_exists(cmd: &str) -> bool {
Command::new(if cfg!(target_os = "windows") {
"where"
} else {
"which"
})
.arg(cmd)
.output()
.map(|output| output.status.success())
.unwrap_or(false)
}
/// Get platform-specific install command for a tool
#[allow(dead_code)]
fn get_install_command(tool: &str) -> String {
#[cfg(target_os = "linux")]
{
match tool {
"poppler-utils" => "sudo apt-get install poppler-utils".to_string(),
"libreoffice" => "sudo apt-get install libreoffice".to_string(),
"tesseract" => "sudo apt-get install tesseract-ocr".to_string(),
"ffmpeg" => "sudo apt-get install ffmpeg".to_string(),
_ => format!("sudo apt-get install {tool}"),
}
}
#[cfg(target_os = "macos")]
{
match tool {
"poppler-utils" => "brew install poppler".to_string(),
"libreoffice" => "brew install --cask libreoffice".to_string(),
"tesseract" => "brew install tesseract".to_string(),
"ffmpeg" => "brew install ffmpeg".to_string(),
_ => format!("brew install {tool}"),
}
}
#[cfg(target_os = "windows")]
{
match tool {
"poppler-utils" => "choco install poppler".to_string(),
"libreoffice" => "choco install libreoffice".to_string(),
"tesseract" => "choco install tesseract".to_string(),
"ffmpeg" => "choco install ffmpeg".to_string(),
_ => format!("choco install {tool}"),
}
}
}
/// Get platform-specific command to install all dependencies
fn get_quick_install_all() -> &'static str {
#[cfg(target_os = "linux")]
{
" ./install/install-deps-linux.sh"
}
#[cfg(target_os = "macos")]
{
" ./install/install-deps-macos.sh"
}
#[cfg(target_os = "windows")]
{
" .\\install\\install-deps-windows.ps1 (or .bat)"
}
}