-
Notifications
You must be signed in to change notification settings - Fork 10
/
build.rs
72 lines (66 loc) · 2.56 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
70
71
72
use std::{env, path::PathBuf, path::Path};
fn main() {
println!("cargo:rerun-if-changed=NULL"); // forces the build script to run even when no file changed and a previous build is existent.
link_libraries();
use_bindgen();
}
/// This function is responsible for linking the required libraries
///
/// The function determines the target operating system and adds the necessary
/// search paths and library names to the build configuration using the `cargo` command.
fn link_libraries() {
let crate_path = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let target = env::var("TARGET").unwrap();
if target.contains("-apple-") {
let library_path = crate_path.join("SDK/Libraries/Mac");
println!(
"cargo:rustc-link-search=framework={}",
library_path.to_str().unwrap()
);
println!("cargo:rustc-link-lib=framework=XPLM");
println!("cargo:rustc-link-lib=framework=XPWidgets");
} else if target.contains("-linux-") {
// Do nothing for Linux
} else if target.contains("-windows-") {
let library_path = crate_path.join("SDK/Libraries/Win");
println!("cargo:rustc-link-search={}", library_path.to_str().unwrap());
if target.contains("x86_64") {
println!("cargo:rustc-link-lib=XPLM_64");
println!("cargo:rustc-link-lib=XPWidgets_64");
} else {
println!("cargo:rustc-link-lib=XPLM");
println!("cargo:rustc-link-lib=XPWidgets");
}
} else {
panic!("Target operating system not Mac OS, Linux, or Windows")
}
}
/// Generates Rust bindings for a C header file using bindgen.
///
/// # Panics
///
/// This function will panic if it fails to generate the bindings.
fn use_bindgen() {
let path_combined = "src/combined.h";
let out_dir = env::var("OUT_DIR").unwrap();
let path_bindgen = Path::new(&out_dir).join("bindgen.rs");
let bindings = bindgen::Builder::default()
.header(path_combined)
.layout_tests(false)
.clang_arg("-ISDK/CHeaders/Widgets")
.clang_arg("-ISDK/CHeaders/XPLM")
.clang_arg("-DLIN=1")
.clang_arg("-DXPLM200")
.clang_arg("-DXPLM210")
.clang_arg("-DXPLM300")
.clang_arg("-DXPLM301")
.clang_arg("-DXPLM303")
.clang_arg("-DXPLM400")
.clang_arg("-DXPLM410")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings");
bindings
.write_to_file(path_bindgen)
.expect("Couldn't write bindings!");
}