From cc641688296a3cf7efbfd30b942b4cdf8d8049e7 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Tue, 17 Jan 2017 11:59:31 +0100 Subject: cmake support for generating from .proto files --- templates/CMakeLists.txt.template | 95 +++++++++++++++++++++++++++++++++++---- 1 file changed, 87 insertions(+), 8 deletions(-) diff --git a/templates/CMakeLists.txt.template b/templates/CMakeLists.txt.template index 028c1b8c32..7868d41229 100644 --- a/templates/CMakeLists.txt.template +++ b/templates/CMakeLists.txt.template @@ -40,6 +40,17 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. <%! + + import re + + proto_re = re.compile('(.*)\\.proto') + + def proto_replace_ext(filename, ext): + m = proto_re.match(filename) + if not m: + return filename + return '${_gRPC_PROTO_GENS_DIR}/' + m.group(1) + ext + def get_deps(target_dict): deps = [] if target_dict.get('baselib', False): @@ -140,6 +151,9 @@ if(TARGET libprotoc) set(_gRPC_PROTOBUF_PROTOC_LIBRARIES libprotoc) endif() + if(TARGET protoc) + set(_gRPC_PROTOBUF_PROTOC protoc) + endif() else() message(WARNING "gRPC_PROTOBUF_PROVIDER is \"module\" but PROTOBUF_ROOT_DIR is wrong") endif() @@ -152,6 +166,9 @@ if(TARGET protobuf::libprotoc) set(_gRPC_PROTOBUF_PROTOC_LIBRARIES protobuf::libprotoc) endif() + if(TARGET protobuf::protoc) + set(_gRPC_PROTOBUF_PROTOC protobuf::protoc) + endif() set(_gRPC_FIND_PROTOBUF "if(NOT protobuf_FOUND)\n find_package(protobuf CONFIG)\nendif()") else() find_package(Protobuf MODULE) @@ -192,16 +209,60 @@ if(NOT DEFINED CMAKE_INSTALL_CMAKEDIR) set(CMAKE_INSTALL_CMAKEDIR "<%text>${CMAKE_INSTALL_LIBDIR}/cmake/gRPC") endif() - + + # Create directory for generated .proto files + set(_gRPC_PROTO_GENS_DIR <%text>${CMAKE_BINARY_DIR}/gens) + file(MAKE_DIRECTORY <%text>${_gRPC_PROTO_GENS_DIR}) + + # protobuf_generate_grpc_cpp + # -------------------------- + # + # Add custom commands to process ``.proto`` files to C++ using protoc and + # GRPC plugin:: + # + # protobuf_generate_grpc_cpp [...] + # + # ``ARGN`` + # ``.proto`` files + # + function(protobuf_generate_grpc_cpp) + if(NOT ARGN) + message(SEND_ERROR "Error: PROTOBUF_GENERATE_GRPC_CPP() called without any proto files") + return() + endif() + + set(_protobuf_include_path -I .) + foreach(FIL <%text>${ARGN}) + get_filename_component(ABS_FIL <%text>${FIL} ABSOLUTE) + get_filename_component(FIL_WE <%text>${FIL} NAME_WE) + file(RELATIVE_PATH REL_FIL <%text>${CMAKE_SOURCE_DIR} <%text>${ABS_FIL}) + get_filename_component(REL_DIR <%text>${REL_FIL} DIRECTORY) + set(RELFIL_WE "<%text>${REL_DIR}/${FIL_WE}") + + add_custom_command( + OUTPUT <%text>"${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.cc" + <%text>"${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.h" + <%text>"${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.cc" + <%text>"${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.h" + COMMAND <%text>${_gRPC_PROTOBUF_PROTOC} + ARGS --grpc_out=<%text>${_gRPC_PROTO_GENS_DIR} + --cpp_out=<%text>${_gRPC_PROTO_GENS_DIR} + --plugin=protoc-gen-grpc=$ + <%text>${_protobuf_include_path} + <%text>${REL_FIL} + DEPENDS <%text>${ABS_FIL} <%text>${_gRPC_PROTOBUF_PROTOC} grpc_cpp_plugin + WORKING_DIRECTORY <%text>${CMAKE_SOURCE_DIR} + COMMENT "Running gRPC C++ protocol buffer compiler on <%text>${FIL}" + VERBATIM) + + <%text>set_source_files_properties("${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.cc" "${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.h" "${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.cc" "${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.h" PROPERTIES GENERATED TRUE) + endforeach() + endfunction() + % for lib in libs: % if lib.build in ["all", "protoc", "tool"] and lib.language in ['c', 'c++']: - ## TODO(jtattermusch): grpc++_reflection includes .proto files - ## which is not yet supported and thus fails the entire build. - ## Re-enable once fixed. - % if lib.name != 'grpc++_reflection': - ${cc_library(lib)} - ${cc_install(lib)} - % endif + ${cc_library(lib)} + ${cc_install(lib)} % endif % endfor @@ -215,9 +276,24 @@ <%def name="cc_library(lib)"> add_library(${lib.name} % for src in lib.src: + % if not proto_re.match(src): ${src} + % else: + ${proto_replace_ext(src, '.pb.cc')} + ${proto_replace_ext(src, '.grpc.pb.cc')} + ${proto_replace_ext(src, '.pb.h')} + ${proto_replace_ext(src, '.grpc.pb.h')} + % endif % endfor ) + + % for src in lib.src: + % if proto_re.match(src): + protobuf_generate_grpc_cpp( + ${src} + ) + % endif + % endfor target_include_directories(${lib.name} PRIVATE <%text>${CMAKE_CURRENT_SOURCE_DIR} @@ -226,6 +302,9 @@ PRIVATE <%text>${PROTOBUF_ROOT_DIR}/src PRIVATE <%text>${ZLIB_INCLUDE_DIR} PRIVATE <%text>${CMAKE_CURRENT_BINARY_DIR}/third_party/zlib + % if any(proto_re.match(src) for src in lib.src): + PRIVATE <%text>${_gRPC_PROTO_GENS_DIR} + % endif ) % if len(get_deps(lib)) > 0: -- cgit v1.2.3 From 54492aa70f366bfe9626f857c08702c5d010988c Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Tue, 17 Jan 2017 11:53:33 +0100 Subject: regenerate --- CMakeLists.txt | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 126 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4627c10e2e..a87e150b53 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -122,6 +122,9 @@ if("${gRPC_PROTOBUF_PROVIDER}" STREQUAL "module") if(TARGET libprotoc) set(_gRPC_PROTOBUF_PROTOC_LIBRARIES libprotoc) endif() + if(TARGET protoc) + set(_gRPC_PROTOBUF_PROTOC protoc) + endif() else() message(WARNING "gRPC_PROTOBUF_PROVIDER is \"module\" but PROTOBUF_ROOT_DIR is wrong") endif() @@ -134,6 +137,9 @@ elseif("${gRPC_PROTOBUF_PROVIDER}" STREQUAL "package") if(TARGET protobuf::libprotoc) set(_gRPC_PROTOBUF_PROTOC_LIBRARIES protobuf::libprotoc) endif() + if(TARGET protobuf::protoc) + set(_gRPC_PROTOBUF_PROTOC protobuf::protoc) + endif() set(_gRPC_FIND_PROTOBUF "if(NOT protobuf_FOUND)\n find_package(protobuf CONFIG)\nendif()") else() find_package(Protobuf MODULE) @@ -175,7 +181,56 @@ if(NOT DEFINED CMAKE_INSTALL_CMAKEDIR) set(CMAKE_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/gRPC") endif() - +# Create directory for generated .proto files +set(_gRPC_PROTO_GENS_DIR ${CMAKE_BINARY_DIR}/gens) +file(MAKE_DIRECTORY ${_gRPC_PROTO_GENS_DIR}) + +# protobuf_generate_grpc_cpp +# -------------------------- +# +# Add custom commands to process ``.proto`` files to C++ using protoc and +# GRPC plugin:: +# +# protobuf_generate_grpc_cpp [...] +# +# ``ARGN`` +# ``.proto`` files +# +function(protobuf_generate_grpc_cpp) + if(NOT ARGN) + message(SEND_ERROR "Error: PROTOBUF_GENERATE_GRPC_CPP() called without any proto files") + return() + endif() + + set(_protobuf_include_path -I .) + foreach(FIL ${ARGN}) + get_filename_component(ABS_FIL ${FIL} ABSOLUTE) + get_filename_component(FIL_WE ${FIL} NAME_WE) + file(RELATIVE_PATH REL_FIL ${CMAKE_SOURCE_DIR} ${ABS_FIL}) + get_filename_component(REL_DIR ${REL_FIL} DIRECTORY) + set(RELFIL_WE "${REL_DIR}/${FIL_WE}") + + add_custom_command( + OUTPUT "${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.cc" + "${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.h" + "${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.cc" + "${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.h" + COMMAND ${_gRPC_PROTOBUF_PROTOC} + ARGS --grpc_out=${_gRPC_PROTO_GENS_DIR} + --cpp_out=${_gRPC_PROTO_GENS_DIR} + --plugin=protoc-gen-grpc=$ + ${_protobuf_include_path} + ${REL_FIL} + DEPENDS ${ABS_FIL} ${_gRPC_PROTOBUF_PROTOC} grpc_cpp_plugin + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + COMMENT "Running gRPC C++ protocol buffer compiler on ${FIL}" + VERBATIM) + + set_source_files_properties("${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.cc" "${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.grpc.pb.h" "${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.cc" "${_gRPC_PROTO_GENS_DIR}/${RELFIL_WE}.pb.h" PROPERTIES GENERATED TRUE) + endforeach() +endfunction() + + add_library(gpr src/core/lib/profiling/basic_timers.c src/core/lib/profiling/stap_timers.c @@ -223,6 +278,7 @@ add_library(gpr src/core/lib/support/wrap_memcpy.c ) + target_include_directories(gpr PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include @@ -279,7 +335,7 @@ foreach(_hdr ) endforeach() - + if (gRPC_INSTALL) install(TARGETS gpr EXPORT gRPCTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} @@ -288,7 +344,7 @@ if (gRPC_INSTALL) ) endif() - + add_library(grpc src/core/lib/surface/init.c src/core/lib/channel/channel_args.c @@ -509,6 +565,7 @@ add_library(grpc src/core/plugin_registry/grpc_plugin_registry.c ) + target_include_directories(grpc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include @@ -563,7 +620,7 @@ foreach(_hdr ) endforeach() - + if (gRPC_INSTALL) install(TARGETS grpc EXPORT gRPCTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} @@ -572,7 +629,7 @@ if (gRPC_INSTALL) ) endif() - + add_library(grpc_cronet src/core/lib/surface/init.c src/core/lib/channel/channel_args.c @@ -764,6 +821,7 @@ add_library(grpc_cronet src/core/plugin_registry/grpc_cronet_plugin_registry.c ) + target_include_directories(grpc_cronet PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include @@ -817,7 +875,7 @@ foreach(_hdr ) endforeach() - + if (gRPC_INSTALL) install(TARGETS grpc_cronet EXPORT gRPCTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} @@ -826,7 +884,7 @@ if (gRPC_INSTALL) ) endif() - + add_library(grpc_unsecure src/core/lib/surface/init.c src/core/lib/surface/init_unsecure.c @@ -1019,6 +1077,7 @@ add_library(grpc_unsecure src/core/plugin_registry/grpc_unsecure_plugin_registry.c ) + target_include_directories(grpc_unsecure PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include @@ -1070,7 +1129,7 @@ foreach(_hdr ) endforeach() - + if (gRPC_INSTALL) install(TARGETS grpc_unsecure EXPORT gRPCTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} @@ -1079,7 +1138,7 @@ if (gRPC_INSTALL) ) endif() - + add_library(grpc++ src/cpp/client/insecure_credentials.cc src/cpp/client/secure_credentials.cc @@ -1120,6 +1179,7 @@ add_library(grpc++ src/cpp/codegen/codegen_init.cc ) + target_include_directories(grpc++ PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include @@ -1233,7 +1293,7 @@ foreach(_hdr ) endforeach() - + if (gRPC_INSTALL) install(TARGETS grpc++ EXPORT gRPCTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} @@ -1242,7 +1302,7 @@ if (gRPC_INSTALL) ) endif() - + add_library(grpc++_cronet src/cpp/client/cronet_credentials.cc src/cpp/client/insecure_credentials.cc @@ -1456,6 +1516,7 @@ add_library(grpc++_cronet third_party/nanopb/pb_encode.c ) + target_include_directories(grpc++_cronet PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include @@ -1579,7 +1640,7 @@ foreach(_hdr ) endforeach() - + if (gRPC_INSTALL) install(TARGETS grpc++_cronet EXPORT gRPCTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} @@ -1588,7 +1649,54 @@ if (gRPC_INSTALL) ) endif() - + +add_library(grpc++_reflection + src/cpp/ext/proto_server_reflection.cc + src/cpp/ext/proto_server_reflection_plugin.cc + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/reflection/v1alpha/reflection.pb.cc + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/reflection/v1alpha/reflection.grpc.pb.cc + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/reflection/v1alpha/reflection.pb.h + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/reflection/v1alpha/reflection.grpc.pb.h +) + +protobuf_generate_grpc_cpp( + src/proto/grpc/reflection/v1alpha/reflection.proto +) + +target_include_directories(grpc++_reflection + PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} + PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include + PRIVATE ${BORINGSSL_ROOT_DIR}/include + PRIVATE ${PROTOBUF_ROOT_DIR}/src + PRIVATE ${ZLIB_INCLUDE_DIR} + PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/third_party/zlib + PRIVATE ${_gRPC_PROTO_GENS_DIR} +) + +target_link_libraries(grpc++_reflection + grpc++ +) + +foreach(_hdr + include/grpc++/ext/proto_server_reflection_plugin.h +) + string(REPLACE "include/" "" _path ${_hdr}) + get_filename_component(_path ${_path} PATH) + install(FILES ${_hdr} + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${_path}" + ) +endforeach() + + +if (gRPC_INSTALL) + install(TARGETS grpc++_reflection EXPORT gRPCTargets + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + ) +endif() + + add_library(grpc++_unsecure src/cpp/client/insecure_credentials.cc src/cpp/common/insecure_create_auth_context.cc @@ -1624,6 +1732,7 @@ add_library(grpc++_unsecure src/cpp/codegen/codegen_init.cc ) + target_include_directories(grpc++_unsecure PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include @@ -1737,7 +1846,7 @@ foreach(_hdr ) endforeach() - + if (gRPC_INSTALL) install(TARGETS grpc++_unsecure EXPORT gRPCTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} @@ -1746,7 +1855,7 @@ if (gRPC_INSTALL) ) endif() - + add_library(grpc_plugin_support src/compiler/cpp_generator.cc src/compiler/csharp_generator.cc @@ -1757,6 +1866,7 @@ add_library(grpc_plugin_support src/compiler/ruby_generator.cc ) + target_include_directories(grpc_plugin_support PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include @@ -1780,7 +1890,7 @@ foreach(_hdr ) endforeach() - + if (gRPC_INSTALL) install(TARGETS grpc_plugin_support EXPORT gRPCTargets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} -- cgit v1.2.3