Is it somehow possible with CMake (version >= 2.8.7) to execute a macro or command as a last step before the configuration phase finishes?
The functionality should be executed before the following lines get printed on screen:
-- Configuring done
-- Generating done
Up to now I wasn't able to find a CMake target which could be used as a dependency to achieve this with add_custom_command
add_custom_target
or add_dependencies
.
EDIT:
We have a library exporting several CMake macros and some of these macros must be executed at the end of each CMakeLists.txt
file after all other CMake commands were run.
Ideally the desired behavior can be achieved by including a file macros.cmake
in a CMakeLists.txt
file without the necessity to add an additional command at the end of this CMakeLists.txt
file.
It would also be possible to achieve this by gathering all functionality in one macro which needs to be called explicitly at the end of the CMakeLists.txt
.
However, there are already several dependent libraries which would need to be adapted and a solution to this problem would omit this additional work.
Also, adding the macro can be forgotten or the requirement for it being the very last statement can be easily violated.
Example macros.cmake
:
macro(FINAL_MACRO)
message(STATUS "Last step before finishing Configure phase")
endmacro()
# HERE: something like add_custom_target(final_steps)
# followed by something like add_dependencies(final_steps cmake_configure_finished)
Example toplevel CMakeLists.txt
:
cmake_minimum_required(VERSION 2.8.7)
include(macros.cmake)
add_subdirectory(source)
add_subdirectory(interfaces)
# Here FINAL_MACRO should get executed without explicitly writing it down
If there is no other option we will have to require every user to call a special macro at the end of their CMakeLists.txt
file.
OK - This answer feels a bit fragile since it's making use of undocumented CMake behaviour. However, it does seem to work.
Towards the end of the configuring process, after all commands in the CMakeLists.txt file have been processed, CMake checks the value of
CMAKE_BACKWARDS_COMPATIBILITY
. If this variable is being watched via thevariable_watch
command, then it will be triggered here.So you can wrap this functionality into a couple of functions:
To use this in any CMakeLists file, simply call
SetupEOFHook()
anywhere in the file.It's a bit risky; if the variable is also read elsewhere in the CMakeLists.txt e.g. via
then it would trigger the
EOFHook
function here and at the end. You could add more complexity to the function by adding a counter and checking that it's only called once or else issue amessage(FATAL_ERROR ...)
The CMake command
execute_process
will be executed during the configure phase rather than the build phase, so if you put this at the end of your CMakeLists.txt file, it will be executed last.None that I know of (and I've searched hard). I'd love to be proven wrong, though.