How to Use Cmake_Export_Compile_Commands

Generating compilation database for a single target with cmake

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

Generate CMake compile commands on Windows

I was able to reproduce your error message with the following CMakeLists.txt. This is not a bug:

cmake_minimum_required(VERSION 3.20)
project(test)

I then ran

> cmake -G Ninja -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
-- The C compiler identification is MSVC 19.28.29915.0
-- The CXX compiler identification is MSVC 19.28.29915.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:

CMAKE_EXPORT_COMPILE_COMMANDS

-- Build files have been written to: D:/test/build

The reason for this is because, as you say,

The top-level CMakeLists.txt does not define any targets, but adds a couple of ExternalProjects to support cross-compilation.

The commands for custom targets are not included in the compile_commands.json, and the variable is only inspected by CMake when processing the first compiled target (either add_executable or add_library).

There is no simple fix; setting CMAKE_EXPORT_COMPILE_COMMANDS in each of your sub-projects will result in one compile_commands.json file for each. Merging them such that Eclipse will understand the result is beyond the scope of this question.

This is part of a larger issue with ExternalProject -- it's too separated. It boils down to automating the creation of some relatively complicated custom targets and commands, so the parent project has no idea what's going on in its children.

A better approach for cross-compiling with host tools is to allow the user to call your build twice: first with a host toolchain in one directory and second with a target toolchain in a different directory with variables set so that the first build will be scanned for host tools instead of re-creating the targets in the target build. The export() and find_package() commands are useful for this.

CMAKE_EXPORT_COMPILE_COMMANDS doesn't work since many cpp files is built into 1 big file somehow

You're correct all the files are being collected into a single file, and that CMake is partially responsible. However this isn't default CMake behavior.

OceanBase is specifically configured as a unity build, meaning all of the translation units are collected into a single "unity" translation unit. You can see where they set this up in their cmake utilities. The relevant target property is UNITY_BUILD ON.

CMake not generating compile_commands.json

This ended up being an issue with using an old version of CMake. I ended up installing the newest version and it worked as expected.

According to Clang docs

"Currently CMake (since 2.8.5) supports generation of compilation databases for Unix Makefile builds (Ninja builds in the works) with the option CMAKE_EXPORT_COMPILE_COMMANDS."

How to generate compiler_command.json using Xcode via CMake

xcodebuild -project path/to/.xcodeproj | xcpretty -r json-compilation-database --output path/for/compile_commands.json



Related Topics



Leave a reply



Submit