From ecdd1c915e8a882d56e6dde48ae718cc8bf57a9a Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Tue, 15 Oct 2019 17:32:02 -0400 Subject: [PATCH] De-duplicate .proto file processing .proto files that are used by more than one target have their code files generated multiple times, which causes issues with add_custom_command(). De-duplicate them so they're only generated once. --- templates/CMakeLists.txt.template | 37 ++++++++++++++++++------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/templates/CMakeLists.txt.template b/templates/CMakeLists.txt.template index 6c9eec351e2..cda9f76ad64 100644 --- a/templates/CMakeLists.txt.template +++ b/templates/CMakeLists.txt.template @@ -68,6 +68,21 @@ return '' return 'endif()\n' %> + <% + # These files are added to a set so that they are not duplicated if multiple + # targets use them. Generating the same file multiple times with + # add_custom_command() is not allowed in CMake. + + protobuf_gen_files = set() + for tgt in targets: + for src in tgt.src: + if proto_re.match(src): + protobuf_gen_files.add(src) + for lib in libs: + for src in lib.src: + if proto_re.match(src): + protobuf_gen_files.add(src) + %> cmake_minimum_required(VERSION 3.5.1) @@ -295,6 +310,12 @@ add_custom_target(tools DEPENDS tools_c tools_cxx) + % for src in sorted(protobuf_gen_files): + protobuf_generate_grpc_cpp( + ${src} + ) + % endfor + if (gRPC_BUILD_TESTS) add_custom_target(buildtests_c) % for tgt in targets: @@ -411,14 +432,6 @@ endif() endif() - % for src in lib.src: - % if proto_re.match(src): - protobuf_generate_grpc_cpp( - ${src} - ) - % endif - % endfor - target_include_directories(${lib.name} PUBLIC <%text>$ $ PRIVATE <%text>${CMAKE_CURRENT_SOURCE_DIR} @@ -501,14 +514,6 @@ % endif ) - % for src in tgt.src: - % if proto_re.match(src): - protobuf_generate_grpc_cpp( - ${src} - ) - % endif - % endfor - target_include_directories(${tgt.name} PRIVATE <%text>${CMAKE_CURRENT_SOURCE_DIR} PRIVATE <%text>${CMAKE_CURRENT_SOURCE_DIR}/include