1cmake_minimum_required(VERSION 3.4.1) 2 3set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 4 5set(PROTOBUF_PROTOC_EXECUTABLE "/usr/local/bin/protoc" CACHE STRING "Protoc binary on host") 6set(gRPC_CPP_PLUGIN_EXECUTABLE "/usr/local/bin/grpc_cpp_plugin" CACHE STRING "gRPC CPP plugin binary on host") 7 8set(GRPC_SRC_DIR ../../../../../) 9 10set(GRPC_BUILD_DIR ../grpc/outputs/${ANDROID_ABI}) 11file(MAKE_DIRECTORY ${GRPC_BUILD_DIR}) 12 13add_subdirectory(${GRPC_SRC_DIR} ${GRPC_BUILD_DIR}) 14 15#include_directories(${GRPC_SRC_DIR}/include) 16include_directories(${GRPC_SRC_DIR}) 17 18set(GRPC_PROTO_GENS_DIR ${CMAKE_BINARY_DIR}/gens) 19file(MAKE_DIRECTORY ${GRPC_PROTO_GENS_DIR}) 20include_directories(${GRPC_PROTO_GENS_DIR}) 21 22function(android_protobuf_grpc_generate_cpp SRC_FILES HDR_FILES INCLUDE_ROOT) 23 if(NOT ARGN) 24 message(SEND_ERROR "Error: android_protobuf_grpc_generate_cpp() called without any proto files") 25 return() 26 endif() 27 28 set(${SRC_FILES}) 29 set(${HDR_FILES}) 30 set(PROTOBUF_INCLUDE_PATH -I ${INCLUDE_ROOT}) 31 foreach(FIL ${ARGN}) 32 get_filename_component(ABS_FIL ${FIL} ABSOLUTE) 33 get_filename_component(FIL_WE ${FIL} NAME_WE) 34 file(RELATIVE_PATH REL_FIL ${CMAKE_CURRENT_SOURCE_DIR}/${INCLUDE_ROOT} ${ABS_FIL}) 35 get_filename_component(REL_DIR ${REL_FIL} DIRECTORY) 36 set(RELFIL_WE "${REL_DIR}/${FIL_WE}") 37 38 list(APPEND ${SRC_FILES} "${GRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.cc") 39 list(APPEND ${HDR_FILES} "${GRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.h") 40 list(APPEND ${SRC_FILES} "${GRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.cc") 41 list(APPEND ${HDR_FILES} "${GRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.h") 42 43 add_custom_command( 44 OUTPUT "${GRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.cc" 45 "${GRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.h" 46 "${GRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.cc" 47 "${GRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.h" 48 COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} 49 ARGS --grpc_out=${GRPC_PROTO_GENS_DIR} 50 --cpp_out=${GRPC_PROTO_GENS_DIR} 51 --plugin=protoc-gen-grpc=${gRPC_CPP_PLUGIN_EXECUTABLE} 52 ${PROTOBUF_INCLUDE_PATH} 53 ${REL_FIL} 54 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 55 DEPENDS ${PROTOBUF_PROTOC_EXECUTABLE} ${gRPC_CPP_PLUGIN_EXECUTABLE} ${ABS_FIL} ) 56 endforeach() 57 58 set_source_files_properties(${${SRC_FILES}} ${${HDR_FILES}} PROPERTIES GENERATED TRUE) 59 set(${SRC_FILES} ${${SRC_FILES}} PARENT_SCOPE) 60 set(${HDR_FILES} ${${HDR_FILES}} PARENT_SCOPE) 61endfunction() 62 63set(PROTO_BASE_DIR ${GRPC_SRC_DIR}/examples/protos) 64 65android_protobuf_grpc_generate_cpp( 66 MESSAGES_PROTO_SRCS MESSAGES_PROTO_HDRS 67 ${GRPC_SRC_DIR} ${GRPC_SRC_DIR}/src/proto/grpc/testing/messages.proto) 68 69add_library(messages_proto_lib 70 SHARED ${MESSAGES_PROTO_SRCS} ${MESSAGES_PROTO_HDRS}) 71 72target_link_libraries(messages_proto_lib 73 libprotobuf 74 grpc++ 75 android 76 log) 77 78android_protobuf_grpc_generate_cpp( 79 EMPTY_PROTO_SRCS EMPTY_PROTO_HDRS 80 ${GRPC_SRC_DIR} ${GRPC_SRC_DIR}/src/proto/grpc/testing/empty.proto) 81 82add_library(empty_proto_lib 83 SHARED ${EMPTY_PROTO_SRCS} ${EMPTY_PROTO_HDRS}) 84 85target_link_libraries(empty_proto_lib 86 libprotobuf 87 grpc++ 88 android 89 log) 90 91android_protobuf_grpc_generate_cpp( 92 TEST_PROTO_SRCS TEST_PROTO_HDRS ${GRPC_SRC_DIR} ${GRPC_SRC_DIR}/src/proto/grpc/testing/test.proto) 93 94add_library(test_proto_lib 95 SHARED ${TEST_PROTO_SRCS} ${TEST_PROTO_HDRS}) 96 97target_link_libraries(test_proto_lib 98 libprotobuf 99 grpc++ 100 empty_proto_lib 101 messages_proto_lib 102 android 103 log) 104 105find_library(log-lib 106 log) 107 108add_library(grpc-interop 109 SHARED 110 src/main/cpp/grpc-interop.cc 111 ${GRPC_SRC_DIR}/test/cpp/interop/interop_client.h 112 ${GRPC_SRC_DIR}/test/cpp/interop/interop_client.cc) 113 114target_link_libraries(grpc-interop 115 messages_proto_lib 116 empty_proto_lib 117 test_proto_lib 118 android 119 ${log-lib}) 120