如何在CMake中将对脚本的依赖项添加到目标? [英] How do I add a dependency on a script to a target in CMake?

查看:170
本文介绍了如何在CMake中将对脚本的依赖项添加到目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序链接后,我需要对其进行一些后处理。我添加了 add_custom_command(TARGET ... ,效果很好。但是,此额外的自定义命令运行了一个脚本(未生成;已检入代码库),并且如果该脚本发生更改,我希望该目标被认为已过时,以便可以正确地重建它。



add_dependencies 规则似乎仅在顶级元素之间起作用,而这不是(只是一个脚本),并且<$ c $的这种形式没有 DEPENDS 元素c>我可以使用的add_custom_command 。



我该怎么做?

解决方案

不幸的是,这有点令人费解,但是您可以使用 add_custom_target 以通过脚本处理模式调用CMake -P



您需要使用 add_custom_target ,因为即使所有内容都是最新的,它也会一直执行。



我们已经做出了此决定,需要具有自定义目标执行命令,这些命令将检查您的后处理脚本文件的新版本(让我们将其称为 my_script,并假设它位于您的根目录中),如果更改了,则会导致从属目标退出日期。



这将包括:


  1. 比较 my_script的先前副本表示源树中当前的 my_script。如果当前的 my_script不同,或者不存在副本(即这是CMake的第一次运行),则...

  2. 将 my_script从源树复制到构建树,然后...

  3. 触摸从属目标的源文件,使其过时。 li>

CMake脚本中所需的所有命令可以使用 execute_process 调用 cmake -E



因此CMake脚本(称为 copy_script .cmake)类似于:

  execute_process(COMMAND $ {CMAKE_COMMAND} -E compare_files 
$ {OriginalScript } $ {CopiedScript} RESULT_VARIABLE结果)
if(结果)
execute_process(COMMAND $ {CMAKE_COMMAND} -E复制
$ {OriginalScript} $ {CopiedScript})
execute_process(COMMAND $ {CMAKE_COMMAND} -E touch_nocreate $ {FileToTouch})
endif()

CMake脚本需要通过 -D args 调用 -P 之前,因此,调用CMakeLists.txt的内容如下:

  set(FileToTouch $ {CMAKE_SOURCE_DIR} /src/main.cpp)

add_custom_target(CopyScript ALL $ {CMAKE_COMMAND}
-DOriginalScript = $ {CMAKE_SOURCE_DIR} / my_script
-DCopiedScript = $ {CMAKE_BINARY_DIR} / my_script
-DFileToTouch = $ { FileToTouch}
-P $ {CMAKE_SOURCE_DIR} /copy_script.cmake)

add_executable(MyExe $ {FileToTouch})

这将导致可执行文件的完全重建,因为它认为源文件已被修改。如果只需要强制重新链接,则可能会有更好的方法来实现。 / p>

After my program is linked I need to perform some post-processing on it. I added a add_custom_command(TARGET ... and that worked fine. However, this extra custom command runs a script (that is not generated; it's checked into the codebase), and I want the target to be considered out of date if that script changes so it will be rebuilt properly.

The add_dependencies rule seems to only work between top-level elements, which this is not (it's just a script), and there's no DEPENDS element in this form of the add_custom_command that I can use.

How do I do this?

解决方案

It's unfortunately a bit convoluted, but you can use add_custom_target to invoke CMake in script-processing mode via -P.

You need to use add_custom_target here since it will always execute, even if everything's up to date.

Having made this decision, we need to have the custom target execute commands which will check for a new version of your post-processing script file (let's call this "my_script" and assume it's in your root dir), and if it's changed cause your dependent target to go out of date.

This would comprise:

  1. Compare a previous copy of "my_script" to the current "my_script" in the source tree. If the current "my_script" is different, or if the copy doesn't exist (i.e. this is the first run of CMake) then...
  2. Copy "my_script" from the source tree to the build tree, and...
  3. Touch a source file of the dependent target so that it goes out of date.

All of the commands required inside the CMake script can be achieved using execute_process to invoke cmake -E.

So the CMake script (called e.g. "copy_script.cmake") would be something like:

execute_process(COMMAND ${CMAKE_COMMAND} -E compare_files
                    ${OriginalScript} ${CopiedScript} RESULT_VARIABLE Result)
if(Result)
  execute_process(COMMAND ${CMAKE_COMMAND} -E copy
                      ${OriginalScript} ${CopiedScript})
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${FileToTouch})
endif()

The CMake script needs to have required variables passed in via the -D args before calling -P, so the calling CMakeLists.txt would have something like:

set(FileToTouch ${CMAKE_SOURCE_DIR}/src/main.cpp)

add_custom_target(CopyScript ALL ${CMAKE_COMMAND}
                      -DOriginalScript=${CMAKE_SOURCE_DIR}/my_script
                      -DCopiedScript=${CMAKE_BINARY_DIR}/my_script
                      -DFileToTouch=${FileToTouch}
                      -P ${CMAKE_SOURCE_DIR}/copy_script.cmake)

add_executable(MyExe ${FileToTouch})

This will cause a full rebuild of the executable, since it thinks a source file has been modified. If you only require to force relinking there may be a better way to achieve this.

这篇关于如何在CMake中将对脚本的依赖项添加到目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆