1# Functions 2 3function(PROTOBUF_GENERATE_CPP SRCS HDRS) 4 if(NOT ARGN) 5 message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files") 6 return() 7 endif() 8 9 if(PROTOBUF_GENERATE_CPP_APPEND_PATH) 10 # Create an include path for each file specified 11 foreach(FIL ${ARGN}) 12 get_filename_component(ABS_FIL ${FIL} ABSOLUTE) 13 get_filename_component(ABS_PATH ${ABS_FIL} PATH) 14 list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) 15 if(${_contains_already} EQUAL -1) 16 list(APPEND _protobuf_include_path -I ${ABS_PATH}) 17 endif() 18 endforeach() 19 else() 20 set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR}) 21 endif() 22 23 if(DEFINED Protobuf_IMPORT_DIRS) 24 foreach(DIR ${Protobuf_IMPORT_DIRS}) 25 get_filename_component(ABS_PATH ${DIR} ABSOLUTE) 26 list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) 27 if(${_contains_already} EQUAL -1) 28 list(APPEND _protobuf_include_path -I ${ABS_PATH}) 29 endif() 30 endforeach() 31 endif() 32 33 set(${SRCS}) 34 set(${HDRS}) 35 foreach(FIL ${ARGN}) 36 get_filename_component(ABS_FIL ${FIL} ABSOLUTE) 37 get_filename_component(FIL_WE ${FIL} NAME_WE) 38 39 list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc") 40 list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h") 41 42 add_custom_command( 43 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc" 44 "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h" 45 COMMAND ${Protobuf_PROTOC_EXECUTABLE} 46 ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL} 47 DEPENDS ${ABS_FIL} ${Protobuf_PROTOC_EXECUTABLE} 48 COMMENT "Running C++ protocol buffer compiler on ${FIL}" 49 VERBATIM ) 50 endforeach() 51 52 set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE) 53 set(${SRCS} ${${SRCS}} PARENT_SCOPE) 54 set(${HDRS} ${${HDRS}} PARENT_SCOPE) 55endfunction() 56 57function(PROTOBUF_GENERATE_PYTHON SRCS) 58 if(NOT ARGN) 59 message(SEND_ERROR "Error: PROTOBUF_GENERATE_PYTHON() called without any proto files") 60 return() 61 endif() 62 63 if(PROTOBUF_GENERATE_CPP_APPEND_PATH) 64 # Create an include path for each file specified 65 foreach(FIL ${ARGN}) 66 get_filename_component(ABS_FIL ${FIL} ABSOLUTE) 67 get_filename_component(ABS_PATH ${ABS_FIL} PATH) 68 list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) 69 if(${_contains_already} EQUAL -1) 70 list(APPEND _protobuf_include_path -I ${ABS_PATH}) 71 endif() 72 endforeach() 73 else() 74 set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR}) 75 endif() 76 77 if(DEFINED Protobuf_IMPORT_DIRS) 78 foreach(DIR ${Protobuf_IMPORT_DIRS}) 79 get_filename_component(ABS_PATH ${DIR} ABSOLUTE) 80 list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) 81 if(${_contains_already} EQUAL -1) 82 list(APPEND _protobuf_include_path -I ${ABS_PATH}) 83 endif() 84 endforeach() 85 endif() 86 87 set(${SRCS}) 88 foreach(FIL ${ARGN}) 89 get_filename_component(ABS_FIL ${FIL} ABSOLUTE) 90 get_filename_component(FIL_WE ${FIL} NAME_WE) 91 92 list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}_pb2.py") 93 add_custom_command( 94 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}_pb2.py" 95 COMMAND ${Protobuf_PROTOC_EXECUTABLE} --python_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL} 96 DEPENDS ${ABS_FIL} ${Protobuf_PROTOC_EXECUTABLE} 97 COMMENT "Running Python protocol buffer compiler on ${FIL}" 98 VERBATIM ) 99 endforeach() 100 101 set(${SRCS} ${${SRCS}} PARENT_SCOPE) 102endfunction() 103 104# Environment 105 106# Backwards compatibility 107# Define camel case versions of input variables 108foreach(UPPER 109 PROTOBUF_SRC_ROOT_FOLDER 110 PROTOBUF_IMPORT_DIRS 111 PROTOBUF_DEBUG 112 PROTOBUF_LIBRARY 113 PROTOBUF_PROTOC_LIBRARY 114 PROTOBUF_INCLUDE_DIR 115 PROTOBUF_PROTOC_EXECUTABLE 116 PROTOBUF_LIBRARY_DEBUG 117 PROTOBUF_PROTOC_LIBRARY_DEBUG 118 PROTOBUF_LITE_LIBRARY 119 PROTOBUF_LITE_LIBRARY_DEBUG 120 ) 121 if (DEFINED ${UPPER}) 122 string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER}) 123 if (NOT DEFINED ${Camel}) 124 set(${Camel} ${${UPPER}}) 125 endif() 126 endif() 127endforeach() 128 129if(DEFINED Protobuf_SRC_ROOT_FOLDER) 130 message(AUTHOR_WARNING "Variable Protobuf_SRC_ROOT_FOLDER defined, but not" 131 " used in CONFIG mode") 132endif() 133 134include(SelectLibraryConfigurations) 135 136# Internal function: search for normal library as well as a debug one 137# if the debug one is specified also include debug/optimized keywords 138# in *_LIBRARIES variable 139function(_protobuf_find_libraries name filename) 140 if(${name}_LIBRARIES) 141 # Use result recorded by a previous call. 142 elseif(${name}_LIBRARY) 143 # Honor cache entry used by CMake 3.5 and lower. 144 set(${name}_LIBRARIES "${${name}_LIBRARY}" PARENT_SCOPE) 145 else() 146 get_target_property(${name}_LIBRARY_RELEASE protobuf::lib${filename} 147 LOCATION_RELEASE) 148 get_target_property(${name}_LIBRARY_DEBUG protobuf::lib${filename} 149 LOCATION_DEBUG) 150 endif() 151 152 select_library_configurations(${name}) 153 set(${name}_LIBRARY ${${name}_LIBRARY} PARENT_SCOPE) 154 set(${name}_LIBRARIES ${${name}_LIBRARIES} PARENT_SCOPE) 155 endif() 156endfunction() 157 158# Internal function: find threads library 159function(_protobuf_find_threads) 160 set(CMAKE_THREAD_PREFER_PTHREAD TRUE) 161 find_package(Threads) 162 if(Threads_FOUND) 163 list(APPEND PROTOBUF_LIBRARIES ${CMAKE_THREAD_LIBS_INIT}) 164 set(PROTOBUF_LIBRARIES "${PROTOBUF_LIBRARIES}" PARENT_SCOPE) 165 endif() 166endfunction() 167 168# 169# Main. 170# 171 172# By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc 173# for each directory where a proto file is referenced. 174if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH) 175 set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE) 176endif() 177 178# The Protobuf library 179_protobuf_find_libraries(Protobuf protobuf) 180 181# The Protobuf Lite library 182_protobuf_find_libraries(Protobuf_LITE protobuf-lite) 183 184# The Protobuf Protoc Library 185_protobuf_find_libraries(Protobuf_PROTOC protoc) 186 187if(UNIX) 188 _protobuf_find_threads() 189endif() 190 191# Set the include directory 192get_target_property(Protobuf_INCLUDE_DIRS protobuf::libprotobuf 193 INTERFACE_INCLUDE_DIRECTORIES) 194 195# Set the protoc Executable 196get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc 197 IMPORTED_LOCATION_RELEASE) 198if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}") 199 get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc 200 IMPORTED_LOCATION_DEBUG) 201endif() 202 203# Version info variable 204set(Protobuf_VERSION "@protobuf_VERSION@") 205 206include(FindPackageHandleStandardArgs) 207FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf 208 REQUIRED_VARS Protobuf_PROTOC_EXECUTABLE Protobuf_LIBRARIES Protobuf_INCLUDE_DIRS 209 VERSION_VAR Protobuf_VERSION 210) 211 212# Backwards compatibility 213# Define upper case versions of output variables 214foreach(Camel 215 Protobuf_VERSION 216 Protobuf_SRC_ROOT_FOLDER 217 Protobuf_IMPORT_DIRS 218 Protobuf_DEBUG 219 Protobuf_INCLUDE_DIRS 220 Protobuf_LIBRARIES 221 Protobuf_PROTOC_LIBRARIES 222 Protobuf_LITE_LIBRARIES 223 Protobuf_LIBRARY 224 Protobuf_PROTOC_LIBRARY 225 Protobuf_INCLUDE_DIR 226 Protobuf_PROTOC_EXECUTABLE 227 Protobuf_LIBRARY_DEBUG 228 Protobuf_PROTOC_LIBRARY_DEBUG 229 Protobuf_LITE_LIBRARY 230 Protobuf_LITE_LIBRARY_DEBUG 231 ) 232 string(TOUPPER ${Camel} UPPER) 233 set(${UPPER} ${${Camel}}) 234endforeach() 235