Run a command or macro in CMake as the last step before completing the Setup step

Is it possible with CMake (version> = 2.8.7) to execute a macro or command as the last step until the configuration phase is complete?

Functionality must be completed before the following lines are printed on the screen:

-- Configuring done -- Generating done 

So far, I have not been able to find a CMake goal that could be used as a dependency to achieve this goal using add_custom_command add_custom_target or add_dependencies .

EDIT: We have a library that exports several CMake macros, and some of these macros must be executed at the end of each CMakeLists.txt file after all other CMake commands have been executed. Ideally, the desired behavior can be achieved by including the macros.cmake file in the CMakeLists.txt file without the need to add an additional command at the end of this CMakeLists.txt file.

It would also be possible to achieve this by collecting all the functionality in one macro, which must be explicitly named at the end of CMakeLists.txt . However, there are already several dependent libraries that will need to be adapted, and solving this problem would skip this extra work. In addition, adding a macro may be forgotten, or the requirement that it be the most recent statement can be easily violated.

macros.cmake example:

 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) 

Top-level example of 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 explicitely writing it down 

If there is no other option, we will have to require each user to call a special macro at the end of their CMakeLists.txt file.

+5
cmake
Apr 02 '13 at 9:28
source share
3 answers

OK - this answer seems a bit fragile as it uses the undocumented behavior of CMake. However, it seems to work.

At the end of the configuration process, after processing all the commands in the CMakeLists.txt file, CMake checks the value of CMAKE_BACKWARDS_COMPATIBILITY . If this variable is viewed using variable_watch , then it will be launched here.

So, you can transfer this functionality to a couple of functions:

 function(EOFHook Variable Access) if(${Variable} STREQUAL CMAKE_BACKWARDS_COMPATIBILITY AND (${Access} STREQUAL UNKNOWN_READ_ACCESS OR ${Access} STREQUAL READ_ACCESS)) execute_process(COMMAND ${CMAKE_COMMAND} -E echo "In EOF hook command") ... do whatever else is required ... endif() endfunction() function(SetupEOFHook) variable_watch(CMAKE_BACKWARDS_COMPATIBILITY EOFHook) endfunction() 

To use this in any CMakeLists file, simply call SetupEOFHook() anywhere in the file.

This is a little risky; if the variable is also read elsewhere in CMakeLists.txt, for example. via

 message(${CMAKE_BACKWARDS_COMPATIBILITY}) 

then it will run the EOFHook function here and at the end. You can add extra complexity to the function by adding a counter and checking that it only message(FATAL_ERROR ...) called once or message(FATAL_ERROR ...)

+2
Apr 05 '13 at 2:19
source share
— -

The CMake execute_process command will be executed during the configuration phase and not the build phase, so if you put this at the end of your CMakeLists.txt file, it will be executed last.

+1
Apr 02 '13 at
source share

No, which I know about (and I was looking hard). I would like to be proved wrong.

0
Apr 2 '13 at 15:00
source share



All Articles