Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake conditionals: comparing timestamps of files

Tags:

cmake

I need to write a custom command that runs whenever file A is newer than file B. How do I do this in CMake? Thanks!

like image 398
meteoritepanama Avatar asked Oct 17 '11 16:10

meteoritepanama


1 Answers

Sounds like you want something similar to this:

add_custom_command(OUTPUT B
  COMMAND ${CMAKE_COMMAND} -Dinput=A -P script_that_generates_B.cmake
  DEPENDS A
  )

Where "B" is the full path to the output file, "A" is the full path to some input file, and the command is something that runs at build time to produce B whenever A changes.

In order for the rule producing B to be executed at build time, something else must depend on B also. It should appear either as a DEPENDS of an add_custom_target that is in "all" or as a source file to an add_library or add_executable command to trigger the command to run.

EDIT:

You can also use the

if(file1 IS_NEWER_THAN file2)

construct at CMake configure time, if necessary. The documentation of the IF command is rather lengthy, but searching on this page for IS_NEWER_THAN yields this nugget:

"True if file1 is newer than file2 or if one of the two files doesn't exist. Behavior is well-defined only for full paths."

like image 109
DLRdave Avatar answered Oct 11 '22 02:10

DLRdave