title: Is there a way for CMake to utilize dependencies generated by `swig -MM`?
body: <p>SWIG generates wrapper code from your C/C++ in a desired target language (Python, Java, C#, etc) using an interface (.i) file that specifies the input code to be wrapped as described in the <a href="http://swig.org/tutorial.html">SWIG tutorial</a>. CMake can be used to call swig in order to generate target code from the .i interface, as described in the <a href="http://www.swig.org/Doc3.0/SWIGDocumentation.html#Introduction_build_system">SWIG documentation</a>.</p>
  
  <p>However, using this method CMake only generates a dependency for the interface file itself, but not for the source files it includes. One can <a href="http://public.kitware.com/pipermail/cmake/2012-December/053045.html">manually add dependencies</a>, but SWIG can generate dependencies automatically with the -MM option, and I would like for these to be utilized by CMake.</p>
  
  <p>There was a <a href="http://public.kitware.com/Bug/view.php?id=4147">commit to CMake</a> that utilized dependencies generated by <code>swig -MM</code> but <a href="http://public.kitware.com/Bug/view.php?id=12307">it was later reverted</a> due to a problem with generated sources that didn't exist at the time of the call to swig. At this time the problem seems to remain unsolved.</p>
  
  <p>So I put the issue to the brilliant StackOverflow community: <strong><em>Is there a way with the current CMake to utilize dependencies generated by <code>swig -MM</code></em></strong> when the interface file (a) does not include generated code (e.g. config.h), and (b) includes generated code?</p>
  
  <p>Here is a small example that can be used for experimentation (<a href="https://www.dropbox.com/s/ii7zh6pk02u6ona/swig_example.zip?dl=0">download it here</a>). </p>
  
  <pre><code>// swig_example.h
  int foo(int n);
  //*** comment this declaration after compiling once to witness dependency failure ***/
  int another_function();
  </code></pre>
  
  
  
  <pre><code>// swig_example.cpp
  #include "swig_example.h"
  int another_function() {return -1;}
  int foo(int n) 
  {
      if (n &lt;= 1) return 1;
      else return another_function();
  }
  </code></pre>
  
  
  
  <pre><code>// swig_example: example.i
  %module example
  %{
  #include "swig_example.h"
  %}
  %include "swig_example.h"
  </code></pre>
  
  
  
  <pre><code># swig_example: CMakeLists.txt
  FIND_PACKAGE(SWIG REQUIRED)
  INCLUDE(${SWIG_USE_FILE})
  FIND_PACKAGE(PythonLibs)
  INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
  INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
  SET(CMAKE_SWIG_FLAGS "")
  
  SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES CPLUSPLUS ON)
  
  #add manual dependencies (must be called before SWIG_ADD_MODULE)
  #SET(SWIG_MODULE_example_EXTRA_DEPS ${CMAKE_SOURCE_DIR}/swig_example.h)
  
  SWIG_ADD_MODULE(example python example.i swig_example.cpp)
  SWIG_LINK_LIBRARIES(example ${PYTHON_LIBRARIES})
  </code></pre>
  
  <p>Compile it once, then comment the declaration of <code>another_function</code> and try to compile again. Because the swig interface is not regenerated an error occurs trying to compile examplePYTHON_wrap.cxx.</p>
  
  <pre><code>examplePYTHON_wrap.cxx:3220:17: error: use of undeclared identifier 'another_function'
    result = (int)another_function();
  </code></pre>
  
  <p>Uncomment the <em>add manual dependencies</em> line of the CMakeLists.txt and the interface will be properly regenerated. However, I want this to work using dependencies generated from <code>swig -MM</code> instead of needing to manually specify dependencies.</p>
  
  <pre class="lang-none prettyprint-override"><code>$ swig -python -MM -c++ ../example.i
  ../example_wrap.cxx: \
    ../example.i \
    ../swig_example.h \
  </code></pre>