Merge pull request #2283 from BioDataAnalysis/bda_cmake_improvements

Diverse cmake improvements in dependency handling
diff --git a/build/cmake/ThriftConfig.cmake.in b/build/cmake/ThriftConfig.cmake.in
index 6bfb12c..f82c0a5 100644
--- a/build/cmake/ThriftConfig.cmake.in
+++ b/build/cmake/ThriftConfig.cmake.in
@@ -27,17 +27,37 @@
 set(THRIFT_COMPILER "${THRIFT_BIN_DIR}/thrift@CMAKE_EXECUTABLE_SUFFIX@")
 
 if (NOT TARGET thrift::thrift)
-  include("${THRIFT_CMAKE_DIR}/thriftTargets.cmake")
+    include("${THRIFT_CMAKE_DIR}/thriftTargets.cmake")
 endif()
-
 set(THRIFT_LIBRARIES thrift::thrift)
 
-if ("${THRIFT_LIBRARIES}" STREQUAL "")
-	message(FATAL_ERROR "thrift libraries were not found")
+if(@ZLIB_FOUND@ AND @WITH_ZLIB@)
+    if (NOT TARGET thriftz::thriftz)
+        include("${THRIFT_CMAKE_DIR}/thriftzTargets.cmake")
+    endif()
+    set(THRIFT_LIBRARIES thriftz::thriftz)
 endif()
 
+if ("${THRIFT_LIBRARIES}" STREQUAL "")
+    message(FATAL_ERROR "thrift libraries were not found")
+endif()
 if (NOT Thrift_FIND_QUIETLY)
-  message(STATUS "Found thrift: ${PACKAGE_PREFIX_DIR}")
+    message(STATUS "Found thrift: ${PACKAGE_PREFIX_DIR}")
+endif()
+
+
+include(CMakeFindDependencyMacro)
+
+if(@ZLIB_FOUND@ AND @WITH_ZLIB@)
+    find_dependency(ZLIB)
+endif()
+
+if(@OPENSSL_FOUND@ AND @WITH_OPENSSL@)
+    find_dependency(OpenSSL)
+endif()
+
+if(@Libevent_FOUND@ AND @WITH_LIBEVENT@)
+    find_dependency(Libevent)
 endif()
 
 check_required_components(Thrift)
diff --git a/build/cmake/ThriftMacros.cmake b/build/cmake/ThriftMacros.cmake
index 038651e..392b96b 100644
--- a/build/cmake/ThriftMacros.cmake
+++ b/build/cmake/ThriftMacros.cmake
@@ -54,8 +54,8 @@
     target_link_libraries(${name} ${ARGN})
 endmacro()
 
-macro(LINK_AGAINST_THRIFT_LIBRARY target libname)
-    target_link_libraries(${target} ${libname})
+macro(LINK_AGAINST_THRIFT_LIBRARY target)
+    target_link_libraries(${target} ${ARGN})
 endmacro()
 
 macro(TARGET_LINK_LIBRARIES_THRIFT_AGAINST_THRIFT_LIBRARY target libname)
diff --git a/lib/c_glib/CMakeLists.txt b/lib/c_glib/CMakeLists.txt
index 3a1f188..5f0b6f5 100644
--- a/lib/c_glib/CMakeLists.txt
+++ b/lib/c_glib/CMakeLists.txt
@@ -73,7 +73,7 @@
 include(ThriftMacros)
 
 ADD_LIBRARY_THRIFT(thrift_c_glib ${thrift_c_glib_SOURCES})
-TARGET_LINK_LIBRARIES_THRIFT(thrift_c_glib ${SYSLIBS})
+TARGET_LINK_LIBRARIES_THRIFT(thrift_c_glib PUBLIC ${SYSLIBS})
 
 # Install the headers
 install(DIRECTORY "src/thrift" DESTINATION "${INCLUDE_INSTALL_DIR}"
diff --git a/lib/cpp/CMakeLists.txt b/lib/cpp/CMakeLists.txt
index b3a34d0..7a656c0 100755
--- a/lib/cpp/CMakeLists.txt
+++ b/lib/cpp/CMakeLists.txt
@@ -153,9 +153,9 @@
 
 ADD_LIBRARY_THRIFT(thrift ${thriftcpp_SOURCES} ${thriftcpp_threads_SOURCES})
 if(WIN32)
-    TARGET_LINK_LIBRARIES_THRIFT(thrift ${SYSLIBS} ws2_32)
+    TARGET_LINK_LIBRARIES_THRIFT(thrift PUBLIC ${SYSLIBS} ws2_32)
 else()
-    TARGET_LINK_LIBRARIES_THRIFT(thrift ${SYSLIBS})
+    TARGET_LINK_LIBRARIES_THRIFT(thrift PUBLIC ${SYSLIBS})
 endif()
 ADD_PKGCONFIG_THRIFT(thrift)
 
@@ -164,8 +164,13 @@
     include_directories(SYSTEM ${LIBEVENT_INCLUDE_DIRS})
 
     ADD_LIBRARY_THRIFT(thriftnb ${thriftcppnb_SOURCES})
-    LINK_AGAINST_THRIFT_LIBRARY(thriftnb thrift)
-    TARGET_LINK_LIBRARIES_THRIFT(thriftnb ${SYSLIBS} ${LIBEVENT_LIBRARIES})
+    LINK_AGAINST_THRIFT_LIBRARY(thriftnb PUBLIC thrift)
+    if(TARGET libevent::core AND TARGET libevent::extra)
+        # libevent was found via its cmake config, use modern style targets
+        TARGET_LINK_LIBRARIES_THRIFT(thriftnb PUBLIC libevent::core libevent::extra)
+    else()
+        TARGET_LINK_LIBRARIES_THRIFT(thriftnb PUBLIC ${LIBEVENT_LIBRARIES})
+    endif()
     ADD_PKGCONFIG_THRIFT(thrift-nb)
 endif()
 
@@ -174,8 +179,8 @@
     include_directories(SYSTEM ${ZLIB_INCLUDE_DIRS})
 
     ADD_LIBRARY_THRIFT(thriftz ${thriftcppz_SOURCES})
-    TARGET_LINK_LIBRARIES_THRIFT(thriftz ${SYSLIBS} ${ZLIB_LIBRARIES})
-    TARGET_LINK_LIBRARIES_THRIFT_AGAINST_THRIFT_LIBRARY(thriftz thrift)
+    LINK_AGAINST_THRIFT_LIBRARY(thriftz PUBLIC thrift)
+    TARGET_LINK_LIBRARIES_THRIFT(thriftz PUBLIC ${ZLIB_LIBRARIES})
     ADD_PKGCONFIG_THRIFT(thrift-z)
 endif()
 
diff --git a/lib/cpp/src/thrift/qt/CMakeLists.txt b/lib/cpp/src/thrift/qt/CMakeLists.txt
index 04a9a31..fc1a2c2 100644
--- a/lib/cpp/src/thrift/qt/CMakeLists.txt
+++ b/lib/cpp/src/thrift/qt/CMakeLists.txt
@@ -24,5 +24,5 @@
 set(CMAKE_AUTOMOC ON)
 find_package(Qt5 REQUIRED COMPONENTS Core Network)
 ADD_LIBRARY_THRIFT(thriftqt5 ${thriftcppqt5_SOURCES})
-TARGET_LINK_LIBRARIES_THRIFT(thriftqt5 Qt5::Core Qt5::Network)
-TARGET_LINK_LIBRARIES_THRIFT_AGAINST_THRIFT_LIBRARY(thriftqt5 thrift)
+LINK_AGAINST_THRIFT_LIBRARY(thriftqt5 PUBLIC thrift)
+TARGET_LINK_LIBRARIES_THRIFT(thriftqt5 PUBLIC Qt5::Core Qt5::Network)
diff --git a/lib/cpp/test/CMakeLists.txt b/lib/cpp/test/CMakeLists.txt
index ced78a2..3263b66 100644
--- a/lib/cpp/test/CMakeLists.txt
+++ b/lib/cpp/test/CMakeLists.txt
@@ -283,7 +283,6 @@
     testgencpp_cob
     ${Boost_LIBRARIES}
 )
-LINK_AGAINST_THRIFT_LIBRARY(processor_test thrift)
 LINK_AGAINST_THRIFT_LIBRARY(processor_test thriftnb)
 add_test(NAME processor_test COMMAND processor_test)
 
@@ -292,10 +291,8 @@
 include_directories(${LIBEVENT_INCLUDE_DIRS})
 target_link_libraries(TNonblockingServerTest
     testgencpp_cob
-    ${LIBEVENT_LIBRARIES}
     ${Boost_LIBRARIES}
 )
-LINK_AGAINST_THRIFT_LIBRARY(TNonblockingServerTest thrift)
 LINK_AGAINST_THRIFT_LIBRARY(TNonblockingServerTest thriftnb)
 add_test(NAME TNonblockingServerTest COMMAND TNonblockingServerTest)
 
@@ -305,10 +302,8 @@
   include_directories(${LIBEVENT_INCLUDE_DIRS})
   target_link_libraries(TNonblockingSSLServerTest
     testgencpp_cob
-    ${LIBEVENT_LIBRARIES}
     ${Boost_LIBRARIES}
   )
-  LINK_AGAINST_THRIFT_LIBRARY(TNonblockingSSLServerTest thrift)
   LINK_AGAINST_THRIFT_LIBRARY(TNonblockingSSLServerTest thriftnb)
   add_test(NAME TNonblockingSSLServerTest COMMAND TNonblockingSSLServerTest -- "${CMAKE_CURRENT_SOURCE_DIR}/../../../test/keys")
 endif(OPENSSL_FOUND AND WITH_OPENSSL)
diff --git a/test/cpp/CMakeLists.txt b/test/cpp/CMakeLists.txt
index 9ecc171..969019d 100755
--- a/test/cpp/CMakeLists.txt
+++ b/test/cpp/CMakeLists.txt
@@ -64,20 +64,17 @@
 LINK_AGAINST_THRIFT_LIBRARY(crossspecificnamegencpp thrift)
 
 add_executable(TestServer src/TestServer.cpp)
-target_link_libraries(TestServer crosstestgencpp ${Boost_LIBRARIES} ${LIBEVENT_LIB})
-LINK_AGAINST_THRIFT_LIBRARY(TestServer thrift)
+target_link_libraries(TestServer crosstestgencpp ${Boost_LIBRARIES})
 LINK_AGAINST_THRIFT_LIBRARY(TestServer thriftnb)
 LINK_AGAINST_THRIFT_LIBRARY(TestServer thriftz)
 
 add_executable(TestClient src/TestClient.cpp)
-target_link_libraries(TestClient crosstestgencpp ${Boost_LIBRARIES} ${LIBEVENT_LIB})
-LINK_AGAINST_THRIFT_LIBRARY(TestClient thrift)
+target_link_libraries(TestClient crosstestgencpp ${Boost_LIBRARIES})
 LINK_AGAINST_THRIFT_LIBRARY(TestClient thriftnb)
 LINK_AGAINST_THRIFT_LIBRARY(TestClient thriftz)
 
 add_executable(StressTest src/StressTest.cpp)
-target_link_libraries(StressTest crossstressgencpp ${Boost_LIBRARIES} ${LIBEVENT_LIB})
-LINK_AGAINST_THRIFT_LIBRARY(StressTest thrift)
+target_link_libraries(StressTest crossstressgencpp ${Boost_LIBRARIES})
 LINK_AGAINST_THRIFT_LIBRARY(StressTest thriftnb)
 add_test(NAME StressTest COMMAND StressTest)
 add_test(NAME StressTestConcurrent COMMAND StressTest --client-type=concurrent)
@@ -86,8 +83,7 @@
 # is broken on Windows. Contributions welcome.
 if (NOT WIN32 AND NOT CYGWIN)
     add_executable(StressTestNonBlocking src/StressTestNonBlocking.cpp)
-    target_link_libraries(StressTestNonBlocking crossstressgencpp ${Boost_LIBRARIES} ${LIBEVENT_LIB})
-    LINK_AGAINST_THRIFT_LIBRARY(StressTestNonBlocking thrift)
+    target_link_libraries(StressTestNonBlocking crossstressgencpp ${Boost_LIBRARIES})
     LINK_AGAINST_THRIFT_LIBRARY(StressTestNonBlocking thriftnb)
     LINK_AGAINST_THRIFT_LIBRARY(StressTestNonBlocking thriftz)
     add_test(NAME StressTestNonBlocking COMMAND StressTestNonBlocking)