-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
458 lines (389 loc) · 15.8 KB
/
build.rs
File metadata and controls
458 lines (389 loc) · 15.8 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
extern crate bindgen;
use std::env;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
fn get_lightgbm_version() -> String {
env::var("LIGHTGBM_VERSION").unwrap_or_else(|_| "4.6.0".to_string())
}
fn get_platform_info() -> (String, String) {
let target = env::var("TARGET").unwrap();
// Determine OS
let os = if target.contains("apple-darwin") {
"darwin"
} else if target.contains("linux") {
"linux"
} else if target.contains("windows") {
"windows"
} else {
panic!("Unsupported target: {}", target);
};
// Determine architecture
let arch = if target.contains("x86_64") {
"x86_64"
} else if target.contains("aarch64") || target.contains("arm64") {
"aarch64"
} else if target.contains("i686") || target.contains("i586") {
"i686"
} else {
panic!("Unsupported architecture for target: {}", target);
};
(os.to_string(), arch.to_string())
}
fn download_lightgbm_headers(out_dir: &Path) -> Result<(), Box<dyn std::error::Error>> {
let version = get_lightgbm_version();
// Create the include/LightGBM directory
let include_dir = out_dir.join("include/LightGBM");
fs::create_dir_all(&include_dir)?;
// Download the c_api.h file
let c_api_url = format!(
"https://raw.githubusercontent.com/microsoft/LightGBM/v{}/include/LightGBM/c_api.h",
version
);
println!("cargo:warning=Downloading c_api.h from: {}", c_api_url);
let response = ureq::get(&c_api_url).call()?;
let status = response.status();
if !(200..300).contains(&status) {
return Err(format!("Failed to download c_api.h: HTTP {}", status).into());
}
let c_api_path = include_dir.join("c_api.h");
let mut file = fs::File::create(&c_api_path)?;
io::copy(&mut response.into_reader(), &mut file)?;
// Also download export.h which is referenced by c_api.h
let export_url = format!(
"https://raw.githubusercontent.com/microsoft/LightGBM/v{}/include/LightGBM/export.h",
version
);
println!("cargo:warning=Downloading export.h from: {}", export_url);
let response = ureq::get(&export_url).call()?;
let status = response.status();
if !(200..300).contains(&status) {
return Err(format!("Failed to download export.h: HTTP {}", status).into());
}
let export_path = include_dir.join("export.h");
let mut file = fs::File::create(&export_path)?;
io::copy(&mut response.into_reader(), &mut file)?;
// Try to download arrow.h which is referenced by c_api.h (added in v4.2.0)
// For older versions, this file doesn't exist, so we skip it
let arrow_url = format!(
"https://raw.githubusercontent.com/microsoft/LightGBM/v{}/include/LightGBM/arrow.h",
version
);
println!(
"cargo:warning=Attempting to download arrow.h from: {}",
arrow_url
);
match ureq::get(&arrow_url).call() {
Ok(response) if response.status() >= 200 && response.status() < 300 => {
let arrow_path = include_dir.join("arrow.h");
let mut file = fs::File::create(&arrow_path)?;
io::copy(&mut response.into_reader(), &mut file)?;
println!("cargo:warning=Successfully downloaded arrow.h");
// Also try to download arrow.tpp which is referenced by arrow.h
let arrow_tpp_url = format!(
"https://raw.githubusercontent.com/microsoft/LightGBM/v{}/include/LightGBM/arrow.tpp",
version
);
println!(
"cargo:warning=Attempting to download arrow.tpp from: {}",
arrow_tpp_url
);
match ureq::get(&arrow_tpp_url).call() {
Ok(resp) if resp.status() >= 200 && resp.status() < 300 => {
let arrow_tpp_path = include_dir.join("arrow.tpp");
let mut file = fs::File::create(&arrow_tpp_path)?;
io::copy(&mut resp.into_reader(), &mut file)?;
println!("cargo:warning=Successfully downloaded arrow.tpp");
}
_ => {
println!("cargo:warning=arrow.tpp not available for this version (optional)");
}
}
}
_ => {
println!(
"cargo:warning=arrow.h not available for this version (optional, only in v4.2.0+)"
);
}
}
Ok(())
}
fn download_compiled_library(out_dir: &Path) -> Result<(), Box<dyn std::error::Error>> {
let (os, arch) = get_platform_info();
let version = get_lightgbm_version();
// Create the library directory
let lib_dir = out_dir.join("libs");
fs::create_dir_all(&lib_dir)?;
// For macOS, extract from Python wheel to get architecture-specific binaries
match (os.as_str(), arch.as_str()) {
// macOS - both x86_64 and ARM64 available
("darwin", "aarch64") | ("darwin", "x86_64") => {
let wheel_arch = if arch == "aarch64" { "arm64" } else { "x86_64" };
let macos_version = if arch == "aarch64" { "12_0" } else { "10_15" };
let wheel_url = format!(
"https://github.com/microsoft/LightGBM/releases/download/v{}/lightgbm-{}-py3-none-macosx_{}_{}.whl",
version, version, macos_version, wheel_arch
);
println!(
"cargo:warning=Downloading LightGBM v{} macOS {} wheel from: {}",
version, wheel_arch, wheel_url
);
download_and_extract_from_wheel(&wheel_url, out_dir, &lib_dir, "lib_lightgbm.dylib")?;
}
// Linux - both x86_64 and ARM64 available
("linux", "aarch64") | ("linux", "x86_64") => {
let (wheel_platform, lib_pattern) = if arch == "aarch64" {
("manylinux2014_aarch64", "lib_lightgbm.so")
} else {
("manylinux_2_28_x86_64", "lib_lightgbm.so")
};
let wheel_url = format!(
"https://github.com/microsoft/LightGBM/releases/download/v{}/lightgbm-{}-py3-none-{}.whl",
version, version, wheel_platform
);
println!(
"cargo:warning=Downloading LightGBM v{} Linux {} wheel from: {}",
version, arch, wheel_url
);
download_and_extract_from_wheel(&wheel_url, out_dir, &lib_dir, lib_pattern)?;
}
// Windows - only x86_64 available
("windows", "x86_64") => {
// For Windows, extract from wheel - need both DLL and import library
let wheel_url = format!(
"https://github.com/microsoft/LightGBM/releases/download/v{}/lightgbm-{}-py3-none-win_amd64.whl",
version, version
);
println!(
"cargo:warning=Downloading LightGBM v{} Windows x86_64 wheel from: {}",
version, wheel_url
);
download_and_extract_windows_libs(&wheel_url, out_dir, &lib_dir)?;
}
("windows", "i686") => {
return Err("Windows 32-bit (i686) is not supported by LightGBM releases. Please use x86_64 Windows or compile LightGBM from source.".into());
}
("windows", "aarch64") => {
return Err("Windows ARM64 is not currently supported by LightGBM releases. Please use x86_64 Windows or compile LightGBM from source.".into());
}
_ => {
return Err(format!(
"Unsupported platform/architecture combination: {} / {}",
os, arch
)
.into());
}
}
Ok(())
}
fn download_and_extract_from_wheel(
wheel_url: &str,
out_dir: &Path,
lib_dir: &Path,
lib_filename: &str,
) -> Result<(), Box<dyn std::error::Error>> {
// Download the wheel to a temp file
let wheel_path = out_dir.join("lightgbm.whl");
let mut dest = fs::File::create(&wheel_path)?;
let response = ureq::get(wheel_url).call()?;
let status = response.status();
if !(200..300).contains(&status) {
return Err(format!("Failed to download wheel: HTTP {}", status).into());
}
io::copy(&mut response.into_reader(), &mut dest)?;
drop(dest); // Close file before reading
// Extract the library from the wheel
// Wheels are just zip files
let wheel_file = fs::File::open(&wheel_path)?;
let mut archive = zip::ZipArchive::new(wheel_file)?;
// Find and extract the library
for i in 0..archive.len() {
let mut file = archive.by_index(i)?;
if file.name().ends_with(lib_filename) {
let lib_path = lib_dir.join(lib_filename);
let mut outfile = fs::File::create(&lib_path)?;
io::copy(&mut file, &mut outfile)?;
println!(
"cargo:warning=Extracted LightGBM library to: {}",
lib_path.display()
);
return Ok(());
}
}
Err(format!("{} not found in wheel", lib_filename).into())
}
fn download_and_extract_windows_libs(
wheel_url: &str,
out_dir: &Path,
lib_dir: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
// Download the wheel to a temp file
let wheel_path = out_dir.join("lightgbm.whl");
let mut dest = fs::File::create(&wheel_path)?;
let response = ureq::get(wheel_url).call()?;
let status = response.status();
if !(200..300).contains(&status) {
return Err(format!("Failed to download wheel: HTTP {}", status).into());
}
io::copy(&mut response.into_reader(), &mut dest)?;
drop(dest); // Close file before reading
// Extract both the DLL and the import library from the wheel
// Wheels are just zip files
let wheel_file = fs::File::open(&wheel_path)?;
let mut archive = zip::ZipArchive::new(wheel_file)?;
let mut dll_found = false;
let mut lib_found = false;
// Find and extract both lib_lightgbm.dll and lib_lightgbm.lib
for i in 0..archive.len() {
let mut file = archive.by_index(i)?;
let filename = file.name();
if filename.ends_with("lib_lightgbm.dll") {
let lib_path = lib_dir.join("lib_lightgbm.dll");
let mut outfile = fs::File::create(&lib_path)?;
io::copy(&mut file, &mut outfile)?;
println!(
"cargo:warning=Extracted LightGBM DLL to: {}",
lib_path.display()
);
dll_found = true;
} else if filename.ends_with("lib_lightgbm.lib") {
let lib_path = lib_dir.join("lib_lightgbm.lib");
let mut outfile = fs::File::create(&lib_path)?;
io::copy(&mut file, &mut outfile)?;
println!(
"cargo:warning=Extracted LightGBM import library to: {}",
lib_path.display()
);
lib_found = true;
}
if dll_found && lib_found {
return Ok(());
}
}
if !dll_found {
return Err("lib_lightgbm.dll not found in wheel".into());
}
if !lib_found {
return Err("lib_lightgbm.lib not found in wheel".into());
}
Ok(())
}
fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let lgbm_include_root = out_dir.join("include");
// Download the headers
if let Err(e) = download_lightgbm_headers(&out_dir) {
eprintln!("Failed to download LightGBM headers: {}", e);
panic!("Cannot proceed without headers");
}
// Download the compiled library
if let Err(e) = download_compiled_library(&out_dir) {
eprintln!("Failed to download compiled library: {}", e);
panic!("Cannot proceed without compiled library");
}
let bindings = bindgen::Builder::default()
.header("wrapper.h")
.clang_arg(format!("-I{}", lgbm_include_root.display()))
.clang_arg("-xc++")
.clang_arg("-std=c++17")
// Only generate bindings for functions starting with LGBM_
.allowlist_function("LGBM_.*")
// Allowlist the main types we need
.allowlist_type("BoosterHandle")
.allowlist_type("DatasetHandle")
.allowlist_type("FastConfigHandle")
.allowlist_type("ArrowArray")
.allowlist_type("ArrowSchema")
// Allowlist constants
.allowlist_var("C_API_DTYPE_.*")
// Treat Arrow types as opaque
.opaque_type("ArrowArray")
.opaque_type("ArrowSchema")
// Block problematic C++ code from arrow.h
.blocklist_type("std::.*")
.blocklist_type("ArrowTable")
.blocklist_type("ArrowChunkedArray")
.blocklist_type(".*_Tp.*")
.blocklist_type(".*_Pred.*")
.size_t_is_usize(true)
.generate()
.expect("Unable to generate bindings.");
bindings
.write_to_file(out_dir.join("bindings.rs"))
.expect("Couldn't write bindings.");
// Get platform info using your existing function
let (os, _arch) = get_platform_info();
// Determine the library filename based on the OS
let lib_filename = match os.as_str() {
"windows" => "lib_lightgbm.dll",
"darwin" => "lib_lightgbm.dylib",
_ => "lib_lightgbm.so", // Default to Linux/Unix
};
// Copy the library from OUT_DIR/libs to the final target directory
let lib_source_path = out_dir.join("libs").join(lib_filename);
// Find the final output directory (e.g., target/release)
let target_dir = out_dir
.ancestors()
.find(|p| p.ends_with("target"))
.unwrap()
.join(env::var("PROFILE").unwrap());
let lib_dest_path = target_dir.join(lib_filename);
fs::copy(&lib_source_path, &lib_dest_path).expect("Failed to copy library to target directory");
// On Windows, also copy the import library (.lib) to the libs directory for linking
if os == "windows" {
let import_lib_source = out_dir.join("libs").join("lib_lightgbm.lib");
if import_lib_source.exists() {
// No need to copy the .lib to target dir, it's only used during linking
println!(
"cargo:warning=Found import library at: {}",
import_lib_source.display()
);
}
}
// Set the library search path for the build-time linker
let lib_search_path = out_dir.join("libs");
println!(
"cargo:rustc-link-search=native={}",
lib_search_path.display()
);
// Set the rpath for the run-time linker based on the OS
match os.as_str() {
"darwin" => {
// For macOS, add multiple rpath entries for IDE compatibility
println!("cargo:rustc-link-arg=-Wl,-rpath,@executable_path");
println!("cargo:rustc-link-arg=-Wl,-rpath,@executable_path/../..");
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{}",
lib_search_path.display()
);
// Add the target directory to rpath as well
if let Some(target_root) = out_dir.ancestors().find(|p| p.ends_with("target")) {
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{}/debug",
target_root.display()
);
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{}/release",
target_root.display()
);
}
println!("cargo:rustc-link-lib=dylib=_lightgbm");
}
"linux" => {
// For Linux, use $ORIGIN
println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN");
println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN/../..");
println!(
"cargo:rustc-link-arg=-Wl,-rpath,{}",
lib_search_path.display()
);
println!("cargo:rustc-link-lib=dylib=_lightgbm");
}
"windows" => {
// On Windows, we need to tell the linker where to find the DLL at runtime
// Copy the DLL to the output directory (already done above)
println!("cargo:rustc-link-lib=dylib=lib_lightgbm");
}
_ => {}
}
}