blob: ac5aece3281a4ab6a29bfb0a227985b8e6ca9d32 [file] [log] [blame]
Michael J. Spencerf5799be2010-12-10 19:47:541#===============================================================================
2# Setup Project
3#===============================================================================
Mark de Wevercbaa3592023-05-24 16:12:324cmake_minimum_required(VERSION 3.20.0)
Michael Krusee14f5f22024-05-25 15:41:215set(LLVM_SUBPROJECT_TITLE "libc++")
Michael J. Spencerf5799be2010-12-10 19:47:546
John Ericson949bbd02022-01-01 07:03:317set(LLVM_COMMON_CMAKE_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
8
Michael J. Spencerf5799be2010-12-10 19:47:549# Add path for custom modules
John Ericson949bbd02022-01-01 07:03:3110list(INSERT CMAKE_MODULE_PATH 0
Michael J. Spencerf5799be2010-12-10 19:47:5411 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
12 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
Nikolas Klauser0af67d12023-02-20 15:26:1913 "${CMAKE_CURRENT_SOURCE_DIR}/../runtimes/cmake/Modules"
John Ericson949bbd02022-01-01 07:03:3114 "${LLVM_COMMON_CMAKE_UTILS}"
15 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
Michael J. Spencerf5799be2010-12-10 19:47:5416 )
17
Marek Kurdej5e7a93a2021-01-25 08:50:0318set(CMAKE_FOLDER "libc++")
19
Dominik Montada8c03fdf2020-08-24 09:01:0520set(LIBCXX_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
21set(LIBCXX_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
Dominik Montada8c03fdf2020-08-24 09:01:0522
John Ericsondf31ff12022-01-19 06:45:0723include(GNUInstallDirs)
Nikolas Klausera7aade12023-02-17 10:31:4124include(WarningFlags)
John Ericsondf31ff12022-01-19 06:45:0725
Saleem Abdulrasool163333f2016-02-08 03:50:1826# Require out of source build.
27include(MacroEnsureOutOfSourceBuild)
28MACRO_ENSURE_OUT_OF_SOURCE_BUILD(
29 "${PROJECT_NAME} requires an out of source build. Please create a separate
30 build directory and run 'cmake /path/to/${PROJECT_NAME} [options]' there."
31 )
Eric Fiselier5e0b8fc2018-10-01 01:31:2332if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" AND "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
33 message(STATUS "Configuring for clang-cl")
34 set(LIBCXX_TARGETING_CLANG_CL ON)
35endif()
Saleem Abdulrasool163333f2016-02-08 03:50:1836
Eric Fiseliercc1f65c2017-01-14 06:06:4737if (MSVC)
Eric Fiselier11edd932018-10-01 01:00:1138 message(STATUS "Configuring for MSVC")
Eric Fiseliercc1f65c2017-01-14 06:06:4739endif()
40
Michael J. Spencerf5799be2010-12-10 19:47:5441#===============================================================================
42# Setup CMake Options
43#===============================================================================
Eric Fiselier309a50a2016-09-07 01:15:1044include(CMakeDependentOption)
Eric Fiselier2aeac462017-03-11 03:24:1845include(HandleCompilerRT)
Michael J. Spencerf5799be2010-12-10 19:47:5446
Eric Fiselier10ed6c32015-07-30 22:30:3447# Basic options ---------------------------------------------------------------
Eric Fiselier10ed6c32015-07-30 22:30:3448option(LIBCXX_ENABLE_SHARED "Build libc++ as a shared library." ON)
Petr Hosek9e49a332016-08-08 22:57:2549option(LIBCXX_ENABLE_STATIC "Build libc++ as a static library." ON)
Louis Dionnee5894962024-11-14 12:06:4350option(LIBCXX_ENABLE_EXCEPTIONS "Enable exceptions in the built library." ON)
51option(LIBCXX_ENABLE_RTTI
52 "Use runtime type information.
53 This option may only be set to OFF when LIBCXX_ENABLE_EXCEPTIONS=OFF." ON)
Louis Dionne66a562d2023-05-31 18:20:2454option(LIBCXX_ENABLE_FILESYSTEM
55 "Whether to include support for parts of the library that rely on a filesystem being
56 available on the platform. This includes things like most parts of <filesystem> and
57 others like <fstream>" ON)
Eric Fiselier10ed6c32015-07-30 22:30:3458option(LIBCXX_INCLUDE_TESTS "Build the libc++ tests." ${LLVM_INCLUDE_TESTS})
Konstantin Varlamov64d413e2023-11-08 19:10:0059set(LIBCXX_SUPPORTED_HARDENING_MODES none fast extensive debug)
60set(LIBCXX_HARDENING_MODE "none" CACHE STRING
varconstd1367ca2023-07-12 17:12:5161 "Specify the default hardening mode to use. This mode will be used inside the
62 compiled library and will be the default when compiling user code. Note that
63 users can override this setting in their own code. This does not affect the
64 ABI. Supported values are ${LIBCXX_SUPPORTED_HARDENING_MODES}.")
65if (NOT "${LIBCXX_HARDENING_MODE}" IN_LIST LIBCXX_SUPPORTED_HARDENING_MODES)
66 message(FATAL_ERROR
67 "Unsupported hardening mode: '${LIBCXX_HARDENING_MODE}'. Supported values are ${LIBCXX_SUPPORTED_HARDENING_MODES}.")
68endif()
Konstantin Varlamov8dfc67d2024-01-18 02:56:0769set(LIBCXX_ASSERTION_HANDLER_FILE
Louis Dionne046a1772024-07-24 16:03:5270 "vendor/llvm/default_assertion_handler.in"
Konstantin Varlamov8dfc67d2024-01-18 02:56:0771 CACHE STRING
72 "Specify the path to a header that contains a custom implementation of the
73 assertion handler that gets invoked when a hardening assertion fails. If
74 provided, this header will be included by the library, replacing the
Louis Dionne046a1772024-07-24 16:03:5275 default assertion handler. If this is specified as a relative path, it
76 is assumed to be relative to '<monorepo>/libcxx'.")
77if (NOT IS_ABSOLUTE "${LIBCXX_ASSERTION_HANDLER_FILE}")
78 set(LIBCXX_ASSERTION_HANDLER_FILE "${CMAKE_CURRENT_SOURCE_DIR}/${LIBCXX_ASSERTION_HANDLER_FILE}")
79endif()
Louis Dionnee0d01292020-10-15 14:32:0980option(LIBCXX_ENABLE_RANDOM_DEVICE
81 "Whether to include support for std::random_device in the library. Disabling
82 this can be useful when building the library for platforms that don't have
83 a source of randomness, such as some embedded platforms. When this is not
84 supported, most of <random> will still be available, but std::random_device
85 will not." ON)
Louis Dionne88ffc722020-10-09 19:31:0586option(LIBCXX_ENABLE_LOCALIZATION
87 "Whether to include support for localization in the library. Disabling
88 localization can be useful when porting to platforms that don't support
89 the C locale API (e.g. embedded). When localization is not supported,
90 several parts of the library will be disabled: <iostream>, <regex>, <locale>
91 will be completely unusable, and other parts may be only partly available." ON)
Mark de Weverdf2af992021-05-25 18:11:0892option(LIBCXX_ENABLE_UNICODE
93 "Whether to include support for Unicode in the library. Disabling Unicode can
Louis Dionnecf765b12021-09-09 15:14:3394 be useful when porting to platforms that don't support UTF-8 encoding (e.g.
95 embedded)." ON)
Joseph Huber7ad7f8f2024-08-21 13:48:5396option(LIBCXX_HAS_TERMINAL_AVAILABLE
97 "Build libc++ with support for checking whether a stream is a terminal." ON)
Louis Dionnef4c12582021-08-23 19:32:3698option(LIBCXX_ENABLE_WIDE_CHARACTERS
99 "Whether to include support for wide characters in the library. Disabling
100 wide character support can be useful when porting to platforms that don't
101 support the C functionality for wide characters. When wide characters are
102 not supported, several parts of the library will be disabled, notably the
103 wide character specializations of std::basic_string." ON)
Louis Dionnee5894962024-11-14 12:06:43104option(LIBCXX_ENABLE_THREADS "Build libc++ with support for threads." ON)
105option(LIBCXX_ENABLE_MONOTONIC_CLOCK
106 "Build libc++ with support for a monotonic clock.
107 This option may only be set to OFF when LIBCXX_ENABLE_THREADS=OFF." ON)
Mark de Weverf78f93b2022-09-23 16:33:20108
109# To use time zone support in libc++ the platform needs to have the IANA
110# database installed. Libc++ will fail to build if this is enabled on a
111# platform that does not provide the IANA database. The default is set to the
112# current implementation state on the different platforms.
113#
114# TODO TZDB make the default always ON when most platforms ship with the IANA
115# database.
116if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
117 set(ENABLE_TIME_ZONE_DATABASE_DEFAULT ON)
118else()
119 set(ENABLE_TIME_ZONE_DATABASE_DEFAULT OFF)
120endif()
121option(LIBCXX_ENABLE_TIME_ZONE_DATABASE
122 "Whether to include support for time zones in the library. Disabling
123 time zone support can be useful when porting to platforms that don't
124 ship the IANA time zone database. When time zones are not supported,
125 time zone support in <chrono> will be disabled." ${ENABLE_TIME_ZONE_DATABASE_DEFAULT})
Louis Dionne2eadbc82020-11-04 20:01:25126option(LIBCXX_ENABLE_VENDOR_AVAILABILITY_ANNOTATIONS
127 "Whether to turn on vendor availability annotations on declarations that depend
128 on definitions in a shared library. By default, we assume that we're not building
129 libc++ for any specific vendor, and we disable those annotations. Vendors wishing
130 to provide compile-time errors when using features unavailable on some version of
Louis Dionne04f01a22024-05-29 01:29:11131 the shared library they shipped should turn this on and see `include/__configuration/availability.h`
Louis Dionne2eadbc82020-11-04 20:01:25132 for more details." OFF)
Louis Dionned0af4272022-03-14 18:23:38133
134if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
135 set(LIBCXX_DEFAULT_TEST_CONFIG "llvm-libc++-shared-gcc.cfg.in")
136elseif(MINGW)
Martin Storsjö9fb23782024-10-10 06:13:47137 set(LIBCXX_DEFAULT_TEST_CONFIG "llvm-libc++-mingw.cfg.in")
Louis Dionned0af4272022-03-14 18:23:38138elseif(WIN32) # clang-cl
139 if (LIBCXX_ENABLE_SHARED)
140 set(LIBCXX_DEFAULT_TEST_CONFIG "llvm-libc++-shared-clangcl.cfg.in")
141 else()
142 set(LIBCXX_DEFAULT_TEST_CONFIG "llvm-libc++-static-clangcl.cfg.in")
143 endif()
144else()
145 if (LIBCXX_ENABLE_SHARED)
146 set(LIBCXX_DEFAULT_TEST_CONFIG "llvm-libc++-shared.cfg.in")
147 else()
148 set(LIBCXX_DEFAULT_TEST_CONFIG "llvm-libc++-static.cfg.in")
149 endif()
150endif()
151set(LIBCXX_TEST_CONFIG "${LIBCXX_DEFAULT_TEST_CONFIG}" CACHE STRING
Louis Dionne54a8a0d2021-09-29 19:26:05152 "The path to the Lit testing configuration to use when running the tests.
153 If a relative path is provided, it is assumed to be relative to '<monorepo>/libcxx/test/configs'.")
154if (NOT IS_ABSOLUTE "${LIBCXX_TEST_CONFIG}")
155 set(LIBCXX_TEST_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/test/configs/${LIBCXX_TEST_CONFIG}")
156endif()
Louis Dionned0af4272022-03-14 18:23:38157message(STATUS "Using libc++ testing configuration: ${LIBCXX_TEST_CONFIG}")
Louis Dionne6f693182020-07-09 15:54:09158set(LIBCXX_TEST_PARAMS "" CACHE STRING
159 "A list of parameters to run the Lit test suite with.")
Eric Fiselier3aa54782016-10-30 22:53:00160
Louis Dionnee236a522024-11-07 14:07:50161# TODO: Figure out how to build GoogleBenchmark on those platforms, and how to build when exceptions or RTTI is disabled
162if (WIN32 OR MINGW OR ANDROID OR ${CMAKE_SYSTEM_NAME} MATCHES "AIX"
163 OR NOT LIBCXX_ENABLE_LOCALIZATION
164 OR NOT LIBCXX_ENABLE_THREADS
165 OR NOT LIBCXX_ENABLE_FILESYSTEM
166 OR NOT LIBCXX_ENABLE_RANDOM_DEVICE
167 OR NOT LIBCXX_ENABLE_EXCEPTIONS
168 OR NOT LIBCXX_ENABLE_RTTI)
169 set(_include_benchmarks OFF)
170else()
171 set(_include_benchmarks ON)
172endif()
173option(LIBCXX_INCLUDE_BENCHMARKS "Build the libc++ benchmarks and their dependencies" ${_include_benchmarks})
Eric Fiselier336a1a62018-11-14 20:38:46174
Eric Fiselierb17bb062015-08-22 19:40:49175option(LIBCXX_INCLUDE_DOCS "Build the libc++ documentation." ${LLVM_INCLUDE_DOCS})
John Ericsone941b032022-08-19 02:44:46176set(LIBCXX_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}" CACHE STRING
177 "Define suffix of library directory name (32/64)")
Eric Fiselier10ed6c32015-07-30 22:30:34178option(LIBCXX_INSTALL_HEADERS "Install the libc++ headers." ON)
Eric Fiselierd77135f2015-08-26 20:18:21179option(LIBCXX_INSTALL_LIBRARY "Install the libc++ library." ON)
Mark de Wever8b47bb62024-01-21 11:16:22180option(LIBCXX_INSTALL_MODULES
Mark de Wever19d2d3f2024-04-28 12:12:27181 "Install the libc++ C++20 module source files (experimental)." ON
Mark de Wever8b47bb62024-01-21 11:16:22182)
Petr Hosek4a1e14e2018-07-24 23:27:51183cmake_dependent_option(LIBCXX_INSTALL_STATIC_LIBRARY
184 "Install the static libc++ library." ON
185 "LIBCXX_ENABLE_STATIC;LIBCXX_INSTALL_LIBRARY" OFF)
186cmake_dependent_option(LIBCXX_INSTALL_SHARED_LIBRARY
187 "Install the shared libc++ library." ON
188 "LIBCXX_ENABLE_SHARED;LIBCXX_INSTALL_LIBRARY" OFF)
Eric Fiselier998a5c82018-07-27 03:07:09189
Louis Dionne817d8972022-02-07 19:52:17190option(LIBCXX_ABI_UNSTABLE "Use the unstable ABI of libc++. This is equivalent to specifying LIBCXX_ABI_VERSION=n, where n is the not-yet-stable version." OFF)
191if (LIBCXX_ABI_UNSTABLE)
192 set(abi_version "2")
193else()
194 set(abi_version "1")
195endif()
Louis Dionne6de59ca2022-05-16 13:50:56196set(LIBCXX_ABI_VERSION "${abi_version}" CACHE STRING
197 "ABI version of libc++. Can be either 1 or 2, where 2 is currently the unstable ABI.
198 Defaults to 1 unless LIBCXX_ABI_UNSTABLE is specified, in which case this is 2.")
199set(LIBCXX_LIBRARY_VERSION "${LIBCXX_ABI_VERSION}.0" CACHE STRING
200 "Version of libc++. This will be reflected in the name of the shared library produced.
201 For example, -DLIBCXX_LIBRARY_VERSION=x.y will result in the library being named
202 libc++.x.y.dylib, along with the usual symlinks pointing to that. On Apple platforms,
203 this also controls the linker's 'current_version' property.")
Louis Dionne817d8972022-02-07 19:52:17204set(LIBCXX_ABI_NAMESPACE "__${LIBCXX_ABI_VERSION}" CACHE STRING "The inline ABI namespace used by libc++. It defaults to __n where `n` is the current ABI version.")
205if (NOT LIBCXX_ABI_NAMESPACE MATCHES "__.*")
Florian Mayer4110c2c2022-06-09 15:49:03206 message(FATAL_ERROR "LIBCXX_ABI_NAMESPACE must be a reserved identifier, got '${LIBCXX_ABI_NAMESPACE}'.")
Louis Dionne817d8972022-02-07 19:52:17207endif()
Shoaib Meenaia4a3d402017-10-05 02:18:08208option(LIBCXX_ABI_FORCE_ITANIUM "Ignore auto-detection and force use of the Itanium ABI.")
209option(LIBCXX_ABI_FORCE_MICROSOFT "Ignore auto-detection and force use of the Microsoft ABI.")
Eric Fiselier2405bd62019-05-29 02:21:37210
Louis Dionnebe00e882020-11-16 23:13:43211set(LIBCXX_TYPEINFO_COMPARISON_IMPLEMENTATION "default" CACHE STRING
212 "Override the implementation to use for comparing typeinfos. By default, this
213 is detected automatically by the library, but this option allows overriding
214 which implementation is used unconditionally.
Eric Fiselier2405bd62019-05-29 02:21:37215
Louis Dionnebe00e882020-11-16 23:13:43216 See the documentation in <libcxx/include/typeinfo> for details on what each
217 value means.")
218set(TYPEINFO_COMPARISON_VALUES "default;1;2;3")
219if (NOT ("${LIBCXX_TYPEINFO_COMPARISON_IMPLEMENTATION}" IN_LIST TYPEINFO_COMPARISON_VALUES))
Louis Dionned0fcdcd2020-05-15 19:58:19220 message(FATAL_ERROR "Value '${LIBCXX_TYPEINFO_COMPARISON_IMPLEMENTATION}' is not a valid value for
Louis Dionnebe00e882020-11-16 23:13:43221 LIBCXX_TYPEINFO_COMPARISON_IMPLEMENTATION")
Eric Fiselier2405bd62019-05-29 02:21:37222endif()
223
Shoaib Meenai89937532017-10-04 23:51:57224set(LIBCXX_ABI_DEFINES "" CACHE STRING "A semicolon separated list of ABI macros to define in the site config header.")
Louis Dionne760261a2023-08-14 22:14:02225set(LIBCXX_EXTRA_SITE_DEFINES "" CACHE STRING "Extra defines to add into __config_site")
Saleem Abdulrasoole81fcb82016-08-24 04:22:52226option(LIBCXX_USE_COMPILER_RT "Use compiler-rt instead of libgcc" OFF)
Eric Fiselier10ed6c32015-07-30 22:30:34227
Petr Hosek3b78dfa2024-07-19 05:17:42228# C Library options -----------------------------------------------------------
229
230set(LIBCXX_SUPPORTED_C_LIBRARIES system llvm-libc)
231set(LIBCXX_LIBC "system" CACHE STRING "Specify C library to use. Supported values are ${LIBCXX_SUPPORTED_C_LIBRARIES}.")
232if (NOT "${LIBCXX_LIBC}" IN_LIST LIBCXX_SUPPORTED_C_LIBRARIES)
Raul Tambre58d4ebb2025-05-06 20:07:49233 message(FATAL_ERROR "Unsupported C library: '${LIBCXX_LIBC}'. Supported values are ${LIBCXX_SUPPORTED_C_LIBRARIES}.")
Petr Hosek3b78dfa2024-07-19 05:17:42234endif()
235
Eric Fiselier10ed6c32015-07-30 22:30:34236# ABI Library options ---------------------------------------------------------
David Spickett43cfc782023-07-07 09:55:36237if (MSVC)
Louis Dionnea80e65e2022-03-01 13:42:13238 set(LIBCXX_DEFAULT_ABI_LIBRARY "vcruntime")
Eugene Zelenko772d1142016-08-08 18:01:50239else()
Louis Dionnea80e65e2022-03-01 13:42:13240 set(LIBCXX_DEFAULT_ABI_LIBRARY "libcxxabi")
241endif()
242
243set(LIBCXX_SUPPORTED_ABI_LIBRARIES none libcxxabi system-libcxxabi libcxxrt libstdc++ libsupc++ vcruntime)
244set(LIBCXX_CXX_ABI "${LIBCXX_DEFAULT_ABI_LIBRARY}" CACHE STRING "Specify C++ ABI library to use. Supported values are ${LIBCXX_SUPPORTED_ABI_LIBRARIES}.")
Louis Dionnea80e65e2022-03-01 13:42:13245if (NOT "${LIBCXX_CXX_ABI}" IN_LIST LIBCXX_SUPPORTED_ABI_LIBRARIES)
246 message(FATAL_ERROR "Unsupported C++ ABI library: '${LIBCXX_CXX_ABI}'. Supported values are ${LIBCXX_SUPPORTED_ABI_LIBRARIES}.")
Eugene Zelenko772d1142016-08-08 18:01:50247endif()
Eric Fiselierbb906852015-10-22 20:54:27248
Louis Dionne0c962cb2019-03-18 18:18:01249option(LIBCXX_ENABLE_STATIC_ABI_LIBRARY
250 "Use a static copy of the ABI library when linking libc++.
251 This option cannot be used with LIBCXX_ENABLE_ABI_LINKER_SCRIPT." OFF)
Eric Fiselier10ed6c32015-07-30 22:30:34252
Nikita Popove911ece2022-09-26 13:42:35253option(LIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY
254 "Statically link the ABI library to static library"
255 ${LIBCXX_ENABLE_STATIC_ABI_LIBRARY})
Petr Hosek058c04c2018-07-24 07:06:17256
Nikita Popove911ece2022-09-26 13:42:35257option(LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY
258 "Statically link the ABI library to shared library"
259 ${LIBCXX_ENABLE_STATIC_ABI_LIBRARY})
Petr Hosek058c04c2018-07-24 07:06:17260
Eric Fiselier82414052015-10-14 19:54:03261# Generate and install a linker script inplace of libc++.so. The linker script
Eric Fiselier1ab69fc2015-10-15 22:41:51262# will link libc++ to the correct ABI library. This option is on by default
Haowei1f8f9e32023-12-01 20:37:04263# on UNIX platforms other than Apple, and on the Fuchsia platform unless we
264# statically link libc++abi inside libc++.so, we don't build libc++.so at all
265# or we don't have any ABI library.
Louis Dionne4b1fe092023-11-23 15:15:03266if (LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY
267 OR NOT LIBCXX_ENABLE_SHARED
268 OR LIBCXX_CXX_ABI STREQUAL "none")
269 set(ENABLE_LINKER_SCRIPT_DEFAULT_VALUE OFF)
Haowei1f8f9e32023-12-01 20:37:04270elseif((UNIX OR FUCHSIA) AND NOT APPLE)
Louis Dionne4b1fe092023-11-23 15:15:03271 set(ENABLE_LINKER_SCRIPT_DEFAULT_VALUE ON)
272else()
273 set(ENABLE_LINKER_SCRIPT_DEFAULT_VALUE OFF)
Eric Fiselier1ab69fc2015-10-15 22:41:51274endif()
Eric Fiselier82414052015-10-14 19:54:03275option(LIBCXX_ENABLE_ABI_LINKER_SCRIPT
Eric Fiselier1ab69fc2015-10-15 22:41:51276 "Use and install a linker script for the given ABI library"
Eric Fiseliera15785b2015-10-15 23:04:54277 ${ENABLE_LINKER_SCRIPT_DEFAULT_VALUE})
Eric Fiselier82414052015-10-14 19:54:03278
Eric Fiselier34992632017-03-02 19:35:33279option(LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS
Louis Dionne9b40ee82019-10-01 13:34:58280 "Build libc++ with definitions for operator new/delete. These are normally
281 defined in libc++abi, but this option can be used to define them in libc++
282 instead. If you define them in libc++, make sure they are NOT defined in
283 libc++abi. Doing otherwise is an ODR violation." OFF)
Eric Fiselier10ed6c32015-07-30 22:30:34284# Build libc++abi with libunwind. We need this option to determine whether to
285# link with libunwind or libgcc_s while running the test cases.
Louis Dionne8f90e692024-01-11 15:13:21286option(LIBCXXABI_USE_LLVM_UNWINDER "Build and use the LLVM unwinder." ON)
Eric Fiselier10ed6c32015-07-30 22:30:34287
288# Target options --------------------------------------------------------------
Louis Dionnefa1c0772021-11-23 21:34:09289option(LIBCXX_BUILD_32_BITS "Build 32 bit multilib libc++. This option is not supported anymore when building the runtimes. Please specify a full triple instead." ${LLVM_BUILD_32_BITS})
290if (LIBCXX_BUILD_32_BITS)
Louis Dionnedc1244d2021-12-01 17:57:30291 message(FATAL_ERROR "LIBCXX_BUILD_32_BITS is not supported anymore when building the runtimes, please specify a full triple instead.")
Louis Dionnefa1c0772021-11-23 21:34:09292endif()
Louis Dionne72117f22021-10-12 19:59:08293
Eric Fiselier10ed6c32015-07-30 22:30:34294# Feature options -------------------------------------------------------------
Vasileios Kalintiris8c58e922015-11-09 10:21:04295option(LIBCXX_HAS_MUSL_LIBC "Build libc++ with support for the Musl C library" OFF)
Ben Craigb9599b12016-05-25 17:40:09296option(LIBCXX_HAS_PTHREAD_API "Ignore auto-detection and force use of pthread API" OFF)
Martin Storsjobf02a092018-01-05 20:48:29297option(LIBCXX_HAS_WIN32_THREAD_API "Ignore auto-detection and force use of win32 thread API" OFF)
Asiri Rathnayake8c2bf452016-09-11 21:46:40298option(LIBCXX_HAS_EXTERNAL_THREAD_API
299 "Build libc++ with an externalized threading API.
300 This option may only be set to ON when LIBCXX_ENABLE_THREADS=ON." OFF)
Eric Fiselier10ed6c32015-07-30 22:30:34301
Nikolas Klausere837f4b2023-05-24 22:33:38302if (LIBCXX_ENABLE_THREADS)
Louis Dionned423d802024-04-17 17:36:53303 set(LIBCXX_PSTL_BACKEND "std_thread" CACHE STRING "Which PSTL backend to use")
Nikolas Klausere837f4b2023-05-24 22:33:38304else()
Louis Dionned423d802024-04-17 17:36:53305 set(LIBCXX_PSTL_BACKEND "serial" CACHE STRING "Which PSTL backend to use")
Nikolas Klausere837f4b2023-05-24 22:33:38306endif()
307
Eric Fiselier10ed6c32015-07-30 22:30:34308# Misc options ----------------------------------------------------------------
Eric Fiselier6fb770a2015-10-10 03:34:52309# FIXME: Turn -pedantic back ON. It is currently off because it warns
310# about #include_next which is used everywhere.
311option(LIBCXX_ENABLE_PEDANTIC "Compile with pedantic enabled." OFF)
Eric Fiselier10ed6c32015-07-30 22:30:34312option(LIBCXX_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
313
Martin Storsjöbedf6572022-04-09 20:40:07314set(LIBCXX_HERMETIC_STATIC_LIBRARY_DEFAULT OFF)
315if (WIN32)
316 set(LIBCXX_HERMETIC_STATIC_LIBRARY_DEFAULT ON)
317endif()
Petr Hoseka2685cd2019-01-06 06:14:31318option(LIBCXX_HERMETIC_STATIC_LIBRARY
Martin Storsjöbedf6572022-04-09 20:40:07319 "Do not export any symbols from the static library." ${LIBCXX_HERMETIC_STATIC_LIBRARY_DEFAULT})
Petr Hoseka2685cd2019-01-06 06:14:31320
Eric Fiselier10ed6c32015-07-30 22:30:34321#===============================================================================
322# Check option configurations
323#===============================================================================
324
325# Ensure LIBCXX_ENABLE_MONOTONIC_CLOCK is set to ON only when
326# LIBCXX_ENABLE_THREADS is on.
327if(LIBCXX_ENABLE_THREADS AND NOT LIBCXX_ENABLE_MONOTONIC_CLOCK)
328 message(FATAL_ERROR "LIBCXX_ENABLE_MONOTONIC_CLOCK can only be set to OFF"
329 " when LIBCXX_ENABLE_THREADS is also set to OFF.")
Eric Fiselier0058c802014-08-18 05:03:46330endif()
Michael J. Spencerf5799be2010-12-10 19:47:54331
Asiri Rathnayake205d7d32017-01-03 12:59:50332if(NOT LIBCXX_ENABLE_THREADS)
333 if(LIBCXX_HAS_PTHREAD_API)
334 message(FATAL_ERROR "LIBCXX_HAS_PTHREAD_API can only be set to ON"
335 " when LIBCXX_ENABLE_THREADS is also set to ON.")
336 endif()
337 if(LIBCXX_HAS_EXTERNAL_THREAD_API)
338 message(FATAL_ERROR "LIBCXX_HAS_EXTERNAL_THREAD_API can only be set to ON"
339 " when LIBCXX_ENABLE_THREADS is also set to ON.")
340 endif()
Martin Storsjobf02a092018-01-05 20:48:29341 if (LIBCXX_HAS_WIN32_THREAD_API)
342 message(FATAL_ERROR "LIBCXX_HAS_WIN32_THREAD_API can only be set to ON"
343 " when LIBCXX_ENABLE_THREADS is also set to ON.")
344 endif()
Eric Fiselier00f6bea2017-01-06 20:05:40345
346endif()
347
Asiri Rathnayakeb71c8992017-01-09 10:38:56348if (LIBCXX_HAS_EXTERNAL_THREAD_API)
Asiri Rathnayakeb71c8992017-01-09 10:38:56349 if (LIBCXX_HAS_PTHREAD_API)
350 message(FATAL_ERROR "The options LIBCXX_HAS_EXTERNAL_THREAD_API"
Mark de Wever0735a6e2023-09-06 18:54:43351 " and LIBCXX_HAS_PTHREAD_API cannot be both"
352 " set to ON at the same time.")
Asiri Rathnayakeb71c8992017-01-09 10:38:56353 endif()
Martin Storsjobf02a092018-01-05 20:48:29354 if (LIBCXX_HAS_WIN32_THREAD_API)
355 message(FATAL_ERROR "The options LIBCXX_HAS_EXTERNAL_THREAD_API"
Mark de Wever0735a6e2023-09-06 18:54:43356 " and LIBCXX_HAS_WIN32_THREAD_API cannot be both"
357 " set to ON at the same time.")
Martin Storsjobf02a092018-01-05 20:48:29358 endif()
359endif()
360
361if (LIBCXX_HAS_PTHREAD_API)
362 if (LIBCXX_HAS_WIN32_THREAD_API)
363 message(FATAL_ERROR "The options LIBCXX_HAS_PTHREAD_API"
Mark de Wever0735a6e2023-09-06 18:54:43364 " and LIBCXX_HAS_WIN32_THREAD_API cannot be both"
365 " set to ON at the same time.")
Martin Storsjobf02a092018-01-05 20:48:29366 endif()
Asiri Rathnayake8c2bf452016-09-11 21:46:40367endif()
368
Mark de Wevered210f92023-12-12 16:11:53369if (NOT LIBCXX_ENABLE_RTTI AND LIBCXX_ENABLE_EXCEPTIONS)
370 message(FATAL_ERROR "Libc++ cannot be built with exceptions enabled but RTTI"
371 " disabled, since that configuration is broken. See"
372 " https://github.com/llvm/llvm-project/issues/66117"
373 " for details.")
374endif()
375
Eric Fiselier82414052015-10-14 19:54:03376if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
377 if (APPLE)
378 message(FATAL_ERROR "LIBCXX_ENABLE_ABI_LINKER_SCRIPT cannot be used on APPLE targets")
379 endif()
Asiri Rathnayake28383a42016-05-14 23:58:11380 if (NOT LIBCXX_ENABLE_SHARED)
381 message(FATAL_ERROR "LIBCXX_ENABLE_ABI_LINKER_SCRIPT is only available for shared library builds.")
382 endif()
Eric Fiselier82414052015-10-14 19:54:03383endif()
384
Petr Hosek4b59ed42018-07-26 05:10:24385if (LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY AND LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
Eric Fiselier82414052015-10-14 19:54:03386 message(FATAL_ERROR "Conflicting options given.
Nikita Popove911ece2022-09-26 13:42:35387 LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY cannot be specified with
Eric Fiselier82414052015-10-14 19:54:03388 LIBCXX_ENABLE_ABI_LINKER_SCRIPT")
389endif()
390
Shoaib Meenaia4a3d402017-10-05 02:18:08391if (LIBCXX_ABI_FORCE_ITANIUM AND LIBCXX_ABI_FORCE_MICROSOFT)
392 message(FATAL_ERROR "Only one of LIBCXX_ABI_FORCE_ITANIUM and LIBCXX_ABI_FORCE_MICROSOFT can be specified.")
Shoaib Meenaid4563852017-10-04 23:44:38393endif ()
394
Martin Storsjöe777e442023-07-17 09:52:47395if (LIBCXX_ENABLE_SHARED AND CMAKE_MSVC_RUNTIME_LIBRARY AND
396 (NOT CMAKE_MSVC_RUNTIME_LIBRARY MATCHES "DLL$"))
397 message(WARNING "A static CRT linked into a shared libc++ doesn't work correctly.")
398endif()
399
Michael J. Spencerf5799be2010-12-10 19:47:54400#===============================================================================
401# Configure System
402#===============================================================================
403
Louis Dionnec06a8f92020-06-26 16:08:59404# TODO: Projects that depend on libc++ should use LIBCXX_GENERATED_INCLUDE_DIR
405# instead of hard-coding include/c++/v1.
John Ericson0a8d15a2022-01-11 03:03:21406
Louis Dionne760261a2023-08-14 22:14:02407set(LIBCXX_INSTALL_INCLUDE_DIR "${CMAKE_INSTALL_INCLUDEDIR}/c++/v1" CACHE STRING
John Ericson0a8d15a2022-01-11 03:03:21408 "Path where target-agnostic libc++ headers should be installed.")
Louis Dionne760261a2023-08-14 22:14:02409set(LIBCXX_INSTALL_RUNTIME_DIR "${CMAKE_INSTALL_BINDIR}" CACHE STRING
John Ericson0a8d15a2022-01-11 03:03:21410 "Path where built libc++ runtime libraries should be installed.")
Mark de Wever8b47bb62024-01-21 11:16:22411set(LIBCXX_INSTALL_MODULES_DIR "share/libc++/v1" CACHE STRING
412 "Path where target-agnostic libc++ module source files should be installed.")
John Ericson0a8d15a2022-01-11 03:03:21413
Louis Dionne10378b32022-10-11 14:05:56414set(LIBCXX_SHARED_OUTPUT_NAME "c++" CACHE STRING "Output name for the shared libc++ runtime library.")
415set(LIBCXX_STATIC_OUTPUT_NAME "c++" CACHE STRING "Output name for the static libc++ runtime library.")
Zibi Sarbinowski36dde912022-10-03 21:41:58416
Petr Hosek887f26d2018-06-28 03:11:52417if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
Petr Hoseked155f32024-05-31 18:48:45418 set(LIBCXX_TARGET_SUBDIR ${LLVM_DEFAULT_TARGET_TRIPLE})
419 if(LIBCXX_LIBDIR_SUBDIR)
420 string(APPEND LIBCXX_TARGET_SUBDIR /${LIBCXX_LIBDIR_SUBDIR})
421 endif()
422 set(LIBCXX_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LIBCXX_TARGET_SUBDIR})
Kirill Stoimenov576b5382025-01-14 18:23:07423 set(LIBCXX_GENERATED_INCLUDE_DIR "${LLVM_BINARY_DIR}/include/c++/v1")
424 set(LIBCXX_GENERATED_MODULE_DIR "${LLVM_BINARY_DIR}/modules/c++/v1")
425 set(LIBCXX_GENERATED_INCLUDE_TARGET_DIR "${LLVM_BINARY_DIR}/include/${LIBCXX_TARGET_SUBDIR}/c++/v1")
Petr Hoseked155f32024-05-31 18:48:45426 set(LIBCXX_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LIBCXX_TARGET_SUBDIR} CACHE STRING
John Ericson1e03c372021-04-28 22:36:47427 "Path where built libc++ libraries should be installed.")
Petr Hoseked155f32024-05-31 18:48:45428 set(LIBCXX_INSTALL_INCLUDE_TARGET_DIR "${CMAKE_INSTALL_INCLUDEDIR}/${LIBCXX_TARGET_SUBDIR}/c++/v1" CACHE STRING
John Ericson1e03c372021-04-28 22:36:47429 "Path where target-specific libc++ headers should be installed.")
Petr Hoseked155f32024-05-31 18:48:45430 unset(LIBCXX_TARGET_SUBDIR)
Jonas Hahnfeld98a7f272017-05-04 06:02:50431else()
John Ericson0a8d15a2022-01-11 03:03:21432 if(LLVM_LIBRARY_OUTPUT_INTDIR)
433 set(LIBCXX_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
Kirill Stoimenov576b5382025-01-14 18:23:07434 set(LIBCXX_GENERATED_INCLUDE_DIR "${LLVM_BINARY_DIR}/include/c++/v1")
435 set(LIBCXX_GENERATED_MODULE_DIR "${LLVM_BINARY_DIR}/modules/c++/v1")
John Ericson0a8d15a2022-01-11 03:03:21436 else()
John Ericsone941b032022-08-19 02:44:46437 set(LIBCXX_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBCXX_LIBDIR_SUFFIX})
Kirill Stoimenov576b5382025-01-14 18:23:07438 set(LIBCXX_GENERATED_INCLUDE_DIR "${CMAKE_BINARY_DIR}/include/c++/v1")
439 set(LIBCXX_GENERATED_MODULE_DIR "${CMAKE_BINARY_DIR}/modules/c++/v1")
John Ericson0a8d15a2022-01-11 03:03:21440 endif()
Petr Hosekea12d772020-07-15 21:10:56441 set(LIBCXX_GENERATED_INCLUDE_TARGET_DIR "${LIBCXX_GENERATED_INCLUDE_DIR}")
Louis Dionne760261a2023-08-14 22:14:02442 set(LIBCXX_INSTALL_LIBRARY_DIR lib${LIBCXX_LIBDIR_SUFFIX} CACHE STRING
John Ericson1e03c372021-04-28 22:36:47443 "Path where built libc++ libraries should be installed.")
Louis Dionne760261a2023-08-14 22:14:02444 set(LIBCXX_INSTALL_INCLUDE_TARGET_DIR "${LIBCXX_INSTALL_INCLUDE_DIR}" CACHE STRING
John Ericson1e03c372021-04-28 22:36:47445 "Path where target-specific libc++ headers should be installed.")
Jonas Hahnfeld98a7f272017-05-04 06:02:50446endif()
Petr Hosek887f26d2018-06-28 03:11:52447
Eric Fiselier10ed6c32015-07-30 22:30:34448set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBCXX_LIBRARY_DIR})
449set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBCXX_LIBRARY_DIR})
Shoaib Meenai61707652017-04-13 16:27:38450set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LIBCXX_LIBRARY_DIR})
Eric Fiselier10ed6c32015-07-30 22:30:34451
Eric Fiselier5aedca92014-11-15 06:26:30452# Declare libc++ configuration variables.
453# They are intended for use as follows:
454# LIBCXX_CXX_FLAGS: General flags for both the compiler and linker.
455# LIBCXX_COMPILE_FLAGS: Compile only flags.
456# LIBCXX_LINK_FLAGS: Linker only flags.
Eric Fiselier054fc4c2016-10-09 21:34:03457# LIBCXX_LIBRARIES: libraries libc++ is linked to.
Eric Fiselier5aedca92014-11-15 06:26:30458set(LIBCXX_COMPILE_FLAGS "")
459set(LIBCXX_LINK_FLAGS "")
Eric Fiselier10ed6c32015-07-30 22:30:34460set(LIBCXX_LIBRARIES "")
Zibi Sarbinowski36dde912022-10-03 21:41:58461set(LIBCXX_ADDITIONAL_COMPILE_FLAGS "" CACHE STRING
Louis Dionnee6744242024-10-17 20:17:40462 "Additional compile flags to use when building libc++. This should be a CMake ;-delimited list of individual
463 compiler options to use. For options that must be passed as-is to the compiler without deduplication (e.g.
464 `-Xclang -foo` option groups), consider using `SHELL:` (https://cmake.org/cmake/help/latest/command/add_compile_options.html#option-de-duplication).")
Zibi Sarbinowski36dde912022-10-03 21:41:58465set(LIBCXX_ADDITIONAL_LIBRARIES "" CACHE STRING
466 "Additional libraries libc++ is linked to which can be provided in cache")
Eric Fiselier5aedca92014-11-15 06:26:30467
Eric Fiseliera4f24602016-06-02 01:10:08468# Include macros for adding and removing libc++ flags.
469include(HandleLibcxxFlags)
470
471# Target flags ================================================================
472# These flags get added to CMAKE_CXX_FLAGS and CMAKE_C_FLAGS so that
473# 'config-ix' use them during feature checks. It also adds them to both
474# 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'
Petr Hosek6fd4e7f2019-02-04 20:02:26475
Jake Egan1cf41132022-06-14 01:44:58476if (${CMAKE_SYSTEM_NAME} MATCHES "AIX")
Nikolas Klauserd0e95fe2023-02-20 15:47:34477 add_flags_if_supported("-mdefault-visibility-export-mapping=explicit")
Jake Egan1cf41132022-06-14 01:44:58478 set(CMAKE_AIX_EXPORT_ALL_SYMBOLS OFF)
479endif()
480
Eric Fiselier382338d2014-11-15 17:25:23481# Configure compiler.
482include(config-ix)
Eric Fiselier10ed6c32015-07-30 22:30:34483
Eric Fiselier382338d2014-11-15 17:25:23484#===============================================================================
485# Setup Compiler Flags
486#===============================================================================
487
Petr Hosek3b78dfa2024-07-19 05:17:42488include(HandleLibC) # Setup the C library flags
JF Bastienfc45f322016-03-05 14:22:02489include(HandleLibCXXABI) # Setup the ABI library flags
Eric Fiselier10ed6c32015-07-30 22:30:34490
Shoaib Meenaif88923d2017-03-25 03:42:20491# FIXME(EricWF): See the FIXME on LIBCXX_ENABLE_PEDANTIC.
Eric Fiselierf2be7c22015-10-15 20:27:15492# Remove the -pedantic flag and -Wno-pedantic and -pedantic-errors
Shoaib Meenaif88923d2017-03-25 03:42:20493# so they don't get transformed into -Wno and -errors respectively.
Eric Fiselierf2be7c22015-10-15 20:27:15494remove_flags(-Wno-pedantic -pedantic-errors -pedantic)
Eric Fiselierf4bfd7c2015-10-13 23:56:33495
Eric Fiselier10ed6c32015-07-30 22:30:34496# Required flags ==============================================================
Louis Dionne925d9d22019-10-02 20:07:01497function(cxx_add_basic_build_flags target)
Eric Fiselier72b41e62019-12-12 01:26:30498
Louis Dionne8a7846f2023-11-05 13:40:51499 # Use C++23 for all targets.
Eric Fiselier72b41e62019-12-12 01:26:30500 set_target_properties(${target} PROPERTIES
Louis Dionne8a7846f2023-11-05 13:40:51501 CXX_STANDARD 23
Louis Dionnef97a5792023-11-16 19:58:36502 CXX_STANDARD_REQUIRED OFF # TODO: Make this REQUIRED once we don't need to accommodate the LLVM documentation builders using an ancient CMake
Eric Fiselier72b41e62019-12-12 01:26:30503 CXX_EXTENSIONS NO)
Eric Fiselier10ed6c32015-07-30 22:30:34504
Louis Dionne918ba932020-07-23 15:05:47505 # When building the dylib, don't warn for unavailable aligned allocation
506 # functions based on the deployment target -- they are always available
Gabriel Ravierc23e2c02022-08-02 10:42:04507 # because they are provided by the dylib itself with the exception of z/OS.
Zbigniew Sarbinowskiaa8a5b82020-11-12 19:40:35508 if (ZOS)
509 target_add_compile_flags_if_supported(${target} PRIVATE -fno-aligned-allocation)
510 else()
511 target_add_compile_flags_if_supported(${target} PRIVATE -faligned-allocation)
512 endif()
Louis Dionne918ba932020-07-23 15:05:47513
Louis Dionne925d9d22019-10-02 20:07:01514 # On all systems the system c++ standard library headers need to be excluded.
515 # MSVC only has -X, which disables all default includes; including the crt.
516 # Thus, we do nothing and hope we don't accidentally include any of the C++
517 # headers
518 target_add_compile_flags_if_supported(${target} PUBLIC -nostdinc++)
Eric Fiselierff16b9a2015-07-29 21:07:28519
Louis Dionne925d9d22019-10-02 20:07:01520 # Hide all inline function definitions which have not explicitly been marked
521 # visible. This prevents new definitions for inline functions from appearing in
522 # the dylib when get ODR used by another function.
523 target_add_compile_flags_if_supported(${target} PRIVATE -fvisibility-inlines-hidden)
Eric Fiselier10b12f02016-10-25 19:43:44524
Nikolas Klauser73340242025-03-25 14:43:48525 target_add_compile_flags_if_supported(${target} PRIVATE -fvisibility=hidden)
Eric Fiselier3ca91852017-05-25 04:36:24526
Louis Dionned2864d12024-06-24 21:26:03527 # Build with -fsized-deallocation, which is default in recent versions of Clang.
528 # TODO(LLVM 21): This can be dropped once we only support Clang >= 19.
529 target_add_compile_flags_if_supported(${target} PRIVATE -fsized-deallocation)
530
Louis Dionne925d9d22019-10-02 20:07:01531 # Let the library headers know they are currently being used to build the
532 # library.
533 target_compile_definitions(${target} PRIVATE -D_LIBCPP_BUILDING_LIBRARY)
Eric Fiselier34992632017-03-02 19:35:33534
Mark de Weverc4162342023-03-10 16:31:58535 # Make sure the library can be build without transitive includes. This makes
536 # it easier to upgrade the library to a newer language standard without build
537 # errors.
538 target_compile_definitions(${target} PRIVATE -D_LIBCPP_REMOVE_TRANSITIVE_INCLUDES)
539
Petr Hosekb3df14b2022-03-10 09:47:09540 if (C_SUPPORTS_COMMENT_LIB_PRAGMA)
Michał Górnya9b5fff2019-12-02 10:49:20541 if (LIBCXX_HAS_PTHREAD_LIB)
542 target_compile_definitions(${target} PRIVATE -D_LIBCPP_LINK_PTHREAD_LIB)
543 endif()
544 if (LIBCXX_HAS_RT_LIB)
545 target_compile_definitions(${target} PRIVATE -D_LIBCPP_LINK_RT_LIB)
546 endif()
Louis Dionne925d9d22019-10-02 20:07:01547 endif()
Zibi Sarbinowski36dde912022-10-03 21:41:58548 target_compile_options(${target} PUBLIC "${LIBCXX_ADDITIONAL_COMPILE_FLAGS}")
Louis Dionne925d9d22019-10-02 20:07:01549endfunction()
Petr Hosek789b7f02019-05-30 04:40:21550
Eric Fiselier10ed6c32015-07-30 22:30:34551# Exception flags =============================================================
Louis Dionne432ae752019-10-04 18:03:17552function(cxx_add_exception_flags target)
553 if (LIBCXX_ENABLE_EXCEPTIONS)
554 # Catches C++ exceptions only and tells the compiler to assume that extern C
555 # functions never throw a C++ exception.
556 target_add_compile_flags_if_supported(${target} PUBLIC -EHsc)
557 else()
Louis Dionne432ae752019-10-04 18:03:17558 target_add_compile_flags_if_supported(${target} PUBLIC -EHs- -EHa-)
559 target_add_compile_flags_if_supported(${target} PUBLIC -fno-exceptions)
560 endif()
561endfunction()
Eric Fiselier10ed6c32015-07-30 22:30:34562
563# RTTI flags ==================================================================
Louis Dionne432ae752019-10-04 18:03:17564function(cxx_add_rtti_flags target)
565 if (NOT LIBCXX_ENABLE_RTTI)
David Spickett43cfc782023-07-07 09:55:36566 if (MSVC)
David Spickett36df92d2023-07-06 08:49:33567 target_add_compile_flags_if_supported(${target} PUBLIC -GR-)
568 else()
569 target_add_compile_flags_if_supported(${target} PUBLIC -fno-rtti)
570 endif()
Louis Dionne432ae752019-10-04 18:03:17571 endif()
572endfunction()
Eric Fiselier10ed6c32015-07-30 22:30:34573
Eric Fiselier194e0272016-10-14 12:56:52574# Modules flags ===============================================================
575# FIXME The libc++ sources are fundamentally non-modular. They need special
576# versions of the headers in order to provide C++03 and legacy ABI definitions.
577# NOTE: The public headers can be used with modules in all other contexts.
Louis Dionne432ae752019-10-04 18:03:17578function(cxx_add_module_flags target)
579 if (LLVM_ENABLE_MODULES)
580 # Ignore that the rest of the modules flags are now unused.
Louis Dionne13c42542019-10-04 19:10:56581 target_add_compile_flags_if_supported(${target} PUBLIC -Wno-unused-command-line-argument)
582 target_compile_options(${target} PUBLIC -fno-modules)
Louis Dionne432ae752019-10-04 18:03:17583 endif()
584endfunction()
Eric Fiselier194e0272016-10-14 12:56:52585
varconst1ba514c2023-07-12 03:18:20586string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
587
Jonathan Roelofs91b3a5e2015-08-24 21:20:07588# Sanitizer flags =============================================================
Eric Fiselierb98aa432015-07-29 23:46:55589
Eric Fiselier02f8e7c352018-11-13 23:08:31590function(get_sanitizer_flags OUT_VAR USE_SANITIZER)
591 set(SANITIZER_FLAGS)
592 set(USE_SANITIZER "${USE_SANITIZER}")
593 # NOTE: LLVM_USE_SANITIZER checks for a UNIX like system instead of MSVC.
594 # But we don't have LLVM_ON_UNIX so checking for MSVC is the best we can do.
595 if (USE_SANITIZER AND NOT MSVC)
596 append_flags_if_supported(SANITIZER_FLAGS "-fno-omit-frame-pointer")
597 append_flags_if_supported(SANITIZER_FLAGS "-gline-tables-only")
598
599 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND
600 NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")
601 append_flags_if_supported(SANITIZER_FLAGS "-gline-tables-only")
602 endif()
603 if (USE_SANITIZER STREQUAL "Address")
604 append_flags(SANITIZER_FLAGS "-fsanitize=address")
Vitaly Buka18014fe2022-08-13 05:12:28605 elseif (USE_SANITIZER STREQUAL "HWAddress")
606 append_flags(SANITIZER_FLAGS "-fsanitize=hwaddress")
Eric Fiselier02f8e7c352018-11-13 23:08:31607 elseif (USE_SANITIZER MATCHES "Memory(WithOrigins)?")
Mark de Weverd563df82022-04-30 11:17:17608 append_flags(SANITIZER_FLAGS -fsanitize=memory)
Eric Fiselier02f8e7c352018-11-13 23:08:31609 if (USE_SANITIZER STREQUAL "MemoryWithOrigins")
610 append_flags(SANITIZER_FLAGS "-fsanitize-memory-track-origins")
611 endif()
612 elseif (USE_SANITIZER STREQUAL "Undefined")
Louis Dionnefa712b02023-11-29 14:29:44613 append_flags(SANITIZER_FLAGS "-fsanitize=undefined" "-fno-sanitize=vptr,function" "-fno-sanitize-recover=all")
Brian Gesiak54176d12020-04-04 17:01:32614 elseif (USE_SANITIZER STREQUAL "Address;Undefined" OR
615 USE_SANITIZER STREQUAL "Undefined;Address")
Louis Dionnefa712b02023-11-29 14:29:44616 append_flags(SANITIZER_FLAGS "-fsanitize=address,undefined" "-fno-sanitize=vptr,function" "-fno-sanitize-recover=all")
Eric Fiselier02f8e7c352018-11-13 23:08:31617 elseif (USE_SANITIZER STREQUAL "Thread")
618 append_flags(SANITIZER_FLAGS -fsanitize=thread)
Zola Bridges0f124802020-04-17 17:15:58619 elseif (USE_SANITIZER STREQUAL "DataFlow")
620 append_flags(SANITIZER_FLAGS -fsanitize=dataflow)
Eric Fiselier02f8e7c352018-11-13 23:08:31621 else()
622 message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${USE_SANITIZER}")
623 endif()
624 elseif(USE_SANITIZER AND MSVC)
625 message(WARNING "LLVM_USE_SANITIZER is not supported on this platform.")
626 endif()
627 set(${OUT_VAR} "${SANITIZER_FLAGS}" PARENT_SCOPE)
628endfunction()
629
Eric Fiselier02f8e7c352018-11-13 23:08:31630get_sanitizer_flags(SANITIZER_FLAGS "${LLVM_USE_SANITIZER}")
Louis Dionnefa712b02023-11-29 14:29:44631add_library(cxx-sanitizer-flags INTERFACE)
632target_compile_options(cxx-sanitizer-flags INTERFACE ${SANITIZER_FLAGS})
Eric Fiselierf9f796e2015-10-13 22:12:02633
Tacet9ed20562023-12-13 05:05:34634# _LIBCPP_INSTRUMENTED_WITH_ASAN informs that library was built with ASan.
635# Defining _LIBCPP_INSTRUMENTED_WITH_ASAN while building the library with ASan is required.
636# Normally, the _LIBCPP_INSTRUMENTED_WITH_ASAN flag is used to keep information whether
637# dylibs are built with AddressSanitizer. However, when building libc++,
638# this flag needs to be defined so that the resulting dylib has all ASan functionalities guarded by this flag.
639# If the _LIBCPP_INSTRUMENTED_WITH_ASAN flag is not defined, then parts of the ASan instrumentation code in libc++
640# will not be compiled into it, resulting in false positives.
641# For context, read: https://github.com/llvm/llvm-project/pull/72677#pullrequestreview-1765402800
642string(FIND "${LLVM_USE_SANITIZER}" "Address" building_with_asan)
643if (NOT "${building_with_asan}" STREQUAL "-1")
644 config_define(ON _LIBCPP_INSTRUMENTED_WITH_ASAN)
Nikolas Klauserc6f3b7b2024-11-06 09:39:19645else()
646 config_define(OFF _LIBCPP_INSTRUMENTED_WITH_ASAN)
Tacet9ed20562023-12-13 05:05:34647endif()
648
Louis Dionne32300872019-10-08 16:26:24649# Link system libraries =======================================================
650function(cxx_link_system_libraries target)
Louis Dionne36bb1342023-10-13 18:53:57651 if (NOT MSVC)
652 target_link_libraries(${target} PRIVATE "-nostdlib++")
Zbigniew Sarbinowski5f9be2c2021-02-16 18:02:22653 endif()
Louis Dionne32300872019-10-08 16:26:24654
Petr Hosekb3df14b2022-03-10 09:47:09655 if (CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG AND LIBCXXABI_USE_LLVM_UNWINDER)
Martin Storsjö529a7932021-10-23 22:11:20656 # If we're linking directly against the libunwind that we're building
657 # in the same invocation, don't try to link in the toolchain's
658 # default libunwind (which may be missing still).
659 target_add_link_flags_if_supported(${target} PRIVATE "--unwindlib=none")
660 endif()
661
Louis Dionnea4336f22023-12-13 18:57:48662 if (MSVC)
663 if (LIBCXX_USE_COMPILER_RT)
664 find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY)
665 if (LIBCXX_BUILTINS_LIBRARY)
666 target_link_libraries(${target} PRIVATE "${LIBCXX_BUILTINS_LIBRARY}")
667 endif()
668 elseif (LIBCXX_HAS_GCC_LIB)
669 target_link_libraries(${target} PRIVATE gcc)
670 elseif (LIBCXX_HAS_GCC_S_LIB)
671 target_link_libraries(${target} PRIVATE gcc_s)
Louis Dionne32300872019-10-08 16:26:24672 endif()
Louis Dionne32300872019-10-08 16:26:24673 endif()
674
Louis Dionnee0184352020-06-04 18:54:38675 if (LIBCXX_HAS_ATOMIC_LIB)
Louis Dionne32300872019-10-08 16:26:24676 target_link_libraries(${target} PRIVATE atomic)
677 endif()
678
679 if (MINGW)
680 target_link_libraries(${target} PRIVATE "${MINGW_LIBRARIES}")
681 endif()
682
David Spickett43cfc782023-07-07 09:55:36683 if (MSVC)
Martin Storsjö9b02e8d2023-07-17 09:50:35684 if ((NOT CMAKE_MSVC_RUNTIME_LIBRARY AND uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
685 OR (CMAKE_MSVC_RUNTIME_LIBRARY MATCHES "Debug"))
Louis Dionne32300872019-10-08 16:26:24686 set(LIB_SUFFIX "d")
687 else()
688 set(LIB_SUFFIX "")
689 endif()
690
Martin Storsjöe777e442023-07-17 09:52:47691 if (NOT CMAKE_MSVC_RUNTIME_LIBRARY OR CMAKE_MSVC_RUNTIME_LIBRARY MATCHES "DLL$")
692 set(CRT_LIB "msvcrt")
693 set(CXX_LIB "msvcprt")
694 else()
695 set(CRT_LIB "libcmt")
696 set(CXX_LIB "libcpmt")
697 endif()
698
699 target_link_libraries(${target} PRIVATE ${CRT_LIB}${LIB_SUFFIX}) # C runtime startup files
700 target_link_libraries(${target} PRIVATE ${CXX_LIB}${LIB_SUFFIX}) # C++ standard library. Required for exception_ptr internals.
Louis Dionne32300872019-10-08 16:26:24701 # Required for standards-complaint wide character formatting functions
702 # (e.g. `printfw`/`scanfw`)
703 target_link_libraries(${target} PRIVATE iso_stdio_wide_specifiers)
704 endif()
Shoaib Meenai076da522020-01-28 01:29:41705
706 if (ANDROID AND ANDROID_PLATFORM_LEVEL LESS 21)
707 target_link_libraries(${target} PUBLIC android_support)
708 endif()
Zibi Sarbinowski36dde912022-10-03 21:41:58709 target_link_libraries(${target} PUBLIC "${LIBCXX_ADDITIONAL_LIBRARIES}")
Louis Dionne32300872019-10-08 16:26:24710endfunction()
711
Louis Dionne925d9d22019-10-02 20:07:01712# Windows-related flags =======================================================
713function(cxx_add_windows_flags target)
714 if(WIN32 AND NOT MINGW)
715 target_compile_definitions(${target} PRIVATE
716 # Ignore the -MSC_VER mismatch, as we may build
717 # with a different compatibility version.
718 _ALLOW_MSC_VER_MISMATCH
719 # Don't check the msvcprt iterator debug levels
720 # as we will define the iterator types; libc++
721 # uses a different macro to identify the debug
722 # level.
723 _ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH
724 # We are building the c++ runtime, don't pull in
725 # msvcprt.
726 _CRTBLD
727 # Don't warn on the use of "deprecated"
728 # "insecure" functions which are standards
729 # specified.
730 _CRT_SECURE_NO_WARNINGS
731 # Use the ISO conforming behaviour for conversion
732 # in printf, scanf.
733 _CRT_STDIO_ISO_WIDE_SPECIFIERS)
734 endif()
735endfunction()
736
Eric Fiselierf9f796e2015-10-13 22:12:02737# Configuration file flags =====================================================
Louis Dionne817d8972022-02-07 19:52:17738config_define(${LIBCXX_ABI_VERSION} _LIBCPP_ABI_VERSION)
739config_define(${LIBCXX_ABI_NAMESPACE} _LIBCPP_ABI_NAMESPACE)
Nikolas Klauserc6f3b7b2024-11-06 09:39:19740config_define(${LIBCXX_ABI_FORCE_ITANIUM} _LIBCPP_ABI_FORCE_ITANIUM)
741config_define(${LIBCXX_ABI_FORCE_MICROSOFT} _LIBCPP_ABI_FORCE_MICROSOFT)
742config_define(${LIBCXX_ENABLE_THREADS} _LIBCPP_HAS_THREADS)
743config_define(${LIBCXX_ENABLE_MONOTONIC_CLOCK} _LIBCPP_HAS_MONOTONIC_CLOCK)
744config_define(${LIBCXX_HAS_TERMINAL_AVAILABLE} _LIBCPP_HAS_TERMINAL)
Louis Dionnebe00e882020-11-16 23:13:43745if (NOT LIBCXX_TYPEINFO_COMPARISON_IMPLEMENTATION STREQUAL "default")
Louis Dionned0fcdcd2020-05-15 19:58:19746 config_define("${LIBCXX_TYPEINFO_COMPARISON_IMPLEMENTATION}" _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION)
Eric Fiselier2405bd62019-05-29 02:21:37747endif()
Nikolas Klauserc6f3b7b2024-11-06 09:39:19748config_define(${LIBCXX_HAS_PTHREAD_API} _LIBCPP_HAS_THREAD_API_PTHREAD)
749config_define(${LIBCXX_HAS_EXTERNAL_THREAD_API} _LIBCPP_HAS_THREAD_API_EXTERNAL)
750config_define(${LIBCXX_HAS_WIN32_THREAD_API} _LIBCPP_HAS_THREAD_API_WIN32)
751config_define(${LIBCXX_HAS_MUSL_LIBC} _LIBCPP_HAS_MUSL_LIBC)
Shoaib Meenai492d7132017-10-09 19:25:17752config_define_if(LIBCXX_NO_VCRUNTIME _LIBCPP_NO_VCRUNTIME)
Nikolas Klauserc6f3b7b2024-11-06 09:39:19753config_define(${LIBCXX_ENABLE_FILESYSTEM} _LIBCPP_HAS_FILESYSTEM)
754config_define(${LIBCXX_ENABLE_RANDOM_DEVICE} _LIBCPP_HAS_RANDOM_DEVICE)
755config_define(${LIBCXX_ENABLE_LOCALIZATION} _LIBCPP_HAS_LOCALIZATION)
756config_define(${LIBCXX_ENABLE_UNICODE} _LIBCPP_HAS_UNICODE)
757config_define(${LIBCXX_ENABLE_WIDE_CHARACTERS} _LIBCPP_HAS_WIDE_CHARACTERS)
758config_define(${LIBCXX_ENABLE_TIME_ZONE_DATABASE} _LIBCPP_HAS_TIME_ZONE_DATABASE)
759config_define(${LIBCXX_ENABLE_VENDOR_AVAILABILITY_ANNOTATIONS} _LIBCPP_HAS_VENDOR_AVAILABILITY_ANNOTATIONS)
Konstantin Varlamovc6151f52023-10-10 18:10:18760
Louis Dionnec9d9dc92024-10-29 14:48:18761# TODO: Remove in LLVM 21. We're leaving an error to make this fail explicitly.
Konstantin Varlamovc6151f52023-10-10 18:10:18762if (LIBCXX_ENABLE_ASSERTIONS)
Louis Dionnec9d9dc92024-10-29 14:48:18763 message(FATAL_ERROR "LIBCXX_ENABLE_ASSERTIONS has been removed. Please use LIBCXX_HARDENING_MODE instead.")
Konstantin Varlamovc6151f52023-10-10 18:10:18764endif()
Konstantin Varlamov64d413e2023-11-08 19:10:00765if (LIBCXX_HARDENING_MODE STREQUAL "none")
766 config_define(2 _LIBCPP_HARDENING_MODE_DEFAULT)
767elseif (LIBCXX_HARDENING_MODE STREQUAL "fast")
768 config_define(4 _LIBCPP_HARDENING_MODE_DEFAULT)
769elseif (LIBCXX_HARDENING_MODE STREQUAL "extensive")
770 config_define(16 _LIBCPP_HARDENING_MODE_DEFAULT)
varconstd1367ca2023-07-12 17:12:51771elseif (LIBCXX_HARDENING_MODE STREQUAL "debug")
Konstantin Varlamov64d413e2023-11-08 19:10:00772 config_define(8 _LIBCPP_HARDENING_MODE_DEFAULT)
varconstd1367ca2023-07-12 17:12:51773endif()
Vasileios Kalintiris8c58e922015-11-09 10:21:04774
Louis Dionned423d802024-04-17 17:36:53775if (LIBCXX_PSTL_BACKEND STREQUAL "serial")
776 config_define(1 _LIBCPP_PSTL_BACKEND_SERIAL)
777elseif(LIBCXX_PSTL_BACKEND STREQUAL "std_thread")
778 config_define(1 _LIBCPP_PSTL_BACKEND_STD_THREAD)
779elseif(LIBCXX_PSTL_BACKEND STREQUAL "libdispatch")
780 config_define(1 _LIBCPP_PSTL_BACKEND_LIBDISPATCH)
Nikolas Klausere837f4b2023-05-24 22:33:38781else()
Louis Dionned423d802024-04-17 17:36:53782 message(FATAL_ERROR "LIBCXX_PSTL_BACKEND is set to ${LIBCXX_PSTL_BACKEND}, which is not a valid backend.
Nikolas Klauser2b2e7f62023-07-12 20:24:57783 Valid backends are: serial, std_thread and libdispatch")
Nikolas Klausere837f4b2023-05-24 22:33:38784endif()
785
Shoaib Meenai8e628122017-10-04 23:17:12786if (LIBCXX_ABI_DEFINES)
787 set(abi_defines)
788 foreach (abi_define ${LIBCXX_ABI_DEFINES})
789 if (NOT abi_define MATCHES "^_LIBCPP_ABI_")
790 message(SEND_ERROR "Invalid ABI macro ${abi_define} in LIBCXX_ABI_DEFINES")
791 endif()
792 list(APPEND abi_defines "#define ${abi_define}")
793 endforeach()
794 string(REPLACE ";" "\n" abi_defines "${abi_defines}")
795 config_define(${abi_defines} _LIBCPP_ABI_DEFINES)
796endif()
797
Martin Storsjöf68e8902021-12-20 23:19:34798if (LIBCXX_EXTRA_SITE_DEFINES)
799 set(extra_site_defines)
800 foreach (extra_site_define ${LIBCXX_EXTRA_SITE_DEFINES})
801 # Allow defines such as DEFINE=VAL, transformed into "#define DEFINE VAL".
802 string(REPLACE "=" " " extra_site_define "${extra_site_define}")
803 list(APPEND extra_site_defines "#define ${extra_site_define}")
804 endforeach()
805 string(REPLACE ";" "\n" extra_site_defines "${extra_site_defines}")
806 config_define(${extra_site_defines} _LIBCPP_EXTRA_SITE_DEFINES)
807endif()
808
Eric Fiselier54644212016-09-26 22:19:41809# By default libc++ on Windows expects to use a shared library, which requires
810# the headers to use DLL import/export semantics. However when building a
811# static library only we modify the headers to disable DLL import/export.
812if (DEFINED WIN32 AND LIBCXX_ENABLE_STATIC AND NOT LIBCXX_ENABLE_SHARED)
813 message(STATUS "Generating custom __config for non-DLL Windows build")
Shoaib Meenaifc6100c2016-12-05 19:40:12814 config_define(ON _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
Eric Fiselier54644212016-09-26 22:19:41815endif()
816
Martin Storsjö8a73aa82020-10-23 07:54:02817if (WIN32 AND LIBCXX_ENABLE_STATIC_ABI_LIBRARY)
818 # If linking libcxxabi statically into libcxx, skip the dllimport attributes
819 # on symbols we refer to from libcxxabi.
820 add_definitions(-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS)
821endif()
822
Louis Dionnefadc84a2019-10-04 22:50:23823# Setup all common build flags =================================================
824function(cxx_add_common_build_flags target)
825 cxx_add_basic_build_flags(${target})
Nikolas Klausera7aade12023-02-17 10:31:41826 cxx_add_warning_flags(${target} ${LIBCXX_ENABLE_WERROR} ${LIBCXX_ENABLE_PEDANTIC})
Louis Dionnefadc84a2019-10-04 22:50:23827 cxx_add_windows_flags(${target})
Louis Dionnefadc84a2019-10-04 22:50:23828 cxx_add_exception_flags(${target})
829 cxx_add_rtti_flags(${target})
830 cxx_add_module_flags(${target})
Louis Dionne32300872019-10-08 16:26:24831 cxx_link_system_libraries(${target})
Louis Dionnefa712b02023-11-29 14:29:44832 target_link_libraries(${target} PRIVATE cxx-sanitizer-flags)
Louis Dionnefadc84a2019-10-04 22:50:23833endfunction()
834
Michael J. Spencerf5799be2010-12-10 19:47:54835#===============================================================================
Eric Fiselier10ed6c32015-07-30 22:30:34836# Setup Source Code And Tests
Michael J. Spencerf5799be2010-12-10 19:47:54837#===============================================================================
Louis Dionnebd135c32024-05-29 01:31:01838add_custom_target(cxx-test-depends
839 COMMENT "Build dependencies required to run the libc++ test suite.")
840
Howard Hinnant6a0d6ce2013-11-15 17:18:57841add_subdirectory(include)
Petr Hosekf80c4b62019-05-01 06:40:36842add_subdirectory(src)
Louis Dionne1f8e2862021-07-15 14:19:39843add_subdirectory(utils)
Mark de Wever1d6b6132024-01-21 11:02:39844add_subdirectory(modules)
Eric Fiselierb17bb062015-08-22 19:40:49845
Eric Fiselier10ed6c32015-07-30 22:30:34846if (LIBCXX_INCLUDE_TESTS)
Louis Dionneeb68a0a2020-06-29 15:51:15847 add_subdirectory(test)
Eric Fiselier28349f92016-11-14 02:43:12848 add_subdirectory(lib/abi)
Zachary Turner50105d22017-09-19 17:19:10849endif()
850
Eric Fiselierb17bb062015-08-22 19:40:49851if (LIBCXX_INCLUDE_DOCS)
852 add_subdirectory(docs)
853endif()