| 1 |
cmake_minimum_required(VERSION 3.4.1) |
| 2 |
|
| 3 |
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") |
| 4 |
|
| 5 |
# https://github.com/abseil/abseil-cpp/issues/626 |
| 6 |
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DABSL_NO_XRAY_ATTRIBUTES=1") |
| 7 |
|
| 8 |
set(helloworld_PROTOBUF_PROTOC_EXECUTABLE "/usr/local/bin/protoc" CACHE STRING "Protoc binary on host") |
| 9 |
set(helloworld_GRPC_CPP_PLUGIN_EXECUTABLE "/usr/local/bin/grpc_cpp_plugin" CACHE STRING "gRPC CPP plugin binary on host") |
| 10 |
|
| 11 |
set(GRPC_SRC_DIR ../../../../) |
| 12 |
|
| 13 |
set(GRPC_BUILD_DIR ../grpc/outputs/${ANDROID_ABI}) |
| 14 |
file(MAKE_DIRECTORY ${GRPC_BUILD_DIR}) |
| 15 |
|
| 16 |
add_subdirectory(${GRPC_SRC_DIR} ${GRPC_BUILD_DIR}) |
| 17 |
|
| 18 |
set(GRPC_PROTO_GENS_DIR ${CMAKE_BINARY_DIR}/gens) |
| 19 |
file(MAKE_DIRECTORY ${GRPC_PROTO_GENS_DIR}) |
| 20 |
include_directories(${GRPC_PROTO_GENS_DIR}) |
| 21 |
|
| 22 |
function(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 ${helloworld_PROTOBUF_PROTOC_EXECUTABLE} |
| 49 |
ARGS --grpc_out=${GRPC_PROTO_GENS_DIR} |
| 50 |
--cpp_out=${GRPC_PROTO_GENS_DIR} |
| 51 |
--plugin=protoc-gen-grpc=${helloworld_GRPC_CPP_PLUGIN_EXECUTABLE} |
| 52 |
${PROTOBUF_INCLUDE_PATH} |
| 53 |
${REL_FIL} |
| 54 |
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 55 |
DEPENDS ${helloworld_PROTOBUF_PROTOC_EXECUTABLE} ${helloworld_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) |
| 61 |
endfunction() |
| 62 |
|
| 63 |
set(PROTO_BASE_DIR ${GRPC_SRC_DIR}/examples/protos) |
| 64 |
|
| 65 |
android_protobuf_grpc_generate_cpp( |
| 66 |
HELLOWORLD_PROTO_SRCS HELLOWORLD_PROTO_HDRS ${PROTO_BASE_DIR} ${PROTO_BASE_DIR}/helloworld.proto) |
| 67 |
|
| 68 |
add_library(helloworld_proto_lib |
| 69 |
SHARED ${HELLOWORLD_PROTO_HDRS} ${HELLOWORLD_PROTO_SRCS}) |
| 70 |
|
| 71 |
target_link_libraries(helloworld_proto_lib |
| 72 |
grpc++ |
| 73 |
libprotobuf |
| 74 |
android |
| 75 |
log) |
| 76 |
|
| 77 |
find_library(log-lib |
| 78 |
log) |
| 79 |
|
| 80 |
add_library(grpc-helloworld |
| 81 |
SHARED src/main/cpp/grpc-helloworld.cc) |
| 82 |
|
| 83 |
target_include_directories(grpc-helloworld |
| 84 |
PRIVATE ${HELLOWORLD_PROTO_HEADERS}) |
| 85 |
|
| 86 |
target_link_libraries(grpc-helloworld |
| 87 |
helloworld_proto_lib |
| 88 |
android |
| 89 |
${log-lib}) |