blob: cd1ec89e5d5e906d0a7decff557520445ba0fb01 [file] [log] [blame]
Siva Chandra Reddy36de85f2023-01-05 01:09:28 -08001.. _full_cross_build:
2
3================
4Full Cross Build
5================
6
7.. contents:: Table of Contents
8 :depth: 1
9 :local:
10
Roland McGrath9abcca52024-12-30 15:36:53 -080011.. note::
Michael Jonesb89fef82024-08-21 10:50:39 -070012 Fullbuild requires running headergen, which is a python program that depends on
13 pyyaml. The minimum versions are listed on the :ref:`header_generation`
14 page, as well as additional information.
15
Siva Chandra Reddy733ac922023-03-31 12:19:58 -070016In this document, we will present recipes to cross build the full libc. When we
17say *cross build* a full libc, we mean that we will build the full libc for a
18target system which is not the same as the system on which the libc is being
19built. For example, you could be building for a bare metal aarch64 *target* on a
20Linux x86_64 *host*.
Siva Chandra Reddy36de85f2023-01-05 01:09:28 -080021
Michael Jonesb89fef82024-08-21 10:50:39 -070022There are two main recipes to cross build the full libc. Each one serves a
Siva Chandra Reddy733ac922023-03-31 12:19:58 -070023different use case. Below is a short description of these recipes to help users
24pick the recipe that best suites their needs and contexts.
Siva Chandra Reddy36de85f2023-01-05 01:09:28 -080025
Siva Chandra Reddy733ac922023-03-31 12:19:58 -070026* **Standalone cross build** - Using this recipe one can build the libc using a
27 compiler of their choice. One should use this recipe if their compiler can
28 build for the host as well as the target.
Siva Chandra Reddy733ac922023-03-31 12:19:58 -070029* **Bootstrap cross build** - In this recipe, one will build the ``clang``
30 compiler and the libc build tools for the host first, and then use them to
Michael Jonesb89fef82024-08-21 10:50:39 -070031 build the libc for the target. Unlike with the standalone build recipe, the
32 user does not have explicitly build ``clang`` and other build tools.
Siva Chandra Reddy733ac922023-03-31 12:19:58 -070033 They get built automatically before building the libc. One should use this
34 recipe if they intend use the built ``clang`` and the libc as part of their
35 toolchain for the target.
36
Michael Jonesb89fef82024-08-21 10:50:39 -070037The following sections present the two recipes in detail.
Siva Chandra Reddy733ac922023-03-31 12:19:58 -070038
39Standalone cross build
40======================
41
42In the *standalone crossbuild* recipe, the system compiler or a custom compiler
43of user's choice is used to build the libc. The necessary build tools for the
44host are built first, and those build tools are then used to build the libc for
45the target. Both these steps happen automatically, as in, the user does not have
46to explicitly build the build tools first and then build the libc. A point to
47keep in mind is that the compiler used should be capable of building for the
48host as well as the target.
49
50CMake configure step
51--------------------
52
53Below is the CMake command to configure the standalone crossbuild of the libc.
Siva Chandra Reddy36de85f2023-01-05 01:09:28 -080054
55.. code-block:: sh
56
57 $> cd llvm-project # The llvm-project checkout
58 $> mkdir build
59 $> cd build
Siva Chandra Reddy733ac922023-03-31 12:19:58 -070060 $> C_COMPILER=<C compiler> # For example "clang"
61 $> CXX_COMPILER=<C++ compiler> # For example "clang++"
Michael Jonesb89fef82024-08-21 10:50:39 -070062 $> cmake ../runtimes \
Jeff Baileyea471e22023-03-14 05:31:01 +000063 -G Ninja \
Michael Jonesb89fef82024-08-21 10:50:39 -070064 -DLLVM_ENABLE_RUNTIMES=libc \
Siva Chandra Reddy733ac922023-03-31 12:19:58 -070065 -DCMAKE_C_COMPILER=$C_COMPILER \
66 -DCMAKE_CXX_COMPILER=$CXX_COMPILER \
Jeff Baileyea471e22023-03-14 05:31:01 +000067 -DLLVM_LIBC_FULL_BUILD=ON \
Siva Chandra Reddy733ac922023-03-31 12:19:58 -070068 -DLIBC_TARGET_TRIPLE=<Your target triple> \
69 -DCMAKE_BUILD_TYPE=<Release|Debug>
Siva Chandra Reddy36de85f2023-01-05 01:09:28 -080070
71We will go over the special options passed to the ``cmake`` command above.
72
Michael Jonesb89fef82024-08-21 10:50:39 -070073* **Enabled Runtimes** - Since we want to build LLVM-libc, we list
74 ``libc`` as the enabled runtime.
Siva Chandra Reddy36de85f2023-01-05 01:09:28 -080075* **The full build option** - Since we want to build the full libc, we pass
76 ``-DLLVM_LIBC_FULL_BUILD=ON``.
77* **The target triple** - This is the target triple of the target for which
78 we are building the libc. For example, for a Linux 32-bit Arm target,
79 one can specify it as ``arm-linux-eabi``.
80
Siva Chandra Reddy733ac922023-03-31 12:19:58 -070081Build step
82----------
Siva Chandra Reddy36de85f2023-01-05 01:09:28 -080083
84After configuring the build with the above ``cmake`` command, one can build the
85the libc for the target with the following command:
86
87.. code-block:: sh
Joseph Huber69c0b2f2024-02-23 16:34:00 -060088
Siva Chandra Reddy733ac922023-03-31 12:19:58 -070089 $> ninja libc libm
Siva Chandra Reddy36de85f2023-01-05 01:09:28 -080090
Siva Chandra Reddy733ac922023-03-31 12:19:58 -070091The above ``ninja`` command will build the libc static archives ``libc.a`` and
92``libm.a`` for the target specified with ``-DLIBC_TARGET_TRIPLE`` in the CMake
93configure step.
94
Siva Chandra Reddy733ac922023-03-31 12:19:58 -070095Bootstrap cross build
96=====================
97
Roland McGrath9abcca52024-12-30 15:36:53 -080098In this recipe, the clang compiler is built automatically before building
99the libc for the target.
Siva Chandra Reddy733ac922023-03-31 12:19:58 -0700100
101CMake configure step
102--------------------
103
104.. code-block:: sh
105
106 $> cd llvm-project # The llvm-project checkout
107 $> mkdir build
108 $> cd build
109 $> C_COMPILER=<C compiler> # For example "clang"
110 $> CXX_COMPILER=<C++ compiler> # For example "clang++"
111 $> TARGET_TRIPLE=<Your target triple>
112 $> cmake ../llvm \
113 -G Ninja \
114 -DCMAKE_C_COMPILER=$C_COMPILER \
115 -DCMAKE_CXX_COMPILER=$CXX_COMPILER \
116 -DLLVM_ENABLE_PROJECTS=clang \
117 -DLLVM_ENABLE_RUNTIMES=libc \
118 -DLLVM_LIBC_FULL_BUILD=ON \
119 -DLLVM_RUNTIME_TARGETS=$TARGET_TRIPLE \
120 -DCMAKE_BUILD_TYPE=Debug
121
Michael Jonesb89fef82024-08-21 10:50:39 -0700122Note how the above cmake command differs from the one used in the other recipe:
Siva Chandra Reddy733ac922023-03-31 12:19:58 -0700123
124* ``clang`` is listed in ``-DLLVM_ENABLE_PROJECTS`` and ``libc`` is
125 listed in ``-DLLVM_ENABLE_RUNTIMES``.
126* The CMake root source directory is ``llvm-project/llvm``.
127* The target triple is specified with ``-DLLVM_RUNTIME_TARGETS``.
128
129Build step
130----------
131
Michael Jonesb89fef82024-08-21 10:50:39 -0700132The build step is similar to the other recipe:
Siva Chandra Reddy733ac922023-03-31 12:19:58 -0700133
134.. code-block:: sh
135
136 $> ninja libc
137
138The above ninja command should build the libc static archives for the target
139specified with ``-DLLVM_RUNTIME_TARGETS``.
Siva Chandra Reddy36de85f2023-01-05 01:09:28 -0800140
141Building for bare metal
142=======================
143
144To build for bare metal, all one has to do is to specify the
145`system <https://clang.llvm.org/docs/CrossCompilation.html#target-triple>`_
146component of the target triple as ``none``. For example, to build for a
14732-bit arm target on bare metal, one can use a target triple like
Siva Chandra Reddy733ac922023-03-31 12:19:58 -0700148``arm-none-eabi``. Other than that, the libc for a bare metal target can be
149built using any of the three recipes described above.
Joseph Huber0cbbcf12024-03-06 10:58:39 -0600150
151Building for the GPU
152====================
153
Roland McGrath9abcca52024-12-30 15:36:53 -0800154To build for a GPU architecture, it should only be necessary to specify the
155target triple as one of the supported GPU targets. Currently, this is either
156``nvptx64-nvidia-cuda`` for NVIDIA GPUs or ``amdgcn-amd-amdhsa`` for AMD GPUs.
157More detailed information is provided in the :ref:`GPU
Joseph Huber0cbbcf12024-03-06 10:58:39 -0600158documentation<libc_gpu_building>`.