[libcxx][libcxxabi] Fix build for OpenBSD (#92186)
[llvm-project.git] / libcxx / src / CMakeLists.txt
blob1b80625304a4122edf607ee4ae9c7b320b8d5f52
1 set(LIBCXX_LIB_CMAKEFILES_DIR "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}"  PARENT_SCOPE)
3 # Get sources
4 set(LIBCXX_SOURCES
5   algorithm.cpp
6   any.cpp
7   bind.cpp
8   call_once.cpp
9   charconv.cpp
10   chrono.cpp
11   error_category.cpp
12   exception.cpp
13   filesystem/filesystem_clock.cpp
14   filesystem/filesystem_error.cpp
15   filesystem/path_parser.h
16   filesystem/path.cpp
17   functional.cpp
18   hash.cpp
19   include/apple_availability.h
20   include/atomic_support.h
21   include/config_elast.h
22   include/refstring.h
23   include/ryu/common.h
24   include/ryu/d2fixed.h
25   include/ryu/d2fixed_full_table.h
26   include/ryu/d2s.h
27   include/ryu/d2s_full_table.h
28   include/ryu/d2s_intrinsics.h
29   include/ryu/digit_table.h
30   include/ryu/f2s.h
31   include/ryu/ryu.h
32   include/to_chars_floating_point.h
33   legacy_pointer_safety.cpp
34   memory.cpp
35   memory_resource.cpp
36   new_handler.cpp
37   new_helpers.cpp
38   optional.cpp
39   print.cpp
40   random_shuffle.cpp
41   ryu/d2fixed.cpp
42   ryu/d2s.cpp
43   ryu/f2s.cpp
44   stdexcept.cpp
45   string.cpp
46   support/runtime/exception_fallback.ipp
47   support/runtime/exception_glibcxx.ipp
48   support/runtime/exception_libcxxabi.ipp
49   support/runtime/exception_libcxxrt.ipp
50   support/runtime/exception_msvc.ipp
51   support/runtime/exception_pointer_cxxabi.ipp
52   support/runtime/exception_pointer_glibcxx.ipp
53   support/runtime/exception_pointer_msvc.ipp
54   support/runtime/exception_pointer_unimplemented.ipp
55   support/runtime/stdexcept_default.ipp
56   support/runtime/stdexcept_vcruntime.ipp
57   system_error.cpp
58   typeinfo.cpp
59   valarray.cpp
60   variant.cpp
61   vector.cpp
62   verbose_abort.cpp
63   )
65 if (LIBCXX_ENABLE_THREADS)
66   list(APPEND LIBCXX_SOURCES
67     atomic.cpp
68     barrier.cpp
69     condition_variable_destructor.cpp
70     condition_variable.cpp
71     future.cpp
72     mutex_destructor.cpp
73     mutex.cpp
74     shared_mutex.cpp
75     thread.cpp
76     )
77 endif()
79 if (LIBCXX_ENABLE_RANDOM_DEVICE)
80   list(APPEND LIBCXX_SOURCES
81     random.cpp
82     )
83 endif()
85 if (LIBCXX_ENABLE_LOCALIZATION)
86   list(APPEND LIBCXX_SOURCES
87     fstream.cpp
88     include/sso_allocator.h
89     ios.cpp
90     ios.instantiations.cpp
91     iostream.cpp
92     locale.cpp
93     ostream.cpp
94     regex.cpp
95     strstream.cpp
96     )
97 endif()
99 if(WIN32)
100   list(APPEND LIBCXX_SOURCES
101     support/win32/locale_win32.cpp
102     support/win32/support.cpp
103     )
105   if (NOT LIBCXX_HAS_PTHREAD_API)
106     list(APPEND LIBCXX_SOURCES
107       support/win32/thread_win32.cpp
108       )
109   endif()
110 elseif(ZOS)
111   list(APPEND LIBCXX_SOURCES
112     support/ibm/mbsnrtowcs.cpp
113     support/ibm/wcsnrtombs.cpp
114     support/ibm/xlocale_zos.cpp
115     )
116 endif()
118 if (LIBCXX_ENABLE_FILESYSTEM)
119   list(APPEND LIBCXX_SOURCES
120     filesystem/directory_entry.cpp
121     filesystem/directory_iterator.cpp
122     filesystem/file_descriptor.h
123     filesystem/operations.cpp
124     filesystem/posix_compat.h
125     filesystem/time_utils.h
126     )
127   # Filesystem uses __int128_t, which requires a definition of __muloi4 when
128   # compiled with UBSAN. This definition is not provided by libgcc_s, but is
129   # provided by compiler-rt. So we need to disable it to avoid having multiple
130   # definitions. See filesystem/int128_builtins.cpp.
131   if (NOT LIBCXX_USE_COMPILER_RT)
132     list(APPEND LIBCXX_SOURCES
133       filesystem/int128_builtins.cpp
134       )
135   endif()
136 endif()
138 if (LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS)
139   list(APPEND LIBCXX_SOURCES
140     new.cpp
141     )
142 endif()
144 # Add all the headers to the project for IDEs.
145 if (LIBCXX_CONFIGURE_IDE)
146   file(GLOB_RECURSE LIBCXX_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../include/*)
147   if(WIN32)
148     file( GLOB LIBCXX_WIN32_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../include/__support/win32/*.h)
149     list(APPEND LIBCXX_HEADERS ${LIBCXX_WIN32_HEADERS})
150   endif()
151   # Force them all into the headers dir on MSVC, otherwise they end up at
152   # project scope because they don't have extensions.
153   if (MSVC_IDE)
154     source_group("Header Files" FILES ${LIBCXX_HEADERS})
155   endif()
156 endif()
158 if(NOT LIBCXX_INSTALL_LIBRARY)
159   set(exclude_from_all EXCLUDE_FROM_ALL)
160 endif()
162 if (LIBCXX_GENERATE_COVERAGE AND NOT LIBCXX_COVERAGE_LIBRARY)
163   find_compiler_rt_library(profile LIBCXX_COVERAGE_LIBRARY)
164 endif()
165 add_library_flags_if(LIBCXX_COVERAGE_LIBRARY "${LIBCXX_COVERAGE_LIBRARY}")
167 if (APPLE AND LLVM_USE_SANITIZER)
168   if (("${LLVM_USE_SANITIZER}" STREQUAL "Address") OR
169       ("${LLVM_USE_SANITIZER}" STREQUAL "Address;Undefined") OR
170       ("${LLVM_USE_SANITIZER}" STREQUAL "Undefined;Address"))
171     set(LIBFILE "libclang_rt.asan_osx_dynamic.dylib")
172   elseif("${LLVM_USE_SANITIZER}" STREQUAL "Undefined")
173     set(LIBFILE "libclang_rt.ubsan_osx_dynamic.dylib")
174   elseif("${LLVM_USE_SANITIZER}" STREQUAL "Thread")
175     set(LIBFILE "libclang_rt.tsan_osx_dynamic.dylib")
176   else()
177     message(WARNING "LLVM_USE_SANITIZER=${LLVM_USE_SANITIZER} is not supported on OS X")
178   endif()
179   if (LIBFILE)
180     find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY)
181     get_filename_component(LIBDIR "${LIBCXX_BUILTINS_LIBRARY}" DIRECTORY)
182     if (NOT IS_DIRECTORY "${LIBDIR}")
183       message(FATAL_ERROR "Cannot find compiler-rt directory on OS X required for LLVM_USE_SANITIZER")
184     endif()
185     set(LIBCXX_SANITIZER_LIBRARY "${LIBDIR}/${LIBFILE}")
186     set(LIBCXX_SANITIZER_LIBRARY "${LIBCXX_SANITIZER_LIBRARY}" PARENT_SCOPE)
187     message(STATUS "Manually linking compiler-rt library: ${LIBCXX_SANITIZER_LIBRARY}")
188     add_library_flags("${LIBCXX_SANITIZER_LIBRARY}")
189     add_link_flags("-Wl,-rpath,${LIBDIR}")
190   endif()
191 endif()
193 split_list(LIBCXX_COMPILE_FLAGS)
194 split_list(LIBCXX_LINK_FLAGS)
196 # Build the shared library.
197 if (LIBCXX_ENABLE_SHARED)
198   add_library(cxx_shared SHARED ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
199   target_include_directories(cxx_shared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
200   target_link_libraries(cxx_shared PUBLIC cxx-headers
201                                    PRIVATE ${LIBCXX_LIBRARIES})
202   set_target_properties(cxx_shared
203     PROPERTIES
204       COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
205       LINK_FLAGS    "${LIBCXX_LINK_FLAGS}"
206       OUTPUT_NAME   "${LIBCXX_SHARED_OUTPUT_NAME}"
207       VERSION       "${LIBCXX_LIBRARY_VERSION}"
208       SOVERSION     "${LIBCXX_ABI_VERSION}"
209       DEFINE_SYMBOL ""
210   )
211   cxx_add_common_build_flags(cxx_shared)
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>
220     )
221   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()
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 MATCHES "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()
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")
246     target_link_libraries(cxx_shared PRIVATE $<TARGET_NAME_IF_EXISTS:cxxabi-reexports>)
247   endif()
249   # Generate a linker script in place of a libc++.so symlink.
250   if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
251     set(link_libraries)
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}>")
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")
265       endif()
266     endif()
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()
277   list(APPEND LIBCXX_BUILD_TARGETS "cxx_shared")
278   if(WIN32 AND NOT MINGW AND NOT "${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
279     # Since we most likely do not have a mt.exe replacement, disable the
280     # manifest bundling.  This allows a normal cmake invocation to pass which
281     # will attempt to use the manifest tool to generate the bundled manifest
282     set_target_properties(cxx_shared PROPERTIES
283                           APPEND_STRING PROPERTY LINK_FLAGS " /MANIFEST:NO")
284   endif()
285 endif()
287 set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
289 # Build the static library.
290 if (LIBCXX_ENABLE_STATIC)
291   add_library(cxx_static STATIC ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
292   target_include_directories(cxx_static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
293   target_link_libraries(cxx_static PUBLIC cxx-headers
294                                    PRIVATE ${LIBCXX_LIBRARIES}
295                                    PRIVATE libcxx-abi-static)
296   set_target_properties(cxx_static
297     PROPERTIES
298       COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
299       LINK_FLAGS    "${LIBCXX_LINK_FLAGS}"
300       OUTPUT_NAME   "${LIBCXX_STATIC_OUTPUT_NAME}"
301   )
302   cxx_add_common_build_flags(cxx_static)
304   if (LIBCXX_HERMETIC_STATIC_LIBRARY)
305     # If the hermetic library doesn't define the operator new/delete functions
306     # then its code shouldn't declare them with hidden visibility.  They might
307     # actually be provided by a shared library at link time.
308     if (LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS)
309       append_flags_if_supported(CXX_STATIC_LIBRARY_FLAGS -fvisibility-global-new-delete=force-hidden)
310       if (NOT CXX_SUPPORTS_FVISIBILITY_GLOBAL_NEW_DELETE_EQ_FORCE_HIDDEN_FLAG)
311         append_flags_if_supported(CXX_STATIC_LIBRARY_FLAGS -fvisibility-global-new-delete-hidden)
312       endif()
313     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()
320   list(APPEND LIBCXX_BUILD_TARGETS "cxx_static")
321   # Attempt to merge the libc++.a archive and the ABI library archive into one.
322   if (LIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY)
323     target_link_libraries(cxx_static PRIVATE libcxx-abi-static-objects)
324   endif()
325 endif()
327 # Add a meta-target for both libraries.
328 add_custom_target(cxx DEPENDS ${LIBCXX_BUILD_TARGETS})
330 set(LIBCXX_EXPERIMENTAL_SOURCES
331   experimental/keep.cpp
332   )
334 if (LIBCXX_PSTL_CPU_BACKEND STREQUAL "libdispatch")
335   list(APPEND LIBCXX_EXPERIMENTAL_SOURCES
336     pstl/libdispatch.cpp
337     )
338 endif()
340 if (LIBCXX_ENABLE_LOCALIZATION AND LIBCXX_ENABLE_FILESYSTEM AND LIBCXX_ENABLE_TIME_ZONE_DATABASE)
341   list(APPEND LIBCXX_EXPERIMENTAL_SOURCES
342     tz.cpp
343     tzdb_list.cpp
344     )
345 endif()
347 add_library(cxx_experimental STATIC ${LIBCXX_EXPERIMENTAL_SOURCES})
348 target_link_libraries(cxx_experimental PUBLIC cxx-headers)
349 if (LIBCXX_ENABLE_SHARED)
350   target_link_libraries(cxx_experimental PRIVATE cxx_shared)
351 else()
352   target_link_libraries(cxx_experimental PRIVATE cxx_static)
353 endif()
355 if (LIBCXX_HERMETIC_STATIC_LIBRARY)
356   # _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS can be defined in __config_site
357   # too. Define it in the same way here, to avoid redefinition conflicts.
358   target_compile_definitions(cxx_experimental PRIVATE _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS=)
359 endif()
361 set_target_properties(cxx_experimental
362   PROPERTIES
363     COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
364     OUTPUT_NAME   "c++experimental"
366 cxx_add_common_build_flags(cxx_experimental)
367 target_compile_options(cxx_experimental PUBLIC -D_LIBCPP_ENABLE_EXPERIMENTAL)
369 if (LIBCXX_INSTALL_SHARED_LIBRARY)
370   install(TARGETS cxx_shared
371     ARCHIVE DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
372     LIBRARY DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
373     RUNTIME DESTINATION ${LIBCXX_INSTALL_RUNTIME_DIR} COMPONENT cxx)
374 endif()
376 if (LIBCXX_INSTALL_STATIC_LIBRARY)
377   install(TARGETS cxx_static
378     ARCHIVE DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
379     LIBRARY DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
380     RUNTIME DESTINATION ${LIBCXX_INSTALL_RUNTIME_DIR} COMPONENT cxx)
381 endif()
383 if (LIBCXX_INSTALL_LIBRARY)
384   install(TARGETS cxx_experimental
385     LIBRARY DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
386     ARCHIVE DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
387     RUNTIME DESTINATION ${LIBCXX_INSTALL_RUNTIME_DIR} COMPONENT cxx)
388 endif()
390 # NOTE: This install command must go after the cxx install command otherwise
391 # it will not be executed after the library symlinks are installed.
392 if (LIBCXX_ENABLE_SHARED AND LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
393   install(FILES "$<TARGET_LINKER_FILE:cxx_shared>"
394     DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR}
395     COMPONENT libcxx)
396 endif()
398 if (NOT CMAKE_CONFIGURATION_TYPES)
399     if(LIBCXX_INSTALL_LIBRARY)
400       set(lib_install_target "cxx;cxx_experimental")
401     endif()
402     if(LIBCXX_INSTALL_HEADERS)
403       set(header_install_target install-cxx-headers)
404     endif()
405     if(LIBCXX_INSTALL_MODULES)
406       set(module_install_target install-cxx-modules)
407     endif()
408     add_custom_target(install-cxx
409                       DEPENDS ${lib_install_target}
410                               cxx_experimental
411                               ${header_install_target}
412                               ${module_install_target}
413                       COMMAND "${CMAKE_COMMAND}"
414                       -DCMAKE_INSTALL_COMPONENT=cxx
415                       -P "${LIBCXX_BINARY_DIR}/cmake_install.cmake")
416     add_custom_target(install-cxx-stripped
417                       DEPENDS ${lib_install_target}
418                               cxx_experimental
419                               ${header_install_target}
420                               ${module_install_target}
421                       COMMAND "${CMAKE_COMMAND}"
422                       -DCMAKE_INSTALL_COMPONENT=cxx
423                       -DCMAKE_INSTALL_DO_STRIP=1
424                       -P "${LIBCXX_BINARY_DIR}/cmake_install.cmake")
425 endif()