From 592ba817babad8deea11d7b0bbb10e7607693049 Mon Sep 17 00:00:00 2001 From: Derek G Foster Date: Fri, 29 Sep 2023 11:41:41 -0700 Subject: [PATCH] Modularize protobuf compile functions (#300) - Move generate_proto_files() and generate_grpc_files() function definitions to ProtobufCompile.cmake. Signed-off-by: Derek G Foster --- cmake/ProtobufCompile.cmake | 133 +++++++++++++++++++++++++++++++++++ stratum/proto/CMakeLists.txt | 128 +++------------------------------ 2 files changed, 144 insertions(+), 117 deletions(-) create mode 100644 cmake/ProtobufCompile.cmake diff --git a/cmake/ProtobufCompile.cmake b/cmake/ProtobufCompile.cmake new file mode 100644 index 00000000..658fc57d --- /dev/null +++ b/cmake/ProtobufCompile.cmake @@ -0,0 +1,133 @@ +# ProtobufCompile.cmake +# +# Copyright 2022-2023 Intel Corporation +# SPDX-License-Identifier: Apache 2.0 +# +# Compiles .proto files to C++ +# + +#----------------------------------------------------------------------- +# The following variables are defined externally: +# +# DOT_PROTO_INSTALL_DIR +# Directory in which the input .proto files should be installed +# HOST_GRPC_CPP_PLUGIN +# Path to the host grpc_cpp_plugin executable +# HOST_PROTOC +# Path to the host protobuf compiler +# PB_HEADER_INSTALL_DIR +# Directory in which the generated pb.h files should be installed +# PB_OUT_DIR +# Directory in which the pb.cc and pb.h files should be generated +# PROTO_IMPORT_PATH +# Directory paths to be searched for input Protobuf files +# STRATUM_SOURCE_DIR +# Path to the root Stratum source directory +#----------------------------------------------------------------------- + +option(INSTALL_PROTO_FILES "Install .proto files" OFF) + +#----------------------------------------------------------------------- +# generate_proto_files() +# Generates C++ files for protobufs. +#----------------------------------------------------------------------- +function(generate_proto_files PROTO_FILES SRC_DIR) + foreach(_file ${PROTO_FILES}) + get_filename_component(_path ${_file} DIRECTORY) + get_filename_component(_name ${_file} NAME_WE) + + set(_src ${PB_OUT_DIR}/${_path}/${_name}.pb.cc) + set(_hdr ${PB_OUT_DIR}/${_path}/${_name}.pb.h) + + set_source_files_properties(${_src} ${_hdr} PROPERTIES GENERATED TRUE) + + add_custom_command( + OUTPUT + ${_src} ${_hdr} + COMMAND + ${HOST_PROTOC} + --proto_path=${PROTO_IMPORT_PATH} + --cpp_out=${PB_OUT_DIR} + -I${STRATUM_SOURCE_DIR} + ${_file} + WORKING_DIRECTORY + ${CMAKE_CURRENT_SOURCE_DIR} + DEPENDS + ${SRC_DIR}/${_file} + COMMENT + "Generating C++ files for ${_file}" + VERBATIM + ) + + # Install .pb.h file in include/. + install( + FILES + ${_hdr} + DESTINATION + ${PB_HEADER_INSTALL_DIR}/${_path} + ) + + # Install .proto file in share/. + if(INSTALL_PROTO_FILES) + install( + FILES + ${SRC_DIR}/${_file} + DESTINATION + ${DOT_PROTO_INSTALL_DIR}/${_path} + ) + endif() + endforeach() +endfunction(generate_proto_files) + +#----------------------------------------------------------------------- +# generate_grpc_files() +# Generates GRPC C++ files for protobufs. +#----------------------------------------------------------------------- +function(generate_grpc_files PROTO_FILES SRC_DIR) + foreach(_file ${PROTO_FILES}) + get_filename_component(_path ${_file} DIRECTORY) + get_filename_component(_name ${_file} NAME_WE) + + set(_src ${PB_OUT_DIR}/${_path}/${_name}.grpc.pb.cc) + set(_hdr ${PB_OUT_DIR}/${_path}/${_name}.grpc.pb.h) + + set_source_files_properties(${_src} ${_hdr} PROPERTIES GENERATED TRUE) + + add_custom_command( + OUTPUT + ${_src} ${_hdr} + COMMAND + ${HOST_PROTOC} + --proto_path=${PROTO_IMPORT_PATH} + --grpc_out=${PB_OUT_DIR} + --plugin=protoc-gen-grpc=${HOST_GRPC_CPP_PLUGIN} + -I${STRATUM_SOURCE_DIR} + ${_file} + WORKING_DIRECTORY + ${CMAKE_CURRENT_SOURCE_DIR} + DEPENDS + ${SRC_DIR}/${_file} + COMMENT + "Generating grpc files for ${_file}" + VERBATIM + ) + + # Install .pb.h file in include/. + install( + FILES + ${_hdr} + DESTINATION + ${PB_HEADER_INSTALL_DIR}/${_path} + ) + + # Install .proto file in share/. + if(INSTALL_PROTO_FILES) + install( + FILES + ${SRC_DIR}/${_file} + DESTINATION + ${DOT_PROTO_INSTALL_DIR}/${_path} + ) + endif() + endforeach() +endfunction(generate_grpc_files) diff --git a/stratum/proto/CMakeLists.txt b/stratum/proto/CMakeLists.txt index e936f316..46c7ac27 100644 --- a/stratum/proto/CMakeLists.txt +++ b/stratum/proto/CMakeLists.txt @@ -6,8 +6,6 @@ include(FindProtobuf) -option(INSTALL_PROTO_FILES "Install .proto files" OFF) - ############################# # Find Protobuf executables # ############################# @@ -17,28 +15,29 @@ option(INSTALL_PROTO_FILES "Install .proto files" OFF) # search the cross-compiled binaries for the Protobuf executables. find_package(Protobuf REQUIRED NO_CMAKE_FIND_ROOT_PATH) +set(HOST_PROTOC ${PROTOBUF_PROTOC_EXECUTABLE}) find_package(gRPC NO_CMAKE_FIND_ROOT_PATH) if(gRPC AND TARGET(gRPC::grpc_cpp_plugin)) - get_property(GRPC_CPP_PLUGIN + get_property(HOST_GRPC_CPP_PLUGIN TARGET gRPC::grpc_cpp_plugin PROPERTY IMPORTED_LOCATION_NOCONFIG) else() - find_program(GRPC_CPP_PLUGIN "grpc_cpp_plugin" REQUIRED + find_program(HOST_GRPC_CPP_PLUGIN "grpc_cpp_plugin" REQUIRED NO_CMAKE_FIND_ROOT_PATH) - mark_as_advanced(GRPC_CPP_PLUGIN) - if(NOT GRPC_CPP_PLUGIN) + mark_as_advanced(HOST_GRPC_CPP_PLUGIN) + if(NOT HOST_GRPC_CPP_PLUGIN) message(FATAL_ERROR "grpc_cpp_plugin not found") endif() endif() -cmake_print_variables(PROTOBUF_PROTOC_EXECUTABLE) -cmake_print_variables(GRPC_CPP_PLUGIN) +cmake_print_variables(HOST_PROTOC) +cmake_print_variables(HOST_GRPC_CPP_PLUGIN) -########################## -# Compile protobuf files # -########################## +############################# +# Define Generate functions # +############################# set(PROTO_PARENT_DIR ${CMAKE_CURRENT_SOURCE_DIR}) @@ -54,112 +53,7 @@ string(JOIN ":" PROTO_IMPORT_PATH set(PB_HEADER_INSTALL_DIR ${CMAKE_INSTALL_INCLUDEDIR}/stratum/pb) set(DOT_PROTO_INSTALL_DIR ${CMAKE_INSTALL_DATAROOTDIR}/stratum/proto) -######################## -# generate_proto_files # -######################## - -# Generates C++ files for protobufs. -function(generate_proto_files PROTO_FILES SRC_DIR) - foreach(_file ${PROTO_FILES}) - get_filename_component(_path ${_file} DIRECTORY) - get_filename_component(_name ${_file} NAME_WE) - - set(_src ${PB_OUT_DIR}/${_path}/${_name}.pb.cc) - set(_hdr ${PB_OUT_DIR}/${_path}/${_name}.pb.h) - - set_source_files_properties(${_src} ${_hdr} PROPERTIES GENERATED TRUE) - - add_custom_command( - OUTPUT - ${_src} ${_hdr} - COMMAND - ${PROTOBUF_PROTOC_EXECUTABLE} - --proto_path=${PROTO_IMPORT_PATH} - --cpp_out=${PB_OUT_DIR} - -I${STRATUM_SOURCE_DIR} - ${_file} - WORKING_DIRECTORY - ${CMAKE_CURRENT_SOURCE_DIR} - DEPENDS - ${SRC_DIR}/${_file} - COMMENT - "Generating C++ files for ${_file}" - VERBATIM - ) - - # Install .pb.h file in include/. - install( - FILES - ${_hdr} - DESTINATION - ${PB_HEADER_INSTALL_DIR}/${_path} - ) - - # Install .proto file in share/. - if(INSTALL_PROTO_FILES) - install( - FILES - ${SRC_DIR}/${_file} - DESTINATION - ${DOT_PROTO_INSTALL_DIR}/${_path} - ) - endif() - endforeach() -endfunction(generate_proto_files) - -####################### -# generate_grpc_files # -####################### - -# Generates GRPC C++ files for protobufs. -function(generate_grpc_files PROTO_FILES SRC_DIR) - foreach(_file ${PROTO_FILES}) - get_filename_component(_path ${_file} DIRECTORY) - get_filename_component(_name ${_file} NAME_WE) - - set(_src ${PB_OUT_DIR}/${_path}/${_name}.grpc.pb.cc) - set(_hdr ${PB_OUT_DIR}/${_path}/${_name}.grpc.pb.h) - - set_source_files_properties(${_src} ${_hdr} PROPERTIES GENERATED TRUE) - - add_custom_command( - OUTPUT - ${_src} ${_hdr} - COMMAND - ${PROTOBUF_PROTOC_EXECUTABLE} - --proto_path=${PROTO_IMPORT_PATH} - --grpc_out=${PB_OUT_DIR} - --plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN} - -I${STRATUM_SOURCE_DIR} - ${_file} - WORKING_DIRECTORY - ${CMAKE_CURRENT_SOURCE_DIR} - DEPENDS - ${SRC_DIR}/${_file} - COMMENT - "Generating grpc files for ${_file}" - VERBATIM - ) - - # Install .pb.h file in include/. - install( - FILES - ${_hdr} - DESTINATION - ${PB_HEADER_INSTALL_DIR}/${_path} - ) - - # Install .proto file in share/. - if(INSTALL_PROTO_FILES) - install( - FILES - ${SRC_DIR}/${_file} - DESTINATION - ${DOT_PROTO_INSTALL_DIR}/${_path} - ) - endif() - endforeach() -endfunction(generate_grpc_files) +include(ProtobufCompile) ####################### # Build libgrpc_proto #