Skip to content

Commit 917ada3

Browse files
authored
[runtimes] Always define cxx_shared, cxx_static & other targets (#80007)
This patch always defines the cxx_shared, cxx_static & other top-level targets. However, they are marked 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. I purposefully avoided to reformat the files (which now has inconsistent indentation) because I wanted to keep the diff minimal, and I know this is an area of the code where folks may have downstream diffs. I will re-indent the code separately once this patch lands. 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 b94c763 commit 917ada3

File tree

5 files changed

+50
-28
lines changed

5 files changed

+50
-28
lines changed

libcxx/cmake/caches/AIX.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ set(LIBCXX_CXX_ABI libcxxabi CACHE STRING "")
1616
set(LIBUNWIND_ENABLE_SHARED ON CACHE BOOL "")
1717
set(LIBUNWIND_ENABLE_STATIC OFF CACHE BOOL "")
1818
set(LIBCXX_ABI_DEFINES "_LIBCPP_ABI_IOS_ALLOW_ARBITRARY_FILL_VALUE" CACHE STRING "")
19+
20+
# On AIX, both shared and static libraries are archived. As a result, both the static and the shared targets end
21+
# up with a `.a` suffix, which conflict. To workaround that, we set a different output name for the static
22+
# libraries, which we never actually build anyway. For more information, see https://gitlab.kitware.com/cmake/cmake/-/issues/19494.
23+
set(LIBCXX_STATIC_OUTPUT_NAME "c++-static" CACHE STRING "")
24+
set(LIBCXXABI_STATIC_OUTPUT_NAME "c++abi-static" CACHE STRING "")
25+
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: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,6 @@ if (LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS)
143143
)
144144
endif()
145145

146-
if(NOT LIBCXX_INSTALL_LIBRARY)
147-
set(exclude_from_all EXCLUDE_FROM_ALL)
148-
endif()
149-
150146
if (APPLE AND LLVM_USE_SANITIZER)
151147
if (("${LLVM_USE_SANITIZER}" STREQUAL "Address") OR
152148
("${LLVM_USE_SANITIZER}" STREQUAL "Address;Undefined") OR
@@ -177,13 +173,13 @@ split_list(LIBCXX_COMPILE_FLAGS)
177173
split_list(LIBCXX_LINK_FLAGS)
178174

179175
# Build the shared library.
180-
if (LIBCXX_ENABLE_SHARED)
181-
add_library(cxx_shared SHARED ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
176+
add_library(cxx_shared SHARED ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
182177
target_include_directories(cxx_shared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
183178
target_link_libraries(cxx_shared PUBLIC cxx-headers libcxx-libc-shared
184179
PRIVATE ${LIBCXX_LIBRARIES})
185180
set_target_properties(cxx_shared
186181
PROPERTIES
182+
EXCLUDE_FROM_ALL "$<IF:$<BOOL:${LIBCXX_ENABLE_SHARED}>,FALSE,TRUE>"
187183
COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
188184
LINK_FLAGS "${LIBCXX_LINK_FLAGS}"
189185
OUTPUT_NAME "${LIBCXX_SHARED_OUTPUT_NAME}"
@@ -247,7 +243,10 @@ if (LIBCXX_ENABLE_SHARED)
247243
)
248244
endif()
249245

246+
if (LIBCXX_ENABLE_SHARED)
250247
list(APPEND LIBCXX_BUILD_TARGETS "cxx_shared")
248+
endif()
249+
251250
if(WIN32 AND NOT MINGW AND NOT "${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
252251
# Since we most likely do not have a mt.exe replacement, disable the
253252
# manifest bundling. This allows a normal cmake invocation to pass which
@@ -260,19 +259,18 @@ if (LIBCXX_ENABLE_SHARED)
260259
APPEND_STRING PROPERTY LINK_FLAGS " -Xlinker /MANIFEST:NO")
261260
endif()
262261
endif()
263-
endif()
264262

265263
set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
266264

267265
# Build the static library.
268-
if (LIBCXX_ENABLE_STATIC)
269-
add_library(cxx_static STATIC ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
266+
add_library(cxx_static STATIC ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
270267
target_include_directories(cxx_static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
271268
target_link_libraries(cxx_static PUBLIC cxx-headers libcxx-libc-static
272269
PRIVATE ${LIBCXX_LIBRARIES}
273270
PRIVATE libcxx-abi-static)
274271
set_target_properties(cxx_static
275272
PROPERTIES
273+
EXCLUDE_FROM_ALL "$<IF:$<BOOL:${LIBCXX_ENABLE_STATIC}>,FALSE,TRUE>"
276274
COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
277275
LINK_FLAGS "${LIBCXX_LINK_FLAGS}"
278276
OUTPUT_NAME "${LIBCXX_STATIC_OUTPUT_NAME}"
@@ -295,16 +293,18 @@ if (LIBCXX_ENABLE_STATIC)
295293
target_compile_definitions(cxx_static PRIVATE _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS=)
296294
endif()
297295

298-
list(APPEND LIBCXX_BUILD_TARGETS "cxx_static")
296+
if (LIBCXX_ENABLE_STATIC)
297+
list(APPEND LIBCXX_BUILD_TARGETS "cxx_static")
298+
endif()
299299
# Attempt to merge the libc++.a archive and the ABI library archive into one.
300300
if (LIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY)
301301
target_link_libraries(cxx_static PRIVATE libcxx-abi-static-objects)
302302
endif()
303-
endif()
304303

305304
# Add a meta-target for both libraries.
306305
add_custom_target(cxx DEPENDS ${LIBCXX_BUILD_TARGETS})
307306

307+
# Build the experimental static library
308308
set(LIBCXX_EXPERIMENTAL_SOURCES
309309
experimental/keep.cpp
310310
)

libcxxabi/src/CMakeLists.txt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ if (CMAKE_POSITION_INDEPENDENT_CODE OR NOT DEFINED CMAKE_POSITION_INDEPENDENT_CO
184184
endif()
185185
target_compile_options(cxxabi_shared_objects PRIVATE "${LIBCXXABI_ADDITIONAL_COMPILE_FLAGS}")
186186

187-
if (LIBCXXABI_ENABLE_SHARED)
188187
add_library(cxxabi_shared SHARED)
189188
set_target_properties(cxxabi_shared
190189
PROPERTIES
190+
EXCLUDE_FROM_ALL "$<IF:$<BOOL:${LIBCXXABI_ENABLE_SHARED}>,FALSE,TRUE>"
191191
LINK_FLAGS "${LIBCXXABI_LINK_FLAGS}"
192192
OUTPUT_NAME "${LIBCXXABI_SHARED_OUTPUT_NAME}"
193193
SOVERSION "1"
@@ -208,10 +208,12 @@ if (LIBCXXABI_ENABLE_SHARED)
208208
PUBLIC cxxabi_shared_objects
209209
PRIVATE ${LIBCXXABI_LIBRARIES})
210210

211+
if (LIBCXXABI_ENABLE_SHARED)
211212
list(APPEND LIBCXXABI_BUILD_TARGETS "cxxabi_shared")
212-
if (LIBCXXABI_INSTALL_SHARED_LIBRARY)
213-
list(APPEND LIBCXXABI_INSTALL_TARGETS "cxxabi_shared")
214-
endif()
213+
endif()
214+
if (LIBCXXABI_INSTALL_SHARED_LIBRARY)
215+
list(APPEND LIBCXXABI_INSTALL_TARGETS "cxxabi_shared")
216+
endif()
215217

216218
# TODO: Move this to libc++'s HandleLibCXXABI.cmake since this is effectively trying to control
217219
# what libc++ re-exports.
@@ -254,7 +256,6 @@ if (LIBCXXABI_ENABLE_SHARED)
254256
reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/personality-v0.exp")
255257
endif()
256258
endif()
257-
endif()
258259

259260
# Build the static library.
260261
add_library(cxxabi_static_objects OBJECT EXCLUDE_FROM_ALL ${LIBCXXABI_SOURCES} ${LIBCXXABI_HEADERS})
@@ -294,24 +295,25 @@ if(LIBCXXABI_HERMETIC_STATIC_LIBRARY)
294295
_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS=)
295296
endif()
296297

297-
if (LIBCXXABI_ENABLE_STATIC)
298298
add_library(cxxabi_static STATIC)
299299
if (LIBCXXABI_USE_LLVM_UNWINDER AND NOT LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY)
300300
target_link_libraries(cxxabi_static PUBLIC unwind_static)
301301
endif()
302302
set_target_properties(cxxabi_static
303303
PROPERTIES
304+
EXCLUDE_FROM_ALL "$<IF:$<BOOL:${LIBCXXABI_ENABLE_STATIC}>,FALSE,TRUE>"
304305
LINK_FLAGS "${LIBCXXABI_LINK_FLAGS}"
305306
OUTPUT_NAME "${LIBCXXABI_STATIC_OUTPUT_NAME}"
306307
)
307308
target_link_libraries(cxxabi_static
308309
PUBLIC cxxabi_static_objects
309310
PRIVATE ${LIBCXXABI_STATIC_LIBRARIES} ${LIBCXXABI_LIBRARIES})
310311

312+
if (LIBCXXABI_ENABLE_STATIC)
311313
list(APPEND LIBCXXABI_BUILD_TARGETS "cxxabi_static")
312-
if (LIBCXXABI_INSTALL_STATIC_LIBRARY)
313-
list(APPEND LIBCXXABI_INSTALL_TARGETS "cxxabi_static")
314-
endif()
314+
endif()
315+
if (LIBCXXABI_INSTALL_STATIC_LIBRARY)
316+
list(APPEND LIBCXXABI_INSTALL_TARGETS "cxxabi_static")
315317
endif()
316318

317319
# Add a meta-target for both libraries.

libunwind/src/CMakeLists.txt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,23 @@ if (CMAKE_POSITION_INDEPENDENT_CODE OR NOT DEFINED CMAKE_POSITION_INDEPENDENT_CO
153153
set_target_properties(unwind_shared_objects PROPERTIES POSITION_INDEPENDENT_CODE ON) # must set manually because it's an object library
154154
endif()
155155

156-
if (LIBUNWIND_ENABLE_SHARED)
157156
add_library(unwind_shared SHARED)
158157
target_link_libraries(unwind_shared PUBLIC unwind_shared_objects)
159158
set_target_properties(unwind_shared
160159
PROPERTIES
160+
EXCLUDE_FROM_ALL "$<IF:$<BOOL:${LIBUNWIND_ENABLE_SHARED}>,FALSE,TRUE>"
161161
LINK_FLAGS "${LIBUNWIND_LINK_FLAGS}"
162162
LINKER_LANGUAGE C
163163
OUTPUT_NAME "${LIBUNWIND_SHARED_OUTPUT_NAME}"
164164
VERSION "${LIBUNWIND_LIBRARY_VERSION}"
165165
SOVERSION "1"
166166
)
167167

168+
if (LIBUNWIND_ENABLE_SHARED)
168169
list(APPEND LIBUNWIND_BUILD_TARGETS "unwind_shared")
169-
if (LIBUNWIND_INSTALL_SHARED_LIBRARY)
170-
list(APPEND LIBUNWIND_INSTALL_TARGETS "unwind_shared")
171-
endif()
170+
endif()
171+
if (LIBUNWIND_INSTALL_SHARED_LIBRARY)
172+
list(APPEND LIBUNWIND_INSTALL_TARGETS "unwind_shared")
172173
endif()
173174

174175
# Build the static library.
@@ -199,20 +200,21 @@ if(LIBUNWIND_HIDE_SYMBOLS)
199200
target_compile_definitions(unwind_static_objects PRIVATE _LIBUNWIND_HIDE_SYMBOLS)
200201
endif()
201202

202-
if (LIBUNWIND_ENABLE_STATIC)
203203
add_library(unwind_static STATIC)
204204
target_link_libraries(unwind_static PUBLIC unwind_static_objects)
205205
set_target_properties(unwind_static
206206
PROPERTIES
207+
EXCLUDE_FROM_ALL "$<IF:$<BOOL:${LIBUNWIND_ENABLE_STATIC}>,FALSE,TRUE>"
207208
LINK_FLAGS "${LIBUNWIND_LINK_FLAGS}"
208209
LINKER_LANGUAGE C
209210
OUTPUT_NAME "${LIBUNWIND_STATIC_OUTPUT_NAME}"
210211
)
211212

213+
if (LIBUNWIND_ENABLE_STATIC)
212214
list(APPEND LIBUNWIND_BUILD_TARGETS "unwind_static")
213-
if (LIBUNWIND_INSTALL_STATIC_LIBRARY)
214-
list(APPEND LIBUNWIND_INSTALL_TARGETS "unwind_static")
215-
endif()
215+
endif()
216+
if (LIBUNWIND_INSTALL_STATIC_LIBRARY)
217+
list(APPEND LIBUNWIND_INSTALL_TARGETS "unwind_static")
216218
endif()
217219

218220
# Add a meta-target for both libraries.

0 commit comments

Comments
 (0)