Last active
September 10, 2020 15:45
-
-
Save phcerdan/146de9398052335d8a76c5b0070715a6 to your computer and use it in GitHub Desktop.
fix_gfortran_lib_rpaths.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#################### | |
# Display help | |
display_usage() { | |
echo "Replace relative paths: @rpath/libname.dylib for /fullpath/libname.dylib." | |
echo " otool -L libname.dylib" | |
echo " install_name_tool -id \${full_libpath} -change \${@rpath/libname} \${full_libpath} -change ... \${full_libpath}" | |
echo -e "\nUsage:\n $(basename "$0") /path/gfortran_folder/lib \n" | |
} | |
# if less than one argument supplied, display usage | |
if [ $# -ne 1 ] | |
then | |
display_usage | |
exit 1 | |
fi | |
# check whether user had supplied -h or --help. | |
if [[ ( $@ == "--help") || $@ == "-h" ]] | |
then | |
display_usage | |
exit 0 | |
fi | |
#################### | |
if [[ $1 != "/"* ]] | |
then | |
echo "Provide a full path to a folder instead of $1" | |
exit 1 | |
fi | |
folder="$1" | |
if [ ! -d "$folder" ]; then | |
echo "Folder $folder does not exist." | |
exit 1 | |
fi | |
names=() | |
files=() | |
for f in ${folder}/*.dylib; do | |
name=$(basename ${f}) | |
files+=("$f") | |
names+=("$name") | |
done; | |
# Get a string with all name changes. | |
#-change old new | |
# Changes the dependent shared library install name old to new in the specified Mach-O binary. | |
# More than one of these options can be specified. | |
# If the Mach-O binary does not contain the old install name in a specified -change option the option is ignored. | |
num_libs=${#names[@]} | |
all_changes="" | |
for ((i=0; i<${num_libs}; i++)); do | |
name="${names[i]}" | |
file="${files[i]}" | |
all_changes+="-change @rpath/$name $file " | |
done; | |
function get_id_from_otool() { | |
local libid="$(otool -L $1 | sed -n 2p | sed -n 's/\(.*\/\)\(lib.[^\/]*\.dylib\).*/\2/p')" | |
echo "${libid}" | |
} | |
for f in ${files[@]}; do | |
echo "Processing lib: ${f}" | |
echo "Before:" | |
otool -L ${f} | |
libid=$(get_id_from_otool ${f}) | |
install_name_tool -id $(dirname ${f})/${libid} ${all_changes} ${f} | |
echo "After:" | |
otool -L ${f} | |
echo "********" | |
done; | |
echo "Finished, errors of type:" | |
echo " install_name_tool input file: /tmp/gfortran_lib/libgcc_ext.10.5.dylib is Mach-O dynamic shared library stub file and can't be changed.'" | |
echo "are ok. You might want to use 'otool -L' for checking the results." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of (verbose) output: