blob: 01f442de3bab8dc915c2277e00ce6e833453464e [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
204 Do not export any symbols from the static libc++ library. This is useful when
205 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 Fiselierfa43a5c2016-05-03 22:32:08 +0000209.. _libc++experimental options:
210
211libc++experimental Specific Options
212------------------------------------
213
214.. option:: LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY:BOOL
215
216 **Default**: ``ON``
217
218 Build and test libc++experimental.a.
219
220.. option:: LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY:BOOL
221
Eric Fiselier121594e2016-09-07 01:15:10 +0000222 **Default**: ``LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY AND LIBCXX_INSTALL_LIBRARY``
Eric Fiselierfa43a5c2016-05-03 22:32:08 +0000223
224 Install libc++experimental.a alongside libc++.
225
226
Eric Fiselier435db152016-06-17 19:46:40 +0000227.. option:: LIBCXX_ENABLE_FILESYSTEM:BOOL
228
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000229 **Default**: ``ON``
Eric Fiselier435db152016-06-17 19:46:40 +0000230
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000231 Build filesystem as a standalone library libc++fs.a.
Eric Fiselier435db152016-06-17 19:46:40 +0000232
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000233.. option:: LIBCXX_INSTALL_FILESYSTEM_LIBRARY:BOOL
234
235 **Default**: ``LIBCXX_ENABLE_FILESYSTEM AND LIBCXX_INSTALL_LIBRARY``
236
237 Install libc++fs.a alongside libc++.
Eric Fiselier435db152016-06-17 19:46:40 +0000238
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000239.. _ABI Library Specific Options:
240
241ABI Library Specific Options
242----------------------------
243
244.. option:: LIBCXX_CXX_ABI:STRING
245
246 **Values**: ``none``, ``libcxxabi``, ``libcxxrt``, ``libstdc++``, ``libsupc++``.
247
248 Select the ABI library to build libc++ against.
249
250.. option:: LIBCXX_CXX_ABI_INCLUDE_PATHS:PATHS
251
252 Provide additional search paths for the ABI library headers.
253
254.. option:: LIBCXX_CXX_ABI_LIBRARY_PATH:PATH
255
256 Provide the path to the ABI library that libc++ should link against.
257
258.. option:: LIBCXX_ENABLE_STATIC_ABI_LIBRARY:BOOL
259
260 **Default**: ``OFF``
261
262 If this option is enabled, libc++ will try and link the selected ABI library
263 statically.
264
Eric Fiselier0b09dd12015-10-15 22:41:51 +0000265.. option:: LIBCXX_ENABLE_ABI_LINKER_SCRIPT:BOOL
266
267 **Default**: ``ON`` by default on UNIX platforms other than Apple unless
268 'LIBCXX_ENABLE_STATIC_ABI_LIBRARY' is ON. Otherwise the default value is ``OFF``.
269
270 This option generate and installs a linker script as ``libc++.so`` which
271 links the correct ABI library.
272
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000273.. option:: LIBCXXABI_USE_LLVM_UNWINDER:BOOL
274
275 **Default**: ``OFF``
276
277 Build and use the LLVM unwinder. Note: This option can only be used when
278 libc++abi is the C++ ABI library used.
279
280
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000281libc++ Feature Options
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000282----------------------
283
284.. option:: LIBCXX_ENABLE_EXCEPTIONS:BOOL
285
286 **Default**: ``ON``
287
288 Build libc++ with exception support.
289
290.. option:: LIBCXX_ENABLE_RTTI:BOOL
291
292 **Default**: ``ON``
293
294 Build libc++ with run time type information.
295
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000296.. option:: LIBCXX_INCLUDE_BENCHMARKS:BOOL
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000297
Eric Fiselier93f212c2016-08-29 19:50:49 +0000298 **Default**: ``ON``
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000299
300 Build the libc++ benchmark tests and the Google Benchmark library needed
301 to support them.
302
Eric Fiselier453bc182018-11-14 20:38:46 +0000303.. option:: LIBCXX_BENCHMARK_TEST_ARGS:STRING
304
305 **Default**: ``--benchmark_min_time=0.01``
306
307 A semicolon list of arguments to pass when running the libc++ benchmarks using the
308 ``check-cxx-benchmarks`` rule. By default we run the benchmarks for a very short amount of time,
309 since the primary use of ``check-cxx-benchmarks`` is to get test and sanitizer coverage, not to
310 get accurate measurements.
311
Eric Fiselier18d2fa32016-10-30 22:53:00 +0000312.. option:: LIBCXX_BENCHMARK_NATIVE_STDLIB:STRING
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000313
Eric Fiselier18d2fa32016-10-30 22:53:00 +0000314 **Default**:: ``""``
315
316 **Values**:: ``libc++``, ``libstdc++``
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000317
318 Build the libc++ benchmark tests and Google Benchmark library against the
Eric Fiselier18d2fa32016-10-30 22:53:00 +0000319 specified standard library on the platform. On linux this can be used to
320 compare libc++ to libstdc++ by building the benchmark tests against both
321 standard libraries.
322
323.. option:: LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN:STRING
324
325 Use the specified GCC toolchain and standard library when building the native
326 stdlib benchmark tests.
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000327
Louis Dionne1821de42018-08-16 12:44:28 +0000328.. option:: LIBCXX_HIDE_FROM_ABI_PER_TU_BY_DEFAULT:BOOL
329
330 **Default**: ``OFF``
331
332 Pick the default for whether to constrain ABI-unstable symbols to
333 each individual translation unit. This setting controls whether
334 `_LIBCPP_HIDE_FROM_ABI_PER_TU_BY_DEFAULT` is defined by default --
335 see the documentation of that macro for details.
336
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000337
338libc++ ABI Feature Options
339--------------------------
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000340
341The following options allow building libc++ for a different ABI version.
342
343.. option:: LIBCXX_ABI_VERSION:STRING
344
345 **Default**: ``1``
346
347 Defines the target ABI version of libc++.
348
349.. option:: LIBCXX_ABI_UNSTABLE:BOOL
350
351 **Default**: ``OFF``
352
353 Build the "unstable" ABI version of libc++. Includes all ABI changing features
354 on top of the current stable version.
355
Eric Fiselierb33778a2018-10-30 21:44:53 +0000356.. option:: LIBCXX_ABI_NAMESPACE:STRING
357
358 **Default**: ``__n`` where ``n`` is the current ABI version.
359
360 This option defines the name of the inline ABI versioning namespace. It can be used for building
361 custom versions of libc++ with unique symbol names in order to prevent conflicts or ODR issues
362 with other libc++ versions.
363
364 .. warning::
365 When providing a custom namespace, it's the users responsibility to ensure the name won't cause
Louis Dionne6f2c6fb2018-11-16 14:57:47 +0000366 conflicts with other names defined by libc++, both now and in the future. In particular, inline
367 namespaces of the form ``__[0-9]+`` are strictly reserved by libc++ and may not be used by users.
368 Doing otherwise could cause conflicts and hinder libc++ ABI evolution.
Eric Fiselierb33778a2018-10-30 21:44:53 +0000369
Shoaib Meenai4ca4b7c2017-10-04 23:17:12 +0000370.. option:: LIBCXX_ABI_DEFINES:STRING
371
372 **Default**: ``""``
373
374 A semicolon-separated list of ABI macros to persist in the site config header.
375 See ``include/__config`` for the list of ABI macros.
376
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000377.. _LLVM-specific variables:
378
379LLVM-specific options
380---------------------
381
382.. option:: LLVM_LIBDIR_SUFFIX:STRING
383
384 Extra suffix to append to the directory where libraries are to be
385 installed. On a 64-bit architecture, one could use ``-DLLVM_LIBDIR_SUFFIX=64``
386 to install libraries to ``/usr/lib64``.
387
388.. option:: LLVM_BUILD_32_BITS:BOOL
389
390 Build 32-bits executables and libraries on 64-bits systems. This option is
391 available only on some 64-bits unix systems. Defaults to OFF.
392
393.. option:: LLVM_LIT_ARGS:STRING
394
395 Arguments given to lit. ``make check`` and ``make clang-test`` are affected.
396 By default, ``'-sv --no-progress-bar'`` on Visual C++ and Xcode, ``'-sv'`` on
397 others.
398
399
400Using Alternate ABI libraries
401=============================
402
403
404.. _libsupcxx:
405
406Using libsupc++ on Linux
407------------------------
408
409You will need libstdc++ in order to provide libsupc++.
410
411Figure out where the libsupc++ headers are on your system. On Ubuntu this
412is ``/usr/include/c++/<version>`` and ``/usr/include/c++/<version>/<target-triple>``
413
414You can also figure this out by running
415
416.. code-block:: bash
417
418 $ echo | g++ -Wp,-v -x c++ - -fsyntax-only
419 ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
420 ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include"
421 #include "..." search starts here:
422 #include &lt;...&gt; search starts here:
423 /usr/include/c++/4.7
424 /usr/include/c++/4.7/x86_64-linux-gnu
425 /usr/include/c++/4.7/backward
426 /usr/lib/gcc/x86_64-linux-gnu/4.7/include
427 /usr/local/include
428 /usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed
429 /usr/include/x86_64-linux-gnu
430 /usr/include
431 End of search list.
432
433Note that the first two entries happen to be what we are looking for. This
434may not be correct on other platforms.
435
436We can now run CMake:
437
438.. code-block:: bash
439
440 $ CC=clang CXX=clang++ cmake -G "Unix Makefiles" \
441 -DLIBCXX_CXX_ABI=libstdc++ \
442 -DLIBCXX_CXX_ABI_INCLUDE_PATHS="/usr/include/c++/4.7/;/usr/include/c++/4.7/x86_64-linux-gnu/" \
443 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr \
444 <libc++-source-dir>
445
446
447You can also substitute ``-DLIBCXX_CXX_ABI=libsupc++``
448above, which will cause the library to be linked to libsupc++ instead
449of libstdc++, but this is only recommended if you know that you will
450never need to link against libstdc++ in the same executable as libc++.
451GCC ships libsupc++ separately but only as a static library. If a
452program also needs to link against libstdc++, it will provide its
453own copy of libsupc++ and this can lead to subtle problems.
454
455.. code-block:: bash
456
457 $ make cxx
458 $ make install
459
460You can now run clang with -stdlib=libc++.
Eric Fiseliera0733922016-06-02 02:16:28 +0000461
462
463.. _libcxxrt_ref:
464
465Using libcxxrt on Linux
466------------------------
467
468You will need to keep the source tree of `libcxxrt`_ available
469on your build machine and your copy of the libcxxrt shared library must
470be placed where your linker will find it.
471
472We can now run CMake like:
473
474.. code-block:: bash
475
476 $ CC=clang CXX=clang++ cmake -G "Unix Makefiles" \
477 -DLIBCXX_CXX_ABI=libcxxrt \
478 -DLIBCXX_CXX_ABI_INCLUDE_PATHS=path/to/libcxxrt-sources/src \
479 -DCMAKE_BUILD_TYPE=Release \
480 -DCMAKE_INSTALL_PREFIX=/usr \
481 <libc++-source-directory>
482 $ make cxx
483 $ make install
484
485Unfortunately you can't simply run clang with "-stdlib=libc++" at this point, as
486clang is set up to link for libc++ linked to libsupc++. To get around this
487you'll have to set up your linker yourself (or patch clang). For example,
488
489.. code-block:: bash
490
491 $ clang++ -stdlib=libc++ helloworld.cpp \
492 -nodefaultlibs -lc++ -lcxxrt -lm -lc -lgcc_s -lgcc
493
494Alternately, you could just add libcxxrt to your libraries list, which in most
495situations will give the same result:
496
497.. code-block:: bash
498
499 $ clang++ -stdlib=libc++ helloworld.cpp -lcxxrt
500
501.. _`libcxxrt`: https://github.com/pathscale/libcxxrt/
502
503
504Using a local ABI library installation
505---------------------------------------
506
507.. warning::
508 This is not recommended in almost all cases.
509
510These instructions should only be used when you can't install your ABI library.
511
512Normally you must link libc++ against a ABI shared library that the
513linker can find. If you want to build and test libc++ against an ABI
514library not in the linker's path you needq to set
515``-DLIBCXX_CXX_ABI_LIBRARY_PATH=/path/to/abi/lib`` when configuring CMake.
516
517An example build using libc++abi would look like:
518
519.. code-block:: bash
520
521 $ CC=clang CXX=clang++ cmake \
522 -DLIBCXX_CXX_ABI=libc++abi \
523 -DLIBCXX_CXX_ABI_INCLUDE_PATHS="/path/to/libcxxabi/include" \
524 -DLIBCXX_CXX_ABI_LIBRARY_PATH="/path/to/libcxxabi-build/lib" \
525 path/to/libcxx
526 $ make
527
528When testing libc++ LIT will automatically link against the proper ABI
529library.