[英]Force CMake to generate configure_file target every build
I have following command in my CMakeLists.txt
file我的CMakeLists.txt
文件中有以下命令
configure_file([...]/Version.h.in [...]/Version.h @ONLY)
How do I make it run on every build, but not only when Version.h.in
changes?我如何让它在每个构建上运行,而不仅仅是在Version.h.in
更改时? 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
有__DATE__
宏,实际上每个构建都应该被视为新的,即使它保持不变。
The Version.h.in
looks like Version.h.in
看起来像
static const char VERSION[] = "Bla-bla-bla " @FOOBAR@ " built on " __DATE__;
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.我将我的版本字符串生成转移到我用${CMAKE_COMMAND} -P ...
在add_custom_command()
/ add_custom_target()
调用的自己的 CMake 脚本中,并使我的目标使用它依赖于它。
In your case let's say you have a DateToVersionH.cmake
script like this:在您的情况下,假设您有一个DateToVersionH.cmake
脚本,如下所示:
string(TIMESTAMP _date "%d %m %Y %H:%M")
file(WRITE ${VERSION_FILE_NAME} "#define MY_VERSION \"Bla-bla-bla ${FOOBAR} built on ${_date}\"\n")
you can do something like:您可以执行以下操作:
add_custom_command(
TARGET MyExe
PRE_BUILD
COMMAND ${CMAKE_COMMAND} -DVERSION_FILE_NAME=Version.h -DFOOBAR="${FOOBAR}" -P "${CMAKE_CURRENT_LIST_DIR}/DateToVersionH.cmake"
)
See also use CMake to get build-time svn revision另请参阅使用 CMake 获取构建时 svn 修订版
That may not be enough if your make environment checks all the "needs to be rebuild" prior of calling the necessary steps (eg 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 环境在调用必要步骤之前检查所有“需要重建”,那可能还不够(例如,在 Ninja 中,仅重新扫描自定义命令的输出;有关更多详细信息,请参阅Ninja 文档中的restat
规则可变的,并且本讨论而导致引入的BYPRODUCTS
在选项add_custom_command() )。
So you could simply add to your build shell script - prior to calling the actual make - removing the object file containing the version information.所以你可以简单地添加到你的构建 shell 脚本 - 在调用实际的 make 之前 - 删除包含版本信息的目标文件。
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:这-假设你有一个Version.c
包括Version.h
-这个样子的:
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"
)
Thinking about it, just deleting the object file and utilizing your current __DATE__
solution is the simplest solution.考虑一下,删除目标文件并使用您当前的__DATE__
解决方案是最简单的解决方案。
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?对于另一个“git 版本解决方案”,请参阅CMake:自动使用 git 标签作为版本字符串以及如何使用 cmake 将 git SHA1 作为定义传递给编译器?
Just to submit an alternative answer that seems simpler to me and does the trick if you are under any *nix OS :只是提交一个对我来说似乎更简单的替代答案,并且如果您在任何 *nix 操作系统下都能做到这一点:
Running touch Version.h.in
as a pre-build command, either implemented in your IDE, manually executed before running your cmake commands in your shell, or in your CI (where it might be useless), allows to systematically generate the Version.h file, even if it has not been modified.将touch Version.h.in
作为预构建命令运行,无论是在 IDE 中实现,还是在 shell 中运行 cmake 命令之前手动执行,或在 CI(可能无用)中,都允许系统地生成版本。 h 文件,即使它没有被修改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.