Skip to content

Commit f20fae6

Browse files
author
scottrfrancis
committed
refactor: Remove RKNN binary patching workarounds - OS 9.1.79.3 includes system library
OS 9.1.79.3 includes librknnrt.so at /usr/lib/, eliminating need for workarounds. Changes: - package script: Removed patch_rknn_binaries() function (~290 lines) - package script: Removed create_rknn_debug_script() function (~170 lines) - package script: Simple wheel extraction only, no binary modifications - init-extension: Removed symlink creation to /tmp/lib/ - init-extension: Added OS version check with helpful error message Impact: - ~460 lines of workaround code removed - Much simpler build and deployment process - Cleaner codebase, easier maintenance - Requires OS 9.1.79.3+ (documented in success message) Tested on: BrightSign player with OS 9.1.79.3 Test result: RKNN initialization succeeds without any workarounds
1 parent 1385607 commit f20fae6

File tree

2 files changed

+8
-320
lines changed

2 files changed

+8
-320
lines changed

package

Lines changed: 2 additions & 297 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,6 @@ copy_rknn_wheel() {
223223
if [[ -d "$temp_dir/rknnlite" ]]; then
224224
cp -r "$temp_dir/rknnlite" "$site_packages/"
225225
success "rknnlite package installed"
226-
227-
# *** NEW: PATCH BINARY RPATH ***
228-
patch_rknn_binaries "$site_packages"
229-
230226
else
231227
error "rknnlite package directory not found in wheel"
232228
rm -rf "$temp_dir"
@@ -243,306 +239,15 @@ copy_rknn_wheel() {
243239

244240
# Cleanup temporary directory
245241
rm -rf "$temp_dir"
246-
247-
success "rknn-toolkit-lite2 successfully installed and patched to site-packages"
242+
243+
success "rknn-toolkit-lite2 successfully installed to site-packages (requires OS 9.1.79.3+)"
248244
else
249245
warn "rknn-toolkit-lite2 wheel not found at: $wheel_path"
250246
warn "Run ./setup first to clone rknn-toolkit2"
251247
return 1
252248
fi
253249
}
254250

255-
# NEW FUNCTION: Patch RKNN binary files to search multiple library paths
256-
patch_rknn_binaries() {
257-
local site_packages="$1"
258-
259-
log "Patching RKNN binary RPATH to search multiple library locations..."
260-
261-
# Ensure patchelf is available
262-
if ! command -v patchelf >/dev/null 2>&1; then
263-
log "patchelf not found. Installing patchelf..."
264-
# Try to install patchelf (adjust based on build environment)
265-
if command -v apt-get >/dev/null 2>&1; then
266-
apt-get update -qq && apt-get install -y patchelf >/dev/null 2>&1 || {
267-
warn "Cannot install patchelf via apt-get. Trying pip..."
268-
pip3 install patchelf >/dev/null 2>&1 || {
269-
error "Cannot install patchelf. Binary patching will be skipped."
270-
return 1
271-
}
272-
}
273-
elif command -v pip3 >/dev/null 2>&1; then
274-
pip3 install patchelf >/dev/null 2>&1 || {
275-
error "Cannot install patchelf via pip. Binary patching will be skipped."
276-
return 1
277-
}
278-
else
279-
error "Neither apt-get nor pip3 found. Cannot install patchelf."
280-
return 1
281-
fi
282-
success "patchelf installed successfully"
283-
else
284-
log "patchelf found: $(patchelf --version 2>/dev/null || echo 'version unknown')"
285-
fi
286-
287-
# Find and patch all .so files in the rknnlite package
288-
local so_count=0
289-
local patched_count=0
290-
291-
if [[ ! -d "$site_packages/rknnlite" ]]; then
292-
error "rknnlite package not found at: $site_packages/rknnlite"
293-
return 1
294-
fi
295-
296-
while IFS= read -r -d '' so_file; do
297-
log "Patching RPATH in: $(basename "$so_file")"
298-
so_count=$((so_count + 1))
299-
300-
# Set new RPATH with $ORIGIN-relative paths to extension lib directory
301-
# From site-packages/rknnlite/api/*.so to extension/usr/lib: ../../../../
302-
local new_rpath="\$ORIGIN/../../../../"
303-
304-
if patchelf --set-rpath "$new_rpath" "$so_file" 2>/dev/null; then
305-
success "Successfully patched RPATH in $(basename "$so_file")"
306-
307-
# Verify the change
308-
local current_rpath
309-
current_rpath=$(patchelf --print-rpath "$so_file" 2>/dev/null || echo "<none>")
310-
log " New RPATH: $current_rpath"
311-
312-
# CRITICAL: Replace ALL hardcoded "/usr/lib/" references with "/tmp/lib/"
313-
# This is necessary because RKNN checks os.path.exists() before dynamic loading
314-
# MUST use same length to avoid binary corruption: "/usr/lib/" (9 chars) → "/tmp/lib/" (9 chars)
315-
316-
# Check if file contains any /usr/lib/ references
317-
if strings "$so_file" | grep -q "/usr/lib/"; then
318-
log " Found hardcoded /usr/lib/ references - replacing with /tmp/lib/ path"
319-
320-
# Create backup for rollback if needed
321-
local backup_file="${so_file}.backup"
322-
cp "$so_file" "$backup_file"
323-
324-
# Replace ALL /usr/lib/ with /tmp/lib/ (both are exactly 9 characters)
325-
if sed -i 's|/usr/lib/|/tmp/lib/|g' "$so_file" 2>/dev/null; then
326-
# Verify the replacement worked
327-
local remaining_usr_lib
328-
remaining_usr_lib=$(strings "$so_file" | grep -c "/usr/lib/" || true)
329-
local found_tmp_lib
330-
found_tmp_lib=$(strings "$so_file" | grep -c "/tmp/lib/" || true)
331-
332-
if [[ $remaining_usr_lib -eq 0 && $found_tmp_lib -gt 0 ]]; then
333-
success " ✅ All /usr/lib/ → /tmp/lib/ replacements successful ($found_tmp_lib instances)"
334-
rm "$backup_file" # Remove backup on success
335-
else
336-
warn " ⚠️ Replacement may be incomplete: $remaining_usr_lib /usr/lib/ remaining, $found_tmp_lib /tmp/lib/ found"
337-
# Keep backup for debugging
338-
fi
339-
340-
# Additional verification: Check ELF header integrity
341-
if ! file "$so_file" | grep -q "ELF.*shared object.*ARM aarch64"; then
342-
error " Binary corruption detected in $(basename "$so_file"). Restoring backup."
343-
mv "$backup_file" "$so_file"
344-
return 1
345-
fi
346-
else
347-
warn " Failed to replace hardcoded paths in $(basename "$so_file")"
348-
# Restore backup on sed failure
349-
mv "$backup_file" "$so_file"
350-
fi
351-
else
352-
log " No hardcoded /usr/lib/ paths found in $(basename "$so_file")"
353-
fi
354-
355-
patched_count=$((patched_count + 1))
356-
else
357-
warn "Failed to patch RPATH in $(basename "$so_file")"
358-
fi
359-
done < <(find "$site_packages/rknnlite" -name "*.so" -type f -print0)
360-
361-
if [[ $so_count -gt 0 ]]; then
362-
if [[ $patched_count -eq $so_count ]]; then
363-
success "Patched RPATH in all $patched_count RKNN binary files"
364-
else
365-
warn "Patched RPATH in $patched_count of $so_count RKNN binary files"
366-
fi
367-
log "Binaries will now search: \$ORIGIN-relative path to extension lib directory"
368-
log "Runtime symlink location: /tmp/lib/librknnrt.so → extension/usr/lib/librknnrt.so"
369-
370-
# Create debugging script for hardware validation
371-
create_rknn_debug_script "$site_packages"
372-
else
373-
warn "No .so files found to patch in rknnlite package"
374-
return 1
375-
fi
376-
}
377-
378-
# Create debugging script for hardware validation
379-
create_rknn_debug_script() {
380-
local site_packages="$1"
381-
382-
log "Creating RKNN debugging script for hardware validation..."
383-
384-
cat > "install/sh/debug_rknn_fix.sh" << 'DEBUG_EOF'
385-
#!/bin/bash
386-
# RKNN Library Loading Debug Script
387-
# Run this script on player to diagnose RKNN library loading issues
388-
389-
echo "=== RKNN Library Loading Debug Report ==="
390-
echo "Generated: $(date)"
391-
echo "Extension Home: ${BRIGHTSIGN_PYTHON_EXTENSION_HOME:-auto-detected}"
392-
echo ""
393-
394-
# Function to find extension home
395-
find_extension_home() {
396-
if [ -n "$BRIGHTSIGN_PYTHON_EXTENSION_HOME" ]; then
397-
echo "$BRIGHTSIGN_PYTHON_EXTENSION_HOME"
398-
elif [ -d "/var/volatile/bsext/ext_pydev" ]; then
399-
echo "/var/volatile/bsext/ext_pydev"
400-
elif [ -d "/usr/local/pydev" ]; then
401-
echo "/usr/local/pydev"
402-
else
403-
echo "ERROR: Extension home not found"
404-
exit 1
405-
fi
406-
}
407-
408-
EXTENSION_HOME=$(find_extension_home)
409-
echo "Detected Extension Home: $EXTENSION_HOME"
410-
echo ""
411-
412-
# Check 1: Extension library exists
413-
echo "1. Extension Library Check:"
414-
EXTENSION_LIB="$EXTENSION_HOME/usr/lib/librknnrt.so"
415-
if [ -f "$EXTENSION_LIB" ]; then
416-
echo " ✅ Extension library exists: $EXTENSION_LIB"
417-
ls -la "$EXTENSION_LIB"
418-
else
419-
echo " ❌ Extension library missing: $EXTENSION_LIB"
420-
fi
421-
echo ""
422-
423-
# Check 2: Symlink exists and is valid
424-
echo "2. Runtime Symlink Check:"
425-
SYMLINK_PATH="/tmp/lib/librknnrt.so"
426-
if [ -L "$SYMLINK_PATH" ]; then
427-
echo " ✅ Symlink exists: $SYMLINK_PATH"
428-
ls -la "$SYMLINK_PATH"
429-
if [ -f "$SYMLINK_PATH" ]; then
430-
echo " ✅ Symlink target is accessible"
431-
else
432-
echo " ❌ Symlink target is broken"
433-
fi
434-
else
435-
echo " ❌ Symlink missing: $SYMLINK_PATH"
436-
fi
437-
echo ""
438-
439-
# Check 3: RKNN package installation
440-
echo "3. RKNN Package Check:"
441-
SITE_PACKAGES="$EXTENSION_HOME/usr/lib/python3.8/site-packages"
442-
if [ -d "$SITE_PACKAGES/rknnlite" ]; then
443-
echo " ✅ RKNN package directory exists"
444-
echo " Package contents:"
445-
find "$SITE_PACKAGES/rknnlite" -name "*.so" -exec ls -la {} \;
446-
else
447-
echo " ❌ RKNN package directory missing"
448-
fi
449-
echo ""
450-
451-
# Check 4: Binary patching verification
452-
echo "4. Binary Patching Verification:"
453-
if [ -f "$SITE_PACKAGES/rknnlite/api/rknn_runtime.cpython-38-aarch64-linux-gnu.so" ]; then
454-
BINARY_FILE="$SITE_PACKAGES/rknnlite/api/rknn_runtime.cpython-38-aarch64-linux-gnu.so"
455-
echo " Binary file: $BINARY_FILE"
456-
457-
# Check RPATH
458-
if command -v patchelf >/dev/null 2>&1; then
459-
echo " RPATH: $(patchelf --print-rpath "$BINARY_FILE" 2>/dev/null || echo 'none')"
460-
fi
461-
462-
# Check string replacement
463-
USR_LIB_COUNT=$(strings "$BINARY_FILE" | grep -c "/usr/lib/" || echo "0")
464-
TMP_LIB_COUNT=$(strings "$BINARY_FILE" | grep -c "/tmp/lib/" || echo "0")
465-
echo " Hardcoded /usr/lib/ references: $USR_LIB_COUNT (should be 0)"
466-
echo " Hardcoded /tmp/lib/ references: $TMP_LIB_COUNT (should be >0)"
467-
468-
if [ "$USR_LIB_COUNT" -eq 0 ] && [ "$TMP_LIB_COUNT" -gt 0 ]; then
469-
echo " ✅ String replacement successful"
470-
else
471-
echo " ❌ String replacement incomplete or failed"
472-
fi
473-
else
474-
echo " ❌ RKNN runtime binary not found"
475-
fi
476-
echo ""
477-
478-
# Check 5: Python environment test
479-
echo "5. Python Environment Test:"
480-
export PYTHONPATH="$SITE_PACKAGES:$PYTHONPATH"
481-
export LD_LIBRARY_PATH="$EXTENSION_HOME/usr/lib:$LD_LIBRARY_PATH"
482-
483-
if python3 -c "import rknnlite; print('✅ RKNN import successful')" 2>/dev/null; then
484-
echo " ✅ RKNN package imports successfully"
485-
else
486-
echo " ❌ RKNN import failed"
487-
fi
488-
489-
if python3 -c "from rknnlite.api import RKNNLite; print('✅ RKNNLite class available')" 2>/dev/null; then
490-
echo " ✅ RKNNLite class available"
491-
else
492-
echo " ❌ RKNNLite class unavailable"
493-
fi
494-
echo ""
495-
496-
# Check 6: Runtime initialization test
497-
echo "6. Runtime Initialization Test:"
498-
python3 << 'PYTHON_EOF'
499-
import sys
500-
try:
501-
from rknnlite.api import RKNNLite
502-
print(" ✅ Import successful")
503-
504-
rknn = RKNNLite()
505-
print(" ✅ Object creation successful")
506-
507-
# This is where the failure typically occurs
508-
# We'll capture any exception details
509-
try:
510-
# Note: This will fail without actual model file, but shows library loading status
511-
rknn.init_runtime()
512-
print(" ✅ Runtime initialization successful (unexpected but good!)")
513-
except Exception as e:
514-
error_msg = str(e)
515-
if "Can not find dynamic library" in error_msg:
516-
if "/tmp/lib/" in error_msg:
517-
print(" ❌ Library loading failed - string replacement worked but symlink/library issue")
518-
print(f" Error: {error_msg}")
519-
else:
520-
print(" ❌ Library loading failed - string replacement didn't work")
521-
print(f" Error: {error_msg}")
522-
else:
523-
print(" ⚠️ Different runtime error (may be expected without model file):")
524-
print(f" Error: {error_msg}")
525-
526-
except ImportError as e:
527-
print(f" ❌ Import failed: {e}")
528-
except Exception as e:
529-
print(f" ❌ Unexpected error: {e}")
530-
PYTHON_EOF
531-
532-
echo ""
533-
echo "=== Debug Report Complete ==="
534-
echo "If RKNN initialization is still failing, please check:"
535-
echo "1. All symlinks are properly created"
536-
echo "2. Library file has correct permissions"
537-
echo "3. No missing dependencies in library file"
538-
echo "4. Architecture compatibility (should be ARM64/aarch64)"
539-
DEBUG_EOF
540-
541-
chmod +x "install/sh/debug_rknn_fix.sh"
542-
success "Created RKNN debug script: install/sh/debug_rknn_fix.sh"
543-
log "Run this script on player to diagnose any remaining issues"
544-
}
545-
546251
# Copy YOLOX example if requested
547252
copy_yolox_example() {
548253
if [[ "$INCLUDE_YOLOX" == "true" ]]; then

sh/init-extension

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,13 @@ else
1616
exit 1
1717
fi
1818

19-
# Setup RKNN runtime library symlink for patched binaries
20-
# RKNN binaries have been patched to look for /tmp/lib/librknnrt.so (same length as original path)
21-
EXTENSION_LIB="${EXTENSION_HOME}/usr/lib/librknnrt.so"
22-
SYSTEM_LIB="/tmp/lib/librknnrt.so"
23-
24-
if [ -f "$EXTENSION_LIB" ]; then
25-
echo "Setting up RKNN runtime library..."
26-
# Ensure /tmp/lib directory exists (always writable)
27-
mkdir -p "/tmp/lib" 2>/dev/null || true
28-
29-
if [ ! -e "$SYSTEM_LIB" ]; then
30-
echo " Creating symlink: $SYSTEM_LIB -> $EXTENSION_LIB"
31-
if ln -sf "$EXTENSION_LIB" "$SYSTEM_LIB" 2>/dev/null; then
32-
echo " ✅ RKNN runtime library linked (patched binaries will find it)"
33-
else
34-
echo " ⚠️ Warning: Failed to create symlink"
35-
logger -t "bsext-pydev" "Failed to create librknnrt.so symlink"
36-
fi
37-
else
38-
echo " ✅ RKNN runtime library already linked at $SYSTEM_LIB"
39-
fi
19+
# Verify RKNN runtime library is available (OS 9.1.79.3+ provides this)
20+
if [ -f "/usr/lib/librknnrt.so" ]; then
21+
echo "✅ RKNN runtime library found at /usr/lib/librknnrt.so (OS 9.1.79.3+)"
4022
else
41-
echo "⚠️ Warning: Extension librknnrt.so not found at $EXTENSION_LIB"
42-
logger -t "bsext-pydev" "RKNN runtime library not found"
23+
echo "⚠️ Warning: RKNN runtime library not found at /usr/lib/librknnrt.so"
24+
echo " Requires BrightSign OS 9.1.79.3 or later"
25+
logger -t "bsext-pydev" "RKNN runtime library not found - OS 9.1.79.3+ required"
4326
fi
4427

4528
# Verify rknn-toolkit-lite2 package is available

0 commit comments

Comments
 (0)