4

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?

Apparently it's possible with makefile : How do you force a makefile to rebuild a target

Edit: Duplicate of CMake - always build specific file

ymoreau
  • 3,402
  • 1
  • 22
  • 60

1 Answers1

4

The solution you trying to apply is a bit against CMake principles since it might lead to the rebuilding of all dependant targets.

However, you can achieve this with an approach like this

add_custom_command(TARGET ${PROJECT_NAME}
        PRE_BUILD
        COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_CURRENT_SOURCE_DIR}/path/file.c) 

Update with the 2nd approach:

add_custom_target(file_toucher 
        COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_CURRENT_SOURCE_DIR}/path/file.c)

add_dependencies(${PROJECT_NAME} file_toucher)
triclosan
  • 5,578
  • 6
  • 26
  • 50
  • The command is called after the build for anything else than VisualStudio (I'm using both VS and emscripten), so it does fully solve my problem. Also, VS does not call my commands at all but at CMake step, not build step. – ymoreau Apr 14 '20 at 14:47
  • @ymoreau it worked for me on macOS. Anyway I'll update my answer with a more portable solution – triclosan Apr 14 '20 at 15:50