protobuf-module.cmake.in 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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_DEBUG protobuf::lib${filename}
  86. LOCATION_DEBUG)
  87. select_library_configurations(${name})
  88. set(${name}_LIBRARY ${${name}_LIBRARY} PARENT_SCOPE)
  89. set(${name}_LIBRARIES ${${name}_LIBRARIES} PARENT_SCOPE)
  90. endif()
  91. endfunction()
  92. # Internal function: find threads library
  93. function(_protobuf_find_threads)
  94. set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
  95. find_package(Threads)
  96. if(Threads_FOUND)
  97. list(APPEND PROTOBUF_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
  98. set(PROTOBUF_LIBRARIES "${PROTOBUF_LIBRARIES}" PARENT_SCOPE)
  99. endif()
  100. endfunction()
  101. #
  102. # Main.
  103. #
  104. # By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc
  105. # for each directory where a proto file is referenced.
  106. if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
  107. set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
  108. endif()
  109. # The Protobuf library
  110. _protobuf_find_libraries(Protobuf protobuf)
  111. # The Protobuf Lite library
  112. _protobuf_find_libraries(Protobuf_LITE protobuf-lite)
  113. # The Protobuf Protoc Library
  114. _protobuf_find_libraries(Protobuf_PROTOC protoc)
  115. if(UNIX)
  116. _protobuf_find_threads()
  117. endif()
  118. # Set the include directory
  119. get_target_property(Protobuf_INCLUDE_DIRS protobuf::libprotobuf
  120. INTERFACE_INCLUDE_DIRECTORIES)
  121. # Set the protoc Executable
  122. get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
  123. IMPORTED_LOCATION_RELEASE)
  124. if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
  125. get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
  126. IMPORTED_LOCATION_DEBUG)
  127. endif()
  128. if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
  129. get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
  130. IMPORTED_LOCATION_NOCONFIG)
  131. endif()
  132. # Version info variable
  133. set(Protobuf_VERSION "@protobuf_VERSION@")
  134. include(FindPackageHandleStandardArgs)
  135. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf
  136. REQUIRED_VARS Protobuf_PROTOC_EXECUTABLE Protobuf_LIBRARIES Protobuf_INCLUDE_DIRS
  137. VERSION_VAR Protobuf_VERSION
  138. )
  139. # Backwards compatibility
  140. # Define upper case versions of output variables
  141. foreach(Camel
  142. Protobuf_VERSION
  143. Protobuf_SRC_ROOT_FOLDER
  144. Protobuf_IMPORT_DIRS
  145. Protobuf_DEBUG
  146. Protobuf_INCLUDE_DIRS
  147. Protobuf_LIBRARIES
  148. Protobuf_PROTOC_LIBRARIES
  149. Protobuf_LITE_LIBRARIES
  150. Protobuf_LIBRARY
  151. Protobuf_PROTOC_LIBRARY
  152. Protobuf_INCLUDE_DIR
  153. Protobuf_PROTOC_EXECUTABLE
  154. Protobuf_LIBRARY_DEBUG
  155. Protobuf_PROTOC_LIBRARY_DEBUG
  156. Protobuf_LITE_LIBRARY
  157. Protobuf_LITE_LIBRARY_DEBUG
  158. )
  159. string(TOUPPER ${Camel} UPPER)
  160. set(${UPPER} ${${Camel}})
  161. endforeach()