Skip to content

Instantly share code, notes, and snippets.

@jeremyong
Last active June 10, 2016 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyong/b0e0da5a98ec64d5dc3ff7cbb7e87a8a to your computer and use it in GitHub Desktop.
Save jeremyong/b0e0da5a98ec64d5dc3ff7cbb7e87a8a to your computer and use it in GitHub Desktop.
Creating a tarball from an executable that includes all dynamically linked libraries
#!/usr/bin/env bash
# This function takes a target executable and replicates its link dependencies to the target directory
# Also copies the dependencies to the bin subdirectory of the target
# Arg 1: executable
# Arg 2: output directory
copy_dependencies()
{
echo "Emitting linking dependencies of $1 to $2..."
mkdir -p $2
SYMLINKS=$(ldd $1 | grep "=>" | awk '{print $3}')
DIRS=$(echo "${SYMLINKS}" | xargs dirname)
LIBS=$(echo "${SYMLINKS}" | xargs readlink -f)
# Create all paths housing the symlinked dependencies with the supplied directory as the root
NEW_DIRS=$(echo "${DIRS}" | awk -v root="$2" '{ print root$1 }')
echo "${NEW_DIRS}" | xargs mkdir -p
NEW_PATHS=$(echo "${SYMLINKS}" | awk -v root="$2" '{ print root$1 }')
# Copy actual libraries to the new locations with the names of the symlinked values
JOINED=$(paste <(echo "${LIBS}") <(echo "${NEW_PATHS}"))
echo "${JOINED}" | awk '{ system("cp "$1" "$2) }'
mkdir -p $2/bin
cp $1 $2/bin
echo "Done."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment