I have a CMakeLists.txt
in the root of my project with CMAKE_EXPORT_COMPILE_COMMANDS
turned on.
I am building target inside a folder and is called via add_subdirectory
. The folder has its own CMakeLists.txt
file.
compile_commands.json
is built in the root of the binary directory, and I want to copy it to root of source directory after running the cmake command.
I have tried two approaches.
add_custom_target
andadd_custom_command
. I created a dummy target. I used this target foradd_custom_command
to copy the file but it does not trigger command inadd_custom_command
.file(COPY ...)
The first time the command is triggered, thecompile_commands.json
is not created via cmake. I don't want to run cmake command twice to get copycompile_commands.json
to root of source folder.
CMAKE_MINIMUM_REQUIRED(VERSION 3.8)
project(exec LANGUAGES CXX)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_subdirectory("DirectoyWithTarget")
add_custom_target(copy_compile_commands
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/compile_commands.json)
add_custom_command(
TARGET copy_compile_commands
PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_CURRENT_BINARY_DIR}/compile_commands.json
${CMAKE_CURRENT_SOURCE_DIR}/compile_commands.json
)
The compile_commands.json
file is not copied to ${CMAKE_CURRENT_SOURCE_DIR}
. This means the add_custom_command
is not triggered.