-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.rs
More file actions
124 lines (107 loc) · 4.52 KB
/
build.rs
File metadata and controls
124 lines (107 loc) · 4.52 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
fn main() {
if cfg!(feature = "docs-only") {
return;
}
// Configure and build the C++ library using CMake
let mut dst = cmake::Config::new("OrbbecSDK");
dst.define("CMAKE_POLICY_VERSION_MINIMUM", "3.10");
dst.define("OB_BUILD_DOCS", "OFF");
dst.define("OB_BUILD_TOOLS", "OFF");
dst.define("OB_BUILD_TESTS", "OFF");
// cmake-rs has some issues setting the correct flags on Windows
#[cfg(target_os = "windows")]
dst.static_crt(true)
.cflag("/DWIN32 /D_WINDOWS -w")
.cxxflag("/DWIN32 /D_WINDOWS -w /EHsc");
// Disable warnings
#[cfg(not(target_os = "windows"))]
dst.cflag("-w").cxxflag("-w");
let dst = dst.build();
// Binary directory differs between Windows and Unix
#[cfg(target_os = "windows")]
let bin_folder = "bin";
#[cfg(not(target_os = "windows"))]
let bin_folder = "lib";
let bin_dir = dst.join(bin_folder);
let target_dir = dst.join("../../../");
// Copy extension directory to target_dir
let extensions_dir = bin_dir.join("extensions");
let dest_extensions_dir = target_dir.join("extensions");
if !dest_extensions_dir.exists() {
std::fs::create_dir_all(&dest_extensions_dir)
.expect("Failed to create extensions directory");
}
// Go through each folder in extensions_dir and copy to dest_extensions_dir
for entry in std::fs::read_dir(&extensions_dir).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.is_dir() {
let folder_name = path.file_name().unwrap();
let dest_folder = dest_extensions_dir.join(folder_name);
if !dest_folder.exists() {
std::fs::create_dir_all(&dest_folder)
.expect("Failed to create extension subdirectory");
}
for file_entry in std::fs::read_dir(&path).unwrap() {
let file_entry = file_entry.unwrap();
let file_path = file_entry.path();
if let Some(ext) = file_path.extension()
&& (ext == "dll" || ext == "so" || ext == "dylib")
{
let file_name = file_path.file_name().unwrap();
let dest_file = dest_folder.join(file_name);
std::fs::copy(&file_path, &dest_file)
.expect("Failed to copy extension library file");
}
}
}
}
// Copy libraries to target directory
for entry in std::fs::read_dir(&bin_dir).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if let Some(ext) = path.extension()
&& (ext == "dll" || ext == "so" || ext == "dylib")
{
let file_name = path.file_name().unwrap();
let dest = target_dir.join(file_name);
std::fs::copy(&path, &dest).expect("Failed to copy library file");
}
}
// Tell cargo to tell rustc to link the OrbbecSDK library
println!(
"cargo:rustc-link-search=native={}",
dst.join("lib").display()
);
println!("cargo:rustc-link-lib=OrbbecSDK");
// Generate bindings using bindgen
#[cfg(feature = "buildtime-bindgen")]
{
let cargo_manifest_dir = std::env::current_dir().unwrap();
// The bindgen::Builder is the main entry point
// to bindgen, and lets you build up options for
// the resulting bindings.
let bindings = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
.header("OrbbecSDK/include/libobsensor/ObSensor.h")
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
// Set the include path for the OrbbecSDK headers
.clang_arg("-IOrbbecSDK/include/")
// Convert enum type
.translate_enum_integer_types(true)
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");
// Write the bindings to file
let bindings_dir = cargo_manifest_dir.join("bindings");
let bindings_file = bindings_dir.join("bindings.rs");
std::fs::create_dir_all(&bindings_dir).expect("Failed to create bindings directory");
bindings
.write_to_file(bindings_file)
.expect("Couldn't write bindings!");
}
}