Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating compilation database for a single target with cmake

Tags:

cmake

I'm using CMAKE_EXPORT_COMPILE_COMMANDS variable of cmake to obtain a json compilation database that I can then parse to identify the options that are given to the compiler for each source file. Now, the project I'm working on has several targets, and there are several occurrences of the source files that are used in different targets in the database, as can be shown by the example below:

f.c:

int main () { return MACRO; }

CMakeLists.txt:

cmake_minimum_required (VERSION 2.6)
project (Test)
add_executable(test1 f.c)
add_executable(test2 f.c)
target_compile_options(test1 PUBLIC -DMACRO=1)
target_compile_options(test2 PUBLIC -DMACRO=2)

running cmake . -DCMAKE_EXPORT_COMPILE_COMMANDS=1 will produce the following compile-commands.json file, with two entries for f.c, and no easy way to distinguish between them.

[
{
  "directory": "/home/virgile/tmp/cmakefile",
  "command": "/usr/bin/cc     -DMACRO=1 -o CMakeFiles/test1.dir/f.c.o   -c /home/virgile/tmp/cmakefile/f.c",
  "file": "/home/virgile/tmp/cmakefile/f.c"
},
{
  "directory": "/home/virgile/tmp/cmakefile",
  "command": "/usr/bin/cc     -DMACRO=2 -o CMakeFiles/test2.dir/f.c.o   -c /home/virgile/tmp/cmakefile/f.c",
  "file": "/home/virgile/tmp/cmakefile/f.c"
}
]

I'm looking for a way to specify that I'm only interested in e.g. target test1, as what you can do in build tool mode with --target, preferably without having to modify CMakeLists.txt, but this is not a major issue. What I'd like to avoid, on the other hand, is to read the argument of -o in the "command" entry and discriminate between the test1.dir and test2.dir path component.

like image 302
Virgile Avatar asked Feb 27 '18 19:02

Virgile


People also ask

What is a compilation database?

What is a compilation database? ¶ A compilation database is a database for compile options. It records which compile options are used to build the files in a project. A compilation database can be, but is not limited to, a JSON Compilation Database.

What is Cmake_export_compile_commands?

CMAKE_EXPORT_COMPILE_COMMANDS. New in version 3.5. Enable/Disable output of compile commands during generation. If enabled, generates a compile_commands.json file containing the exact compiler calls for all translation units of the project in machine-readable form.

Is a collection and compilation database?

What is a Compilation Database? In the most general terms a Compilation Database is simply the collection of all information needed to compile a source file, or set of source files – on a file-by-file basis.

What does Compile_commands json do?

You can use the 'compile_commands. json' file to compile a project or analyze the project by third-party utilities. The PVS-Studio C and C++ analyzer works with this format as well.


1 Answers

Apparently this feature has been implemented in a merge-request about 3 months ago: https://gitlab.kitware.com/cmake/cmake/-/merge_requests/5651

From there I quote:

The new target property EXPORT_COMPILE_COMMANDS associated with the existing global variable can be used to optionally configure targets for their compile commands to be exported.

So it seems that you now can set a property on the respective targets in order to control whether or not they will be included in the generated DB.


This feature is part of cmake 3.20. Official docs: https://cmake.org/cmake/help/latest/prop_tgt/EXPORT_COMPILE_COMMANDS.html

like image 149
Raven Avatar answered Dec 04 '22 08:12

Raven