Skip to content

Commit 79ee034

Browse files
committed
[runtimes] Always define cxx_shared, cxx_static & other targets
However, mark them as EXCLUDE_FROM_ALL when we don't want to build them. Simply declaring the targets should be of no harm, and it allows other projects to mention these targets regardless of whether they end up being built or not. While the diff may not make that obvious, this patch basically moves the definition of e.g. `cxx_shared` out of the `if (LIBCXX_ENABLE_SHARED)` and instead marks it as `EXCLUDE_FROM_ALL` conditionally on whether LIBCXX_ENABLE_SHARED is passed. It then does the same for libunwind and libc++abi targets. Differential Revision: https://reviews.llvm.org/D134221
1 parent ec2640b commit 79ee034

File tree

4 files changed

+220
-207
lines changed

4 files changed

+220
-207
lines changed

libcxx/cmake/caches/AIX.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@ set(LIBCXX_CXX_ABI libcxxabi CACHE STRING "")
1717
set(LIBCXXABI_USE_LLVM_UNWINDER ON CACHE BOOL "")
1818
set(LIBUNWIND_ENABLE_SHARED ON CACHE BOOL "")
1919
set(LIBUNWIND_ENABLE_STATIC OFF CACHE BOOL "")
20+
21+
# On AIX, both shared and static libraries are archived. As a result, both the static and the shared targets end
22+
# up with a `.a` suffix, which conflict. To workaround that, we set a different output name for the static
23+
# libraries, which we never actually build anyway. For more information, see https://gitlab.kitware.com/cmake/cmake/-/issues/19494.
24+
set(LIBCXX_STATIC_OUTPUT_NAME "c++-static" CACHE STRING "")
25+
set(LIBCXXABI_STATIC_OUTPUT_NAME "c++abi-static" CACHE STRING "")
26+
set(LIBUNWIND_STATIC_OUTPUT_NAME "unwind-static" CACHE STRING "")

libcxx/src/CMakeLists.txt

Lines changed: 116 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,6 @@ if (LIBCXX_CONFIGURE_IDE)
147147
endif()
148148
endif()
149149

150-
if(NOT LIBCXX_INSTALL_LIBRARY)
151-
set(exclude_from_all EXCLUDE_FROM_ALL)
152-
endif()
153-
154150
if (LIBCXX_GENERATE_COVERAGE AND NOT LIBCXX_COVERAGE_LIBRARY)
155151
find_compiler_rt_library(profile LIBCXX_COVERAGE_LIBRARY)
156152
endif()
@@ -195,139 +191,146 @@ endfunction()
195191
split_list(LIBCXX_COMPILE_FLAGS)
196192
split_list(LIBCXX_LINK_FLAGS)
197193

194+
#
198195
# Build the shared library.
199-
if (LIBCXX_ENABLE_SHARED)
200-
add_library(cxx_shared SHARED ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
201-
target_include_directories(cxx_shared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
202-
target_link_libraries(cxx_shared PUBLIC cxx-headers
203-
PRIVATE ${LIBCXX_LIBRARIES})
204-
set_target_properties(cxx_shared
205-
PROPERTIES
206-
COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
207-
LINK_FLAGS "${LIBCXX_LINK_FLAGS}"
208-
OUTPUT_NAME "${LIBCXX_SHARED_OUTPUT_NAME}"
209-
VERSION "${LIBCXX_LIBRARY_VERSION}"
210-
SOVERSION "${LIBCXX_ABI_VERSION}"
211-
DEFINE_SYMBOL ""
196+
#
197+
add_library(cxx_shared SHARED $<$<NOT:$<BOOL:LIBCXX_ENABLE_SHARED>>:EXCLUDE_FROM_ALL> ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
198+
target_include_directories(cxx_shared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
199+
target_link_libraries(cxx_shared PUBLIC cxx-headers
200+
PRIVATE ${LIBCXX_LIBRARIES})
201+
set_target_properties(cxx_shared
202+
PROPERTIES
203+
COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
204+
LINK_FLAGS "${LIBCXX_LINK_FLAGS}"
205+
OUTPUT_NAME "${LIBCXX_SHARED_OUTPUT_NAME}"
206+
VERSION "${LIBCXX_LIBRARY_VERSION}"
207+
SOVERSION "${LIBCXX_ABI_VERSION}"
208+
DEFINE_SYMBOL ""
209+
)
210+
cxx_add_common_build_flags(cxx_shared)
211+
cxx_set_common_defines(cxx_shared)
212+
213+
if(ZOS)
214+
add_custom_command(TARGET cxx_shared POST_BUILD
215+
COMMAND
216+
${LIBCXX_SOURCE_DIR}/utils/zos_rename_dll_side_deck.sh
217+
$<TARGET_LINKER_FILE_NAME:cxx_shared> $<TARGET_FILE_NAME:cxx_shared> "${LIBCXX_DLL_NAME}"
218+
COMMENT "Rename dll name inside the side deck file"
219+
WORKING_DIRECTORY $<TARGET_FILE_DIR:cxx_shared>
212220
)
213-
cxx_add_common_build_flags(cxx_shared)
214-
cxx_set_common_defines(cxx_shared)
215-
216-
if(ZOS)
217-
add_custom_command(TARGET cxx_shared POST_BUILD
218-
COMMAND
219-
${LIBCXX_SOURCE_DIR}/utils/zos_rename_dll_side_deck.sh
220-
$<TARGET_LINKER_FILE_NAME:cxx_shared> $<TARGET_FILE_NAME:cxx_shared> "${LIBCXX_DLL_NAME}"
221-
COMMENT "Rename dll name inside the side deck file"
222-
WORKING_DIRECTORY $<TARGET_FILE_DIR:cxx_shared>
223-
)
224-
endif()
221+
endif()
225222

226-
# Link against libc++abi
227-
if (LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY)
228-
target_link_libraries(cxx_shared PRIVATE libcxx-abi-shared-objects)
229-
else()
230-
target_link_libraries(cxx_shared PUBLIC libcxx-abi-shared)
231-
endif()
223+
# Link against libc++abi
224+
if (LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY)
225+
target_link_libraries(cxx_shared PRIVATE libcxx-abi-shared-objects)
226+
else()
227+
target_link_libraries(cxx_shared PUBLIC libcxx-abi-shared)
228+
endif()
232229

233-
# Maybe re-export symbols from libc++abi
234-
# In particular, we don't re-export the symbols if libc++abi is merged statically
235-
# into libc++ because in that case there's no dylib to re-export from.
236-
if (APPLE AND LIBCXX_CXX_ABI STREQUAL "libcxxabi"
237-
AND NOT DEFINED LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS
238-
AND NOT LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY)
239-
set(LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS ON)
240-
endif()
230+
# Maybe re-export symbols from libc++abi
231+
# In particular, we don't re-export the symbols if libc++abi is merged statically
232+
# into libc++ because in that case there's no dylib to re-export from.
233+
if (APPLE AND LIBCXX_CXX_ABI STREQUAL "libcxxabi"
234+
AND NOT DEFINED LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS
235+
AND NOT LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY)
236+
set(LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS ON)
237+
endif()
241238

242-
if (LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS)
243-
target_link_libraries(cxx_shared PRIVATE
244-
"-Wl,-unexported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/libc++unexp.exp"
245-
"-Wl,-reexported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/libc++abi.exp"
246-
"-Wl,-force_symbols_not_weak_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/notweak.exp"
247-
"-Wl,-force_symbols_weak_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/weak.exp")
239+
if (LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS)
240+
target_link_libraries(cxx_shared PRIVATE
241+
"-Wl,-unexported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/libc++unexp.exp"
242+
"-Wl,-reexported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/libc++abi.exp"
243+
"-Wl,-force_symbols_not_weak_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/notweak.exp"
244+
"-Wl,-force_symbols_weak_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/weak.exp")
248245

249-
target_link_libraries(cxx_shared PRIVATE $<TARGET_NAME_IF_EXISTS:cxxabi-reexports>)
250-
endif()
246+
target_link_libraries(cxx_shared PRIVATE $<TARGET_NAME_IF_EXISTS:cxxabi-reexports>)
247+
endif()
251248

252-
# Generate a linker script in place of a libc++.so symlink.
253-
if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
254-
set(link_libraries)
255-
256-
set(imported_libname "$<TARGET_PROPERTY:libcxx-abi-shared,IMPORTED_LIBNAME>")
257-
set(output_name "$<TARGET_PROPERTY:libcxx-abi-shared,OUTPUT_NAME>")
258-
string(APPEND link_libraries "${CMAKE_LINK_LIBRARY_FLAG}$<IF:$<BOOL:${imported_libname}>,${imported_libname},${output_name}>")
259-
260-
# TODO: Move to the same approach as above for the unwind library
261-
if (LIBCXXABI_USE_LLVM_UNWINDER)
262-
if (LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY)
263-
# libunwind is already included in libc++abi
264-
elseif (TARGET unwind_shared OR HAVE_LIBUNWIND)
265-
string(APPEND link_libraries " ${CMAKE_LINK_LIBRARY_FLAG}$<TARGET_PROPERTY:unwind_shared,OUTPUT_NAME>")
266-
else()
267-
string(APPEND link_libraries " ${CMAKE_LINK_LIBRARY_FLAG}unwind")
268-
endif()
249+
# Generate a linker script in place of a libc++.so symlink.
250+
if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
251+
set(link_libraries)
252+
253+
set(imported_libname "$<TARGET_PROPERTY:libcxx-abi-shared,IMPORTED_LIBNAME>")
254+
set(output_name "$<TARGET_PROPERTY:libcxx-abi-shared,OUTPUT_NAME>")
255+
string(APPEND link_libraries "${CMAKE_LINK_LIBRARY_FLAG}$<IF:$<BOOL:${imported_libname}>,${imported_libname},${output_name}>")
256+
257+
# TODO: Move to the same approach as above for the unwind library
258+
if (LIBCXXABI_USE_LLVM_UNWINDER)
259+
if (LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY)
260+
# libunwind is already included in libc++abi
261+
elseif (TARGET unwind_shared OR HAVE_LIBUNWIND)
262+
string(APPEND link_libraries " ${CMAKE_LINK_LIBRARY_FLAG}$<TARGET_PROPERTY:unwind_shared,OUTPUT_NAME>")
263+
else()
264+
string(APPEND link_libraries " ${CMAKE_LINK_LIBRARY_FLAG}unwind")
269265
endif()
270-
271-
set(linker_script "INPUT($<TARGET_SONAME_FILE_NAME:cxx_shared> ${link_libraries})")
272-
add_custom_command(TARGET cxx_shared POST_BUILD
273-
COMMAND "${CMAKE_COMMAND}" -E remove "$<TARGET_LINKER_FILE:cxx_shared>"
274-
COMMAND "${CMAKE_COMMAND}" -E echo "${linker_script}" > "$<TARGET_LINKER_FILE:cxx_shared>"
275-
COMMENT "Generating linker script: '${linker_script}' as file $<TARGET_LINKER_FILE:cxx_shared>"
276-
VERBATIM
277-
)
278266
endif()
279267

268+
set(linker_script "INPUT($<TARGET_SONAME_FILE_NAME:cxx_shared> ${link_libraries})")
269+
add_custom_command(TARGET cxx_shared POST_BUILD
270+
COMMAND "${CMAKE_COMMAND}" -E remove "$<TARGET_LINKER_FILE:cxx_shared>"
271+
COMMAND "${CMAKE_COMMAND}" -E echo "${linker_script}" > "$<TARGET_LINKER_FILE:cxx_shared>"
272+
COMMENT "Generating linker script: '${linker_script}' as file $<TARGET_LINKER_FILE:cxx_shared>"
273+
VERBATIM
274+
)
275+
endif()
276+
277+
if (LIBCXX_ENABLE_SHARED)
280278
list(APPEND LIBCXX_BUILD_TARGETS "cxx_shared")
281-
if(WIN32 AND NOT MINGW AND NOT "${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
282-
# Since we most likely do not have a mt.exe replacement, disable the
283-
# manifest bundling. This allows a normal cmake invocation to pass which
284-
# will attempt to use the manifest tool to generate the bundled manifest
285-
set_target_properties(cxx_shared PROPERTIES
286-
APPEND_STRING PROPERTY LINK_FLAGS " /MANIFEST:NO")
287-
endif()
279+
endif()
280+
if(WIN32 AND NOT MINGW AND NOT "${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
281+
# Since we most likely do not have a mt.exe replacement, disable the
282+
# manifest bundling. This allows a normal cmake invocation to pass which
283+
# will attempt to use the manifest tool to generate the bundled manifest
284+
set_target_properties(cxx_shared PROPERTIES
285+
APPEND_STRING PROPERTY LINK_FLAGS " /MANIFEST:NO")
288286
endif()
289287

290288
set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
291289

290+
#
292291
# Build the static library.
293-
if (LIBCXX_ENABLE_STATIC)
294-
add_library(cxx_static STATIC ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
295-
target_include_directories(cxx_static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
296-
target_link_libraries(cxx_static PUBLIC cxx-headers
297-
PRIVATE ${LIBCXX_LIBRARIES}
298-
PRIVATE libcxx-abi-static)
299-
set_target_properties(cxx_static
300-
PROPERTIES
301-
COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
302-
LINK_FLAGS "${LIBCXX_LINK_FLAGS}"
303-
OUTPUT_NAME "${LIBCXX_STATIC_OUTPUT_NAME}"
304-
)
305-
cxx_add_common_build_flags(cxx_static)
306-
cxx_set_common_defines(cxx_static)
307-
308-
if (LIBCXX_HERMETIC_STATIC_LIBRARY)
309-
# If the hermetic library doesn't define the operator new/delete functions
310-
# then its code shouldn't declare them with hidden visibility. They might
311-
# actually be provided by a shared library at link time.
312-
if (LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS)
313-
append_flags_if_supported(CXX_STATIC_LIBRARY_FLAGS -fvisibility-global-new-delete-hidden)
314-
endif()
315-
target_compile_options(cxx_static PRIVATE ${CXX_STATIC_LIBRARY_FLAGS})
316-
# _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS can be defined in __config_site
317-
# too. Define it in the same way here, to avoid redefinition conflicts.
318-
target_compile_definitions(cxx_static PRIVATE _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS=)
292+
#
293+
add_library(cxx_static STATIC $<$<NOT:$<BOOL:LIBCXX_ENABLE_STATIC>>:EXCLUDE_FROM_ALL> ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
294+
target_include_directories(cxx_static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
295+
target_link_libraries(cxx_static PUBLIC cxx-headers
296+
PRIVATE ${LIBCXX_LIBRARIES}
297+
PRIVATE libcxx-abi-static)
298+
set_target_properties(cxx_static
299+
PROPERTIES
300+
COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
301+
LINK_FLAGS "${LIBCXX_LINK_FLAGS}"
302+
OUTPUT_NAME "${LIBCXX_STATIC_OUTPUT_NAME}"
303+
)
304+
cxx_add_common_build_flags(cxx_static)
305+
cxx_set_common_defines(cxx_static)
306+
307+
if (LIBCXX_HERMETIC_STATIC_LIBRARY)
308+
# If the hermetic library doesn't define the operator new/delete functions
309+
# then its code shouldn't declare them with hidden visibility. They might
310+
# actually be provided by a shared library at link time.
311+
if (LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS)
312+
append_flags_if_supported(CXX_STATIC_LIBRARY_FLAGS -fvisibility-global-new-delete-hidden)
319313
endif()
314+
target_compile_options(cxx_static PRIVATE ${CXX_STATIC_LIBRARY_FLAGS})
315+
# _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS can be defined in __config_site
316+
# too. Define it in the same way here, to avoid redefinition conflicts.
317+
target_compile_definitions(cxx_static PRIVATE _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS=)
318+
endif()
320319

320+
if (LIBCXX_ENABLE_STATIC)
321321
list(APPEND LIBCXX_BUILD_TARGETS "cxx_static")
322-
# Attempt to merge the libc++.a archive and the ABI library archive into one.
323-
if (LIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY)
324-
target_link_libraries(cxx_static PRIVATE libcxx-abi-static-objects)
325-
endif()
322+
endif()
323+
# Attempt to merge the libc++.a archive and the ABI library archive into one.
324+
if (LIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY)
325+
target_link_libraries(cxx_static PRIVATE libcxx-abi-static-objects)
326326
endif()
327327

328328
# Add a meta-target for both libraries.
329329
add_custom_target(cxx DEPENDS ${LIBCXX_BUILD_TARGETS})
330330

331+
#
332+
# Build the experimental static library
333+
#
331334
set(LIBCXX_EXPERIMENTAL_SOURCES
332335
experimental/memory_resource.cpp
333336
format.cpp

0 commit comments

Comments
 (0)