Skip to content

Commit 0fb18e6

Browse files
Add Cargo.toml and build.rs for Bazel workspaces
This commit introduces Cargo.toml files for the detector, tracker, and examples workspaces to enable Rust tooling and standalone Cargo builds. - detector/Cargo.toml: Defines the 'rtmo' library, which includes C++ interoperability via the 'cxx' crate. - detector/build.rs: Configured to compile the C++ sources (engine.cpp, rtmo.cpp) and link against dependencies like CUDA, TensorRT, and CVCUDA, mirroring the Bazel setup. - tracker/Cargo.toml: Defines the 'tracker' library with its Rust dependencies and a path dependency on the 'rtmo' library. - examples/Cargo.toml: Defines the 'image_demo' and 'video_demo' binaries, with path dependencies on 'rtmo' and 'tracker', along with other necessary crates. These additions facilitate the use of standard Rust development tools and workflows alongside the existing Bazel build system.
1 parent 164bff1 commit 0fb18e6

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

detector/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "rtmo"
3+
version = "0.1.0" # Default version, can be adjusted
4+
edition = "2021" # From MODULE.bazel (rust.toolchain)
5+
authors = ["Your Name <you@example.com>"] # Placeholder
6+
7+
[lib]
8+
name = "rtmo"
9+
path = "src/lib.rs"
10+
11+
[dependencies]
12+
cxx = "1.0.149"
13+
14+
# For handling the C++ compilation and linking:
15+
[build-dependencies]
16+
cxx-build = "1.0.149" # Or a suitable build script helper if available via rules_rust
17+
18+
# Potentially, if rtmo.rs is a separate module for the bridge
19+
# [[bin]]
20+
# name = "rtmo_bridge_bin" # Example if it were a binary
21+
# path = "src/rtmo.rs"

detector/build.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use std::path::PathBuf;
2+
3+
fn main() {
4+
println!("cargo:rerun-if-changed=src/rtmo.rs");
5+
println!("cargo:rerun-if-changed=src/engine.cpp");
6+
println!("cargo:rerun-if-changed=src/engine.hpp");
7+
println!("cargo:rerun-if-changed=src/rtmo.cpp");
8+
println!("cargo:rerun-if-changed=src/rtmo.hpp");
9+
10+
let manifest_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
11+
let workspace_root = manifest_dir.parent().unwrap_or(&manifest_dir); // Assuming detector is one level down from workspace root
12+
13+
// --- CUDA ---
14+
// These are common paths. Adjust if your CUDA installation is different.
15+
let cuda_include_path = PathBuf::from("/usr/local/cuda/include");
16+
let cuda_lib_path = PathBuf::from("/usr/local/cuda/lib64");
17+
println!("cargo:rustc-link-search=native={}", cuda_lib_path.display());
18+
println!("cargo:rustc-link-lib=dylib=cudart"); // Link against CUDA runtime
19+
20+
// --- TensorRT ---
21+
// Path relative to workspace root.
22+
let tensorrt_base_path = workspace_root.join("repositories/tensorrt");
23+
let tensorrt_include_path = tensorrt_base_path.join("include/x86_64-linux-gnu");
24+
let tensorrt_lib_path = tensorrt_base_path.join("lib/x86_64-linux-gnu");
25+
26+
println!("cargo:rustc-link-search=native={}", tensorrt_lib_path.display());
27+
println!("cargo:rustc-link-lib=dylib=nvinfer");
28+
println!("cargo:rustc-link-lib=dylib=nvinfer_plugin");
29+
30+
// --- CVCUDA ---
31+
// Path relative to workspace root.
32+
let cvcuda_base_path = workspace_root.join("repositories/cvcuda");
33+
let cvcuda_include_path = cvcuda_base_path.join("include");
34+
let cvcuda_lib_path = cvcuda_base_path.join("lib");
35+
36+
println!("cargo:rustc-link-search=native={}", cvcuda_lib_path.display());
37+
println!("cargo:rustc-link-lib=dylib=cvcuda");
38+
println!("cargo:rustc-link-lib=dylib=nvcv_types");
39+
40+
41+
// --- Build C++ files using cxx_build ---
42+
let mut build = cxx_build::bridge("src/rtmo.rs"); // Path to the Rust file with CXX bridge
43+
build
44+
.file("src/engine.cpp")
45+
.file("src/rtmo.cpp")
46+
.include(&manifest_dir.join("src")) // for engine.hpp, rtmo.hpp
47+
.include(&cuda_include_path)
48+
.include(&tensorrt_include_path)
49+
.include(&cvcuda_include_path)
50+
.flag_if_supported("-std=c++17") // Assuming C++17, adjust if necessary
51+
.flag_if_supported("-pthread"); // For TensorRT plugins
52+
53+
// Add any other necessary compiler flags or definitions
54+
// e.g., .define("SOME_MACRO", "value")
55+
56+
// Linker arguments for specific libraries if needed (beyond simple -l flags)
57+
// For TensorRT plugin, Bazel had: linkopts = ["-lpthread", "-Wl,--no-as-needed -ldl -lrt -Wl,--as-needed"]
58+
// The -lpthread is covered by cargo:rustc-link-lib=pthread if cc crate doesn't add it.
59+
// The cc crate usually handles pthread implicitly if specified with .flag("-pthread") for compiler and linker.
60+
// Let's ensure pthread is linked.
61+
println!("cargo:rustc-link-lib=dylib=pthread");
62+
// For dlsym, rt_timer related symbols which might be needed by TensorRT
63+
println!("cargo:rustc-link-lib=dylib=dl");
64+
println!("cargo:rustc-link-lib=dylib=rt");
65+
66+
67+
build.compile("rtmo_cpp_bridge");
68+
69+
println!("cargo:warning=This build.rs attempts to compile C++ dependencies. Ensure CUDA, TensorRT, and CVCUDA are correctly installed and paths are configured if issues arise.");
70+
println!("cargo:warning=CUDA include path: {}", cuda_include_path.display());
71+
println!("cargo:warning=CUDA lib path: {}", cuda_lib_path.display());
72+
println!("cargo:warning=TensorRT include path: {}", tensorrt_include_path.display());
73+
println!("cargo:warning=TensorRT lib path: {}", tensorrt_lib_path.display());
74+
println!("cargo:warning=CVCUDA include path: {}", cvcuda_include_path.display());
75+
println!("cargo:warning=CVCUDA lib path: {}", cvcuda_lib_path.display());
76+
}

examples/Cargo.toml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[package]
2+
name = "examples"
3+
version = "0.1.0"
4+
edition = "2021" # From MODULE.bazel (rust.toolchain)
5+
authors = ["Your Name <you@example.com>"] # Placeholder
6+
publish = false # Examples are typically not published
7+
8+
[[bin]]
9+
name = "image_demo"
10+
path = "src/image_demo.rs"
11+
12+
[[bin]]
13+
name = "video_demo"
14+
path = "src/video_demo.rs"
15+
16+
[dependencies]
17+
rtmo = { path = "../detector" }
18+
tracker = { path = "../tracker" } # Needed by video_demo
19+
20+
cxx = "1.0.149"
21+
image = "0.25.0"
22+
ab_glyph = "0.2.29"
23+
imageproc = "0.25.0"
24+
minifb = "0.28.0"
25+
video-rs = { version = "0.10.3", features = ["ndarray"] }
26+
27+
# Note: Since image_demo does not use tracker, ab_glyph, imageproc, minifb, video-rs,
28+
# these will still be compiled if you do `cargo build --bin image_demo` because
29+
# they are listed as common dependencies. For example crates, this is often acceptable.
30+
# If strict separation is needed, they would need to be separate Cargo packages.

tracker/Cargo.toml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[package]
2+
name = "tracker"
3+
version = "0.1.0"
4+
edition = "2021" # From MODULE.bazel (rust.toolchain)
5+
authors = ["Your Name <you@example.com>"] # Placeholder
6+
7+
[lib]
8+
name = "tracker"
9+
path = "src/lib.rs"
10+
11+
[dependencies]
12+
rtmo = { path = "../detector" }
13+
nalgebra = "0.33.2"
14+
num = "0.4.3"
15+
thiserror = "2.0.12"
16+
17+
[dev-dependencies]
18+
# rtmo = { path = "../detector" } # Already in [dependencies]
19+
# nalgebra = "0.33.2" # Already in [dependencies]
20+
# num = "0.4.3" # Already in [dependencies]
21+
# thiserror = "2.0.12" # Already in [dependencies]
22+
nearly_eq = "0.2.4"
23+
quickcheck = "1.0.3"
24+
rand = "0.8.5"
25+
26+
# The tracker_test in BUILD.bazel uses srcs = glob(["src/*.rs"]),
27+
# which suggests tests might be inline or in files picked up by Cargo's
28+
# default test discovery (e.g. src/tests/some_test.rs or tests/integration_test.rs).
29+
# If tests are embedded within lib.rs or other src/*.rs files (like src/test_byte_tracker.rs),
30+
# Cargo will pick them up automatically. No specific [[test]] section is needed
31+
# unless there's a non-standard test file name or location.

0 commit comments

Comments
 (0)