Skip to main content

Compilation Database Generation

Goal

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

Third party tools to generate the compile_commands.json

Tip

To analyze a complete project with many source files, you need a compilation database, which is a file typically named compile_commands.json containing all the compiler invocations for the source files. It provides Codee with information about the name, version and location of the compiler in the system, required compiler flags, and location of the target source files.

For Linux, the recommendation is to use Bear.

For Windows, the recommendation is to use CMake with Ninja generators.

LinuxCompilersCompiler wrappersWindows
bear
CMakecodee CLINinja generator

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:

compile_commands.json
[
{
"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"
}
]

Now you can use the JSON generated to substitute the compilation command in the Codee invocation. For example:

Codee command
codee checks --check-id PWR069 -p compile_commands.json
Codee output
Note: the compilation database entries will be analyzed in the order necessary to meet module dependencies between Fortran source files.
Configuration file 'compile_commands.json' successfully parsed.
Date: 2025-10-08 Codee version: 2025.3.7 License type: Professional
Performing Fortran module dependency analysis... Done

[1/1] himeno.f90 ... Done

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

SUGGESTIONS

Use --verbose to get more details, e.g:
codee checks --verbose --check-id PWR069 -p compile_commands.json

1 file, 7 functions, 5 loops, 214 LOCs successfully analyzed (5 checkers) and 0 non-analyzed files in 289 ms

Using CMake (Alternative)

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:

compile_commands.json
[
{
"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 command
codee checks --check-id PWR069 -p compile_commands.json
Codee output
Note: the compilation database entries will be analyzed in the order necessary to meet module dependencies between Fortran source files.
Configuration file 'build/compile_commands.json' successfully parsed.
Date: 2025-10-08 Codee version: 2025.3.7 License type: Professional
Performing Fortran module dependency analysis... Done

[1/1] /home/user/codee-demos/Fortran/Himeno/himeno.f90 ... Done

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

SUGGESTIONS

Use --verbose to get more details, e.g:
codee checks --verbose --check-id PWR069 -p build/compile_commands.json

1 file, 7 functions, 5 loops, 214 LOCs successfully analyzed (5 checkers) and 0 non-analyzed files in 182 ms