From eace956416883966f0ad8a525c5d284a933fd8f5 Mon Sep 17 00:00:00 2001 From: Mika Lindqvist Date: Fri, 23 Jun 2023 04:32:58 +0300 Subject: [PATCH] Allow re-enabling @rpath on OS/X when using relative install prefix --- CMakeLists.txt | 23 ++++++++++++++++++++--- configure | 7 ++++++- test/pkgcheck.sh | 19 +++++++++++-------- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b420769ea..420a5c78d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -966,8 +966,15 @@ else() set(PC_INC_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}") endif() +if(APPLE) + option(WITH_RPATH "Enable RPATH for shared library" OFF) +endif() if(NOT IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}") - set(PC_LIB_INSTALL_DIR "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}") + if(APPLE AND WITH_RPATH) + set(PC_LIB_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}") + else() + set(PC_LIB_INSTALL_DIR "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}") + endif() elseif(LIBDIR_POS EQUAL 0) string(SUBSTRING "${CMAKE_INSTALL_LIBDIR}" "${INSTALL_PREFIX_LEN}" "-1" LIBDIR_RELATIVE) set(PC_LIB_INSTALL_DIR "\${exec_prefix}/${LIBDIR_RELATIVE}") @@ -1135,9 +1142,11 @@ if(NOT DEFINED BUILD_SHARED_LIBS OR BUILD_SHARED_LIBS) if(NOT APPLE) set_target_properties(zlib PROPERTIES LINK_FLAGS "-Wl,--version-script,\"${CMAKE_CURRENT_SOURCE_DIR}/zlib${SUFFIX}.map\"") + elseif(IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}" OR NOT WITH_RPATH) + # Match configure/make's behavior (i.e. don't use @rpath on mac when using absolute path). + set_target_properties(zlib PROPERTIES INSTALL_NAME_DIR "@rpath/${CMAKE_INSTALL_FULL_LIBDIR}") else() - # Match configure/make's behavior (i.e. don't use @rpath on mac). - set_target_properties(zlib PROPERTIES INSTALL_NAME_DIR "${CMAKE_INSTALL_FULL_LIBDIR}") + set_target_properties(zlib PROPERTIES INSTALL_NAME_DIR "@rpath/${CMAKE_INSTALL_LIBDIR}") endif() endif() if(MSYS) @@ -1174,6 +1183,10 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/zlib${SUFFIX}.h.in configure_file(${CMAKE_CURRENT_SOURCE_DIR}/gzread.c.in ${CMAKE_CURRENT_BINARY_DIR}/gzread.c @ONLY) +# Fix install directory after generating zlib.pc/zlib-ng.pc +if (NOT IS_ABSOLUTE CMAKE_INSTALL_LIBDIR AND WITH_RPATH) + set(CMAKE_INSTALL_LIBDIR "/${CMAKE_INSTALL_LIBDIR}") +endif() if (NOT ZLIB_SYMBOL_PREFIX STREQUAL "") add_feature_info(ZLIB_SYMBOL_PREFIX ON "Publicly exported symbols have a custom prefix") @@ -1273,4 +1286,8 @@ endif() add_feature_info(INSTALL_UTILS INSTALL_UTILS "Copy minigzip and minideflate during install") +if(APPLE) + add_feature_info(WITH_RPATH WITH_RPATH "Enable RPATH for shared library") +endif() + FEATURE_SUMMARY(WHAT ALL INCLUDE_QUIET_PACKAGES) diff --git a/configure b/configure index 9c0ea34314..8714590e52 100755 --- a/configure +++ b/configure @@ -513,7 +513,12 @@ if test "$gcc" -eq 1 && ($cc $CFLAGS -c $test.c) >> configure.log 2>&1; then SHAREDLIBM=${LIBNAME}.$VER1$shared_ext SHAREDTARGET=$SHAREDLIBV LDSHARED=${LDSHARED-"$cc"} - LDSHAREDFLAGS="-dynamiclib -install_name $libdir/$SHAREDLIBM -compatibility_version $VER1 -current_version $VER3" + case ${libdir} in + /*) + LDSHAREDFLAGS="-dynamiclib -install_name ${libdir}/${SHAREDLIBM} -compatibility_version ${VER1} -current_version ${VER3}" ;; + *) + LDSHAREDFLAGS="-dynamiclib -install_name @rpath/${libdir}/${SHAREDLIBM} -compatibility_version ${VER1} -current_version ${VER3}" ;; + esac if libtool -V 2>&1 | grep Apple > /dev/null; then AR="libtool" else diff --git a/test/pkgcheck.sh b/test/pkgcheck.sh index 87a4e61efd..629f98aded 100644 --- a/test/pkgcheck.sh +++ b/test/pkgcheck.sh @@ -47,10 +47,6 @@ _EOF_ set -ex -# Caller can also set CMAKE_ARGS or CONFIGURE_ARGS if desired -CMAKE_ARGS="-DCMAKE_INSTALL_LIBDIR=lib ${CMAKE_ARGS}" -CONFIGURE_ARGS=${CONFIGURE_ARGS} - case "$1" in --zlib-compat) suffix="" @@ -85,6 +81,11 @@ Darwin) sysctl -n machdep.cpu.features sysctl -n machdep.cpu.leaf7_features sysctl -n machdep.cpu.extfeatures + CMAKE_ARGS="-DCMAKE_INSTALL_LIBDIR=lib -DPKGCONFIG_INSTALL_DIR=/lib/pkgconfig -DWITH_RPATH=on ${CMAKE_ARGS}" + CONFIGURE_ARGS="--libdir=lib ${CONFIGURE_ARGS}" + ;; +*) + CMAKE_ARGS="-DCMAKE_INSTALL_LIBDIR=lib ${CMAKE_ARGS}" ;; esac @@ -117,7 +118,7 @@ cd .. # Original build system rm -rf btmp1 pkgtmp1 mkdir btmp1 pkgtmp1 -export DESTDIR=$(pwd)/pkgtmp1 +export DESTDIR=$(pwd)/pkgtmp1/ cd btmp1 case $(uname) in Darwin) @@ -130,13 +131,15 @@ cd btmp1 cd .. repack_ar() { - if ! cmp --silent pkgtmp1/usr/local/lib/libz$suffix.a pkgtmp2/usr/local/lib/libz$suffix.a + archive1=$(cd pkgtmp1; find . -type f -name '*.a'; cd ..) + archive2=$(cd pkgtmp2; find . -type f -name '*.a'; cd ..) + if ! cmp --silent pkgtmp1/$archive1 pkgtmp2/$archive2 then echo "libz$suffix.a does not match. Probably filenames differ (.o vs .c.o). Unpacking and renaming..." # Note: %% is posix shell syntax meaning "Remove Largest Suffix Pattern", see # https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 - cd pkgtmp1; ar x usr/local/lib/libz$suffix.a; rm usr/local/lib/libz$suffix.a; cd .. - cd pkgtmp2; ar x usr/local/lib/libz$suffix.a; rm usr/local/lib/libz$suffix.a; for a in *.c.o; do mv $a ${a%%.c.o}.o; done; cd .. + cd pkgtmp1; ar x $archive1; rm $archive1; cd .. + cd pkgtmp2; ar x $archive2; rm $archive2; for a in *.c.o; do mv $a ${a%%.c.o}.o; done; cd .. # Also, remove __.SYMDEF SORTED if present, as it has those funky .c.o names embedded in it. rm -f pkgtmp[12]/__.SYMDEF\ SORTED fi