FindProtobuf: Fix protobuf_generate COMMENT with multiple PROTOC_OPTIONS

With two or more PROTOC_OPTIONS the custom command's comment picked up
the raw list separator, so the unquoted COMMENT expanded to several
arguments. Since CMP0175 that's a configure error under NEW and a
policy warning otherwise, and the comment only ever showed the last
option.

We're now joining the options with spaces before they go into the
comment, which is how upstream protobuf fixed the same code in v33.0
(protocolbuffers/protobuf@aa1f665cdc). The COMMENT argument is also
quoted now, and the module sets CMP0175 to NEW for its own
add_custom_command() call instead of inheriting the project's setting.
That keeps a stray separator from splitting the comment again, and it
stops the policy warning that pointed into this module.

Fixes: #28078
This commit is contained in:
15daksh-2003 committed 2026-09-25 22:43:49 +05:30
1 parent 356af0e38a
commit 5a082c7dde
2 files changed
+18 -3

No files matched your search

+6 -3
View File
@@ -579,6 +579,7 @@ using a deprecated command and its modern replacement:
cmake_policy(PUSH)
cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
cmake_policy(SET CMP0175 NEW) # add_custom_command() rejects invalid arguments
function(protobuf_generate)
set(_options APPEND_PATH DESCRIPTORS)
@@ -723,10 +724,12 @@ function(protobuf_generate)
set(_comment "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}")
if(protobuf_generate_PROTOC_OPTIONS)
set(_comment "${_comment}, protoc-options: ${protobuf_generate_PROTOC_OPTIONS}")
string(REPLACE ";" " " _protoc_options_str "${protobuf_generate_PROTOC_OPTIONS}")
set(_comment "${_comment}, protoc-options: ${_protoc_options_str}")
endif()
if(_plugin_options)
set(_comment "${_comment}, plugin-options: ${_plugin_options}")
string(REPLACE ";" " " _plugin_options_str "${_plugin_options}")
set(_comment "${_comment}, plugin-options: ${_plugin_options_str}")
endif()
add_custom_command(
@@ -734,7 +737,7 @@ function(protobuf_generate)
COMMAND ${protobuf_generate_PROTOC_EXE}
ARGS ${protobuf_generate_PROTOC_OPTIONS} --${protobuf_generate_LANGUAGE}_out ${_plugin_options}:${protobuf_generate_PROTOC_OUT_DIR} ${_plugin} ${_dll_desc_out} ${_protobuf_include_path} ${_abs_file}
DEPENDS ${_abs_file} protobuf::protoc ${protobuf_generate_DEPENDENCIES}
COMMENT ${_comment}
COMMENT "${_comment}"
VERBATIM )
endforeach()
+12
View File
@@ -53,6 +53,18 @@ target_link_libraries(test_desc msgs ${Protobuf_LIBRARIES})
target_compile_features(test_desc PRIVATE cxx_std_11)
add_test(NAME test_desc COMMAND test_desc ${DESC_PROTO_DESC})
# Multiple protoc and plugin options must still give add_custom_command a
# single COMMENT. The speed option is already this file's default.
add_library(msgs_protoc_options msgs/example.proto)
# NOTE: generated files will be placed under ${CMAKE_CURRENT_BINARY_DIR}/msgs/
target_include_directories(msgs_protoc_options PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(msgs_protoc_options PUBLIC ${Protobuf_LIBRARIES})
target_compile_definitions(msgs_protoc_options PRIVATE PROTO_EXPORT=)
target_compile_features(msgs_protoc_options PRIVATE cxx_std_11)
protobuf_generate(TARGET msgs_protoc_options DESCRIPTORS
EXPORT_MACRO PROTO_EXPORT PLUGIN_OPTIONS speed
PROTOC_OPTIONS --include_imports --include_source_info)
if(CMake_TEST_FindProtobuf_gRPC)
find_program(gRPC_CPP_PLUGIN grpc_cpp_plugin)
add_library(msgs_grpc msgs/grpc/example_service.proto)