BuildingLibcxx.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. .. _`libc++abi`: http://libcxxabi.llvm.org/
  66. .. _CMake Options:
  67. CMake Options
  68. =============
  69. Here are some of the CMake variables that are used often, along with a
  70. brief explanation and LLVM-specific notes. For full documentation, check the
  71. CMake docs or execute ``cmake --help-variable VARIABLE_NAME``.
  72. **CMAKE_BUILD_TYPE**:STRING
  73. Sets the build type for ``make`` based generators. Possible values are
  74. Release, Debug, RelWithDebInfo and MinSizeRel. On systems like Visual Studio
  75. the user sets the build type with the IDE settings.
  76. **CMAKE_INSTALL_PREFIX**:PATH
  77. Path where LLVM will be installed if "make install" is invoked or the
  78. "INSTALL" target is built.
  79. **CMAKE_CXX_COMPILER**:STRING
  80. The C++ compiler to use when building and testing libc++.
  81. .. _libcxx-specific options:
  82. libc++ specific options
  83. -----------------------
  84. .. option:: LIBCXX_INSTALL_LIBRARY:BOOL
  85. **Default**: ``ON``
  86. Toggle the installation of the library portion of libc++.
  87. .. option:: LIBCXX_INSTALL_HEADERS:BOOL
  88. **Default**: ``ON``
  89. Toggle the installation of the libc++ headers.
  90. .. option:: LIBCXX_ENABLE_ASSERTIONS:BOOL
  91. **Default**: ``ON``
  92. Build libc++ with assertions enabled.
  93. .. option:: LIBCXX_BUILD_32_BITS:BOOL
  94. **Default**: ``OFF``
  95. Build libc++ as a 32 bit library. Also see `LLVM_BUILD_32_BITS`.
  96. .. option:: LIBCXX_ENABLE_SHARED:BOOL
  97. **Default**: ``ON``
  98. Build libc++ as a shared library. Either `LIBCXX_ENABLE_SHARED` or
  99. `LIBCXX_ENABLE_STATIC` has to be enabled.
  100. .. option:: LIBCXX_ENABLE_STATIC:BOOL
  101. **Default**: ``ON``
  102. Build libc++ as a static library. Either `LIBCXX_ENABLE_SHARED` or
  103. `LIBCXX_ENABLE_STATIC` has to be enabled.
  104. .. option:: LIBCXX_LIBDIR_SUFFIX:STRING
  105. Extra suffix to append to the directory where libraries are to be installed.
  106. This option overrides `LLVM_LIBDIR_SUFFIX`.
  107. .. _libc++experimental options:
  108. libc++experimental Specific Options
  109. ------------------------------------
  110. .. option:: LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY:BOOL
  111. **Default**: ``ON``
  112. Build and test libc++experimental.a.
  113. .. option:: LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY:BOOL
  114. **Default**: ``LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY AND LIBCXX_INSTALL_LIBRARY``
  115. Install libc++experimental.a alongside libc++.
  116. .. option:: LIBCXX_ENABLE_FILESYSTEM:BOOL
  117. **Default**: ``LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY``
  118. Build filesystem as part of libc++experimental.a. This allows filesystem
  119. to be disabled without turning off the entire experimental library.
  120. .. _ABI Library Specific Options:
  121. ABI Library Specific Options
  122. ----------------------------
  123. .. option:: LIBCXX_CXX_ABI:STRING
  124. **Values**: ``none``, ``libcxxabi``, ``libcxxrt``, ``libstdc++``, ``libsupc++``.
  125. Select the ABI library to build libc++ against.
  126. .. option:: LIBCXX_CXX_ABI_INCLUDE_PATHS:PATHS
  127. Provide additional search paths for the ABI library headers.
  128. .. option:: LIBCXX_CXX_ABI_LIBRARY_PATH:PATH
  129. Provide the path to the ABI library that libc++ should link against.
  130. .. option:: LIBCXX_ENABLE_STATIC_ABI_LIBRARY:BOOL
  131. **Default**: ``OFF``
  132. If this option is enabled, libc++ will try and link the selected ABI library
  133. statically.
  134. .. option:: LIBCXX_ENABLE_ABI_LINKER_SCRIPT:BOOL
  135. **Default**: ``ON`` by default on UNIX platforms other than Apple unless
  136. 'LIBCXX_ENABLE_STATIC_ABI_LIBRARY' is ON. Otherwise the default value is ``OFF``.
  137. This option generate and installs a linker script as ``libc++.so`` which
  138. links the correct ABI library.
  139. .. option:: LIBCXXABI_USE_LLVM_UNWINDER:BOOL
  140. **Default**: ``OFF``
  141. Build and use the LLVM unwinder. Note: This option can only be used when
  142. libc++abi is the C++ ABI library used.
  143. libc++ Feature Options
  144. ----------------------
  145. .. option:: LIBCXX_ENABLE_EXCEPTIONS:BOOL
  146. **Default**: ``ON``
  147. Build libc++ with exception support.
  148. .. option:: LIBCXX_ENABLE_RTTI:BOOL
  149. **Default**: ``ON``
  150. Build libc++ with run time type information.
  151. .. option:: LIBCXX_INCLUDE_BENCHMARKS:BOOL
  152. **Default**: ``ON``
  153. Build the libc++ benchmark tests and the Google Benchmark library needed
  154. to support them.
  155. .. option:: LIBCXX_BENCHMARK_NATIVE_STDLIB:STRING
  156. **Default**:: ``""``
  157. **Values**:: ``libc++``, ``libstdc++``
  158. Build the libc++ benchmark tests and Google Benchmark library against the
  159. specified standard library on the platform. On linux this can be used to
  160. compare libc++ to libstdc++ by building the benchmark tests against both
  161. standard libraries.
  162. .. option:: LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN:STRING
  163. Use the specified GCC toolchain and standard library when building the native
  164. stdlib benchmark tests.
  165. libc++ ABI Feature Options
  166. --------------------------
  167. The following options allow building libc++ for a different ABI version.
  168. .. option:: LIBCXX_ABI_VERSION:STRING
  169. **Default**: ``1``
  170. Defines the target ABI version of libc++.
  171. .. option:: LIBCXX_ABI_UNSTABLE:BOOL
  172. **Default**: ``OFF``
  173. Build the "unstable" ABI version of libc++. Includes all ABI changing features
  174. on top of the current stable version.
  175. .. _LLVM-specific variables:
  176. LLVM-specific options
  177. ---------------------
  178. .. option:: LLVM_LIBDIR_SUFFIX:STRING
  179. Extra suffix to append to the directory where libraries are to be
  180. installed. On a 64-bit architecture, one could use ``-DLLVM_LIBDIR_SUFFIX=64``
  181. to install libraries to ``/usr/lib64``.
  182. .. option:: LLVM_BUILD_32_BITS:BOOL
  183. Build 32-bits executables and libraries on 64-bits systems. This option is
  184. available only on some 64-bits unix systems. Defaults to OFF.
  185. .. option:: LLVM_LIT_ARGS:STRING
  186. Arguments given to lit. ``make check`` and ``make clang-test`` are affected.
  187. By default, ``'-sv --no-progress-bar'`` on Visual C++ and Xcode, ``'-sv'`` on
  188. others.
  189. Using Alternate ABI libraries
  190. =============================
  191. .. _libsupcxx:
  192. Using libsupc++ on Linux
  193. ------------------------
  194. You will need libstdc++ in order to provide libsupc++.
  195. Figure out where the libsupc++ headers are on your system. On Ubuntu this
  196. is ``/usr/include/c++/<version>`` and ``/usr/include/c++/<version>/<target-triple>``
  197. You can also figure this out by running
  198. .. code-block:: bash
  199. $ echo | g++ -Wp,-v -x c++ - -fsyntax-only
  200. ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
  201. ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include"
  202. #include "..." search starts here:
  203. #include &lt;...&gt; search starts here:
  204. /usr/include/c++/4.7
  205. /usr/include/c++/4.7/x86_64-linux-gnu
  206. /usr/include/c++/4.7/backward
  207. /usr/lib/gcc/x86_64-linux-gnu/4.7/include
  208. /usr/local/include
  209. /usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed
  210. /usr/include/x86_64-linux-gnu
  211. /usr/include
  212. End of search list.
  213. Note that the first two entries happen to be what we are looking for. This
  214. may not be correct on other platforms.
  215. We can now run CMake:
  216. .. code-block:: bash
  217. $ CC=clang CXX=clang++ cmake -G "Unix Makefiles" \
  218. -DLIBCXX_CXX_ABI=libstdc++ \
  219. -DLIBCXX_CXX_ABI_INCLUDE_PATHS="/usr/include/c++/4.7/;/usr/include/c++/4.7/x86_64-linux-gnu/" \
  220. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr \
  221. <libc++-source-dir>
  222. You can also substitute ``-DLIBCXX_CXX_ABI=libsupc++``
  223. above, which will cause the library to be linked to libsupc++ instead
  224. of libstdc++, but this is only recommended if you know that you will
  225. never need to link against libstdc++ in the same executable as libc++.
  226. GCC ships libsupc++ separately but only as a static library. If a
  227. program also needs to link against libstdc++, it will provide its
  228. own copy of libsupc++ and this can lead to subtle problems.
  229. .. code-block:: bash
  230. $ make cxx
  231. $ make install
  232. You can now run clang with -stdlib=libc++.
  233. .. _libcxxrt_ref:
  234. Using libcxxrt on Linux
  235. ------------------------
  236. You will need to keep the source tree of `libcxxrt`_ available
  237. on your build machine and your copy of the libcxxrt shared library must
  238. be placed where your linker will find it.
  239. We can now run CMake like:
  240. .. code-block:: bash
  241. $ CC=clang CXX=clang++ cmake -G "Unix Makefiles" \
  242. -DLIBCXX_CXX_ABI=libcxxrt \
  243. -DLIBCXX_CXX_ABI_INCLUDE_PATHS=path/to/libcxxrt-sources/src \
  244. -DCMAKE_BUILD_TYPE=Release \
  245. -DCMAKE_INSTALL_PREFIX=/usr \
  246. <libc++-source-directory>
  247. $ make cxx
  248. $ make install
  249. Unfortunately you can't simply run clang with "-stdlib=libc++" at this point, as
  250. clang is set up to link for libc++ linked to libsupc++. To get around this
  251. you'll have to set up your linker yourself (or patch clang). For example,
  252. .. code-block:: bash
  253. $ clang++ -stdlib=libc++ helloworld.cpp \
  254. -nodefaultlibs -lc++ -lcxxrt -lm -lc -lgcc_s -lgcc
  255. Alternately, you could just add libcxxrt to your libraries list, which in most
  256. situations will give the same result:
  257. .. code-block:: bash
  258. $ clang++ -stdlib=libc++ helloworld.cpp -lcxxrt
  259. .. _`libcxxrt`: https://github.com/pathscale/libcxxrt/
  260. Using a local ABI library installation
  261. ---------------------------------------
  262. .. warning::
  263. This is not recommended in almost all cases.
  264. These instructions should only be used when you can't install your ABI library.
  265. Normally you must link libc++ against a ABI shared library that the
  266. linker can find. If you want to build and test libc++ against an ABI
  267. library not in the linker's path you needq to set
  268. ``-DLIBCXX_CXX_ABI_LIBRARY_PATH=/path/to/abi/lib`` when configuring CMake.
  269. An example build using libc++abi would look like:
  270. .. code-block:: bash
  271. $ CC=clang CXX=clang++ cmake \
  272. -DLIBCXX_CXX_ABI=libc++abi \
  273. -DLIBCXX_CXX_ABI_INCLUDE_PATHS="/path/to/libcxxabi/include" \
  274. -DLIBCXX_CXX_ABI_LIBRARY_PATH="/path/to/libcxxabi-build/lib" \
  275. path/to/libcxx
  276. $ make
  277. When testing libc++ LIT will automatically link against the proper ABI
  278. library.