本文介绍了CMake:执行宏/函数作为add_custom_command的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个外部库,该库提供用于自动代码生成的CMake功能,该库将在我的CMakeList中使用.问题是,每当我修改CMakeLists时,该函数就会再次运行,从而触发新生成但未更改的源的重新编译.我需要诸如add_custom_command之类的东西,并且可以将CMake函数指定为COMMAND而不是可执行文件,以便仅当自动生成的文件不存在时才运行该函数.这可行吗?如果不是,是否存在另一种获得相同结果的方法?谢谢.

I'm using an external library which provides a CMake function for automatic code generation, to be used in my CMakeLists. The problem is that whenever I modify a CMakeLists then the function is run again, triggering the recompilation of the newly generated but unchanged sources. I'd need something like add_custom_command with the possibility to specify the CMake function as COMMAND instead of an executable, so that the function is run only if the automatically generated files are not already present.Is this feasible? If not, does it exist another way to obtain the same result?Thanks.

推荐答案

要阻止该函数运行,只需将其包装到if中:

To prevent that function to run, just wrap it into if:

if(NOT EXISTS ${CMAKE_BINARY_DIR}/blah-blah/generated.cpp)
   run_your_provided_command(BLAH_BLAH)
endif()

容易!

更新:要在配置文件更改后运行它,只需使用稍微复杂一点的条件即可:

Update: To run it when config file has changed just use little more complicated condition:

if(
   NOT EXISTS ${CMAKE_BINARY_DIR}/blah-blah/generated.cpp OR
   ${CMAKE_SOURCE_DIR}/blah-blah.config IS_NEWER_THAN ${CMAKE_BINARY_DIR}/blah-blah/generated.cpp
  )
...

并使用 add_dependencies 命令,以确保在配置文件被修改的情况下可以重新构建您的二进制文件:

and use add_dependencies command to make sure your binary will be rebuild in case of config file modifications:

add_executable(
    YourBinary
    ...
    ${CMAKE_BINARY_DIR}/blah-blah/generated.cpp
  )
add_dependencies(YourBinary ${CMAKE_SOURCE_DIR}/blah-blah.config)

这篇关于CMake:执行宏/函数作为add_custom_command的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 05:52