-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompileProto.cmake
92 lines (75 loc) · 3.57 KB
/
CompileProto.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Checking availability of the gRPC plugin.
find_program(GRPC_CPP_PLUGIN grpc_cpp_plugin)
# A function that creates CPP sources from proto files.
function(COMPILE_PROTO USE_GRPC PROTO_PATH OUT_PATH SRCS HDRS)
# Checking args.
if(NOT ARGN)
message(SEND_ERROR "Error: COMPILE_PROTO() called without any proto files")
return()
endif()
# To collect paths to created sources and headers.
set(${SRCS})
set(${HDRS})
# Getting actual absolute paths to all protos location and output directory.
get_filename_component(ABS_PROTO_PATH "${PROTO_PATH}" ABSOLUTE)
get_filename_component(ABS_OUT_PATH "${OUT_PATH}" ABSOLUTE)
# Launching sources generation for all proto files.
foreach(FIL ${ARGN})
# Getting the absolute path and filename without extension for the current proto file.
get_filename_component(ABS_FIL "${FIL}" ABSOLUTE)
get_filename_component(FIL_WE "${FIL}" NAME_WE)
# Getting the relative dir of the proto file (relative to the protos root dir).
file(RELATIVE_PATH REL_FIL_TO_PROTO "${ABS_PROTO_PATH}" "${ABS_FIL}")
get_filename_component(REL_DIR_TO_PROTO "${REL_FIL_TO_PROTO}" DIRECTORY)
# Preparing a path to label created sources from proto.
set(COMPILED_NAME_TEMPLATE "${ABS_OUT_PATH}/${REL_DIR_TO_PROTO}/${FIL_WE}")
# Firing sources generation command with gRPC application.
if(${USE_GRPC})
# Marking created files for CMake.
list(APPEND ${SRCS} "${COMPILED_NAME_TEMPLATE}.grpc.pb.cc")
list(APPEND ${HDRS} "${COMPILED_NAME_TEMPLATE}.grpc.pb.h")
# Launching proto compilation command.
add_custom_command(
COMMAND ${CMAKE_COMMAND} -E make_directory "${ABS_OUT_PATH}"
OUTPUT
"${COMPILED_NAME_TEMPLATE}.grpc.pb.cc"
"${COMPILED_NAME_TEMPLATE}.grpc.pb.h"
COMMAND
${Protobuf_PROTOC_EXECUTABLE}
ARGS
--grpc_out=${ABS_OUT_PATH}
--plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN}
--proto_path=${ABS_PROTO_PATH}
${ABS_FIL}
DEPENDS
${ABS_FIL} ${Protobuf_PROTOC_EXECUTABLE}
COMMENT
"Running gRPC C++ protocol buffer compiler on ${FIL}"
VERBATIM)
# Without gRPC.
else()
list(APPEND ${SRCS} "${COMPILED_NAME_TEMPLATE}.pb.cc")
list(APPEND ${HDRS} "${COMPILED_NAME_TEMPLATE}.pb.h")
add_custom_command(
COMMAND ${CMAKE_COMMAND} -E make_directory "${ABS_OUT_PATH}"
OUTPUT
"${COMPILED_NAME_TEMPLATE}.pb.cc"
"${COMPILED_NAME_TEMPLATE}.pb.h"
COMMAND
${Protobuf_PROTOC_EXECUTABLE}
ARGS
--cpp_out=${ABS_OUT_PATH}
--proto_path=${ABS_PROTO_PATH}
${ABS_FIL}
DEPENDS
${ABS_FIL} ${Protobuf_PROTOC_EXECUTABLE}
COMMENT
"Running C++ protocol buffer compiler on ${FIL}"
VERBATIM)
endif()
endforeach()
# Returning generated sources list.
set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
set(${SRCS} ${${SRCS}} PARENT_SCOPE)
set(${HDRS} ${${HDRS}} PARENT_SCOPE)
endfunction()