From 5f440c1a15e92e27622fb2eaece4c9c6169ecb3c Mon Sep 17 00:00:00 2001
From: "A. Maitland Bottoms" <bottoms@debian.org>
Date: Thu, 8 Nov 2018 12:06:51 -0500
Subject: [PATCH 3/4] modern cmake targets

Use CMake install(EXPORT ... to export UHD targets in Modern CMake usage,
so that find_package(UHD) does the right things.
---
 host/CMakeLists.txt                   | 10 +++++++++-
 host/cmake/Modules/UHDConfig.cmake.in |  5 ++++-
 host/lib/CMakeLists.txt               |  2 ++
 3 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/host/CMakeLists.txt b/host/CMakeLists.txt
index 48d8eea3b..da0b0e129 100644
--- a/host/CMakeLists.txt
+++ b/host/CMakeLists.txt
@@ -555,9 +555,17 @@ SET(cmake_configs
 
 UHD_INSTALL(
     FILES ${cmake_configs}
-    DESTINATION ${CMAKE_MODULES_DIR}/uhd
+    DESTINATION ${CMAKE_INSTALL_PREFIX}/${LIBRARY_DIR}/cmake/uhd
     COMPONENT "devel"
 )
+install(EXPORT UHD-export
+  FILE
+  UHDTargets.cmake
+  NAMESPACE
+  UHD::
+  DESTINATION
+  ${CMAKE_INSTALL_PREFIX}/${LIBRARY_DIR}/cmake/uhd
+  )
 
 ########################################################################
 # Handle pre-built UHD Images for packaging
diff --git a/host/cmake/Modules/UHDConfig.cmake.in b/host/cmake/Modules/UHDConfig.cmake.in
index 4651e3a9c..feeb1d27c 100644
--- a/host/cmake/Modules/UHDConfig.cmake.in
+++ b/host/cmake/Modules/UHDConfig.cmake.in
@@ -36,7 +36,6 @@ IF(UHD_DIR)
     LIST(APPEND UHD_LIBDIR_HINTS ${UHD_DIR}/lib)
 ENDIF()
 
-INCLUDE(FindPkgConfig)
 IF(PKG_CONFIG_FOUND)
   IF(NOT ${CMAKE_VERSION} VERSION_LESS "2.8.0")
     SET(UHD_QUIET "QUIET")
@@ -107,3 +106,7 @@ ELSEIF(UHD_FIND_REQUIRED)
   MESSAGE(FATAL_ERROR "UHD is required, but was not found.")
 
 ENDIF()
+
+if(NOT TARGET UHD::uhd)
+  include("${UHD_CMAKE_DIR}/UHDTargets.cmake")
+endif()
diff --git a/host/lib/CMakeLists.txt b/host/lib/CMakeLists.txt
index 3b243ab8b..2202884bb 100644
--- a/host/lib/CMakeLists.txt
+++ b/host/lib/CMakeLists.txt
@@ -186,6 +186,7 @@ ENDIF(DEFINED LIBUHD_OUTPUT_NAME)
 
 IF(NOT UHDHOST_PKG) #Syntax makes it unusable by UHD_INSTALL
     INSTALL(TARGETS uhd
+        EXPORT UHD-export
         LIBRARY DESTINATION ${LIBRARY_DIR} COMPONENT libraries # .so file
         ARCHIVE DESTINATION ${LIBRARY_DIR} COMPONENT libraries # .lib file
         RUNTIME DESTINATION ${RUNTIME_DIR} COMPONENT libraries # .dll file
@@ -200,6 +201,7 @@ IF(ENABLE_STATIC_LIBS)
     SET_TARGET_PROPERTIES(uhd_static PROPERTIES OUTPUT_NAME uhd)
     SET_TARGET_PROPERTIES(uhd_static PROPERTIES COMPILE_DEFINITIONS UHD_STATIC_LIB)
     INSTALL(TARGETS uhd_static
+        EXPORT UHD-export
         ARCHIVE DESTINATION lib${LIB_SUFFIX} # .lib or .a file
     )
 ENDIF(ENABLE_STATIC_LIBS)
-- 
2.11.0

