title: CMAKE: Force that if a files exists then it is up-to-date (at build)
body: <p>I am using cmake to build a paper, which has an involved build process.</p>
  <ol>
  <li>Build an exe</li>
  <li>run the exe to generate some data (in ${PROJECT_BINARY_DIR}/data)</li>
  <li>run some python to process the data into figures (Postscript images). These are saved in ${CMAKE_SOURCE_DIR}/fig.</li>
  <li>convert images to png</li>
  <li>build a latex document with those figures</li>
  </ol>
  <p>This can all be done in cmake by chaining the outputs of custom_commands to custom targets:</p>
  <pre><code>#This is the command that does the work (run python, or some executable etc.).  Will only run if ${CUSTOM_DEPENDS} is newer that ${CUSTOM_OUTPUT}
  add_custom_command( OUTPUT ${CUSTOM_OUTPUT}
          COMMAND ${CUSTOM_COMMAND}
          DEPENDS ${CUSTOM_DEPENDS}
          WORKING_DIRECTORY ${CUSTOM_WORKING_DIRECTORY}
          USES_TERMINAL
          VERBATIM
          )
  
  #This target allows one to depend on the custom command from another file
  add_custom_target(${name} 
      DEPENDS ${CUSTOM_OUTPUT}
      VERBATIM
      )
  </code></pre>
  <p>The issue is that when I am actually working on this, I generate the figures on one machine, save the Postscript images files to the git repository, and then work on the actual latex document on a laptop.  The laptop can't handle running the executable, and honestly I would like to have some fairly tight controls in general about if and when a figure is regenerated.  <em>Ideally the Postscript files would only build if they are missing.</em>  It would also be nice if the Postscript files rebuilt if the dependencies (e.g. the executable source files) changed since first config, but that may be asking too much.</p>
  <p>This is easy enough to do in config time, but doesn't really the case where a file is deleted post-config.</p>
  <p>I've tried something like this (where the output is the Postscript files):</p>
  <pre><code>
  #same custom command as before, but we also generate a copy.  This copy should always be newer that the output
  add_custom_command( OUTPUT ${CUSTOM_OUTPUT}
          COMMAND ${CUSTOM_COMMAND}
          COMMAND ${CMAKE_COMMAND} -E copy ${CUSTOM_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/copy/
          DEPENDS ${CUSTOM_DEPENDS}
          WORKING_DIRECTORY ${CUSTOM_WORKING_DIRECTORY} #usually this is ${CMAKE_CURRENT_LIST_DIR}
          ${USES_TERMINAL}
          VERBATIM
          )
  
  
  #for every output make a command that makes the copy depends on the original output.  So that they are always up to date if they exist.
  foreach(arg IN ITEMS ${CUSTOM_OUTPUT})
  
      if(EXISTS ${COPY_OBJ})
           file(COPY ${arg} DESTINATION &quot;${CMAKE_CURRENT_BINARY_DIR}/copy/&quot;)
      endif()
      list(APPEND CUSTOM_TARGET_DEPENDS &quot;${CMAKE_CURRENT_BINARY_DIR}/copy/${arg}&quot;)
  
  
      add_custom_command( OUTPUT  ${CMAKE_CURRENT_BINARY_DIR}/copy/${arg}
           DEPENDS &quot;${arg}&quot;
      )
  
              
  endforeach()
  
  #now have the target depend on the copies which are always up to date with the original outputs
  add_custom_target(${name}
      ${CUSTOM_ALL}   
      DEPENDS ${CUSTOM_TARGET_DEPENDS}
      VERBATIM
      )
  </code></pre>
  <p>This doesn't work, and I'm not sure if this is because these operations are occurring in a source folder, or if I'm misunderstanding of add_custom_command up-to-date logic.</p>
  <p>Note: This is different from <a href="https://stackoverflow.com/questions/18785168/cmake-check-if-file-exists-at-build-time-rather-than-during-cmake-configuration">This Question</a> where they only test for existence.  I'm trying to change up-to-dateness with existence.</p>