2016-06-01 14:00:08 +00:00
|
|
|
# User options
|
|
|
|
include("${CMAKE_CURRENT_LIST_DIR}/protobuf-options.cmake")
|
2015-08-23 01:25:48 +00:00
|
|
|
|
2016-08-02 19:37:36 +00:00
|
|
|
# Depend packages
|
|
|
|
@_protobuf_FIND_ZLIB@
|
|
|
|
|
2015-08-23 01:25:48 +00:00
|
|
|
# Imported targets
|
2016-05-28 08:59:58 +00:00
|
|
|
include("${CMAKE_CURRENT_LIST_DIR}/protobuf-targets.cmake")
|
2015-08-23 01:25:48 +00:00
|
|
|
|
2017-05-30 23:49:18 +00:00
|
|
|
function(protobuf_generate)
|
|
|
|
include(CMakeParseArguments)
|
2017-09-12 08:00:04 +00:00
|
|
|
|
|
|
|
set(_options APPEND_PATH)
|
2018-05-14 07:03:42 +00:00
|
|
|
set(_singleargs LANGUAGE OUT_VAR EXPORT_MACRO PROTOC_OUT_DIR)
|
2017-05-30 23:49:18 +00:00
|
|
|
if(COMMAND target_sources)
|
|
|
|
list(APPEND _singleargs TARGET)
|
|
|
|
endif()
|
2017-09-12 08:00:04 +00:00
|
|
|
set(_multiargs PROTOS IMPORT_DIRS GENERATE_EXTENSIONS)
|
2017-05-30 23:49:18 +00:00
|
|
|
|
2017-09-12 08:00:04 +00:00
|
|
|
cmake_parse_arguments(protobuf_generate "${_options}" "${_singleargs}" "${_multiargs}" "${ARGN}")
|
2017-05-30 23:49:18 +00:00
|
|
|
|
2017-09-12 08:00:04 +00:00
|
|
|
if(NOT protobuf_generate_PROTOS AND NOT protobuf_generate_TARGET)
|
2017-05-30 23:49:18 +00:00
|
|
|
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)
|
|
|
|
|
2018-05-14 07:03:42 +00:00
|
|
|
if(NOT protobuf_generate_PROTOC_OUT_DIR)
|
|
|
|
set(protobuf_generate_PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
|
|
|
|
endif()
|
|
|
|
|
2017-09-22 09:44:59 +00:00
|
|
|
if(protobuf_generate_EXPORT_MACRO AND protobuf_generate_LANGUAGE STREQUAL cpp)
|
|
|
|
set(_dll_export_decl "dllexport_decl=${protobuf_generate_EXPORT_MACRO}:")
|
|
|
|
endif()
|
|
|
|
|
2018-05-14 06:52:28 +00:00
|
|
|
if(NOT protobuf_generate_GENERATE_EXTENSIONS)
|
2017-05-30 23:49:18 +00:00
|
|
|
if(protobuf_generate_LANGUAGE STREQUAL cpp)
|
2018-05-14 06:52:28 +00:00
|
|
|
set(protobuf_generate_GENERATE_EXTENSIONS .pb.h .pb.cc)
|
2017-05-30 23:49:18 +00:00
|
|
|
elseif(protobuf_generate_LANGUAGE STREQUAL python)
|
2018-05-14 06:52:28 +00:00
|
|
|
set(protobuf_generate_GENERATE_EXTENSIONS _pb2.py)
|
2017-05-30 23:49:18 +00:00
|
|
|
else()
|
|
|
|
message(SEND_ERROR "Error: protobuf_generate given unknown Language ${LANGUAGE}, please provide a value for GENERATE_EXTENSIONS")
|
|
|
|
return()
|
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
|
2017-09-29 09:07:36 +00:00
|
|
|
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()
|
|
|
|
|
2017-05-30 23:49:18 +00:00
|
|
|
if(protobuf_generate_APPEND_PATH)
|
|
|
|
# Create an include path for each file specified
|
2017-09-29 09:07:36 +00:00
|
|
|
foreach(_file ${protobuf_generate_PROTOS})
|
2017-05-30 23:49:18 +00:00
|
|
|
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()
|
|
|
|
|
2017-09-12 08:00:04 +00:00
|
|
|
set(_generated_srcs_all)
|
2017-05-30 23:49:18 +00:00
|
|
|
foreach(_proto ${protobuf_generate_PROTOS})
|
|
|
|
get_filename_component(_abs_file ${_proto} ABSOLUTE)
|
2018-06-25 09:29:56 +00:00
|
|
|
get_filename_component(_abs_dir ${_abs_file} DIRECTORY)
|
2017-05-30 23:49:18 +00:00
|
|
|
get_filename_component(_basename ${_proto} NAME_WE)
|
2018-06-25 09:29:56 +00:00
|
|
|
file(RELATIVE_PATH _rel_dir ${CMAKE_CURRENT_SOURCE_DIR} ${_abs_dir})
|
2017-05-30 23:49:18 +00:00
|
|
|
|
2017-09-12 08:00:04 +00:00
|
|
|
set(_generated_srcs)
|
2018-05-14 06:52:28 +00:00
|
|
|
foreach(_ext ${protobuf_generate_GENERATE_EXTENSIONS})
|
2018-06-25 09:29:56 +00:00
|
|
|
list(APPEND _generated_srcs "${protobuf_generate_PROTOC_OUT_DIR}/${_rel_dir}/${_basename}${_ext}")
|
2017-05-30 23:49:18 +00:00
|
|
|
endforeach()
|
2017-09-12 08:00:04 +00:00
|
|
|
list(APPEND _generated_srcs_all ${_generated_srcs})
|
2017-05-30 23:49:18 +00:00
|
|
|
|
|
|
|
add_custom_command(
|
|
|
|
OUTPUT ${_generated_srcs}
|
|
|
|
COMMAND protobuf::protoc
|
2018-07-10 16:09:35 +00:00
|
|
|
ARGS --${protobuf_generate_LANGUAGE}_out ${_dll_export_decl}${protobuf_generate_PROTOC_OUT_DIR}/${_rel_dir} ${_protobuf_include_path} ${_abs_file}
|
2018-05-15 07:06:54 +00:00
|
|
|
DEPENDS ${_abs_file} protobuf::protoc
|
2017-05-30 23:49:18 +00:00
|
|
|
COMMENT "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}"
|
|
|
|
VERBATIM )
|
|
|
|
endforeach()
|
|
|
|
|
2017-09-12 08:00:04 +00:00
|
|
|
set_source_files_properties(${_generated_srcs_all} PROPERTIES GENERATED TRUE)
|
2017-05-30 23:49:18 +00:00
|
|
|
if(protobuf_generate_OUT_VAR)
|
2017-09-12 08:00:04 +00:00
|
|
|
set(${protobuf_generate_OUT_VAR} ${_generated_srcs_all} PARENT_SCOPE)
|
2017-05-30 23:49:18 +00:00
|
|
|
endif()
|
|
|
|
if(protobuf_generate_TARGET)
|
2018-01-19 17:05:00 +00:00
|
|
|
target_sources(${protobuf_generate_TARGET} PRIVATE ${_generated_srcs_all})
|
2017-05-30 23:49:18 +00:00
|
|
|
endif()
|
|
|
|
|
|
|
|
endfunction()
|
|
|
|
|
2015-08-23 01:25:48 +00:00
|
|
|
# CMake FindProtobuf module compatible file
|
2016-06-01 14:00:08 +00:00
|
|
|
if(protobuf_MODULE_COMPATIBLE)
|
|
|
|
include("${CMAKE_CURRENT_LIST_DIR}/protobuf-module.cmake")
|
2015-08-23 01:25:48 +00:00
|
|
|
endif()
|