Skip to content

Commit 9ace096

Browse files
authored
doc: compile_commands.json (#162)
1 parent aeac6ca commit 9ace096

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

pages/spec/compile_commands.md

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# `compile_commands.json`
2+
3+
`fpm` automatically generates a `compile_commands.json` file during each successful build or dry run. The file is placed in the `build/` directory and provides detailed information about how each source file is compiled.
4+
5+
```txt
6+
build/compile_commands.json
7+
```
8+
9+
It follows the [Clang Compilation Database format](https://clang.llvm.org/docs/JSONCompilationDatabase.html), widely used by editors, IDEs, and development tools for navigation, static analysis, and diagnostics.
10+
11+
## Purpose
12+
13+
The `compile_commands.json` file enables integration with tools such as:
14+
15+
- Language Server Protocol (LSP) servers (e.g., `fortls`)
16+
- Code editors (e.g., Visual Studio Code)
17+
- Static analyzers
18+
- Linters and formatters
19+
20+
These tools use it to retrieve compilation flags, include directories, and output paths automatically.
21+
22+
## How It Works
23+
24+
- Generated during `fpm build` or `fpm build --list`
25+
- Created in `build/compile_commands.json`
26+
- No manual configuration required
27+
- Regenerated after each build or dry run
28+
29+
The `--list` option performs a dry run: it downloads dependencies and generates `compile_commands.json` without compiling any source files.
30+
31+
Each command is recorded as a list of parsed arguments, using:
32+
- `shlex` on Unix and macOS
33+
- `mslex` on Windows
34+
35+
This ensures correct handling of spaces, quotes, and escape characters across platforms.
36+
37+
## Example
38+
39+
A minimal entry in `compile_commands.json`:
40+
41+
```json
42+
[
43+
{
44+
"directory": "/path/to/my/fpm/package",
45+
"arguments": [
46+
"gfortran",
47+
"-c",
48+
"src/module.f90",
49+
"-Iinclude",
50+
"-Jbuild",
51+
"-o",
52+
"build/module.o"
53+
],
54+
"file": "src/module.f90"
55+
}
56+
]
57+
```
58+
59+
## Working with the File
60+
61+
As a standard JSON file, it can be inspected using any JSON tool. For example, to pretty-print it:
62+
63+
```bash
64+
cat build/compile_commands.json | jq .
65+
```
66+
67+
## Limitations
68+
69+
- Only created after a successful build or dry run
70+
- Currently not configurable via `fpm.toml`
71+
- Overwrites any previous version during each build
72+
- Feature available since `v0.12.0`.

pages/spec/index.md

+3
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ The generated API documentation of the fpm internals can be found [here](https:/
1313
:::{toctree}
1414
manifest
1515
metapackages
16+
compile_commands
1617
API documentation <https://fortran-lang.github.io/fpm>
1718
:::
19+
20+

0 commit comments

Comments
 (0)