title: Install prebuilt static library dependency with the parent library
body: <p>My project structure looks like this:</p>
  <pre><code>Parent/
      CMakeLists.txt
      file.cpp
      third_party/
          dependency/
              CMakeLists.txt
              lib/
                  dependency.lib
  </code></pre>
  <p>Note that dependency.lib is a precompiled/prebuilt library: I don't even have sources for it.</p>
  <p>When I install <code>Parent</code>, I want it to go into a dist folder like this (within the previous tree structure but omitted for clarity):</p>
  <pre><code>Parent/
      dist/
          Parent/
              include/
              bin/
              lib/
                  cmake/
                      ParentTargets.cmake
                  Parent.lib
                  dependency.lib
  </code></pre>
  <p>The idea here is to just get both the generated as well as the dependency libraries all in one place for easier install.</p>
  <p>Relevant parts of Parent's <code>CMakeLists.txt</code> look like this (omitting a lot of stuff like includes, etc.):</p>
  <pre><code>cmake_minimum_required(VERSION 3.22)
  project(Parent VERSION 0.1.0 LANGUAGES C CXX)
  add_library(Parent)
  
  target_sources(Parent PRIVATE file.cpp)
  
  add_subdirectory(&quot;${CMAKE_SOURCE_DIR}/third_party/dependency&quot;)
  target_link_libraries(Parent PRIVATE dependency)
  
  # Manual copy of dependency.lib to install location
  install(DIRECTORY &quot;${CMAKE_SOURCE_DIR}/third_party/dependency/lib/&quot;
      DESTINATION lib
      FILES_MATCHING
      PATTERN &quot;dependency.lib&quot;
  )
  
  # Install all targets to ParentTargets export set.
  # (omitting install dir variables, components, etc. for clarity)
  install(
      TARGETS
          Parent dependency
      EXPORT
          ParentTargets
      LIBRARY
          DESTINATION lib
      ARCHIVE
          DESTINATION lib
  )
  
  # Write ParentTargets export set to targets file directly in install folder
  install(EXPORT ParentTargets
      FILE ParentTargets.cmake
      NAMESPACE Parent::
      DESTINATION lib/cmake
  )
  </code></pre>
  <p>And this is what I have for the dependency's <code>CMakeLists.txt</code>:</p>
  <pre><code>add_library(dependency INTERFACE)
  target_link_libraries(dependency
      INTERFACE
          $&lt;BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/lib/dependency.lib&gt;
          $&lt;INSTALL_INTERFACE:${CMAKE_INSTALL_PREFIX}/lib/dependency.lib&gt;
  )
  </code></pre>
  <p>This almost sort of works, the issue is that if you look at the generated <code>ParentTargets.cmake</code>, this is what CMake generates for the <code>dependency</code> target:</p>
  <pre><code># Create imported target Parent::dependency
  add_library(Parent::dependency INTERFACE IMPORTED)
  
  set_target_properties(Parent::dependency PROPERTIES
    INTERFACE_LINK_LIBRARIES &quot;E:/absolute/path/Parent/dist/Parent/lib/dependency.lib&quot;
  )
  </code></pre>
  <p>...its a hard-coded absolute path to my installation directory. This is not great, and usually for built libraries CMake would have generated a path like <code>${_IMPORT_PREFIX}/lib/dependency.lib</code>, which is what I wanted and important to have the install relocatable.</p>
  <p>Here are my questions:</p>
  <ol>
  <li>Is this the right way of doing this? I could never find a good reference on how to package dependency static libraries alongside your project, and CMake doesn't seem to do this &quot;on its own&quot; too eagerly (I even had to manually copy the .lib there as you can see);</li>
  <li>How do I get CMake to use <code>${_IMPORT_PREFIX}</code> there? I can't just use that on my CMakeLists.txt as it would evaluate to the empty string during generation, and I can't escape it like <code>\${_IMPORT_PREFIX}</code> as it still places the escaped version in ParentTargets.cmake...</li>
  </ol>