protobuf-module.cmake.in 7.7 KB

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