• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DEBUG protobuf::lib${filename}
101      LOCATION_DEBUG)
102
103    select_library_configurations(${name})
104    set(${name}_LIBRARY ${${name}_LIBRARY} PARENT_SCOPE)
105    set(${name}_LIBRARIES ${${name}_LIBRARIES} PARENT_SCOPE)
106  endif()
107endfunction()
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
130_protobuf_find_libraries(Protobuf protobuf)
131
132# The Protobuf Lite library
133_protobuf_find_libraries(Protobuf_LITE protobuf-lite)
134
135# The Protobuf Protoc Library
136_protobuf_find_libraries(Protobuf_PROTOC protoc)
137
138if(UNIX)
139  _protobuf_find_threads()
140endif()
141
142# Set the include directory
143get_target_property(Protobuf_INCLUDE_DIRS protobuf::libprotobuf
144  INTERFACE_INCLUDE_DIRECTORIES)
145
146# Set the protoc Executable
147get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
148  IMPORTED_LOCATION_RELEASE)
149if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
150  get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
151    IMPORTED_LOCATION_DEBUG)
152endif()
153if(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@")
160
161include(FindPackageHandleStandardArgs)
162FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf
163    REQUIRED_VARS Protobuf_PROTOC_EXECUTABLE Protobuf_LIBRARIES Protobuf_INCLUDE_DIRS
164    VERSION_VAR Protobuf_VERSION
165)
166
167# 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()
190