导致cmake强制重新编译文件,但前提是项目被重新编译?

发布于 2025-02-13 07:28:44 字数 1023 浏览 1 评论 0原文

我通常有和可以cmake can cmake can始终强制汇编/构建特定文件?

我使用__ Date __有一个C ++文件来显示我的应用程序的构建日期。但是,如果未修改此文件,则不会重建该文件,并且不会更新日期。

cmake可以始终重建该特定文件吗?

...除了我想要一些稍有不同的东西:

在CMAKE项目中,我(对于C,用于使用我使用的transpiles),有时我运行make时,代码没有实际更改从程序没有重新编译(或重新链接)的意义上,可以很好地检测到。

显然,在这种情况下,我不想更新时间戳,最终得到了一个新的可执行文件,除了构建日期外,它与前面相同。

我在引用的帖子中看到,只需确保文件上更改的时间戳即可迫使重新编译。因此,假设我的__ DATE __用法在use_date.c中,我想要的是use_date.date.c的时间戳已更新(已更新(强迫重新编译),只有在项目中的任何其他文件(例如main.c)进行了更改,因此它会迫使项目重新编译和链接(显然,如果我只是只有更改use_date.c手动,没有其他文件)。

因此,假设我的项目只是生成可执行文件(没有库):

add_executable(my_project use_date.c other_file.c main.c)

... ...是否可以添加一个cmake步骤,以更新use_date.c的时间戳(从而导致其重新编译),仅导致其重新编译)如果否则,该项目将重新编译和重新链接?

I have generally the same question as in Can CMake always force the compilation/build of a specific file?

I have a C++ file using __DATE__ to display the build date of my app. But if this file is not modified, it will not be rebuilt and the date will not be updated.

Can CMake always rebuild that specific file?

... except I want something slightly different:

In the CMake project I have (for C, transpiles to Makefile which I use), sometimes there are no actual changes to the code when I run make, which is detected nicely, in the sense that there is no recompilation (or relinking) of the program.

Obviously, in this case, I do not want to update the timestamp, and end up with a new executable, which is otherwise identical to the previous one - apart from the build date.

I have seen in the quoted post, that one simply has to ensure a changed timestamp on the file, to force a recompilation. So, assuming my __DATE__ usage is in use_date.c, what I'd want, is that the timestamp of use_date.c is updated (forcing recompilation), only if any other file in the project (say, main.c) has been changed, so it forces project recompilation and linking (obviously, this should also work if I just change use_date.c manually, and no other file).

So, assuming my project just generates an executable (no libraries):

add_executable(my_project use_date.c other_file.c main.c)

... is it possible to add a CMake step, that updates the timestamp of use_date.c (and thus causes its recompilation), only if otherwise the project is getting recompiled and relinked?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走野 2025-02-20 07:28:44

好的,找到了一种方法:这似乎是一种吱吱作响的解决方案 - 希望在CMAKE中更有知识的人最终会发布适当的解决方案。但是与此同时:

我发现add_custom_command带有POST_BUILD运行custic命令仅如果新二进制(.elf <) /code>对我来说)是生成的,否则不是运行自定义命令。

因此,基本上,我们可以在自定义命令中有一个小的bash脚本:

  • 如果调用了此bash脚本,我们可以假设post_build称其为命名,这意味着一个新的.felf(出于其他原因)已建立
  • ,因此我们可以touch use_date.c,而在bash脚本中,请逐渐降为构建目录,并明确跑步再次制作 - 它应该仅重新编译use_date.c,然后与上一个构建中的其余链接。

这本质上是有效的 - 除了它导致无限循环。

这可以通过创建一个临时文件来解决:

  • 了新的.felf(出于其他原因)
  • 如果调用此bash脚本,我们可以假设post_build称其为命名,这意味着已建立 临时文件:
    • 如果它不存在,请创建它 - 然后我们可以touch use_date.c,而在bash script中仍然,下降到构建目录,然后再次显式运行make - 应该...
    • 如果确实存在,那么我们已经从第二个.feld。临时文件,然后退出 - 停止无限循环


这似乎可以正常工作:

add_custom_command(TARGET ${PROJECT_NAME}
                   POST_BUILD
                   DEPENDS ALL
                   #COMMAND bash ARGS -c "touch ${CMAKE_CURRENT_SOURCE_DIR}/use_date.c && cd build && make" ## infinite loop!
                   COMMAND bash ARGS -c "if [ ! -f .refresh ]; then echo '.refresh' | tee .refresh && touch ${CMAKE_CURRENT_SOURCE_DIR}/use_date.c && cd build && make; else rm -v .refresh; fi" # OK!
                   WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                   COMMENT "remake with refreshed __DATE__ in use_date.c (${CMAKE_CURRENT_SOURCE_DIR})"
                   VERBATIM
)

OK, found a way: it seems kind of a squeaky solution - hopefully someone more knowledgeable in CMake will post a proper solution eventually. But in the meantime:

I've found that add_custom_command with POST_BUILD runs the custom command only if a new binary (.elf for me) is generate, and otherwise does not run the custom command.

So, basically, we could have a small bash script in the custom command:

  • If this bash script is called, we can assume that POST_BUILD has called it, meaning that a new .elf (for whatever other reasons) has been built
  • So we can touch the use_date.c, and while in the bash script still, descend into the build directory, and explicitly run make again - which should recompile only use_date.c, and then link with the rest from the previous build.

This essentially works - except, it causes an infinite loop.

This can get solved with creating a temp file:

  • If this bash script is called, we can assume that POST_BUILD has called it, meaning that a new .elf (for whatever other reasons) has been built
  • check for a temp file:
    • if it does not exist, create it - and then we can touch the use_date.c, and while in the bash script still, descend into the build directory, and explicitly run make again - which should ...
    • if it does exist, then we've been called from the second .elf. build (for the touched use_date.c), so we want to remove the temp file, and just exit - stopping the infinite loop

This seems to work fine:

add_custom_command(TARGET ${PROJECT_NAME}
                   POST_BUILD
                   DEPENDS ALL
                   #COMMAND bash ARGS -c "touch ${CMAKE_CURRENT_SOURCE_DIR}/use_date.c && cd build && make" ## infinite loop!
                   COMMAND bash ARGS -c "if [ ! -f .refresh ]; then echo '.refresh' | tee .refresh && touch ${CMAKE_CURRENT_SOURCE_DIR}/use_date.c && cd build && make; else rm -v .refresh; fi" # OK!
                   WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                   COMMENT "remake with refreshed __DATE__ in use_date.c (${CMAKE_CURRENT_SOURCE_DIR})"
                   VERBATIM
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文