Skip to content

Commit 7059c8f

Browse files
committed
Rework binding generation to reduce boilerplate
This is a pretty big (breaking!) change. - Update OrbbecSDK submodule, vendor in crate - Rework binding generation to change how properties work. - Add macros for common patterns of calling orbbec sdk functions. I've also collapsed the other PRs on this repo into this one, with separate commits for each. The first commit updates the OrbbecSDK submodule, and changes the build process to vendor the contents of the OrbbecSDK folder into the crate. Caveat: OrbbecSDK writes out a couple files to its source directory, so we need to copy the whole source directory to OUT_DIR before building. The next commit reworks the binding generation to use Rust-y enums instead of raw integers. In many cases, we're now using the rust enums directly in the public API. This isn't complete, since I would like to rename struct members too (see rust-lang/rust-bindgen#3358). The third commit adds macros for common patterns of calling the C SDK functions, and rewrites all of the current calls to the C SDK to use them. I think my vision is to further reduce the sys/ layer so that src/ does most of the work. The rest of the commits are adding various features to the public API. Topic: vendor-submodule
1 parent d28b0aa commit 7059c8f

4 files changed

Lines changed: 95 additions & 9 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ keywords = ["orbbec", "3d", "camera", "depth", "pointcloud"]
77
repository = "https://github.com/danielstuart14/orbbec-sdk-rs"
88
license = "MIT"
99
edition = "2024"
10-
version = "0.1.0"
10+
version = "0.2.0"
11+
# These files are generated by CMake and should not be included in the crate
12+
exclude = [
13+
"/OrbbecSDK/include/libobsensor/h/Export.h",
14+
"/OrbbecSDK/src/shared/environment/Version.hpp",
15+
]
1116

1217
[dependencies]
1318

1419
[build-dependencies]
1520
cmake = "0.1"
16-
bindgen = { version = "0.71.0", optional = true }
21+
bindgen = { version = "0.72", optional = true }
1722

1823
[dev-dependencies]
1924
colorous = "1.0.16"

OrbbecSDK

Submodule OrbbecSDK updated 941 files

build.rs

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,44 @@
1+
use std::{
2+
env, fs,
3+
path::{Path, PathBuf},
4+
};
5+
16
fn main() {
27
if cfg!(feature = "docs-only") {
38
return;
49
}
510

11+
// Tell Cargo to rerun if anything in the SDK changes
12+
println!("cargo:rerun-if-changed=OrbbecSDK");
13+
println!("cargo:rerun-if-changed=build.rs");
14+
15+
let out = PathBuf::from(env::var("OUT_DIR").unwrap());
16+
let src = PathBuf::from("OrbbecSDK");
17+
let work_src = out.join("orbbec_src");
18+
let stamp = out.join("orbbec_src.stamp");
19+
20+
let current_stamp = dir_fingerprint(&src).unwrap();
21+
22+
let needs_refresh = match fs::read_to_string(&stamp) {
23+
Ok(old) => old != current_stamp,
24+
Err(_) => true,
25+
};
26+
27+
if needs_refresh {
28+
if work_src.exists() {
29+
fs::remove_dir_all(&work_src).unwrap();
30+
}
31+
copy_dir_all(&src, &work_src).unwrap();
32+
fs::write(&stamp, &current_stamp).unwrap();
33+
}
34+
635
// Configure and build the C++ library using CMake
7-
let mut dst = cmake::Config::new("OrbbecSDK");
36+
let mut dst = cmake::Config::new(work_src);
837
dst.define("CMAKE_POLICY_VERSION_MINIMUM", "3.10");
9-
dst.define("OB_BUILD_DOCS", "OFF");
10-
dst.define("OB_BUILD_TOOLS", "OFF");
38+
dst.define("OB_BUILD_EXAMPLES", "OFF");
1139
dst.define("OB_BUILD_TESTS", "OFF");
40+
dst.define("OB_BUILD_TOOLS", "OFF");
41+
dst.define("OB_BUILD_DOCS", "OFF");
1242

1343
// cmake-rs has some issues setting the correct flags on Windows
1444
#[cfg(target_os = "windows")]
@@ -92,6 +122,8 @@ fn main() {
92122
{
93123
let cargo_manifest_dir = std::env::current_dir().unwrap();
94124

125+
let target = std::env::var("TARGET").unwrap();
126+
95127
// The bindgen::Builder is the main entry point
96128
// to bindgen, and lets you build up options for
97129
// the resulting bindings.
@@ -104,6 +136,8 @@ fn main() {
104136
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
105137
// Set the include path for the OrbbecSDK headers
106138
.clang_arg("-IOrbbecSDK/include/")
139+
// Set the target architecture (The committed bindings are for x86_64-unknown-linux-gnu!)
140+
.clang_arg(format!("--target={target}"))
107141
// Convert enum type
108142
.translate_enum_integer_types(true)
109143
// Finish the builder and generate the bindings.
@@ -122,3 +156,50 @@ fn main() {
122156
.expect("Couldn't write bindings!");
123157
}
124158
}
159+
160+
fn copy_dir_all(src: &Path, dst: &Path) -> std::io::Result<()> {
161+
fs::create_dir_all(dst)?;
162+
for entry in fs::read_dir(src)? {
163+
let entry = entry?;
164+
let ty = entry.file_type()?;
165+
let from = entry.path();
166+
let to = dst.join(entry.file_name());
167+
if ty.is_dir() {
168+
copy_dir_all(&from, &to)?;
169+
} else {
170+
fs::copy(&from, &to)?;
171+
}
172+
}
173+
Ok(())
174+
}
175+
176+
fn dir_fingerprint(root: &Path) -> std::io::Result<String> {
177+
let mut entries = Vec::new();
178+
collect_entries(root, root, &mut entries)?;
179+
Ok(entries.join("\n"))
180+
}
181+
182+
fn collect_entries(root: &Path, dir: &Path, out: &mut Vec<String>) -> std::io::Result<()> {
183+
for entry in fs::read_dir(dir)? {
184+
let entry = entry?;
185+
let path = entry.path();
186+
let ty = entry.file_type()?;
187+
188+
if ty.is_dir() {
189+
collect_entries(root, &path, out)?;
190+
} else {
191+
let rel = path.strip_prefix(root).unwrap();
192+
let meta = entry.metadata()?;
193+
let len = meta.len();
194+
let modified = meta
195+
.modified()?
196+
.duration_since(std::time::UNIX_EPOCH)
197+
.unwrap()
198+
.as_secs();
199+
200+
out.push(format!("{}:{}:{}", rel.display(), len, modified));
201+
}
202+
}
203+
out.sort();
204+
Ok(())
205+
}

0 commit comments

Comments
 (0)