cmake_minimum_required(VERSION 2.8) if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/foo.cpp) message("Generating foo.cpp") file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/foo.cpp "int main() {}") endif() add_custom_command(OUTPUT foo2.cpp COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR}/foo.cpp ${CMAKE_CURRENT_BINARY_DIR}/foo2.cpp DEPENDS foo.cpp) set_source_files_properties(foo2.cpp PROPERTIES GENERATED ON) add_executable(foo foo2.cpp)