blob: c334269a6d55a4a177e3a835396f793eab0628e9 [file] [log] [blame]
Eric Fiseliera0733922016-06-02 02:16:28 +00001.. _BuildingLibcxx:
Eric Fiselierd720d1f2015-08-22 19:40:49 +00002
3===============
4Building libc++
5===============
6
7.. contents::
8 :local:
9
Eric Fiselierfa43a5c2016-05-03 22:32:08 +000010.. _build instructions:
11
Eric Fiselierd720d1f2015-08-22 19:40:49 +000012Getting Started
13===============
14
15On Mac OS 10.7 (Lion) and later, the easiest way to get this library is to install
16Xcode 4.2 or later. However if you want to install tip-of-trunk from here
17(getting the bleeding edge), read on.
18
19The basic steps needed to build libc++ are:
20
James Y Knightf18eebf2019-01-29 16:37:27 +000021#. Checkout and configure LLVM (including libc++ and libc++abi), according to the `LLVM
22 getting started <https://llvm.org/docs/GettingStarted.html>`_ documentation. Make sure
23 to include ``libcxx`` and ``libcxxabi`` in the ``LLVM_ENABLE_PROJECTS`` option passed
24 to CMake.
Eric Fiselierd720d1f2015-08-22 19:40:49 +000025
26 For more information about configuring libc++ see :ref:`CMake Options`.
27
28 * ``make cxx`` --- will build libc++ and libc++abi.
Eric Fiselierbd402832016-08-28 18:33:08 +000029 * ``make check-cxx check-cxxabi`` --- will run the test suites.
Eric Fiselierd720d1f2015-08-22 19:40:49 +000030
31 Shared libraries for libc++ and libc++ abi should now be present in llvm/build/lib.
32 See :ref:`using an alternate libc++ installation <alternate libcxx>`
33
34#. **Optional**: Install libc++ and libc++abi
35
36 If your system already provides a libc++ installation it is important to be
37 careful not to replace it. Remember Use the CMake option ``CMAKE_INSTALL_PREFIX`` to
38 select a safe place to install libc++.
39
Eric Fiselierbd402832016-08-28 18:33:08 +000040 * ``make install-cxx install-cxxabi`` --- Will install the libraries and the headers
Eric Fiselierd720d1f2015-08-22 19:40:49 +000041
42 .. warning::
43 * Replacing your systems libc++ installation could render the system non-functional.
44 * Mac OS X will not boot without a valid copy of ``libc++.1.dylib`` in ``/usr/lib``.
45
46
47The instructions are for building libc++ on
48FreeBSD, Linux, or Mac using `libc++abi`_ as the C++ ABI library.
49On Linux, it is also possible to use :ref:`libsupc++ <libsupcxx>` or libcxxrt.
50
James Y Knightf18eebf2019-01-29 16:37:27 +000051It is sometimes beneficial to build separately from the full LLVM build. An
52out-of-tree build would look like this:
Eric Fiselierd720d1f2015-08-22 19:40:49 +000053
54.. code-block:: bash
55
56 $ cd where-you-want-libcxx-to-live
James Y Knightf18eebf2019-01-29 16:37:27 +000057 $ # Check out the sources (includes everything, but we'll only use libcxx)
58 $ ``git clone https://github.com/llvm/llvm-project.git``
Eric Fiselierd720d1f2015-08-22 19:40:49 +000059 $ cd where-you-want-to-build
60 $ mkdir build && cd build
61 $ export CC=clang CXX=clang++
James Y Knightf18eebf2019-01-29 16:37:27 +000062 $ cmake -DLLVM_PATH=path/to/separate/llvm \
Eric Fiselierd720d1f2015-08-22 19:40:49 +000063 -DLIBCXX_CXX_ABI=libcxxabi \
James Y Knightf18eebf2019-01-29 16:37:27 +000064 -DLIBCXX_CXX_ABI_INCLUDE_PATHS=path/to/separate/libcxxabi/include \
65 path/to/llvm-project/libcxx
Eric Fiselierd720d1f2015-08-22 19:40:49 +000066 $ make
67 $ make check-libcxx # optional
68
69
Saleem Abdulrasool3ab0b702017-02-10 03:58:20 +000070Experimental Support for Windows
71--------------------------------
72
73The Windows support requires building with clang-cl as cl does not support one
74required extension: `#include_next`. Furthermore, VS 2015 or newer (19.00) is
75required. In the case of clang-cl, we need to specify the "MS Compatibility
76Version" as it defaults to 2014 (18.00).
77
78CMake + Visual Studio
79~~~~~~~~~~~~~~~~~~~~~
80
81Building with Visual Studio currently does not permit running tests. However,
82it is the simplest way to build.
83
84.. code-block:: batch
85
86 > cmake -G "Visual Studio 14 2015" ^
87 -T "LLVM-vs2014" ^
88 -DLIBCXX_ENABLE_SHARED=YES ^
89 -DLIBCXX_ENABLE_STATIC=NO ^
90 -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=NO ^
91 \path\to\libcxx
92 > cmake --build .
93
94CMake + ninja
95~~~~~~~~~~~~~
96
97Building with ninja is required for development to enable tests.
98Unfortunately, doing so requires additional configuration as we cannot
99just specify a toolset.
100
101.. code-block:: batch
102
103 > cmake -G Ninja ^
104 -DCMAKE_MAKE_PROGRAM=/path/to/ninja ^
105 -DCMAKE_SYSTEM_NAME=Windows ^
106 -DCMAKE_C_COMPILER=clang-cl ^
107 -DCMAKE_C_FLAGS="-fms-compatibility-version=19.00 --target=i686--windows" ^
Hamza Sood4e2e4712017-12-03 10:18:35 +0000108 -DCMAKE_CXX_COMPILER=clang-cl ^
Saleem Abdulrasool3ab0b702017-02-10 03:58:20 +0000109 -DCMAKE_CXX_FLAGS="-fms-compatibility-version=19.00 --target=i686--windows" ^
110 -DLLVM_PATH=/path/to/llvm/tree ^
111 -DLIBCXX_ENABLE_SHARED=YES ^
112 -DLIBCXX_ENABLE_STATIC=NO ^
113 -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=NO ^
114 \path\to\libcxx
115 > /path/to/ninja cxx
116 > /path/to/ninja check-cxx
117
118Note that the paths specified with backward slashes must use the `\\` as the
119directory separator as clang-cl may otherwise parse the path as an argument.
120
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000121.. _`libc++abi`: http://libcxxabi.llvm.org/
122
123
124.. _CMake Options:
125
126CMake Options
127=============
128
129Here are some of the CMake variables that are used often, along with a
130brief explanation and LLVM-specific notes. For full documentation, check the
131CMake docs or execute ``cmake --help-variable VARIABLE_NAME``.
132
133**CMAKE_BUILD_TYPE**:STRING
134 Sets the build type for ``make`` based generators. Possible values are
135 Release, Debug, RelWithDebInfo and MinSizeRel. On systems like Visual Studio
136 the user sets the build type with the IDE settings.
137
138**CMAKE_INSTALL_PREFIX**:PATH
139 Path where LLVM will be installed if "make install" is invoked or the
140 "INSTALL" target is built.
141
142**CMAKE_CXX_COMPILER**:STRING
143 The C++ compiler to use when building and testing libc++.
144
145
146.. _libcxx-specific options:
147
148libc++ specific options
149-----------------------
150
Eric Fiselierfa43a5c2016-05-03 22:32:08 +0000151.. option:: LIBCXX_INSTALL_LIBRARY:BOOL
152
153 **Default**: ``ON``
154
155 Toggle the installation of the library portion of libc++.
156
157.. option:: LIBCXX_INSTALL_HEADERS:BOOL
158
159 **Default**: ``ON``
160
161 Toggle the installation of the libc++ headers.
162
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000163.. option:: LIBCXX_ENABLE_ASSERTIONS:BOOL
164
165 **Default**: ``ON``
166
167 Build libc++ with assertions enabled.
168
169.. option:: LIBCXX_BUILD_32_BITS:BOOL
170
171 **Default**: ``OFF``
172
Eric Fiselier887b86b2016-09-16 03:47:53 +0000173 Build libc++ as a 32 bit library. Also see `LLVM_BUILD_32_BITS`.
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000174
175.. option:: LIBCXX_ENABLE_SHARED:BOOL
176
177 **Default**: ``ON``
178
Eric Fiselier887b86b2016-09-16 03:47:53 +0000179 Build libc++ as a shared library. Either `LIBCXX_ENABLE_SHARED` or
180 `LIBCXX_ENABLE_STATIC` has to be enabled.
Petr Hosek13620052016-08-08 22:57:25 +0000181
182.. option:: LIBCXX_ENABLE_STATIC:BOOL
183
184 **Default**: ``ON``
185
Eric Fiselier887b86b2016-09-16 03:47:53 +0000186 Build libc++ as a static library. Either `LIBCXX_ENABLE_SHARED` or
187 `LIBCXX_ENABLE_STATIC` has to be enabled.
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000188
189.. option:: LIBCXX_LIBDIR_SUFFIX:STRING
190
191 Extra suffix to append to the directory where libraries are to be installed.
Eric Fiselier887b86b2016-09-16 03:47:53 +0000192 This option overrides `LLVM_LIBDIR_SUFFIX`.
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000193
Petr Hosek3975d8d2017-07-11 02:39:50 +0000194.. option:: LIBCXX_INSTALL_PREFIX:STRING
195
196 **Default**: ``""``
197
198 Define libc++ destination prefix.
Eric Fiselierfa43a5c2016-05-03 22:32:08 +0000199
Petr Hosekb42e3492019-01-06 06:14:31 +0000200.. option:: LIBCXX_HERMETIC_STATIC_LIBRARY:BOOL
201
202 **Default**: ``OFF``
203
Jonathan Metzmance2328d2019-04-10 23:44:27 +0000204 Do not export any symbols from the static libc++ library.
Petr Hosekb42e3492019-01-06 06:14:31 +0000205 This is useful when the static libc++ library is being linked into shared
206 libraries that may be used in with other shared libraries that use different
207 C++ library. We want to avoid avoid exporting any libc++ symbols in that case.
208
Eric Fiselier72e2dd82019-03-21 00:04:31 +0000209.. option:: LIBCXX_ENABLE_FILESYSTEM:BOOL
210
211 **Default**: ``ON`` except on Windows.
212
213 This option can be used to enable or disable the filesystem components on
214 platforms that may not support them. For example on Windows.
215
Eric Fiselierfa43a5c2016-05-03 22:32:08 +0000216.. _libc++experimental options:
217
218libc++experimental Specific Options
219------------------------------------
220
221.. option:: LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY:BOOL
222
223 **Default**: ``ON``
224
225 Build and test libc++experimental.a.
226
227.. option:: LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY:BOOL
228
Eric Fiselier121594e2016-09-07 01:15:10 +0000229 **Default**: ``LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY AND LIBCXX_INSTALL_LIBRARY``
Eric Fiselierfa43a5c2016-05-03 22:32:08 +0000230
231 Install libc++experimental.a alongside libc++.
232
233
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000234.. _ABI Library Specific Options:
235
236ABI Library Specific Options
237----------------------------
238
239.. option:: LIBCXX_CXX_ABI:STRING
240
241 **Values**: ``none``, ``libcxxabi``, ``libcxxrt``, ``libstdc++``, ``libsupc++``.
242
243 Select the ABI library to build libc++ against.
244
245.. option:: LIBCXX_CXX_ABI_INCLUDE_PATHS:PATHS
246
247 Provide additional search paths for the ABI library headers.
248
249.. option:: LIBCXX_CXX_ABI_LIBRARY_PATH:PATH
250
251 Provide the path to the ABI library that libc++ should link against.
252
253.. option:: LIBCXX_ENABLE_STATIC_ABI_LIBRARY:BOOL
254
255 **Default**: ``OFF``
256
257 If this option is enabled, libc++ will try and link the selected ABI library
258 statically.
259
Eric Fiselier0b09dd12015-10-15 22:41:51 +0000260.. option:: LIBCXX_ENABLE_ABI_LINKER_SCRIPT:BOOL
261
262 **Default**: ``ON`` by default on UNIX platforms other than Apple unless
263 'LIBCXX_ENABLE_STATIC_ABI_LIBRARY' is ON. Otherwise the default value is ``OFF``.
264
265 This option generate and installs a linker script as ``libc++.so`` which
266 links the correct ABI library.
267
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000268.. option:: LIBCXXABI_USE_LLVM_UNWINDER:BOOL
269
270 **Default**: ``OFF``
271
272 Build and use the LLVM unwinder. Note: This option can only be used when
273 libc++abi is the C++ ABI library used.
274
275
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000276libc++ Feature Options
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000277----------------------
278
279.. option:: LIBCXX_ENABLE_EXCEPTIONS:BOOL
280
281 **Default**: ``ON``
282
283 Build libc++ with exception support.
284
285.. option:: LIBCXX_ENABLE_RTTI:BOOL
286
287 **Default**: ``ON``
288
289 Build libc++ with run time type information.
290
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000291.. option:: LIBCXX_INCLUDE_BENCHMARKS:BOOL
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000292
Eric Fiselier93f212c2016-08-29 19:50:49 +0000293 **Default**: ``ON``
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000294
295 Build the libc++ benchmark tests and the Google Benchmark library needed
296 to support them.
297
Eric Fiselier453bc182018-11-14 20:38:46 +0000298.. option:: LIBCXX_BENCHMARK_TEST_ARGS:STRING
299
300 **Default**: ``--benchmark_min_time=0.01``
301
302 A semicolon list of arguments to pass when running the libc++ benchmarks using the
303 ``check-cxx-benchmarks`` rule. By default we run the benchmarks for a very short amount of time,
304 since the primary use of ``check-cxx-benchmarks`` is to get test and sanitizer coverage, not to
305 get accurate measurements.
306
Eric Fiselier18d2fa32016-10-30 22:53:00 +0000307.. option:: LIBCXX_BENCHMARK_NATIVE_STDLIB:STRING
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000308
Eric Fiselier18d2fa32016-10-30 22:53:00 +0000309 **Default**:: ``""``
310
311 **Values**:: ``libc++``, ``libstdc++``
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000312
313 Build the libc++ benchmark tests and Google Benchmark library against the
Eric Fiselier18d2fa32016-10-30 22:53:00 +0000314 specified standard library on the platform. On linux this can be used to
315 compare libc++ to libstdc++ by building the benchmark tests against both
316 standard libraries.
317
318.. option:: LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN:STRING
319
320 Use the specified GCC toolchain and standard library when building the native
321 stdlib benchmark tests.
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000322
Louis Dionne1821de42018-08-16 12:44:28 +0000323.. option:: LIBCXX_HIDE_FROM_ABI_PER_TU_BY_DEFAULT:BOOL
324
325 **Default**: ``OFF``
326
327 Pick the default for whether to constrain ABI-unstable symbols to
328 each individual translation unit. This setting controls whether
329 `_LIBCPP_HIDE_FROM_ABI_PER_TU_BY_DEFAULT` is defined by default --
330 see the documentation of that macro for details.
331
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000332
333libc++ ABI Feature Options
334--------------------------
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000335
336The following options allow building libc++ for a different ABI version.
337
338.. option:: LIBCXX_ABI_VERSION:STRING
339
340 **Default**: ``1``
341
342 Defines the target ABI version of libc++.
343
344.. option:: LIBCXX_ABI_UNSTABLE:BOOL
345
346 **Default**: ``OFF``
347
348 Build the "unstable" ABI version of libc++. Includes all ABI changing features
349 on top of the current stable version.
350
Eric Fiselierb33778a2018-10-30 21:44:53 +0000351.. option:: LIBCXX_ABI_NAMESPACE:STRING
352
353 **Default**: ``__n`` where ``n`` is the current ABI version.
354
355 This option defines the name of the inline ABI versioning namespace. It can be used for building
356 custom versions of libc++ with unique symbol names in order to prevent conflicts or ODR issues
357 with other libc++ versions.
358
359 .. warning::
360 When providing a custom namespace, it's the users responsibility to ensure the name won't cause
Louis Dionne6f2c6fb2018-11-16 14:57:47 +0000361 conflicts with other names defined by libc++, both now and in the future. In particular, inline
362 namespaces of the form ``__[0-9]+`` are strictly reserved by libc++ and may not be used by users.
363 Doing otherwise could cause conflicts and hinder libc++ ABI evolution.
Eric Fiselierb33778a2018-10-30 21:44:53 +0000364
Shoaib Meenai4ca4b7c2017-10-04 23:17:12 +0000365.. option:: LIBCXX_ABI_DEFINES:STRING
366
367 **Default**: ``""``
368
369 A semicolon-separated list of ABI macros to persist in the site config header.
370 See ``include/__config`` for the list of ABI macros.
371
Eric Fiselierf3566292019-05-29 02:21:37 +0000372
373.. option:: LIBCXX_HAS_MERGED_TYPEINFO_NAMES_DEFAULT
374
375 **Default**: ``None``. When defined this option overrides the libraries default configuration
376 for whether merged type info names are present.
377
378
379 Build ``std::type_info`` with the assumption that type info names for a type have been fully
380 merged are unique across the entire program. This may not be the case for libraries built with
381 ``-Bsymbolic`` or due to compiler or linker bugs (Ex. llvm.org/PR37398).
382
383 When the value is ``ON`` typeinfo comparisons compare only the pointer value, otherwise ``strcmp``
384 is used as a fallback.
385
386
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000387.. _LLVM-specific variables:
388
389LLVM-specific options
390---------------------
391
392.. option:: LLVM_LIBDIR_SUFFIX:STRING
393
394 Extra suffix to append to the directory where libraries are to be
395 installed. On a 64-bit architecture, one could use ``-DLLVM_LIBDIR_SUFFIX=64``
396 to install libraries to ``/usr/lib64``.
397
398.. option:: LLVM_BUILD_32_BITS:BOOL
399
400 Build 32-bits executables and libraries on 64-bits systems. This option is
401 available only on some 64-bits unix systems. Defaults to OFF.
402
403.. option:: LLVM_LIT_ARGS:STRING
404
405 Arguments given to lit. ``make check`` and ``make clang-test`` are affected.
406 By default, ``'-sv --no-progress-bar'`` on Visual C++ and Xcode, ``'-sv'`` on
407 others.
408
409
410Using Alternate ABI libraries
411=============================
412
413
414.. _libsupcxx:
415
416Using libsupc++ on Linux
417------------------------
418
419You will need libstdc++ in order to provide libsupc++.
420
421Figure out where the libsupc++ headers are on your system. On Ubuntu this
422is ``/usr/include/c++/<version>`` and ``/usr/include/c++/<version>/<target-triple>``
423
424You can also figure this out by running
425
426.. code-block:: bash
427
428 $ echo | g++ -Wp,-v -x c++ - -fsyntax-only
429 ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
430 ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include"
431 #include "..." search starts here:
432 #include &lt;...&gt; search starts here:
433 /usr/include/c++/4.7
434 /usr/include/c++/4.7/x86_64-linux-gnu
435 /usr/include/c++/4.7/backward
436 /usr/lib/gcc/x86_64-linux-gnu/4.7/include
437 /usr/local/include
438 /usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed
439 /usr/include/x86_64-linux-gnu
440 /usr/include
441 End of search list.
442
443Note that the first two entries happen to be what we are looking for. This
444may not be correct on other platforms.
445
446We can now run CMake:
447
448.. code-block:: bash
449
450 $ CC=clang CXX=clang++ cmake -G "Unix Makefiles" \
451 -DLIBCXX_CXX_ABI=libstdc++ \
452 -DLIBCXX_CXX_ABI_INCLUDE_PATHS="/usr/include/c++/4.7/;/usr/include/c++/4.7/x86_64-linux-gnu/" \
453 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr \
454 <libc++-source-dir>
455
456
457You can also substitute ``-DLIBCXX_CXX_ABI=libsupc++``
458above, which will cause the library to be linked to libsupc++ instead
459of libstdc++, but this is only recommended if you know that you will
460never need to link against libstdc++ in the same executable as libc++.
461GCC ships libsupc++ separately but only as a static library. If a
462program also needs to link against libstdc++, it will provide its
463own copy of libsupc++ and this can lead to subtle problems.
464
465.. code-block:: bash
466
467 $ make cxx
468 $ make install
469
470You can now run clang with -stdlib=libc++.
Eric Fiseliera0733922016-06-02 02:16:28 +0000471
472
473.. _libcxxrt_ref:
474
475Using libcxxrt on Linux
476------------------------
477
478You will need to keep the source tree of `libcxxrt`_ available
479on your build machine and your copy of the libcxxrt shared library must
480be placed where your linker will find it.
481
482We can now run CMake like:
483
484.. code-block:: bash
485
486 $ CC=clang CXX=clang++ cmake -G "Unix Makefiles" \
487 -DLIBCXX_CXX_ABI=libcxxrt \
488 -DLIBCXX_CXX_ABI_INCLUDE_PATHS=path/to/libcxxrt-sources/src \
489 -DCMAKE_BUILD_TYPE=Release \
490 -DCMAKE_INSTALL_PREFIX=/usr \
491 <libc++-source-directory>
492 $ make cxx
493 $ make install
494
495Unfortunately you can't simply run clang with "-stdlib=libc++" at this point, as
496clang is set up to link for libc++ linked to libsupc++. To get around this
497you'll have to set up your linker yourself (or patch clang). For example,
498
499.. code-block:: bash
500
501 $ clang++ -stdlib=libc++ helloworld.cpp \
502 -nodefaultlibs -lc++ -lcxxrt -lm -lc -lgcc_s -lgcc
503
504Alternately, you could just add libcxxrt to your libraries list, which in most
505situations will give the same result:
506
507.. code-block:: bash
508
509 $ clang++ -stdlib=libc++ helloworld.cpp -lcxxrt
510
511.. _`libcxxrt`: https://github.com/pathscale/libcxxrt/
512
513
514Using a local ABI library installation
515---------------------------------------
516
517.. warning::
518 This is not recommended in almost all cases.
519
520These instructions should only be used when you can't install your ABI library.
521
522Normally you must link libc++ against a ABI shared library that the
523linker can find. If you want to build and test libc++ against an ABI
524library not in the linker's path you needq to set
525``-DLIBCXX_CXX_ABI_LIBRARY_PATH=/path/to/abi/lib`` when configuring CMake.
526
527An example build using libc++abi would look like:
528
529.. code-block:: bash
530
531 $ CC=clang CXX=clang++ cmake \
532 -DLIBCXX_CXX_ABI=libc++abi \
533 -DLIBCXX_CXX_ABI_INCLUDE_PATHS="/path/to/libcxxabi/include" \
534 -DLIBCXX_CXX_ABI_LIBRARY_PATH="/path/to/libcxxabi-build/lib" \
535 path/to/libcxx
536 $ make
537
538When testing libc++ LIT will automatically link against the proper ABI
539library.