Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / src / CMakeLists.txt
blobe57fbf1468acb2b3c679eb4181d36b9430c22e3a
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   atomic.cpp
8   barrier.cpp
9   bind.cpp
10   charconv.cpp
11   chrono.cpp
12   condition_variable.cpp
13   condition_variable_destructor.cpp
14   error_category.cpp
15   exception.cpp
16   filesystem/filesystem_clock.cpp
17   filesystem/filesystem_error.cpp
18   filesystem/path_parser.h
19   filesystem/path.cpp
20   functional.cpp
21   future.cpp
22   hash.cpp
23   include/apple_availability.h
24   include/atomic_support.h
25   include/config_elast.h
26   include/refstring.h
27   include/ryu/common.h
28   include/ryu/d2fixed.h
29   include/ryu/d2fixed_full_table.h
30   include/ryu/d2s.h
31   include/ryu/d2s_full_table.h
32   include/ryu/d2s_intrinsics.h
33   include/ryu/digit_table.h
34   include/ryu/f2s.h
35   include/ryu/ryu.h
36   include/to_chars_floating_point.h
37   legacy_pointer_safety.cpp
38   memory.cpp
39   memory_resource.cpp
40   mutex.cpp
41   mutex_destructor.cpp
42   new_handler.cpp
43   new_helpers.cpp
44   optional.cpp
45   print.cpp
46   random_shuffle.cpp
47   ryu/d2fixed.cpp
48   ryu/d2s.cpp
49   ryu/f2s.cpp
50   shared_mutex.cpp
51   stdexcept.cpp
52   string.cpp
53   support/runtime/exception_fallback.ipp
54   support/runtime/exception_glibcxx.ipp
55   support/runtime/exception_libcxxabi.ipp
56   support/runtime/exception_libcxxrt.ipp
57   support/runtime/exception_msvc.ipp
58   support/runtime/exception_pointer_cxxabi.ipp
59   support/runtime/exception_pointer_glibcxx.ipp
60   support/runtime/exception_pointer_msvc.ipp
61   support/runtime/exception_pointer_unimplemented.ipp
62   support/runtime/stdexcept_default.ipp
63   support/runtime/stdexcept_vcruntime.ipp
64   system_error.cpp
65   thread.cpp
66   typeinfo.cpp
67   valarray.cpp
68   variant.cpp
69   vector.cpp
70   verbose_abort.cpp
71   )
73 if (LIBCXX_ENABLE_RANDOM_DEVICE)
74   list(APPEND LIBCXX_SOURCES
75     random.cpp
76     )
77 endif()
79 if (LIBCXX_ENABLE_LOCALIZATION)
80   list(APPEND LIBCXX_SOURCES
81     include/sso_allocator.h
82     ios.cpp
83     ios.instantiations.cpp
84     iostream.cpp
85     locale.cpp
86     regex.cpp
87     strstream.cpp
88     )
89 endif()
91 if(WIN32)
92   list(APPEND LIBCXX_SOURCES
93     support/win32/locale_win32.cpp
94     support/win32/support.cpp
95     )
97   if (NOT LIBCXX_HAS_PTHREAD_API)
98     list(APPEND LIBCXX_SOURCES
99       support/win32/thread_win32.cpp
100       )
101   endif()
102 elseif(ZOS)
103   list(APPEND LIBCXX_SOURCES
104     support/ibm/mbsnrtowcs.cpp
105     support/ibm/wcsnrtombs.cpp
106     support/ibm/xlocale_zos.cpp
107     )
108 endif()
110 if (LIBCXX_ENABLE_FILESYSTEM)
111   list(APPEND LIBCXX_SOURCES
112     filesystem/directory_entry.cpp
113     filesystem/directory_iterator.cpp
114     filesystem/file_descriptor.h
115     filesystem/operations.cpp
116     filesystem/posix_compat.h
117     filesystem/time_utils.h
118     )
119   # Filesystem uses __int128_t, which requires a definition of __muloi4 when
120   # compiled with UBSAN. This definition is not provided by libgcc_s, but is
121   # provided by compiler-rt. So we need to disable it to avoid having multiple
122   # definitions. See filesystem/int128_builtins.cpp.
123   if (NOT LIBCXX_USE_COMPILER_RT)
124     list(APPEND LIBCXX_SOURCES
125       filesystem/int128_builtins.cpp
126       )
127   endif()
128 endif()
130 if (LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS)
131   list(APPEND LIBCXX_SOURCES
132     new.cpp
133     )
134 endif()
136 # Add all the headers to the project for IDEs.
137 if (LIBCXX_CONFIGURE_IDE)
138   file(GLOB_RECURSE LIBCXX_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../include/*)
139   if(WIN32)
140     file( GLOB LIBCXX_WIN32_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../include/__support/win32/*.h)
141     list(APPEND LIBCXX_HEADERS ${LIBCXX_WIN32_HEADERS})
142   endif()
143   # Force them all into the headers dir on MSVC, otherwise they end up at
144   # project scope because they don't have extensions.
145   if (MSVC_IDE)
146     source_group("Header Files" FILES ${LIBCXX_HEADERS})
147   endif()
148 endif()
150 if(NOT LIBCXX_INSTALL_LIBRARY)
151   set(exclude_from_all EXCLUDE_FROM_ALL)
152 endif()
154 if (LIBCXX_GENERATE_COVERAGE AND NOT LIBCXX_COVERAGE_LIBRARY)
155   find_compiler_rt_library(profile LIBCXX_COVERAGE_LIBRARY)
156 endif()
157 add_library_flags_if(LIBCXX_COVERAGE_LIBRARY "${LIBCXX_COVERAGE_LIBRARY}")
159 if (APPLE AND LLVM_USE_SANITIZER)
160   if (("${LLVM_USE_SANITIZER}" STREQUAL "Address") OR
161       ("${LLVM_USE_SANITIZER}" STREQUAL "Address;Undefined") OR
162       ("${LLVM_USE_SANITIZER}" STREQUAL "Undefined;Address"))
163     set(LIBFILE "libclang_rt.asan_osx_dynamic.dylib")
164   elseif("${LLVM_USE_SANITIZER}" STREQUAL "Undefined")
165     set(LIBFILE "libclang_rt.ubsan_osx_dynamic.dylib")
166   elseif("${LLVM_USE_SANITIZER}" STREQUAL "Thread")
167     set(LIBFILE "libclang_rt.tsan_osx_dynamic.dylib")
168   else()
169     message(WARNING "LLVM_USE_SANITIZER=${LLVM_USE_SANITIZER} is not supported on OS X")
170   endif()
171   if (LIBFILE)
172     find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY)
173     get_filename_component(LIBDIR "${LIBCXX_BUILTINS_LIBRARY}" DIRECTORY)
174     if (NOT IS_DIRECTORY "${LIBDIR}")
175       message(FATAL_ERROR "Cannot find compiler-rt directory on OS X required for LLVM_USE_SANITIZER")
176     endif()
177     set(LIBCXX_SANITIZER_LIBRARY "${LIBDIR}/${LIBFILE}")
178     set(LIBCXX_SANITIZER_LIBRARY "${LIBCXX_SANITIZER_LIBRARY}" PARENT_SCOPE)
179     message(STATUS "Manually linking compiler-rt library: ${LIBCXX_SANITIZER_LIBRARY}")
180     add_library_flags("${LIBCXX_SANITIZER_LIBRARY}")
181     add_link_flags("-Wl,-rpath,${LIBDIR}")
182   endif()
183 endif()
185 split_list(LIBCXX_COMPILE_FLAGS)
186 split_list(LIBCXX_LINK_FLAGS)
188 # Build the shared library.
189 if (LIBCXX_ENABLE_SHARED)
190   add_library(cxx_shared SHARED ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
191   target_include_directories(cxx_shared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
192   target_link_libraries(cxx_shared PUBLIC cxx-headers
193                                    PRIVATE ${LIBCXX_LIBRARIES})
194   set_target_properties(cxx_shared
195     PROPERTIES
196       COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
197       LINK_FLAGS    "${LIBCXX_LINK_FLAGS}"
198       OUTPUT_NAME   "${LIBCXX_SHARED_OUTPUT_NAME}"
199       VERSION       "${LIBCXX_LIBRARY_VERSION}"
200       SOVERSION     "${LIBCXX_ABI_VERSION}"
201       DEFINE_SYMBOL ""
202   )
203   cxx_add_common_build_flags(cxx_shared)
205   if(ZOS)
206     add_custom_command(TARGET cxx_shared POST_BUILD
207       COMMAND
208         ${LIBCXX_SOURCE_DIR}/utils/zos_rename_dll_side_deck.sh
209         $<TARGET_LINKER_FILE_NAME:cxx_shared> $<TARGET_FILE_NAME:cxx_shared> "${LIBCXX_DLL_NAME}"
210       COMMENT "Rename dll name inside the side deck file"
211       WORKING_DIRECTORY $<TARGET_FILE_DIR:cxx_shared>
212     )
213   endif()
215   # Link against libc++abi
216   if (LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY)
217     target_link_libraries(cxx_shared PRIVATE libcxx-abi-shared-objects)
218   else()
219     target_link_libraries(cxx_shared PUBLIC libcxx-abi-shared)
220   endif()
222   # Maybe re-export symbols from libc++abi
223   # In particular, we don't re-export the symbols if libc++abi is merged statically
224   # into libc++ because in that case there's no dylib to re-export from.
225   if (APPLE AND LIBCXX_CXX_ABI STREQUAL "libcxxabi"
226             AND NOT DEFINED LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS
227             AND NOT LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY)
228     set(LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS ON)
229   endif()
231   if (LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS)
232     target_link_libraries(cxx_shared PRIVATE
233       "-Wl,-unexported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/libc++unexp.exp"
234       "-Wl,-reexported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/libc++abi.exp"
235       "-Wl,-force_symbols_not_weak_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/notweak.exp"
236       "-Wl,-force_symbols_weak_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/weak.exp")
238     target_link_libraries(cxx_shared PRIVATE $<TARGET_NAME_IF_EXISTS:cxxabi-reexports>)
239   endif()
241   # Generate a linker script in place of a libc++.so symlink.
242   if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
243     set(link_libraries)
245     set(imported_libname "$<TARGET_PROPERTY:libcxx-abi-shared,IMPORTED_LIBNAME>")
246     set(output_name "$<TARGET_PROPERTY:libcxx-abi-shared,OUTPUT_NAME>")
247     string(APPEND link_libraries "${CMAKE_LINK_LIBRARY_FLAG}$<IF:$<BOOL:${imported_libname}>,${imported_libname},${output_name}>")
249     # TODO: Move to the same approach as above for the unwind library
250     if (LIBCXXABI_USE_LLVM_UNWINDER)
251       if (LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY)
252         # libunwind is already included in libc++abi
253       elseif (TARGET unwind_shared OR HAVE_LIBUNWIND)
254         string(APPEND link_libraries " ${CMAKE_LINK_LIBRARY_FLAG}$<TARGET_PROPERTY:unwind_shared,OUTPUT_NAME>")
255       else()
256         string(APPEND link_libraries " ${CMAKE_LINK_LIBRARY_FLAG}unwind")
257       endif()
258     endif()
260     set(linker_script "INPUT($<TARGET_SONAME_FILE_NAME:cxx_shared> ${link_libraries})")
261     add_custom_command(TARGET cxx_shared POST_BUILD
262       COMMAND "${CMAKE_COMMAND}" -E remove "$<TARGET_LINKER_FILE:cxx_shared>"
263       COMMAND "${CMAKE_COMMAND}" -E echo "${linker_script}" > "$<TARGET_LINKER_FILE:cxx_shared>"
264       COMMENT "Generating linker script: '${linker_script}' as file $<TARGET_LINKER_FILE:cxx_shared>"
265       VERBATIM
266     )
267   endif()
269   list(APPEND LIBCXX_BUILD_TARGETS "cxx_shared")
270   if(WIN32 AND NOT MINGW AND NOT "${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
271     # Since we most likely do not have a mt.exe replacement, disable the
272     # manifest bundling.  This allows a normal cmake invocation to pass which
273     # will attempt to use the manifest tool to generate the bundled manifest
274     set_target_properties(cxx_shared PROPERTIES
275                           APPEND_STRING PROPERTY LINK_FLAGS " /MANIFEST:NO")
276   endif()
277 endif()
279 set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
281 # Build the static library.
282 if (LIBCXX_ENABLE_STATIC)
283   add_library(cxx_static STATIC ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
284   target_include_directories(cxx_static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
285   target_link_libraries(cxx_static PUBLIC cxx-headers
286                                    PRIVATE ${LIBCXX_LIBRARIES}
287                                    PRIVATE libcxx-abi-static)
288   set_target_properties(cxx_static
289     PROPERTIES
290       COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
291       LINK_FLAGS    "${LIBCXX_LINK_FLAGS}"
292       OUTPUT_NAME   "${LIBCXX_STATIC_OUTPUT_NAME}"
293   )
294   cxx_add_common_build_flags(cxx_static)
296   if (LIBCXX_HERMETIC_STATIC_LIBRARY)
297     # If the hermetic library doesn't define the operator new/delete functions
298     # then its code shouldn't declare them with hidden visibility.  They might
299     # actually be provided by a shared library at link time.
300     if (LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS)
301       append_flags_if_supported(CXX_STATIC_LIBRARY_FLAGS -fvisibility-global-new-delete-hidden)
302     endif()
303     target_compile_options(cxx_static PRIVATE ${CXX_STATIC_LIBRARY_FLAGS})
304     # _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS can be defined in __config_site
305     # too. Define it in the same way here, to avoid redefinition conflicts.
306     target_compile_definitions(cxx_static PRIVATE _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS=)
307   endif()
309   list(APPEND LIBCXX_BUILD_TARGETS "cxx_static")
310   # Attempt to merge the libc++.a archive and the ABI library archive into one.
311   if (LIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY)
312     target_link_libraries(cxx_static PRIVATE libcxx-abi-static-objects)
313   endif()
314 endif()
316 # Add a meta-target for both libraries.
317 add_custom_target(cxx DEPENDS ${LIBCXX_BUILD_TARGETS})
319 set(LIBCXX_EXPERIMENTAL_SOURCES
320   experimental/memory_resource.cpp
321   )
323 if (LIBCXX_PSTL_CPU_BACKEND STREQUAL "libdispatch")
324   list(APPEND LIBCXX_EXPERIMENTAL_SOURCES
325     pstl/libdispatch.cpp
326     )
327 endif()
329 if (LIBCXX_ENABLE_LOCALIZATION AND LIBCXX_ENABLE_FILESYSTEM AND LIBCXX_ENABLE_TIME_ZONE_DATABASE)
330   list(APPEND LIBCXX_EXPERIMENTAL_SOURCES
331     tz.cpp
332     tzdb_list.cpp
333     )
334 endif()
336 add_library(cxx_experimental STATIC ${LIBCXX_EXPERIMENTAL_SOURCES})
337 target_link_libraries(cxx_experimental PUBLIC cxx-headers)
338 if (LIBCXX_ENABLE_SHARED)
339   target_link_libraries(cxx_experimental PRIVATE cxx_shared)
340 else()
341   target_link_libraries(cxx_experimental PRIVATE cxx_static)
342 endif()
344 if (LIBCXX_HERMETIC_STATIC_LIBRARY)
345   # _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS can be defined in __config_site
346   # too. Define it in the same way here, to avoid redefinition conflicts.
347   target_compile_definitions(cxx_experimental PRIVATE _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS=)
348 endif()
350 set_target_properties(cxx_experimental
351   PROPERTIES
352     COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
353     OUTPUT_NAME   "c++experimental"
355 cxx_add_common_build_flags(cxx_experimental)
356 target_compile_options(cxx_experimental PUBLIC -D_LIBCPP_ENABLE_EXPERIMENTAL)
358 if (LIBCXX_INSTALL_SHARED_LIBRARY)
359   install(TARGETS cxx_shared
360     ARCHIVE DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
361     LIBRARY DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
362     RUNTIME DESTINATION ${LIBCXX_INSTALL_RUNTIME_DIR} COMPONENT cxx)
363 endif()
365 if (LIBCXX_INSTALL_STATIC_LIBRARY)
366   install(TARGETS cxx_static
367     ARCHIVE DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
368     LIBRARY DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
369     RUNTIME DESTINATION ${LIBCXX_INSTALL_RUNTIME_DIR} COMPONENT cxx)
370 endif()
372 if (LIBCXX_INSTALL_LIBRARY)
373   install(TARGETS cxx_experimental
374     LIBRARY DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
375     ARCHIVE DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
376     RUNTIME DESTINATION ${LIBCXX_INSTALL_RUNTIME_DIR} COMPONENT cxx)
377 endif()
379 # NOTE: This install command must go after the cxx install command otherwise
380 # it will not be executed after the library symlinks are installed.
381 if (LIBCXX_ENABLE_SHARED AND LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
382   install(FILES "$<TARGET_LINKER_FILE:cxx_shared>"
383     DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR}
384     COMPONENT libcxx)
385 endif()
387 if (NOT CMAKE_CONFIGURATION_TYPES)
388     if(LIBCXX_INSTALL_LIBRARY)
389       set(lib_install_target "cxx;cxx_experimental")
390     endif()
391     if(LIBCXX_INSTALL_HEADERS)
392       set(header_install_target install-cxx-headers)
393     endif()
394     add_custom_target(install-cxx
395                       DEPENDS ${lib_install_target}
396                               cxx_experimental
397                               ${header_install_target}
398                       COMMAND "${CMAKE_COMMAND}"
399                       -DCMAKE_INSTALL_COMPONENT=cxx
400                       -P "${LIBCXX_BINARY_DIR}/cmake_install.cmake")
401     add_custom_target(install-cxx-stripped
402                       DEPENDS ${lib_install_target}
403                               cxx_experimental
404                               ${header_install_target}
405                       COMMAND "${CMAKE_COMMAND}"
406                       -DCMAKE_INSTALL_COMPONENT=cxx
407                       -DCMAKE_INSTALL_DO_STRIP=1
408                       -P "${LIBCXX_BINARY_DIR}/cmake_install.cmake")
409 endif()