113 lines
3.7 KiB
CMake
113 lines
3.7 KiB
CMake
# User options
|
|
include("${CMAKE_CURRENT_LIST_DIR}/protobuf-options.cmake")
|
|
|
|
# Depend packages
|
|
@_protobuf_FIND_ZLIB@
|
|
|
|
# Imported targets
|
|
include("${CMAKE_CURRENT_LIST_DIR}/protobuf-targets.cmake")
|
|
|
|
function(protobuf_generate)
|
|
include(CMakeParseArguments)
|
|
set(_singleargs LANGUAGE OUT_VAR)
|
|
if(COMMAND target_sources)
|
|
list(APPEND _singleargs TARGET)
|
|
endif()
|
|
|
|
cmake_parse_arguments(protobuf_generate "APPEND_PATH" "${_singleargs}" "PROTOS IMPORT_DIRS GENERATE_EXTENSIONS" "${ARGN}")
|
|
|
|
if(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_EXTENSIONS)
|
|
if(protobuf_generate_LANGUAGE STREQUAL cpp)
|
|
set(protobuf_GENERATE_EXTENSIONS .pb.h .pb.cc)
|
|
elseif(protobuf_generate_LANGUAGE STREQUAL python)
|
|
set(protobuf_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_APPEND_PATH)
|
|
# Create an include path for each file specified
|
|
foreach(_file ${ARGN})
|
|
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()
|
|
|
|
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()
|
|
|
|
set(_generated_srcs)
|
|
foreach(_proto ${protobuf_generate_PROTOS})
|
|
get_filename_component(_abs_file ${_proto} ABSOLUTE)
|
|
get_filename_component(_basename ${_proto} NAME_WE)
|
|
|
|
foreach(_ext ${_output_extensions})
|
|
list(APPEND _generated_srcs "${CMAKE_CURRENT_BINARY_DIR}/${_basename}${_ext}")
|
|
endforeach()
|
|
|
|
add_custom_command(
|
|
OUTPUT ${_generated_srcs}
|
|
COMMAND protobuf::protoc
|
|
ARGS --${protobuf_generate_LANGUAGE}_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${_abs_file}
|
|
DEPENDS ${ABS_FIL} protobuf::protoc
|
|
COMMENT "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}"
|
|
VERBATIM )
|
|
endforeach()
|
|
|
|
set_source_files_properties(${_generated_srcs} PROPERTIES GENERATED TRUE)
|
|
if(protobuf_generate_OUT_VAR)
|
|
set(${protobuf_generate_OUT_VAR} ${_generated_srcs} PARENT_SCOPE)
|
|
endif()
|
|
if(protobuf_generate_TARGET)
|
|
target_sources(${protobuf_generate_TARGET} PUBLIC ${_generated_srcs})
|
|
endif()
|
|
|
|
endfunction()
|
|
|
|
# CMake FindProtobuf module compatible file
|
|
if(protobuf_MODULE_COMPATIBLE)
|
|
include("${CMAKE_CURRENT_LIST_DIR}/protobuf-module.cmake")
|
|
endif()
|