Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find the gRPC package correctly #278

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 68 additions & 101 deletions cmake/FindGRPC.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#
# The original repository is open-sourced with the MIT license.
#
#
# Locate and configure the gRPC library
#
# Adds the following targets:
Expand All @@ -16,128 +15,96 @@
# gRPC::grpc_cpp_plugin - C++ generator plugin for Protocol Buffers
#

#
# Generates C++ sources from the .proto files
#
# grpc_generate_cpp (<SRCS> <HDRS> <DEST> [<ARGN>...])
#
# SRCS - variable to define with autogenerated source files
# HDRS - variable to define with autogenerated header files
# DEST - directory where the source files will be created
# ARGN - .proto files
#
function(GRPC_GENERATE_CPP SRCS HDRS DEST)
if(NOT ARGN)
message(SEND_ERROR "Error: GRPC_GENERATE_CPP() called without any proto files")
return()
endif()

if(GRPC_GENERATE_CPP_APPEND_PATH)
# Create an include path for each file specified
foreach(FIL ${ARGN})
get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
get_filename_component(ABS_PATH ${ABS_FIL} PATH)
list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
if(${_contains_already} EQUAL -1)
list(APPEND _protobuf_include_path -I ${ABS_PATH})
endif()
endforeach()
else()
set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
endif()

if(DEFINED PROTOBUF_IMPORT_DIRS)
foreach(DIR ${PROTOBUF_IMPORT_DIRS})
get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
if(${_contains_already} EQUAL -1)
list(APPEND _protobuf_include_path -I ${ABS_PATH})
endif()
endforeach()
endif()

set(${SRCS})
set(${HDRS})
foreach(FIL ${ARGN})
get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
get_filename_component(FIL_WE ${FIL} NAME_WE)

list(APPEND ${SRCS} "${DEST}/${FIL_WE}.grpc.pb.cc")
list(APPEND ${HDRS} "${DEST}/${FIL_WE}.grpc.pb.h")

add_custom_command(
OUTPUT "${DEST}/${FIL_WE}.grpc.pb.cc"
"${DEST}/${FIL_WE}.grpc.pb.h"
COMMAND protobuf::protoc
ARGS --grpc_out ${DEST} ${_protobuf_include_path} --plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN} ${ABS_FIL}
DEPENDS ${ABS_FIL} protobuf::protoc gRPC::grpc_cpp_plugin
COMMENT "Running C++ gRPC compiler on ${FIL}"
VERBATIM )
endforeach()

set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
set(${SRCS} ${${SRCS}} PARENT_SCOPE)
set(${HDRS} ${${HDRS}} PARENT_SCOPE)
endfunction()

# By default have GRPC_GENERATE_CPP macro pass -I to protoc
# for each directory where a proto file is referenced.
if(NOT DEFINED GRPC_GENERATE_CPP_APPEND_PATH)
set(GRPC_GENERATE_CPP_APPEND_PATH TRUE)
endif()

# Find gRPC include directory
find_path(GRPC_INCLUDE_DIR grpc/grpc.h)
mark_as_advanced(GRPC_INCLUDE_DIR)

# Find gGPR library
find_library(GPR_LIBRARY NAMES gpr)
mark_as_advanced(GRPC_GPR_LIBRARY)
add_library(gRPC::gpr UNKNOWN IMPORTED)
set_target_properties(gRPC::gpr PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GRPC_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES "-lpthread;-ldl"
IMPORTED_LOCATION ${GPR_LIBRARY}
)
if(NOT TARGET gRPC::gpr)
add_library(gRPC::gpr UNKNOWN IMPORTED)
set_target_properties(gRPC::gpr PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GRPC_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES "-lpthread;-ldl"
IMPORTED_LOCATION ${GPR_LIBRARY}
)
endif()

# Find gRPC library
find_library(GRPC_LIBRARY NAMES grpc)
mark_as_advanced(GRPC_LIBRARY)
add_library(gRPC::grpc UNKNOWN IMPORTED)
set_target_properties(gRPC::grpc PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GRPC_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES gRPC::gpr
IMPORTED_LOCATION ${GRPC_LIBRARY}
)
if(NOT TARGET gRPC::grpc)
add_library(gRPC::grpc UNKNOWN IMPORTED)
set_target_properties(gRPC::grpc PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GRPC_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES gRPC::gpr
IMPORTED_LOCATION ${GRPC_LIBRARY}
)
endif()

# Find gRPC C++ library
find_library(GRPC_GRPC++_LIBRARY NAMES grpc++)
mark_as_advanced(GRPC_GRPC++_LIBRARY)
add_library(gRPC::grpc++ UNKNOWN IMPORTED)
set_target_properties(gRPC::grpc++ PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GRPC_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES gRPC::grpc
IMPORTED_LOCATION ${GRPC_GRPC++_LIBRARY}
)
if(NOT TARGET gRPC::grpc++)
add_library(gRPC::grpc++ UNKNOWN IMPORTED)
set_target_properties(gRPC::grpc++ PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GRPC_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES gRPC::grpc
IMPORTED_LOCATION ${GRPC_GRPC++_LIBRARY}
)
endif()

# Find gRPC C++ reflection library
find_library(GRPC_GRPC++_REFLECTION_LIBRARY NAMES grpc++_reflection)
mark_as_advanced(GRPC_GRPC++_REFLECTION_LIBRARY)
add_library(gRPC::grpc++_reflection UNKNOWN IMPORTED)
set_target_properties(gRPC::grpc++_reflection PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GRPC_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES gRPC::grpc++
IMPORTED_LOCATION ${GRPC_GRPC++_REFLECTION_LIBRARY}
)
if(NOT TARGET gRPC::grpc++_reflection)
add_library(gRPC::grpc++_reflection UNKNOWN IMPORTED)
set_target_properties(gRPC::grpc++_reflection PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${GRPC_INCLUDE_DIR}
INTERFACE_LINK_LIBRARIES gRPC::grpc++
IMPORTED_LOCATION ${GRPC_GRPC++_REFLECTION_LIBRARY}
)
endif()

# Find gRPC CPP generator
find_program(GRPC_CPP_PLUGIN NAMES grpc_cpp_plugin)
mark_as_advanced(GRPC_CPP_PLUGIN)
add_executable(gRPC::grpc_cpp_plugin IMPORTED)
set_target_properties(gRPC::grpc_cpp_plugin PROPERTIES
IMPORTED_LOCATION ${GRPC_CPP_PLUGIN}
)
if(NOT TARGET gRPC::grpc_cpp_plugin)
add_executable(gRPC::grpc_cpp_plugin IMPORTED)
set_target_properties(gRPC::grpc_cpp_plugin PROPERTIES
IMPORTED_LOCATION ${GRPC_CPP_PLUGIN}
)
endif()

file(
WRITE "${CMAKE_BINARY_DIR}/get_gRPC_version.cc"
[====[
#include <grpc++/grpc++.h>
#include <iostream>
int main() {
std::cout << grpc::Version(); // no newline to simplify CMake module
return 0;
}
]====])

try_run(
_gRPC_GET_VERSION_STATUS
_gRPC_GET_VERSION_COMPILE_STATUS
"${CMAKE_BINARY_DIR}"
"${CMAKE_BINARY_DIR}/get_gRPC_version.cc"
CMAKE_FLAGS
-DCMAKE_CXX_STANDARD=11
LINK_LIBRARIES
gRPC::grpc++
gRPC::grpc
COMPILE_OUTPUT_VARIABLE _gRPC_GET_VERSION_COMPILE_OUTPUT
RUN_OUTPUT_VARIABLE gRPC_VERSION)

file(REMOVE "${CMAKE_BINARY_DIR}/get_gRPC_version.cc")

include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(gRPC DEFAULT_MSG
GPR_LIBRARY GRPC_LIBRARY GRPC_INCLUDE_DIR GRPC_GRPC++_REFLECTION_LIBRARY GRPC_CPP_PLUGIN)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(gRPC
FOUND_VAR gRPC_FOUND
REQUIRED_VARS GRPC_LIBRARY GPR_LIBRARY GRPC_INCLUDE_DIR GRPC_GRPC++_REFLECTION_LIBRARY GRPC_CPP_PLUGIN
VERSION_VAR gRPC_VERSION)
22 changes: 11 additions & 11 deletions graphlearn/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ if (WITH_HIACTOR)
endif ()
endif ()

## contrib vineyard
if (WITH_VINEYARD)
message("Graph-learn is built with vineyard support.")
find_package (vineyard)
include_directories (SYSTEM ${VINEYARD_INCLUDE_DIRS})
add_definitions (-DWITH_VINEYARD)
if (VINEYARD_USE_OID)
add_definitions (-DVINEYARD_USE_OID)
endif ()
endif ()

## protobuf and gRPC package
find_package (Protobuf QUIET)
if (NOT Protobuf_FOUND)
Expand Down Expand Up @@ -292,17 +303,6 @@ else ()
set (GL_KNN_FILES)
endif ()

## contrib vineyard
if (WITH_VINEYARD)
message("Graph-learn is built with vineyard support.")
find_package (vineyard)
include_directories (SYSTEM ${VINEYARD_INCLUDE_DIRS})
add_definitions (-DWITH_VINEYARD)
if (VINEYARD_USE_OID)
add_definitions (-DVINEYARD_USE_OID)
endif ()
endif ()

## cxx flags
if (DEBUG)
set (GL_MODE_FLAGS -DDEBUG -g)
Expand Down
Loading