protobuf-module.cmake.in 5.6 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. elseif(TARGET protobuf::lib${filename})
  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. if(NOT Protobuf_PROTOC_EXECUTABLE AND TARGET protobuf::protoc)
  115. get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
  116. IMPORTED_LOCATION_RELEASE)
  117. if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
  118. get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
  119. IMPORTED_LOCATION_RELWITHDEBINFO)
  120. endif()
  121. if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
  122. get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
  123. IMPORTED_LOCATION_MINSIZEREL)
  124. endif()
  125. if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
  126. get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
  127. IMPORTED_LOCATION_DEBUG)
  128. endif()
  129. if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
  130. get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
  131. IMPORTED_LOCATION_NOCONFIG)
  132. endif()
  133. endif()
  134. # Version info variable
  135. set(Protobuf_VERSION "@protobuf_VERSION@")
  136. include(FindPackageHandleStandardArgs)
  137. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf
  138. REQUIRED_VARS Protobuf_PROTOC_EXECUTABLE Protobuf_LIBRARIES Protobuf_INCLUDE_DIRS
  139. VERSION_VAR Protobuf_VERSION
  140. )
  141. # Backwards compatibility
  142. # Define upper case versions of output variables
  143. foreach(Camel
  144. Protobuf_VERSION
  145. Protobuf_SRC_ROOT_FOLDER
  146. Protobuf_IMPORT_DIRS
  147. Protobuf_DEBUG
  148. Protobuf_INCLUDE_DIRS
  149. Protobuf_LIBRARIES
  150. Protobuf_PROTOC_LIBRARIES
  151. Protobuf_LITE_LIBRARIES
  152. Protobuf_LIBRARY
  153. Protobuf_PROTOC_LIBRARY
  154. Protobuf_INCLUDE_DIR
  155. Protobuf_PROTOC_EXECUTABLE
  156. Protobuf_LIBRARY_DEBUG
  157. Protobuf_PROTOC_LIBRARY_DEBUG
  158. Protobuf_LITE_LIBRARY
  159. Protobuf_LITE_LIBRARY_DEBUG
  160. )
  161. string(TOUPPER ${Camel} UPPER)
  162. set(${UPPER} ${${Camel}})
  163. endforeach()