protobuf-module.cmake.in 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. endif()
  133. select_library_configurations(${name})
  134. set(${name}_LIBRARY ${${name}_LIBRARY} PARENT_SCOPE)
  135. set(${name}_LIBRARIES ${${name}_LIBRARIES} PARENT_SCOPE)
  136. endif()
  137. endfunction()
  138. # Internal function: find threads library
  139. function(_protobuf_find_threads)
  140. set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
  141. find_package(Threads)
  142. if(Threads_FOUND)
  143. list(APPEND PROTOBUF_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
  144. set(PROTOBUF_LIBRARIES "${PROTOBUF_LIBRARIES}" PARENT_SCOPE)
  145. endif()
  146. endfunction()
  147. #
  148. # Main.
  149. #
  150. # By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc
  151. # for each directory where a proto file is referenced.
  152. if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
  153. set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
  154. endif()
  155. # The Protobuf library
  156. _protobuf_find_libraries(Protobuf protobuf)
  157. # The Protobuf Lite library
  158. _protobuf_find_libraries(Protobuf_LITE protobuf-lite)
  159. # The Protobuf Protoc Library
  160. _protobuf_find_libraries(Protobuf_PROTOC protoc)
  161. if(UNIX)
  162. _protobuf_find_threads()
  163. endif()
  164. # Set the include directory
  165. get_target_property(Protobuf_INCLUDE_DIRS protobuf::libprotobuf
  166. INTERFACE_INCLUDE_DIRECTORIES)
  167. # Set the protoc Executable
  168. get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
  169. IMPORTED_LOCATION_RELEASE)
  170. if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
  171. get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
  172. IMPORTED_LOCATION_DEBUG)
  173. endif()
  174. # Version info variable
  175. set(Protobuf_VERSION "@protobuf_VERSION@")
  176. include(FindPackageHandleStandardArgs)
  177. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf
  178. REQUIRED_VARS Protobuf_PROTOC_EXECUTABLE Protobuf_LIBRARIES Protobuf_INCLUDE_DIRS
  179. VERSION_VAR Protobuf_VERSION
  180. )
  181. # Backwards compatibility
  182. # Define upper case versions of output variables
  183. foreach(Camel
  184. Protobuf_VERSION
  185. Protobuf_SRC_ROOT_FOLDER
  186. Protobuf_IMPORT_DIRS
  187. Protobuf_DEBUG
  188. Protobuf_INCLUDE_DIRS
  189. Protobuf_LIBRARIES
  190. Protobuf_PROTOC_LIBRARIES
  191. Protobuf_LITE_LIBRARIES
  192. Protobuf_LIBRARY
  193. Protobuf_PROTOC_LIBRARY
  194. Protobuf_INCLUDE_DIR
  195. Protobuf_PROTOC_EXECUTABLE
  196. Protobuf_LIBRARY_DEBUG
  197. Protobuf_PROTOC_LIBRARY_DEBUG
  198. Protobuf_LITE_LIBRARY
  199. Protobuf_LITE_LIBRARY_DEBUG
  200. )
  201. string(TOUPPER ${Camel} UPPER)
  202. set(${UPPER} ${${Camel}})
  203. endforeach()