Compilation Database Generation
To analyze a complete project, you need a compilation database containing all
the compiler instructions for its source files. In this guide, we walk you through
generating a compile_commands.json file , demonstrated with a Fortran open
source project.
Getting ready
For this demonstration, we will use the Fortran implementation of the Himeno benchmark, a Poisson equation solver. Start by cloning the repository:
git clone https://github.com/codee-com/codee-demos.git
Navigate to the source code directory:
cd codee-demos/Fortran/Himeno
Tools for generating compile_commands.json files
CMake (meta-build system) and Bear (recommended) are the two most common tools for generating
compile_commands.json files.
See all the options: Alternatives to Bear
Using Bear (recommended)
Ensure you are using the bear tool distributed with Codee, not other
installations of bear, in order to take advantage of its enhanced
support for compilers (e.g. GNU, LLVM, Intel, AMD, Nvidia, Cray) and compiler
wrappers (e.g. MPI wrappers in supercomputers, Microsoft wrappers).
The recommended way of generating the compilation database is to use
bear, which comes bundled with
Codee packages. bear is able to intercept the compiler calls made by
your build system in order to generate the compile_commands.json file.
To use it you only need to prepend bear -- to your typical build command.
To generate it for the Himeno project copy the following command:
bear -- make
Since Himeno is a single-file project, the compilation database will have a single entry:
[
{
"arguments": [
"/usr/bin/gfortran",
"-c",
"-O3",
"-I",
"/usr/include",
"-I",
".",
"-o",
"himeno",
"himeno.f90"
],
"directory": "/home/user/codee-demos/Fortran/Himeno",
"file": "/home/user/codee-demos/Fortran/Himeno/himeno.f90",
"output": "/home/user/codee-demos/Fortran/Himeno/himeno"
}
]
Running codee commands with the additional --db codee.db flag enables
Incremental Static Analysis. This reduces runtime by storing analysis
results and reusing them in subsequent analysis, reanalyzing only
the source code that has changed.
Now you can use the JSON generated to substitute the compilation command in the Codee invocation. For example:
codee checks --check-id PWR069 -p compile_commands.json --db codee.db
Date: 2026-03-25 Codee version: 2026.1 License type: Team
Searching Incremental Static Analysis database... Enabled
[1/1] himeno.f90 ... Done
QUALITY CHECKS REPORT
himeno.f90:137:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module
himeno.f90:165:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module
himeno.f90:224:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module
himeno.f90:256:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module
himeno.f90:276:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module
OPTIMIZATION CHECKS REPORT
No actionable items were found
SUGGESTIONS
Use --check-id and --verbose to focus on specific subsets of checkers, e.g.:
codee checks --check-id PWR069 --verbose -p compile_commands.json --db codee.db
1 built file (0 results taken from cache), 0 dependencies (0 reused from cache)
1 target file, 7 functions, 5 loops, 214 SLOCs successfully analyzed (5 checkers) and 0 non-analyzed files in 351 ms
Using CMake (alternative option)
Ensure you are using CMake version 3.5 or higher.
As an alternative, CMake has its own way of obtaining the compile_commands.json file.
Add -DCMAKE_EXPORT_COMPILE_COMMANDS=ON to your usual CMake invocation; for instance:
cmake . -DCMAKE_Fortran_COMPILER=gfortran -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -G "Ninja" -B build
The compile_commands.json file will be now stored in the build/ directory and
it will look like this:
[
{
"directory": "/home/user/codee-demos/Fortran/Himeno/build",
"command": "/usr/bin/gfortran -c /home/user/codee-demos/Fortran/Himeno/himeno.f90 -o CMakeFiles/himeno.dir/himeno.f90.o",
"file": "/home/user/codee-demos/Fortran/Himeno/himeno.f90",
"output": "CMakeFiles/himeno.dir/himeno.f90.o"
}
]
Now you can use the JSON generated to substitute the compilation command in the Codee invocation. For example:
codee checks --check-id PWR069 -p build/compile_commands.json --db codee.db
Date: 2026-03-25 Codee version: 2026.1 License type: Team
Searching Incremental Static Analysis database... Enabled
[1/1] /home/user/codee-demos/Fortran/Himeno/himeno.f90 ... Done
QUALITY CHECKS REPORT
/home/user/codee-demos/Fortran/Himeno/himeno.f90:137:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module
/home/user/codee-demos/Fortran/Himeno/himeno.f90:165:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module
/home/user/codee-demos/Fortran/Himeno/himeno.f90:224:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module
/home/user/codee-demos/Fortran/Himeno/himeno.f90:256:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module
/home/user/codee-demos/Fortran/Himeno/himeno.f90:276:1 [PWR069] (level: L2): Use the keyword only to explicitly state what to import from a module
OPTIMIZATION CHECKS REPORT
No actionable items were found
SUGGESTIONS
Use --check-id and --verbose to focus on specific subsets of checkers, e.g.:
codee checks --check-id PWR069 --verbose -p build/compile_commands.json --db codee.db
1 built file (0 results taken from cache), 0 dependencies (0 reused from cache)
1 target file, 7 functions, 5 loops, 214 SLOCs successfully analyzed (5 checkers) and 0 non-analyzed files in 337 ms