#!/bin/bash # Fix NEEDED entries in release builds # This script replaces full paths with just library names in NEEDED entries if [[ "$1" == "--help" || "$1" == "-h" ]]; then echo "Usage: $0 [binary_path] [library_name]" echo " binary_path: Path to binary (default: script_dir/zig-out/bin/stt)" echo " library_name: Library to fix (default: libvosk.so)" echo "" echo "Example: $0 my_binary libfoo.so" exit 0 fi SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" BINARY="${1:-$SCRIPT_DIR/zig-out/bin/stt}" LIBRARY="${2:-libvosk.so}" if [ ! -f "$BINARY" ]; then echo "Binary not found: $BINARY" >&2 exit 1 fi echo "Fixing NEEDED entries for $BINARY..." # Get the current NEEDED entries if command -v readelf >/dev/null 2>&1; then FULL_PATH=$(readelf --dynamic "$BINARY" | grep "NEEDED" | grep "$LIBRARY" | head -1 | sed 's/.*\[\(.*\)\]/\1/') elif command -v nix-shell >/dev/null 2>&1; then echo "Using nix-shell to run readelf..." FULL_PATH=$(nix-shell -p binutils --run "readelf --dynamic '$BINARY'" | grep "NEEDED" | grep "$LIBRARY" | head -1 | sed 's/.*\[\(.*\)\]/\1/') else echo "Error: Neither readelf nor nix-shell found" >&2 echo "Install binutils or nix to check NEEDED entries" >&2 exit 1 fi if [[ "$FULL_PATH" == *"/"* ]]; then echo "Found full path: $FULL_PATH" echo "Replacing with: $LIBRARY" # Try patchelf directly, fall back to nix-shell if command -v patchelf >/dev/null 2>&1; then patchelf --replace-needed "$FULL_PATH" "$LIBRARY" "$BINARY" elif command -v nix-shell >/dev/null 2>&1; then echo "Using nix-shell to run patchelf..." nix-shell -p patchelf --run "patchelf --replace-needed '$FULL_PATH' '$LIBRARY' '$BINARY'" else echo "Error: Neither patchelf nor nix-shell found" >&2 echo "Install patchelf or nix to fix NEEDED entries" >&2 exit 1 fi echo "Fixed!" else echo "No full path found, binary is already portable" fi