强制CMake在每个版本中生成configure_file目标 [英] Force CMake to generate configure_file target every build

查看:386
本文介绍了强制CMake在每个版本中生成configure_file目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 CMakeLists.txt 文件中有以下命令

configure_file([...]/Version.h.in [...]/Version.h @ONLY)

如何使它在每个版本上运行,但不仅要在 Version.h.in 更改时运行?我需要它,因为 Version.h 中具有 __ DATE __ 宏,实际上,即使对于每个构建,它也应视为新的宏,甚至

How do I make it run on every build, but not only when Version.h.in changes? I need that because Version.h has __DATE__ macro in it and actually should be treated as new for every build, even while it remains the same.

Version.h.in 看起来像

static const char VERSION[] = "Bla-bla-bla " @FOOBAR@ " built on " __DATE__;


推荐答案

我将版本字符串生成转移到了自己的CMake脚本中我在 add_custom_command() / $ {CMAKE_COMMAND} -P ... 调用的> add_custom_target()并使其依赖于我的目标。

I transfered my version string generation into its own CMake script that I call with ${CMAKE_COMMAND} -P ... in a add_custom_command()/add_custom_target() and make my target using it depend on it.

在您的情况下,假设您有一个 DateToVersionH.cmake 脚本如下:

In your case let's say you have a DateToVersionH.cmake script like this:

string(TIMESTAMP _date "%d %m %Y %H:%M")
file(WRITE ${VERSION_FILE_NAME} "#define MY_VERSION \"Bla-bla-bla ${FOOBAR} built on ${_date}\"\n")

您可以执行以下操作:

add_custom_command(
    TARGET MyExe
    PRE_BUILD
    COMMAND ${CMAKE_COMMAND} -DVERSION_FILE_NAME=Version.h -DFOOBAR="${FOOBAR}" -P "${CMAKE_CURRENT_LIST_DIR}/DateToVersionH.cmake"
)

另请参见使用CMake来获取构建时的svn修订版

如果您的make环境事先检查了所有需要重建的内容,那可能还不够调用必要步骤(例如在Ninja中,仅重新扫描自定义命令的输出;有关更多详细信息,请参见忍者文档中的 restat 规则变量和此讨论,从而引入了< BYPRODUCTS 选项> add_custom_command())。

That may not be enough if your make environment checks all the "needs to be rebuild" prior of calling the necessary steps (e.g. in Ninja only the outputs of custom commands are re-scanned; for more details see the Ninja documentation for the restat rule variable and this discussion which led to the introduction of the BYPRODUCTS option in add_custom_command()).

因此,您可以在调用实际make之前简单地将其添加到构建shell脚本中-删除包含该版本的目标文件信息。

So you could simply add to your build shell script - prior to calling the actual make - removing the object file containing the version information.

或者在链接之前您要强制重新编译对象。假设您有一个 Version.c ,其中包括 Version.h ,这将是这样的:

Or you are forcing the re-compilation of the object before linking. This would - assuming you have a Version.c which includes Version.h - look like this:

include_directories(${CMAKE_CURRENT_BINARY_DIR})
execute_process(COMMAND "${CMAKE_COMMAND}" -DVERSION_FILE_NAME=Version.h -DFOOBAR="${FOOBAR}" -P "${CMAKE_CURRENT_LIST_DIR}/DateToVersionH.cmake")
add_library(MyVersionObj OBJECT Version.c)

add_executable(MyExe ... $<TARGET_OBJECTS:MyVersionObj>)

add_custom_command(
    TARGET MyExe
    PRE_LINK 
    COMMAND "${CMAKE_COMMAND}" -DVERSION_FILE_NAME=Version.h -DFOOBAR="${FOOBAR}" -P "${CMAKE_CURRENT_LIST_DIR}/DateToVersionH.cmake"
    COMMAND "${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" --config $<CONFIG> --target "MyVersionObj"
)

考虑一下,只需删除目标文件并使用当前的 __ DATE __ 解决方案是最简单的解决方案。

Thinking about it, just deleting the object file and utilizing your current __DATE__ solution is the simplest solution.

对于另一个 git ver sion解决方案,请参见 CMake:自动使用git标签作为版本字符串如何我使用cmake将git SHA1作为定义传递给编译器?

For another "git version solution" see CMake: Automatically use git tags as version strings and How can I pass git SHA1 to compiler as definition using cmake?

这篇关于强制CMake在每个版本中生成configure_file目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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