Skip to content

Commit 01012cc

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. 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. This is a reapplication of 79ee034, which was reverted in a353909 because it broke the TSAN and the Fuchsia builds. Resolves #77654 Differential Revision: https://reviews.llvm.org/D134221
1 parent 1ec5b1f commit 01012cc

File tree

5 files changed

+48
-32
lines changed

5 files changed

+48
-32
lines changed

libcxx/cmake/caches/AIX.cmake

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

libcxx/cmake/caches/Armv7M-picolibc.cmake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,14 @@ set(LIBUNWIND_IS_BAREMETAL ON CACHE BOOL "")
3939
set(LIBUNWIND_REMEMBER_HEAP_ALLOC ON CACHE BOOL "")
4040
set(LIBUNWIND_USE_COMPILER_RT ON CACHE BOOL "")
4141
find_program(QEMU_SYSTEM_ARM qemu-system-arm REQUIRED)
42+
43+
# On embedded platforms that don't support shared library targets, CMake implicitly changes shared
44+
# library targets to be static library targets. This results in duplicate definitions of the static
45+
# library targets even though we might not ever build the shared library target, which breaks the
46+
# build. To work around this, we change the output name of the shared library target so that it
47+
# can't conflict with the static library target.
48+
#
49+
# This is tracked by https://gitlab.kitware.com/cmake/cmake/-/issues/25759.
50+
set(LIBCXX_SHARED_OUTPUT_NAME "c++-shared" CACHE STRING "")
51+
set(LIBCXXABI_SHARED_OUTPUT_NAME "c++abi-shared" CACHE STRING "")
52+
set(LIBUNWIND_SHARED_OUTPUT_NAME "unwind-shared" CACHE STRING "")

libcxx/src/CMakeLists.txt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,6 @@ if (LIBCXX_CONFIGURE_IDE)
155155
endif()
156156
endif()
157157

158-
if(NOT LIBCXX_INSTALL_LIBRARY)
159-
set(exclude_from_all EXCLUDE_FROM_ALL)
160-
endif()
161-
162158
if (LIBCXX_GENERATE_COVERAGE AND NOT LIBCXX_COVERAGE_LIBRARY)
163159
find_compiler_rt_library(profile LIBCXX_COVERAGE_LIBRARY)
164160
endif()
@@ -194,8 +190,7 @@ split_list(LIBCXX_COMPILE_FLAGS)
194190
split_list(LIBCXX_LINK_FLAGS)
195191

196192
# Build the shared library.
197-
if (LIBCXX_ENABLE_SHARED)
198-
add_library(cxx_shared SHARED ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
193+
add_library(cxx_shared SHARED $<$<NOT:$<BOOL:LIBCXX_ENABLE_SHARED>>:EXCLUDE_FROM_ALL> ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
199194
target_include_directories(cxx_shared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
200195
target_link_libraries(cxx_shared PUBLIC cxx-headers
201196
PRIVATE ${LIBCXX_LIBRARIES})
@@ -269,21 +264,22 @@ if (LIBCXX_ENABLE_SHARED)
269264
)
270265
endif()
271266

267+
if (LIBCXX_ENABLE_SHARED)
272268
list(APPEND LIBCXX_BUILD_TARGETS "cxx_shared")
269+
endif()
270+
273271
if(WIN32 AND NOT MINGW AND NOT "${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
274272
# Since we most likely do not have a mt.exe replacement, disable the
275273
# manifest bundling. This allows a normal cmake invocation to pass which
276274
# will attempt to use the manifest tool to generate the bundled manifest
277275
set_target_properties(cxx_shared PROPERTIES
278276
APPEND_STRING PROPERTY LINK_FLAGS " /MANIFEST:NO")
279277
endif()
280-
endif()
281278

282279
set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
283280

284281
# Build the static library.
285-
if (LIBCXX_ENABLE_STATIC)
286-
add_library(cxx_static STATIC ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
282+
add_library(cxx_static STATIC $<$<NOT:$<BOOL:LIBCXX_ENABLE_STATIC>>:EXCLUDE_FROM_ALL> ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
287283
target_include_directories(cxx_static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
288284
target_link_libraries(cxx_static PUBLIC cxx-headers
289285
PRIVATE ${LIBCXX_LIBRARIES}
@@ -309,16 +305,18 @@ if (LIBCXX_ENABLE_STATIC)
309305
target_compile_definitions(cxx_static PRIVATE _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS=)
310306
endif()
311307

312-
list(APPEND LIBCXX_BUILD_TARGETS "cxx_static")
308+
if (LIBCXX_ENABLE_STATIC)
309+
list(APPEND LIBCXX_BUILD_TARGETS "cxx_static")
310+
endif()
313311
# Attempt to merge the libc++.a archive and the ABI library archive into one.
314312
if (LIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY)
315313
target_link_libraries(cxx_static PRIVATE libcxx-abi-static-objects)
316314
endif()
317-
endif()
318315

319316
# Add a meta-target for both libraries.
320317
add_custom_target(cxx DEPENDS ${LIBCXX_BUILD_TARGETS})
321318

319+
# Build the experimental static library
322320
set(LIBCXX_EXPERIMENTAL_SOURCES
323321
experimental/keep.cpp
324322
)

libcxxabi/src/CMakeLists.txt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ if (CMAKE_POSITION_INDEPENDENT_CODE OR NOT DEFINED CMAKE_POSITION_INDEPENDENT_CO
183183
set_target_properties(cxxabi_shared_objects PROPERTIES POSITION_INDEPENDENT_CODE ON) # must set manually because it's an object library
184184
endif()
185185

186-
if (LIBCXXABI_ENABLE_SHARED)
187-
add_library(cxxabi_shared SHARED)
186+
add_library(cxxabi_shared SHARED $<$<NOT:$<BOOL:LIBCXXABI_ENABLE_SHARED>>:EXCLUDE_FROM_ALL>)
188187
set_target_properties(cxxabi_shared
189188
PROPERTIES
190189
LINK_FLAGS "${LIBCXXABI_LINK_FLAGS}"
@@ -207,10 +206,12 @@ if (LIBCXXABI_ENABLE_SHARED)
207206
PUBLIC cxxabi_shared_objects
208207
PRIVATE ${LIBCXXABI_LIBRARIES})
209208

209+
if (LIBCXXABI_ENABLE_SHARED)
210210
list(APPEND LIBCXXABI_BUILD_TARGETS "cxxabi_shared")
211-
if (LIBCXXABI_INSTALL_SHARED_LIBRARY)
212-
list(APPEND LIBCXXABI_INSTALL_TARGETS "cxxabi_shared")
213-
endif()
211+
endif()
212+
if (LIBCXXABI_INSTALL_SHARED_LIBRARY)
213+
list(APPEND LIBCXXABI_INSTALL_TARGETS "cxxabi_shared")
214+
endif()
214215

215216
add_library(cxxabi-reexports INTERFACE)
216217
function(reexport_symbols file)
@@ -243,7 +244,6 @@ if (LIBCXXABI_ENABLE_SHARED)
243244
reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/personality-v0.exp")
244245
endif()
245246
endif()
246-
endif()
247247

248248
# Build the static library.
249249
add_library(cxxabi_static_objects OBJECT EXCLUDE_FROM_ALL ${LIBCXXABI_SOURCES} ${LIBCXXABI_HEADERS})
@@ -279,8 +279,7 @@ if(LIBCXXABI_HERMETIC_STATIC_LIBRARY)
279279
_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS=)
280280
endif()
281281

282-
if (LIBCXXABI_ENABLE_STATIC)
283-
add_library(cxxabi_static STATIC)
282+
add_library(cxxabi_static STATIC $<$<NOT:$<BOOL:LIBCXXABI_ENABLE_STATIC>>:EXCLUDE_FROM_ALL>)
284283
if (LIBCXXABI_USE_LLVM_UNWINDER AND NOT LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY)
285284
target_link_libraries(cxxabi_static PUBLIC unwind_static)
286285
endif()
@@ -293,10 +292,11 @@ if (LIBCXXABI_ENABLE_STATIC)
293292
PUBLIC cxxabi_static_objects
294293
PRIVATE ${LIBCXXABI_STATIC_LIBRARIES} ${LIBCXXABI_LIBRARIES})
295294

295+
if (LIBCXXABI_ENABLE_STATIC)
296296
list(APPEND LIBCXXABI_BUILD_TARGETS "cxxabi_static")
297-
if (LIBCXXABI_INSTALL_STATIC_LIBRARY)
298-
list(APPEND LIBCXXABI_INSTALL_TARGETS "cxxabi_static")
299-
endif()
297+
endif()
298+
if (LIBCXXABI_INSTALL_STATIC_LIBRARY)
299+
list(APPEND LIBCXXABI_INSTALL_TARGETS "cxxabi_static")
300300
endif()
301301

302302
# Add a meta-target for both libraries.

libunwind/src/CMakeLists.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ if (CMAKE_POSITION_INDEPENDENT_CODE OR NOT DEFINED CMAKE_POSITION_INDEPENDENT_CO
162162
set_target_properties(unwind_shared_objects PROPERTIES POSITION_INDEPENDENT_CODE ON) # must set manually because it's an object library
163163
endif()
164164

165-
if (LIBUNWIND_ENABLE_SHARED)
166-
add_library(unwind_shared SHARED)
165+
add_library(unwind_shared SHARED $<$<NOT:$<BOOL:LIBUNWIND_ENABLE_SHARED>>:EXCLUDE_FROM_ALL>)
167166
target_link_libraries(unwind_shared PUBLIC unwind_shared_objects)
168167
set_target_properties(unwind_shared
169168
PROPERTIES
@@ -174,10 +173,11 @@ if (LIBUNWIND_ENABLE_SHARED)
174173
SOVERSION "1"
175174
)
176175

176+
if (LIBUNWIND_ENABLE_SHARED)
177177
list(APPEND LIBUNWIND_BUILD_TARGETS "unwind_shared")
178-
if (LIBUNWIND_INSTALL_SHARED_LIBRARY)
179-
list(APPEND LIBUNWIND_INSTALL_TARGETS "unwind_shared")
180-
endif()
178+
endif()
179+
if (LIBUNWIND_INSTALL_SHARED_LIBRARY)
180+
list(APPEND LIBUNWIND_INSTALL_TARGETS "unwind_shared")
181181
endif()
182182

183183
# Build the static library.
@@ -205,8 +205,7 @@ if(LIBUNWIND_HIDE_SYMBOLS)
205205
target_compile_definitions(unwind_static_objects PRIVATE _LIBUNWIND_HIDE_SYMBOLS)
206206
endif()
207207

208-
if (LIBUNWIND_ENABLE_STATIC)
209-
add_library(unwind_static STATIC)
208+
add_library(unwind_static STATIC $<$<NOT:$<BOOL:LIBUNWIND_ENABLE_STATIC>>:EXCLUDE_FROM_ALL>)
210209
target_link_libraries(unwind_static PUBLIC unwind_static_objects)
211210
set_target_properties(unwind_static
212211
PROPERTIES
@@ -215,10 +214,11 @@ if (LIBUNWIND_ENABLE_STATIC)
215214
OUTPUT_NAME "${LIBUNWIND_STATIC_OUTPUT_NAME}"
216215
)
217216

217+
if (LIBUNWIND_ENABLE_STATIC)
218218
list(APPEND LIBUNWIND_BUILD_TARGETS "unwind_static")
219-
if (LIBUNWIND_INSTALL_STATIC_LIBRARY)
220-
list(APPEND LIBUNWIND_INSTALL_TARGETS "unwind_static")
221-
endif()
219+
endif()
220+
if (LIBUNWIND_INSTALL_STATIC_LIBRARY)
221+
list(APPEND LIBUNWIND_INSTALL_TARGETS "unwind_static")
222222
endif()
223223

224224
# Add a meta-target for both libraries.

0 commit comments

Comments
 (0)