2020-09-30 22:28:23 +00:00
#[[
D e f i n e s t h e f o l l o w i n g v a r i a b l e s :
` ` g R P C _ F O U N D ` `
W h e t h e r t h e g R P C f r a m e w o r k i s f o u n d
` ` g R P C _ I N C L U D E _ D I R S ` `
T h e i n c l u d e d i r e c t o r i e s o f t h e g R P C f r a m e w o r k , i n c l u d i n g t h e i n c l u d e d i r e c t o r i e s o f t h e C + + w r a p p e r .
` ` g R P C _ L I B R A R I E S ` `
T h e l i b r a r i e s o f t h e g R P C f r a m e w o r k .
2020-10-07 21:42:27 +00:00
` ` g R P C _ C P P _ P L U G I N ` `
2020-09-30 22:28:23 +00:00
T h e p l u g i n f o r g e n e r a t i n g g R P C c l i e n t a n d s e r v e r C + + s t u b s f r o m ` . p r o t o ` f i l e s
2020-10-07 21:42:27 +00:00
` ` g R P C _ P Y T H O N _ P L U G I N ` `
2020-09-30 22:28:23 +00:00
T h e p l u g i n f o r g e n e r a t i n g g R P C c l i e n t a n d s e r v e r P y t h o n s t u b s f r o m ` . p r o t o ` f i l e s
T h e f o l l o w i n g : p r o p _ t g t : ` I M P O R T E D ` t a r g e t s a r e a l s o d e f i n e d :
` ` g r p c + + ` `
` ` g r p c + + _ u n s e c u r e ` `
` ` g r p c _ c p p _ p l u g i n ` `
` ` g r p c _ p y t h o n _ p l u g i n ` `
2020-10-07 21:42:27 +00:00
S e t t h e f o l l o w i n g v a r i a b l e s t o a d j u s t t h e b e h a v i o u r o f t h i s s c r i p t :
` ` g R P C _ U S E _ U N S E C U R E _ L I B R A R I E S ` `
i f s e t g R P C _ L I B R A R I E S w i l l b e f i l l e d w i t h t h e u n s e c u r e v e r s i o n o f t h e libraries ( i.e. without SSL )
i n s t e a d o f t h e s e c u r e o n e s .
` ` g R P C _ D E B U G `
i f s e t t h e d e b u g m e s s a g e w i l l b e p r i n t e d .
2020-09-30 22:28:23 +00:00
A d d c u s t o m c o m m a n d s t o p r o c e s s ` ` . p r o t o ` ` f i l e s t o C + + : :
protobuf_generate_grpc_cpp ( <SRCS> <HDRS>
[ D E S C R I P T O R S < D E S C > ] [ E X P O R T _ M A C R O < M A C R O > ] [ < A R G N > . . . ] )
` ` S R C S ` `
V a r i a b l e t o d e f i n e w i t h a u t o g e n e r a t e d s o u r c e f i l e s
` ` H D R S ` `
V a r i a b l e t o d e f i n e w i t h a u t o g e n e r a t e d h e a d e r f i l e s
` ` D E S C R I P T O R S ` `
V a r i a b l e t o d e f i n e w i t h a u t o g e n e r a t e d d e s c r i p t o r f i l e s , i f r e q u e s t e d .
` ` E X P O R T _ M A C R O ` `
i s a m a c r o w h i c h s h o u l d e x p a n d t o ` ` __declspec ( dllexport ) ` ` o r
` ` __declspec ( dllimport ) ` ` d e p e n d i n g o n w h a t i s b e i n g c o m p i l e d .
` ` A R G N ` `
` ` . p r o t o ` ` f i l e s
#]]
# Function to generate C++ files from .proto files.
# This function is a modified version of the function PROTOBUF_GENERATE_CPP() copied from https://github.com/Kitware/CMake/blob/master/Modules/FindProtobuf.cmake.
function ( PROTOBUF_GENERATE_GRPC_CPP SRCS HDRS )
cmake_parse_arguments ( protobuf_generate_grpc_cpp "" "EXPORT_MACRO;DESCRIPTORS" "" ${ ARGN } )
set ( _proto_files "${protobuf_generate_grpc_cpp_UNPARSED_ARGUMENTS}" )
if ( NOT _proto_files )
message ( SEND_ERROR "Error: PROTOBUF_GENERATE_GRPC_CPP() called without any proto files" )
return ( )
endif ( )
if ( PROTOBUF_GENERATE_GRPC_CPP_APPEND_PATH )
set ( _append_arg APPEND_PATH )
endif ( )
if ( protobuf_generate_grpc_cpp_DESCRIPTORS )
set ( _descriptors DESCRIPTORS )
endif ( )
if ( DEFINED PROTOBUF_IMPORT_DIRS AND NOT DEFINED Protobuf_IMPORT_DIRS )
set ( Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}" )
endif ( )
if ( DEFINED Protobuf_IMPORT_DIRS )
set ( _import_arg IMPORT_DIRS ${ Protobuf_IMPORT_DIRS } )
endif ( )
set ( _outvar )
protobuf_generate_grpc ( ${ _append_arg } ${ _descriptors } LANGUAGE cpp EXPORT_MACRO ${ protobuf_generate_cpp_EXPORT_MACRO } OUT_VAR _outvar ${ _import_arg } PROTOS ${ _proto_files } )
set ( ${ SRCS } )
set ( ${ HDRS } )
if ( protobuf_generate_grpc_cpp_DESCRIPTORS )
set ( ${ protobuf_generate_grpc_cpp_DESCRIPTORS } )
endif ( )
foreach ( _file ${ _outvar } )
if ( _file MATCHES "cc$" )
list ( APPEND ${ SRCS } ${ _file } )
elseif ( _file MATCHES "desc$" )
list ( APPEND ${ protobuf_generate_grpc_cpp_DESCRIPTORS } ${ _file } )
else ( )
list ( APPEND ${ HDRS } ${ _file } )
endif ( )
endforeach ( )
set ( ${ SRCS } ${ ${SRCS } } PARENT_SCOPE )
set ( ${ HDRS } ${ ${HDRS } } PARENT_SCOPE )
if ( protobuf_generate_grpc_cpp_DESCRIPTORS )
set ( ${ protobuf_generate_grpc_cpp_DESCRIPTORS } "${${protobuf_generate_grpc_cpp_DESCRIPTORS}}" PARENT_SCOPE )
endif ( )
endfunction ( )
# Helper function.
# This function is a modified version of the function protobuf_generate() copied from https://github.com/Kitware/CMake/blob/master/Modules/FindProtobuf.cmake.
function ( protobuf_generate_grpc )
set ( _options APPEND_PATH DESCRIPTORS )
set ( _singleargs LANGUAGE OUT_VAR EXPORT_MACRO PROTOC_OUT_DIR )
if ( COMMAND target_sources )
list ( APPEND _singleargs TARGET )
endif ( )
set ( _multiargs PROTOS IMPORT_DIRS GENERATE_EXTENSIONS )
cmake_parse_arguments ( protobuf_generate_grpc "${_options}" "${_singleargs}" "${_multiargs}" "${ARGN}" )
if ( NOT protobuf_generate_grpc_PROTOS AND NOT protobuf_generate_grpc_TARGET )
message ( SEND_ERROR "Error: protobuf_generate_grpc called without any targets or source files" )
return ( )
endif ( )
if ( NOT protobuf_generate_grpc_OUT_VAR AND NOT protobuf_generate_grpc_TARGET )
message ( SEND_ERROR "Error: protobuf_generate_grpc called without a target or output variable" )
return ( )
endif ( )
if ( NOT protobuf_generate_grpc_LANGUAGE )
set ( protobuf_generate_grpc_LANGUAGE cpp )
endif ( )
string ( TOLOWER ${ protobuf_generate_grpc_LANGUAGE } protobuf_generate_grpc_LANGUAGE )
if ( NOT protobuf_generate_grpc_PROTOC_OUT_DIR )
set ( protobuf_generate_grpc_PROTOC_OUT_DIR ${ CMAKE_CURRENT_BINARY_DIR } )
endif ( )
if ( protobuf_generate_grpc_EXPORT_MACRO AND protobuf_generate_grpc_LANGUAGE STREQUAL cpp )
set ( _dll_export_decl "dllexport_decl=${protobuf_generate_grpc_EXPORT_MACRO}:" )
endif ( )
if ( NOT protobuf_generate_grpc_GENERATE_EXTENSIONS )
if ( protobuf_generate_grpc_LANGUAGE STREQUAL cpp )
set ( protobuf_generate_grpc_GENERATE_EXTENSIONS .pb.h .pb.cc .grpc.pb.h .grpc.pb.cc )
elseif ( protobuf_generate_grpc_LANGUAGE STREQUAL python )
set ( protobuf_generate_grpc_GENERATE_EXTENSIONS _pb2.py )
else ( )
message ( SEND_ERROR "Error: protobuf_generate_grpc given unknown Language ${LANGUAGE}, please provide a value for GENERATE_EXTENSIONS" )
return ( )
endif ( )
endif ( )
if ( NOT protobuf_generate_grpc_PLUGIN )
if ( protobuf_generate_grpc_LANGUAGE STREQUAL cpp )
set ( protobuf_generate_grpc_PLUGIN "grpc_cpp_plugin" )
elseif ( protobuf_generate_grpc_LANGUAGE STREQUAL python )
set ( protobuf_generate_grpc_PLUGIN "grpc_python_plugin" )
else ( )
message ( SEND_ERROR "Error: protobuf_generate_grpc given unknown Language ${LANGUAGE}, please provide a value for PLUGIN" )
return ( )
endif ( )
endif ( )
if ( protobuf_generate_grpc_TARGET )
get_target_property ( _source_list ${ protobuf_generate_grpc_TARGET } SOURCES )
foreach ( _file ${ _source_list } )
if ( _file MATCHES "proto$" )
list ( APPEND protobuf_generate_grpc_PROTOS ${ _file } )
endif ( )
endforeach ( )
endif ( )
if ( NOT protobuf_generate_grpc_PROTOS )
message ( SEND_ERROR "Error: protobuf_generate_grpc could not find any .proto files" )
return ( )
endif ( )
if ( protobuf_generate_grpc_APPEND_PATH )
# Create an include path for each file specified
foreach ( _file ${ protobuf_generate_grpc_PROTOS } )
get_filename_component ( _abs_file ${ _file } ABSOLUTE )
get_filename_component ( _abs_path ${ _abs_file } 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 ( )
foreach ( DIR ${ protobuf_generate_grpc_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 ( )
set ( _generated_srcs_all )
foreach ( _proto ${ protobuf_generate_grpc_PROTOS } )
get_filename_component ( _abs_file ${ _proto } ABSOLUTE )
get_filename_component ( _abs_dir ${ _abs_file } DIRECTORY )
get_filename_component ( _basename ${ _proto } NAME_WE )
file ( RELATIVE_PATH _rel_dir ${ CMAKE_CURRENT_SOURCE_DIR } ${ _abs_dir } )
set ( _possible_rel_dir )
if ( NOT protobuf_generate_grpc_APPEND_PATH )
set ( _possible_rel_dir ${ _rel_dir } / )
endif ( )
set ( _generated_srcs )
foreach ( _ext ${ protobuf_generate_grpc_GENERATE_EXTENSIONS } )
list ( APPEND _generated_srcs "${protobuf_generate_grpc_PROTOC_OUT_DIR}/${_possible_rel_dir}${_basename}${_ext}" )
endforeach ( )
if ( protobuf_generate_grpc_DESCRIPTORS AND protobuf_generate_grpc_LANGUAGE STREQUAL cpp )
set ( _descriptor_file "${CMAKE_CURRENT_BINARY_DIR}/${_basename}.desc" )
set ( _dll_desc_out "--descriptor_set_out=${_descriptor_file}" )
list ( APPEND _generated_srcs ${ _descriptor_file } )
endif ( )
list ( APPEND _generated_srcs_all ${ _generated_srcs } )
add_custom_command (
O U T P U T $ { _ g e n e r a t e d _ s r c s }
C O M M A N D p r o t o b u f : : p r o t o c
A R G S - - $ { p r o t o b u f _ g e n e r a t e _ g r p c _ L A N G U A G E } _ o u t $ { _ d l l _ e x p o r t _ d e c l } $ { p r o t o b u f _ g e n e r a t e _ g r p c _ P R O T O C _ O U T _ D I R }
- - g r p c _ o u t $ { _ d l l _ e x p o r t _ d e c l } $ { p r o t o b u f _ g e n e r a t e _ g r p c _ P R O T O C _ O U T _ D I R }
- - p l u g i n = p r o t o c - g e n - g r p c = $ < T A R G E T _ F I L E : $ { p r o t o b u f _ g e n e r a t e _ g r p c _ P L U G I N } >
$ { _ d l l _ d e s c _ o u t } $ { _ p r o t o b u f _ i n c l u d e _ p a t h } $ { _ a b s _ f i l e }
D E P E N D S $ { _ a b s _ f i l e } p r o t o b u f : : p r o t o c $ { p r o t o b u f _ g e n e r a t e _ g r p c _ P L U G I N }
C O M M E N T " R u n n i n g $ { p r o t o b u f _ g e n e r a t e _ g r p c _ L A N G U A G E } p r o t o c o l b u f f e r c o m p i l e r o n $ { _ p r o t o } "
V E R B A T I M )
endforeach ( )
set_source_files_properties ( ${ _generated_srcs_all } PROPERTIES GENERATED TRUE )
if ( protobuf_generate_grpc_OUT_VAR )
set ( ${ protobuf_generate_grpc_OUT_VAR } ${ _generated_srcs_all } PARENT_SCOPE )
endif ( )
if ( protobuf_generate_grpc_TARGET )
target_sources ( ${ protobuf_generate_grpc_TARGET } PRIVATE ${ _generated_srcs_all } )
endif ( )
endfunction ( )
# Find the libraries.
if ( gRPC_USE_STATIC_LIBS )
# Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
set ( _gRPC_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${ CMAKE_FIND_LIBRARY_SUFFIXES } )
if ( WIN32 )
set ( CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${ CMAKE_FIND_LIBRARY_SUFFIXES } )
else ( )
set ( CMAKE_FIND_LIBRARY_SUFFIXES .a )
endif ( )
endif ( )
find_library ( gRPC_LIBRARY NAMES grpc )
find_library ( gRPC_CPP_LIBRARY NAMES grpc++ )
find_library ( gRPC_UNSECURE_LIBRARY NAMES grpc_unsecure )
find_library ( gRPC_CPP_UNSECURE_LIBRARY NAMES grpc++_unsecure )
2020-10-07 21:42:27 +00:00
find_library ( gRPC_CARES_LIBRARY NAMES cares )
2020-09-30 22:28:23 +00:00
set ( gRPC_LIBRARIES )
if ( gRPC_USE_UNSECURE_LIBRARIES )
if ( gRPC_UNSECURE_LIBRARY )
set ( gRPC_LIBRARIES ${ gRPC_LIBRARIES } ${ gRPC_UNSECURE_LIBRARY } )
endif ( )
if ( gRPC_CPP_UNSECURE_LIBRARY )
set ( gRPC_LIBRARIES ${ gRPC_LIBRARIES } ${ gRPC_CPP_UNSECURE_LIBRARY } )
endif ( )
else ( )
if ( gRPC_LIBRARY )
set ( gRPC_LIBRARIES ${ gRPC_LIBRARIES } ${ gRPC_LIBRARY } )
endif ( )
if ( gRPC_CPP_UNSECURE_LIBRARY )
set ( gRPC_LIBRARIES ${ gRPC_LIBRARIES } ${ gRPC_CPP_LIBRARY } )
endif ( )
endif ( )
2020-10-07 21:42:27 +00:00
set ( gRPC_LIBRARIES ${ gRPC_LIBRARIES } ${ gRPC_CARES_LIBRARY } )
2020-09-30 22:28:23 +00:00
# Restore the original find library ordering.
if ( gRPC_USE_STATIC_LIBS )
set ( CMAKE_FIND_LIBRARY_SUFFIXES ${ _gRPC_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES } )
endif ( )
# Find the include directories.
find_path ( gRPC_INCLUDE_DIR grpc/grpc.h )
find_path ( gRPC_CPP_INCLUDE_DIR grpc++/grpc++.h )
if ( gRPC_INCLUDE_DIR AND gRPC_CPP_INCLUDE_DIR AND NOT(gRPC_INCLUDE_DIR STREQUAL gRPC_CPP_INCLUDE_DIR ) )
set ( gRPC_INCLUDE_DIRS ${ gRPC_INCLUDE_DIR } ${ gRPC_CPP_INCLUDE_DIR } )
elseif ( gRPC_INCLUDE_DIR )
set ( gRPC_INCLUDE_DIRS ${ gRPC_INCLUDE_DIR } )
else ( )
set ( gRPC_INCLUDE_DIRS ${ gRPC_CPP_INCLUDE_DIR } )
endif ( )
# Get full path to plugin.
2020-10-07 21:42:27 +00:00
find_program ( gRPC_CPP_PLUGIN
2020-09-30 22:28:23 +00:00
N A M E S g r p c _ c p p _ p l u g i n
D O C " T h e p l u g i n f o r g e n e r a t i n g g R P C c l i e n t a n d s e r v e r C + + s t u b s f r o m ` . p r o t o ` f i l e s " )
2020-10-07 21:42:27 +00:00
find_program ( gRPC_PYTHON_PLUGIN
2020-09-30 22:28:23 +00:00
N A M E S g r p c _ p y t h o n _ p l u g i n
D O C " T h e p l u g i n f o r g e n e r a t i n g g R P C c l i e n t a n d s e r v e r P y t h o n s t u b s f r o m ` . p r o t o ` f i l e s " )
# Add imported targets.
if ( gRPC_CPP_LIBRARY AND NOT TARGET grpc++ )
add_library ( grpc++ UNKNOWN IMPORTED )
set_target_properties ( grpc++ PROPERTIES
I M P O R T E D _ L O C A T I O N " $ { g R P C _ C P P _ L I B R A R Y } " )
set_target_properties ( grpc++ PROPERTIES
I N T E R F A C E _ I N C L U D E _ D I R E C T O R I E S $ { g R P C _ I N C L U D E _ D I R S } )
endif ( )
if ( gRPC_CPP_UNSECURE_LIBRARY AND NOT TARGET grpc++_unsecure )
add_library ( grpc++_unsecure UNKNOWN IMPORTED )
set_target_properties ( grpc++_unsecure PROPERTIES
I M P O R T E D _ L O C A T I O N " $ { g R P C _ C P P _ U N S E C U R E _ L I B R A R Y } " )
set_target_properties ( grpc++_unsecure PROPERTIES
I N T E R F A C E _ I N C L U D E _ D I R E C T O R I E S $ { g R P C _ I N C L U D E _ D I R S } )
endif ( )
if ( gRPC_CPP_PLUGIN AND NOT TARGET grpc_cpp_plugin )
add_executable ( grpc_cpp_plugin IMPORTED )
set_target_properties ( grpc_cpp_plugin PROPERTIES
I M P O R T E D _ L O C A T I O N " $ { g R P C _ C P P _ P L U G I N } " )
endif ( )
if ( gRPC_PYTHON_PLUGIN AND NOT TARGET grpc_python_plugin )
add_executable ( grpc_python_plugin IMPORTED )
set_target_properties ( grpc_python_plugin PROPERTIES
I M P O R T E D _ L O C A T I O N " $ { g R P C _ P Y T H O N _ P L U G I N } " )
endif ( )
#include(FindPackageHandleStandardArgs.cmake)
FIND_PACKAGE_HANDLE_STANDARD_ARGS ( gRPC
2020-10-07 21:42:27 +00:00
R E Q U I R E D _ V A R S g R P C _ L I B R A R Y g R P C _ C P P _ L I B R A R Y g R P C _ U N S E C U R E _ L I B R A R Y g R P C _ C P P _ U N S E C U R E _ L I B R A R Y g R P C _ C A R E S _ L I B R A R Y
g R P C _ I N C L U D E _ D I R g R P C _ C P P _ I N C L U D E _ D I R g R P C _ C P P _ P L U G I N g R P C _ P Y T H O N _ P L U G I N )
2020-09-30 22:28:23 +00:00
if ( gRPC_FOUND )
if ( gRPC_DEBUG )
message ( STATUS "gRPC: INCLUDE_DIRS=${gRPC_INCLUDE_DIRS}" )
message ( STATUS "gRPC: LIBRARIES=${gRPC_LIBRARIES}" )
2020-10-07 21:42:27 +00:00
message ( STATUS "gRPC: CPP_PLUGIN=${gRPC_CPP_PLUGIN}" )
message ( STATUS "gRPC: PYTHON_PLUGIN=${gRPC_PYTHON_PLUGIN}" )
2020-09-30 22:28:23 +00:00
endif ( )
endif ( )