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