brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.6 KiB · ca04c4c Raw
126 lines · plain
1.. _overlay_mode:2 3============4Overlay Mode5============6 7.. contents:: Table of Contents8  :depth: 49  :local:10 11One can choose to use LLVM's libc in the overlay mode. In this mode, the link12order semantics are exploited to pick symbols from ``libllvmlibc.a`` (if they13are available in ``libllvmlibc.a``) and the rest are picked from the system14libc. The user programs also have to use header files from the system libc.15Naturally, only functions which do not depend on implementation specific ABI16are included in ``libllvmlibc.a``. Examples of such functions are ``strlen``17and ``round``. Functions like ``fopen`` and friends are not included as they18depend on the implementation specific definition of the ``FILE`` data structure.19 20Building the libc in the overlay mode21=====================================22 23There are two different ways in which the libc can be built for use in the24overlay mode. In both the ways, we build a static archive named25``libllvmlibc.a``. We use a rather verbose name with a repeated ``lib`` to make26it clear that it is not the system libc, which is typically named ``libc.a``.27Also, if users choose to mix more than one libc with the system libc, then28the name ``libllvmlibc.a`` makes it absolutely clear that it is the static29archive of LLVM's libc.30 31Building LLVM-libc as a standalone runtime32------------------------------------------33 34We can treat the ``libc`` project like any other normal LLVM runtime library by35building it with the following cmake command:36 37.. code-block:: sh38 39  $> cd llvm-project  # The llvm-project checkout40  $> mkdir build41  $> cd build42  $> cmake ../runtimes -G Ninja -DLLVM_ENABLE_RUNTIMES="libc"  \43     -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \44     -DCMAKE_BUILD_TYPE=<Debug|Release>                    \  # Select build type45     -DCMAKE_INSTALL_PREFIX=<Your prefix of choice>           # Optional46 47Next, build the libc:48 49.. code-block:: sh50 51  $> ninja libc52 53Then, run the tests:54 55.. code-block:: sh56 57  $> ninja check-libc58 59The build step will build the static archive the in the directory60``build/projects/libc/lib``. Notice that the above CMake configure step also61specified an install prefix. This is optional, but it's used, then the following62command will install the static archive to the install path:63 64.. code-block:: sh65 66  $> ninja install-libc67 68Building the static archive as part of the bootstrap build69----------------------------------------------------------70 71The bootstrap build is a build mode in which runtime components like libc++,72libcxx-abi, libc etc. are built using the ToT clang. The idea is that this build73produces an in-sync toolchain of compiler + runtime libraries. This ensures that74LLVM-libc has access to the latest clang features, which should provide the best75performance possible.76 77.. code-block:: sh78 79  $> cmake ../llvm -G Ninja -DLLVM_ENABLE_PROJECTS="clang" \80     -DLLVM_ENABLE_RUNTIMES="libc"  \  # libc is listed as runtime and not as a project81     -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \82     -DCMAKE_BUILD_TYPE=<Debug|Release>                    \  # Select build type83     -DCMAKE_INSTALL_PREFIX=<Your prefix of choice>           # Optional84 85The build and install steps are the same as above, but the build step will take86much longer since ``clang`` will be built before building ``libllvmlibc.a``.87 88.. code-block:: sh89 90  $> ninja libc91  $> ninja check-libc92 93Using the overlay static archive94================================95 96Once built (and optionally installed), the overlay static archive can be linked97to your binaries like any other static archive. For example, when building with98``clang`` on Linux, one should follow a recipe like:99 100 101.. code-block:: sh102 103  $> clang <other compiler and/or linker options> <file.o|c(pp)>     \104     -L <path to the directory in which libllvmlibc.a is installed>  \ # Optional105     -lllvmlibc106 107If you installed ``libllvmlibc.a`` in a standard linker lookup path, for example108``/usr/local/lib`` on Linux like systems, then specifying the path to the109static archive using the ``-L`` option is not necessary.110 111Linking the static archive to other LLVM binaries112-------------------------------------------------113 114Since the libc and other LLVM binaries are developed in the same source tree,115linking ``libllvmlibc.a`` to those LLVM binaries does not require any special116install step or explicitly passing any special linker flags/options. One can117simply add ``llvmlibc`` as a link library to that binary's target. For example,118if you want to link ``libllvmlibc.a`` to ``llvm-objcopy``, all you have to do119is to add a CMake command as follows:120 121.. code-block:: cmake122 123  target_link_libraries(llvm-objcopy PRIVATE llvmlibc)124 125 126