BuildingLibcxx.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. .. _BuildingLibcxx:
  2. ===============
  3. Building libc++
  4. ===============
  5. .. contents::
  6. :local:
  7. .. _build instructions:
  8. Getting Started
  9. ===============
  10. On Mac OS 10.7 (Lion) and later, the easiest way to get this library is to install
  11. Xcode 4.2 or later. However if you want to install tip-of-trunk from here
  12. (getting the bleeding edge), read on.
  13. The basic steps needed to build libc++ are:
  14. #. Checkout and configure LLVM (including libc++ and libc++abi), according to the `LLVM
  15. getting started <https://llvm.org/docs/GettingStarted.html>`_ documentation. Make sure
  16. to include ``libcxx`` and ``libcxxabi`` in the ``LLVM_ENABLE_PROJECTS`` option passed
  17. to CMake.
  18. For more information about configuring libc++ see :ref:`CMake Options`.
  19. * ``make cxx`` --- will build libc++ and libc++abi.
  20. * ``make check-cxx check-cxxabi`` --- will run the test suites.
  21. Shared libraries for libc++ and libc++ abi should now be present in llvm/build/lib.
  22. See :ref:`using an alternate libc++ installation <alternate libcxx>`
  23. #. **Optional**: Install libc++ and libc++abi
  24. If your system already provides a libc++ installation it is important to be
  25. careful not to replace it. Remember Use the CMake option ``CMAKE_INSTALL_PREFIX`` to
  26. select a safe place to install libc++.
  27. * ``make install-cxx install-cxxabi`` --- Will install the libraries and the headers
  28. .. warning::
  29. * Replacing your systems libc++ installation could render the system non-functional.
  30. * macOS will not boot without a valid copy of ``libc++.1.dylib`` in ``/usr/lib``.
  31. The instructions are for building libc++ on
  32. FreeBSD, Linux, or Mac using `libc++abi`_ as the C++ ABI library.
  33. On Linux, it is also possible to use :ref:`libsupc++ <libsupcxx>` or libcxxrt.
  34. It is sometimes beneficial to build separately from the full LLVM build. An
  35. out-of-tree build would look like this:
  36. .. code-block:: bash
  37. $ cd where-you-want-libcxx-to-live
  38. $ # Check out the sources (includes everything, but we'll only use libcxx)
  39. $ ``git clone https://github.com/llvm/llvm-project.git``
  40. $ cd where-you-want-to-build
  41. $ mkdir build && cd build
  42. $ export CC=clang CXX=clang++
  43. $ cmake -DLLVM_PATH=path/to/separate/llvm \
  44. -DLIBCXX_CXX_ABI=libcxxabi \
  45. -DLIBCXX_CXX_ABI_INCLUDE_PATHS=path/to/separate/libcxxabi/include \
  46. path/to/llvm-project/libcxx
  47. $ make
  48. $ make check-libcxx # optional
  49. Experimental Support for Windows
  50. --------------------------------
  51. The Windows support requires building with clang-cl as cl does not support one
  52. required extension: `#include_next`. Furthermore, VS 2015 or newer (19.00) is
  53. required. In the case of clang-cl, we need to specify the "MS Compatibility
  54. Version" as it defaults to 2014 (18.00).
  55. CMake + Visual Studio
  56. ~~~~~~~~~~~~~~~~~~~~~
  57. Building with Visual Studio currently does not permit running tests. However,
  58. it is the simplest way to build.
  59. .. code-block:: batch
  60. > cmake -G "Visual Studio 14 2015" ^
  61. -T "LLVM-vs2014" ^
  62. -DLIBCXX_ENABLE_SHARED=YES ^
  63. -DLIBCXX_ENABLE_STATIC=NO ^
  64. -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=NO ^
  65. \path\to\libcxx
  66. > cmake --build .
  67. CMake + ninja
  68. ~~~~~~~~~~~~~
  69. Building with ninja is required for development to enable tests.
  70. Unfortunately, doing so requires additional configuration as we cannot
  71. just specify a toolset.
  72. .. code-block:: batch
  73. > cmake -G Ninja ^
  74. -DCMAKE_MAKE_PROGRAM=/path/to/ninja ^
  75. -DCMAKE_SYSTEM_NAME=Windows ^
  76. -DCMAKE_C_COMPILER=clang-cl ^
  77. -DCMAKE_C_FLAGS="-fms-compatibility-version=19.00 --target=i686--windows" ^
  78. -DCMAKE_CXX_COMPILER=clang-cl ^
  79. -DCMAKE_CXX_FLAGS="-fms-compatibility-version=19.00 --target=i686--windows" ^
  80. -DLLVM_PATH=/path/to/llvm/tree ^
  81. -DLIBCXX_ENABLE_SHARED=YES ^
  82. -DLIBCXX_ENABLE_STATIC=NO ^
  83. -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=NO ^
  84. \path\to\libcxx
  85. > /path/to/ninja cxx
  86. > /path/to/ninja check-cxx
  87. Note that the paths specified with backward slashes must use the `\\` as the
  88. directory separator as clang-cl may otherwise parse the path as an argument.
  89. .. _`libc++abi`: http://libcxxabi.llvm.org/
  90. .. _CMake Options:
  91. CMake Options
  92. =============
  93. Here are some of the CMake variables that are used often, along with a
  94. brief explanation and LLVM-specific notes. For full documentation, check the
  95. CMake docs or execute ``cmake --help-variable VARIABLE_NAME``.
  96. **CMAKE_BUILD_TYPE**:STRING
  97. Sets the build type for ``make`` based generators. Possible values are
  98. Release, Debug, RelWithDebInfo and MinSizeRel. On systems like Visual Studio
  99. the user sets the build type with the IDE settings.
  100. **CMAKE_INSTALL_PREFIX**:PATH
  101. Path where LLVM will be installed if "make install" is invoked or the
  102. "INSTALL" target is built.
  103. **CMAKE_CXX_COMPILER**:STRING
  104. The C++ compiler to use when building and testing libc++.
  105. .. _libcxx-specific options:
  106. libc++ specific options
  107. -----------------------
  108. .. option:: LIBCXX_INSTALL_LIBRARY:BOOL
  109. **Default**: ``ON``
  110. Toggle the installation of the library portion of libc++.
  111. .. option:: LIBCXX_INSTALL_HEADERS:BOOL
  112. **Default**: ``ON``
  113. Toggle the installation of the libc++ headers.
  114. .. option:: LIBCXX_ENABLE_ASSERTIONS:BOOL
  115. **Default**: ``ON``
  116. Build libc++ with assertions enabled.
  117. .. option:: LIBCXX_BUILD_32_BITS:BOOL
  118. **Default**: ``OFF``
  119. Build libc++ as a 32 bit library. Also see `LLVM_BUILD_32_BITS`.
  120. .. option:: LIBCXX_ENABLE_SHARED:BOOL
  121. **Default**: ``ON``
  122. Build libc++ as a shared library. Either `LIBCXX_ENABLE_SHARED` or
  123. `LIBCXX_ENABLE_STATIC` has to be enabled.
  124. .. option:: LIBCXX_ENABLE_STATIC:BOOL
  125. **Default**: ``ON``
  126. Build libc++ as a static library. Either `LIBCXX_ENABLE_SHARED` or
  127. `LIBCXX_ENABLE_STATIC` has to be enabled.
  128. .. option:: LIBCXX_LIBDIR_SUFFIX:STRING
  129. Extra suffix to append to the directory where libraries are to be installed.
  130. This option overrides `LLVM_LIBDIR_SUFFIX`.
  131. .. option:: LIBCXX_INSTALL_PREFIX:STRING
  132. **Default**: ``""``
  133. Define libc++ destination prefix.
  134. .. option:: LIBCXX_HERMETIC_STATIC_LIBRARY:BOOL
  135. **Default**: ``OFF``
  136. Do not export any symbols from the static libc++ library.
  137. This is useful when the static libc++ library is being linked into shared
  138. libraries that may be used in with other shared libraries that use different
  139. C++ library. We want to avoid exporting any libc++ symbols in that case.
  140. .. option:: LIBCXX_ENABLE_FILESYSTEM:BOOL
  141. **Default**: ``ON`` except on Windows.
  142. This option can be used to enable or disable the filesystem components on
  143. platforms that may not support them. For example on Windows.
  144. .. _libc++experimental options:
  145. libc++experimental Specific Options
  146. ------------------------------------
  147. .. option:: LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY:BOOL
  148. **Default**: ``ON``
  149. Build and test libc++experimental.a.
  150. .. option:: LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY:BOOL
  151. **Default**: ``LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY AND LIBCXX_INSTALL_LIBRARY``
  152. Install libc++experimental.a alongside libc++.
  153. .. _ABI Library Specific Options:
  154. ABI Library Specific Options
  155. ----------------------------
  156. .. option:: LIBCXX_CXX_ABI:STRING
  157. **Values**: ``none``, ``libcxxabi``, ``libcxxrt``, ``libstdc++``, ``libsupc++``.
  158. Select the ABI library to build libc++ against.
  159. .. option:: LIBCXX_CXX_ABI_INCLUDE_PATHS:PATHS
  160. Provide additional search paths for the ABI library headers.
  161. .. option:: LIBCXX_CXX_ABI_LIBRARY_PATH:PATH
  162. Provide the path to the ABI library that libc++ should link against.
  163. .. option:: LIBCXX_ENABLE_STATIC_ABI_LIBRARY:BOOL
  164. **Default**: ``OFF``
  165. If this option is enabled, libc++ will try and link the selected ABI library
  166. statically.
  167. .. option:: LIBCXX_ENABLE_ABI_LINKER_SCRIPT:BOOL
  168. **Default**: ``ON`` by default on UNIX platforms other than Apple unless
  169. 'LIBCXX_ENABLE_STATIC_ABI_LIBRARY' is ON. Otherwise the default value is ``OFF``.
  170. This option generate and installs a linker script as ``libc++.so`` which
  171. links the correct ABI library.
  172. .. option:: LIBCXXABI_USE_LLVM_UNWINDER:BOOL
  173. **Default**: ``OFF``
  174. Build and use the LLVM unwinder. Note: This option can only be used when
  175. libc++abi is the C++ ABI library used.
  176. libc++ Feature Options
  177. ----------------------
  178. .. option:: LIBCXX_ENABLE_EXCEPTIONS:BOOL
  179. **Default**: ``ON``
  180. Build libc++ with exception support.
  181. .. option:: LIBCXX_ENABLE_RTTI:BOOL
  182. **Default**: ``ON``
  183. Build libc++ with run time type information.
  184. .. option:: LIBCXX_INCLUDE_TESTS:BOOL
  185. **Default**: ``ON`` (or value of ``LLVM_INCLUDE_DIR``)
  186. Build the libc++ tests.
  187. .. option:: LIBCXX_INCLUDE_BENCHMARKS:BOOL
  188. **Default**: ``ON``
  189. Build the libc++ benchmark tests and the Google Benchmark library needed
  190. to support them.
  191. .. option:: LIBCXX_BENCHMARK_TEST_ARGS:STRING
  192. **Default**: ``--benchmark_min_time=0.01``
  193. A semicolon list of arguments to pass when running the libc++ benchmarks using the
  194. ``check-cxx-benchmarks`` rule. By default we run the benchmarks for a very short amount of time,
  195. since the primary use of ``check-cxx-benchmarks`` is to get test and sanitizer coverage, not to
  196. get accurate measurements.
  197. .. option:: LIBCXX_BENCHMARK_NATIVE_STDLIB:STRING
  198. **Default**:: ``""``
  199. **Values**:: ``libc++``, ``libstdc++``
  200. Build the libc++ benchmark tests and Google Benchmark library against the
  201. specified standard library on the platform. On Linux this can be used to
  202. compare libc++ to libstdc++ by building the benchmark tests against both
  203. standard libraries.
  204. .. option:: LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN:STRING
  205. Use the specified GCC toolchain and standard library when building the native
  206. stdlib benchmark tests.
  207. .. option:: LIBCXX_HIDE_FROM_ABI_PER_TU_BY_DEFAULT:BOOL
  208. **Default**: ``OFF``
  209. Pick the default for whether to constrain ABI-unstable symbols to
  210. each individual translation unit. This setting controls whether
  211. `_LIBCPP_HIDE_FROM_ABI_PER_TU_BY_DEFAULT` is defined by default --
  212. see the documentation of that macro for details.
  213. libc++ ABI Feature Options
  214. --------------------------
  215. The following options allow building libc++ for a different ABI version.
  216. .. option:: LIBCXX_ABI_VERSION:STRING
  217. **Default**: ``1``
  218. Defines the target ABI version of libc++.
  219. .. option:: LIBCXX_ABI_UNSTABLE:BOOL
  220. **Default**: ``OFF``
  221. Build the "unstable" ABI version of libc++. Includes all ABI changing features
  222. on top of the current stable version.
  223. .. option:: LIBCXX_ABI_NAMESPACE:STRING
  224. **Default**: ``__n`` where ``n`` is the current ABI version.
  225. This option defines the name of the inline ABI versioning namespace. It can be used for building
  226. custom versions of libc++ with unique symbol names in order to prevent conflicts or ODR issues
  227. with other libc++ versions.
  228. .. warning::
  229. When providing a custom namespace, it's the users responsibility to ensure the name won't cause
  230. conflicts with other names defined by libc++, both now and in the future. In particular, inline
  231. namespaces of the form ``__[0-9]+`` are strictly reserved by libc++ and may not be used by users.
  232. Doing otherwise could cause conflicts and hinder libc++ ABI evolution.
  233. .. option:: LIBCXX_ABI_DEFINES:STRING
  234. **Default**: ``""``
  235. A semicolon-separated list of ABI macros to persist in the site config header.
  236. See ``include/__config`` for the list of ABI macros.
  237. .. option:: LIBCXX_HAS_MERGED_TYPEINFO_NAMES_DEFAULT
  238. **Default**: ``None``. When defined this option overrides the libraries default configuration
  239. for whether merged type info names are present.
  240. Build ``std::type_info`` with the assumption that type info names for a type have been fully
  241. merged are unique across the entire program. This may not be the case for libraries built with
  242. ``-Bsymbolic`` or due to compiler or linker bugs (Ex. llvm.org/PR37398).
  243. When the value is ``ON`` typeinfo comparisons compare only the pointer value, otherwise ``strcmp``
  244. is used as a fallback.
  245. .. _LLVM-specific variables:
  246. LLVM-specific options
  247. ---------------------
  248. .. option:: LLVM_LIBDIR_SUFFIX:STRING
  249. Extra suffix to append to the directory where libraries are to be
  250. installed. On a 64-bit architecture, one could use ``-DLLVM_LIBDIR_SUFFIX=64``
  251. to install libraries to ``/usr/lib64``.
  252. .. option:: LLVM_BUILD_32_BITS:BOOL
  253. Build 32-bits executables and libraries on 64-bits systems. This option is
  254. available only on some 64-bits Unix systems. Defaults to OFF.
  255. .. option:: LLVM_LIT_ARGS:STRING
  256. Arguments given to lit. ``make check`` and ``make clang-test`` are affected.
  257. By default, ``'-sv --no-progress-bar'`` on Visual C++ and Xcode, ``'-sv'`` on
  258. others.
  259. Using Alternate ABI libraries
  260. =============================
  261. .. _libsupcxx:
  262. Using libsupc++ on Linux
  263. ------------------------
  264. You will need libstdc++ in order to provide libsupc++.
  265. Figure out where the libsupc++ headers are on your system. On Ubuntu this
  266. is ``/usr/include/c++/<version>`` and ``/usr/include/c++/<version>/<target-triple>``
  267. You can also figure this out by running
  268. .. code-block:: bash
  269. $ echo | g++ -Wp,-v -x c++ - -fsyntax-only
  270. ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
  271. ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include"
  272. #include "..." search starts here:
  273. #include &lt;...&gt; search starts here:
  274. /usr/include/c++/4.7
  275. /usr/include/c++/4.7/x86_64-linux-gnu
  276. /usr/include/c++/4.7/backward
  277. /usr/lib/gcc/x86_64-linux-gnu/4.7/include
  278. /usr/local/include
  279. /usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed
  280. /usr/include/x86_64-linux-gnu
  281. /usr/include
  282. End of search list.
  283. Note that the first two entries happen to be what we are looking for. This
  284. may not be correct on other platforms.
  285. We can now run CMake:
  286. .. code-block:: bash
  287. $ CC=clang CXX=clang++ cmake -G "Unix Makefiles" \
  288. -DLIBCXX_CXX_ABI=libstdc++ \
  289. -DLIBCXX_CXX_ABI_INCLUDE_PATHS="/usr/include/c++/4.7/;/usr/include/c++/4.7/x86_64-linux-gnu/" \
  290. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr \
  291. <libc++-source-dir>
  292. You can also substitute ``-DLIBCXX_CXX_ABI=libsupc++``
  293. above, which will cause the library to be linked to libsupc++ instead
  294. of libstdc++, but this is only recommended if you know that you will
  295. never need to link against libstdc++ in the same executable as libc++.
  296. GCC ships libsupc++ separately but only as a static library. If a
  297. program also needs to link against libstdc++, it will provide its
  298. own copy of libsupc++ and this can lead to subtle problems.
  299. .. code-block:: bash
  300. $ make cxx
  301. $ make install
  302. You can now run clang with -stdlib=libc++.
  303. .. _libcxxrt_ref:
  304. Using libcxxrt on Linux
  305. ------------------------
  306. You will need to keep the source tree of `libcxxrt`_ available
  307. on your build machine and your copy of the libcxxrt shared library must
  308. be placed where your linker will find it.
  309. We can now run CMake like:
  310. .. code-block:: bash
  311. $ CC=clang CXX=clang++ cmake -G "Unix Makefiles" \
  312. -DLIBCXX_CXX_ABI=libcxxrt \
  313. -DLIBCXX_CXX_ABI_INCLUDE_PATHS=path/to/libcxxrt-sources/src \
  314. -DCMAKE_BUILD_TYPE=Release \
  315. -DCMAKE_INSTALL_PREFIX=/usr \
  316. <libc++-source-directory>
  317. $ make cxx
  318. $ make install
  319. Unfortunately you can't simply run clang with "-stdlib=libc++" at this point, as
  320. clang is set up to link for libc++ linked to libsupc++. To get around this
  321. you'll have to set up your linker yourself (or patch clang). For example,
  322. .. code-block:: bash
  323. $ clang++ -stdlib=libc++ helloworld.cpp \
  324. -nodefaultlibs -lc++ -lcxxrt -lm -lc -lgcc_s -lgcc
  325. Alternately, you could just add libcxxrt to your libraries list, which in most
  326. situations will give the same result:
  327. .. code-block:: bash
  328. $ clang++ -stdlib=libc++ helloworld.cpp -lcxxrt
  329. .. _`libcxxrt`: https://github.com/pathscale/libcxxrt/
  330. Using a local ABI library installation
  331. ---------------------------------------
  332. .. warning::
  333. This is not recommended in almost all cases.
  334. These instructions should only be used when you can't install your ABI library.
  335. Normally you must link libc++ against a ABI shared library that the
  336. linker can find. If you want to build and test libc++ against an ABI
  337. library not in the linker's path you need to set
  338. ``-DLIBCXX_CXX_ABI_LIBRARY_PATH=/path/to/abi/lib`` when configuring CMake.
  339. An example build using libc++abi would look like:
  340. .. code-block:: bash
  341. $ CC=clang CXX=clang++ cmake \
  342. -DLIBCXX_CXX_ABI=libc++abi \
  343. -DLIBCXX_CXX_ABI_INCLUDE_PATHS="/path/to/libcxxabi/include" \
  344. -DLIBCXX_CXX_ABI_LIBRARY_PATH="/path/to/libcxxabi-build/lib" \
  345. path/to/libcxx
  346. $ make
  347. When testing libc++ LIT will automatically link against the proper ABI
  348. library.