Skip to content

Commit

Permalink
[CMake] Allow custom plugin options e.g. to generate mocks (#9105)
Browse files Browse the repository at this point in the history
* add plugin options

* refactored comment message

* added tabs for better readability

* removed newlines as they are not working properly

* allow PROTOC_OPTIONS and EXPORT_MACRO in parallel for protobuf_generate
  • Loading branch information
tiolan committed Feb 9, 2022
1 parent ce731e2 commit 4ed3941
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions cmake/protobuf-config.cmake.in
Expand Up @@ -11,7 +11,7 @@ function(protobuf_generate)
include(CMakeParseArguments)

set(_options APPEND_PATH)
set(_singleargs LANGUAGE OUT_VAR EXPORT_MACRO PROTOC_OUT_DIR PLUGIN)
set(_singleargs LANGUAGE OUT_VAR EXPORT_MACRO PROTOC_OUT_DIR PLUGIN PLUGIN_OPTIONS)
if(COMMAND target_sources)
list(APPEND _singleargs TARGET)
endif()
Expand Down Expand Up @@ -39,9 +39,18 @@ function(protobuf_generate)
endif()

if(protobuf_generate_EXPORT_MACRO AND protobuf_generate_LANGUAGE STREQUAL cpp)
set(_dll_export_decl "dllexport_decl=${protobuf_generate_EXPORT_MACRO}:")
set(_dll_export_decl "dllexport_decl=${protobuf_generate_EXPORT_MACRO}")
endif()


foreach(_option ${_dll_export_decl} ${protobuf_generate_PLUGIN_OPTIONS})
# append comma - not using CMake lists and string replacement as users
# might have semicolons in options
if(_plugin_options)
set( _plugin_options "${_plugin_options},")
endif()
set(_plugin_options "${_plugin_options}${_option}")
endforeach()

if(protobuf_generate_PLUGIN)
set(_plugin "--plugin=${protobuf_generate_PLUGIN}")
endif()
Expand Down Expand Up @@ -127,12 +136,20 @@ function(protobuf_generate)
endforeach()
list(APPEND _generated_srcs_all ${_generated_srcs})

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}")
endif()
if(_plugin_options)
set(_comment "${_comment}, plugin-options: ${_plugin_options}")
endif()

add_custom_command(
OUTPUT ${_generated_srcs}
COMMAND protobuf::protoc
ARGS ${protobuf_generate_PROTOC_OPTIONS} --${protobuf_generate_LANGUAGE}_out ${_dll_export_decl}${protobuf_generate_PROTOC_OUT_DIR} ${_plugin} ${_protobuf_include_path} ${_abs_file}
COMMAND protobuf::protoc
ARGS ${protobuf_generate_PROTOC_OPTIONS} --${protobuf_generate_LANGUAGE}_out ${_plugin_options}:${protobuf_generate_PROTOC_OUT_DIR} ${_plugin} ${_protobuf_include_path} ${_abs_file}
DEPENDS ${_abs_file} protobuf::protoc
COMMENT "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}. Custom options: ${protobuf_generate_PROTOC_OPTIONS}"
COMMENT ${_comment}
VERBATIM )
endforeach()

Expand Down

0 comments on commit 4ed3941

Please sign in to comment.