Skip to content

Commit 18ff4fc

Browse files
committed
Fix dynlib loading
1 parent 9663760 commit 18ff4fc

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

cpython-unix/build-cpython.sh

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -616,21 +616,41 @@ if [ "${PYBUILD_SHARED}" = "1" ]; then
616616
LIBPYTHON_SHARED_LIBRARY_BASENAME=libpython${PYTHON_MAJMIN_VERSION}${PYTHON_BINARY_SUFFIX}.so.1.0
617617
LIBPYTHON_SHARED_LIBRARY=${ROOT}/out/python/install/lib/${LIBPYTHON_SHARED_LIBRARY_BASENAME}
618618

619-
# If we simply set DT_RUNPATH via --set-rpath, LD_LIBRARY_PATH would be used before
620-
# DT_RUNPATH, which could result in confusion at run-time. But if DT_NEEDED
621-
# contains a slash, the explicit path is used.
622-
patchelf --replace-needed ${LIBPYTHON_SHARED_LIBRARY_BASENAME} "\$ORIGIN/../lib/${LIBPYTHON_SHARED_LIBRARY_BASENAME}" \
623-
${ROOT}/out/python/install/bin/python${PYTHON_MAJMIN_VERSION}
624-
625-
# libpython3.so isn't present in debug builds.
626-
if [ -z "${CPYTHON_DEBUG}" ]; then
627-
patchelf --replace-needed ${LIBPYTHON_SHARED_LIBRARY_BASENAME} "\$ORIGIN/../lib/${LIBPYTHON_SHARED_LIBRARY_BASENAME}" \
628-
${ROOT}/out/python/install/lib/libpython3.so
629-
fi
630-
631-
if [ -n "${PYTHON_BINARY_SUFFIX}" ]; then
619+
if [ "${CC}" == "musl-clang" ]; then
620+
# musl does not support $ORIGIN in DT_NEEDED, so we use RPATH instead. This could be
621+
# problematic, i.e., we could load the shared library from the wrong location — but
622+
# there's not a clear alternative at this time. The longterm solution is likely to
623+
# statically link to libpython instead.
624+
patchelf --set-rpath "\$ORIGIN/../lib" \
625+
${ROOT}/out/python/install/bin/python${PYTHON_MAJMIN_VERSION}
626+
627+
# libpython3.so isn't present in debug builds.
628+
if [ -z "${CPYTHON_DEBUG}" ]; then
629+
patchelf --set-rpath "\$ORIGIN/../lib" \
630+
${ROOT}/out/python/install/lib/libpython3.so
631+
fi
632+
633+
if [ -n "${PYTHON_BINARY_SUFFIX}" ]; then
634+
patchelf --set-rpath "\$ORIGIN/../lib" \
635+
${ROOT}/out/python/install/bin/python${PYTHON_MAJMIN_VERSION}${PYTHON_BINARY_SUFFIX}
636+
fi
637+
else
638+
# If we simply set DT_RUNPATH via --set-rpath, LD_LIBRARY_PATH would be used before
639+
# DT_RUNPATH, which could result in confusion at run-time. But if DT_NEEDED contains a
640+
# slash, the explicit path is used.
632641
patchelf --replace-needed ${LIBPYTHON_SHARED_LIBRARY_BASENAME} "\$ORIGIN/../lib/${LIBPYTHON_SHARED_LIBRARY_BASENAME}" \
633-
${ROOT}/out/python/install/bin/python${PYTHON_MAJMIN_VERSION}${PYTHON_BINARY_SUFFIX}
642+
${ROOT}/out/python/install/bin/python${PYTHON_MAJMIN_VERSION}
643+
644+
# libpython3.so isn't present in debug builds.
645+
if [ -z "${CPYTHON_DEBUG}" ]; then
646+
patchelf --replace-needed ${LIBPYTHON_SHARED_LIBRARY_BASENAME} "\$ORIGIN/../lib/${LIBPYTHON_SHARED_LIBRARY_BASENAME}" \
647+
${ROOT}/out/python/install/lib/libpython3.so
648+
fi
649+
650+
if [ -n "${PYTHON_BINARY_SUFFIX}" ]; then
651+
patchelf --replace-needed ${LIBPYTHON_SHARED_LIBRARY_BASENAME} "\$ORIGIN/../lib/${LIBPYTHON_SHARED_LIBRARY_BASENAME}" \
652+
${ROOT}/out/python/install/bin/python${PYTHON_MAJMIN_VERSION}${PYTHON_BINARY_SUFFIX}
653+
fi
634654
fi
635655
fi
636656
fi

0 commit comments

Comments
 (0)