Joao Marcos Costa | 3cc4234 | 2023-10-01 12:00:34 +0200 | [diff] [blame] | 1 | .. SPDX-License-Identifier: GPL-2.0-only |
| 2 | |
| 3 | Create build database for IDEs |
| 4 | ============================== |
| 5 | |
| 6 | gen_compile_commands (scripts/gen_compile_commands.py) is a script used to |
| 7 | generate a compilation database (compile_commands.json). This database consists |
| 8 | of an array of "command objects" describing how each translation unit was |
| 9 | compiled. |
| 10 | |
| 11 | Example:: |
| 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 | |
| 19 | Such information comes from parsing the respective .cmd file of each translation |
| 20 | unit. In the previous example, that would be `arch/x86/cpu/.lapic.o.cmd`. |
| 21 | |
| 22 | For more details on the database format, please refer to the official |
| 23 | documentation at https://clang.llvm.org/docs/JSONCompilationDatabase.html. |
| 24 | |
| 25 | The compilation database is quite useful for text editors (and IDEs) that use |
| 26 | Clangd LSP. It allows jumping to definitions and declarations. Since it relies |
| 27 | on parsing .cmd files, one needs to have a target (e.g. configs/xxx_defconfig) |
| 28 | built before running the script. |
| 29 | |
| 30 | Example:: |
| 31 | |
| 32 | make sandbox_defconfig |
| 33 | make |
| 34 | ./scripts/gen_compile_commands.py |
| 35 | |
| 36 | Beware that depending on the changes you made to the project's source code, you |
| 37 | may need to run the script again (presuming you recompiled your target, of |
| 38 | course) to have an up-to-date database. |
| 39 | |
| 40 | The database will be in the root of the repository. No further modifications are |
| 41 | needed for it to be usable by the LSP, unless you set a name for the database |
| 42 | other than it's default one (compile_commands.json). |
| 43 | |
| 44 | Compatible IDEs |
| 45 | =============== |
| 46 | |
| 47 | Several popular integrated development environments (IDEs) support the use |
| 48 | of JSON compilation databases for C/C++ development, making it easier to |
| 49 | manage build configurations and code analysis. Some of these IDEs include: |
| 50 | |
| 51 | 1. **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 | |
| 55 | 2. **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 | |
| 61 | 3. **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 | |
| 66 | 4. **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 | |
| 71 | For Vim, Neovim, and Emacs, if you are using Clangd as your LSP, placing the |
| 72 | compile_commands.json in the root of the repository should suffice to enable |
| 73 | code navigation. |
| 74 | |
| 75 | Usage |
| 76 | ===== |
| 77 | |
| 78 | For further details on the script's options, please refer to its help message, |
| 79 | as in the example below. |
| 80 | |
| 81 | Help:: |
| 82 | |
| 83 | ./scripts/gen_compile_commands.py --help |