title: Build external library only once with CMake
body: <p>My C++ projects includes the source code of a third-party library (currently as a git submodule).</p>
  
  <p>This library is added to the project by our main CMakelists through the use of <code>add_subdirectory</code>, and then the library is linked with the main target.</p>
  
  <p>Here is a reduced version of my current Cmake file :</p>
  
  <pre><code>add_subdirectory(foo)
  set(FOO_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/libfoo/libfoo.so)
  
  add_executable(target main.cpp)
  add_dependencies(target foo)
  target_link_libraries(target ${FOO_LIBRARY})
  </code></pre>
  
  <p>This library takes a long time to build and, since I don't change its code I need it built only once (per build configuration). But when I clean and rebuild my code it also cleans the library files and recompile them.</p>
  
  <p>I have tried to set the property <a href="https://cmake.org/cmake/help/v3.2/prop_dir/CLEAN_NO_CUSTOM.html" rel="noreferrer"><code>CLEAN_NO_CUSTOM</code></a> in the library's directory, but according to the documentation it only works for custom command targets.</p>
  
  <p>Is there a mechanism in CMake through which it is possible to specify that this library target needs to be generated only once, or alternatively not cleaned by <code>make clean</code> ?</p>