Create a CMakeLists.txt to generate compile_commands.json (#80)

* Add CMakeList to create compilation database with minimal instructions

* Tidy up CMakeLists and add documentation

* Rename project in CMakeLists to generalize

* Add details to the LSP doc

* Add editor config files to `.gitignore`
This commit is contained in:
Mityno
2026-06-16 15:02:05 +00:00
committed by GitHub
parent 5e934a8c9b
commit 01a4804485
3 changed files with 63 additions and 0 deletions
+3
View File
@@ -1,5 +1,8 @@
build/
.vscode/
.clangd
cmake/
compile_commands.json
ph_*/
*.nds
*bios.bin
+46
View File
@@ -0,0 +1,46 @@
# See CONTRIBUTING.md for explanations
# Adapted from https://gist.github.com/Strus/042a92a00070a943053006bf46912ae9
# Build the compilation database with `cmake -S . -G "Unix Makefiles" -B cmake`
# The database is exported in the cmake/ directory
cmake_minimum_required(VERSION 3.8)
project(st_decomp)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Change path from /src if needed, or add more directories
file(GLOB_RECURSE sources
"${CMAKE_SOURCE_DIR}/src/*.c"
"${CMAKE_SOURCE_DIR}/src/*.cpp"
)
# Add precompiler definitions like that:
# add_definitions(-DSOME_DEFINITION)
add_executable(st_decomp ${sources})
# Add include directories
target_include_directories(st_decomp PUBLIC "${CMAKE_SOURCE_DIR}/include")
# Add lib directories
file(GLOB lib_paths
"${CMAKE_SOURCE_DIR}/libs/*"
)
foreach(path IN LISTS lib_paths)
# Retrieve the name of the lib
string(REGEX MATCH "([a-z]+)$" lib_name ${path}) # last word of the path is the name
# Add the library as an interface only since we don't intend on actually compiling anyway
add_library(${lib_name} INTERFACE)
set_target_properties(${lib_name} PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${path}/include"
)
target_link_libraries(st_decomp ${lib_name}) # also adds the required include path
endforeach()
# If you have precompiled headers you can add them like this
# trget_precompiled_headers(st_decomp PRIVATE "${CMAKE_SOURCE_DIR}/src/pch.h")
+14
View File
@@ -6,6 +6,7 @@ Contents:
- [Build the ROM](#build-the-rom)
- [Matching the base ROM](#matching-the-base-rom)
- [Building with non-matching code](#building-with-non-matching-code)
- [[Optional] LSP setup](#lsp-setup)
## Prerequisites
@@ -50,3 +51,16 @@ ARM7 BIOS in the root directory of this repository, and verify that your dumped
| --------------- | ------------------------------------------ |
| `arm7_bios.bin` | `6ee830c7f552c5bf194c20a2c13d5bb44bdb5c03` |
| `arm7_bios.bin` | `24f67bdea115a2c847c8813a262502ee1607b7df` |
## LSP setup
**This is likely not necessary.** Most C++ editors usually have their one LSP (Language Server Protocol, a tool for code completion and more) configuration that should recognize the project structure and work out of the box. This section is about how to setup your LSP yourself **if the need be**.
The repository contains a [`CMakeLists.txt`](CMakeLists.txt) that allows generating a compilation database. For now, the `CMakeLists.txt` can only be used to generate `compile_commands.json` and similar files, not compiling the project.
To generate the compilation database, run `cmake -S . -G "Unix Makefiles" -B cmake` from the root directory of the project. This will create a `cmake/` directory that contains the `compile_commands.json`.
Once the file is generated, you can dynamically link it to the root directory and let your LSP detect it (make sure not to `git add` it though), or edit your `.clangd` as follows for it to recognize the compilation database:
```clangd
CompileFlags:
CompilationDatabase: "cmake"
```
This setup is adapted from a [tutorial by Strus](https://gist.github.com/Strus/042a92a00070a943053006bf46912ae9), refer to his post for further details.