protobuf-module.cmake.in 5.9 KB

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