Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
fix_gfortran_lib_rpaths.sh
#!/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."
@phcerdan

This comment has been minimized.

Copy link
Owner Author

@phcerdan phcerdan commented Sep 13, 2018

Example of (verbose) output:

./fix_gfortran_lib_rpaths.sh $(pwd)/lib

Processing lib: /path/gfortran-env/lib-exp/libcloog-isl.4.dylib
Before:
/path/gfortran-env/lib-exp/libcloog-isl.4.dylib:
        @rpath/libcloog-isl.4.dylib (compatibility version 5.0.0, current version 5.0.0)
        @rpath/libisl.10.dylib (compatibility version 13.0.0, current version 13.1.0)
        @rpath/libgmp.10.dylib (compatibility version 14.0.0, current version 14.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
After:
/path/gfortran-env/lib-exp/libcloog-isl.4.dylib:
        /path/gfortran-env/lib-exp/libcloog-isl.4.dylib (compatibility version 5.0.0, current version 5.0.0)
        /path/gfortran-env/lib-exp/libisl.10.dylib (compatibility version 13.0.0, current version 13.1.0)
        /path/gfortran-env/lib-exp/libgmp.10.dylib (compatibility version 14.0.0, current version 14.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
********
Processing lib: /path/gfortran-env/lib-exp/libgcc_ext.10.4.dylib
Before:
/path/gfortran-env/lib-exp/libgcc_ext.10.4.dylib:
        /path/gfortran-env/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/install_name_tool: input file: /path/gfortran-env/lib-exp/libgcc_ext.10.4.dylib is Mach-O dynamic shared library stub file and can't be changed
After:
/path/gfortran-env/lib-exp/libgcc_ext.10.4.dylib:
        /path/gfortran-env/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
********
Processing lib: /path/gfortran-env/lib-exp/libgcc_ext.10.5.dylib
Before:
/path/gfortran-env/lib-exp/libgcc_ext.10.5.dylib:
        /path/gfortran-env/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/install_name_tool: input file: /path/gfortran-env/lib-exp/libgcc_ext.10.5.dylib is Mach-O dynamic shared library stub file and can't be changed
After:
/path/gfortran-env/lib-exp/libgcc_ext.10.5.dylib:
        /path/gfortran-env/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
********
Processing lib: /path/gfortran-env/lib-exp/libgcc_s.1.dylib
Before:
/path/gfortran-env/lib-exp/libgcc_s.1.dylib:
        @rpath/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
After:
/path/gfortran-env/lib-exp/libgcc_s.1.dylib:
        /path/gfortran-env/lib-exp/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
********
Processing lib: /path/gfortran-env/lib-exp/libgcc_s_ppc64.1.dylib
Before:
/path/gfortran-env/lib-exp/libgcc_s_ppc64.1.dylib:
        @rpath/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
After:
/path/gfortran-env/lib-exp/libgcc_s_ppc64.1.dylib:
        /path/gfortran-env/lib-exp/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
********
Processing lib: /path/gfortran-env/lib-exp/libgcc_s_x86_64.1.dylib
Before:
/path/gfortran-env/lib-exp/libgcc_s_x86_64.1.dylib:
        @rpath/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
After:
/path/gfortran-env/lib-exp/libgcc_s_x86_64.1.dylib:
        /path/gfortran-env/lib-exp/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
********
Processing lib: /path/gfortran-env/lib-exp/libgfortran.3.dylib
Before:
/path/gfortran-env/lib-exp/libgfortran.3.dylib:
        @rpath/libgfortran.3.dylib (compatibility version 4.0.0, current version 4.0.0)
        @rpath/libquadmath.0.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
        @rpath/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
After:
/path/gfortran-env/lib-exp/libgfortran.3.dylib:
        /path/gfortran-env/lib-exp/libgfortran.3.dylib (compatibility version 4.0.0, current version 4.0.0)
        /path/gfortran-env/lib-exp/libquadmath.0.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
        /path/gfortran-env/lib-exp/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
********
Processing lib: /path/gfortran-env/lib-exp/libgfortran.dylib
Before:
/path/gfortran-env/lib-exp/libgfortran.dylib:
        @rpath/libgfortran.3.dylib (compatibility version 4.0.0, current version 4.0.0)
        @rpath/libquadmath.0.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
        @rpath/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
After:
/path/gfortran-env/lib-exp/libgfortran.dylib:
        /path/gfortran-env/lib-exp/libgfortran.3.dylib (compatibility version 4.0.0, current version 4.0.0)
        /path/gfortran-env/lib-exp/libquadmath.0.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
        /path/gfortran-env/lib-exp/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
********
Processing lib: /path/gfortran-env/lib-exp/libgmp.10.dylib
Before:
/path/gfortran-env/lib-exp/libgmp.10.dylib:
        @rpath/libgmp.10.dylib (compatibility version 14.0.0, current version 14.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
After:
/path/gfortran-env/lib-exp/libgmp.10.dylib:
        /path/gfortran-env/lib-exp/libgmp.10.dylib (compatibility version 14.0.0, current version 14.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
********
Processing lib: /path/gfortran-env/lib-exp/libgomp.1.dylib
Before:
/path/gfortran-env/lib-exp/libgomp.1.dylib:
        @rpath/libgomp.1.dylib (compatibility version 2.0.0, current version 2.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
        @rpath/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
After:
/path/gfortran-env/lib-exp/libgomp.1.dylib:
        /path/gfortran-env/lib-exp/libgomp.1.dylib (compatibility version 2.0.0, current version 2.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
        /path/gfortran-env/lib-exp/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
********
Processing lib: /path/gfortran-env/lib-exp/libgomp.dylib
Before:
/path/gfortran-env/lib-exp/libgomp.dylib:
        @rpath/libgomp.1.dylib (compatibility version 2.0.0, current version 2.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
        @rpath/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
After:
/path/gfortran-env/lib-exp/libgomp.dylib:
        /path/gfortran-env/lib-exp/libgomp.1.dylib (compatibility version 2.0.0, current version 2.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
        /path/gfortran-env/lib-exp/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
********
Processing lib: /path/gfortran-env/lib-exp/libisl.10.dylib
Before:
/path/gfortran-env/lib-exp/libisl.10.dylib:
        @rpath/libisl.10.dylib (compatibility version 13.0.0, current version 13.1.0)
        @rpath/libgmp.10.dylib (compatibility version 14.0.0, current version 14.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
After:
/path/gfortran-env/lib-exp/libisl.10.dylib:
        /path/gfortran-env/lib-exp/libisl.10.dylib (compatibility version 13.0.0, current version 13.1.0)
        /path/gfortran-env/lib-exp/libgmp.10.dylib (compatibility version 14.0.0, current version 14.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
********
Processing lib: /path/gfortran-env/lib-exp/libquadmath.0.dylib
Before:
/path/gfortran-env/lib-exp/libquadmath.0.dylib:
        @rpath/libquadmath.0.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
        @rpath/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
After:
/path/gfortran-env/lib-exp/libquadmath.0.dylib:
        /path/gfortran-env/lib-exp/libquadmath.0.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
        /path/gfortran-env/lib-exp/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
********
Processing lib: /path/gfortran-env/lib-exp/libquadmath.dylib
Before:
/path/gfortran-env/lib-exp/libquadmath.dylib:
        @rpath/libquadmath.0.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
        @rpath/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
After:
/path/gfortran-env/lib-exp/libquadmath.dylib:
        /path/gfortran-env/lib-exp/libquadmath.0.dylib (compatibility version 1.0.0, current version 1.0.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
        /path/gfortran-env/lib-exp/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
********
Processing lib: /path/gfortran-env/lib-exp/libstdc++.6.dylib
Before:
/path/gfortran-env/lib-exp/libstdc++.6.dylib:
        @rpath/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.19.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
        @rpath/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
After:
/path/gfortran-env/lib-exp/libstdc++.6.dylib:
        /path/gfortran-env/lib-exp/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.19.0)
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
        /path/gfortran-env/lib-exp/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
********
Finished, errors of type:
  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.'
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
You can’t perform that action at this time.