title: CMake: Recommended way for a Config-File Package to depend on a Find-Module Package
body: <p>I've just been reading about CMake's Config-File Package "concept" which sounds very promising. What I like very much about it is that if I create a Config-File Package myself I can specify other packages on which it depends. My Question is: How can I create a Config-File package that is "relocatable" and depends on a Find-Module Package (e.g. boost)?</p>
  
  <p>In more detail: Suppose I want to create a package named HyDi. The <a href="http://www.cmake.org/cmake/help/v3.2/manual/cmake-packages.7.html#id15" rel="nofollow">cmake documentation</a> explains then very nicely how I can create the corresponding HydiConfig.cmake and HydiTargets.cmake files automatically. A very simple version of the CMakeLists.txt that does this is:</p>
  
  <pre><code>project(HyDi)
  
  find_package(Boost COMPONENTS program_options)
  add_library(HyDi foo.cpp foo.hpp)
  target_include_directories(HyDi PUBLIC INTERFACE ${Boost_INCLUDE_DIRS})
  target_link_libraries(HyDi ${Boost_LIBRARIES})
  target_compile_options(HyDi INTERFACE PUBLIC "-std=c++11")
  
  
  install(TARGETS HyDi EXPORT HyDiTarget
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    RUNTIME DESTINATION bin
    INCLUDES DESTINATION include
  )
  
  install(FILES foo.hpp DESTINATION include)
  
  configure_file(cmake/HyDiConfig.cmake
    "${CMAKE_CURRENT_BINARY_DIR}/HyDi/HyDiConfig.cmake"
    COPYONLY
  )
  
  set(ConfigPackageLocation lib/cmake/HyDi)
  install(EXPORT HyDiTarget FILE HyDiTargets.cmake 
      NAMESPACE Upstream:: DESTINATION ${ConfigPackageLocation} )
  install(FILES cmake/HyDiConfig.cmake DESTINATION ${ConfigPackageLocation})
  </code></pre>
  
  <p>The corresponding HydiConfig.cmake is:</p>
  
  <pre><code>include(CMakeFindDependencyMacro)
  find_dependency(Boost COMPONENTS program_options)
  
  include("${CMAKE_CURRENT_LIST_DIR}/HyDiTargets.cmake")
  </code></pre>
  
  <p>However if I install this library, the HyDiTargets.cmake file will contain the include path to the Boost Libraries hardcoded and is thus not relocatable. </p>
  
  <p>Note that the <a href="http://www.cmake.org/cmake/help/v3.2/manual/cmake-packages.7.html#id15" rel="nofollow">cmake documentation</a> gives an example of how not to include the boost libraries that is essentially my version. But they unfortunately don't explain how to do it better.</p>
  
  <p>I understand that I could build boost using cmake and could then import boost as a Config-file package so that my HydiTargets.cmake would relocatable. But this approach doesn't work with every other library that provides a Findxxx.cmake file. </p>