在“配置”步骤完成之前的最后一步,在CMake中执行命令或宏 [英] Execute command or macro in CMake as the last step before the 'configure' step finishes
问题描述
在配置阶段完成之前,使用CMake(版本> = 2.8.7)是否可以在最后一步执行宏或命令?
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
到目前为止,我仍然找不到能够用作实现此目标的依赖项的CMake目标。使用 add_custom_command
add_custom_target
或 add_dependencies
。
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
.
编辑:
我们有一个库,导出几个CMake宏,其中某些宏必须在每个 CMakeLists.txt $ c的末尾执行。运行所有其他CMake命令后,$ c>文件。
理想情况下,可以通过在 CMakeLists.txt
文件中包含文件 macros.cmake
来实现所需的行为。无需在此 CMakeLists.txt
文件末尾添加其他命令。
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.
可以通过将所有功能收集在一个宏中来实现此目的,该宏需要在 CMakeLists.txt
的末尾显式调用。
但是,已经有几个依赖库需要进行调整,解决此问题的方法将省去这项额外的工作。
另外,添加宏可能会被忽略,或者很容易违反其作为最后一条语句的要求。
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.
示例 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)
示例顶级 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
没有其他选择,我们将不得不要求每个用户在其 CMakeLists.txt
文件末尾调用一个特殊的宏。
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.
推荐答案
好-由于该答案利用了未记录的CMake行为,因此感觉有点脆弱。但是,它似乎确实起作用。
OK - This answer feels a bit fragile since it's making use of undocumented CMake behaviour. However, it does seem to work.
在配置过程结束时,处理完CMakeLists.txt文件中的所有命令后,CMake会检查< a href = http://www.cmake.org/cmake/help/v2.8.10/cmake.html#variable%3aCMAKE_BACKWARDS_COMPATIBILITY rel = noreferrer title = CMake v2.8.10文档 CMAKE_BACKWARDS_COMPATIBILITY变量 > CMAKE_BACKWARDS_COMPATIBILITY
。如果正在通过 variable_watch
命令的文档,然后将在此处触发。
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 the variable_watch
command, then it will be triggered here.
因此您可以将此功能包装为几个功能:
So you can wrap this functionality into 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()
要在任何CMakeLists文件中使用此功能,只需调用 SetupEOFHook()
在文件中的任何位置。
To use this in any CMakeLists file, simply call SetupEOFHook()
anywhere in the file.
这有点冒险;如果该变量还在CMakeLists.txt中的其他位置读取,例如通过
It's a bit risky; if the variable is also read elsewhere in the CMakeLists.txt e.g. via
message(${CMAKE_BACKWARDS_COMPATIBILITY})
,那么它将在和末尾触发 EOFHook
函数。您可以通过添加计数器并检查它仅被调用一次来增加函数的复杂性,否则发出 message(FATAL_ERROR ...)
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 a message(FATAL_ERROR ...)
这篇关于在“配置”步骤完成之前的最后一步,在CMake中执行命令或宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!