如何在实际构建/链接后让 cmake 创建时间戳文件?(如果可执行文件没有改变,什么都不做) [英] How to get cmake to create timestamp file after an actual build/link? (do nothing if executable hasn't changed)
问题描述
我想调用 date +"%s" >${TIMESTAMP}
对于我生成的三个可执行文件中的每一个,myapp_data
、myapp_live
和 myapp_sim
(即只创建时间戳如果创建了相应的可执行文件).
I would like to invoke date +"%s" > ${TIMESTAMP}
for each of the three executables, myapp_data
, myapp_live
and myapp_sim
that I generate (ie only create the timestamp if the respective executables are created).
我似乎无法弄清楚为什么即使在我删除二进制文件并重新链接之后我的自定义命令也没有被执行.构建工作正常 - 只有时间戳生成不起作用.
I can't seem to figure out why my custom command isn't being executed even after I remove the binaries and relink. Build works fine - only the timestamp generation doesn't work.
MACRO( MY_APP TAG )
SET( BINARY_TGT "myapp_${TAG}" )
SET( TIMESTAMP "TIMESTAMP_${TAG}" )
ADD_EXECUTABLE( ${BINARY_TGT} ${APP_SRCS} )
ADD_CUSTOM_COMMAND(
OUTPUT ${TIMESTAMP}
COMMAND date
ARGS +"%s" > ${TIMESTAMP}
DEPENDS ${BINARY_TGT}
)
ENDMACRO( MY_APP )
SUBDIRS( data )
SUBDIRS( live )
SUBDIRS( sim )
在数据目录中,我有:
FILE(GLOB APP_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} main_data.cpp)
SET( MY_TAG data )
MY_APP( "${MY_TAG}" )
推荐答案
CMake 不运行自立的自定义命令,除非某些东西依赖于它们的输出.一种选择是将自定义命令更改为后期构建:
CMake does not run self-standing custom commands unless something depends on their output. One option is to change the custom command to a post-build:
add_custom_command(
TARGET ${BINARY_TGT}
POST_BUILD
COMMAND date +"%s" > ${TIMESTAMP}
VERBATIM
)
另一个选项是添加自定义目标来驱动自定义命令.对于所有自定义命令,一个目标就足够了.
The other option is to add a custom target to drive the custom command(s). One target is sufficient for all custom commands.
add_custom_target(
GenerateTimestamps ALL
DEPENDS ${yourListOfTimestampFiles}
)
<小时>
但是,我不确定重定向是否会如您所愿.当您在 shell/命令提示符中键入 >
时,它不是程序的参数,而是 shell/命令处理器的指令.如果它不起作用(我从未测试过),则必须将 date
的调用放入脚本中.
However, I am not sure if the redirecting will work as you expect. When you type >
in a shell/command prompt, it's not an argument to the program, but an instruction to the shell/command processor. If it doesn't work (I've never tested), you'll have to put the invocation of date
into a script.
这篇关于如何在实际构建/链接后让 cmake 创建时间戳文件?(如果可执行文件没有改变,什么都不做)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!