Skip to content

Commit b7fb856

Browse files
committed
[libc++] Simplify how we define the linker script for libc++
Trying to be generic didn't work properly because we had to special-case some interface libraries that we didn't want in the linker script. Instead, only look at the ABI and the unwinding libraries explicitly. This should solve the issue reported by @dim in [1]. [1]: https://discourse.llvm.org/t/15-0-0-rc1-has-been-tagged/64174/22 Differential Revision: https://reviews.llvm.org/D131037
1 parent 44b4f4d commit b7fb856

File tree

2 files changed

+24
-72
lines changed

2 files changed

+24
-72
lines changed

libcxx/cmake/Modules/DefineLinkerScript.cmake

Lines changed: 0 additions & 56 deletions
This file was deleted.

libcxx/src/CMakeLists.txt

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -212,20 +212,6 @@ if (LIBCXX_ENABLE_SHARED)
212212
cxx_add_common_build_flags(cxx_shared)
213213
cxx_set_common_defines(cxx_shared)
214214

215-
# Link against LLVM libunwind
216-
# Note that we do need to link against libunwind directly to ensure that the correct
217-
# dependencies are recorded when creating a linker script.
218-
# TODO: Look into modifying the linker script creation to recursively consider interface libraries
219-
if (LIBCXXABI_USE_LLVM_UNWINDER)
220-
if (LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY)
221-
# libunwind is already included in libc++abi
222-
elseif (TARGET unwind_shared OR HAVE_LIBUNWIND)
223-
target_link_libraries(cxx_shared PUBLIC unwind_shared)
224-
else()
225-
target_link_libraries(cxx_shared PUBLIC unwind)
226-
endif()
227-
endif()
228-
229215
# Link against libc++abi
230216
if (LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY)
231217
target_link_libraries(cxx_shared PRIVATE libcxx-abi-shared-objects)
@@ -254,8 +240,30 @@ if (LIBCXX_ENABLE_SHARED)
254240

255241
# Generate a linker script in place of a libc++.so symlink.
256242
if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
257-
include(DefineLinkerScript)
258-
define_linker_script(cxx_shared)
243+
set(link_libraries)
244+
245+
set(imported_libname "$<TARGET_PROPERTY:libcxx-abi-shared,IMPORTED_LIBNAME>")
246+
set(output_name "$<TARGET_PROPERTY:libcxx-abi-shared,OUTPUT_NAME>")
247+
string(APPEND link_libraries "${CMAKE_LINK_LIBRARY_FLAG}$<IF:$<BOOL:${imported_libname}>,${imported_libname},${output_name}>")
248+
249+
# TODO: Move to the same approach as above for the unwind library
250+
if (LIBCXXABI_USE_LLVM_UNWINDER)
251+
if (LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY)
252+
# libunwind is already included in libc++abi
253+
elseif (TARGET unwind_shared OR HAVE_LIBUNWIND)
254+
string(APPEND link_libraries " ${CMAKE_LINK_LIBRARY_FLAG}$<TARGET_PROPERTY:unwind_shared,OUTPUT_NAME>")
255+
else()
256+
string(APPEND link_libraries " ${CMAKE_LINK_LIBRARY_FLAG}unwind")
257+
endif()
258+
endif()
259+
260+
set(linker_script "INPUT($<TARGET_SONAME_FILE_NAME:cxx_shared> ${link_libraries})")
261+
add_custom_command(TARGET cxx_shared POST_BUILD
262+
COMMAND "${CMAKE_COMMAND}" -E remove "$<TARGET_LINKER_FILE:cxx_shared>"
263+
COMMAND "${CMAKE_COMMAND}" -E echo "${linker_script}" > "$<TARGET_LINKER_FILE:cxx_shared>"
264+
COMMENT "Generating linker script: '${linker_script}' as file $<TARGET_LINKER_FILE:cxx_shared>"
265+
VERBATIM
266+
)
259267
endif()
260268

261269
list(APPEND LIBCXX_BUILD_TARGETS "cxx_shared")

0 commit comments

Comments
 (0)