protobuf-module.cmake 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #
  97. # Main.
  98. #
  99. # By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc
  100. # for each directory where a proto file is referenced.
  101. if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
  102. set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
  103. endif()
  104. # The Protobuf library
  105. _protobuf_find_libraries(Protobuf protobuf)
  106. # The Protobuf Lite library
  107. _protobuf_find_libraries(Protobuf_LITE protobuf-lite)
  108. # The Protobuf Protoc Library
  109. _protobuf_find_libraries(Protobuf_PROTOC protoc)
  110. # Set the include directory
  111. get_target_property(Protobuf_INCLUDE_DIRS protobuf::libprotobuf
  112. INTERFACE_INCLUDE_DIRECTORIES)
  113. # Set the protoc Executable
  114. get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
  115. IMPORTED_LOCATION_RELEASE)
  116. if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
  117. get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
  118. IMPORTED_LOCATION_RELWITHDEBINFO)
  119. endif()
  120. if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
  121. get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
  122. IMPORTED_LOCATION_MINSIZEREL)
  123. endif()
  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 "3.19.4.0")
  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()