|
| 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`. |
0 commit comments