xref: /openbsd/gnu/llvm/libcxx/src/CMakeLists.txt (revision 4bdff4be)
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  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  memory_resource.cpp
35  mutex.cpp
36  mutex_destructor.cpp
37  new.cpp
38  optional.cpp
39  random_shuffle.cpp
40  ryu/d2fixed.cpp
41  ryu/d2s.cpp
42  ryu/f2s.cpp
43  shared_mutex.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/new_handler_fallback.ipp
56  support/runtime/stdexcept_default.ipp
57  support/runtime/stdexcept_vcruntime.ipp
58  system_error.cpp
59  thread.cpp
60  typeinfo.cpp
61  utility.cpp
62  valarray.cpp
63  variant.cpp
64  vector.cpp
65  verbose_abort.cpp
66  )
67
68if (LIBCXX_ENABLE_DEBUG_MODE OR LIBCXX_ENABLE_BACKWARDS_COMPATIBILITY_DEBUG_MODE_SYMBOLS)
69  list(APPEND LIBCXX_SOURCES
70    debug.cpp
71    legacy_debug_handler.cpp
72    )
73endif()
74
75if (LIBCXX_ENABLE_RANDOM_DEVICE)
76  list(APPEND LIBCXX_SOURCES
77    random.cpp
78    )
79endif()
80
81if (LIBCXX_ENABLE_LOCALIZATION)
82  list(APPEND LIBCXX_SOURCES
83    include/sso_allocator.h
84    ios.cpp
85    ios.instantiations.cpp
86    iostream.cpp
87    locale.cpp
88    regex.cpp
89    strstream.cpp
90    )
91endif()
92
93if(WIN32)
94  list(APPEND LIBCXX_SOURCES
95    support/win32/locale_win32.cpp
96    support/win32/support.cpp
97    )
98
99  if (NOT LIBCXX_HAS_PTHREAD_API)
100    list(APPEND LIBCXX_SOURCES
101      support/win32/thread_win32.cpp
102      )
103  endif()
104elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "SunOS")
105  list(APPEND LIBCXX_SOURCES
106    support/solaris/mbsnrtowcs.inc
107    support/solaris/wcsnrtombs.inc
108    support/solaris/xlocale.cpp
109    )
110elseif(ZOS)
111  list(APPEND LIBCXX_SOURCES
112    support/ibm/mbsnrtowcs.cpp
113    support/ibm/wcsnrtombs.cpp
114    support/ibm/xlocale_zos.cpp
115    )
116endif()
117
118if (LIBCXX_ENABLE_FILESYSTEM)
119  list(APPEND LIBCXX_SOURCES
120    filesystem/filesystem_common.h
121    filesystem/operations.cpp
122    filesystem/directory_iterator.cpp
123    filesystem/posix_compat.h
124    )
125  # Filesystem uses __int128_t, which requires a definition of __muloi4 when
126  # compiled with UBSAN. This definition is not provided by libgcc_s, but is
127  # provided by compiler-rt. So we need to disable it to avoid having multiple
128  # definitions. See filesystem/int128_builtins.cpp.
129  if (NOT LIBCXX_USE_COMPILER_RT)
130    list(APPEND LIBCXX_SOURCES
131      filesystem/int128_builtins.cpp
132      )
133  endif()
134endif()
135
136# Add all the headers to the project for IDEs.
137if (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()
148endif()
149
150if(NOT LIBCXX_INSTALL_LIBRARY)
151  set(exclude_from_all EXCLUDE_FROM_ALL)
152endif()
153
154if (LIBCXX_GENERATE_COVERAGE AND NOT LIBCXX_COVERAGE_LIBRARY)
155  find_compiler_rt_library(profile LIBCXX_COVERAGE_LIBRARY)
156endif()
157add_library_flags_if(LIBCXX_COVERAGE_LIBRARY "${LIBCXX_COVERAGE_LIBRARY}")
158
159if (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()
183endif()
184
185if (LIBCXX_ENABLE_PARALLEL_ALGORITHMS AND NOT TARGET pstl::ParallelSTL)
186  message(FATAL_ERROR "Could not find ParallelSTL")
187endif()
188
189function(cxx_set_common_defines name)
190  if (LIBCXX_ENABLE_PARALLEL_ALGORITHMS)
191    target_link_libraries(${name} PUBLIC pstl::ParallelSTL)
192  endif()
193endfunction()
194
195split_list(LIBCXX_COMPILE_FLAGS)
196split_list(LIBCXX_LINK_FLAGS)
197
198# Build the shared library.
199if (LIBCXX_ENABLE_SHARED)
200  add_library(cxx_shared SHARED ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
201  target_include_directories(cxx_shared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
202  target_link_libraries(cxx_shared PUBLIC cxx-headers
203                                   PRIVATE ${LIBCXX_LIBRARIES})
204  set_target_properties(cxx_shared
205    PROPERTIES
206      COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
207      LINK_FLAGS    "${LIBCXX_LINK_FLAGS}"
208      OUTPUT_NAME   "${LIBCXX_SHARED_OUTPUT_NAME}"
209      VERSION       "${LIBCXX_LIBRARY_VERSION}"
210      SOVERSION     "${LIBCXX_ABI_VERSION}"
211      DEFINE_SYMBOL ""
212  )
213  cxx_add_common_build_flags(cxx_shared)
214  cxx_set_common_defines(cxx_shared)
215
216  if(ZOS)
217    add_custom_command(TARGET cxx_shared POST_BUILD
218      COMMAND
219        ${LIBCXX_SOURCE_DIR}/utils/zos_rename_dll_side_deck.sh
220        $<TARGET_LINKER_FILE_NAME:cxx_shared> $<TARGET_FILE_NAME:cxx_shared> "${LIBCXX_DLL_NAME}"
221      COMMENT "Rename dll name inside the side deck file"
222      WORKING_DIRECTORY $<TARGET_FILE_DIR:cxx_shared>
223    )
224  endif()
225
226  # Link against libc++abi
227  if (LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY)
228    target_link_libraries(cxx_shared PRIVATE libcxx-abi-shared-objects)
229  else()
230    target_link_libraries(cxx_shared PUBLIC libcxx-abi-shared)
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 STREQUAL "libcxxabi"
237            AND NOT DEFINED LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS
238            AND NOT LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY)
239    set(LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS ON)
240  endif()
241
242  if (LIBCXX_OSX_REEXPORT_LIBCXXABI_SYMBOLS)
243    target_link_libraries(cxx_shared PRIVATE
244      "-Wl,-unexported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/libc++unexp.exp"
245      "-Wl,-reexported_symbols_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/libc++abi.exp"
246      "-Wl,-force_symbols_not_weak_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/notweak.exp"
247      "-Wl,-force_symbols_weak_list,${CMAKE_CURRENT_SOURCE_DIR}/../lib/weak.exp")
248
249    target_link_libraries(cxx_shared PRIVATE $<TARGET_NAME_IF_EXISTS:cxxabi-reexports>)
250  endif()
251
252  # Generate a linker script in place of a libc++.so symlink.
253  if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
254    set(link_libraries)
255
256    set(imported_libname "$<TARGET_PROPERTY:libcxx-abi-shared,IMPORTED_LIBNAME>")
257    set(output_name "$<TARGET_PROPERTY:libcxx-abi-shared,OUTPUT_NAME>")
258    string(APPEND link_libraries "${CMAKE_LINK_LIBRARY_FLAG}$<IF:$<BOOL:${imported_libname}>,${imported_libname},${output_name}>")
259
260    # TODO: Move to the same approach as above for the unwind library
261    if (LIBCXXABI_USE_LLVM_UNWINDER)
262      if (LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY)
263        # libunwind is already included in libc++abi
264      elseif (TARGET unwind_shared OR HAVE_LIBUNWIND)
265        string(APPEND link_libraries " ${CMAKE_LINK_LIBRARY_FLAG}$<TARGET_PROPERTY:unwind_shared,OUTPUT_NAME>")
266      else()
267        string(APPEND link_libraries " ${CMAKE_LINK_LIBRARY_FLAG}unwind")
268      endif()
269    endif()
270
271    set(linker_script "INPUT($<TARGET_SONAME_FILE_NAME:cxx_shared> ${link_libraries})")
272    add_custom_command(TARGET cxx_shared POST_BUILD
273      COMMAND "${CMAKE_COMMAND}" -E remove "$<TARGET_LINKER_FILE:cxx_shared>"
274      COMMAND "${CMAKE_COMMAND}" -E echo "${linker_script}" > "$<TARGET_LINKER_FILE:cxx_shared>"
275      COMMENT "Generating linker script: '${linker_script}' as file $<TARGET_LINKER_FILE:cxx_shared>"
276      VERBATIM
277    )
278  endif()
279
280  list(APPEND LIBCXX_BUILD_TARGETS "cxx_shared")
281  if(WIN32 AND NOT MINGW AND NOT "${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
282    # Since we most likely do not have a mt.exe replacement, disable the
283    # manifest bundling.  This allows a normal cmake invocation to pass which
284    # will attempt to use the manifest tool to generate the bundled manifest
285    set_target_properties(cxx_shared PROPERTIES
286                          APPEND_STRING PROPERTY LINK_FLAGS " /MANIFEST:NO")
287  endif()
288endif()
289
290set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
291
292# Build the static library.
293if (LIBCXX_ENABLE_STATIC)
294  add_library(cxx_static STATIC ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
295  target_include_directories(cxx_static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
296  target_link_libraries(cxx_static PUBLIC cxx-headers
297                                   PRIVATE ${LIBCXX_LIBRARIES}
298                                   PRIVATE libcxx-abi-static)
299  set_target_properties(cxx_static
300    PROPERTIES
301      COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
302      LINK_FLAGS    "${LIBCXX_LINK_FLAGS}"
303      OUTPUT_NAME   "${LIBCXX_STATIC_OUTPUT_NAME}"
304  )
305  cxx_add_common_build_flags(cxx_static)
306  cxx_set_common_defines(cxx_static)
307
308  if (LIBCXX_HERMETIC_STATIC_LIBRARY)
309    # If the hermetic library doesn't define the operator new/delete functions
310    # then its code shouldn't declare them with hidden visibility.  They might
311    # actually be provided by a shared library at link time.
312    if (LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS)
313      append_flags_if_supported(CXX_STATIC_LIBRARY_FLAGS -fvisibility-global-new-delete-hidden)
314    endif()
315    target_compile_options(cxx_static PRIVATE ${CXX_STATIC_LIBRARY_FLAGS})
316    # _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS can be defined in __config_site
317    # too. Define it in the same way here, to avoid redefinition conflicts.
318    target_compile_definitions(cxx_static PRIVATE _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS=)
319  endif()
320
321  list(APPEND LIBCXX_BUILD_TARGETS "cxx_static")
322  # Attempt to merge the libc++.a archive and the ABI library archive into one.
323  if (LIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY)
324    target_link_libraries(cxx_static PRIVATE libcxx-abi-static-objects)
325  endif()
326endif()
327
328# Add a meta-target for both libraries.
329add_custom_target(cxx DEPENDS ${LIBCXX_BUILD_TARGETS})
330
331set(LIBCXX_EXPERIMENTAL_SOURCES
332  experimental/memory_resource.cpp
333  format.cpp
334  )
335
336add_library(cxx_experimental STATIC ${LIBCXX_EXPERIMENTAL_SOURCES})
337target_link_libraries(cxx_experimental PUBLIC cxx-headers)
338if (LIBCXX_ENABLE_SHARED)
339  target_link_libraries(cxx_experimental PRIVATE cxx_shared)
340else()
341  target_link_libraries(cxx_experimental PRIVATE cxx_static)
342endif()
343
344if (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=)
348endif()
349
350set_target_properties(cxx_experimental
351  PROPERTIES
352    COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
353    OUTPUT_NAME   "c++experimental"
354)
355cxx_add_common_build_flags(cxx_experimental)
356target_compile_options(cxx_experimental PUBLIC -D_LIBCPP_ENABLE_EXPERIMENTAL)
357
358
359if (LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY)
360  set(LIBCXX_EXTERNAL_THREADING_SUPPORT_SOURCES
361      "${CMAKE_CURRENT_SOURCE_DIR}/../test/support/external_threads.cpp")
362
363  if (LIBCXX_ENABLE_SHARED)
364    add_library(cxx_external_threads SHARED ${LIBCXX_EXTERNAL_THREADING_SUPPORT_SOURCES})
365  else()
366    add_library(cxx_external_threads STATIC ${LIBCXX_EXTERNAL_THREADING_SUPPORT_SOURCES})
367  endif()
368
369  set_target_properties(cxx_external_threads
370    PROPERTIES
371      LINK_FLAGS    "${LIBCXX_LINK_FLAGS}"
372      COMPILE_FLAGS "${LIBCXX_COMPILE_FLAGS}"
373      OUTPUT_NAME   "c++external_threads"
374  )
375
376  target_link_libraries(cxx_external_threads PRIVATE cxx-headers)
377endif()
378
379if (LIBCXX_INSTALL_SHARED_LIBRARY)
380  install(TARGETS cxx_shared
381    ARCHIVE DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
382    LIBRARY DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
383    RUNTIME DESTINATION ${LIBCXX_INSTALL_RUNTIME_DIR} COMPONENT cxx)
384endif()
385
386if (LIBCXX_INSTALL_STATIC_LIBRARY)
387  install(TARGETS cxx_static
388    ARCHIVE DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
389    LIBRARY DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
390    RUNTIME DESTINATION ${LIBCXX_INSTALL_RUNTIME_DIR} COMPONENT cxx)
391endif()
392
393if (LIBCXX_INSTALL_LIBRARY)
394  install(TARGETS cxx_experimental
395    LIBRARY DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
396    ARCHIVE DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR} COMPONENT cxx
397    RUNTIME DESTINATION ${LIBCXX_INSTALL_RUNTIME_DIR} COMPONENT cxx)
398endif()
399
400# NOTE: This install command must go after the cxx install command otherwise
401# it will not be executed after the library symlinks are installed.
402if (LIBCXX_ENABLE_SHARED AND LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
403  install(FILES "$<TARGET_LINKER_FILE:cxx_shared>"
404    DESTINATION ${LIBCXX_INSTALL_LIBRARY_DIR}
405    COMPONENT libcxx)
406endif()
407
408if (NOT CMAKE_CONFIGURATION_TYPES)
409    if(LIBCXX_INSTALL_LIBRARY)
410      set(lib_install_target "cxx;cxx_experimental")
411    endif()
412    if(LIBCXX_INSTALL_HEADERS)
413      set(header_install_target install-cxx-headers)
414    endif()
415    if (LIBCXX_ENABLE_PARALLEL_ALGORITHMS)
416      set(pstl_install_target install-pstl)
417    endif()
418    add_custom_target(install-cxx
419                      DEPENDS ${lib_install_target}
420                              cxx_experimental
421                              ${header_install_target}
422                              ${pstl_install_target}
423                      COMMAND "${CMAKE_COMMAND}"
424                      -DCMAKE_INSTALL_COMPONENT=cxx
425                      -P "${LIBCXX_BINARY_DIR}/cmake_install.cmake")
426    add_custom_target(install-cxx-stripped
427                      DEPENDS ${lib_install_target}
428                              cxx_experimental
429                              ${header_install_target}
430                              ${pstl_install_target}
431                      COMMAND "${CMAKE_COMMAND}"
432                      -DCMAKE_INSTALL_COMPONENT=cxx
433                      -DCMAKE_INSTALL_DO_STRIP=1
434                      -P "${LIBCXX_BINARY_DIR}/cmake_install.cmake")
435endif()
436