blob: 50305cec4a83a558525ff6916270fb6f91ecedc4 [file] [log] [blame]
Joao Marcos Costa3a839602023-10-01 12:00:34 +02001.. SPDX-License-Identifier: GPL-2.0-only
2
3Create build database for IDEs
4==============================
5
6gen_compile_commands (scripts/gen_compile_commands.py) is a script used to
7generate a compilation database (compile_commands.json). This database consists
8of an array of "command objects" describing how each translation unit was
9compiled.
10
11Example::
12
13 {
14 "command": "gcc -Wp,-MD,arch/x86/cpu/.lapic.o.d -nostdinc -isystem (...)"
15 "directory": "/home/jmcosta/u-boot",
16 "file": "/home/jmcosta/u-boot/arch/x86/cpu/lapic.c"
17 }
18
19Such information comes from parsing the respective .cmd file of each translation
20unit. In the previous example, that would be `arch/x86/cpu/.lapic.o.cmd`.
21
22For more details on the database format, please refer to the official
23documentation at https://clang.llvm.org/docs/JSONCompilationDatabase.html.
24
25The compilation database is quite useful for text editors (and IDEs) that use
26Clangd LSP. It allows jumping to definitions and declarations. Since it relies
27on parsing .cmd files, one needs to have a target (e.g. configs/xxx_defconfig)
28built before running the script.
29
30Example::
31
32 make sandbox_defconfig
33 make
34 ./scripts/gen_compile_commands.py
35
36Beware that depending on the changes you made to the project's source code, you
37may need to run the script again (presuming you recompiled your target, of
38course) to have an up-to-date database.
39
40The database will be in the root of the repository. No further modifications are
41needed for it to be usable by the LSP, unless you set a name for the database
42other than it's default one (compile_commands.json).
43
44Compatible IDEs
45===============
46
47Several popular integrated development environments (IDEs) support the use
48of JSON compilation databases for C/C++ development, making it easier to
49manage build configurations and code analysis. Some of these IDEs include:
50
511. **Visual Studio Code (VS Code)**: IntelliSense in VS Code can be set up to
52 use compile_commands.json by following the instructions in
53 https://code.visualstudio.com/docs/cpp/faq-cpp#_how-do-i-get-intellisense-to-work-correctly.
54
552. **CLion**: JetBrains' CLion IDE supports JSON compilation databases out
56 of the box. You can configure your project to use a compile_commands.json
57 file via the project settings. Details on setting up CLion with JSON
58 compilation databases can be found at
59 https://www.jetbrains.com/help/clion/compilation-database.html.
60
613. **Qt Creator**: Qt Creator, a popular IDE for Qt development, also
62 supports compile_commands.json for C/C++ projects. Instructions on how to
63 use this feature can be found at
64 https://doc.qt.io/qtcreator/creator-clang-codemodel.html#using-compilation-databases.
65
664. **Eclipse CDT**: Eclipse's C/C++ Development Tools (CDT) can be
67 configured to use JSON compilation databases for better project management.
68 You can find guidance on setting up JSON compilation database support at the
69 wiki: https://wiki.eclipse.org/CDT/User/NewIn910#Build.
70
71For Vim, Neovim, and Emacs, if you are using Clangd as your LSP, placing the
72compile_commands.json in the root of the repository should suffice to enable
73code navigation.
74
75Usage
76=====
77
78For further details on the script's options, please refer to its help message,
79as in the example below.
80
81Help::
82
83 ./scripts/gen_compile_commands.py --help