blob: 66cfb7b21ea79dee0074affa154c561bd8ca8468 [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
21#. Checkout LLVM:
22
23 * ``cd where-you-want-llvm-to-live``
24 * ``svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm``
25
26#. Checkout libc++:
27
28 * ``cd where-you-want-llvm-to-live``
Eric Fiselierc3ca4712015-12-14 22:26:28 +000029 * ``cd llvm/projects``
Eric Fiselierd720d1f2015-08-22 19:40:49 +000030 * ``svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx``
31
32#. Checkout libc++abi:
33
34 * ``cd where-you-want-llvm-to-live``
35 * ``cd llvm/projects``
Eric Fiselierc3ca4712015-12-14 22:26:28 +000036 * ``svn co http://llvm.org/svn/llvm-project/libcxxabi/trunk libcxxabi``
Eric Fiselierd720d1f2015-08-22 19:40:49 +000037
38#. Configure and build libc++ with libc++abi:
39
Alexey Samsonovadc99cd2016-01-30 01:11:42 +000040 CMake is the only supported configuration system.
Eric Fiselierd720d1f2015-08-22 19:40:49 +000041
42 Clang is the preferred compiler when building and using libc++.
43
44 * ``cd where you want to build llvm``
45 * ``mkdir build``
46 * ``cd build``
47 * ``cmake -G <generator> [options] <path to llvm sources>``
48
49 For more information about configuring libc++ see :ref:`CMake Options`.
50
51 * ``make cxx`` --- will build libc++ and libc++abi.
Eric Fiselierbd402832016-08-28 18:33:08 +000052 * ``make check-cxx check-cxxabi`` --- will run the test suites.
Eric Fiselierd720d1f2015-08-22 19:40:49 +000053
54 Shared libraries for libc++ and libc++ abi should now be present in llvm/build/lib.
55 See :ref:`using an alternate libc++ installation <alternate libcxx>`
56
57#. **Optional**: Install libc++ and libc++abi
58
59 If your system already provides a libc++ installation it is important to be
60 careful not to replace it. Remember Use the CMake option ``CMAKE_INSTALL_PREFIX`` to
61 select a safe place to install libc++.
62
Eric Fiselierbd402832016-08-28 18:33:08 +000063 * ``make install-cxx install-cxxabi`` --- Will install the libraries and the headers
Eric Fiselierd720d1f2015-08-22 19:40:49 +000064
65 .. warning::
66 * Replacing your systems libc++ installation could render the system non-functional.
67 * Mac OS X will not boot without a valid copy of ``libc++.1.dylib`` in ``/usr/lib``.
68
69
70The instructions are for building libc++ on
71FreeBSD, Linux, or Mac using `libc++abi`_ as the C++ ABI library.
72On Linux, it is also possible to use :ref:`libsupc++ <libsupcxx>` or libcxxrt.
73
Eric Fiselierb1c50fd2015-09-06 23:31:16 +000074It is sometimes beneficial to build outside of the LLVM tree. An out-of-tree
75build would look like this:
Eric Fiselierd720d1f2015-08-22 19:40:49 +000076
77.. code-block:: bash
78
79 $ cd where-you-want-libcxx-to-live
80 $ # Check out llvm, libc++ and libc++abi.
81 $ ``svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm``
82 $ ``svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx``
83 $ ``svn co http://llvm.org/svn/llvm-project/libcxxabi/trunk libcxxabi``
84 $ cd where-you-want-to-build
85 $ mkdir build && cd build
86 $ export CC=clang CXX=clang++
87 $ cmake -DLLVM_PATH=path/to/llvm \
88 -DLIBCXX_CXX_ABI=libcxxabi \
89 -DLIBCXX_CXX_ABI_INCLUDE_PATHS=path/to/libcxxabi/include \
90 path/to/libcxx
91 $ make
92 $ make check-libcxx # optional
93
94
Saleem Abdulrasool3ab0b702017-02-10 03:58:20 +000095Experimental Support for Windows
96--------------------------------
97
98The Windows support requires building with clang-cl as cl does not support one
99required extension: `#include_next`. Furthermore, VS 2015 or newer (19.00) is
100required. In the case of clang-cl, we need to specify the "MS Compatibility
101Version" as it defaults to 2014 (18.00).
102
103CMake + Visual Studio
104~~~~~~~~~~~~~~~~~~~~~
105
106Building with Visual Studio currently does not permit running tests. However,
107it is the simplest way to build.
108
109.. code-block:: batch
110
111 > cmake -G "Visual Studio 14 2015" ^
112 -T "LLVM-vs2014" ^
113 -DLIBCXX_ENABLE_SHARED=YES ^
114 -DLIBCXX_ENABLE_STATIC=NO ^
115 -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=NO ^
116 \path\to\libcxx
117 > cmake --build .
118
119CMake + ninja
120~~~~~~~~~~~~~
121
122Building with ninja is required for development to enable tests.
123Unfortunately, doing so requires additional configuration as we cannot
124just specify a toolset.
125
126.. code-block:: batch
127
128 > cmake -G Ninja ^
129 -DCMAKE_MAKE_PROGRAM=/path/to/ninja ^
130 -DCMAKE_SYSTEM_NAME=Windows ^
131 -DCMAKE_C_COMPILER=clang-cl ^
132 -DCMAKE_C_FLAGS="-fms-compatibility-version=19.00 --target=i686--windows" ^
Hamza Sood4e2e4712017-12-03 10:18:35 +0000133 -DCMAKE_CXX_COMPILER=clang-cl ^
Saleem Abdulrasool3ab0b702017-02-10 03:58:20 +0000134 -DCMAKE_CXX_FLAGS="-fms-compatibility-version=19.00 --target=i686--windows" ^
135 -DLLVM_PATH=/path/to/llvm/tree ^
136 -DLIBCXX_ENABLE_SHARED=YES ^
137 -DLIBCXX_ENABLE_STATIC=NO ^
138 -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=NO ^
139 \path\to\libcxx
140 > /path/to/ninja cxx
141 > /path/to/ninja check-cxx
142
143Note that the paths specified with backward slashes must use the `\\` as the
144directory separator as clang-cl may otherwise parse the path as an argument.
145
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000146.. _`libc++abi`: http://libcxxabi.llvm.org/
147
148
149.. _CMake Options:
150
151CMake Options
152=============
153
154Here are some of the CMake variables that are used often, along with a
155brief explanation and LLVM-specific notes. For full documentation, check the
156CMake docs or execute ``cmake --help-variable VARIABLE_NAME``.
157
158**CMAKE_BUILD_TYPE**:STRING
159 Sets the build type for ``make`` based generators. Possible values are
160 Release, Debug, RelWithDebInfo and MinSizeRel. On systems like Visual Studio
161 the user sets the build type with the IDE settings.
162
163**CMAKE_INSTALL_PREFIX**:PATH
164 Path where LLVM will be installed if "make install" is invoked or the
165 "INSTALL" target is built.
166
167**CMAKE_CXX_COMPILER**:STRING
168 The C++ compiler to use when building and testing libc++.
169
170
171.. _libcxx-specific options:
172
173libc++ specific options
174-----------------------
175
Eric Fiselierfa43a5c2016-05-03 22:32:08 +0000176.. option:: LIBCXX_INSTALL_LIBRARY:BOOL
177
178 **Default**: ``ON``
179
180 Toggle the installation of the library portion of libc++.
181
182.. option:: LIBCXX_INSTALL_HEADERS:BOOL
183
184 **Default**: ``ON``
185
186 Toggle the installation of the libc++ headers.
187
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000188.. option:: LIBCXX_ENABLE_ASSERTIONS:BOOL
189
190 **Default**: ``ON``
191
192 Build libc++ with assertions enabled.
193
194.. option:: LIBCXX_BUILD_32_BITS:BOOL
195
196 **Default**: ``OFF``
197
Eric Fiselier887b86b2016-09-16 03:47:53 +0000198 Build libc++ as a 32 bit library. Also see `LLVM_BUILD_32_BITS`.
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000199
200.. option:: LIBCXX_ENABLE_SHARED:BOOL
201
202 **Default**: ``ON``
203
Eric Fiselier887b86b2016-09-16 03:47:53 +0000204 Build libc++ as a shared library. Either `LIBCXX_ENABLE_SHARED` or
205 `LIBCXX_ENABLE_STATIC` has to be enabled.
Petr Hosek13620052016-08-08 22:57:25 +0000206
207.. option:: LIBCXX_ENABLE_STATIC:BOOL
208
209 **Default**: ``ON``
210
Eric Fiselier887b86b2016-09-16 03:47:53 +0000211 Build libc++ as a static library. Either `LIBCXX_ENABLE_SHARED` or
212 `LIBCXX_ENABLE_STATIC` has to be enabled.
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000213
214.. option:: LIBCXX_LIBDIR_SUFFIX:STRING
215
216 Extra suffix to append to the directory where libraries are to be installed.
Eric Fiselier887b86b2016-09-16 03:47:53 +0000217 This option overrides `LLVM_LIBDIR_SUFFIX`.
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000218
Petr Hosek3975d8d2017-07-11 02:39:50 +0000219.. option:: LIBCXX_INSTALL_PREFIX:STRING
220
221 **Default**: ``""``
222
223 Define libc++ destination prefix.
Eric Fiselierfa43a5c2016-05-03 22:32:08 +0000224
225.. _libc++experimental options:
226
227libc++experimental Specific Options
228------------------------------------
229
230.. option:: LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY:BOOL
231
232 **Default**: ``ON``
233
234 Build and test libc++experimental.a.
235
236.. option:: LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY:BOOL
237
Eric Fiselier121594e2016-09-07 01:15:10 +0000238 **Default**: ``LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY AND LIBCXX_INSTALL_LIBRARY``
Eric Fiselierfa43a5c2016-05-03 22:32:08 +0000239
240 Install libc++experimental.a alongside libc++.
241
242
Eric Fiselier435db152016-06-17 19:46:40 +0000243.. option:: LIBCXX_ENABLE_FILESYSTEM:BOOL
244
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000245 **Default**: ``ON``
Eric Fiselier435db152016-06-17 19:46:40 +0000246
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000247 Build filesystem as a standalone library libc++fs.a.
Eric Fiselier435db152016-06-17 19:46:40 +0000248
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000249.. option:: LIBCXX_INSTALL_FILESYSTEM_LIBRARY:BOOL
250
251 **Default**: ``LIBCXX_ENABLE_FILESYSTEM AND LIBCXX_INSTALL_LIBRARY``
252
253 Install libc++fs.a alongside libc++.
Eric Fiselier435db152016-06-17 19:46:40 +0000254
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000255.. _ABI Library Specific Options:
256
257ABI Library Specific Options
258----------------------------
259
260.. option:: LIBCXX_CXX_ABI:STRING
261
262 **Values**: ``none``, ``libcxxabi``, ``libcxxrt``, ``libstdc++``, ``libsupc++``.
263
264 Select the ABI library to build libc++ against.
265
266.. option:: LIBCXX_CXX_ABI_INCLUDE_PATHS:PATHS
267
268 Provide additional search paths for the ABI library headers.
269
270.. option:: LIBCXX_CXX_ABI_LIBRARY_PATH:PATH
271
272 Provide the path to the ABI library that libc++ should link against.
273
274.. option:: LIBCXX_ENABLE_STATIC_ABI_LIBRARY:BOOL
275
276 **Default**: ``OFF``
277
278 If this option is enabled, libc++ will try and link the selected ABI library
279 statically.
280
Eric Fiselier0b09dd12015-10-15 22:41:51 +0000281.. option:: LIBCXX_ENABLE_ABI_LINKER_SCRIPT:BOOL
282
283 **Default**: ``ON`` by default on UNIX platforms other than Apple unless
284 'LIBCXX_ENABLE_STATIC_ABI_LIBRARY' is ON. Otherwise the default value is ``OFF``.
285
286 This option generate and installs a linker script as ``libc++.so`` which
287 links the correct ABI library.
288
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000289.. option:: LIBCXXABI_USE_LLVM_UNWINDER:BOOL
290
291 **Default**: ``OFF``
292
293 Build and use the LLVM unwinder. Note: This option can only be used when
294 libc++abi is the C++ ABI library used.
295
296
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000297libc++ Feature Options
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000298----------------------
299
300.. option:: LIBCXX_ENABLE_EXCEPTIONS:BOOL
301
302 **Default**: ``ON``
303
304 Build libc++ with exception support.
305
306.. option:: LIBCXX_ENABLE_RTTI:BOOL
307
308 **Default**: ``ON``
309
310 Build libc++ with run time type information.
311
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000312.. option:: LIBCXX_INCLUDE_BENCHMARKS:BOOL
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000313
Eric Fiselier93f212c2016-08-29 19:50:49 +0000314 **Default**: ``ON``
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000315
316 Build the libc++ benchmark tests and the Google Benchmark library needed
317 to support them.
318
Eric Fiselier18d2fa32016-10-30 22:53:00 +0000319.. option:: LIBCXX_BENCHMARK_NATIVE_STDLIB:STRING
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000320
Eric Fiselier18d2fa32016-10-30 22:53:00 +0000321 **Default**:: ``""``
322
323 **Values**:: ``libc++``, ``libstdc++``
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000324
325 Build the libc++ benchmark tests and Google Benchmark library against the
Eric Fiselier18d2fa32016-10-30 22:53:00 +0000326 specified standard library on the platform. On linux this can be used to
327 compare libc++ to libstdc++ by building the benchmark tests against both
328 standard libraries.
329
330.. option:: LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN:STRING
331
332 Use the specified GCC toolchain and standard library when building the native
333 stdlib benchmark tests.
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000334
Louis Dionne1821de42018-08-16 12:44:28 +0000335.. option:: LIBCXX_HIDE_FROM_ABI_PER_TU_BY_DEFAULT:BOOL
336
337 **Default**: ``OFF``
338
339 Pick the default for whether to constrain ABI-unstable symbols to
340 each individual translation unit. This setting controls whether
341 `_LIBCPP_HIDE_FROM_ABI_PER_TU_BY_DEFAULT` is defined by default --
342 see the documentation of that macro for details.
343
Eric Fiselier9e3e1372016-07-19 23:07:03 +0000344
345libc++ ABI Feature Options
346--------------------------
Evgeniy Stepanovda2ff7e2015-10-13 23:48:28 +0000347
348The following options allow building libc++ for a different ABI version.
349
350.. option:: LIBCXX_ABI_VERSION:STRING
351
352 **Default**: ``1``
353
354 Defines the target ABI version of libc++.
355
356.. option:: LIBCXX_ABI_UNSTABLE:BOOL
357
358 **Default**: ``OFF``
359
360 Build the "unstable" ABI version of libc++. Includes all ABI changing features
361 on top of the current stable version.
362
Eric Fiselierb33778a2018-10-30 21:44:53 +0000363.. option:: LIBCXX_ABI_NAMESPACE:STRING
364
365 **Default**: ``__n`` where ``n`` is the current ABI version.
366
367 This option defines the name of the inline ABI versioning namespace. It can be used for building
368 custom versions of libc++ with unique symbol names in order to prevent conflicts or ODR issues
369 with other libc++ versions.
370
371 .. warning::
372 When providing a custom namespace, it's the users responsibility to ensure the name won't cause
373 conflicts with other names defined by libc++, both now and in the future.
374
Shoaib Meenai4ca4b7c2017-10-04 23:17:12 +0000375.. option:: LIBCXX_ABI_DEFINES:STRING
376
377 **Default**: ``""``
378
379 A semicolon-separated list of ABI macros to persist in the site config header.
380 See ``include/__config`` for the list of ABI macros.
381
Eric Fiselierd720d1f2015-08-22 19:40:49 +0000382.. _LLVM-specific variables:
383
384LLVM-specific options
385---------------------
386
387.. option:: LLVM_LIBDIR_SUFFIX:STRING
388
389 Extra suffix to append to the directory where libraries are to be
390 installed. On a 64-bit architecture, one could use ``-DLLVM_LIBDIR_SUFFIX=64``
391 to install libraries to ``/usr/lib64``.
392
393.. option:: LLVM_BUILD_32_BITS:BOOL
394
395 Build 32-bits executables and libraries on 64-bits systems. This option is
396 available only on some 64-bits unix systems. Defaults to OFF.
397
398.. option:: LLVM_LIT_ARGS:STRING
399
400 Arguments given to lit. ``make check`` and ``make clang-test`` are affected.
401 By default, ``'-sv --no-progress-bar'`` on Visual C++ and Xcode, ``'-sv'`` on
402 others.
403
404
405Using Alternate ABI libraries
406=============================
407
408
409.. _libsupcxx:
410
411Using libsupc++ on Linux
412------------------------
413
414You will need libstdc++ in order to provide libsupc++.
415
416Figure out where the libsupc++ headers are on your system. On Ubuntu this
417is ``/usr/include/c++/<version>`` and ``/usr/include/c++/<version>/<target-triple>``
418
419You can also figure this out by running
420
421.. code-block:: bash
422
423 $ echo | g++ -Wp,-v -x c++ - -fsyntax-only
424 ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
425 ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include"
426 #include "..." search starts here:
427 #include &lt;...&gt; search starts here:
428 /usr/include/c++/4.7
429 /usr/include/c++/4.7/x86_64-linux-gnu
430 /usr/include/c++/4.7/backward
431 /usr/lib/gcc/x86_64-linux-gnu/4.7/include
432 /usr/local/include
433 /usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed
434 /usr/include/x86_64-linux-gnu
435 /usr/include
436 End of search list.
437
438Note that the first two entries happen to be what we are looking for. This
439may not be correct on other platforms.
440
441We can now run CMake:
442
443.. code-block:: bash
444
445 $ CC=clang CXX=clang++ cmake -G "Unix Makefiles" \
446 -DLIBCXX_CXX_ABI=libstdc++ \
447 -DLIBCXX_CXX_ABI_INCLUDE_PATHS="/usr/include/c++/4.7/;/usr/include/c++/4.7/x86_64-linux-gnu/" \
448 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr \
449 <libc++-source-dir>
450
451
452You can also substitute ``-DLIBCXX_CXX_ABI=libsupc++``
453above, which will cause the library to be linked to libsupc++ instead
454of libstdc++, but this is only recommended if you know that you will
455never need to link against libstdc++ in the same executable as libc++.
456GCC ships libsupc++ separately but only as a static library. If a
457program also needs to link against libstdc++, it will provide its
458own copy of libsupc++ and this can lead to subtle problems.
459
460.. code-block:: bash
461
462 $ make cxx
463 $ make install
464
465You can now run clang with -stdlib=libc++.
Eric Fiseliera0733922016-06-02 02:16:28 +0000466
467
468.. _libcxxrt_ref:
469
470Using libcxxrt on Linux
471------------------------
472
473You will need to keep the source tree of `libcxxrt`_ available
474on your build machine and your copy of the libcxxrt shared library must
475be placed where your linker will find it.
476
477We can now run CMake like:
478
479.. code-block:: bash
480
481 $ CC=clang CXX=clang++ cmake -G "Unix Makefiles" \
482 -DLIBCXX_CXX_ABI=libcxxrt \
483 -DLIBCXX_CXX_ABI_INCLUDE_PATHS=path/to/libcxxrt-sources/src \
484 -DCMAKE_BUILD_TYPE=Release \
485 -DCMAKE_INSTALL_PREFIX=/usr \
486 <libc++-source-directory>
487 $ make cxx
488 $ make install
489
490Unfortunately you can't simply run clang with "-stdlib=libc++" at this point, as
491clang is set up to link for libc++ linked to libsupc++. To get around this
492you'll have to set up your linker yourself (or patch clang). For example,
493
494.. code-block:: bash
495
496 $ clang++ -stdlib=libc++ helloworld.cpp \
497 -nodefaultlibs -lc++ -lcxxrt -lm -lc -lgcc_s -lgcc
498
499Alternately, you could just add libcxxrt to your libraries list, which in most
500situations will give the same result:
501
502.. code-block:: bash
503
504 $ clang++ -stdlib=libc++ helloworld.cpp -lcxxrt
505
506.. _`libcxxrt`: https://github.com/pathscale/libcxxrt/
507
508
509Using a local ABI library installation
510---------------------------------------
511
512.. warning::
513 This is not recommended in almost all cases.
514
515These instructions should only be used when you can't install your ABI library.
516
517Normally you must link libc++ against a ABI shared library that the
518linker can find. If you want to build and test libc++ against an ABI
519library not in the linker's path you needq to set
520``-DLIBCXX_CXX_ABI_LIBRARY_PATH=/path/to/abi/lib`` when configuring CMake.
521
522An example build using libc++abi would look like:
523
524.. code-block:: bash
525
526 $ CC=clang CXX=clang++ cmake \
527 -DLIBCXX_CXX_ABI=libc++abi \
528 -DLIBCXX_CXX_ABI_INCLUDE_PATHS="/path/to/libcxxabi/include" \
529 -DLIBCXX_CXX_ABI_LIBRARY_PATH="/path/to/libcxxabi-build/lib" \
530 path/to/libcxx
531 $ make
532
533When testing libc++ LIT will automatically link against the proper ABI
534library.