Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 1 | # This file contains backwards compatibility patches for various legacy functions and variables |
| 2 | # Functions |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 3 | |
| 4 | function(PROTOBUF_GENERATE_CPP SRCS HDRS) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 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) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 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) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 14 | set(_append_arg APPEND_PATH) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 15 | endif() |
| 16 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 17 | if(DEFINED Protobuf_IMPORT_DIRS) |
| 18 | set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS}) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 19 | endif() |
| 20 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 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 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 24 | set(${SRCS}) |
| 25 | set(${HDRS}) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 26 | foreach(_file ${_outvar}) |
| 27 | if(_file MATCHES "cc$") |
| 28 | list(APPEND ${SRCS} ${_file}) |
| 29 | else() |
| 30 | list(APPEND ${HDRS} ${_file}) |
| 31 | endif() |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 32 | endforeach() |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 33 | set(${SRCS} ${${SRCS}} PARENT_SCOPE) |
| 34 | set(${HDRS} ${${HDRS}} PARENT_SCOPE) |
| 35 | endfunction() |
| 36 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 37 | function(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) |
| 54 | endfunction() |
| 55 | |
| 56 | # Environment |
| 57 | |
| 58 | # Backwards compatibility |
| 59 | # Define camel case versions of input variables |
| 60 | foreach(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() |
| 79 | endforeach() |
| 80 | |
| 81 | if(DEFINED Protobuf_SRC_ROOT_FOLDER) |
| 82 | message(AUTHOR_WARNING "Variable Protobuf_SRC_ROOT_FOLDER defined, but not" |
| 83 | " used in CONFIG mode") |
| 84 | endif() |
| 85 | |
| 86 | include(SelectLibraryConfigurations) |
| 87 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 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 |
| 91 | function(_protobuf_find_libraries name filename) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 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_DEBUG protobuf::lib${filename} |
| 101 | LOCATION_DEBUG) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 102 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 103 | select_library_configurations(${name}) |
| 104 | set(${name}_LIBRARY ${${name}_LIBRARY} PARENT_SCOPE) |
| 105 | set(${name}_LIBRARIES ${${name}_LIBRARIES} PARENT_SCOPE) |
| 106 | endif() |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 107 | endfunction() |
| 108 | |
| 109 | # Internal function: find threads library |
| 110 | function(_protobuf_find_threads) |
| 111 | set(CMAKE_THREAD_PREFER_PTHREAD TRUE) |
| 112 | find_package(Threads) |
| 113 | if(Threads_FOUND) |
| 114 | list(APPEND PROTOBUF_LIBRARIES ${CMAKE_THREAD_LIBS_INIT}) |
| 115 | set(PROTOBUF_LIBRARIES "${PROTOBUF_LIBRARIES}" PARENT_SCOPE) |
| 116 | endif() |
| 117 | endfunction() |
| 118 | |
| 119 | # |
| 120 | # Main. |
| 121 | # |
| 122 | |
| 123 | # By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc |
| 124 | # for each directory where a proto file is referenced. |
| 125 | if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH) |
| 126 | set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE) |
| 127 | endif() |
| 128 | |
| 129 | # The Protobuf library |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 130 | _protobuf_find_libraries(Protobuf protobuf) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 131 | |
| 132 | # The Protobuf Lite library |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 133 | _protobuf_find_libraries(Protobuf_LITE protobuf-lite) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 134 | |
| 135 | # The Protobuf Protoc Library |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 136 | _protobuf_find_libraries(Protobuf_PROTOC protoc) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 137 | |
| 138 | if(UNIX) |
| 139 | _protobuf_find_threads() |
| 140 | endif() |
| 141 | |
| 142 | # Set the include directory |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 143 | get_target_property(Protobuf_INCLUDE_DIRS protobuf::libprotobuf |
| 144 | INTERFACE_INCLUDE_DIRECTORIES) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 145 | |
| 146 | # Set the protoc Executable |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 147 | get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 148 | IMPORTED_LOCATION_RELEASE) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 149 | if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}") |
| 150 | get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 151 | IMPORTED_LOCATION_DEBUG) |
| 152 | endif() |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 153 | if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}") |
| 154 | get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc |
| 155 | IMPORTED_LOCATION_NOCONFIG) |
| 156 | endif() |
| 157 | |
| 158 | # Version info variable |
| 159 | set(Protobuf_VERSION "@protobuf_VERSION@") |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 160 | |
| 161 | include(FindPackageHandleStandardArgs) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 162 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf |
| 163 | REQUIRED_VARS Protobuf_PROTOC_EXECUTABLE Protobuf_LIBRARIES Protobuf_INCLUDE_DIRS |
| 164 | VERSION_VAR Protobuf_VERSION |
| 165 | ) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 166 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame] | 167 | # Backwards compatibility |
| 168 | # Define upper case versions of output variables |
| 169 | foreach(Camel |
| 170 | Protobuf_VERSION |
| 171 | Protobuf_SRC_ROOT_FOLDER |
| 172 | Protobuf_IMPORT_DIRS |
| 173 | Protobuf_DEBUG |
| 174 | Protobuf_INCLUDE_DIRS |
| 175 | Protobuf_LIBRARIES |
| 176 | Protobuf_PROTOC_LIBRARIES |
| 177 | Protobuf_LITE_LIBRARIES |
| 178 | Protobuf_LIBRARY |
| 179 | Protobuf_PROTOC_LIBRARY |
| 180 | Protobuf_INCLUDE_DIR |
| 181 | Protobuf_PROTOC_EXECUTABLE |
| 182 | Protobuf_LIBRARY_DEBUG |
| 183 | Protobuf_PROTOC_LIBRARY_DEBUG |
| 184 | Protobuf_LITE_LIBRARY |
| 185 | Protobuf_LITE_LIBRARY_DEBUG |
| 186 | ) |
| 187 | string(TOUPPER ${Camel} UPPER) |
| 188 | set(${UPPER} ${${Camel}}) |
| 189 | endforeach() |