[WebAssembly] Add new target feature in support of 'extended-const' proposal
[llvm-project.git] / libcxx / src / CMakeLists.txt
blob3395003c373bae601fe087bc5f9050231898ca94
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   exception.cpp
15   functional.cpp
16   future.cpp
17   hash.cpp
18   include/apple_availability.h
19   include/atomic_support.h
20   include/config_elast.h
21   include/refstring.h
22   include/ryu/common.h
23   include/ryu/d2fixed.h
24   include/ryu/d2fixed_full_table.h
25   include/ryu/d2s.h
26   include/ryu/d2s_full_table.h
27   include/ryu/d2s_intrinsics.h
28   include/ryu/digit_table.h
29   include/ryu/f2s.h
30   include/ryu/ryu.h
31   include/to_chars_floating_point.h
32   legacy_pointer_safety.cpp
33   memory.cpp
34   mutex.cpp
35   mutex_destructor.cpp
36   new.cpp
37   optional.cpp
38   random_shuffle.cpp
39   ryu/d2fixed.cpp
40   ryu/d2s.cpp
41   ryu/f2s.cpp
42   shared_mutex.cpp
43   stdexcept.cpp
44   string.cpp
45   support/runtime/exception_fallback.ipp
46   support/runtime/exception_glibcxx.ipp
47   support/runtime/exception_libcxxabi.ipp
48   support/runtime/exception_libcxxrt.ipp
49   support/runtime/exception_msvc.ipp
50   support/runtime/exception_pointer_cxxabi.ipp
51   support/runtime/exception_pointer_glibcxx.ipp
52   support/runtime/exception_pointer_msvc.ipp
53   support/runtime/exception_pointer_unimplemented.ipp
54   support/runtime/new_handler_fallback.ipp
55   support/runtime/stdexcept_default.ipp
56   support/runtime/stdexcept_vcruntime.ipp
57   system_error.cpp
58   thread.cpp
59   typeinfo.cpp
60   utility.cpp
61   valarray.cpp
62   variant.cpp
63   vector.cpp
64   )
66 if (LIBCXX_ENABLE_DEBUG_MODE_SUPPORT)
67   list(APPEND LIBCXX_SOURCES
68     assert.cpp
69     debug.cpp
70     )
71 endif()
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(LIBCXX_ENABLE_INCOMPLETE_FEATURES)
92   list(APPEND LIBCXX_SOURCES
93     format.cpp
94   )
95 endif()
97 if(WIN32)
98   list(APPEND LIBCXX_SOURCES
99     support/win32/locale_win32.cpp
100     support/win32/support.cpp
101     )
103   if (NOT LIBCXX_HAS_PTHREAD_API)
104     list(APPEND LIBCXX_SOURCES
105       support/win32/thread_win32.cpp
106       )
107   endif()
108 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "SunOS")
109   list(APPEND LIBCXX_SOURCES
110     support/solaris/mbsnrtowcs.inc
111     support/solaris/wcsnrtombs.inc
112     support/solaris/xlocale.cpp
113     )
114 elseif(ZOS)
115   list(APPEND LIBCXX_SOURCES
116     support/ibm/mbsnrtowcs.cpp
117     support/ibm/wcsnrtombs.cpp
118     support/ibm/xlocale_zos.cpp
119     )
120 endif()
122 if (LIBCXX_ENABLE_FILESYSTEM)
123   list(APPEND LIBCXX_SOURCES
124     filesystem/filesystem_common.h
125     filesystem/operations.cpp
126     filesystem/directory_iterator.cpp
127     filesystem/posix_compat.h
128     )
129   # Filesystem uses __int128_t, which requires a definition of __muloi4 when
130   # compiled with UBSAN. This definition is not provided by libgcc_s, but is
131   # provided by compiler-rt. So we need to disable it to avoid having multiple
132   # definitions. See filesystem/int128_builtins.cpp.
133   if (NOT LIBCXX_USE_COMPILER_RT)
134     list(APPEND LIBCXX_SOURCES
135       filesystem/int128_builtins.cpp
136       )
137   endif()
138 endif()
140 # Add all the headers to the project for IDEs.
141 if (LIBCXX_CONFIGURE_IDE)
142   file(GLOB_RECURSE LIBCXX_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../include/*)
143   if(WIN32)
144     file( GLOB LIBCXX_WIN32_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../include/__support/win32/*.h)
145     list(APPEND LIBCXX_HEADERS ${LIBCXX_WIN32_HEADERS})
146   endif()
147   # Force them all into the headers dir on MSVC, otherwise they end up at
148   # project scope because they don't have extensions.
149   if (MSVC_IDE)
150     source_group("Header Files" FILES ${LIBCXX_HEADERS})
151   endif()
152 endif()
154 if(NOT LIBCXX_INSTALL_LIBRARY)
155   set(exclude_from_all EXCLUDE_FROM_ALL)
156 endif()
158 # If LIBCXX_CXX_ABI_LIBRARY_PATH is defined we want to add it to the search path.
159 add_link_flags_if(LIBCXX_CXX_ABI_LIBRARY_PATH
160                   "${CMAKE_LIBRARY_PATH_FLAG}${LIBCXX_CXX_ABI_LIBRARY_PATH}")
163 if (LIBCXX_GENERATE_COVERAGE AND NOT LIBCXX_COVERAGE_LIBRARY)
164   find_compiler_rt_library(profile LIBCXX_COVERAGE_LIBRARY)
165 endif()
166 add_library_flags_if(LIBCXX_COVERAGE_LIBRARY "${LIBCXX_COVERAGE_LIBRARY}")
168 if (APPLE AND LLVM_USE_SANITIZER)
169   if (("${LLVM_USE_SANITIZER}" STREQUAL "Address") OR
170       ("${LLVM_USE_SANITIZER}" STREQUAL "Address;Undefined") OR
171       ("${LLVM_USE_SANITIZER}" STREQUAL "Undefined;Address"))
172     set(LIBFILE "libclang_rt.asan_osx_dynamic.dylib")
173   elseif("${LLVM_USE_SANITIZER}" STREQUAL "Undefined")
174     set(LIBFILE "libclang_rt.ubsan_osx_dynamic.dylib")
175   elseif("${LLVM_USE_SANITIZER}" STREQUAL "Thread")
176     set(LIBFILE "libclang_rt.tsan_osx_dynamic.dylib")
177   else()
178     message(WARNING "LLVM_USE_SANITIZER=${LLVM_USE_SANITIZER} is not supported on OS X")
179   endif()
180   if (LIBFILE)
181     find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY)
182     get_filename_component(LIBDIR "${LIBCXX_BUILTINS_LIBRARY}" DIRECTORY)
183     if (NOT IS_DIRECTORY "${LIBDIR}")
184       message(FATAL_ERROR "Cannot find compiler-rt directory on OS X required for LLVM_USE_SANITIZER")
185     endif()
186     set(LIBCXX_SANITIZER_LIBRARY "${LIBDIR}/${LIBFILE}")
187     set(LIBCXX_SANITIZER_LIBRARY "${LIBCXX_SANITIZER_LIBRARY}" PARENT_SCOPE)
188     message(STATUS "Manually linking compiler-rt library: ${LIBCXX_SANITIZER_LIBRARY}")
189     add_library_flags("${LIBCXX_SANITIZER_LIBRARY}")
190     add_link_flags("-Wl,-rpath,${LIBDIR}")
191   endif()
192 endif()
194 if (LIBCXX_ENABLE_PARALLEL_ALGORITHMS AND NOT TARGET pstl::ParallelSTL)
195   message(FATAL_ERROR "Could not find ParallelSTL")
196 endif()
198 function(cxx_set_common_defines name)
199   if(LIBCXX_CXX_ABI_HEADER_TARGET)
200     add_dependencies(${name} ${LIBCXX_CXX_ABI_HEADER_TARGET})
201   endif()
203   if (LIBCXX_ENABLE_PARALLEL_ALGORITHMS)
204     target_link_libraries(${name} PUBLIC pstl::ParallelSTL)
205   endif()
206 endfunction()
208 split_list(LIBCXX_COMPILE_FLAGS)
209 split_list(LIBCXX_LINK_FLAGS)
211 # Build the shared library.
212 if (LIBCXX_ENABLE_SHARED)
213   add_library(cxx_shared SHARED ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
214   target_include_directories(cxx_shared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
215   target_link_libraries(cxx_shared PUBLIC cxx-headers
216                                    PRIVATE ${LIBCXX_LIBRARIES})
217   set_target_properties(cxx_shared
218     PROPERTIES
219       COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
220       LINK_FLAGS    "${LIBCXX_LINK_FLAGS}"
221       OUTPUT_NAME   "c++"
222       VERSION       "${LIBCXX_ABI_VERSION}.0"
223       SOVERSION     "${LIBCXX_ABI_VERSION}"
224       DEFINE_SYMBOL ""
225   )
226   cxx_add_common_build_flags(cxx_shared)
227   cxx_set_common_defines(cxx_shared)
229   # Link against LLVM libunwind
230   if (LIBCXXABI_USE_LLVM_UNWINDER)
231     if (NOT LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY AND (TARGET unwind_shared OR HAVE_LIBUNWIND))
232       target_link_libraries(cxx_shared PUBLIC unwind_shared)
233     elseif (LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY AND (TARGET unwind_static OR HAVE_LIBUNWIND))
234       # libunwind is already included in libc++abi
235     else()
236       target_link_libraries(cxx_shared PUBLIC unwind)
237     endif()
238   endif()
240   # Link against libc++abi
241   if (LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY)
242     if (APPLE)
243       target_link_libraries(cxx_shared PRIVATE "-Wl,-force_load" "${LIBCXX_CXX_STATIC_ABI_LIBRARY}")
244     else()
245       target_link_libraries(cxx_shared PRIVATE "-Wl,--whole-archive,-Bstatic" "${LIBCXX_CXX_STATIC_ABI_LIBRARY}" "-Wl,-Bdynamic,--no-whole-archive")
246     endif()
247   else()
248     target_link_libraries(cxx_shared PUBLIC "${LIBCXX_CXX_SHARED_ABI_LIBRARY}")
249   endif()
251   # Maybe re-export symbols from libc++abi
252   # In particular, we don't re-export the symbols if libc++abi is merged statically
253   # into libc++ because in that case there's no dylib to re-export from.
254   if (APPLE AND (LIBCXX_CXX_ABI_LIBNAME STREQUAL "libcxxabi" OR
255                  LIBCXX_CXX_ABI_LIBNAME STREQUAL "default")
256             AND NOT DEFINED LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS
257             AND NOT LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY)
258     set(LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS ON)
259   endif()
261   if (LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS)
262     target_link_libraries(cxx_shared PRIVATE
263       "-Wl,-unexported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/libc++unexp.exp"
264       "-Wl,-reexported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/libc++abi.v${LIBCXX_LIBCPPABI_VERSION}.exp"
265       "-Wl,-force_symbols_not_weak_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/notweak.exp"
266       "-Wl,-force_symbols_weak_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/weak.exp")
268     target_link_libraries(cxx_shared PRIVATE $<TARGET_NAME_IF_EXISTS:cxxabi-reexports>)
269   endif()
271   # Generate a linker script in place of a libc++.so symlink.
272   if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
273       include(DefineLinkerScript)
274       define_linker_script(cxx_shared)
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   set_target_properties(cxx_static
296     PROPERTIES
297       COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
298       LINK_FLAGS    "${LIBCXX_LINK_FLAGS}"
299       OUTPUT_NAME   "c++"
300   )
301   cxx_add_common_build_flags(cxx_static)
302   cxx_set_common_defines(cxx_static)
304   if (LIBCXXABI_USE_LLVM_UNWINDER)
305     # If we intend to use the just-built unwinder, add a dependency so that it
306     # gets built, even if we technically aren't going to link it in at this
307     # stage.
308     add_dependencies(cxx_static unwind)
309   endif()
311   if (LIBCXX_HERMETIC_STATIC_LIBRARY)
312     # If the hermetic library doesn't define the operator new/delete functions
313     # then its code shouldn't declare them with hidden visibility.  They might
314     # actually be provided by a shared library at link time.
315     if (LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS)
316       append_flags_if_supported(CXX_STATIC_LIBRARY_FLAGS -fvisibility-global-new-delete-hidden)
317     endif()
318     target_compile_options(cxx_static PRIVATE ${CXX_STATIC_LIBRARY_FLAGS})
319     target_compile_definitions(cxx_static PRIVATE _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
320   endif()
322   list(APPEND LIBCXX_BUILD_TARGETS "cxx_static")
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     set(MERGE_ARCHIVES_SEARCH_PATHS "")
326     if (LIBCXX_CXX_ABI_LIBRARY_PATH)
327       set(MERGE_ARCHIVES_SEARCH_PATHS "-L${LIBCXX_CXX_ABI_LIBRARY_PATH}")
328     endif()
329     if (TARGET "${LIBCXX_CXX_STATIC_ABI_LIBRARY}" OR HAVE_LIBCXXABI)
330       set(MERGE_ARCHIVES_ABI_TARGET "$<TARGET_LINKER_FILE:${LIBCXX_CXX_STATIC_ABI_LIBRARY}>")
331     else()
332       set(MERGE_ARCHIVES_ABI_TARGET
333         "${CMAKE_STATIC_LIBRARY_PREFIX}${LIBCXX_CXX_STATIC_ABI_LIBRARY}${CMAKE_STATIC_LIBRARY_SUFFIX}")
334       if (LIBCXX_CXX_ABI_LIBRARY_PATH)
335         set(MERGE_ARCHIVES_ABI_TARGET "${LIBCXX_CXX_ABI_LIBRARY_PATH}/${MERGE_ARCHIVES_ABI_TARGET}")
336       endif ()
337     endif()
338     if (APPLE)
339       set(MERGE_ARCHIVES_LIBTOOL "--use-libtool" "--libtool" "${CMAKE_LIBTOOL}")
340     endif()
341     add_custom_command(TARGET cxx_static POST_BUILD
342     COMMAND
343       ${Python3_EXECUTABLE} ${LIBCXX_SOURCE_DIR}/utils/merge_archives.py
344     ARGS
345       -o $<TARGET_LINKER_FILE:cxx_static>
346       --ar "${CMAKE_AR}"
347       ${MERGE_ARCHIVES_LIBTOOL}
348       "$<TARGET_LINKER_FILE:cxx_static>"
349       "${MERGE_ARCHIVES_ABI_TARGET}"
350       "${MERGE_ARCHIVES_SEARCH_PATHS}"
351     WORKING_DIRECTORY ${LIBCXX_BUILD_DIR}
352     DEPENDS ${MERGE_ARCHIVES_ABI_TARGET}
353     )
354   endif()
355 endif()
357 # Add a meta-target for both libraries.
358 add_custom_target(cxx DEPENDS ${LIBCXX_BUILD_TARGETS})
360 if (LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY)
361   set(LIBCXX_EXPERIMENTAL_SOURCES
362     experimental/memory_resource.cpp
363     )
364   add_library(cxx_experimental STATIC ${LIBCXX_EXPERIMENTAL_SOURCES})
365   if (LIBCXX_ENABLE_SHARED)
366     target_link_libraries(cxx_experimental PRIVATE cxx_shared)
367   else()
368     target_link_libraries(cxx_experimental PRIVATE cxx_static)
369   endif()
371   set_target_properties(cxx_experimental
372     PROPERTIES
373       COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
374       OUTPUT_NAME   "c++experimental"
375   )
376   cxx_add_common_build_flags(cxx_experimental)
377 endif()
380 if (LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY)
381   set(LIBCXX_EXTERNAL_THREADING_SUPPORT_SOURCES
382       "${CMAKE_CURRENT_SOURCE_DIR}/../test/support/external_threads.cpp")
384   if (LIBCXX_ENABLE_SHARED)
385     add_library(cxx_external_threads SHARED ${LIBCXX_EXTERNAL_THREADING_SUPPORT_SOURCES})
386   else()
387     add_library(cxx_external_threads STATIC ${LIBCXX_EXTERNAL_THREADING_SUPPORT_SOURCES})
388   endif()
390   set_target_properties(cxx_external_threads
391     PROPERTIES
392       LINK_FLAGS    "${LIBCXX_LINK_FLAGS}"
393       COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
394       OUTPUT_NAME   "c++external_threads"
395   )
397   target_link_libraries(cxx_external_threads PRIVATE cxx-headers)
398 endif()
400 if (LIBCXX_INSTALL_SHARED_LIBRARY)
401   install(TARGETS cxx_shared
402     ARCHIVE DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
403     LIBRARY DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
404     RUNTIME DESTINATION ${LIBCXX_INSTALL_RUNTIME_DIR} COMPONENT cxx)
405 endif()
407 if (LIBCXX_INSTALL_STATIC_LIBRARY)
408   install(TARGETS cxx_static
409     ARCHIVE DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
410     LIBRARY DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
411     RUNTIME DESTINATION ${LIBCXX_INSTALL_RUNTIME_DIR} COMPONENT cxx)
412 endif()
414 if(LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY)
415   install(TARGETS cxx_experimental
416     LIBRARY DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
417     ARCHIVE DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
418     RUNTIME DESTINATION ${LIBCXX_INSTALL_RUNTIME_DIR} COMPONENT cxx)
419 endif()
421 # NOTE: This install command must go after the cxx install command otherwise
422 # it will not be executed after the library symlinks are installed.
423 if (LIBCXX_ENABLE_SHARED AND LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
424   install(FILES "$<TARGET_LINKER_FILE:cxx_shared>"
425     DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR}
426     COMPONENT libcxx)
427 endif()
429 if (NOT CMAKE_CONFIGURATION_TYPES)
430     if(LIBCXX_INSTALL_LIBRARY)
431       set(lib_install_target cxx)
432     endif()
433     if (LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY)
434       set(experimental_lib_install_target cxx_experimental)
435     endif()
436     if(LIBCXX_INSTALL_HEADERS)
437       set(header_install_target install-cxx-headers)
438     endif()
439     if (LIBCXX_ENABLE_PARALLEL_ALGORITHMS)
440       set(pstl_install_target install-pstl)
441     endif()
442     add_custom_target(install-cxx
443                       DEPENDS ${lib_install_target}
444                               ${experimental_lib_install_target}
445                               ${header_install_target}
446                               ${pstl_install_target}
447                       COMMAND "${CMAKE_COMMAND}"
448                       -DCMAKE_INSTALL_COMPONENT=cxx
449                       -P "${LIBCXX_BINARY_DIR}/cmake_install.cmake")
450     add_custom_target(install-cxx-stripped
451                       DEPENDS ${lib_install_target}
452                               ${experimental_lib_install_target}
453                               ${header_install_target}
454                               ${pstl_install_target}
455                       COMMAND "${CMAKE_COMMAND}"
456                       -DCMAKE_INSTALL_COMPONENT=cxx
457                       -DCMAKE_INSTALL_DO_STRIP=1
458                       -P "${LIBCXX_BINARY_DIR}/cmake_install.cmake")
459 endif()