BuildingLibcxx.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 LLVM:
  15. * ``cd where-you-want-llvm-to-live``
  16. * ``svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm``
  17. #. Checkout libc++:
  18. * ``cd where-you-want-llvm-to-live``
  19. * ``cd llvm/projects``
  20. * ``svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx``
  21. #. Checkout libc++abi:
  22. * ``cd where-you-want-llvm-to-live``
  23. * ``cd llvm/projects``
  24. * ``svn co http://llvm.org/svn/llvm-project/libcxxabi/trunk libcxxabi``
  25. #. Configure and build libc++ with libc++abi:
  26. CMake is the only supported configuration system.
  27. Clang is the preferred compiler when building and using libc++.
  28. * ``cd where you want to build llvm``
  29. * ``mkdir build``
  30. * ``cd build``
  31. * ``cmake -G <generator> [options] <path to llvm sources>``
  32. For more information about configuring libc++ see :ref:`CMake Options`.
  33. * ``make cxx`` --- will build libc++ and libc++abi.
  34. * ``make check-cxx check-cxxabi`` --- will run the test suites.
  35. Shared libraries for libc++ and libc++ abi should now be present in llvm/build/lib.
  36. See :ref:`using an alternate libc++ installation <alternate libcxx>`
  37. #. **Optional**: Install libc++ and libc++abi
  38. If your system already provides a libc++ installation it is important to be
  39. careful not to replace it. Remember Use the CMake option ``CMAKE_INSTALL_PREFIX`` to
  40. select a safe place to install libc++.
  41. * ``make install-cxx install-cxxabi`` --- Will install the libraries and the headers
  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. The instructions are for building libc++ on
  46. FreeBSD, Linux, or Mac using `libc++abi`_ as the C++ ABI library.
  47. On Linux, it is also possible to use :ref:`libsupc++ <libsupcxx>` or libcxxrt.
  48. It is sometimes beneficial to build outside of the LLVM tree. An out-of-tree
  49. build would look like this:
  50. .. code-block:: bash
  51. $ cd where-you-want-libcxx-to-live
  52. $ # Check out llvm, libc++ and libc++abi.
  53. $ ``svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm``
  54. $ ``svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx``
  55. $ ``svn co http://llvm.org/svn/llvm-project/libcxxabi/trunk libcxxabi``
  56. $ cd where-you-want-to-build
  57. $ mkdir build && cd build
  58. $ export CC=clang CXX=clang++
  59. $ cmake -DLLVM_PATH=path/to/llvm \
  60. -DLIBCXX_CXX_ABI=libcxxabi \
  61. -DLIBCXX_CXX_ABI_INCLUDE_PATHS=path/to/libcxxabi/include \
  62. path/to/libcxx
  63. $ make
  64. $ make check-libcxx # optional
  65. Experimental Support for Windows
  66. --------------------------------
  67. The Windows support requires building with clang-cl as cl does not support one
  68. required extension: `#include_next`. Furthermore, VS 2015 or newer (19.00) is
  69. required. In the case of clang-cl, we need to specify the "MS Compatibility
  70. Version" as it defaults to 2014 (18.00).
  71. CMake + Visual Studio
  72. ~~~~~~~~~~~~~~~~~~~~~
  73. Building with Visual Studio currently does not permit running tests. However,
  74. it is the simplest way to build.
  75. .. code-block:: batch
  76. > cmake -G "Visual Studio 14 2015" ^
  77. -T "LLVM-vs2014" ^
  78. -DLIBCXX_ENABLE_SHARED=YES ^
  79. -DLIBCXX_ENABLE_STATIC=NO ^
  80. -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=NO ^
  81. \path\to\libcxx
  82. > cmake --build .
  83. CMake + ninja
  84. ~~~~~~~~~~~~~
  85. Building with ninja is required for development to enable tests.
  86. Unfortunately, doing so requires additional configuration as we cannot
  87. just specify a toolset.
  88. .. code-block:: batch
  89. > cmake -G Ninja ^
  90. -DCMAKE_MAKE_PROGRAM=/path/to/ninja ^
  91. -DCMAKE_SYSTEM_NAME=Windows ^
  92. -DCMAKE_C_COMPILER=clang-cl ^
  93. -DCMAKE_C_FLAGS="-fms-compatibility-version=19.00 --target=i686--windows" ^
  94. -DCMAKE_CXX_COMPILER=clang-c ^
  95. -DCMAKE_CXX_FLAGS="-fms-compatibility-version=19.00 --target=i686--windows" ^
  96. -DLLVM_PATH=/path/to/llvm/tree ^
  97. -DLIBCXX_ENABLE_SHARED=YES ^
  98. -DLIBCXX_ENABLE_STATIC=NO ^
  99. -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=NO ^
  100. \path\to\libcxx
  101. > /path/to/ninja cxx
  102. > /path/to/ninja check-cxx
  103. Note that the paths specified with backward slashes must use the `\\` as the
  104. directory separator as clang-cl may otherwise parse the path as an argument.
  105. .. _`libc++abi`: http://libcxxabi.llvm.org/
  106. .. _CMake Options:
  107. CMake Options
  108. =============
  109. Here are some of the CMake variables that are used often, along with a
  110. brief explanation and LLVM-specific notes. For full documentation, check the
  111. CMake docs or execute ``cmake --help-variable VARIABLE_NAME``.
  112. **CMAKE_BUILD_TYPE**:STRING
  113. Sets the build type for ``make`` based generators. Possible values are
  114. Release, Debug, RelWithDebInfo and MinSizeRel. On systems like Visual Studio
  115. the user sets the build type with the IDE settings.
  116. **CMAKE_INSTALL_PREFIX**:PATH
  117. Path where LLVM will be installed if "make install" is invoked or the
  118. "INSTALL" target is built.
  119. **CMAKE_CXX_COMPILER**:STRING
  120. The C++ compiler to use when building and testing libc++.
  121. .. _libcxx-specific options:
  122. libc++ specific options
  123. -----------------------
  124. .. option:: LIBCXX_INSTALL_LIBRARY:BOOL
  125. **Default**: ``ON``
  126. Toggle the installation of the library portion of libc++.
  127. .. option:: LIBCXX_INSTALL_HEADERS:BOOL
  128. **Default**: ``ON``
  129. Toggle the installation of the libc++ headers.
  130. .. option:: LIBCXX_ENABLE_ASSERTIONS:BOOL
  131. **Default**: ``ON``
  132. Build libc++ with assertions enabled.
  133. .. option:: LIBCXX_BUILD_32_BITS:BOOL
  134. **Default**: ``OFF``
  135. Build libc++ as a 32 bit library. Also see `LLVM_BUILD_32_BITS`.
  136. .. option:: LIBCXX_ENABLE_SHARED:BOOL
  137. **Default**: ``ON``
  138. Build libc++ as a shared library. Either `LIBCXX_ENABLE_SHARED` or
  139. `LIBCXX_ENABLE_STATIC` has to be enabled.
  140. .. option:: LIBCXX_ENABLE_STATIC:BOOL
  141. **Default**: ``ON``
  142. Build libc++ as a static library. Either `LIBCXX_ENABLE_SHARED` or
  143. `LIBCXX_ENABLE_STATIC` has to be enabled.
  144. .. option:: LIBCXX_LIBDIR_SUFFIX:STRING
  145. Extra suffix to append to the directory where libraries are to be installed.
  146. This option overrides `LLVM_LIBDIR_SUFFIX`.
  147. .. option:: LIBCXX_INSTALL_PREFIX:STRING
  148. **Default**: ``""``
  149. Define libc++ destination prefix.
  150. .. _libc++experimental options:
  151. libc++experimental Specific Options
  152. ------------------------------------
  153. .. option:: LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY:BOOL
  154. **Default**: ``ON``
  155. Build and test libc++experimental.a.
  156. .. option:: LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY:BOOL
  157. **Default**: ``LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY AND LIBCXX_INSTALL_LIBRARY``
  158. Install libc++experimental.a alongside libc++.
  159. .. option:: LIBCXX_ENABLE_FILESYSTEM:BOOL
  160. **Default**: ``LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY``
  161. Build filesystem as part of libc++experimental.a. This allows filesystem
  162. to be disabled without turning off the entire experimental library.
  163. .. _ABI Library Specific Options:
  164. ABI Library Specific Options
  165. ----------------------------
  166. .. option:: LIBCXX_CXX_ABI:STRING
  167. **Values**: ``none``, ``libcxxabi``, ``libcxxrt``, ``libstdc++``, ``libsupc++``.
  168. Select the ABI library to build libc++ against.
  169. .. option:: LIBCXX_CXX_ABI_INCLUDE_PATHS:PATHS
  170. Provide additional search paths for the ABI library headers.
  171. .. option:: LIBCXX_CXX_ABI_LIBRARY_PATH:PATH
  172. Provide the path to the ABI library that libc++ should link against.
  173. .. option:: LIBCXX_ENABLE_STATIC_ABI_LIBRARY:BOOL
  174. **Default**: ``OFF``
  175. If this option is enabled, libc++ will try and link the selected ABI library
  176. statically.
  177. .. option:: LIBCXX_ENABLE_ABI_LINKER_SCRIPT:BOOL
  178. **Default**: ``ON`` by default on UNIX platforms other than Apple unless
  179. 'LIBCXX_ENABLE_STATIC_ABI_LIBRARY' is ON. Otherwise the default value is ``OFF``.
  180. This option generate and installs a linker script as ``libc++.so`` which
  181. links the correct ABI library.
  182. .. option:: LIBCXXABI_USE_LLVM_UNWINDER:BOOL
  183. **Default**: ``OFF``
  184. Build and use the LLVM unwinder. Note: This option can only be used when
  185. libc++abi is the C++ ABI library used.
  186. libc++ Feature Options
  187. ----------------------
  188. .. option:: LIBCXX_ENABLE_EXCEPTIONS:BOOL
  189. **Default**: ``ON``
  190. Build libc++ with exception support.
  191. .. option:: LIBCXX_ENABLE_RTTI:BOOL
  192. **Default**: ``ON``
  193. Build libc++ with run time type information.
  194. .. option:: LIBCXX_INCLUDE_BENCHMARKS:BOOL
  195. **Default**: ``ON``
  196. Build the libc++ benchmark tests and the Google Benchmark library needed
  197. to support them.
  198. .. option:: LIBCXX_BENCHMARK_NATIVE_STDLIB:STRING
  199. **Default**:: ``""``
  200. **Values**:: ``libc++``, ``libstdc++``
  201. Build the libc++ benchmark tests and Google Benchmark library against the
  202. specified standard library on the platform. On linux this can be used to
  203. compare libc++ to libstdc++ by building the benchmark tests against both
  204. standard libraries.
  205. .. option:: LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN:STRING
  206. Use the specified GCC toolchain and standard library when building the native
  207. stdlib benchmark tests.
  208. libc++ ABI Feature Options
  209. --------------------------
  210. The following options allow building libc++ for a different ABI version.
  211. .. option:: LIBCXX_ABI_VERSION:STRING
  212. **Default**: ``1``
  213. Defines the target ABI version of libc++.
  214. .. option:: LIBCXX_ABI_UNSTABLE:BOOL
  215. **Default**: ``OFF``
  216. Build the "unstable" ABI version of libc++. Includes all ABI changing features
  217. on top of the current stable version.
  218. .. _LLVM-specific variables:
  219. LLVM-specific options
  220. ---------------------
  221. .. option:: LLVM_LIBDIR_SUFFIX:STRING
  222. Extra suffix to append to the directory where libraries are to be
  223. installed. On a 64-bit architecture, one could use ``-DLLVM_LIBDIR_SUFFIX=64``
  224. to install libraries to ``/usr/lib64``.
  225. .. option:: LLVM_BUILD_32_BITS:BOOL
  226. Build 32-bits executables and libraries on 64-bits systems. This option is
  227. available only on some 64-bits unix systems. Defaults to OFF.
  228. .. option:: LLVM_LIT_ARGS:STRING
  229. Arguments given to lit. ``make check`` and ``make clang-test`` are affected.
  230. By default, ``'-sv --no-progress-bar'`` on Visual C++ and Xcode, ``'-sv'`` on
  231. others.
  232. Using Alternate ABI libraries
  233. =============================
  234. .. _libsupcxx:
  235. Using libsupc++ on Linux
  236. ------------------------
  237. You will need libstdc++ in order to provide libsupc++.
  238. Figure out where the libsupc++ headers are on your system. On Ubuntu this
  239. is ``/usr/include/c++/<version>`` and ``/usr/include/c++/<version>/<target-triple>``
  240. You can also figure this out by running
  241. .. code-block:: bash
  242. $ echo | g++ -Wp,-v -x c++ - -fsyntax-only
  243. ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
  244. ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include"
  245. #include "..." search starts here:
  246. #include &lt;...&gt; search starts here:
  247. /usr/include/c++/4.7
  248. /usr/include/c++/4.7/x86_64-linux-gnu
  249. /usr/include/c++/4.7/backward
  250. /usr/lib/gcc/x86_64-linux-gnu/4.7/include
  251. /usr/local/include
  252. /usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed
  253. /usr/include/x86_64-linux-gnu
  254. /usr/include
  255. End of search list.
  256. Note that the first two entries happen to be what we are looking for. This
  257. may not be correct on other platforms.
  258. We can now run CMake:
  259. .. code-block:: bash
  260. $ CC=clang CXX=clang++ cmake -G "Unix Makefiles" \
  261. -DLIBCXX_CXX_ABI=libstdc++ \
  262. -DLIBCXX_CXX_ABI_INCLUDE_PATHS="/usr/include/c++/4.7/;/usr/include/c++/4.7/x86_64-linux-gnu/" \
  263. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr \
  264. <libc++-source-dir>
  265. You can also substitute ``-DLIBCXX_CXX_ABI=libsupc++``
  266. above, which will cause the library to be linked to libsupc++ instead
  267. of libstdc++, but this is only recommended if you know that you will
  268. never need to link against libstdc++ in the same executable as libc++.
  269. GCC ships libsupc++ separately but only as a static library. If a
  270. program also needs to link against libstdc++, it will provide its
  271. own copy of libsupc++ and this can lead to subtle problems.
  272. .. code-block:: bash
  273. $ make cxx
  274. $ make install
  275. You can now run clang with -stdlib=libc++.
  276. .. _libcxxrt_ref:
  277. Using libcxxrt on Linux
  278. ------------------------
  279. You will need to keep the source tree of `libcxxrt`_ available
  280. on your build machine and your copy of the libcxxrt shared library must
  281. be placed where your linker will find it.
  282. We can now run CMake like:
  283. .. code-block:: bash
  284. $ CC=clang CXX=clang++ cmake -G "Unix Makefiles" \
  285. -DLIBCXX_CXX_ABI=libcxxrt \
  286. -DLIBCXX_CXX_ABI_INCLUDE_PATHS=path/to/libcxxrt-sources/src \
  287. -DCMAKE_BUILD_TYPE=Release \
  288. -DCMAKE_INSTALL_PREFIX=/usr \
  289. <libc++-source-directory>
  290. $ make cxx
  291. $ make install
  292. Unfortunately you can't simply run clang with "-stdlib=libc++" at this point, as
  293. clang is set up to link for libc++ linked to libsupc++. To get around this
  294. you'll have to set up your linker yourself (or patch clang). For example,
  295. .. code-block:: bash
  296. $ clang++ -stdlib=libc++ helloworld.cpp \
  297. -nodefaultlibs -lc++ -lcxxrt -lm -lc -lgcc_s -lgcc
  298. Alternately, you could just add libcxxrt to your libraries list, which in most
  299. situations will give the same result:
  300. .. code-block:: bash
  301. $ clang++ -stdlib=libc++ helloworld.cpp -lcxxrt
  302. .. _`libcxxrt`: https://github.com/pathscale/libcxxrt/
  303. Using a local ABI library installation
  304. ---------------------------------------
  305. .. warning::
  306. This is not recommended in almost all cases.
  307. These instructions should only be used when you can't install your ABI library.
  308. Normally you must link libc++ against a ABI shared library that the
  309. linker can find. If you want to build and test libc++ against an ABI
  310. library not in the linker's path you needq to set
  311. ``-DLIBCXX_CXX_ABI_LIBRARY_PATH=/path/to/abi/lib`` when configuring CMake.
  312. An example build using libc++abi would look like:
  313. .. code-block:: bash
  314. $ CC=clang CXX=clang++ cmake \
  315. -DLIBCXX_CXX_ABI=libc++abi \
  316. -DLIBCXX_CXX_ABI_INCLUDE_PATHS="/path/to/libcxxabi/include" \
  317. -DLIBCXX_CXX_ABI_LIBRARY_PATH="/path/to/libcxxabi-build/lib" \
  318. path/to/libcxx
  319. $ make
  320. When testing libc++ LIT will automatically link against the proper ABI
  321. library.