1# User options 2include("${CMAKE_CURRENT_LIST_DIR}/protobuf-options.cmake") 3 4# Depend packages 5@_protobuf_FIND_ZLIB@ 6 7# Imported targets 8include("${CMAKE_CURRENT_LIST_DIR}/protobuf-targets.cmake") 9 10function(protobuf_generate) 11 include(CMakeParseArguments) 12 13 set(_options APPEND_PATH) 14 set(_singleargs LANGUAGE OUT_VAR EXPORT_MACRO PROTOC_OUT_DIR) 15 if(COMMAND target_sources) 16 list(APPEND _singleargs TARGET) 17 endif() 18 set(_multiargs PROTOS IMPORT_DIRS GENERATE_EXTENSIONS) 19 20 cmake_parse_arguments(protobuf_generate "${_options}" "${_singleargs}" "${_multiargs}" "${ARGN}") 21 22 if(NOT protobuf_generate_PROTOS AND NOT protobuf_generate_TARGET) 23 message(SEND_ERROR "Error: protobuf_generate called without any targets or source files") 24 return() 25 endif() 26 27 if(NOT protobuf_generate_OUT_VAR AND NOT protobuf_generate_TARGET) 28 message(SEND_ERROR "Error: protobuf_generate called without a target or output variable") 29 return() 30 endif() 31 32 if(NOT protobuf_generate_LANGUAGE) 33 set(protobuf_generate_LANGUAGE cpp) 34 endif() 35 string(TOLOWER ${protobuf_generate_LANGUAGE} protobuf_generate_LANGUAGE) 36 37 if(NOT protobuf_generate_PROTOC_OUT_DIR) 38 set(protobuf_generate_PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}) 39 endif() 40 41 if(protobuf_generate_EXPORT_MACRO AND protobuf_generate_LANGUAGE STREQUAL cpp) 42 set(_dll_export_decl "dllexport_decl=${protobuf_generate_EXPORT_MACRO}:") 43 endif() 44 45 if(NOT protobuf_generate_GENERATE_EXTENSIONS) 46 if(protobuf_generate_LANGUAGE STREQUAL cpp) 47 set(protobuf_generate_GENERATE_EXTENSIONS .pb.h .pb.cc) 48 elseif(protobuf_generate_LANGUAGE STREQUAL python) 49 set(protobuf_generate_GENERATE_EXTENSIONS _pb2.py) 50 else() 51 message(SEND_ERROR "Error: protobuf_generate given unknown Language ${LANGUAGE}, please provide a value for GENERATE_EXTENSIONS") 52 return() 53 endif() 54 endif() 55 56 if(protobuf_generate_TARGET) 57 get_target_property(_source_list ${protobuf_generate_TARGET} SOURCES) 58 foreach(_file ${_source_list}) 59 if(_file MATCHES "proto$") 60 list(APPEND protobuf_generate_PROTOS ${_file}) 61 endif() 62 endforeach() 63 endif() 64 65 if(NOT protobuf_generate_PROTOS) 66 message(SEND_ERROR "Error: protobuf_generate could not find any .proto files") 67 return() 68 endif() 69 70 if(protobuf_generate_APPEND_PATH) 71 # Create an include path for each file specified 72 foreach(_file ${protobuf_generate_PROTOS}) 73 get_filename_component(_abs_file ${_file} ABSOLUTE) 74 get_filename_component(_abs_path ${_abs_file} PATH) 75 list(FIND _protobuf_include_path ${_abs_path} _contains_already) 76 if(${_contains_already} EQUAL -1) 77 list(APPEND _protobuf_include_path -I ${_abs_path}) 78 endif() 79 endforeach() 80 else() 81 set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR}) 82 endif() 83 84 foreach(DIR ${protobuf_generate_IMPORT_DIRS}) 85 get_filename_component(ABS_PATH ${DIR} ABSOLUTE) 86 list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) 87 if(${_contains_already} EQUAL -1) 88 list(APPEND _protobuf_include_path -I ${ABS_PATH}) 89 endif() 90 endforeach() 91 92 set(_generated_srcs_all) 93 foreach(_proto ${protobuf_generate_PROTOS}) 94 get_filename_component(_abs_file ${_proto} ABSOLUTE) 95 get_filename_component(_abs_dir ${_abs_file} DIRECTORY) 96 get_filename_component(_basename ${_proto} NAME_WE) 97 file(RELATIVE_PATH _rel_dir ${CMAKE_CURRENT_SOURCE_DIR} ${_abs_dir}) 98 99 set(_generated_srcs) 100 foreach(_ext ${protobuf_generate_GENERATE_EXTENSIONS}) 101 list(APPEND _generated_srcs "${protobuf_generate_PROTOC_OUT_DIR}/${_rel_dir}/${_basename}${_ext}") 102 endforeach() 103 list(APPEND _generated_srcs_all ${_generated_srcs}) 104 105 add_custom_command( 106 OUTPUT ${_generated_srcs} 107 COMMAND protobuf::protoc 108 ARGS --${protobuf_generate_LANGUAGE}_out ${_dll_export_decl}${protobuf_generate_PROTOC_OUT_DIR} ${_protobuf_include_path} ${_abs_file} 109 DEPENDS ${_abs_file} protobuf::protoc 110 COMMENT "Running ${protobuf_generate_LANGUAGE} protocol buffer compiler on ${_proto}" 111 VERBATIM ) 112 endforeach() 113 114 set_source_files_properties(${_generated_srcs_all} PROPERTIES GENERATED TRUE) 115 if(protobuf_generate_OUT_VAR) 116 set(${protobuf_generate_OUT_VAR} ${_generated_srcs_all} PARENT_SCOPE) 117 endif() 118 if(protobuf_generate_TARGET) 119 target_sources(${protobuf_generate_TARGET} PRIVATE ${_generated_srcs_all}) 120 endif() 121 122endfunction() 123 124# CMake FindProtobuf module compatible file 125if(protobuf_MODULE_COMPATIBLE) 126 include("${CMAKE_CURRENT_LIST_DIR}/protobuf-module.cmake") 127endif() 128