xref: /openbsd/gnu/llvm/libcxx/src/CMakeLists.txt (revision 76d0caae)
1set(LIBCXX_LIB_CMAKEFILES_DIR "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}"  PARENT_SCOPE)
2
3# Get sources
4set(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  memory.cpp
23  mutex.cpp
24  mutex_destructor.cpp
25  new.cpp
26  optional.cpp
27  random_shuffle.cpp
28  shared_mutex.cpp
29  stdexcept.cpp
30  string.cpp
31  support/runtime/exception_fallback.ipp
32  support/runtime/exception_glibcxx.ipp
33  support/runtime/exception_libcxxabi.ipp
34  support/runtime/exception_libcxxrt.ipp
35  support/runtime/exception_msvc.ipp
36  support/runtime/exception_pointer_cxxabi.ipp
37  support/runtime/exception_pointer_glibcxx.ipp
38  support/runtime/exception_pointer_msvc.ipp
39  support/runtime/exception_pointer_unimplemented.ipp
40  support/runtime/new_handler_fallback.ipp
41  support/runtime/stdexcept_default.ipp
42  support/runtime/stdexcept_vcruntime.ipp
43  system_error.cpp
44  thread.cpp
45  typeinfo.cpp
46  utility.cpp
47  valarray.cpp
48  variant.cpp
49  vector.cpp
50  )
51
52if (LIBCXX_ENABLE_DEBUG_MODE_SUPPORT)
53  list(APPEND LIBCXX_SOURCES
54    debug.cpp
55    )
56endif()
57
58if (LIBCXX_ENABLE_RANDOM_DEVICE)
59  list(APPEND LIBCXX_SOURCES
60    random.cpp
61    )
62endif()
63
64if (LIBCXX_ENABLE_LOCALIZATION)
65  list(APPEND LIBCXX_SOURCES
66    include/sso_allocator.h
67    ios.cpp
68    ios.instantiations.cpp
69    iostream.cpp
70    locale.cpp
71    regex.cpp
72    strstream.cpp
73    )
74endif()
75
76if(LIBCXX_ENABLE_INCOMPLETE_FEATURES)
77  list(APPEND LIBCXX_SOURCES
78    format.cpp
79  )
80endif()
81
82if(WIN32)
83  list(APPEND LIBCXX_SOURCES
84    support/win32/locale_win32.cpp
85    support/win32/support.cpp
86    )
87
88  if (NOT LIBCXX_HAS_PTHREAD_API)
89    list(APPEND LIBCXX_SOURCES
90      support/win32/thread_win32.cpp
91      )
92  endif()
93elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "SunOS")
94  list(APPEND LIBCXX_SOURCES
95    support/solaris/mbsnrtowcs.inc
96    support/solaris/wcsnrtombs.inc
97    support/solaris/xlocale.cpp
98    )
99elseif(ZOS)
100  list(APPEND LIBCXX_SOURCES
101    support/ibm/xlocale_zos.cpp
102    )
103endif()
104
105if (LIBCXX_ENABLE_FILESYSTEM)
106  list(APPEND LIBCXX_SOURCES
107    filesystem/filesystem_common.h
108    filesystem/operations.cpp
109    filesystem/directory_iterator.cpp
110    filesystem/posix_compat.h
111    )
112  # Filesystem uses __int128_t, which requires a definition of __muloi4 when
113  # compiled with UBSAN. This definition is not provided by libgcc_s, but is
114  # provided by compiler-rt. So we need to disable it to avoid having multiple
115  # definitions. See filesystem/int128_builtins.cpp.
116  if (NOT LIBCXX_USE_COMPILER_RT)
117    list(APPEND LIBCXX_SOURCES
118      filesystem/int128_builtins.cpp
119      )
120  endif()
121endif()
122
123# Add all the headers to the project for IDEs.
124if (LIBCXX_CONFIGURE_IDE)
125  file(GLOB_RECURSE LIBCXX_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../include/*)
126  if(WIN32)
127    file( GLOB LIBCXX_WIN32_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../include/__support/win32/*.h)
128    list(APPEND LIBCXX_HEADERS ${LIBCXX_WIN32_HEADERS})
129  endif()
130  # Force them all into the headers dir on MSVC, otherwise they end up at
131  # project scope because they don't have extensions.
132  if (MSVC_IDE)
133    source_group("Header Files" FILES ${LIBCXX_HEADERS})
134  endif()
135endif()
136
137if(NOT LIBCXX_INSTALL_LIBRARY)
138  set(exclude_from_all EXCLUDE_FROM_ALL)
139endif()
140
141# If LIBCXX_CXX_ABI_LIBRARY_PATH is defined we want to add it to the search path.
142add_link_flags_if(LIBCXX_CXX_ABI_LIBRARY_PATH
143                  "${CMAKE_LIBRARY_PATH_FLAG}${LIBCXX_CXX_ABI_LIBRARY_PATH}")
144
145
146if (LIBCXX_GENERATE_COVERAGE AND NOT LIBCXX_COVERAGE_LIBRARY)
147  find_compiler_rt_library(profile LIBCXX_COVERAGE_LIBRARY)
148endif()
149add_library_flags_if(LIBCXX_COVERAGE_LIBRARY "${LIBCXX_COVERAGE_LIBRARY}")
150
151if (APPLE AND LLVM_USE_SANITIZER)
152  if (("${LLVM_USE_SANITIZER}" STREQUAL "Address") OR
153      ("${LLVM_USE_SANITIZER}" STREQUAL "Address;Undefined") OR
154      ("${LLVM_USE_SANITIZER}" STREQUAL "Undefined;Address"))
155    set(LIBFILE "libclang_rt.asan_osx_dynamic.dylib")
156  elseif("${LLVM_USE_SANITIZER}" STREQUAL "Undefined")
157    set(LIBFILE "libclang_rt.ubsan_osx_dynamic.dylib")
158  elseif("${LLVM_USE_SANITIZER}" STREQUAL "Thread")
159    set(LIBFILE "libclang_rt.tsan_osx_dynamic.dylib")
160  else()
161    message(WARNING "LLVM_USE_SANITIZER=${LLVM_USE_SANITIZER} is not supported on OS X")
162  endif()
163  if (LIBFILE)
164    find_compiler_rt_dir(LIBDIR)
165    if (NOT IS_DIRECTORY "${LIBDIR}")
166      message(FATAL_ERROR "Cannot find compiler-rt directory on OS X required for LLVM_USE_SANITIZER")
167    endif()
168    set(LIBCXX_SANITIZER_LIBRARY "${LIBDIR}/${LIBFILE}")
169    set(LIBCXX_SANITIZER_LIBRARY "${LIBCXX_SANITIZER_LIBRARY}" PARENT_SCOPE)
170    message(STATUS "Manually linking compiler-rt library: ${LIBCXX_SANITIZER_LIBRARY}")
171    add_library_flags("${LIBCXX_SANITIZER_LIBRARY}")
172    add_link_flags("-Wl,-rpath,${LIBDIR}")
173  endif()
174endif()
175
176if (LIBCXX_ENABLE_PARALLEL_ALGORITHMS AND NOT TARGET pstl::ParallelSTL)
177  message(FATAL_ERROR "Could not find ParallelSTL")
178endif()
179
180function(cxx_set_common_defines name)
181  if(LIBCXX_CXX_ABI_HEADER_TARGET)
182    add_dependencies(${name} ${LIBCXX_CXX_ABI_HEADER_TARGET})
183  endif()
184
185  if (LIBCXX_ENABLE_PARALLEL_ALGORITHMS)
186    target_link_libraries(${name} PUBLIC pstl::ParallelSTL)
187  endif()
188endfunction()
189
190split_list(LIBCXX_COMPILE_FLAGS)
191split_list(LIBCXX_LINK_FLAGS)
192
193# Build the shared library.
194if (LIBCXX_ENABLE_SHARED)
195  add_library(cxx_shared SHARED ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
196  target_link_libraries(cxx_shared PUBLIC cxx-headers
197                                   PRIVATE ${LIBCXX_LIBRARIES})
198  set_target_properties(cxx_shared
199    PROPERTIES
200      COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
201      LINK_FLAGS    "${LIBCXX_LINK_FLAGS}"
202      OUTPUT_NAME   "c++"
203      VERSION       "${LIBCXX_ABI_VERSION}.0"
204      SOVERSION     "${LIBCXX_ABI_VERSION}"
205      DEFINE_SYMBOL ""
206      POSITION_INDEPENDENT_CODE ON
207  )
208  cxx_add_common_build_flags(cxx_shared)
209  cxx_set_common_defines(cxx_shared)
210
211  # Link against LLVM libunwind
212  if (LIBCXXABI_USE_LLVM_UNWINDER)
213    if (NOT LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY AND (TARGET unwind_shared OR HAVE_LIBUNWIND))
214      target_link_libraries(cxx_shared PUBLIC unwind_shared)
215    elseif (LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY AND (TARGET unwind_static OR HAVE_LIBUNWIND))
216      # libunwind is already included in libc++abi
217    else()
218      target_link_libraries(cxx_shared PUBLIC unwind)
219    endif()
220  endif()
221
222  # Link against libc++abi
223  if (LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY)
224    if (APPLE)
225      target_link_libraries(cxx_shared PRIVATE "-Wl,-force_load" "${LIBCXX_CXX_STATIC_ABI_LIBRARY}")
226    else()
227      target_link_libraries(cxx_shared PRIVATE "-Wl,--whole-archive,-Bstatic" "${LIBCXX_CXX_STATIC_ABI_LIBRARY}" "-Wl,-Bdynamic,--no-whole-archive")
228    endif()
229  else()
230    target_link_libraries(cxx_shared PUBLIC "${LIBCXX_CXX_SHARED_ABI_LIBRARY}")
231  endif()
232
233  # Maybe re-export symbols from libc++abi
234  # In particular, we don't re-export the symbols if libc++abi is merged statically
235  # into libc++ because in that case there's no dylib to re-export from.
236  if (APPLE AND (LIBCXX_CXX_ABI_LIBNAME STREQUAL "libcxxabi" OR
237                 LIBCXX_CXX_ABI_LIBNAME STREQUAL "default")
238            AND NOT DEFINED LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS
239            AND NOT LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY)
240    set(LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS ON)
241  endif()
242
243  if (LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS)
244    target_link_libraries(cxx_shared PRIVATE
245      "-Wl,-unexported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/libc++unexp.exp"
246      "-Wl,-reexported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/libc++abi.v${LIBCXX_LIBCPPABI_VERSION}.exp"
247      "-Wl,-force_symbols_not_weak_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/notweak.exp"
248      "-Wl,-force_symbols_weak_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/weak.exp")
249
250    target_link_libraries(cxx_shared PRIVATE $<TARGET_NAME_IF_EXISTS:cxxabi-reexports>)
251  endif()
252
253  # Generate a linker script in place of a libc++.so symlink.
254  if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
255      include(DefineLinkerScript)
256      define_linker_script(cxx_shared)
257  endif()
258
259  list(APPEND LIBCXX_BUILD_TARGETS "cxx_shared")
260  if(WIN32 AND NOT MINGW AND NOT "${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
261    # Since we most likely do not have a mt.exe replacement, disable the
262    # manifest bundling.  This allows a normal cmake invocation to pass which
263    # will attempt to use the manifest tool to generate the bundled manifest
264    set_target_properties(cxx_shared PROPERTIES
265                          APPEND_STRING PROPERTY LINK_FLAGS " /MANIFEST:NO")
266  endif()
267endif()
268
269set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
270
271# Build the static library.
272if (LIBCXX_ENABLE_STATIC)
273  add_library(cxx_static STATIC ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
274  target_link_libraries(cxx_static PUBLIC cxx-headers
275                                   PRIVATE ${LIBCXX_LIBRARIES})
276  set_target_properties(cxx_static
277    PROPERTIES
278      COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
279      LINK_FLAGS    "${LIBCXX_LINK_FLAGS}"
280      OUTPUT_NAME   "c++"
281      POSITION_INDEPENDENT_CODE ON
282  )
283  cxx_add_common_build_flags(cxx_static)
284  cxx_set_common_defines(cxx_static)
285
286  if (LIBCXX_HERMETIC_STATIC_LIBRARY)
287    # If the hermetic library doesn't define the operator new/delete functions
288    # then its code shouldn't declare them with hidden visibility.  They might
289    # actually be provided by a shared library at link time.
290    if (LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS)
291      append_flags_if_supported(CXX_STATIC_LIBRARY_FLAGS -fvisibility-global-new-delete-hidden)
292    endif()
293    target_compile_options(cxx_static PRIVATE ${CXX_STATIC_LIBRARY_FLAGS})
294    target_compile_definitions(cxx_static PRIVATE _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
295  endif()
296
297  list(APPEND LIBCXX_BUILD_TARGETS "cxx_static")
298  # Attempt to merge the libc++.a archive and the ABI library archive into one.
299  if (LIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY)
300    set(MERGE_ARCHIVES_SEARCH_PATHS "")
301    if (LIBCXX_CXX_ABI_LIBRARY_PATH)
302      set(MERGE_ARCHIVES_SEARCH_PATHS "-L${LIBCXX_CXX_ABI_LIBRARY_PATH}")
303    endif()
304    if (TARGET "${LIBCXX_CXX_STATIC_ABI_LIBRARY}" OR HAVE_LIBCXXABI)
305      set(MERGE_ARCHIVES_ABI_TARGET "$<TARGET_LINKER_FILE:${LIBCXX_CXX_STATIC_ABI_LIBRARY}>")
306    else()
307      set(MERGE_ARCHIVES_ABI_TARGET
308        "${CMAKE_STATIC_LIBRARY_PREFIX}${LIBCXX_CXX_STATIC_ABI_LIBRARY}${CMAKE_STATIC_LIBRARY_SUFFIX}")
309      if (LIBCXX_CXX_ABI_LIBRARY_PATH)
310        set(MERGE_ARCHIVES_ABI_TARGET "${LIBCXX_CXX_ABI_LIBRARY_PATH}/${MERGE_ARCHIVES_ABI_TARGET}")
311      endif ()
312    endif()
313    if (APPLE)
314      set(MERGE_ARCHIVES_LIBTOOL "--use-libtool" "--libtool" "${CMAKE_LIBTOOL}")
315    endif()
316    add_custom_command(TARGET cxx_static POST_BUILD
317    COMMAND
318      ${Python3_EXECUTABLE} ${LIBCXX_SOURCE_DIR}/utils/merge_archives.py
319    ARGS
320      -o $<TARGET_LINKER_FILE:cxx_static>
321      --ar "${CMAKE_AR}"
322      ${MERGE_ARCHIVES_LIBTOOL}
323      "$<TARGET_LINKER_FILE:cxx_static>"
324      "${MERGE_ARCHIVES_ABI_TARGET}"
325      "${MERGE_ARCHIVES_SEARCH_PATHS}"
326    WORKING_DIRECTORY ${LIBCXX_BUILD_DIR}
327    DEPENDS ${MERGE_ARCHIVES_ABI_TARGET}
328    )
329  endif()
330endif()
331
332# Add a meta-target for both libraries.
333add_custom_target(cxx DEPENDS ${LIBCXX_BUILD_TARGETS})
334
335if (LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY)
336  set(LIBCXX_EXPERIMENTAL_SOURCES
337    experimental/memory_resource.cpp
338    )
339  add_library(cxx_experimental STATIC ${LIBCXX_EXPERIMENTAL_SOURCES})
340  if (LIBCXX_ENABLE_SHARED)
341    target_link_libraries(cxx_experimental PRIVATE cxx_shared)
342  else()
343    target_link_libraries(cxx_experimental PRIVATE cxx_static)
344  endif()
345
346  set_target_properties(cxx_experimental
347    PROPERTIES
348      COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
349      OUTPUT_NAME   "c++experimental"
350  )
351  cxx_add_common_build_flags(cxx_experimental)
352endif()
353
354
355if (LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY)
356  set(LIBCXX_EXTERNAL_THREADING_SUPPORT_SOURCES
357      "${CMAKE_CURRENT_SOURCE_DIR}/../test/support/external_threads.cpp")
358
359  if (LIBCXX_ENABLE_SHARED)
360    add_library(cxx_external_threads SHARED ${LIBCXX_EXTERNAL_THREADING_SUPPORT_SOURCES})
361  else()
362    add_library(cxx_external_threads STATIC ${LIBCXX_EXTERNAL_THREADING_SUPPORT_SOURCES})
363  endif()
364
365  set_target_properties(cxx_external_threads
366    PROPERTIES
367      LINK_FLAGS    "${LIBCXX_LINK_FLAGS}"
368      COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
369      OUTPUT_NAME   "c++external_threads"
370  )
371
372  target_link_libraries(cxx_external_threads PRIVATE cxx-headers)
373endif()
374
375if (LIBCXX_INSTALL_SHARED_LIBRARY)
376  install(TARGETS cxx_shared
377    ARCHIVE DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
378    LIBRARY DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
379    RUNTIME DESTINATION bin COMPONENT cxx)
380endif()
381
382if (LIBCXX_INSTALL_STATIC_LIBRARY)
383  install(TARGETS cxx_static
384    ARCHIVE DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
385    LIBRARY DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
386    RUNTIME DESTINATION bin COMPONENT cxx)
387endif()
388
389if(LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY)
390  install(TARGETS cxx_experimental
391    LIBRARY DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
392    ARCHIVE DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
393    RUNTIME DESTINATION bin COMPONENT cxx)
394endif()
395
396# NOTE: This install command must go after the cxx install command otherwise
397# it will not be executed after the library symlinks are installed.
398if (LIBCXX_ENABLE_SHARED AND LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
399  install(FILES "$<TARGET_LINKER_FILE:cxx_shared>"
400    DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR}
401    COMPONENT libcxx)
402endif()
403
404if (NOT CMAKE_CONFIGURATION_TYPES)
405    if(LIBCXX_INSTALL_LIBRARY)
406      set(lib_install_target cxx)
407    endif()
408    if (LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY)
409      set(experimental_lib_install_target cxx_experimental)
410    endif()
411    if(LIBCXX_INSTALL_HEADERS)
412      set(header_install_target install-cxx-headers)
413    endif()
414    if (LIBCXX_ENABLE_PARALLEL_ALGORITHMS)
415      set(pstl_install_target install-pstl)
416    endif()
417    add_custom_target(install-cxx
418                      DEPENDS ${lib_install_target}
419                              ${experimental_lib_install_target}
420                              ${header_install_target}
421                              ${pstl_install_target}
422                      COMMAND "${CMAKE_COMMAND}"
423                      -DCMAKE_INSTALL_COMPONENT=cxx
424                      -P "${LIBCXX_BINARY_DIR}/cmake_install.cmake")
425    add_custom_target(install-cxx-stripped
426                      DEPENDS ${lib_install_target}
427                              ${experimental_lib_install_target}
428                              ${header_install_target}
429                              ${pstl_install_target}
430                      COMMAND "${CMAKE_COMMAND}"
431                      -DCMAKE_INSTALL_COMPONENT=cxx
432                      -DCMAKE_INSTALL_DO_STRIP=1
433                      -P "${LIBCXX_BINARY_DIR}/cmake_install.cmake")
434endif()
435