blob: 74c5488729a1bb8de9c32b09152fb1ffe58af69d [file] [log] [blame]
Austin Schuh40c16522018-10-28 20:27:54 -07001# This file contains backwards compatibility patches for various legacy functions and variables
2# Functions
Brian Silverman9c614bc2016-02-15 20:20:02 -05003
4function(PROTOBUF_GENERATE_CPP SRCS HDRS)
Austin Schuh40c16522018-10-28 20:27:54 -07005 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 Silverman9c614bc2016-02-15 20:20:02 -05009 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 Schuh40c16522018-10-28 20:27:54 -070014 set(_append_arg APPEND_PATH)
Brian Silverman9c614bc2016-02-15 20:20:02 -050015 endif()
16
Austin Schuh40c16522018-10-28 20:27:54 -070017 if(DEFINED Protobuf_IMPORT_DIRS)
18 set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS})
Brian Silverman9c614bc2016-02-15 20:20:02 -050019 endif()
20
Austin Schuh40c16522018-10-28 20:27:54 -070021 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 Silverman9c614bc2016-02-15 20:20:02 -050024 set(${SRCS})
25 set(${HDRS})
Austin Schuh40c16522018-10-28 20:27:54 -070026 foreach(_file ${_outvar})
27 if(_file MATCHES "cc$")
28 list(APPEND ${SRCS} ${_file})
29 else()
30 list(APPEND ${HDRS} ${_file})
31 endif()
Brian Silverman9c614bc2016-02-15 20:20:02 -050032 endforeach()
Brian Silverman9c614bc2016-02-15 20:20:02 -050033 set(${SRCS} ${${SRCS}} PARENT_SCOPE)
34 set(${HDRS} ${${HDRS}} PARENT_SCOPE)
35endfunction()
36
Austin Schuh40c16522018-10-28 20:27:54 -070037function(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
Brian Silverman9c614bc2016-02-15 20:20:02 -050088# 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)
Austin Schuh40c16522018-10-28 20:27:54 -070092 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 Silverman9c614bc2016-02-15 20:20:02 -0500102
Austin Schuh40c16522018-10-28 20:27:54 -0700103 select_library_configurations(${name})
104 set(${name}_LIBRARY ${${name}_LIBRARY} PARENT_SCOPE)
105 set(${name}_LIBRARIES ${${name}_LIBRARIES} PARENT_SCOPE)
106 endif()
Brian Silverman9c614bc2016-02-15 20:20:02 -0500107endfunction()
108
109# Internal function: find threads library
110function(_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()
117endfunction()
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.
125if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
126 set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
127endif()
128
129# The Protobuf library
Austin Schuh40c16522018-10-28 20:27:54 -0700130_protobuf_find_libraries(Protobuf protobuf)
Brian Silverman9c614bc2016-02-15 20:20:02 -0500131
132# The Protobuf Lite library
Austin Schuh40c16522018-10-28 20:27:54 -0700133_protobuf_find_libraries(Protobuf_LITE protobuf-lite)
Brian Silverman9c614bc2016-02-15 20:20:02 -0500134
135# The Protobuf Protoc Library
Austin Schuh40c16522018-10-28 20:27:54 -0700136_protobuf_find_libraries(Protobuf_PROTOC protoc)
Brian Silverman9c614bc2016-02-15 20:20:02 -0500137
138if(UNIX)
139 _protobuf_find_threads()
140endif()
141
142# Set the include directory
Austin Schuh40c16522018-10-28 20:27:54 -0700143get_target_property(Protobuf_INCLUDE_DIRS protobuf::libprotobuf
144 INTERFACE_INCLUDE_DIRECTORIES)
Brian Silverman9c614bc2016-02-15 20:20:02 -0500145
146# Set the protoc Executable
Austin Schuh40c16522018-10-28 20:27:54 -0700147get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
Brian Silverman9c614bc2016-02-15 20:20:02 -0500148 IMPORTED_LOCATION_RELEASE)
Austin Schuh40c16522018-10-28 20:27:54 -0700149if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
150 get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
Brian Silverman9c614bc2016-02-15 20:20:02 -0500151 IMPORTED_LOCATION_DEBUG)
152endif()
Austin Schuh40c16522018-10-28 20:27:54 -0700153if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
154 get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
155 IMPORTED_LOCATION_NOCONFIG)
156endif()
157
158# Version info variable
159set(Protobuf_VERSION "@protobuf_VERSION@")
Brian Silverman9c614bc2016-02-15 20:20:02 -0500160
161include(FindPackageHandleStandardArgs)
Austin Schuh40c16522018-10-28 20:27:54 -0700162FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf
163 REQUIRED_VARS Protobuf_PROTOC_EXECUTABLE Protobuf_LIBRARIES Protobuf_INCLUDE_DIRS
164 VERSION_VAR Protobuf_VERSION
165)
Brian Silverman9c614bc2016-02-15 20:20:02 -0500166
Austin Schuh40c16522018-10-28 20:27:54 -0700167# Backwards compatibility
168# Define upper case versions of output variables
169foreach(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}})
189endforeach()