title: Call cmake from make to create Makefiles?
body: <p>I am using <code>cmake</code> to build my project.  For UNIX, I would like to type <code>make</code> from my project's root directory, and have <code>cmake</code> invoked to create the proper Makefiles (if they don't exist yet) and then build my project.  I would like the <code>cmake</code> "internal" files (object files, <code>cmake</code> internal Makefiles, etc.) to be hidden (e.g. put in a <code>.build</code> directory) so it doesn't clutter my project directory.</p>
  
  <p>My project has several sub-projects (in particular, a library, a user executable, and a unit test executable).  I would like Makefiles (i.e. I type <code>make</code> and this happens) for each sub-project to execute <code>cmake</code> (as above) and build only that sub-project (with dependencies, so the library would be built from the executables' Makefiles, if needed).  The resulting binary (.so library or executable) should be in the sub-project's directory.</p>
  
  <p>I made a Makefile which does the main project bit somewhat well, though it feels somewhat hackish.  I can't build specific targets using it, because my Makefile simply calls <code>make</code> in <code>cmake</code>'s build directory.</p>
  
  <p>Note that because the library is a sole dependency (and probably doesn't need to be build manually, and because I'm lazy) I omitted it in my Makefile.</p>
  
  <pre><code>BUILD_DIR   :=  .build
  
  .PHONY: all clean project-gui ${BUILD_DIR}/Makefile
  
  all:    project-gui project-test
  
  clean:
      @([ -d ${BUILD_DIR} ] &amp;&amp; make -C ${BUILD_DIR} clean &amp;&amp; rm -r ${BUILD_DIR}) || echo Nothing to clean
  
  project-gui:  ${BUILD_DIR}/Makefile
      @make -C ${BUILD_DIR} project-gui
      @cp ${BUILD_DIR}/project-gui/project-gui $@
  
  project-test:  ${BUILD_DIR}/Makefile
      @make -C ${BUILD_DIR} project-test
      @cp ${BUILD_DIR}/project-test/project-test $@
  
  ${BUILD_DIR}/Makefile:
      @[ -d ${BUILD_DIR} ] || mkdir -p ${BUILD_DIR}
      @[ -f ${BUILD_DIR}/Makefile ] || (cd ${BUILD_DIR} &amp;&amp; cmake ${CMAKE_OPTS} ..)
  </code></pre>
  
  <p>If it helps, here's my project structure (if this is "wrong" please tell me -- I'm still learning <code>cmake</code>):</p>
  
  <pre><code>project/
  project/CMakeLists.txt
  project/common.cmake
  project/Makefile -- see Makefile above for this; should be replaced with something better, building libproject, project-gui, and project-test
  
  project/libproject/
  project/libproject/CMakeLists.txt
  project/libproject/libproject.so -- after build
  project/libproject/Makefile -- doesn't exist yet; should build libproject only
  project/libproject/source/
  project/libproject/include/
  
  project/project-gui/
  project/project-gui/CMakeLists.txt
  project/project-gui/Makefile -- doesn't exist yet; should build libproject then project-gui
  project/project-gui/source/
  project/project-gui/include/
  
  project/project-test/
  project/project-test/CMakeLists.txt
  project/project-test/Makefile -- doesn't exist yet; should build libproject then project-test
  project/project-test/source/
  project/project-test/include/
  </code></pre>
  
  <p>If you haven't caught on yet, I'm basically looking for a way to build the project and sub-projects as if <code>cmake</code> wasn't there: as if my project consisted of only Makefiles.  Can this be done?  Is the solution elegant, or messy?  Should I be trying to do something else instead?</p>
  
  <p>Thanks!</p>