7 .. contents:: Table of Contents
11 One can choose to use LLVM's libc in the overlay mode. In this mode, the link
12 order semantics are exploited to pick symbols from ``libllvmlibc.a`` (if they
13 are available in ``libllvmlibc.a``) and the rest are picked from the system
14 libc. The user programs also have to use header files from the system libc.
15 Naturally, only functions which do not depend on implementation specific ABI
16 are included in ``libllvmlibc.a``. Examples of such functions are ``strlen``
17 and ``round``. Functions like ``fopen`` and friends are not included as they
18 depend on the implementation specific definition of the ``FILE`` data structure.
20 Building the libc in the overlay mode
21 =====================================
23 There are two different ways in which the libc can be built for use in the
24 overlay mode. In both the ways, we build a static archive named
25 ``libllvmlibc.a``. We use a rather verbose name with a repeated ``lib`` to make
26 it clear that it is not the system libc, which is typically named ``libc.a``.
27 Also, if users choose to mix more than one libc with the system libc, then
28 the name ``libllvmlibc.a`` makes it absolutely clear that it is the static
29 archive of LLVM's libc.
31 Building the static archive with libc as a normal LLVM project
32 --------------------------------------------------------------
34 We can treat the ``libc`` project as any other normal LLVM project and perform
35 the CMake configure step as follows:
39 $> cd llvm-project # The llvm-project checkout
42 $> cmake ../llvm -G Ninja -DLLVM_ENABLE_PROJECTS="libc" \
43 -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \
44 -DCMAKE_BUILD_TYPE=<Debug|Release> \ # Select build type
45 -DCMAKE_INSTALL_PREFIX=<Your prefix of choice> # Optional
53 The build step will build the static archive the in the directory
54 ``build/projects/libc/lib``. Notice that the above CMake configure step also
55 specified an install prefix. This is optional, but if one uses it, then they
56 can follow up the build step with an install step:
60 $> ninja install-llvmlibc
62 Building the static archive as part of the bootstrap build
63 ----------------------------------------------------------
65 The bootstrap build is a build mode in which runtime components like libc++,
66 libcxx-abi, libc etc. are built using the ToT clang. The idea is that this build
67 produces an in-sync toolchain of compiler + runtime libraries. Such a synchrony
68 is not essential for the libc but can one still build the overlay static archive
69 as part of the bootstrap build if one wants to. The first step is to configure
74 $> cmake ../llvm -G Ninja -DLLVM_ENABLE_PROJECTS="clang" \
75 -DLLVM_ENABLE_RUNTIMES="libc" \ # libc is listed as runtime and not as a project
76 -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \
77 -DCMAKE_BUILD_TYPE=<Debug|Release> \ # Select build type
78 -DCMAKE_INSTALL_PREFIX=<Your prefix of choice> # Optional
80 The build and install steps are similar to the those used when configured
81 as a normal project. Note that the build step takes much longer this time
82 as ``clang`` will be built before building ``libllvmlibc.a``.
87 $> ninja install-llvmlibc
89 Using the overlay static archive
90 ================================
92 Once built (and optionally installed), the overlay static archive can be linked
93 to your binaries like any other static archive. For example, when building with
94 ``clang`` on Linux, one should follow a recipe like:
99 $> clang <other compiler and/or linker options> <file.o|c(pp)> \
100 -L <path to the directory in which libllvmlibc.a is installed> \ # Optional
103 If you installed ``libllvmlibc.a`` in a standard linker lookup path, for example
104 ``/usr/local/lib`` on Linux like systems, then specifying the path to the
105 static archive using the ``-L`` option is not necessary.
107 Linking the static archive to other LLVM binaries
108 -------------------------------------------------
110 Since the libc and other LLVM binaries are developed in the same source tree,
111 linking ``libllvmlibc.a`` to those LLVM binaries does not require any special
112 install step or explicitly passing any special linker flags/options. One can
113 simply add ``llvmlibc`` as a link library to that binary's target. For example,
114 if you want to link ``libllvmlibc.a`` to ``llvm-objcopy``, all you have to do
115 is to add a CMake command as follows:
117 .. code-block:: cmake
119 target_link_libraries(llvm-objcopy PRIVATE llvmlibc)