Skip to content

Commit 83f93aa

Browse files
committed
fix(rust,build,core): Align Rust version to 1.5.0, improve build safety and test robustness
- Upgrade Rust crate version from 1.1.0 to 1.5.0 to match main project - Enhance build_and_test.sh with CLI-assisted device discovery validation: - Auto-build ccap CLI if not found (with CCAP_BUILD_CLI=ON) - Compare Rust print_camera and CLI --list-devices device counts - Report mismatches or inconsistencies; skip tests gracefully if no cameras - Fix: Use print_camera instead of non-existent list_cameras example - Fix: Directory restoration after CLI construction - Update: Usage examples list to match actual examples - Strengthen build_and_test.sh library rebuild: - Always rebuild when libccap.a missing, even if build dir exists - Use cmake --build instead of make for better portability - Check CMakeCache.txt to skip redundant reconfiguration - Extend scripts/update_version.sh to sync Rust crate version: - Update Cargo.toml version when running version script - Update Cargo.lock ccap entry via Python regex - Ensures future version bumps include Rust bindings - Add <limits> header to src/ccap_convert_frame.cpp: - Prevent uint32_t sizeInBytes overflow (return false if allocator->size() > UINT32_MAX) - Safety check for inplaceConvertFrame post-conversion
1 parent 18a6be3 commit 83f93aa

4 files changed

Lines changed: 166 additions & 17 deletions

File tree

bindings/rust/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindings/rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ccap"
3-
version = "1.1.0"
3+
version = "1.5.0"
44
edition = "2021"
55
rust-version = "1.65"
66
authors = ["wysaid <this@wysaid.org>"]

bindings/rust/build_and_test.sh

Lines changed: 140 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,71 @@
44

55
set -e
66

7+
detect_cli_devices() {
8+
local cli_bin=""
9+
local original_dir
10+
original_dir="$(pwd)"
11+
12+
# Prefer Debug build
13+
if [ -x "$PROJECT_ROOT/build/Debug/ccap" ]; then
14+
cli_bin="$PROJECT_ROOT/build/Debug/ccap"
15+
elif [ -x "$PROJECT_ROOT/build/Debug/ccap.exe" ]; then
16+
cli_bin="$PROJECT_ROOT/build/Debug/ccap.exe"
17+
elif [ -x "$PROJECT_ROOT/build/Release/ccap" ]; then
18+
cli_bin="$PROJECT_ROOT/build/Release/ccap"
19+
elif [ -x "$PROJECT_ROOT/build/Release/ccap.exe" ]; then
20+
cli_bin="$PROJECT_ROOT/build/Release/ccap.exe"
21+
else
22+
echo "ccap CLI not found. Building (Debug) with CCAP_BUILD_CLI=ON..."
23+
pushd "$PROJECT_ROOT" >/dev/null
24+
25+
mkdir -p build/Debug
26+
pushd build/Debug >/dev/null
27+
28+
# Reconfigure with CLI enabled (idempotent)
29+
cmake ../.. -DCMAKE_BUILD_TYPE=Debug -DCCAP_BUILD_CLI=ON
30+
cmake --build . --config Debug --target ccap-cli -- -j"$(nproc 2>/dev/null || echo 4)"
31+
32+
popd >/dev/null
33+
popd >/dev/null
34+
35+
cli_bin="$PROJECT_ROOT/build/Debug/ccap"
36+
fi
37+
38+
cd "$original_dir"
39+
echo "$cli_bin"
40+
}
41+
42+
parse_cli_device_count() {
43+
local output="$1"
44+
local count=0
45+
46+
if [[ "$output" =~ Found[[:space:]]+([0-9]+)[[:space:]]+camera ]]; then
47+
count=${BASH_REMATCH[1]}
48+
elif echo "$output" | grep -qi "No camera devices found"; then
49+
count=0
50+
else
51+
count=-1
52+
fi
53+
54+
echo "$count"
55+
}
56+
57+
parse_rust_device_count() {
58+
local output="$1"
59+
local count=0
60+
61+
if [[ "$output" =~ \#\#[[:space:]]+Found[[:space:]]+([0-9]+)[[:space:]]+video[[:space:]]+capture[[:space:]]+device ]]; then
62+
count=${BASH_REMATCH[1]}
63+
elif echo "$output" | grep -qi "Failed to find any video capture device"; then
64+
count=0
65+
else
66+
count=-1
67+
fi
68+
69+
echo "$count"
70+
}
71+
772
echo "ccap Rust Bindings - Build and Test Script"
873
echo "==========================================="
974

@@ -22,15 +87,15 @@ if [ ! -f "$PROJECT_ROOT/build/Debug/libccap.a" ] && [ ! -f "$PROJECT_ROOT/build
2287
echo "ccap C library not found. Building..."
2388
cd "$PROJECT_ROOT"
2489

25-
if [ ! -d "build" ]; then
26-
mkdir -p build/Debug
27-
cd build/Debug
90+
mkdir -p build/Debug
91+
cd build/Debug
92+
93+
if [ ! -f "CMakeCache.txt" ]; then
2894
cmake ../.. -DCMAKE_BUILD_TYPE=Debug
29-
make -j"$(nproc 2>/dev/null || echo 4)"
30-
else
31-
echo "Build directory exists, assuming library is built"
3295
fi
3396

97+
cmake --build . --config Debug -- -j"$(nproc 2>/dev/null || echo 4)"
98+
3499
cd "$RUST_DIR"
35100
else
36101
echo "ccap C library found"
@@ -63,23 +128,83 @@ echo "Step 4: Building examples..."
63128
cargo build --examples
64129
cargo build --features async --examples
65130

66-
# Try to run basic example
67131
echo ""
68-
echo "Step 5: Testing basic functionality..."
69-
echo "Running camera discovery test..."
70-
if cargo run --example list_cameras; then
71-
echo "✅ Camera discovery test passed"
132+
echo "Step 5: Testing basic functionality (camera discovery vs CLI)..."
133+
134+
# Run Rust discovery (print_camera) and capture output without aborting
135+
set +e
136+
RUST_DISCOVERY_OUTPUT=$(cargo run --example print_camera 2>&1)
137+
RUST_DISCOVERY_STATUS=$?
138+
set -e
139+
140+
RUST_DEVICE_COUNT=$(parse_rust_device_count "$RUST_DISCOVERY_OUTPUT")
141+
142+
CLI_BIN=$(detect_cli_devices)
143+
if [ ! -x "$CLI_BIN" ]; then
144+
echo "❌ Failed to build or locate ccap CLI for reference checks." >&2
145+
exit 1
146+
fi
147+
148+
set +e
149+
CLI_DISCOVERY_OUTPUT=$("$CLI_BIN" --list-devices 2>&1)
150+
CLI_DISCOVERY_STATUS=$?
151+
set -e
152+
153+
CLI_DEVICE_COUNT=$(parse_cli_device_count "$CLI_DISCOVERY_OUTPUT")
154+
155+
echo "Rust discovery exit: $RUST_DISCOVERY_STATUS, devices: $RUST_DEVICE_COUNT"
156+
echo "CLI discovery exit: $CLI_DISCOVERY_STATUS, devices: $CLI_DEVICE_COUNT"
157+
158+
# Decision logic
159+
if [ $CLI_DISCOVERY_STATUS -ne 0 ]; then
160+
echo "❌ CLI discovery failed. Output:" >&2
161+
echo "$CLI_DISCOVERY_OUTPUT" >&2
162+
exit 1
163+
fi
164+
165+
if [ $CLI_DEVICE_COUNT -lt 0 ]; then
166+
echo "❌ Unable to parse CLI device count. Output:" >&2
167+
echo "$CLI_DISCOVERY_OUTPUT" >&2
168+
exit 1
169+
fi
170+
171+
if [ $RUST_DISCOVERY_STATUS -ne 0 ] && [ $CLI_DEVICE_COUNT -gt 0 ]; then
172+
echo "❌ Rust discovery failed while CLI sees devices. Output:" >&2
173+
echo "$RUST_DISCOVERY_OUTPUT" >&2
174+
exit 1
175+
fi
176+
177+
if [ $RUST_DEVICE_COUNT -lt 0 ]; then
178+
echo "⚠️ Rust discovery output could not be parsed. Output:" >&2
179+
echo "$RUST_DISCOVERY_OUTPUT" >&2
180+
if [ $CLI_DEVICE_COUNT -gt 0 ]; then
181+
echo "❌ CLI sees devices but Rust discovery is inconclusive." >&2
182+
exit 1
183+
fi
184+
fi
185+
186+
if [ $CLI_DEVICE_COUNT -eq 0 ] && [ $RUST_DEVICE_COUNT -eq 0 ]; then
187+
echo "ℹ️ No cameras detected by CLI or Rust. Skipping capture tests (expected in headless environments)."
188+
elif [ $CLI_DEVICE_COUNT -ne $RUST_DEVICE_COUNT ]; then
189+
echo "❌ Device count mismatch (Rust: $RUST_DEVICE_COUNT, CLI: $CLI_DEVICE_COUNT)." >&2
190+
echo "Rust output:" >&2
191+
echo "$RUST_DISCOVERY_OUTPUT" >&2
192+
echo "CLI output:" >&2
193+
echo "$CLI_DISCOVERY_OUTPUT" >&2
194+
exit 1
72195
else
73-
echo "⚠️ Camera discovery test failed (this may be normal if no cameras are available)"
196+
echo "Camera discovery consistent (devices: $CLI_DEVICE_COUNT)."
74197
fi
75198

76199
echo ""
77200
echo "✅ All Rust binding builds completed successfully!"
78201
echo ""
79202
echo "Usage examples:"
80-
echo " cargo run --example list_cameras"
81-
echo " cargo run --example capture_frames"
82-
echo " cargo run --features async --example async_capture"
203+
echo " cargo run --example print_camera"
204+
echo " cargo run --example minimal_example"
205+
echo " cargo run --example capture_grab"
206+
echo " cargo run --example capture_callback"
207+
echo " cargo run --features async --example capture_callback"
83208
echo ""
84209
echo "To use in your project, add to Cargo.toml:"
85210
echo ' ccap = { path = "'$RUST_DIR'" }'

scripts/update_version.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,28 @@ update_file "s/version = \".*\"/version = \"$NEW_VERSION\"/" "$PROJECT_ROOT/cona
9191
# 4. Update BUILD_AND_INSTALL.md (documentation)
9292
update_file "s/Current version: .*/Current version: $NEW_VERSION/" "$PROJECT_ROOT/BUILD_AND_INSTALL.md" "BUILD_AND_INSTALL.md"
9393

94+
# 5. Update Rust crate version (Cargo.toml)
95+
update_file "s/^version = \".*\"$/version = \"$NEW_VERSION\"/" "$PROJECT_ROOT/bindings/rust/Cargo.toml" "Rust crate version (Cargo.toml)"
96+
97+
# 6. Update Rust lockfile entry for ccap (Cargo.lock)
98+
RUST_LOCKFILE="$PROJECT_ROOT/bindings/rust/Cargo.lock"
99+
if [ -f "$RUST_LOCKFILE" ]; then
100+
python3 - "$RUST_LOCKFILE" "$NEW_VERSION" <<'PY'
101+
import pathlib, re, sys
102+
103+
path = pathlib.Path(sys.argv[1])
104+
new_ver = sys.argv[2]
105+
text = path.read_text()
106+
pattern = r'(?m)(^name = "ccap"\nversion = ")[^"]+("\n)'
107+
new_text, count = re.subn(pattern, rf"\1{new_ver}\2", text, count=1)
108+
if count:
109+
path.write_text(new_text)
110+
print(f"✅ Updated Rust lockfile ccap version to {new_ver}")
111+
else:
112+
print(f"⚠️ ccap entry not found in {path}, lockfile not modified")
113+
PY
114+
else
115+
echo "⚠️ $RUST_LOCKFILE not found, skipping Rust lockfile update"
116+
fi
117+
94118
echo "Version update complete!"

0 commit comments

Comments
 (0)