How to get cmake to create timestamp file after an

2019-01-20 17:09发布

问题:

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  )

Inside the data dir, I have:

FILE(GLOB APP_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} main_data.cpp)
SET( MY_TAG data )
MY_APP( "${MY_TAG}" )

回答1:

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}
)

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