title: Installing only one target (and its dependencies) out of a complex project with cmake (open to better solutions)
body: <p>Let's say I have a project made of several subprojects A, B, C, D...
  <strong>All subprojects depends on A</strong>, which changes rather frequently.
  Plus, there might be some further dependencies:
  in this example, <strong>D depends on B</strong>.</p>
  
  <p>Now: many people are working on these projects. The main CMakeLists.txt file should include all directories, so that the build all builds everything. But people would like also to be able to work only on one of these projects, and not having to build/install everything everytime.</p>
  
  <p>If I am working on D, I can easily build "only" D by calling</p>
  
  <pre><code>cmake --build . --target D -- -j7
  </code></pre>
  
  <p>or</p>
  
  <pre><code>ninja -j7 D
  </code></pre>
  
  <p>This will also build A and B if something for them has changed. Perfect.</p>
  
  <p>But how can I call install only for D without triggering build all?
  I would like that if I call:</p>
  
  <pre><code>ninja -j7 D install
  </code></pre>
  
  <p>it only built D (and dependencies) and then installed only D <strong>and its dependencies (A and B).</strong>
  Instead, it builds the target all and install all.</p>
  
  <p>I would like to keep that the target all keep building everything. So EXCLUDE_FROM_ALL wouldn't be an option. But going in that direction I couldn't find any solution.</p>
  
  <p>So I am thinking of the following strategy:</p>
  
  <ul>
  <li>Apart from subproject A, all other targets are set to EXCLUDE_FROM_ALL, and OPTIONAL at installation.</li>
  <li>I add one extra subproject that simply depends from all other sub-projects (maybe I make each target publish its name by using some variable set at PARENT_SCOPE), and people will have to call that when they want to build and install everything.</li>
  </ul>
  
  <p>Is it going to work? Is there any better solution?</p>
  
  <p>We would like to avoid that everybody has to edit the main CMakeLists.txt file to exclude projects he is not interested in. The solution should be portable to different OSs.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I tried the strategy I proposed, but it didn't work: in my case, putting an install statement for a target (even if specified as OPTIONAL) will make ineffective EXCLUDE_FROM_ALL. Reading better in the <a href="http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command%3ainstall">documentation</a> I found out that:</p>
  
  <pre><code>Installing a target with EXCLUDE_FROM_ALL set to true has undefined behavior.
  </code></pre>
  
  <p>I also get this warning:</p>
  
  <pre><code>Target &lt;targetname&gt; has EXCLUDE_FROM_ALL set and will not be built by default but an install rule has been provided for it.  CMake does not define behavior for this case.
  </code></pre>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>I tried putting EXCLUDE_FROM_ALL as an option of add_subdirectory (instead of add_library/add_executable), but then all the install statements in those sub-directory seem to be ignored: only install statements in non excluded-from-all subdirectories will be installed.</p>
  
  <p><strong>Edit 3:</strong></p>
  
  <p>Even if I activate <a href="http://www.cmake.org/Wiki/CMake_Useful_Variables">CMAKE_SKIP_INSTALL_ALL_DEPENDENCY</a>:</p>
  
  <pre><code>set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY true)
  </code></pre>
  
  <p>in the main CMakeLists.txt file, and I omit all EXCLUDE_FROM_ALL, put installation of as many targets as I want optional (in my case, all but A), and if building of specific targets precede installation, yet the command:</p>
  
  <pre><code>ninja -j7 D &amp;&amp; ninja install
  </code></pre>
  
  <p>for some reason will fail, stating that C (whose installation was set to OPTIONAL) does not exist (it was not created because D depended only on A and B)...</p>
  
  <pre><code>file INSTALL cannot find "&lt;name of dll file for C&gt;"
  </code></pre>
  
  <p><strong>Edit 4:</strong></p>
  
  <p>It looks like a cmake bug to me. (I am using 2.8.11 under Windows, also tested 2.8.10)
  This INSTALL command</p>
  
  <pre><code>install(TARGETS ${targetname} RUNTIME DESTINATION . LIBRARY DESTINATION . OPTIONAL)
  </code></pre>
  
  <p>is converted in the cmake_install.cmake as:</p>
  
  <blockquote>
  <pre><code>IF(NOT CMAKE_INSTALL_COMPONENT OR "${CMAKE_INSTALL_COMPONENT}" STREQUAL "Unspecified")   
  </code></pre>
    
    <p>FILE(INSTALL DESTINATION "${CMAKE_INSTALL_PREFIX}/." TYPE SHARED_LIBRARY FILES *path_to_dll*)</p>
    
    <p>IF(EXISTS "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/./" AND NOT IS_SYMLINK "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/./*dll_name*")</p>
  
  <pre><code>IF(CMAKE_INSTALL_DO_STRIP)
  
    EXECUTE_PROCESS(COMMAND "C:/Programs/MinGW/bin/strip.exe" "$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX}/./*dll_name*")
  
  ENDIF(CMAKE_INSTALL_DO_STRIP)   ENDIF() ENDIF(NOT CMAKE_INSTALL_COMPONENT OR "${CMAKE_INSTALL_COMPONENT}" STREQUAL "Unspecified")
  </code></pre>
  </blockquote>
  
  <p>with the command FILE missing OPTIONAL! If I add OPTIONAL manually, it works!
  (note: I have edited here to put *dll_name* and *path_to_dll* placeholders)</p>
  
  <p><strong>Edit 5:</strong></p>
  
  <p>I confirm it's a bug of cmake, or at least wrong documentation. I will report this.
  The situation solved either putting a more simple</p>
  
  <pre><code>install(TARGETS ${targetname} DESTINATION . OPTIONAL)
  </code></pre>
  
  <p>(but this in my case will also install .lib.a files that I don't want)</p>
  
  <p>or moving in front the OPTIONAL flag:</p>
  
  <pre><code>install(TARGETS ${targetname} OPTIONAL RUNTIME DESTINATION . LIBRARY DESTINATION .)
  </code></pre>
  
  <p>What one understands from the <a href="http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command%3ainstall">cmake documentation</a> is that OPTIONAL should be put as last option.</p>