Skip to content

Commit

Permalink
Allow re-enabling @rpath on OS/X when using relative install prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
mtl1979 authored and Dead2 committed Jun 28, 2023
1 parent 1b5908b commit eace956
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
23 changes: 20 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
7 changes: 6 additions & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 11 additions & 8 deletions test/pkgcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=""
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit eace956

Please sign in to comment.