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