How to rebuild project after SWIG files changed?
<p>Given the below makefile:</p>

<pre><code>TARGET = _example.pyd
OFILES = example.obj example_wrap.obj
HFILES =

CC = cl
CXX = cl
LINK = link
CPPFLAGS = -DNDEBUG -DUNICODE -DWIN32 -I. -Id:\virtual_envs\py351\include
CFLAGS = -nologo -Zm200 -Zc:wchar_t- -FS -Zc:strictStrings -O2 -MD -W3 -w44456 -w44457 -w44458
CXXFLAGS = -nologo -Zm200 -Zc:wchar_t- -FS -Zc:strictStrings -D_HAS_EXCEPTIONS=0 -O2 -MD -W3 -w34100 -w34189 -w44996 -w44456 -w44457 -w44458 -wd4577
LFLAGS = /LIBPATH:. /NOLOGO /DYNAMICBASE /NXCOMPAT /DLL /MANIFEST /MANIFESTFILE:$(TARGET).manifest /SUBSYSTEM:WINDOWS /INCREMENTAL:NO
LIBS = /LIBPATH:d:\virtual_envs\py351\libs python35.lib
.SUFFIXES: .c .cpp .cc .cxx .C


{.}.cpp{}.obj::
    $(CXX) -c $(CXXFLAGS) $(CPPFLAGS) -Fo @&lt;&lt;
    $&lt;
&lt;&lt;

{.}.cc{}.obj::
    $(CXX) -c $(CXXFLAGS) $(CPPFLAGS) -Fo @&lt;&lt;
    $&lt;
&lt;&lt;

{.}.cxx{}.obj::
    $(CXX) -c $(CXXFLAGS) $(CPPFLAGS) -Fo @&lt;&lt;
    $&lt;
&lt;&lt;

{.}.C{}.obj::
    $(CXX) -c $(CXXFLAGS) $(CPPFLAGS) -Fo @&lt;&lt;
    $&lt;
&lt;&lt;

{.}.c{}.obj::
    $(CC) -c $(CFLAGS) $(CPPFLAGS) -Fo @&lt;&lt;
    $&lt;
&lt;&lt;

all: $(TARGET)

$(OFILES): $(HFILES)

$(TARGET): $(OFILES)
    $(LINK) $(LFLAGS) /OUT:$(TARGET) @&lt;&lt;
      $(OFILES) $(LIBS)
&lt;&lt;
    mt -nologo -manifest $(TARGET).manifest -outputresource:$(TARGET);2

install: $(TARGET)
    @if not exist d:\virtual_envs\py351\Lib\site-packages mkdir d:\virtual_envs\py351\Lib\site-packages
    copy /y $(TARGET) d:\virtual_envs\py351\Lib\site-packages\$(TARGET)

clean:
    -del $(TARGET)
    -del *.obj
    -del *.exp
    -del *.lib
    -del $(TARGET).manifest

test:
    python runme.py
</code></pre>

<p>I'd like to improve a couple of things here:</p>

<ul>
<li>I'd like to consider swig files (*.i) in the makefile. For example, every time some swig file has been changed a new wrap file should be generated (ie: swig -python -c++ file_has_changed.cpp) and then rebuild the project</li>
<li>I'd like to avoid having hardcoded object files. For instance, I'd like to use all cpp files using wildcards somehow</li>
</ul>

<p>I've read a little bit of the docs talking about <a href="https://msdn.microsoft.com/en-us/library/yz1tske6.aspx" rel="nofollow">Makefiles</a> but I'm still pretty much confused. How could I achieve this?</p>

<p>Right now I'm using a <strong>hacky</strong> solution like <code>swig -python -c++ whatever_file.i &amp;&amp; nmake</code>, that's of course it's not ideal at all</p>

<p>REFERENCES</p>

<p>Achieving this inside visual studio IDE is quite easy following <a href="http://stackoverflow.com/questions/5969173/how-to-swig-in-vs2010/6117641#6117641">these steps</a> but I'd like to use this makefile inside SublimeText, that's why I'm quite interested on knowing how to have a proper Makefile</p>

<p>Producing any kind of target from any kind of source, that's the essence of a makefile:</p>

<pre><code>.i.cpp:
    swig -python -c++ $&lt;
</code></pre>

<p>This elegance will, however, break with <code>nmake</code> (<a href="https://www.gnu.org/software/make/manual/html_node/Chained-Rules.html" rel="nofollow">as opposed to GNU <code>make</code></a>) if the <code>.cpp</code> file is missing because <a href="http://stackoverflow.com/questions/4808674/nmake-inference-rules-limited-to-depth-of-1"><code>nmake</code> doesn't try to chain inference rules through a missing link</a>.<br>
Moreover, it will break silently and "build" from stale versions of the files that are later in the build chain (which includes the resulting executable) if they are present.</p>

<p>Possible <s>kludges</s> workarounds here (save for ditching <code>nmake</code>, of course) are:</p>

<ul>
<li><p>invoke <code>nmake</code> multiple times, first, to generate all files that are an intermediate steps between two <a href="https://msdn.microsoft.com/en-us/library/968fkazs.aspx" rel="nofollow">inference rules</a> (which can in turn require multiple invocations if they are generated from one another), and then for the final targets</p>

<ul>
<li><p>This requires an external script which can very well be another makefile. E.g.:
move the current <code>Makefile</code> to <code>main_makefile</code> and create a new <code>Makefile</code> with commands for the main target like this:</p>

<pre><code>python -c "import os,os.path,subprocess;
           subprocess.check_call(['nmake', '/F', 'main_makefile']
               +[os.path.splitext(f)[0]+'.cpp'
                 for f in os.listdir('.') if os.path.isfile(f)
                                             and f.endswith('.i')])"
nmake /F main_makefile
</code></pre></li>
</ul></li>
<li><p>do not rely solely on inference rules but have an explicit rule for each <code>.cpp</code> to be produced (that's what CMake does btw)</p>

<ul>
<li><p>this asks for the relevant part of Makefile to be autogenerated. That part can be <code>!INCLUDE</code>'d, but still, external code is needed to do the generation before <code>nmake</code> gets to work on the result. Example code (again, in Python):</p>

<pre class="lang-python prettyprint-override"><code>import os,os.path,subprocess
for f in os.listdir('.') if os.path.isfile(f) and f.endswith('.i'):
    print '"%s": "%s"'%(os.path.splitext(f)[0]+'.cxx',f)
    #quotes are to allow for special characters,
    # see https://msdn.microsoft.com/en-us/library/956d3677.aspx
    #command is not needed, it will be added from the inferred rule I gave
    # in the beginning, see http://www.darkblue.ch/programming/Namke.pdf, p.41 (567)
</code></pre></li>
</ul></li>
</ul>

python c++ makefile visual-studio-2015 swig