Skip to content

Commit 49f4101

Browse files
committed
fixup-libgfortran.sh: Use patchelf instead of parsing ldd output
This should be less brittle, and even better, work on `musl` Also ask `$FC` where `libgfortran` lives if we have no dirname Ask `libgfortran` where to get its libraries, don't rely on recursive dep printing
1 parent c5b5e95 commit 49f4101

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

contrib/fixup-libgfortran.sh

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ find_shlib()
3636
lib_path="$1"
3737
if [ -f "$lib_path" ]; then
3838
if [ "$UNAME" = "Linux" ]; then
39-
ldd "$lib_path" | grep $2 | grep -v "not found" | cut -d' ' -f3 | xargs
39+
"${PATCHELF}" --print-needed "$lib_path" | grep $2 | xargs
4040
else # $UNAME is "Darwin", we only have two options, see above
4141
otool -L "$lib_path" | grep $2 | cut -d' ' -f1 | xargs
4242
fi
@@ -50,7 +50,7 @@ find_shlib_dir()
5050
# only get something like `@rpath/libgfortran.5.dylib` when inspecting the
5151
# libraries. We can, as a last resort, ask `$FC` directly what the full
5252
# filepath for this library is, but only if we don't have a direct path to it:
53-
if [ $(dirname "$1") = "@rpath" ]; then
53+
if [ $(dirname "$1") = "@rpath" ] || [ $(dirname "$1") = "." ]; then
5454
dirname "$($FC -print-file-name="$(basename "$1")" 2>/dev/null)"
5555
else
5656
dirname "$1" 2>/dev/null
@@ -62,19 +62,25 @@ for lib in lapack blas openblas; do
6262
for private_libname in ${private_libdir}/lib$lib*.$SHLIB_EXT*; do
6363
# Find the paths to the libraries we're interested in. These are almost
6464
# always within the same directory, but we like to be general.
65-
LIBGFORTRAN_PATH=$(find_shlib "$private_libname" libgfortran)
66-
LIBGCC_PATH=$(find_shlib "$private_libname" libgcc_s)
67-
LIBQUADMATH_PATH=$(find_shlib "$private_libname" libquadmath)
65+
LIBGFORTRAN_PATH="$(find_shlib "$private_libname" libgfortran)"
6866

6967
# Take the directories, add them onto LIBGFORTRAN_DIRS, which we use to
7068
# search for these libraries in the future. If there is no directory, try
7169
# asking `$FC` where such a file could be found.
72-
LIBGFORTRAN_DIRS="$LIBGFORTRAN_DIRS $(find_shlib_dir $LIBGFORTRAN_PATH)"
70+
LIBGFORTRAN_DIR="$(find_shlib_dir "$LIBGFORTRAN_PATH")"
71+
LIBGFORTRAN_DIRS="$LIBGFORTRAN_DIRS $LIBGFORTRAN_DIR"
72+
73+
# Save the SONAME
74+
LIBGFORTRAN_SONAME="$(basename "$LIBGFORTRAN_PATH")"
75+
LIBGFORTRAN_SONAMES="$LIBGFORTRAN_SONAMES $LIBGFORTRAN_SONAME"
76+
77+
78+
# Now that we've (maybe) found a libgfortran, ask _it_ for things like libgcc_s and libquadmath:
79+
LIBGCC_PATH="$(find_shlib "${LIBGFORTRAN_DIR}/${LIBGFORTRAN_SONAME}" libgcc_s)"
80+
LIBQUADMATH_PATH="$(find_shlib "${LIBGFORTRAN_DIR}/${LIBGFORTRAN_SONAME}" libquadmath)"
81+
7382
LIBGFORTRAN_DIRS="$LIBGFORTRAN_DIRS $(find_shlib_dir $LIBGCC_PATH)"
7483
LIBGFORTRAN_DIRS="$LIBGFORTRAN_DIRS $(find_shlib_dir $LIBQUADMATH_PATH)"
75-
76-
# Save the SONAMES
77-
LIBGFORTRAN_SONAMES="$LIBGFORTRAN_SONAMES $(basename "$LIBGFORTRAN_PATH")"
7884
LIBGCC_SONAMES="$LIBGCC_SONAMES $(basename "$LIBGCC_PATH")"
7985
LIBQUADMATH_SONAMES="$LIBQUADMATH_SONAMES $(basename "$LIBQUADMATH_PATH")"
8086
done

0 commit comments

Comments
 (0)