Skip to content

Commit

Permalink
Modularize protobuf compile functions
Browse files Browse the repository at this point in the history
- Move generate_proto_files() and generate_grpc_files()
  function definitions to ProtobufCompile.cmake.

Signed-off-by: Derek G Foster <[email protected]>
  • Loading branch information
ffoulkes committed Sep 27, 2023
1 parent 047b2e7 commit bfe359f
Show file tree
Hide file tree
Showing 3 changed files with 323 additions and 117 deletions.
133 changes: 133 additions & 0 deletions cmake/ProtobufCompile.cmake
Original file line number Diff line number Diff line change
@@ -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)
128 changes: 11 additions & 117 deletions stratum/proto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

include(FindProtobuf)

option(INSTALL_PROTO_FILES "Install .proto files" OFF)

#############################
# Find Protobuf executables #
#############################
Expand All @@ -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})

Expand All @@ -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 #
Expand Down
Loading

0 comments on commit bfe359f

Please sign in to comment.