mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-14 03:25:15 +00:00
1ef5bca591
even if input ".proto" files are located in sibling directories.
230 lines
8.3 KiB
CMake
230 lines
8.3 KiB
CMake
# The code in this file was copied from https://github.com/Kitware/CMake/blob/master/Modules/FindProtobuf.cmake
|
|
|
|
#[[
|
|
Add custom commands to process ``.proto`` files to C++::
|
|
|
|
protobuf_generate_cpp (<SRCS> <HDRS>
|
|
[DESCRIPTORS <DESC>] [EXPORT_MACRO <MACRO>] [<ARGN>...])
|
|
|
|
``SRCS``
|
|
Variable to define with autogenerated source files
|
|
``HDRS``
|
|
Variable to define with autogenerated header files
|
|
``DESCRIPTORS``
|
|
Variable to define with autogenerated descriptor files, if requested.
|
|
``EXPORT_MACRO``
|
|
is a macro which should expand to ``__declspec(dllexport)`` or
|
|
``__declspec(dllimport)`` depending on what is being compiled.
|
|
``ARGN``
|
|
``.proto`` files
|
|
#]]
|
|
|
|
function(PROTOBUF_GENERATE_CPP SRCS HDRS)
|
|
cmake_parse_arguments(protobuf_generate_cpp "" "EXPORT_MACRO;DESCRIPTORS" "" ${ARGN})
|
|
|
|
set(_proto_files "${protobuf_generate_cpp_UNPARSED_ARGUMENTS}")
|
|
if(NOT _proto_files)
|
|
message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
|
|
return()
|
|
endif()
|
|
|
|
if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
|
|
set(_append_arg APPEND_PATH)
|
|
endif()
|
|
|
|
if(protobuf_generate_cpp_DESCRIPTORS)
|
|
set(_descriptors DESCRIPTORS)
|
|
endif()
|
|
|
|
if(DEFINED PROTOBUF_IMPORT_DIRS AND NOT DEFINED Protobuf_IMPORT_DIRS)
|
|
set(Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}")
|
|
endif()
|
|
|
|
if(DEFINED Protobuf_IMPORT_DIRS)
|
|
set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS})
|
|
endif()
|
|
|
|
set(_outvar)
|
|
protobuf_generate(${_append_arg} ${_descriptors} LANGUAGE cpp EXPORT_MACRO ${protobuf_generate_cpp_EXPORT_MACRO} OUT_VAR _outvar ${_import_arg} PROTOS ${_proto_files})
|
|
|
|
set(${SRCS})
|
|
set(${HDRS})
|
|
if(protobuf_generate_cpp_DESCRIPTORS)
|
|
set(${protobuf_generate_cpp_DESCRIPTORS})
|
|
endif()
|
|
|
|
foreach(_file ${_outvar})
|
|
if(_file MATCHES "cc$")
|
|
list(APPEND ${SRCS} ${_file})
|
|
elseif(_file MATCHES "desc$")
|
|
list(APPEND ${protobuf_generate_cpp_DESCRIPTORS} ${_file})
|
|
else()
|
|
list(APPEND ${HDRS} ${_file})
|
|
endif()
|
|
endforeach()
|
|
set(${SRCS} ${${SRCS}} PARENT_SCOPE)
|
|
set(${HDRS} ${${HDRS}} PARENT_SCOPE)
|
|
if(protobuf_generate_cpp_DESCRIPTORS)
|
|
set(${protobuf_generate_cpp_DESCRIPTORS} "${${protobuf_generate_cpp_DESCRIPTORS}}" PARENT_SCOPE)
|
|
endif()
|
|
endfunction()
|
|
|
|
# By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc
|
|
# for each directory where a proto file is referenced.
|
|
if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
|
|
set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
|
|
endif()
|
|
|
|
function(protobuf_generate)
|
|
set(_options APPEND_PATH DESCRIPTORS)
|
|
set(_singleargs LANGUAGE OUT_VAR EXPORT_MACRO PROTOC_OUT_DIR)
|
|
if(COMMAND target_sources)
|
|
list(APPEND _singleargs TARGET)
|
|
endif()
|
|
set(_multiargs PROTOS IMPORT_DIRS GENERATE_EXTENSIONS)
|
|
|
|
cmake_parse_arguments(protobuf_generate "${_options}" "${_singleargs}" "${_multiargs}" "${ARGN}")
|
|
|
|
if(NOT protobuf_generate_PROTOS AND NOT protobuf_generate_TARGET)
|
|
message(SEND_ERROR "Error: protobuf_generate called without any targets or source files")
|
|
return()
|
|
endif()
|
|
|
|
if(NOT protobuf_generate_OUT_VAR AND NOT protobuf_generate_TARGET)
|
|
message(SEND_ERROR "Error: protobuf_generate called without a target or output variable")
|
|
return()
|
|
endif()
|
|
|
|
if(NOT protobuf_generate_LANGUAGE)
|
|
set(protobuf_generate_LANGUAGE cpp)
|
|
endif()
|
|
string(TOLOWER ${protobuf_generate_LANGUAGE} protobuf_generate_LANGUAGE)
|
|
|
|
if(NOT protobuf_generate_PROTOC_OUT_DIR)
|
|
set(protobuf_generate_PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
|
|
endif()
|
|
|
|
if(protobuf_generate_EXPORT_MACRO AND protobuf_generate_LANGUAGE STREQUAL cpp)
|
|
set(_dll_export_decl "dllexport_decl=${protobuf_generate_EXPORT_MACRO}:")
|
|
endif()
|
|
|
|
if(NOT protobuf_generate_GENERATE_EXTENSIONS)
|
|
if(protobuf_generate_LANGUAGE STREQUAL cpp)
|
|
set(protobuf_generate_GENERATE_EXTENSIONS .pb.h .pb.cc)
|
|
elseif(protobuf_generate_LANGUAGE STREQUAL python)
|
|
set(protobuf_generate_GENERATE_EXTENSIONS _pb2.py)
|
|
else()
|
|
message(SEND_ERROR "Error: protobuf_generate given unknown Language ${LANGUAGE}, please provide a value for GENERATE_EXTENSIONS")
|
|
return()
|
|
endif()
|
|
endif()
|
|
|
|
if(protobuf_generate_TARGET)
|
|
get_target_property(_source_list ${protobuf_generate_TARGET} SOURCES)
|
|
foreach(_file ${_source_list})
|
|
if(_file MATCHES "proto$")
|
|
list(APPEND protobuf_generate_PROTOS ${_file})
|
|
endif()
|
|
endforeach()
|
|
endif()
|
|
|
|
if(NOT protobuf_generate_PROTOS)
|
|
message(SEND_ERROR "Error: protobuf_generate could not find any .proto files")
|
|
return()
|
|
endif()
|
|
|
|
if(protobuf_generate_APPEND_PATH)
|
|
# Create an include path for each file specified
|
|
foreach(_file ${protobuf_generate_PROTOS})
|
|
get_filename_component(_abs_file ${_file} ABSOLUTE)
|
|
get_filename_component(_abs_path ${_abs_file} PATH)
|
|
list(FIND _protobuf_include_path ${_abs_path} _contains_already)
|
|
if(${_contains_already} EQUAL -1)
|
|
list(APPEND _protobuf_include_path -I ${_abs_path})
|
|
endif()
|
|
endforeach()
|
|
else()
|
|
set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
|
|
endif()
|
|
|
|
foreach(DIR ${protobuf_generate_IMPORT_DIRS})
|
|
get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
|
|
list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
|
|
if(${_contains_already} EQUAL -1)
|
|
list(APPEND _protobuf_include_path -I ${ABS_PATH})
|
|
endif()
|
|
endforeach()
|
|
|
|
set(_generated_srcs_all)
|
|
foreach(_proto ${protobuf_generate_PROTOS})
|
|
# The protobuf compiler doesn't return paths to the files it generates so we have to calculate those paths here:
|
|
# _abs_file - absolute path to a .proto file,
|
|
# _possible_rel_dir - relative path to the .proto file from some import directory specified in Protobuf_IMPORT_DIRS,
|
|
# _basename - filename of the .proto file (without path and without extenstion).
|
|
get_proto_absolute_path(_abs_file "${_proto}" ${_protobuf_include_path})
|
|
get_proto_relative_path(_possible_rel_dir "${_abs_file}" ${_protobuf_include_path})
|
|
get_filename_component(_basename "${_abs_file}" NAME_WE)
|
|
|
|
set(_generated_srcs)
|
|
foreach(_ext ${protobuf_generate_GENERATE_EXTENSIONS})
|
|
list(APPEND _generated_srcs "${protobuf_generate_PROTOC_OUT_DIR}/${_possible_rel_dir}${_basename}${_ext}")
|
|
endforeach()
|
|
|
|
if(protobuf_generate_DESCRIPTORS AND protobuf_generate_LANGUAGE STREQUAL cpp)
|
|
set(_descriptor_file "${protobuf_generate_PROTOC_OUT_DIR}/${_possible_rel_dir}${_basename}.desc")
|
|
set(_dll_desc_out "--descriptor_set_out=${_descriptor_file}")
|
|
list(APPEND _generated_srcs ${_descriptor_file})
|
|
endif()
|
|
list(APPEND _generated_srcs_all ${_generated_srcs})
|
|
|
|
add_custom_command(
|
|
OUTPUT ${_generated_srcs}
|
|
COMMAND $<TARGET_FILE:protoc>
|
|
ARGS --${protobuf_generate_LANGUAGE}_out ${_dll_export_decl}${protobuf_generate_PROTOC_OUT_DIR} ${_dll_desc_out} ${_protobuf_include_path} ${_abs_file}
|
|
DEPENDS ${_abs_file} protoc
|
|
COMMENT "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}"
|
|
VERBATIM)
|
|
endforeach()
|
|
|
|
set_source_files_properties(${_generated_srcs_all} PROPERTIES GENERATED TRUE)
|
|
if(protobuf_generate_OUT_VAR)
|
|
set(${protobuf_generate_OUT_VAR} ${_generated_srcs_all} PARENT_SCOPE)
|
|
endif()
|
|
if(protobuf_generate_TARGET)
|
|
target_sources(${protobuf_generate_TARGET} PRIVATE ${_generated_srcs_all})
|
|
endif()
|
|
endfunction()
|
|
|
|
# Calculates the absolute path to a .proto file.
|
|
function(get_proto_absolute_path result proto)
|
|
cmake_path(IS_ABSOLUTE proto _is_abs_path)
|
|
if(_is_abs_path)
|
|
set(${result} "${proto}" PARENT_SCOPE)
|
|
return()
|
|
endif()
|
|
foreach(_include_dir ${ARGN})
|
|
if(EXISTS "${_include_dir}/${proto}")
|
|
set(${result} "${_include_dir}/${proto}" PARENT_SCOPE)
|
|
return()
|
|
endif()
|
|
endforeach()
|
|
message(SEND_ERROR "Not found protobuf ${proto} in Protobuf_IMPORT_DIRS: ${ARGN}")
|
|
endfunction()
|
|
|
|
# Calculates a relative path to a .proto file. The returned path is relative to one of include directories.
|
|
function(get_proto_relative_path result abs_path)
|
|
set(${result} "" PARENT_SCOPE)
|
|
get_filename_component(_abs_dir "${abs_path}" DIRECTORY)
|
|
foreach(_include_dir ${ARGN})
|
|
cmake_path(IS_PREFIX _include_dir "${_abs_dir}" _is_prefix)
|
|
if(_is_prefix)
|
|
file(RELATIVE_PATH _rel_dir "${_include_dir}" "${_abs_dir}")
|
|
if(NOT _rel_dir STREQUAL "")
|
|
set(${result} "${_rel_dir}/" PARENT_SCOPE)
|
|
endif()
|
|
return()
|
|
endif()
|
|
endforeach()
|
|
message(WARNING "Not found protobuf ${abs_path} in Protobuf_IMPORT_DIRS: ${ARGN}")
|
|
endfunction()
|