-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.rs
69 lines (60 loc) · 2.52 KB
/
build.rs
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
extern crate bindgen;
use std::env;
use std::path::PathBuf;
fn main() {
// Base path to the NDI SDK from the environment variable or default based on the platform
let ndi_sdk_path = env::var("NDI_SDK_DIR").unwrap_or_else(|_| {
if cfg!(unix) {
"/usr/share/NDI SDK for Linux".to_string()
} else if cfg!(windows) {
"C:\\Program Files\\NDI SDK for Windows".to_string()
} else {
panic!("Unsupported platform, please set NDI_SDK_DIR manually.");
}
});
// Paths to the include and main header file
let ndi_include_path = format!("{}/include", ndi_sdk_path);
let main_header = format!("{}/Processing.NDI.Lib.h", ndi_include_path);
// Determine the library name and linking type based on the platform
let (lib_name, link_type) = if cfg!(unix) {
// For Unix-like systems, use the shared library `libndi.so`
("ndi", "dylib") // Use "dylib" for dynamic linking
} else if cfg!(windows) {
// For Windows systems, use the specific x86/x64 libraries with static linking
let target = env::var("TARGET").expect("TARGET environment variable not set");
if target.contains("x86_64") {
("Processing.NDI.Lib.x64", "static")
} else {
("Processing.NDI.Lib.x86", "static")
}
} else {
panic!("Unsupported platform");
};
// On Windows, add the specific library directory path
if cfg!(windows) {
let target = env::var("TARGET").expect("TARGET environment variable not set");
let lib_subdir = if target.contains("x86_64") {
"x64"
} else {
"x86"
};
let lib_path = format!("{}\\lib\\{}", ndi_sdk_path, lib_subdir);
// Inform cargo about the search path for the linker and the library to link against
println!("cargo:rustc-link-search=native={}", lib_path);
}
// Inform cargo about the library to link against
println!("cargo:rustc-link-lib={}={}", link_type, lib_name);
// Generate the bindings
let bindings = bindgen::Builder::default()
.header(main_header)
.clang_arg(format!("-I{}", ndi_include_path))
.derive_default(true)
.generate()
.expect("Unable to generate bindings");
// Write the bindings to the $OUT_DIR/ndi_lib.rs file
let out_path =
PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR environment variable not set"));
bindings
.write_to_file(out_path.join("ndi_lib.rs"))
.expect("Couldn't write bindings!");
}