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