1# This file contains backwards compatibility patches for various legacy functions and variables 2# Functions 3 4function(PROTOBUF_GENERATE_CPP SRCS HDRS) 5 cmake_parse_arguments(protobuf_generate_cpp "" "EXPORT_MACRO" "" ${ARGN}) 6 7 set(_proto_files "${protobuf_generate_cpp_UNPARSED_ARGUMENTS}") 8 if(NOT _proto_files) 9 message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files") 10 return() 11 endif() 12 13 if(PROTOBUF_GENERATE_CPP_APPEND_PATH) 14 set(_append_arg APPEND_PATH) 15 endif() 16 17 if(DEFINED Protobuf_IMPORT_DIRS) 18 set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS}) 19 endif() 20 21 set(_outvar) 22 protobuf_generate(${_append_arg} LANGUAGE cpp EXPORT_MACRO ${protobuf_generate_cpp_EXPORT_MACRO} OUT_VAR _outvar ${_import_arg} PROTOS ${_proto_files}) 23 24 set(${SRCS}) 25 set(${HDRS}) 26 foreach(_file ${_outvar}) 27 if(_file MATCHES "cc$") 28 list(APPEND ${SRCS} ${_file}) 29 else() 30 list(APPEND ${HDRS} ${_file}) 31 endif() 32 endforeach() 33 set(${SRCS} ${${SRCS}} PARENT_SCOPE) 34 set(${HDRS} ${${HDRS}} PARENT_SCOPE) 35endfunction() 36 37function(PROTOBUF_GENERATE_PYTHON SRCS) 38 if(NOT ARGN) 39 message(SEND_ERROR "Error: PROTOBUF_GENERATE_PYTHON() called without any proto files") 40 return() 41 endif() 42 43 if(PROTOBUF_GENERATE_CPP_APPEND_PATH) 44 set(_append_arg APPEND_PATH) 45 endif() 46 47 if(DEFINED Protobuf_IMPORT_DIRS) 48 set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS}) 49 endif() 50 51 set(_outvar) 52 protobuf_generate(${_append_arg} LANGUAGE python OUT_VAR _outvar ${_import_arg} PROTOS ${ARGN}) 53 set(${SRCS} ${_outvar} PARENT_SCOPE) 54endfunction() 55 56# Environment 57 58# Backwards compatibility 59# Define camel case versions of input variables 60foreach(UPPER 61 PROTOBUF_SRC_ROOT_FOLDER 62 PROTOBUF_IMPORT_DIRS 63 PROTOBUF_DEBUG 64 PROTOBUF_LIBRARY 65 PROTOBUF_PROTOC_LIBRARY 66 PROTOBUF_INCLUDE_DIR 67 PROTOBUF_PROTOC_EXECUTABLE 68 PROTOBUF_LIBRARY_DEBUG 69 PROTOBUF_PROTOC_LIBRARY_DEBUG 70 PROTOBUF_LITE_LIBRARY 71 PROTOBUF_LITE_LIBRARY_DEBUG 72 ) 73 if (DEFINED ${UPPER}) 74 string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER}) 75 if (NOT DEFINED ${Camel}) 76 set(${Camel} ${${UPPER}}) 77 endif() 78 endif() 79endforeach() 80 81if(DEFINED Protobuf_SRC_ROOT_FOLDER) 82 message(AUTHOR_WARNING "Variable Protobuf_SRC_ROOT_FOLDER defined, but not" 83 " used in CONFIG mode") 84endif() 85 86include(SelectLibraryConfigurations) 87 88# Internal function: search for normal library as well as a debug one 89# if the debug one is specified also include debug/optimized keywords 90# in *_LIBRARIES variable 91function(_protobuf_find_libraries name filename) 92 if(${name}_LIBRARIES) 93 # Use result recorded by a previous call. 94 elseif(${name}_LIBRARY) 95 # Honor cache entry used by CMake 3.5 and lower. 96 set(${name}_LIBRARIES "${${name}_LIBRARY}" PARENT_SCOPE) 97 else() 98 get_target_property(${name}_LIBRARY_RELEASE protobuf::lib${filename} 99 LOCATION_RELEASE) 100 get_target_property(${name}_LIBRARY_RELWITHDEBINFO protobuf::lib${filename} 101 LOCATION_RELWITHDEBINFO) 102 get_target_property(${name}_LIBRARY_MINSIZEREL protobuf::lib${filename} 103 LOCATION_MINSIZEREL) 104 get_target_property(${name}_LIBRARY_DEBUG protobuf::lib${filename} 105 LOCATION_DEBUG) 106 107 select_library_configurations(${name}) 108 set(${name}_LIBRARY ${${name}_LIBRARY} PARENT_SCOPE) 109 set(${name}_LIBRARIES ${${name}_LIBRARIES} PARENT_SCOPE) 110 endif() 111endfunction() 112 113# Internal function: find threads library 114function(_protobuf_find_threads) 115 set(CMAKE_THREAD_PREFER_PTHREAD TRUE) 116 find_package(Threads) 117 if(Threads_FOUND) 118 list(APPEND PROTOBUF_LIBRARIES ${CMAKE_THREAD_LIBS_INIT}) 119 set(PROTOBUF_LIBRARIES "${PROTOBUF_LIBRARIES}" PARENT_SCOPE) 120 endif() 121endfunction() 122 123# 124# Main. 125# 126 127# By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc 128# for each directory where a proto file is referenced. 129if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH) 130 set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE) 131endif() 132 133# The Protobuf library 134_protobuf_find_libraries(Protobuf protobuf) 135 136# The Protobuf Lite library 137_protobuf_find_libraries(Protobuf_LITE protobuf-lite) 138 139# The Protobuf Protoc Library 140_protobuf_find_libraries(Protobuf_PROTOC protoc) 141 142if(UNIX) 143 _protobuf_find_threads() 144endif() 145 146# Set the include directory 147get_target_property(Protobuf_INCLUDE_DIRS protobuf::libprotobuf 148 INTERFACE_INCLUDE_DIRECTORIES) 149 150# Set the protoc Executable 151get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc 152 IMPORTED_LOCATION_RELEASE) 153if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}") 154 get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc 155 IMPORTED_LOCATION_RELWITHDEBINFO) 156endif() 157if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}") 158 get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc 159 IMPORTED_LOCATION_MINSIZEREL) 160endif() 161if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}") 162 get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc 163 IMPORTED_LOCATION_DEBUG) 164endif() 165if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}") 166 get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc 167 IMPORTED_LOCATION_NOCONFIG) 168endif() 169 170# Version info variable 171set(Protobuf_VERSION "@protobuf_VERSION@") 172 173include(FindPackageHandleStandardArgs) 174FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf 175 REQUIRED_VARS Protobuf_PROTOC_EXECUTABLE Protobuf_LIBRARIES Protobuf_INCLUDE_DIRS 176 VERSION_VAR Protobuf_VERSION 177) 178 179# Backwards compatibility 180# Define upper case versions of output variables 181foreach(Camel 182 Protobuf_VERSION 183 Protobuf_SRC_ROOT_FOLDER 184 Protobuf_IMPORT_DIRS 185 Protobuf_DEBUG 186 Protobuf_INCLUDE_DIRS 187 Protobuf_LIBRARIES 188 Protobuf_PROTOC_LIBRARIES 189 Protobuf_LITE_LIBRARIES 190 Protobuf_LIBRARY 191 Protobuf_PROTOC_LIBRARY 192 Protobuf_INCLUDE_DIR 193 Protobuf_PROTOC_EXECUTABLE 194 Protobuf_LIBRARY_DEBUG 195 Protobuf_PROTOC_LIBRARY_DEBUG 196 Protobuf_LITE_LIBRARY 197 Protobuf_LITE_LIBRARY_DEBUG 198 ) 199 string(TOUPPER ${Camel} UPPER) 200 set(${UPPER} ${${Camel}}) 201endforeach() 202