• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1cmake_minimum_required(VERSION 3.16)
2
3set(helloworld_PROTOBUF_PROTOC_EXECUTABLE "/usr/local/bin/protoc" CACHE STRING "Protoc binary on host")
4set(helloworld_GRPC_CPP_PLUGIN_EXECUTABLE "/usr/local/bin/grpc_cpp_plugin" CACHE STRING "gRPC CPP plugin binary on host")
5
6set(GRPC_SRC_DIR ../../../../)
7
8set(GRPC_BUILD_DIR ../grpc/outputs/${ANDROID_ABI})
9file(MAKE_DIRECTORY ${GRPC_BUILD_DIR})
10
11add_subdirectory(${GRPC_SRC_DIR} ${GRPC_BUILD_DIR})
12
13set(GRPC_PROTO_GENS_DIR ${CMAKE_BINARY_DIR}/gens)
14file(MAKE_DIRECTORY ${GRPC_PROTO_GENS_DIR})
15include_directories(${GRPC_PROTO_GENS_DIR})
16
17function(android_protobuf_grpc_generate_cpp SRC_FILES HDR_FILES INCLUDE_ROOT)
18  if(NOT ARGN)
19    message(SEND_ERROR "Error: android_protobuf_grpc_generate_cpp() called without any proto files")
20    return()
21  endif()
22
23  set(${SRC_FILES})
24  set(${HDR_FILES})
25  set(PROTOBUF_INCLUDE_PATH -I ${INCLUDE_ROOT})
26  foreach(FIL ${ARGN})
27    get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
28    get_filename_component(FIL_WE ${FIL} NAME_WE)
29    file(RELATIVE_PATH REL_FIL ${CMAKE_CURRENT_SOURCE_DIR}/${INCLUDE_ROOT} ${ABS_FIL})
30    get_filename_component(REL_DIR ${REL_FIL} DIRECTORY)
31    set(RELFIL_WE "${REL_DIR}/${FIL_WE}")
32
33    list(APPEND ${SRC_FILES} "${GRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.cc")
34    list(APPEND ${HDR_FILES} "${GRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.h")
35    list(APPEND ${SRC_FILES} "${GRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.cc")
36    list(APPEND ${HDR_FILES} "${GRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.h")
37
38    add_custom_command(
39      OUTPUT "${GRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.cc"
40             "${GRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.h"
41             "${GRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.cc"
42             "${GRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.h"
43      COMMAND ${helloworld_PROTOBUF_PROTOC_EXECUTABLE}
44      ARGS --grpc_out=${GRPC_PROTO_GENS_DIR}
45        --cpp_out=${GRPC_PROTO_GENS_DIR}
46        --plugin=protoc-gen-grpc=${helloworld_GRPC_CPP_PLUGIN_EXECUTABLE}
47        ${PROTOBUF_INCLUDE_PATH}
48        ${REL_FIL}
49      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
50      DEPENDS ${helloworld_PROTOBUF_PROTOC_EXECUTABLE} ${helloworld_GRPC_CPP_PLUGIN_EXECUTABLE} ${ABS_FIL} )
51  endforeach()
52
53  set_source_files_properties(${${SRC_FILES}} ${${HDR_FILES}} PROPERTIES GENERATED TRUE)
54  set(${SRC_FILES} ${${SRC_FILES}} PARENT_SCOPE)
55  set(${HDR_FILES} ${${HDR_FILES}} PARENT_SCOPE)
56endfunction()
57
58set(PROTO_BASE_DIR ${GRPC_SRC_DIR}/examples/protos)
59
60android_protobuf_grpc_generate_cpp(
61  HELLOWORLD_PROTO_SRCS HELLOWORLD_PROTO_HDRS ${PROTO_BASE_DIR} ${PROTO_BASE_DIR}/helloworld.proto)
62
63add_library(helloworld_proto_lib
64  SHARED ${HELLOWORLD_PROTO_HDRS} ${HELLOWORLD_PROTO_SRCS})
65
66target_link_libraries(helloworld_proto_lib
67  grpc++
68  libprotobuf
69  android
70  log)
71
72find_library(log-lib
73 log)
74
75add_library(grpc-helloworld
76  SHARED src/main/cpp/grpc-helloworld.cc)
77
78target_include_directories(grpc-helloworld
79  PRIVATE ${HELLOWORLD_PROTO_HEADERS})
80
81target_link_libraries(grpc-helloworld
82  helloworld_proto_lib
83  android
84  ${log-lib})
85