From c79e999c098d29cb5daaccb6a46720bfac13c0df Mon Sep 17 00:00:00 2001
From: Keith Thorne <keith.thorne@ligo.org>
Date: Wed, 21 Aug 2019 17:00:40 -0500
Subject: [PATCH] Add in RPC library detection to support newer glibc with
 SunRPC removed. Use transport-independent RPC package (tirpc) if available,
 otherwise use RPC headers if still available. Mostly for non-Debian compiles,
 as Debian asserts non-default flag in glibc to retain the old RPC support

---
 CMakeLists.txt             |  1 +
 config/cmake/FindRPC.cmake | 79 ++++++++++++++++++++++++++++++++++++++
 src/daqd/CMakeLists.txt    |  4 ++
 src/gds/CMakeLists.txt     |  8 ++--
 4 files changed, 89 insertions(+), 3 deletions(-)
 create mode 100644 config/cmake/FindRPC.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 04d0cb8ed..05fbd906c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -28,6 +28,7 @@ FIND_PACKAGE(Dolphin)
 FIND_PACKAGE(NDS2Client)
 FIND_PACKAGE(Boost COMPONENTS system)
 FIND_PACKAGE(RapidJSON)
+FIND_PACKAGE(RPC)
 
 CHECK_CXX_SOURCE_COMPILES("#include <iostream>
 #include <FlexLexer.h>
diff --git a/config/cmake/FindRPC.cmake b/config/cmake/FindRPC.cmake
new file mode 100644
index 000000000..e73786e56
--- /dev/null
+++ b/config/cmake/FindRPC.cmake
@@ -0,0 +1,79 @@
+# FindRPC
+# ---------
+#
+# Find the RPC includes and library.
+#
+#  - They can either be in the Transport-Independent RPC (TIRPC) package or 
+#  still in glibc.    
+#
+# Result Variables
+# ^^^^^^^^^^^^^^^^
+#
+# This module will set the following variables in your project:
+#  ``RPC_FOUND``
+#   true if the RPC header are found from TIRPC or glibc
+#  ``RPC_INCLUDE_DIRS```
+#   where to find rpc.h, etc. (could be simply /usr/include)
+#  ``RPC_LIBRARIES``
+#   the libraries to link against for RPC (may be empty if glibc)
+# ``TIRPC_FOUND``
+#   true if the TIRPC headers and libraries were found.
+#
+# The following may be set if TIRPC found
+# ``TIRPC_INCLUDE_DIRS``
+#   where to find rpc.h, etc. from TIRPC
+# ``TIRPC_LIBRARIES``
+#   the libraries to link against to use TIRPC.
+# ``TIRPC_VERSION``
+#   the version of TIRPC found.
+#
+if (RPC_FOUND)
+else (RPC_FOUND)
+
+  if (TIRPC_FOUND)
+  else (TIRPC_FOUND)
+
+    find_package(PkgConfig QUIET)
+    pkg_check_modules(PC_TIRPC libtirpc)
+
+    find_path(TIRPC_INCLUDE_DIRS
+      NAMES netconfig.h
+      PATH_SUFFIXES tirpc
+      HINTS ${PC_TIRPC_INCLUDE_DIRS}
+    )
+
+    find_library(TIRPC_LIBRARIES
+      NAMES tirpc
+      HINTS ${PC_TIRPC_LIBRARY_DIRS}
+    )
+
+    set(TIRPC_VERSION ${PC_TIRPC_VERSION})
+
+    include(FindPackageHandleStandardArgs)
+
+    find_package_handle_standard_args(TIRPC
+       REQUIRED_VARS TIRPC_LIBRARIES TIRPC_INCLUDE_DIRS
+       VERSION_VAR TIRPC_VERSION
+    )
+
+    mark_as_advanced(TIRPC_INCLUDE_DIRS TIRPC_LIBRARIES)
+ 
+  endif (TIRPC_FOUND)
+  
+  if (TIRPC_FOUND)
+    set (RPC_FOUND TRUE)
+    set (RPC_INCLUDE_DIRS ${TIRPC_INCLUDE_DIRS})
+    set (RPC_LIBRARIES ${TIRPC_LIBRARIES})
+  else (TIRPC_FOUND)
+    find_path(rpc_RPC_H rpc.h hints /usr/include/rpc)
+    if (rpc_RPC_H)
+      Message("RPC found from glibc")
+      set (RPC_FOUND TRUE)
+      set (RPC_INCLUDE_DIRS "/usr/include")
+      set (RPC_LIBRARIES "")
+    else (rpc_RPC_H)
+      Message("RPC Libraries not Found")
+    endif (rpc_RPC_H)
+  endif (TIRPC_FOUND)
+
+endif (RPC_FOUND)
diff --git a/src/daqd/CMakeLists.txt b/src/daqd/CMakeLists.txt
index 7c0dd03ef..16eb7fdee 100644
--- a/src/daqd/CMakeLists.txt
+++ b/src/daqd/CMakeLists.txt
@@ -74,6 +74,7 @@ target_include_directories(dc_gds_shmem PUBLIC
 		${CMAKE_CURRENT_SOURCE_DIR}/../include
 		${CMAKE_CURRENT_SOURCE_DIR}
 		${CMAKE_CURRENT_BINARY_DIR}
+		${RPC_INCLUDE_DIRS}
 		)
 target_compile_options(dc_gds_shmem PUBLIC
 		-fno-common
@@ -83,6 +84,7 @@ target_compile_options(dc_gds_shmem PUBLIC
 target_link_libraries(dc_gds_shmem PRIVATE
 		gds::daqd
 		ldastools::framecpp
+		${RPC_LIBRARIES}
 		)
 
 add_executable(daqd_shmem
@@ -120,6 +122,7 @@ target_include_directories(daqd_shmem PUBLIC
 		${CMAKE_CURRENT_SOURCE_DIR}
 		${CMAKE_CURRENT_SOURCE_DIR}/../include
 		${CMAKE_CURRENT_SOURCE_DIR}/../zmq_stream
+		${RPC_INCLUDE_DIRS}
 		)
 target_link_libraries(daqd_shmem PUBLIC
 		gds::daqd
@@ -133,6 +136,7 @@ target_link_libraries(daqd_shmem PUBLIC
 		driver::shmem
 		driver::ini_parsing
 		${CMAKE_THREAD_LIBS_INIT}
+		${RPC_LIBRARIES}
 		)
 
 install(TARGETS daqd_shmem DESTINATION bin)
diff --git a/src/gds/CMakeLists.txt b/src/gds/CMakeLists.txt
index 3290be62a..c1981321f 100644
--- a/src/gds/CMakeLists.txt
+++ b/src/gds/CMakeLists.txt
@@ -234,8 +234,8 @@ target_include_directories(gds_cc PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/gds_include
 add_dependencies(gds_cc _gds_rpc_gen_hdrs)
 
 
-add_executable(awgtpman $<TARGET_OBJECTS:gds_c> $<TARGET_OBJECTS:gds_cc> ${CMAKE_CURRENT_BINARY_DIR}/gds_includes)
-target_link_libraries(awgtpman ${CMAKE_THREAD_LIBS_INIT})
+add_executable(awgtpman $<TARGET_OBJECTS:gds_c> $<TARGET_OBJECTS:gds_cc> ${CMAKE_CURRENT_BINARY_DIR}/gds_includes ${RPC_INCLUDE_DIRS})
+target_link_libraries(awgtpman ${CMAKE_THREAD_LIBS_INIT} ${RPC_LIBRARIES})
 
 
 #######
@@ -262,6 +262,7 @@ target_compile_definitions(gds_daqd_general PRIVATE
         -D_TESTPOINT_DIRECT=0)
 target_include_directories(gds_daqd_general PRIVATE
         ${CMAKE_CURRENT_BINARY_DIR}/gds_includes
+        ${RPC_INCLUDE_DIRS}
         )
 add_dependencies(gds_daqd_general _gds_rpc_gen_hdrs)
 
@@ -271,10 +272,11 @@ target_compile_definitions(gds_daqd_testpoint PRIVATE
         -D_TP_DAQD -D_TESTPOINT_DIRECT=0 -D_NO_KEEP_ALIVE)
 target_include_directories(gds_daqd_testpoint PRIVATE
         ${CMAKE_CURRENT_BINARY_DIR}/gds_includes
+        ${RPC_INCLUDE_DIRS}
         )
 add_dependencies(gds_daqd_testpoint _gds_rpc_gen_hdrs)
 
 add_library(gds_daqd_libs STATIC $<TARGET_OBJECTS:gds_daqd_general> $<TARGET_OBJECTS:gds_daqd_testpoint>)
 add_library(gds::daqd ALIAS gds_daqd_libs)
 
-install(TARGETS awgtpman DESTINATION bin)
\ No newline at end of file
+install(TARGETS awgtpman DESTINATION bin)
-- 
GitLab