Skip to content

Commit 1cd384f

Browse files
ldionnenikic
authored andcommitted
[libc++] Fix broken configuration system-libcxxabi on Apple (llvm#110920)
On Apple platforms, using system-libcxxabi as an ABI library wouldn't work because we'd try to re-export symbols from libc++abi that the system libc++abi.dylib might not have. Instead, only re-export those symbols when we're using the in-tree libc++abi. This does mean that libc++.dylib won't re-export any libc++abi symbols when building against the system libc++abi, which could be fixed in various ways. However, the best solution really depends on the intended use case, so this patch doesn't try to solve that problem. As a drive-by, also improve the diagnostic message when the user forgets to set the LIBCXX_CXX_ABI_INCLUDE_PATHS variable, which would previously lead to a confusing error. Closes llvm#104672 (cherry picked from commit 21da4e7)
1 parent 1112ac1 commit 1cd384f

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

libcxx/cmake/Modules/HandleLibCXXABI.cmake

+24-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ endfunction()
8383

8484
# Link against a system-provided libstdc++
8585
if ("${LIBCXX_CXX_ABI}" STREQUAL "libstdc++")
86+
if(NOT LIBCXX_CXX_ABI_INCLUDE_PATHS)
87+
message(FATAL_ERROR "LIBCXX_CXX_ABI_INCLUDE_PATHS must be set when selecting libstdc++ as an ABI library")
88+
endif()
89+
8690
add_library(libcxx-abi-headers INTERFACE)
8791
import_private_headers(libcxx-abi-headers "${LIBCXX_CXX_ABI_INCLUDE_PATHS}"
8892
"cxxabi.h;bits/c++config.h;bits/os_defines.h;bits/cpu_defines.h;bits/cxxabi_tweaks.h;bits/cxxabi_forced.h")
@@ -96,6 +100,10 @@ if ("${LIBCXX_CXX_ABI}" STREQUAL "libstdc++")
96100

97101
# Link against a system-provided libsupc++
98102
elseif ("${LIBCXX_CXX_ABI}" STREQUAL "libsupc++")
103+
if(NOT LIBCXX_CXX_ABI_INCLUDE_PATHS)
104+
message(FATAL_ERROR "LIBCXX_CXX_ABI_INCLUDE_PATHS must be set when selecting libsupc++ as an ABI library")
105+
endif()
106+
99107
add_library(libcxx-abi-headers INTERFACE)
100108
import_private_headers(libcxx-abi-headers "${LIBCXX_CXX_ABI_INCLUDE_PATHS}"
101109
"cxxabi.h;bits/c++config.h;bits/os_defines.h;bits/cpu_defines.h;bits/cxxabi_tweaks.h;bits/cxxabi_forced.h")
@@ -114,7 +122,18 @@ elseif ("${LIBCXX_CXX_ABI}" STREQUAL "libcxxabi")
114122
target_compile_definitions(libcxx-abi-headers INTERFACE "-DLIBCXX_BUILDING_LIBCXXABI")
115123

116124
if (TARGET cxxabi_shared)
117-
add_library(libcxx-abi-shared ALIAS cxxabi_shared)
125+
add_library(libcxx-abi-shared INTERFACE)
126+
target_link_libraries(libcxx-abi-shared INTERFACE cxxabi_shared)
127+
128+
# When using the in-tree libc++abi as an ABI library, libc++ re-exports the
129+
# libc++abi symbols (on platforms where it can) because libc++abi is only an
130+
# implementation detail of libc++.
131+
target_link_libraries(libcxx-abi-shared INTERFACE cxxabi-reexports)
132+
133+
# Populate the OUTPUT_NAME property of libcxx-abi-shared because that is used when
134+
# generating a linker script.
135+
get_target_property(_output_name cxxabi_shared OUTPUT_NAME)
136+
set_target_properties(libcxx-abi-shared PROPERTIES "OUTPUT_NAME" "${_output_name}")
118137
endif()
119138

120139
if (TARGET cxxabi_static)
@@ -131,6 +150,10 @@ elseif ("${LIBCXX_CXX_ABI}" STREQUAL "libcxxabi")
131150

132151
# Link against a system-provided libc++abi
133152
elseif ("${LIBCXX_CXX_ABI}" STREQUAL "system-libcxxabi")
153+
if(NOT LIBCXX_CXX_ABI_INCLUDE_PATHS)
154+
message(FATAL_ERROR "LIBCXX_CXX_ABI_INCLUDE_PATHS must be set when selecting system-libcxxabi as an ABI library")
155+
endif()
156+
134157
add_library(libcxx-abi-headers INTERFACE)
135158
import_private_headers(libcxx-abi-headers "${LIBCXX_CXX_ABI_INCLUDE_PATHS}" "cxxabi.h;__cxxabi_config.h")
136159
target_compile_definitions(libcxx-abi-headers INTERFACE "-DLIBCXX_BUILDING_LIBCXXABI")

libcxx/src/CMakeLists.txt

+2-6
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,10 @@ if (LIBCXX_ENABLE_SHARED)
229229
target_link_libraries(cxx_shared PUBLIC libcxx-abi-shared)
230230
endif()
231231

232-
# Maybe re-export symbols from libc++abi
233-
# In particular, we don't re-export the symbols if libc++abi is merged statically
234-
# into libc++ because in that case there's no dylib to re-export from.
232+
# Maybe force some symbols to be weak, not weak or not exported.
233+
# TODO: This shouldn't depend on the platform, and ideally it should be done in the sources.
235234
if (APPLE AND LIBCXX_CXX_ABI MATCHES "libcxxabi$"
236235
AND NOT LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY)
237-
target_link_libraries(cxx_shared PRIVATE cxxabi-reexports)
238-
239-
# TODO: These exports controls should not be tied to whether we re-export libc++abi symbols
240236
target_link_libraries(cxx_shared PRIVATE
241237
"-Wl,-unexported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/libc++unexp.exp"
242238
"-Wl,-force_symbols_not_weak_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/notweak.exp"

libcxxabi/src/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ if (LIBCXXABI_ENABLE_SHARED)
213213
list(APPEND LIBCXXABI_INSTALL_TARGETS "cxxabi_shared")
214214
endif()
215215

216+
# TODO: Move this to libc++'s HandleLibCXXABI.cmake since this is effectively trying to control
217+
# what libc++ re-exports.
216218
add_library(cxxabi-reexports INTERFACE)
217219
function(export_symbols file)
218220
# -exported_symbols_list is only available on Apple platforms

0 commit comments

Comments
 (0)