Skip to content

Instantly share code, notes, and snippets.

@lynxluna
Created January 27, 2025 14:45
Show Gist options
  • Save lynxluna/8f0045827f1a436c3d1590a053b9194a to your computer and use it in GitHub Desktop.
Save lynxluna/8f0045827f1a436c3d1590a053b9194a to your computer and use it in GitHub Desktop.
#!/bin/bash
# merge-sdk.sh
# Merges extracted SDK with existing crosstool-ng sysroot
set -e
# Configuration
CROSSTOOL_SYSROOT="/path/to/your/crosstool/sysroot" # Change this
EXTRACTED_SDK="./sysroot"
BACKUP_DIR="./sysroot_backup_$(date +%Y%m%d_%H%M%S)"
# Verify paths
if [ ! -d "$CROSSTOOL_SYSROOT" ]; then
echo "Error: Crosstool sysroot directory not found: $CROSSTOOL_SYSROOT"
exit 1
fi
if [ ! -d "$EXTRACTED_SDK" ]; then
echo "Error: Extracted SDK directory not found: $EXTRACTED_SDK"
exit 1
fi
# Create backup of crosstool sysroot
echo "Creating backup of crosstool sysroot..."
cp -a "$CROSSTOOL_SYSROOT" "$BACKUP_DIR"
# Function to merge directories
merge_directory() {
local src="$1"
local dst="$2"
local dir="$3"
if [ -d "${src}${dir}" ]; then
mkdir -p "${dst}${dir}"
# Copy with dereferencing symlinks (-L) and preserve attributes (-p)
cp -a -L "${src}${dir}"/* "${dst}${dir}/" 2>/dev/null || true
fi
}
echo "Merging SDK into crosstool sysroot..."
# Merge key directories
merge_directory "$EXTRACTED_SDK" "$CROSSTOOL_SYSROOT" "/usr/include"
merge_directory "$EXTRACTED_SDK" "$CROSSTOOL_SYSROOT" "/usr/lib"
merge_directory "$EXTRACTED_SDK" "$CROSSTOOL_SYSROOT" "/usr/lib/pkgconfig"
merge_directory "$EXTRACTED_SDK" "$CROSSTOOL_SYSROOT" "/usr/share/pkgconfig"
# Fix pkg-config files
echo "Updating pkg-config files..."
find "$CROSSTOOL_SYSROOT" -name '*.pc' -type f -exec sed -i "s|^prefix=.*|prefix=${CROSSTOOL_SYSROOT}/usr|" {} \;
# Fix symlinks
echo "Fixing symlinks..."
find "$CROSSTOOL_SYSROOT" -type l | while read -r link; do
target=$(readlink "$link")
if [[ "$target" == /* ]]; then
# Convert absolute symlinks to relative
rel_target=$(realpath --relative-to="$(dirname "$link")" "${CROSSTOOL_SYSROOT}${target}")
ln -sf "$rel_target" "$link"
fi
done
echo "Merge complete."
echo "Backup of original sysroot saved in: $BACKUP_DIR"
echo "Remember to check pkg-config and library paths in your build configuration."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment