CMake: Fix building libraries on macOS

When building libraries on macOS they'd be missing the SONAME
attribute which causes the linker to embed relative paths into
other libraries and executables:

Dynamic section at offset 0x52794 contains 28 entries:
 Type     Name/Value
(NEEDED) Shared library: [libgcc_s.so]
(NEEDED) Shared library: [Userland/Libraries/LibCrypt/libcrypt.so]
(NEEDED) Shared library: [Userland/Libraries/LibCrypto/libcrypto.so]
(NEEDED) Shared library: [Userland/Libraries/LibC/libc.so]
(NEEDED) Shared library: [libsystem.so]
(NEEDED) Shared library: [libm.so]
(NEEDED) Shared library: [libc.so]

The dynamic linker then fails to load those libraries which makes
the system unbootable.
This commit is contained in:
Gunnar Beutner 2021-04-29 22:58:05 +02:00 committed by Linus Groh
parent 55446172cb
commit 8ae0794584
Notes: sideshowbarker 2024-07-18 18:53:07 +09:00

View file

@ -140,9 +140,10 @@ foreach(lang ASM C CXX OBJC OBJCXX)
unset(CMAKE_${lang}_OSX_DEPLOYMENT_TARGET_FLAG) unset(CMAKE_${lang}_OSX_DEPLOYMENT_TARGET_FLAG)
unset(CMAKE_${lang}_SYSROOT_FLAG) unset(CMAKE_${lang}_SYSROOT_FLAG)
if (CMAKE_SYSTEM_NAME MATCHES Darwin) if (CMAKE_SYSTEM_NAME MATCHES Darwin)
## MacOS Workaround. Don't generate install_name flag when cross compiling ## macOS workaround. Use GNU ld flags for SONAMEs.
set(CMAKE_${lang}_CREATE_SHARED_LIBRARY set(CMAKE_${lang}_CREATE_SHARED_LIBRARY
"<CMAKE_${lang}_COMPILER> <LANGUAGE_COMPILE_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS> <LINK_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>") "<CMAKE_${lang}_COMPILER> <LANGUAGE_COMPILE_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_${lang}_FLAGS> <LINK_FLAGS> <SONAME_FLAG><TARGET_SONAME> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
set(CMAKE_SHARED_LIBRARY_SONAME_${lang}_FLAG "-Wl,-soname,")
endif() endif()
endforeach() endforeach()