Louis Dionne | 41dcdfb | 2024-08-22 13:16:01 | [diff] [blame] | 1 | .. _VendorDocumentation: |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 2 | |
Louis Dionne | 41dcdfb | 2024-08-22 13:16:01 | [diff] [blame] | 3 | ==================== |
| 4 | Vendor Documentation |
| 5 | ==================== |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 6 | |
| 7 | .. contents:: |
| 8 | :local: |
| 9 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 10 | The instructions on this page are aimed at vendors who ship libc++ as part of an |
Kazu Hirata | da6a1f6 | 2023-05-23 06:25:16 | [diff] [blame] | 11 | operating system distribution, a toolchain or similar shipping vehicles. If you |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 12 | are a user merely trying to use libc++ in your program, you most likely want to |
Louis Dionne | 41dcdfb | 2024-08-22 13:16:01 | [diff] [blame] | 13 | refer to your vendor's documentation, or to the general user documentation |
| 14 | :ref:`here <user-documentation>`. |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 15 | |
Dan Albert | 626260c | 2019-11-07 20:40:05 | [diff] [blame] | 16 | .. warning:: |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 17 | If your operating system already provides libc++, it is important to be careful |
| 18 | not to replace it. Replacing your system's libc++ installation could render it |
| 19 | non-functional. Use the CMake option ``CMAKE_INSTALL_PREFIX`` to select a safe |
| 20 | place to install libc++. |
| 21 | |
| 22 | |
| 23 | The default build |
| 24 | ================= |
| 25 | |
Louis Dionne | 79175f3 | 2021-10-07 20:19:11 | [diff] [blame] | 26 | The default way of building libc++, libc++abi and libunwind is to root the CMake |
| 27 | invocation at ``<monorepo>/runtimes``. While those projects are under the LLVM |
| 28 | umbrella, they are different in nature from other build tools, so it makes sense |
| 29 | to treat them as a separate set of entities. The default build can be achieved |
| 30 | with the following CMake invocation: |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 31 | |
Dan Albert | 626260c | 2019-11-07 20:40:05 | [diff] [blame] | 32 | .. code-block:: bash |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 33 | |
Dan Albert | 626260c | 2019-11-07 20:40:05 | [diff] [blame] | 34 | $ git clone https://github.com/llvm/llvm-project.git |
| 35 | $ cd llvm-project |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 36 | $ mkdir build |
Louis Dionne | 79175f3 | 2021-10-07 20:19:11 | [diff] [blame] | 37 | $ cmake -G Ninja -S runtimes -B build -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" # Configure |
| 38 | $ ninja -C build cxx cxxabi unwind # Build |
| 39 | $ ninja -C build check-cxx check-cxxabi check-unwind # Test |
| 40 | $ ninja -C build install-cxx install-cxxabi install-unwind # Install |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 41 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 42 | .. note:: |
Louis Dionne | 41dcdfb | 2024-08-22 13:16:01 | [diff] [blame] | 43 | See :ref:`Vendor Configuration Options` below for more configuration options. |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 44 | |
Louis Dionne | 79175f3 | 2021-10-07 20:19:11 | [diff] [blame] | 45 | After building the various ``install-XXX`` targets, shared libraries for libc++, libc++abi and |
| 46 | libunwind should now be present in ``<CMAKE_INSTALL_PREFIX>/lib``, and headers in |
Louis Dionne | 41dcdfb | 2024-08-22 13:16:01 | [diff] [blame] | 47 | ``<CMAKE_INSTALL_PREFIX>/include/c++/v1``. See the instructions below for information on how |
| 48 | to use this libc++ over the default one. |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 49 | |
Louis Dionne | 79175f3 | 2021-10-07 20:19:11 | [diff] [blame] | 50 | In the default configuration, the runtimes will be built using the compiler available by default |
| 51 | on your system. Of course, you can change what compiler is being used with the usual CMake |
| 52 | variables. If you wish to build the runtimes from a just-built Clang, the bootstrapping build |
| 53 | explained below makes this task easy. |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 54 | |
Louis Dionne | 41dcdfb | 2024-08-22 13:16:01 | [diff] [blame] | 55 | Using the just-built libc++ |
| 56 | --------------------------- |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 57 | |
Louis Dionne | 41dcdfb | 2024-08-22 13:16:01 | [diff] [blame] | 58 | Most compilers provide a way to disable the default behavior for finding the standard library and |
| 59 | to override it with custom paths. With Clang, this can be done with: |
| 60 | |
| 61 | .. code-block:: bash |
| 62 | |
| 63 | $ clang++ -nostdinc++ -isystem <install>/include/c++/v1 \ |
| 64 | -nostdlib++ -L <install>/lib -lc++ \ |
| 65 | -Wl,-rpath,<install>/lib \ |
| 66 | test.cpp |
| 67 | |
| 68 | The option ``-Wl,-rpath,<install>/lib`` adds a runtime library search path, which causes the system's |
| 69 | dynamic linker to look for libc++ in ``<install>/lib`` whenever the program is loaded. |
| 70 | |
| 71 | |
| 72 | The Bootstrapping build |
| 73 | ======================= |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 74 | |
Louis Dionne | 79175f3 | 2021-10-07 20:19:11 | [diff] [blame] | 75 | It is possible to build Clang and then build the runtimes using that just-built compiler in a |
| 76 | single CMake invocation. This is usually the correct way to build the runtimes when putting together |
| 77 | a toolchain, or when the system compiler is not adequate to build them (too old, unsupported, etc.). |
| 78 | To do this, use the following CMake invocation, and in particular notice how we're now rooting the |
| 79 | CMake invocation at ``<monorepo>/llvm``: |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 80 | |
| 81 | .. code-block:: bash |
| 82 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 83 | $ mkdir build |
Louis Dionne | 79175f3 | 2021-10-07 20:19:11 | [diff] [blame] | 84 | $ cmake -G Ninja -S llvm -B build -DLLVM_ENABLE_PROJECTS="clang" \ # Configure |
| 85 | -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" \ |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 86 | -DLLVM_RUNTIME_TARGETS="<target-triple>" |
Louis Dionne | 79175f3 | 2021-10-07 20:19:11 | [diff] [blame] | 87 | $ ninja -C build runtimes # Build |
| 88 | $ ninja -C build check-runtimes # Test |
| 89 | $ ninja -C build install-runtimes # Install |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 90 | |
Louis Dionne | 79175f3 | 2021-10-07 20:19:11 | [diff] [blame] | 91 | .. note:: |
Louis Dionne | 41dcdfb | 2024-08-22 13:16:01 | [diff] [blame] | 92 | - This type of build is also commonly called a "Runtimes build", but we would like to move |
| 93 | away from that terminology, which is too confusing. |
| 94 | |
| 95 | - Adding the `--fresh` flag to the top-level cmake invocation in a bootstrapping build *will not* |
| 96 | freshen the cmake cache of any of the enabled runtimes. |
| 97 | |
| 98 | |
| 99 | .. _Vendor Configuration Options: |
| 100 | |
| 101 | Vendor Configuration Options |
| 102 | ============================ |
| 103 | |
| 104 | This section documents configuration options that can be used by vendors when building the library. |
| 105 | These options provide a great deal of flexibility to customize libc++, such as selecting the ABI in |
| 106 | use, whether some features are provided, etc. |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 107 | |
Will Hawkins | f00e0f2 | 2023-09-07 17:22:07 | [diff] [blame] | 108 | .. warning:: |
Louis Dionne | 41dcdfb | 2024-08-22 13:16:01 | [diff] [blame] | 109 | Many of these CMake options are tied to configuration macros with a corresponding name in the source |
| 110 | code. However, these configuration macros are not intended to be customized by users directly, since |
| 111 | many of them require the library to be built with a matching configuration. If you don't build libc++ |
| 112 | yourself, you should not use the options documented here. |
Will Hawkins | f00e0f2 | 2023-09-07 17:22:07 | [diff] [blame] | 113 | |
Louis Dionne | 41dcdfb | 2024-08-22 13:16:01 | [diff] [blame] | 114 | General purpose options |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 115 | ----------------------- |
| 116 | |
Eric Fiselier | 539cd67 | 2016-05-03 22:32:08 | [diff] [blame] | 117 | .. option:: LIBCXX_INSTALL_LIBRARY:BOOL |
| 118 | |
| 119 | **Default**: ``ON`` |
| 120 | |
| 121 | Toggle the installation of the library portion of libc++. |
| 122 | |
| 123 | .. option:: LIBCXX_INSTALL_HEADERS:BOOL |
| 124 | |
| 125 | **Default**: ``ON`` |
| 126 | |
| 127 | Toggle the installation of the libc++ headers. |
| 128 | |
Mark de Wever | 8e0a4a8 | 2024-04-16 18:22:48 | [diff] [blame] | 129 | .. option:: LIBCXX_INSTALL_MODULES:BOOL |
| 130 | |
Mark de Wever | 19d2d3f | 2024-04-28 12:12:27 | [diff] [blame] | 131 | **Default**: ``ON`` |
Mark de Wever | 8e0a4a8 | 2024-04-16 18:22:48 | [diff] [blame] | 132 | |
| 133 | Toggle the installation of the experimental libc++ module sources. |
| 134 | |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 135 | .. option:: LIBCXX_ENABLE_SHARED:BOOL |
| 136 | |
| 137 | **Default**: ``ON`` |
| 138 | |
Eric Fiselier | 8e68d6a | 2016-09-16 03:47:53 | [diff] [blame] | 139 | Build libc++ as a shared library. Either `LIBCXX_ENABLE_SHARED` or |
| 140 | `LIBCXX_ENABLE_STATIC` has to be enabled. |
Petr Hosek | 9e49a33 | 2016-08-08 22:57:25 | [diff] [blame] | 141 | |
| 142 | .. option:: LIBCXX_ENABLE_STATIC:BOOL |
| 143 | |
| 144 | **Default**: ``ON`` |
| 145 | |
Eric Fiselier | 8e68d6a | 2016-09-16 03:47:53 | [diff] [blame] | 146 | Build libc++ as a static library. Either `LIBCXX_ENABLE_SHARED` or |
| 147 | `LIBCXX_ENABLE_STATIC` has to be enabled. |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 148 | |
John Ericson | e941b03 | 2022-08-19 02:44:46 | [diff] [blame] | 149 | .. option:: LIBCXX_LIBDIR_SUFFIX:STRING |
| 150 | |
| 151 | Extra suffix to append to the directory where libraries are to be installed. |
| 152 | This option overrides `LLVM_LIBDIR_SUFFIX`. |
| 153 | |
Petr Hosek | a2685cd | 2019-01-06 06:14:31 | [diff] [blame] | 154 | .. option:: LIBCXX_HERMETIC_STATIC_LIBRARY:BOOL |
| 155 | |
| 156 | **Default**: ``OFF`` |
| 157 | |
Jonathan Metzman | ff79ee1 | 2019-04-10 23:44:27 | [diff] [blame] | 158 | Do not export any symbols from the static libc++ library. |
Petr Hosek | a2685cd | 2019-01-06 06:14:31 | [diff] [blame] | 159 | This is useful when the static libc++ library is being linked into shared |
| 160 | libraries that may be used in with other shared libraries that use different |
Louis Dionne | 236f801 | 2019-08-23 19:42:09 | [diff] [blame] | 161 | C++ library. We want to avoid exporting any libc++ symbols in that case. |
Petr Hosek | a2685cd | 2019-01-06 06:14:31 | [diff] [blame] | 162 | |
Eric Fiselier | f1d87f8 | 2019-03-21 00:04:31 | [diff] [blame] | 163 | .. option:: LIBCXX_ENABLE_FILESYSTEM:BOOL |
| 164 | |
Mark de Wever | 92b758c | 2021-07-29 05:54:48 | [diff] [blame] | 165 | **Default**: ``ON`` except on Windows when using MSVC. |
Eric Fiselier | f1d87f8 | 2019-03-21 00:04:31 | [diff] [blame] | 166 | |
| 167 | This option can be used to enable or disable the filesystem components on |
Mark de Wever | 92b758c | 2021-07-29 05:54:48 | [diff] [blame] | 168 | platforms that may not support them. For example on Windows when using MSVC. |
Eric Fiselier | f1d87f8 | 2019-03-21 00:04:31 | [diff] [blame] | 169 | |
Louis Dionne | f4c1258 | 2021-08-23 19:32:36 | [diff] [blame] | 170 | .. option:: LIBCXX_ENABLE_WIDE_CHARACTERS:BOOL |
| 171 | |
| 172 | **Default**: ``ON`` |
| 173 | |
| 174 | This option can be used to disable support for ``wchar_t`` in the library. It also |
| 175 | allows the library to work on top of a C Standard Library that does not provide |
| 176 | support for ``wchar_t``. This is especially useful in embedded settings where |
| 177 | C Standard Libraries don't always provide all the usual bells and whistles. |
| 178 | |
Mark de Wever | f78f93b | 2022-09-23 16:33:20 | [diff] [blame] | 179 | .. option:: LIBCXX_ENABLE_TIME_ZONE_DATABASE:BOOL |
| 180 | |
| 181 | **Default**: ``ON`` |
| 182 | |
| 183 | Whether to include support for time zones in the library. Disabling |
| 184 | time zone support can be useful when porting to platforms that don't |
| 185 | ship the IANA time zone database. When time zones are not supported, |
| 186 | time zone support in <chrono> will be disabled. |
| 187 | |
John Ericson | 1e03c37 | 2021-04-28 22:36:47 | [diff] [blame] | 188 | .. option:: LIBCXX_INSTALL_LIBRARY_DIR:PATH |
| 189 | |
John Ericson | e941b03 | 2022-08-19 02:44:46 | [diff] [blame] | 190 | **Default**: ``lib${LIBCXX_LIBDIR_SUFFIX}`` |
John Ericson | 1e03c37 | 2021-04-28 22:36:47 | [diff] [blame] | 191 | |
| 192 | Path where built libc++ libraries should be installed. If a relative path, |
| 193 | relative to ``CMAKE_INSTALL_PREFIX``. |
| 194 | |
| 195 | .. option:: LIBCXX_INSTALL_INCLUDE_DIR:PATH |
| 196 | |
| 197 | **Default**: ``include/c++/v1`` |
| 198 | |
| 199 | Path where target-agnostic libc++ headers should be installed. If a relative |
| 200 | path, relative to ``CMAKE_INSTALL_PREFIX``. |
| 201 | |
| 202 | .. option:: LIBCXX_INSTALL_INCLUDE_TARGET_DIR:PATH |
| 203 | |
| 204 | **Default**: ``include/c++/v1`` or |
Leonard Chan | 96d6399 | 2022-12-05 22:20:51 | [diff] [blame] | 205 | ``include/${LLVM_DEFAULT_TARGET_TRIPLE}/c++/v1`` |
John Ericson | 1e03c37 | 2021-04-28 22:36:47 | [diff] [blame] | 206 | |
| 207 | Path where target-specific libc++ headers should be installed. If a relative |
| 208 | path, relative to ``CMAKE_INSTALL_PREFIX``. |
| 209 | |
Zibi Sarbinowski | 36dde91 | 2022-10-03 21:41:58 | [diff] [blame] | 210 | .. option:: LIBCXX_SHARED_OUTPUT_NAME:STRING |
| 211 | |
| 212 | **Default**: ``c++`` |
| 213 | |
| 214 | Output name for the shared libc++ runtime library. |
| 215 | |
Louis Dionne | e674424 | 2024-10-17 20:17:40 | [diff] [blame] | 216 | .. option:: {LIBCXX,LIBCXXABI,LIBUNWIND}_ADDITIONAL_COMPILE_FLAGS:STRING |
Zibi Sarbinowski | 36dde91 | 2022-10-03 21:41:58 | [diff] [blame] | 217 | |
| 218 | **Default**: ``""`` |
| 219 | |
Louis Dionne | e674424 | 2024-10-17 20:17:40 | [diff] [blame] | 220 | Additional compile flags to use when building the runtimes. This should be a CMake ``;``-delimited list of individual |
| 221 | compiler options to use. For options that must be passed as-is to the compiler without deduplication (e.g. |
| 222 | ``-Xclang -foo`` option groups), consider using ``SHELL:`` as `documented here <https://cmake.org/cmake/help/latest/command/add_compile_options.html#option-de-duplication>`_. |
Zibi Sarbinowski | 36dde91 | 2022-10-03 21:41:58 | [diff] [blame] | 223 | |
| 224 | .. option:: LIBCXX_ADDITIONAL_LIBRARIES:STRING |
| 225 | |
| 226 | **Default**: ``""`` |
| 227 | |
| 228 | Additional libraries libc++ is linked to which can be provided in cache. |
| 229 | |
Louis Dionne | 41dcdfb | 2024-08-22 13:16:01 | [diff] [blame] | 230 | .. option:: LIBCXX_ENABLE_EXCEPTIONS:BOOL |
Eric Fiselier | 539cd67 | 2016-05-03 22:32:08 | [diff] [blame] | 231 | |
Louis Dionne | 41dcdfb | 2024-08-22 13:16:01 | [diff] [blame] | 232 | **Default**: ``ON`` |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 233 | |
Louis Dionne | 41dcdfb | 2024-08-22 13:16:01 | [diff] [blame] | 234 | Build libc++ with exception support. |
| 235 | |
| 236 | .. option:: LIBCXX_ENABLE_RTTI:BOOL |
| 237 | |
| 238 | **Default**: ``ON`` |
| 239 | |
| 240 | Build libc++ with run time type information. |
| 241 | This option may only be set to OFF when LIBCXX_ENABLE_EXCEPTIONS=OFF. |
| 242 | |
| 243 | .. option:: LIBCXX_INCLUDE_TESTS:BOOL |
| 244 | |
| 245 | **Default**: ``ON`` (or value of ``LLVM_INCLUDE_TESTS``) |
| 246 | |
Louis Dionne | e236a52 | 2024-11-07 14:07:50 | [diff] [blame] | 247 | Build the libc++ test suite, which includes various types of tests like conformance |
| 248 | tests, vendor-specific tests and benchmarks. |
Louis Dionne | 41dcdfb | 2024-08-22 13:16:01 | [diff] [blame] | 249 | |
| 250 | .. option:: LIBCXX_INCLUDE_BENCHMARKS:BOOL |
| 251 | |
| 252 | **Default**: ``ON`` |
| 253 | |
| 254 | Build the libc++ benchmark tests and the Google Benchmark library needed |
| 255 | to support them. |
| 256 | |
Louis Dionne | 41dcdfb | 2024-08-22 13:16:01 | [diff] [blame] | 257 | .. option:: LIBCXX_ASSERTION_HANDLER_FILE:PATH |
| 258 | |
| 259 | **Default**:: ``"${CMAKE_CURRENT_SOURCE_DIR}/vendor/llvm/default_assertion_handler.in"`` |
| 260 | |
| 261 | Specify the path to a header that contains a custom implementation of the |
| 262 | assertion handler that gets invoked when a hardening assertion fails. If |
| 263 | provided, this header will be included by the library, replacing the |
| 264 | default assertion handler. If this is specified as a relative path, it |
| 265 | is assumed to be relative to ``<monorepo>/libcxx``. |
| 266 | |
| 267 | ABI Specific Options |
| 268 | -------------------- |
| 269 | |
| 270 | The following options allow building libc++ for a different ABI version. |
| 271 | |
| 272 | .. option:: LIBCXX_ABI_VERSION:STRING |
| 273 | |
| 274 | **Default**: ``1`` |
| 275 | |
| 276 | Defines the target ABI version of libc++. |
| 277 | |
| 278 | .. option:: LIBCXX_ABI_UNSTABLE:BOOL |
| 279 | |
| 280 | **Default**: ``OFF`` |
| 281 | |
| 282 | Build the "unstable" ABI version of libc++. Includes all ABI changing features |
| 283 | on top of the current stable version. |
| 284 | |
| 285 | .. option:: LIBCXX_ABI_NAMESPACE:STRING |
| 286 | |
| 287 | **Default**: ``__n`` where ``n`` is the current ABI version. |
| 288 | |
| 289 | This option defines the name of the inline ABI versioning namespace. It can be used for building |
| 290 | custom versions of libc++ with unique symbol names in order to prevent conflicts or ODR issues |
| 291 | with other libc++ versions. |
| 292 | |
| 293 | .. warning:: |
| 294 | When providing a custom namespace, it's the vendor's responsibility to ensure the name won't cause |
| 295 | conflicts with other names defined by libc++, both now and in the future. In particular, inline |
| 296 | namespaces of the form ``__[0-9]+`` could cause conflicts with future versions of the library, |
| 297 | and so should be avoided. |
| 298 | |
| 299 | .. option:: LIBCXX_ABI_DEFINES:STRING |
| 300 | |
| 301 | **Default**: ``""`` |
| 302 | |
| 303 | A semicolon-separated list of ABI macros to persist in the site config header. |
| 304 | See ``include/__config`` for the list of ABI macros. |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 305 | |
| 306 | .. option:: LIBCXX_CXX_ABI:STRING |
| 307 | |
Louis Dionne | a80e65e | 2022-03-01 13:42:13 | [diff] [blame] | 308 | **Values**: ``none``, ``libcxxabi``, ``system-libcxxabi``, ``libcxxrt``, ``libstdc++``, ``libsupc++``, ``vcruntime``. |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 309 | |
| 310 | Select the ABI library to build libc++ against. |
| 311 | |
| 312 | .. option:: LIBCXX_CXX_ABI_INCLUDE_PATHS:PATHS |
| 313 | |
| 314 | Provide additional search paths for the ABI library headers. |
| 315 | |
| 316 | .. option:: LIBCXX_CXX_ABI_LIBRARY_PATH:PATH |
| 317 | |
Louis Dionne | a80e65e | 2022-03-01 13:42:13 | [diff] [blame] | 318 | Provide the path to the ABI library that libc++ should link against. This is only |
| 319 | useful when linking against an out-of-tree ABI library. |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 320 | |
| 321 | .. option:: LIBCXX_ENABLE_STATIC_ABI_LIBRARY:BOOL |
| 322 | |
| 323 | **Default**: ``OFF`` |
| 324 | |
| 325 | If this option is enabled, libc++ will try and link the selected ABI library |
| 326 | statically. |
| 327 | |
Eric Fiselier | 1ab69fc | 2015-10-15 22:41:51 | [diff] [blame] | 328 | .. option:: LIBCXX_ENABLE_ABI_LINKER_SCRIPT:BOOL |
| 329 | |
| 330 | **Default**: ``ON`` by default on UNIX platforms other than Apple unless |
| 331 | 'LIBCXX_ENABLE_STATIC_ABI_LIBRARY' is ON. Otherwise the default value is ``OFF``. |
| 332 | |
| 333 | This option generate and installs a linker script as ``libc++.so`` which |
| 334 | links the correct ABI library. |
| 335 | |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 336 | .. option:: LIBCXXABI_USE_LLVM_UNWINDER:BOOL |
| 337 | |
Louis Dionne | 8f90e69 | 2024-01-11 15:13:21 | [diff] [blame] | 338 | **Default**: ``ON`` |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 339 | |
| 340 | Build and use the LLVM unwinder. Note: This option can only be used when |
| 341 | libc++abi is the C++ ABI library used. |
| 342 | |
Zibi Sarbinowski | 36dde91 | 2022-10-03 21:41:58 | [diff] [blame] | 343 | .. option:: LIBCXXABI_ADDITIONAL_LIBRARIES:STRING |
| 344 | |
| 345 | **Default**: ``""`` |
| 346 | |
| 347 | Additional libraries libc++abi is linked to which can be provided in cache. |
| 348 | |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 349 | LLVM-specific options |
| 350 | --------------------- |
| 351 | |
John Ericson | e941b03 | 2022-08-19 02:44:46 | [diff] [blame] | 352 | .. option:: LLVM_LIBDIR_SUFFIX:STRING |
| 353 | |
| 354 | Extra suffix to append to the directory where libraries are to be |
| 355 | installed. On a 64-bit architecture, one could use ``-DLLVM_LIBDIR_SUFFIX=64`` |
| 356 | to install libraries to ``/usr/lib64``. |
| 357 | |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 358 | .. option:: LLVM_BUILD_32_BITS:BOOL |
| 359 | |
| 360 | Build 32-bits executables and libraries on 64-bits systems. This option is |
Louis Dionne | 85ee0c2 | 2019-10-01 20:34:50 | [diff] [blame] | 361 | available only on some 64-bits Unix systems. Defaults to OFF. |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 362 | |
| 363 | .. option:: LLVM_LIT_ARGS:STRING |
| 364 | |
| 365 | Arguments given to lit. ``make check`` and ``make clang-test`` are affected. |
| 366 | By default, ``'-sv --no-progress-bar'`` on Visual C++ and Xcode, ``'-sv'`` on |
| 367 | others. |
| 368 | |
| 369 | |
Louis Dionne | 41dcdfb | 2024-08-22 13:16:01 | [diff] [blame] | 370 | Support for Windows |
| 371 | =================== |
| 372 | |
| 373 | Libc++ supports being built with clang-cl, but not with MSVC's cl.exe, as |
| 374 | cl doesn't support the ``#include_next`` extension. Furthermore, VS 2017 or |
| 375 | newer (19.14) is required. |
| 376 | |
| 377 | Libc++ also supports being built with clang targeting MinGW environments. |
| 378 | |
| 379 | CMake + Visual Studio |
| 380 | --------------------- |
| 381 | |
| 382 | Building with Visual Studio currently does not permit running tests. However, |
| 383 | it is the simplest way to build. |
| 384 | |
| 385 | .. code-block:: batch |
| 386 | |
| 387 | > cmake -G "Visual Studio 16 2019" -S runtimes -B build ^ |
| 388 | -T "ClangCL" ^ |
| 389 | -DLLVM_ENABLE_RUNTIMES=libcxx ^ |
| 390 | -DLIBCXX_ENABLE_SHARED=YES ^ |
| 391 | -DLIBCXX_ENABLE_STATIC=NO |
| 392 | > cmake --build build |
| 393 | |
| 394 | CMake + ninja (MSVC) |
| 395 | -------------------- |
| 396 | |
| 397 | Building with ninja is required for development to enable tests. |
| 398 | A couple of tests require Bash to be available, and a couple dozens |
| 399 | of tests require other posix tools (cp, grep and similar - LLVM's tests |
| 400 | require the same). Without those tools the vast majority of tests |
| 401 | can still be ran successfully. |
| 402 | |
| 403 | If Git for Windows is available, that can be used to provide the bash |
| 404 | shell by adding the right bin directory to the path, e.g. |
| 405 | ``set PATH=%PATH%;C:\Program Files\Git\usr\bin``. |
| 406 | |
| 407 | Alternatively, one can also choose to run the whole build in a MSYS2 |
| 408 | shell. That can be set up e.g. by starting a Visual Studio Tools Command |
| 409 | Prompt (for getting the environment variables pointing to the headers and |
| 410 | import libraries), and making sure that clang-cl is available in the |
| 411 | path. From there, launch an MSYS2 shell via e.g. |
| 412 | ``C:\msys64\msys2_shell.cmd -full-path -mingw64`` (preserving the earlier |
| 413 | environment, allowing the MSVC headers/libraries and clang-cl to be found). |
| 414 | |
| 415 | In either case, then run: |
| 416 | |
| 417 | .. code-block:: batch |
| 418 | |
| 419 | > cmake -G Ninja -S runtimes -B build ^ |
| 420 | -DCMAKE_C_COMPILER=clang-cl ^ |
| 421 | -DCMAKE_CXX_COMPILER=clang-cl ^ |
| 422 | -DLLVM_ENABLE_RUNTIMES=libcxx |
| 423 | > ninja -C build cxx |
| 424 | > ninja -C build check-cxx |
| 425 | |
| 426 | If you are running in an MSYS2 shell and you have installed the |
| 427 | MSYS2-provided clang package (which defaults to a non-MSVC target), you |
| 428 | should add e.g. ``-DCMAKE_CXX_COMPILER_TARGET=x86_64-windows-msvc`` (replacing |
| 429 | ``x86_64`` with the architecture you're targeting) to the ``cmake`` command |
| 430 | line above. This will instruct ``check-cxx`` to use the right target triple |
| 431 | when invoking ``clang++``. |
| 432 | |
| 433 | CMake + ninja (MinGW) |
| 434 | --------------------- |
| 435 | |
| 436 | libcxx can also be built in MinGW environments, e.g. with the MinGW |
| 437 | compilers in MSYS2. This requires clang to be available (installed with |
| 438 | e.g. the ``mingw-w64-x86_64-clang`` package), together with CMake and ninja. |
| 439 | |
| 440 | .. code-block:: bash |
| 441 | |
| 442 | > cmake -G Ninja -S runtimes -B build \ |
| 443 | -DCMAKE_C_COMPILER=clang \ |
| 444 | -DCMAKE_CXX_COMPILER=clang++ \ |
| 445 | -DLLVM_ENABLE_LLD=ON \ |
| 446 | -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \ |
| 447 | -DLIBCXXABI_ENABLE_SHARED=OFF \ |
| 448 | -DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=ON |
| 449 | > ninja -C build cxx |
| 450 | > ninja -C build check-cxx |
| 451 | |
| 452 | .. _`libc++abi`: http://libcxxabi.llvm.org/ |
| 453 | |
| 454 | |
Konstantin Varlamov | 8dfc67d | 2024-01-18 02:56:07 | [diff] [blame] | 455 | .. _assertion-handler: |
| 456 | |
| 457 | Overriding the default assertion handler |
Konstantin Varlamov | 58780b8 | 2024-01-19 21:48:13 | [diff] [blame] | 458 | ======================================== |
Konstantin Varlamov | 8dfc67d | 2024-01-18 02:56:07 | [diff] [blame] | 459 | |
Konstantin Varlamov | 58780b8 | 2024-01-19 21:48:13 | [diff] [blame] | 460 | When the library wants to terminate due to a hardening assertion failure, the |
| 461 | program is aborted by invoking a trap instruction (or in debug mode, by |
| 462 | a special verbose termination function that prints an error message and calls |
| 463 | ``std::abort()``). This is done to minimize the code size impact of enabling |
| 464 | hardening in the library. However, vendors can also override that mechanism at |
| 465 | CMake configuration time. |
Konstantin Varlamov | 8dfc67d | 2024-01-18 02:56:07 | [diff] [blame] | 466 | |
Konstantin Varlamov | 58780b8 | 2024-01-19 21:48:13 | [diff] [blame] | 467 | Under the hood, a hardening assertion will invoke the |
| 468 | ``_LIBCPP_ASSERTION_HANDLER`` macro upon failure. A vendor may provide a header |
| 469 | that contains a custom definition of this macro and specify the path to the |
| 470 | header via the ``LIBCXX_ASSERTION_HANDLER_FILE`` CMake variable. If provided, |
| 471 | this header will be included by the library and replace the default |
| 472 | implementation. The header must not include any standard library headers |
| 473 | (directly or transitively) because doing so will almost always create a circular |
| 474 | dependency. The ``_LIBCPP_ASSERTION_HANDLER(message)`` macro takes a single |
| 475 | parameter that contains an error message explaining the hardening failure and |
| 476 | some details about the source location that triggered it. |
Konstantin Varlamov | 8dfc67d | 2024-01-18 02:56:07 | [diff] [blame] | 477 | |
| 478 | When a hardening assertion fails, it means that the program is about to invoke |
| 479 | library undefined behavior. For this reason, the custom assertion handler is |
| 480 | generally expected to terminate the program. If a custom assertion handler |
| 481 | decides to avoid doing so (e.g. it chooses to log and continue instead), it does |
| 482 | so at its own risk -- this approach should only be used in non-production builds |
| 483 | and with an understanding of potential consequences. Furthermore, the custom |
| 484 | assertion handler should not throw any exceptions as it may be invoked from |
| 485 | standard library functions that are marked ``noexcept`` (so throwing will result |
| 486 | in ``std::terminate`` being called). |
| 487 | |
| 488 | |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 489 | Using Alternate ABI libraries |
| 490 | ============================= |
| 491 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 492 | In order to implement various features like exceptions, RTTI, ``dynamic_cast`` and |
| 493 | more, libc++ requires what we refer to as an ABI library. Typically, that library |
| 494 | implements the `Itanium C++ ABI <https://itanium-cxx-abi.github.io/cxx-abi/abi.html>`_. |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 495 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 496 | By default, libc++ uses libc++abi as an ABI library. However, it is possible to use |
| 497 | other ABI libraries too. |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 498 | |
| 499 | Using libsupc++ on Linux |
| 500 | ------------------------ |
| 501 | |
| 502 | You will need libstdc++ in order to provide libsupc++. |
| 503 | |
| 504 | Figure out where the libsupc++ headers are on your system. On Ubuntu this |
| 505 | is ``/usr/include/c++/<version>`` and ``/usr/include/c++/<version>/<target-triple>`` |
| 506 | |
| 507 | You can also figure this out by running |
| 508 | |
| 509 | .. code-block:: bash |
| 510 | |
| 511 | $ echo | g++ -Wp,-v -x c++ - -fsyntax-only |
| 512 | ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu" |
| 513 | ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include" |
| 514 | #include "..." search starts here: |
| 515 | #include <...> search starts here: |
| 516 | /usr/include/c++/4.7 |
| 517 | /usr/include/c++/4.7/x86_64-linux-gnu |
| 518 | /usr/include/c++/4.7/backward |
| 519 | /usr/lib/gcc/x86_64-linux-gnu/4.7/include |
| 520 | /usr/local/include |
| 521 | /usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed |
| 522 | /usr/include/x86_64-linux-gnu |
| 523 | /usr/include |
| 524 | End of search list. |
| 525 | |
| 526 | Note that the first two entries happen to be what we are looking for. This |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 527 | may not be correct on all platforms. |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 528 | |
| 529 | We can now run CMake: |
| 530 | |
| 531 | .. code-block:: bash |
| 532 | |
Louis Dionne | 4ae83bb | 2022-02-09 17:08:44 | [diff] [blame] | 533 | $ cmake -G Ninja -S runtimes -B build \ |
| 534 | -DLLVM_ENABLE_RUNTIMES="libcxx" \ |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 535 | -DLIBCXX_CXX_ABI=libstdc++ \ |
itrofimow | 51e91b6 | 2024-01-22 15:12:41 | [diff] [blame] | 536 | -DLIBCXXABI_USE_LLVM_UNWINDER=OFF \ |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 537 | -DLIBCXX_CXX_ABI_INCLUDE_PATHS="/usr/include/c++/4.7/;/usr/include/c++/4.7/x86_64-linux-gnu/" |
| 538 | $ ninja -C build install-cxx |
Eric Fiselier | b17bb06 | 2015-08-22 19:40:49 | [diff] [blame] | 539 | |
| 540 | |
| 541 | You can also substitute ``-DLIBCXX_CXX_ABI=libsupc++`` |
| 542 | above, which will cause the library to be linked to libsupc++ instead |
| 543 | of libstdc++, but this is only recommended if you know that you will |
| 544 | never need to link against libstdc++ in the same executable as libc++. |
| 545 | GCC ships libsupc++ separately but only as a static library. If a |
| 546 | program also needs to link against libstdc++, it will provide its |
| 547 | own copy of libsupc++ and this can lead to subtle problems. |
| 548 | |
Eric Fiselier | e44604a | 2016-06-02 02:16:28 | [diff] [blame] | 549 | Using libcxxrt on Linux |
| 550 | ------------------------ |
| 551 | |
| 552 | You will need to keep the source tree of `libcxxrt`_ available |
| 553 | on your build machine and your copy of the libcxxrt shared library must |
| 554 | be placed where your linker will find it. |
| 555 | |
| 556 | We can now run CMake like: |
| 557 | |
| 558 | .. code-block:: bash |
| 559 | |
Louis Dionne | 4ae83bb | 2022-02-09 17:08:44 | [diff] [blame] | 560 | $ cmake -G Ninja -S runtimes -B build \ |
| 561 | -DLLVM_ENABLE_RUNTIMES="libcxx" \ |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 562 | -DLIBCXX_CXX_ABI=libcxxrt \ |
itrofimow | 51e91b6 | 2024-01-22 15:12:41 | [diff] [blame] | 563 | -DLIBCXX_ENABLE_NEW_DELETE_DEFINITIONS=ON \ |
| 564 | -DLIBCXXABI_USE_LLVM_UNWINDER=OFF \ |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 565 | -DLIBCXX_CXX_ABI_INCLUDE_PATHS=path/to/libcxxrt-sources/src |
| 566 | $ ninja -C build install-cxx |
Eric Fiselier | e44604a | 2016-06-02 02:16:28 | [diff] [blame] | 567 | |
| 568 | Unfortunately you can't simply run clang with "-stdlib=libc++" at this point, as |
| 569 | clang is set up to link for libc++ linked to libsupc++. To get around this |
| 570 | you'll have to set up your linker yourself (or patch clang). For example, |
| 571 | |
| 572 | .. code-block:: bash |
| 573 | |
| 574 | $ clang++ -stdlib=libc++ helloworld.cpp \ |
| 575 | -nodefaultlibs -lc++ -lcxxrt -lm -lc -lgcc_s -lgcc |
| 576 | |
| 577 | Alternately, you could just add libcxxrt to your libraries list, which in most |
| 578 | situations will give the same result: |
| 579 | |
| 580 | .. code-block:: bash |
| 581 | |
| 582 | $ clang++ -stdlib=libc++ helloworld.cpp -lcxxrt |
| 583 | |
Louis Dionne | 2ce0df4 | 2021-07-06 14:39:01 | [diff] [blame] | 584 | .. _`libcxxrt`: https://github.com/libcxxrt/libcxxrt |