title: How to use find_package to locate project targets in the same build-tree?
body: <p>If one has a workspace with multiple CMake packages (i.e. a project with its own <code>CMakeLists.txt</code>), and a top-level <code>CMakeLists.txt</code> building each package in a common build directory through <code>add_subdirectory</code>, is it possible to use <code>find_package</code> from within a package to specify build dependency to another package?</p>
  <p>The packages are properly configured to be located by <code>find_package</code> (the targets are exported), but generated on their own subdirectory in the build location (e.g. <code>build/pkg1/kg1Config.cmake</code>) and thus CMake complains that <code>pkg1Config.cmake</code> file can't be found.</p>
  <p>EDIT:</p>
  <p>Currently I'm checking manually if <code>pkg1</code> is in the build tree or is available externally:</p>
  <pre><code>if (TARGET pkg1)
      # if in the build tree / being built
      if (DEFINED pkg1_SOURCE_DIR)
          set(pkg1_INCLUDE_DIR ${pkg1_SOURCE_DIR}/include)
          message(STATUS &quot;pkg1_INCLUDE_DIR: ${pkg1_INCLUDE_DIR}&quot;)
      else()
          message(FATAL_ERROR &quot;pkg1 was NOT FOUND! can't build without it&quot;)
      endif()
  else ()
      # search if installed on the system
      find_package(pkg1)
      if (pkg1_FOUND AND DEFINED pkg1_INCLUDE_DIR)
          message(STATUS &quot;pkg1_INCLUDE_DIR: ${pkg1_INCLUDE_DIR}&quot;)
      else()
          message(FATAL_ERROR &quot;pkg1 was NOT FOUND! can't build without it&quot;)
      endif()
  endif()
  </code></pre>