Skip to content

Commit 6c3232b

Browse files
[lldb][Docs] Add simpler "automatic" cross-compile option to build docs (llvm#65311)
The main way I cross build lldb is to point CMake at an existing host build to get the native tablegen tools. This is what we had documented before. There is another option where you start from scratch and the host tools are built for you. This patch documents that and explains which one to choose. Added another arm64 example which uses this. So the frst one is the "automatic" build and the second is the traditional approach. For ease of copy paste and understanding, I've kept the full command in each section and noted the one difference between them. Along the way I updated some of the preamble to explain the two approaches and updated some language e.g. removing "just ...". Eveyone's "just" is different, doubly so when cross-compiling.
1 parent f470c36 commit 6c3232b

File tree

1 file changed

+58
-17
lines changed

1 file changed

+58
-17
lines changed

lldb/docs/resources/build.rst

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,10 @@ of CMake at this time. Please refer to `CMake's documentation <https://cmake.org
427427
if you have any doubts or want more in depth information.
428428

429429
In order to debug remote targets running different architectures than your
430-
host, you will need to compile LLDB (or at least the server component) for the
431-
target. While the easiest solution is to just compile it locally on the target,
432-
this is often not feasible, and in these cases you will need to cross-compile
433-
LLDB on your host.
430+
host, you will need to compile LLDB (or at least the server component
431+
``lldb-server``) for the target. While the easiest solution is to compile it
432+
locally on the target, this is often not feasible, and in these cases you will
433+
need to cross-compile LLDB on your host.
434434

435435
Cross-compilation is often a daunting task and has a lot of quirks which depend
436436
on the exact host and target architectures, so it is not possible to give a
@@ -470,9 +470,22 @@ If you find that CMake is finding a version of an optional dependency that
470470
for whatever reason doesn't work, consider simply disabling it if you don't
471471
know that you need it.
472472

473-
Once all of the dependencies are in place, it's just a matter of configuring
474-
the build system with the locations and arguments of all the necessary tools.
475-
The most important cmake options here are:
473+
Once all of the dependencies are in place, you need to configure the build
474+
system with the locations and arguments of all the necessary tools.
475+
476+
There are 2 ways to do this depending on your starting point and requirements.
477+
478+
1. If you are starting from scratch and only need the resulting cross compiled
479+
binaries, you can have LLVM build the native tools for you.
480+
481+
2. If you need a host build too, or already have one, you can tell CMake where
482+
that is and it will use those native tools instead.
483+
484+
If you are going to run ``lldb`` and ``lldb-server`` only on the target machine,
485+
choose option 1. If you are going to run ``lldb`` on the host machine and
486+
connect to ``lldb-server`` on the target, choose option 2.
487+
488+
Either way, the most important cmake options when cross-compiling are:
476489

477490
* ``CMAKE_SYSTEM_NAME`` and ``CMAKE_SYSTEM_PROCESSOR``: This tells CMake what
478491
the build target is and from this it will infer that you are cross compiling.
@@ -482,17 +495,18 @@ The most important cmake options here are:
482495
compilers. You may need to specify the exact target cpu and ABI besides the
483496
include paths for the target headers.
484497
* ``CMAKE_EXE_LINKER_FLAGS`` : The flags to be passed to the linker. Usually
485-
just a list of library search paths referencing the target libraries.
498+
this is a list of library search paths referencing the target libraries.
486499
* ``LLVM_HOST_TRIPLE`` : The triple of the system that lldb (or lldb-server)
487500
will run on. Not setting this (or setting it incorrectly) can cause a lot of
488501
issues with remote debugging as a lot of the choices lldb makes depend on the
489502
triple reported by the remote platform.
490-
* ``LLVM_NATIVE_TOOL_DIR`` : Is a path to the llvm tools compiled for the host.
491-
Any tool that must be run on the host during a cross build will be configured
492-
from this path, so you do not need to set them all individually. If you are
493-
doing a host build just for the purpose of a cross build, you will need it
494-
to include at least ``llvm-tblgen``, ``clang-tblgen`` and ``lldb-tblgen``.
495-
Please be aware that that list may grow over time.
503+
* ``LLVM_NATIVE_TOOL_DIR`` (only when using an existing host build): Is a
504+
path to the llvm tools compiled for the host. Any tool that must be run on the
505+
host during a cross build will be configured from this path, so you do not
506+
need to set them all individually. If you are doing a host build only for the
507+
purpose of a cross build, you will need it to include at least
508+
``llvm-tblgen``, ``clang-tblgen`` and ``lldb-tblgen``. Be aware that
509+
the list may grow over time.
496510
* ``CMAKE_LIBRARY_ARCHITECTURE`` : Affects the cmake search path when looking
497511
for libraries. You may need to set this to your architecture triple if you do
498512
not specify all your include and library paths explicitly.
@@ -516,8 +530,33 @@ Example 1: Cross-compiling for linux arm64 on Ubuntu host
516530

517531
Ubuntu already provides the packages necessary to cross-compile LLDB for arm64.
518532
It is sufficient to install packages ``gcc-aarch64-linux-gnu``,
519-
``g++-aarch64-linux-gnu``, ``binutils-aarch64-linux-gnu``. Then it is possible
520-
to prepare the cmake build with the following parameters:
533+
``g++-aarch64-linux-gnu``, ``binutils-aarch64-linux-gnu``.
534+
535+
Configure as follows:
536+
537+
::
538+
539+
cmake <path-to-monorepo>/llvm-project/llvm -G Ninja \
540+
-DCMAKE_BUILD_TYPE=Release \
541+
-DLLVM_ENABLE_PROJECTS="clang;lld;lldb" \
542+
-DCMAKE_SYSTEM_NAME=Linux \
543+
-DCMAKE_SYSTEM_PROCESSOR=AArch64 \
544+
-DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc \
545+
-DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++ \
546+
-DLLVM_HOST_TRIPLE=aarch64-unknown-linux-gnu \
547+
-DLLDB_ENABLE_PYTHON=0 \
548+
-DLLDB_ENABLE_LIBEDIT=0 \
549+
-DLLDB_ENABLE_CURSES=0
550+
551+
During this build native tools will be built automatically when they are needed.
552+
The contents of ``<build dir>/bin`` will be target binaries as you'd expect.
553+
AArch64 binaries in this case.
554+
555+
Example 2: Cross-compiling for linux arm64 on Ubuntu host using an existing host build
556+
**************************************************************************************
557+
558+
This build requires an existing host build that includes the required native
559+
tools. Install the compiler as in example 1 then run CMake as follows:
521560

522561
::
523562

@@ -534,6 +573,8 @@ to prepare the cmake build with the following parameters:
534573
-DLLDB_ENABLE_LIBEDIT=0 \
535574
-DLLDB_ENABLE_CURSES=0
536575

576+
The only difference from example 1 is the addition of
577+
``DLLVM_NATIVE_TOOL_DIR`` pointing to your existing host build.
537578

538579
An alternative (and recommended) way to compile LLDB is with clang.
539580
Unfortunately, clang is not able to find all the include paths necessary for a
@@ -556,7 +597,7 @@ qemu and chroot to simulate the target environment. Then you can install the
556597
necessary packages in this environment (python-dev, libedit-dev, etc.) and
557598
point your compiler to use them using the correct -I and -L arguments.
558599

559-
Example 2: Cross-compiling for Android on Linux
600+
Example 3: Cross-compiling for Android on Linux
560601
***********************************************
561602

562603
In the case of Android, the toolchain and all required headers and libraries

0 commit comments

Comments
 (0)