xref: /llvm-project/lldb/docs/resources/build.rst (revision 9ea64dd8781328d831d7c69a586f0c84dece1c11)
1Building
2========
3
4Getting the Sources
5-------------------
6
7Please refer to the `LLVM Getting Started Guide
8<https://llvm.org/docs/GettingStarted.html#getting-started-with-llvm>`_ for
9general instructions on how to check out the LLVM monorepo, which contains the
10LLDB sources.
11
12Git browser: https://github.com/llvm/llvm-project/tree/main/lldb
13
14Preliminaries
15-------------
16
17LLDB relies on many of the technologies developed by the larger LLVM project.
18In particular, it requires both Clang and LLVM itself in order to build. Due to
19this tight integration the Getting Started guides for both of these projects
20come as prerequisite reading:
21
22* `LLVM <https://llvm.org/docs/GettingStarted.html>`_
23* `Clang <http://clang.llvm.org/get_started.html>`_
24
25The following requirements are shared on all platforms.
26
27* `CMake <https://cmake.org>`_
28* `Ninja <https://ninja-build.org>`_ (strongly recommended)
29
30If you want to run the test suite, you'll need to build LLDB with Python
31scripting support.
32
33* `Python <http://www.python.org/>`_
34* `SWIG <http://swig.org/>`_ 4 or later.
35
36If you are on FreeBSD or NetBSD, you will need to install ``gmake`` for building
37the test programs. On other platforms ``make`` is used.
38
39.. _Optional Dependencies:
40
41Optional Dependencies
42*********************
43
44Although the following dependencies are optional, they have a big impact on
45LLDB's functionality. It is strongly encouraged to build LLDB with these
46dependencies enabled.
47
48By default they are auto-detected: if CMake can find the dependency it will be
49used. It is possible to override this behavior by setting the corresponding
50CMake flag to ``On`` or ``Off`` to force the dependency to be enabled or
51disabled. When a dependency is set to ``On`` and can't be found it will cause a
52CMake configuration error.
53
54+-------------------+--------------------------------------------------------------+--------------------------+
55| Feature           | Description                                                  | CMake Flag               |
56+===================+==============================================================+==========================+
57| Editline          | Generic line editing, history, Emacs and Vi bindings         | ``LLDB_ENABLE_LIBEDIT``  |
58+-------------------+--------------------------------------------------------------+--------------------------+
59| Curses            | Text user interface                                          | ``LLDB_ENABLE_CURSES``   |
60+-------------------+--------------------------------------------------------------+--------------------------+
61| LZMA              | Lossless data compression                                    | ``LLDB_ENABLE_LZMA``     |
62+-------------------+--------------------------------------------------------------+--------------------------+
63| Libxml2           | XML                                                          | ``LLDB_ENABLE_LIBXML2``  |
64+-------------------+--------------------------------------------------------------+--------------------------+
65| Python            | Python scripting. >= 3.8 is required.                        | ``LLDB_ENABLE_PYTHON``   |
66+-------------------+--------------------------------------------------------------+--------------------------+
67| Lua               | Lua scripting. Lua 5.3 and 5.4 are supported.                | ``LLDB_ENABLE_LUA``      |
68+-------------------+--------------------------------------------------------------+--------------------------+
69
70Depending on your platform and package manager, one might run any of the
71commands below.
72
73::
74
75  $ yum install libedit-devel libxml2-devel ncurses-devel python-devel swig
76  $ sudo apt-get install build-essential swig python3-dev libedit-dev libncurses5-dev libxml2-dev
77  $ pkg install swig python libxml2
78  $ pkgin install swig python38 cmake ninja-build
79  $ brew install swig cmake ninja
80
81.. note::
82   There is an `incompatibility
83   <https://github.com/swig/swig/issues/1321>`_ between Python version 3.7 and later
84   and swig versions older than 4.0.0 which makes builds of LLDB using debug
85   versions of python unusable. This primarily affects Windows, as debug builds of
86   LLDB must use debug python as well.
87
88.. note::
89  Installing multiple versions of Curses, particularly when only one is built with
90  wide character support, can cause lldb to be linked with an incorrect set of
91  libraries. If your system already has Curses, we recommend you use that version.
92  If you do install another one, use a tool like ``ldd`` to ensure only one version
93  of Curses is being used in the final ``lldb`` executable.
94
95Windows
96*******
97
98* Visual Studio 2019.
99* The latest Windows SDK.
100* The Active Template Library (ATL).
101* `GnuWin32 <http://gnuwin32.sourceforge.net/>`_ for CoreUtils and Make.
102* `Python 3 <https://www.python.org/downloads/windows/>`_.  Make sure to (1) get
103  the x64 variant if that's what you're targetting and (2) install the debug
104  library if you want to build a debug lldb. The standalone installer is the
105  easiest way to get the debug library.
106* `Python Tools for Visual Studio
107  <https://github.com/Microsoft/PTVS/>`_. If you plan to debug test failures
108  or even write new tests at all, PTVS is an indispensable debugging
109  extension to VS that enables full editing and debugging support for Python
110  (including mixed native/managed debugging).
111* `SWIG for Windows <http://www.swig.org/download.html>`_
112
113The steps outlined here describes how to set up your system and install the
114required dependencies such that they can be found when needed during the build
115process. They only need to be performed once.
116
117#. Install Visual Studio with the "Desktop Development with C++" workload and
118   the "Python Development" workload.
119#. Install GnuWin32, making sure ``<GnuWin32 install dir>\bin`` is added to
120   your PATH environment variable. Verify that utilities like ``dirname`` and
121   ``make`` are available from your terminal.
122#. Install SWIG for Windows, making sure ``<SWIG install dir>`` is added to
123   your PATH environment variable. Verify that ``swig`` is available from your
124   terminal.
125#. Install Python 3 from the standalone installer and include the debug libraries
126   in the install, making sure the Python install path is added to your PATH
127   environment variable.
128#. Register the Debug Interface Access DLLs with the Registry from a privileged
129   terminal.
130
131::
132
133> regsvr32 "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\DIA SDK\bin\msdia140.dll"
134> regsvr32 "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\DIA SDK\bin\amd64\msdia140.dll"
135
136Any command prompt from which you build LLDB should have a valid Visual Studio
137environment setup. This means you should open an appropriate `Developer Command
138Prompt for VS <https://docs.microsoft.com/en-us/visualstudio/ide/reference/command-prompt-powershell?view=vs-2019>`_
139corresponding to the version you wish to use or run ``vcvarsall.bat`` or
140``VsDevCmd.bat``.
141
142macOS
143*****
144
145* To use the in-tree debug server on macOS, lldb needs to be code signed. For
146  more information see :ref:`CodeSigning` below.
147* If you are building both Clang and LLDB together, be sure to also check out
148  libc++, which is a required for testing on macOS.
149
150Building LLDB with CMake
151------------------------
152
153The LLVM project is migrating to a single monolithic repository for LLVM and
154its subprojects. This is the recommended way to build LLDB. Check out the
155source-tree with git:
156
157::
158
159  $ git clone https://github.com/llvm/llvm-project.git
160
161CMake is a cross-platform build-generator tool. CMake does not build the
162project, it generates the files needed by your build tool. The recommended
163build tool for LLVM is Ninja, but other generators like Xcode or Visual Studio
164may be used as well. Please also read `Building LLVM with CMake
165<https://llvm.org/docs/CMake.html>`_.
166
167Regular in-tree builds
168**********************
169
170Create a new directory for your build-tree. From there run CMake and point it
171to the ``llvm`` directory in the source-tree:
172
173::
174
175  $ cmake -G Ninja -DLLVM_ENABLE_PROJECTS="clang;lldb" [<cmake options>] path/to/llvm-project/llvm
176
177We used the ``LLVM_ENABLE_PROJECTS`` option here to tell the build-system which
178subprojects to build in addition to LLVM (for more options see
179:ref:`CommonCMakeOptions` and :ref:`CMakeCaches`). Parts of the LLDB test suite
180require ``lld``. Add it to the list in order to run all tests. Once CMake is done,
181run ninja to perform the actual build.
182
183::
184
185  $ ninja lldb lldb-server
186
187If you only want lldb, or are on a platform where lldb-server is not supported,
188you can pass just ``lldb``. Ninja will only build what is necessary to run the
189lldb driver:
190
191::
192
193  $ ninja lldb
194
195Standalone builds
196*****************
197
198This is another way to build LLDB. We can use the same source-tree as we
199checked out above, but now we will have multiple build-trees:
200
201* the main build-tree for LLDB in ``/path/to/lldb-build``
202* one or more provided build-trees for LLVM and Clang; for simplicity we use a
203  single one in ``/path/to/llvm-build``
204
205Run CMake with ``-B`` pointing to a new directory for the provided
206build-tree\ :sup:`1` and the positional argument pointing to the ``llvm``
207directory in the source-tree. Note that we leave out LLDB here and only include
208Clang. Then we build the ``ALL`` target with ninja:
209
210::
211
212  $ cmake -B /path/to/llvm-build -G Ninja \
213          -DLLVM_ENABLE_PROJECTS=clang \
214          [<more cmake options>] /path/to/llvm-project/llvm
215  $ ninja
216
217Now run CMake a second time with ``-B`` pointing to a new directory for the
218main build-tree and the positional argument pointing to the ``lldb`` directory
219in the source-tree. In order to find the provided build-tree, the build system
220looks for the path to its CMake modules in ``LLVM_DIR``. If you use a separate
221build directory for Clang, remember to pass its module path via ``Clang_DIR``
222(CMake variables are case-sensitive!):
223
224::
225
226  $ cmake -B /path/to/lldb-build -G Ninja \
227          -DLLVM_DIR=/path/to/llvm-build/lib/cmake/llvm \
228          [<more cmake options>] /path/to/llvm-project/lldb
229  $ ninja lldb lldb-server
230
231If you do not require or cannot build ``lldb-server`` on your platform, simply
232remove it from the Ninja command.
233
234.. note::
235
236   #. The ``-B`` argument was undocumented for a while and is only officially
237      supported since `CMake version 3.14
238      <https://cmake.org/cmake/help/v3.14/release/3.14.html#command-line>`_
239
240.. _CommonCMakeOptions:
241
242Common CMake options
243********************
244
245Following is a description of some of the most important CMake variables which
246you are likely to encounter. A variable FOO is set by adding ``-DFOO=value`` to
247the CMake command line.
248
249If you want to debug the lldb that you're building -- that is, build it with
250debug info enabled -- pass two additional arguments to cmake before running
251ninja:
252
253::
254
255  $ cmake -G Ninja \
256      -DLLDB_EXPORT_ALL_SYMBOLS=1 \
257      -DCMAKE_BUILD_TYPE=Debug
258      <path to root of llvm source tree>
259
260If you want to run the test suite, you will need a compiler to build the test
261programs. If you have Clang checked out, that will be used by default.
262Alternatively, you can specify a C and C++ compiler to be used by the test
263suite.
264
265::
266
267  $ cmake -G Ninja \
268      -DLLDB_TEST_COMPILER=<path to C compiler> \
269      <path to root of llvm source tree>
270
271It is strongly recommend to use a release build for the compiler to speed up
272test execution.
273
274Windows
275^^^^^^^
276
277On Windows the LLDB test suite requires lld. Either add ``lld`` to
278``LLVM_ENABLE_PROJECTS`` or disable the test suite with
279``LLDB_INCLUDE_TESTS=OFF``.
280
281Although the following CMake variables are by no means Windows specific, they
282are commonly used on Windows.
283
284* ``LLDB_TEST_DEBUG_TEST_CRASHES`` (Default=0): If set to 1, will cause Windows
285  to generate a crash dialog whenever lldb.exe or the python extension module
286  crashes while running the test suite. If set to 0, LLDB will silently crash.
287  Setting to 1 allows a developer to attach a JIT debugger at the time of a
288  crash, rather than having to reproduce a failure or use a crash dump.
289* ``PYTHON_HOME`` (Required): Path to the folder where the Python distribution
290  is installed. For example, ``C:\Python35``.
291* ``LLDB_EMBED_PYTHON_HOME`` (Default=1 on Windows): When this is 1, LLDB will bind
292  statically to the location specified in the ``PYTHON_HOME`` CMake variable,
293  ignoring any value of ``PYTHONHOME`` set in the environment. This is most
294  useful for developers who simply want to run LLDB after they build it. If you
295  wish to move a build of LLDB to a different machine where Python will be in a
296  different location, setting ``LLDB_EMBED_PYTHON_HOME`` to 0 will cause
297  Python to use its default mechanism for finding the python installation at
298  runtime (looking for installed Pythons, or using the ``PYTHONHOME``
299  environment variable if it is specified).
300
301Sample command line:
302
303::
304
305  $ cmake -G Ninja^
306      -DLLDB_TEST_DEBUG_TEST_CRASHES=1^
307      -DPYTHON_HOME=C:\Python35^
308      -DLLDB_TEST_COMPILER=d:\src\llvmbuild\ninja_release\bin\clang.exe^
309      <path to root of llvm source tree>
310
311
312Building with ninja is both faster and simpler than building with Visual Studio,
313but chances are you still want to debug LLDB with an IDE. One solution is to run
314cmake twice and generate the output into two different folders. One for
315compiling (the ninja folder), and one for editing, browsing and debugging.
316
317Follow the previous instructions in one directory, and generate a Visual Studio
318project in another directory.
319
320::
321
322  $ cmake -G "Visual Studio 16 2019" -A x64 -T host=x64 <cmake variables> <path to root of llvm source tree>
323
324Then you can open the .sln file in Visual Studio, set lldb as the startup
325project, and use F5 to run it. You need only edit the project settings to set
326the executable and the working directory to point to binaries inside of the
327ninja tree.
328
329
330macOS
331^^^^^
332
333On macOS the LLDB test suite requires libc++. Either add
334``LLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind"`` or disable the test suite with
335``LLDB_INCLUDE_TESTS=OFF``. Further useful options:
336
337* ``LLDB_BUILD_FRAMEWORK:BOOL``: Builds the LLDB.framework.
338* ``LLDB_CODESIGN_IDENTITY:STRING``: Set the identity to use for code-signing
339  all executables. If not explicitly specified, only ``debugserver`` will be
340  code-signed with identity ``lldb_codesign`` (see :ref:`CodeSigning`).
341* ``LLDB_USE_SYSTEM_DEBUGSERVER:BOOL``: Use the system's debugserver, so lldb is
342  functional without setting up code-signing.
343
344
345.. _CMakeCaches:
346
347CMake caches
348************
349
350CMake caches allow to store common sets of configuration options in the form of
351CMake scripts and can be useful to reproduce builds for particular use-cases
352(see by analogy `usage in LLVM and Clang <https://llvm.org/docs/AdvancedBuilds.html>`_).
353A cache is passed to CMake with the ``-C`` flag, following the absolute path to
354the file on disk. Subsequent ``-D`` options are still allowed. Please find the
355currently available caches in the `lldb/cmake/caches/
356<https://github.com/llvm/llvm-project/tree/main/lldb/cmake/caches>`_
357directory.
358
359Common configurations on macOS
360^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
361
362Build, test and install a distribution of LLDB from the `monorepo
363<https://github.com/llvm/llvm-project>`_ (see also `Building a Distribution of
364LLVM <https://llvm.org/docs/BuildingADistribution.html>`_):
365
366::
367
368  $ git clone https://github.com/llvm/llvm-project
369
370  $ cmake -B /path/to/lldb-build -G Ninja \
371          -C /path/to/llvm-project/lldb/cmake/caches/Apple-lldb-macOS.cmake \
372          -DLLVM_ENABLE_PROJECTS="clang;lldb" \
373          -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" \
374          llvm-project/llvm
375
376  $ DESTDIR=/path/to/lldb-install ninja -C /path/to/lldb-build check-lldb install-distribution
377
378.. _CMakeGeneratedXcodeProject:
379
380Build LLDB standalone for development with Xcode:
381
382::
383
384  $ git clone https://github.com/llvm/llvm-project
385
386  $ cmake -B /path/to/llvm-build -G Ninja \
387          -C /path/to/llvm-project/lldb/cmake/caches/Apple-lldb-base.cmake \
388          -DLLVM_ENABLE_PROJECTS="clang" \
389          -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" \
390          llvm-project/llvm
391  $ ninja -C /path/to/llvm-build
392
393  $ cmake -B /path/to/lldb-build \
394          -C /path/to/llvm-project/lldb/cmake/caches/Apple-lldb-Xcode.cmake \
395          -DLLVM_DIR=/path/to/llvm-build/lib/cmake/llvm \
396          llvm-project/lldb
397  $ open lldb.xcodeproj
398  $ cmake --build /path/to/lldb-build --target check-lldb
399
400.. note::
401
402   The ``-B`` argument was undocumented for a while and is only officially
403   supported since `CMake version 3.14
404   <https://cmake.org/cmake/help/v3.14/release/3.14.html#command-line>`_
405
406
407Building the Documentation
408--------------------------
409
410If you wish to build the optional (reference) documentation, additional
411dependencies are required:
412
413* Sphinx (for the website and the Python API reference)
414* Graphviz (for the 'dot' tool)
415* doxygen (if you wish to build the C++ API reference)
416* SWIG (for generating Python bindings)
417
418To install the system prerequisites for building the documentation (on Debian/Ubuntu)
419do:
420
421::
422
423  $ sudo apt-get install doxygen graphviz swig
424
425To install Sphinx and its dependencies, use the ``requirements.txt`` available within LLVM
426to ensure you get a working configuration:
427
428::
429
430  $ pip3 install -r /path/to/llvm-project/llvm/docs/requirements.txt
431
432To build the documentation, configure with ``LLVM_ENABLE_SPHINX=ON`` and build the desired target(s).
433
434::
435
436  $ ninja docs-lldb-html
437  $ ninja docs-lldb-man
438  $ ninja lldb-cpp-doc
439
440Cross-compiling LLDB
441--------------------
442
443The advice presented here may not be complete or represent the best practices
444of CMake at this time. Please refer to `CMake's documentation <https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html>`_
445if you have any doubts or want more in depth information.
446
447In order to debug remote targets running different architectures than your
448host, you will need to compile LLDB (or at least the server component
449``lldb-server``) for the target. While the easiest solution is to compile it
450locally on the target, this is often not feasible, and in these cases you will
451need to cross-compile LLDB on your host.
452
453Cross-compilation is often a daunting task and has a lot of quirks which depend
454on the exact host and target architectures, so it is not possible to give a
455universal guide which will work on all platforms. However, here we try to
456provide an overview of the cross-compilation process along with the main things
457you should look out for.
458
459First, you will need a working toolchain which is capable of producing binaries
460for the target architecture. Since you already have a checkout of clang and
461lldb, you can compile a host version of clang in a separate folder and use
462that. Alternatively you can use system clang or even cross-gcc if your
463distribution provides such packages (e.g., ``g++-aarch64-linux-gnu`` on
464Ubuntu).
465
466Next, you will need a copy of the required target headers and libraries on your
467host. The libraries can be usually obtained by copying from the target machine,
468however the headers are often not found there, especially in case of embedded
469platforms. In this case, you will need to obtain them from another source,
470either a cross-package if one is available, or cross-compiling the respective
471library from source. Fortunately the list of LLDB dependencies is not big and
472if you are only interested in the server component, you can reduce this even
473further by passing the appropriate cmake options, such as:
474
475::
476
477  -DLLDB_ENABLE_PYTHON=0
478  -DLLDB_ENABLE_LIBEDIT=0
479  -DLLDB_ENABLE_CURSES=0
480
481(see :ref:`Optional Dependencies` for more)
482
483In this case you, will often not need anything other than the standard C and
484C++ libraries.
485
486If you find that CMake is finding a version of an optional dependency that
487for whatever reason doesn't work, consider simply disabling it if you don't
488know that you need it.
489
490Once all of the dependencies are in place, you need to configure the build
491system with the locations and arguments of all the necessary tools.
492
493There are 2 ways to do this depending on your starting point and requirements.
494
4951. If you are starting from scratch and only need the resulting cross compiled
496binaries, you can have LLVM build the native tools for you.
497
4982. If you need a host build too, or already have one, you can tell CMake where
499that is and it will use those native tools instead.
500
501If you are going to run ``lldb`` and ``lldb-server`` only on the target machine,
502choose option 1. If you are going to run ``lldb`` on the host machine and
503connect to ``lldb-server`` on the target, choose option 2.
504
505Either way, the most important cmake options when cross-compiling are:
506
507* ``CMAKE_SYSTEM_NAME`` and ``CMAKE_SYSTEM_PROCESSOR``: This tells CMake what
508  the build target is and from this it will infer that you are cross compiling.
509* ``CMAKE_C_COMPILER``, ``CMAKE_CXX_COMPILER`` : C and C++ compilers for the
510  target architecture.
511* ``CMAKE_C_FLAGS``, ``CMAKE_CXX_FLAGS`` : The flags for the C and C++ target
512  compilers. You may need to specify the exact target cpu and ABI besides the
513  include paths for the target headers.
514* ``CMAKE_EXE_LINKER_FLAGS`` : The flags to be passed to the linker. Usually
515  this is a list of library search paths referencing the target libraries.
516* ``LLVM_HOST_TRIPLE`` : The triple of the system that lldb (or lldb-server)
517  will run on. Not setting this (or setting it incorrectly) can cause a lot of
518  issues with remote debugging as a lot of the choices lldb makes depend on the
519  triple reported by the remote platform.
520* ``LLVM_NATIVE_TOOL_DIR`` (only when using an existing host build): Is a
521  path to the llvm tools compiled for the host. Any tool that must be run on the
522  host during a cross build will be configured from this path, so you do not
523  need to set them all individually. If you are doing a host build only for the
524  purpose of a cross build, you will need it to include at least
525  ``llvm-tblgen``, ``clang-tblgen`` and ``lldb-tblgen``. Be aware that
526  the list may grow over time.
527* ``CMAKE_LIBRARY_ARCHITECTURE`` : Affects the cmake search path when looking
528  for libraries. You may need to set this to your architecture triple if you do
529  not specify all your include and library paths explicitly.
530
531To find the possible values of the ``CMAKE_*`` options, please refer to the
532CMake documentation.
533
534You can of course also specify the usual cmake options like
535``CMAKE_BUILD_TYPE``, etc.
536
537For testing, you may want to set one of:
538
539* ``LLDB_TEST_COMPILER`` : The compiler used to build programs used
540  in the test suite. If you are also building clang, this will be used
541  but if you want to test remotely from the host, you should choose the
542  cross compiler you are using for the cross build.
543* ``LLDB_INCLUDE_TESTS=0`` : To disable the tests completely.
544
545Example 1: Cross-compiling for linux arm64 on Ubuntu host
546*********************************************************
547
548Ubuntu already provides the packages necessary to cross-compile LLDB for arm64.
549It is sufficient to install packages ``gcc-aarch64-linux-gnu``,
550``g++-aarch64-linux-gnu``, ``binutils-aarch64-linux-gnu``.
551
552Configure as follows:
553
554::
555
556  cmake <path-to-monorepo>/llvm-project/llvm -G Ninja \
557    -DCMAKE_BUILD_TYPE=Release \
558    -DLLVM_ENABLE_PROJECTS="clang;lld;lldb" \
559    -DCMAKE_SYSTEM_NAME=Linux \
560    -DCMAKE_SYSTEM_PROCESSOR=AArch64 \
561    -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc \
562    -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++ \
563    -DLLVM_HOST_TRIPLE=aarch64-unknown-linux-gnu \
564    -DLLDB_ENABLE_PYTHON=0 \
565    -DLLDB_ENABLE_LIBEDIT=0 \
566    -DLLDB_ENABLE_CURSES=0
567
568During this build native tools will be built automatically when they are needed.
569The contents of ``<build dir>/bin`` will be target binaries as you'd expect.
570AArch64 binaries in this case.
571
572Example 2: Cross-compiling for linux arm64 on Ubuntu host using an existing host build
573**************************************************************************************
574
575This build requires an existing host build that includes the required native
576tools. Install the compiler as in example 1 then run CMake as follows:
577
578::
579
580  cmake <path-to-monorepo>/llvm-project/llvm -G Ninja \
581    -DCMAKE_BUILD_TYPE=Release \
582    -DLLVM_ENABLE_PROJECTS="clang;lld;lldb" \
583    -DCMAKE_SYSTEM_NAME=Linux \
584    -DCMAKE_SYSTEM_PROCESSOR=AArch64 \
585    -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc \
586    -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++ \
587    -DLLVM_HOST_TRIPLE=aarch64-unknown-linux-gnu \
588    -DLLVM_NATIVE_TOOL_DIR=<path-to-host>/bin/ \
589    -DLLDB_ENABLE_PYTHON=0 \
590    -DLLDB_ENABLE_LIBEDIT=0 \
591    -DLLDB_ENABLE_CURSES=0
592
593The only difference from example 1 is the addition of
594``DLLVM_NATIVE_TOOL_DIR`` pointing to your existing host build.
595
596An alternative (and recommended) way to compile LLDB is with clang.
597Unfortunately, clang is not able to find all the include paths necessary for a
598successful cross-compile, so we need to help it with a couple of CFLAGS
599options. In my case it was sufficient to add the following arguments to
600``CMAKE_C_FLAGS`` and ``CMAKE_CXX_FLAGS`` (in addition to changing
601``CMAKE_C(XX)_COMPILER`` to point to clang compilers):
602
603::
604
605  -target aarch64-linux-gnu \
606  -I /usr/aarch64-linux-gnu/include/c++/4.8.2/aarch64-linux-gnu \
607  -I /usr/aarch64-linux-gnu/include
608
609If you wanted to build a full version of LLDB and avoid passing
610``-DLLDB_ENABLE_PYTHON=0`` and other options, you would need to obtain the
611target versions of the respective libraries. The easiest way to achieve this is
612to use the qemu-debootstrap utility, which can prepare a system image using
613qemu and chroot to simulate the target environment. Then you can install the
614necessary packages in this environment (python-dev, libedit-dev, etc.) and
615point your compiler to use them using the correct -I and -L arguments.
616
617Example 3: Cross-compiling for Android on Linux
618***********************************************
619
620In the case of Android, the toolchain and all required headers and libraries
621are available in the Android NDK.
622
623The NDK also contains a cmake toolchain file, which makes configuring the build
624much simpler. The compiler, include and library paths will be configured by the
625toolchain file and all you need to do is to select the architecture
626(ANDROID_ABI) and platform level (``ANDROID_PLATFORM``, should be at least 21).
627You will also need to set ``ANDROID_ALLOW_UNDEFINED_SYMBOLS=On``, as the
628toolchain file defaults to "no undefined symbols in shared libraries", which is
629not compatible with some llvm libraries. The first version of NDK which
630supports this approach is r14.
631
632For example, the following arguments are sufficient to configure an android
633arm64 build:
634
635::
636
637  -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake \
638  -DANDROID_ABI=arm64-v8a \
639  -DANDROID_PLATFORM=android-21 \
640  -DANDROID_ALLOW_UNDEFINED_SYMBOLS=On \
641  -DLLVM_HOST_TRIPLE=aarch64-unknown-linux-android \
642  -DCROSS_TOOLCHAIN_FLAGS_NATIVE='-DCMAKE_C_COMPILER=cc;-DCMAKE_CXX_COMPILER=c++'
643
644Note that currently only lldb-server is functional on android. The lldb client
645is not supported and unlikely to work.
646
647Verifying Python Support
648------------------------
649
650LLDB has a Python scripting capability and supplies its own Python module named
651lldb. If a script is run inside the command line lldb application, the Python
652module is made available automatically. However, if a script is to be run by a
653Python interpreter outside the command line application, the ``PYTHONPATH``
654environment variable can be used to let the Python interpreter find the lldb
655module.
656
657The correct path can be obtained by invoking the command line lldb tool with
658the -P flag:
659
660::
661
662  $ export PYTHONPATH=`$llvm/build/Debug+Asserts/bin/lldb -P`
663
664If you used a different build directory or made a release build, you may need
665to adjust the above to suit your needs. To test that the lldb Python module is
666built correctly and is available to the default Python interpreter, run:
667
668::
669
670  $ python -c 'import lldb'
671
672
673Make sure you're using the Python interpreter that matches the Python library
674you linked against. For more details please refer to the :ref:`caveats
675<python_caveat>`.
676
677.. _CodeSigning:
678
679Code Signing on macOS
680---------------------
681
682To use the in-tree debug server on macOS, lldb needs to be code signed. The
683Debug, DebugClang and Release builds are set to code sign using a code signing
684certificate named ``lldb_codesign``.
685
686Automatic setup, run:
687
688* ``scripts/macos-setup-codesign.sh``
689
690Note that it's possible to build and use lldb on macOS without setting up code
691signing by using the system's debug server. To configure lldb in this way with
692cmake, specify ``-DLLDB_USE_SYSTEM_DEBUGSERVER=ON``.
693
694If you have re-installed a new OS, please delete all old ``lldb_codesign`` items
695from your keychain. There will be a code signing certification and a public
696and private key. Reboot after deleting them. You will also need to delete and
697build folders that contained old signed items. The darwin kernel will cache
698code signing using the executable's file system node, so you will need to
699delete the file so the kernel clears its cache.
700
701When you build your LLDB for the first time, the Xcode GUI will prompt you for
702permission to use the ``lldb_codesign`` keychain. Be sure to click "Always
703Allow" on your first build. From here on out, the ``lldb_codesign`` will be
704trusted and you can build from the command line without having to authorize.
705Also the first time you debug using a LLDB that was built with this code
706signing certificate, you will need to authenticate once.
707