Issue
We build with CMake and normal Unix makefiles. There are some static analysis checks, e.g. Cppcheck, we run at every C/C++ file in the project to catch errors at compile time.
I have created a custom target for cppcheck and attached it into "all" target. This checks all the *.c and *.cpp files in the project.
We want to run a check every time a file is changed and recompiled and only on that file. The check should be run automatically and without the user having to add extra commands in CMake. Essentially, the check should be "attached/hooked" to normal CMake commands add_library()
and add_executable()
. Is there any way to do this in CMake?
Solution
While add_executable
(and add_library
) is provided by CMake itself, you may define a function or a macro with the same name, which would "hide" the original CMake function. Inside your function/macro you may call original CMake function using underscore-prefixed name:
function(add_executable target_name)
# Call the original function
_add_executable(${target_name} ${ARGN})
... perform additional steps...
endfunction(add_executable target_name)
Answered By - Tsyvarev Answer Checked By - Candace Johnson (WPSolving Volunteer)