user1610950
user1610950

Reputation: 1917

clion run cmake on each build

I am not sure if its possible to do this with clion, I was testing out the program and enjoy using it to write c code because the ctags and etags support is really nice.

I am copying some files over from the cmake source tree to the bin location on each build. While using clion if I update some of the files that I am copying the results aren't updated within clion.

If I instead go back to the terminal and just run the typical cmake steps

cmake ../ && make && bin/./program

that copies the updated files and I am able to see my results.

This is the CMake command that I am using in my build.

FILE(COPY ${CMAKE_CURRENT_SOURCE_DIR}/resources/ DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/bin/resources/)

I might be able to restructure the cmake command to make it copy every time or it might just be a clion issue. I am unsure and would like to be able to take care of this all from within clion, instead of going back to the terminal to run the cmake command for updates to take effect.

Upvotes: 0

Views: 1648

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 66278

If you want CMake to make some action whenever some file is changed, you should create a rule using add_custom_command and pass file via DEPENDS argument. Note, that only file-level dependencies are supported by CMake, for make dependency on directory you need to list all files in it.

# Collect list of files within directory.
FILES(GLOB files_list RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/resources/
    "${CMAKE_CURRENT_SOURCE_DIR}/resources/*") 
# This will contain full paths to files in binary directory. 
set(binary_files_list)
foreach(file ${files_list})
    set(source_file ${CMAKE_CURRENT_SOURCE_DIR}/resources/${file})
    set(binary_file ${CMAKE_CURRENT_BINARY_DIR}/bin/resources/${file})
    add_custom_command(OUTPUT ${binary_file}
        COMMAND cmake -E ${source_file} ${binary_file}
        DEPENDS ${source_file})
    list(APPEND binary_files_list ${binary_file})
endforeach()
add_custom_target(resources DEPENDS ${binary_files_list})

Note, that in case of adding/removing files you should to run cmake explicitely. That's why hardcoded files list is preferred to GLOBing.

Upvotes: 1

Related Questions