From 3163111b6fb1da08b9d6ad75518d91b89cd03bbf Mon Sep 17 00:00:00 2001 From: Jonathan Ringer Date: Tue, 24 Sep 2024 13:50:29 -0700 Subject: [PATCH] [CMake] Allow for protoc executable to be configured (#17888) For cross-compilation, we may not want to re-use the same protoc which would be used during a CMake build (these would differ in hostPlatform). Instead, allow for invocations of `protobuf_generate` pass their preferred programs, similar to how you can do this for grpc plugins. Optional proposal: Use `find_program`, however, this is a pretty big behavior change, this PR allows you to "opt-out" of the legacy behavior, but users shouldn't see a difference unless they opt-in to passing their own protoc-exe command/path Closes #17888 COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/17888 from jonringer:configure-protoc-cmake 9ca38aee11adc88916340c5e3b49957dd1cf2110 PiperOrigin-RevId: 678383523 --- cmake/protobuf-generate.cmake | 9 ++++++-- docs/cmake_protobuf_generate.md | 40 +++++++++++++++++++++++---------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/cmake/protobuf-generate.cmake b/cmake/protobuf-generate.cmake index 1956121f55..244407ee2f 100644 --- a/cmake/protobuf-generate.cmake +++ b/cmake/protobuf-generate.cmake @@ -2,7 +2,7 @@ function(protobuf_generate) include(CMakeParseArguments) set(_options APPEND_PATH) - set(_singleargs LANGUAGE OUT_VAR EXPORT_MACRO PROTOC_OUT_DIR PLUGIN PLUGIN_OPTIONS DEPENDENCIES) + set(_singleargs LANGUAGE OUT_VAR EXPORT_MACRO PROTOC_OUT_DIR PLUGIN PLUGIN_OPTIONS DEPENDENCIES PROTOC_EXE) if(COMMAND target_sources) list(APPEND _singleargs TARGET) endif() @@ -83,6 +83,11 @@ function(protobuf_generate) endforeach() endif() + if(NOT protobuf_generate_PROTOC_EXE) + # Default to using the CMake executable + set(protobuf_generate_PROTOC_EXE protobuf::protoc) + endif() + foreach(DIR ${protobuf_generate_IMPORT_DIRS}) get_filename_component(ABS_PATH ${DIR} ABSOLUTE) list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) @@ -143,7 +148,7 @@ function(protobuf_generate) add_custom_command( OUTPUT ${_generated_srcs} - COMMAND protobuf::protoc + COMMAND ${protobuf_generate_PROTOC_EXE} 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_EXE} ${protobuf_generate_DEPENDENCIES} COMMENT ${_comment} diff --git a/docs/cmake_protobuf_generate.md b/docs/cmake_protobuf_generate.md index d3e9110ebc..0d07f08d9a 100644 --- a/docs/cmake_protobuf_generate.md +++ b/docs/cmake_protobuf_generate.md @@ -118,18 +118,34 @@ Flag arguments: Single-value arguments: -- `LANGUAGE` — A single value: cpp or python. Determines what kind of source files are being generated. -- `OUT_VAR` — Name of a CMake variable that will be filled with the paths to the generated source files. -- `EXPORT_MACRO` — Name of a macro that is applied to all generated Protobuf message classes and extern variables. It can, for example, be used to declare DLL exports. -- `PROTOC_OUT_DIR` — Output directory of generated source files. Defaults to `CMAKE_CURRENT_BINARY_DIR`. -- `PLUGIN` — An optional plugin executable. This could, for example, be the path to `grpc_cpp_plugin`. -- `PLUGIN_OPTIONS` — Additional options provided to the plugin, such as `generate_mock_code=true` for the gRPC cpp plugin. -- `DEPENDENCIES` — Arguments forwarded to the `DEPENDS` of the underlying `add_custom_command` invocation. -- `TARGET` — CMake target that will have the generated files added as sources. +- `LANGUAGE` — A single value: cpp or python. Determines what kind of source + files are being generated. +- `OUT_VAR` — Name of a CMake variable that will be filled with the paths to + the generated source files. +- `EXPORT_MACRO` — Name of a macro that is applied to all generated Protobuf + message classes and extern variables. It can, for example, be used to + declare DLL exports. +- `PROTOC_EXE` — Command name, path, or CMake executable used to run protoc + commands. Defaults to `protobuf::protoc`. +- `PROTOC_OUT_DIR` — Output directory of generated source files. Defaults to + `CMAKE_CURRENT_BINARY_DIR`. +- `PLUGIN` — An optional plugin executable. This could, for example, be the + path to `grpc_cpp_plugin`. +- `PLUGIN_OPTIONS` — Additional options provided to the plugin, such as + `generate_mock_code=true` for the gRPC cpp plugin. +- `DEPENDENCIES` — Arguments forwarded to the `DEPENDS` of the underlying + `add_custom_command` invocation. +- `TARGET` — CMake target that will have the generated files added as sources. Multi-value arguments: -- `PROTOS` — List of proto schema files. If omitted, then every source file ending in *proto* of `TARGET` will be used. -- `IMPORT_DIRS` — A common parent directory for the schema files. For example, if the schema file is `proto/helloworld/helloworld.proto` and the import directory `proto/` then the generated files are `${PROTOC_OUT_DIR}/helloworld/helloworld.pb.h` and `${PROTOC_OUT_DIR}/helloworld/helloworld.pb.cc`. -- `GENERATE_EXTENSIONS` — If LANGUAGE is omitted then this must be set to the extensions that protoc generates. -- `PROTOC_OPTIONS` — Additional arguments that are forwarded to protoc. \ No newline at end of file +- `PROTOS` — List of proto schema files. If omitted, then every source file + ending in *proto* of `TARGET` will be used. +- `IMPORT_DIRS` — A common parent directory for the schema files. For example, + if the schema file is `proto/helloworld/helloworld.proto` and the import + directory `proto/` then the generated files are + `${PROTOC_OUT_DIR}/helloworld/helloworld.pb.h` and + `${PROTOC_OUT_DIR}/helloworld/helloworld.pb.cc`. +- `GENERATE_EXTENSIONS` — If LANGUAGE is omitted then this must be set to the + extensions that protoc generates. +- `PROTOC_OPTIONS` — Additional arguments that are forwarded to protoc.