title: Dynamically generated source file list for cmake
body: <p>We have a code generator that generates communication protocol definitions into multiple c++ files and want to switch our build system to use cmake.
  I have made a sample project to demonstrate the problem we're facing.</p>
  
  <p>The code generator takes an input file and may generate hundreds of
  output files so enumerating them in <code>CMakeLists.txt</code> is not an option especially since the protocol changes during development. We neither want to use wildcards for this.</p>
  
  <p>We did not find a way to make cmake regenerate the makefiles if one of it's input file changes, according to the documentation it is our belief that <code>configure_file</code> should be able to do that but it apparently fails at it's job.</p>
  
  <p>We want the makefile for the main project (in <code>main</code> folder in example) to be regenerated by cmake whenever either the generator (in <code>gen</code> folder in example) code changes or the protocol definition file (in our example this is <code>in.txt</code>) is updated.</p>
  
  <p>I have the following file structure:</p>
  
  <pre><code>src
  ├── CMakeLists.txt
  ├── gen
  │   ├── CMakeLists.txt
  │   └── gen.cpp
  ├── in.txt
  └── main
      ├── CMakeLists.txt
      └── main.cpp
  </code></pre>
  
  <p>with the following contents:
  src/CMakeLists.txt:</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.5.1)
  project(cmake-config)
  
  add_subdirectory(main)
  add_subdirectory(gen)
  
  add_dependencies(main gen run_gen)
  </code></pre>
  
  <p>gen/CMakeLists.txt:</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.5.1)
  project(gen)
  
  add_executable(gen gen.cpp)
  
  add_custom_target(
      run_gen
      COMMAND ./gen ${CMAKE_SOURCE_DIR}/in.txt
      COMMENT running gen
  )
  
  add_dependencies(run_gen gen)
  </code></pre>
  
  <p>gen/gen.cpp:</p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  int main(int argc, char *argv[]){
      ifstream inf(argv[1]);
      ofstream outf("input.txt");
      string word;
      while(inf &gt;&gt; word)
          outf &lt;&lt; "set(SOURCES ${SOURCES} " &lt;&lt; word &lt;&lt; ")\n";
  
  }
  </code></pre>
  
  <p>main/CMakeLists.txt:</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.5.1)
  project(main)
  
  if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/../gen/input.txt)
      file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/../gen/input.txt "") 
  endif()
  
  configure_file(${CMAKE_CURRENT_BINARY_DIR}/../gen/input.txt ${CMAKE_CURRENT_BINARY_DIR}/input.txt COPYONLY)
  
  set(SOURCES main.cpp)
  
  include(${CMAKE_CURRENT_BINARY_DIR}/input.txt)
  
  message(${SOURCES})
  
  add_executable(main ${SOURCES})
  </code></pre>
  
  <p>main/main.cpp:</p>
  
  <pre><code>int main(){}
  </code></pre>
  
  <p><code>gen</code> generates <code>input.txt</code> using <code>src/in.txt</code>. <code>input.txt</code> contains a list of source files which I want to use to build <code>main</code>:</p>
  
  <pre><code>set(SOURCES ${SOURCES} a.cpp)
  set(SOURCES ${SOURCES} b.cpp)
  set(SOURCES ${SOURCES} c.cpp)
  </code></pre>
  
  <p>We are looking for a solution which does not require bootstrapping <code>cmake</code>  or needing to rerun it every time the protocol changes during development. This is easily achievable but undesirable as it leads to problematic code which would end up using older protocol definitions if someone from the team does not realize the protocol file (or generator) has changed.</p>