protobuf-module.cmake.in 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. # Functions
  2. function(PROTOBUF_GENERATE_CPP SRCS HDRS)
  3. if(NOT ARGN)
  4. message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
  5. return()
  6. endif()
  7. if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
  8. # Create an include path for each file specified
  9. foreach(FIL ${ARGN})
  10. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  11. get_filename_component(ABS_PATH ${ABS_FIL} PATH)
  12. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  13. if(${_contains_already} EQUAL -1)
  14. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  15. endif()
  16. endforeach()
  17. else()
  18. set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
  19. endif()
  20. if(DEFINED Protobuf_IMPORT_DIRS)
  21. foreach(DIR ${Protobuf_IMPORT_DIRS})
  22. get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
  23. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  24. if(${_contains_already} EQUAL -1)
  25. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  26. endif()
  27. endforeach()
  28. endif()
  29. set(${SRCS})
  30. set(${HDRS})
  31. foreach(FIL ${ARGN})
  32. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  33. get_filename_component(FIL_WE ${FIL} NAME_WE)
  34. list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc")
  35. list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")
  36. add_custom_command(
  37. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc"
  38. "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h"
  39. COMMAND ${Protobuf_PROTOC_EXECUTABLE}
  40. ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL}
  41. DEPENDS ${ABS_FIL} ${Protobuf_PROTOC_EXECUTABLE}
  42. COMMENT "Running C++ protocol buffer compiler on ${FIL}"
  43. VERBATIM )
  44. endforeach()
  45. set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
  46. set(${SRCS} ${${SRCS}} PARENT_SCOPE)
  47. set(${HDRS} ${${HDRS}} PARENT_SCOPE)
  48. endfunction()
  49. function(PROTOBUF_GENERATE_PYTHON SRCS)
  50. if(NOT ARGN)
  51. message(SEND_ERROR "Error: PROTOBUF_GENERATE_PYTHON() called without any proto files")
  52. return()
  53. endif()
  54. if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
  55. # Create an include path for each file specified
  56. foreach(FIL ${ARGN})
  57. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  58. get_filename_component(ABS_PATH ${ABS_FIL} PATH)
  59. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  60. if(${_contains_already} EQUAL -1)
  61. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  62. endif()
  63. endforeach()
  64. else()
  65. set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
  66. endif()
  67. if(DEFINED Protobuf_IMPORT_DIRS)
  68. foreach(DIR ${Protobuf_IMPORT_DIRS})
  69. get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
  70. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  71. if(${_contains_already} EQUAL -1)
  72. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  73. endif()
  74. endforeach()
  75. endif()
  76. set(${SRCS})
  77. foreach(FIL ${ARGN})
  78. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  79. get_filename_component(FIL_WE ${FIL} NAME_WE)
  80. list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}_pb2.py")
  81. add_custom_command(
  82. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}_pb2.py"
  83. COMMAND ${Protobuf_PROTOC_EXECUTABLE} --python_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL}
  84. DEPENDS ${ABS_FIL} ${Protobuf_PROTOC_EXECUTABLE}
  85. COMMENT "Running Python protocol buffer compiler on ${FIL}"
  86. VERBATIM )
  87. endforeach()
  88. set(${SRCS} ${${SRCS}} PARENT_SCOPE)
  89. endfunction()
  90. # Environment
  91. # Backwards compatibility
  92. # Define camel case versions of input variables
  93. foreach(UPPER
  94. PROTOBUF_SRC_ROOT_FOLDER
  95. PROTOBUF_IMPORT_DIRS
  96. PROTOBUF_DEBUG
  97. PROTOBUF_LIBRARY
  98. PROTOBUF_PROTOC_LIBRARY
  99. PROTOBUF_INCLUDE_DIR
  100. PROTOBUF_PROTOC_EXECUTABLE
  101. PROTOBUF_LIBRARY_DEBUG
  102. PROTOBUF_PROTOC_LIBRARY_DEBUG
  103. PROTOBUF_LITE_LIBRARY
  104. PROTOBUF_LITE_LIBRARY_DEBUG
  105. )
  106. if (DEFINED ${UPPER})
  107. string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER})
  108. if (NOT DEFINED ${Camel})
  109. set(${Camel} ${${UPPER}})
  110. endif()
  111. endif()
  112. endforeach()
  113. if(DEFINED Protobuf_SRC_ROOT_FOLDER)
  114. message(AUTHOR_WARNING "Variable Protobuf_SRC_ROOT_FOLDER defined, but not"
  115. " used in CONFIG mode")
  116. endif()
  117. include(SelectLibraryConfigurations)
  118. # Internal function: search for normal library as well as a debug one
  119. # if the debug one is specified also include debug/optimized keywords
  120. # in *_LIBRARIES variable
  121. function(_protobuf_find_libraries name filename)
  122. if(${name}_LIBRARIES)
  123. # Use result recorded by a previous call.
  124. elseif(${name}_LIBRARY)
  125. # Honor cache entry used by CMake 3.5 and lower.
  126. set(${name}_LIBRARIES "${${name}_LIBRARY}" PARENT_SCOPE)
  127. else()
  128. get_target_property(${name}_LIBRARY_RELEASE protobuf::lib${filename}
  129. LOCATION_RELEASE)
  130. get_target_property(${name}_LIBRARY_DEBUG protobuf::lib${filename}
  131. LOCATION_DEBUG)
  132. select_library_configurations(${name})
  133. set(${name}_LIBRARY ${${name}_LIBRARY} PARENT_SCOPE)
  134. set(${name}_LIBRARIES ${${name}_LIBRARIES} PARENT_SCOPE)
  135. endif()
  136. endfunction()
  137. # Internal function: find threads library
  138. function(_protobuf_find_threads)
  139. set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
  140. find_package(Threads)
  141. if(Threads_FOUND)
  142. list(APPEND PROTOBUF_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
  143. set(PROTOBUF_LIBRARIES "${PROTOBUF_LIBRARIES}" PARENT_SCOPE)
  144. endif()
  145. endfunction()
  146. #
  147. # Main.
  148. #
  149. # By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc
  150. # for each directory where a proto file is referenced.
  151. if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
  152. set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
  153. endif()
  154. # The Protobuf library
  155. _protobuf_find_libraries(Protobuf protobuf)
  156. # The Protobuf Lite library
  157. _protobuf_find_libraries(Protobuf_LITE protobuf-lite)
  158. # The Protobuf Protoc Library
  159. _protobuf_find_libraries(Protobuf_PROTOC protoc)
  160. if(UNIX)
  161. _protobuf_find_threads()
  162. endif()
  163. # Set the include directory
  164. get_target_property(Protobuf_INCLUDE_DIRS protobuf::libprotobuf
  165. INTERFACE_INCLUDE_DIRECTORIES)
  166. # Set the protoc Executable
  167. get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
  168. IMPORTED_LOCATION_RELEASE)
  169. if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
  170. get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
  171. IMPORTED_LOCATION_DEBUG)
  172. endif()
  173. # Version info variable
  174. set(Protobuf_VERSION "@protobuf_VERSION@")
  175. include(FindPackageHandleStandardArgs)
  176. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf
  177. REQUIRED_VARS Protobuf_PROTOC_EXECUTABLE Protobuf_LIBRARIES Protobuf_INCLUDE_DIRS
  178. VERSION_VAR Protobuf_VERSION
  179. )
  180. # Backwards compatibility
  181. # Define upper case versions of output variables
  182. foreach(Camel
  183. Protobuf_VERSION
  184. Protobuf_SRC_ROOT_FOLDER
  185. Protobuf_IMPORT_DIRS
  186. Protobuf_DEBUG
  187. Protobuf_INCLUDE_DIRS
  188. Protobuf_LIBRARIES
  189. Protobuf_PROTOC_LIBRARIES
  190. Protobuf_LITE_LIBRARIES
  191. Protobuf_LIBRARY
  192. Protobuf_PROTOC_LIBRARY
  193. Protobuf_INCLUDE_DIR
  194. Protobuf_PROTOC_EXECUTABLE
  195. Protobuf_LIBRARY_DEBUG
  196. Protobuf_PROTOC_LIBRARY_DEBUG
  197. Protobuf_LITE_LIBRARY
  198. Protobuf_LITE_LIBRARY_DEBUG
  199. )
  200. string(TOUPPER ${Camel} UPPER)
  201. set(${UPPER} ${${Camel}})
  202. endforeach()