protobuf-module.cmake.in 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # This file contains backwards compatibility patches for various legacy functions and variables
  2. # Functions
  3. function(PROTOBUF_GENERATE_CPP SRCS HDRS)
  4. if(NOT ARGN)
  5. message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files")
  6. return()
  7. endif()
  8. if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
  9. set(_append_arg APPEND_PATH)
  10. endif()
  11. if(DEFINED Protobuf_IMPORT_DIRS)
  12. set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS})
  13. endif()
  14. set(_outvar)
  15. protobuf_generate(${append_arg} LANGUAGE cpp OUT_VAR _outvar ${_import_arg} PROTOS ${ARGN})
  16. set(${SRCS})
  17. set(${HDRS})
  18. foreach(_file ${_outvar})
  19. if(_file MATCHES "cc$")
  20. list(APPEND ${SRCS} ${_file})
  21. else()
  22. list(APPEND ${HDRS} ${_file})
  23. endif()
  24. endforeach()
  25. set(${SRCS} ${${SRCS}} PARENT_SCOPE)
  26. set(${HDRS} ${${HDRS}} PARENT_SCOPE)
  27. endfunction()
  28. function(PROTOBUF_GENERATE_PYTHON SRCS)
  29. if(NOT ARGN)
  30. message(SEND_ERROR "Error: PROTOBUF_GENERATE_PYTHON() called without any proto files")
  31. return()
  32. endif()
  33. if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
  34. set(_append_arg APPEND_PATH)
  35. endif()
  36. if(DEFINED Protobuf_IMPORT_DIRS)
  37. set(_import_arg IMPORT_DIRS ${Protobuf_IMPORT_DIRS})
  38. endif()
  39. set(_outvar)
  40. protobuf_generate(${append_arg} LANGUAGE cpp OUT_VAR _outvar ${_import_arg} PROTOS ${ARGN})
  41. set(${SRCS} ${_outvar} PARENT_SCOPE)
  42. endfunction()
  43. # Environment
  44. # Backwards compatibility
  45. # Define camel case versions of input variables
  46. foreach(UPPER
  47. PROTOBUF_SRC_ROOT_FOLDER
  48. PROTOBUF_IMPORT_DIRS
  49. PROTOBUF_DEBUG
  50. PROTOBUF_LIBRARY
  51. PROTOBUF_PROTOC_LIBRARY
  52. PROTOBUF_INCLUDE_DIR
  53. PROTOBUF_PROTOC_EXECUTABLE
  54. PROTOBUF_LIBRARY_DEBUG
  55. PROTOBUF_PROTOC_LIBRARY_DEBUG
  56. PROTOBUF_LITE_LIBRARY
  57. PROTOBUF_LITE_LIBRARY_DEBUG
  58. )
  59. if (DEFINED ${UPPER})
  60. string(REPLACE "PROTOBUF_" "Protobuf_" Camel ${UPPER})
  61. if (NOT DEFINED ${Camel})
  62. set(${Camel} ${${UPPER}})
  63. endif()
  64. endif()
  65. endforeach()
  66. if(DEFINED Protobuf_SRC_ROOT_FOLDER)
  67. message(AUTHOR_WARNING "Variable Protobuf_SRC_ROOT_FOLDER defined, but not"
  68. " used in CONFIG mode")
  69. endif()
  70. include(SelectLibraryConfigurations)
  71. # Internal function: search for normal library as well as a debug one
  72. # if the debug one is specified also include debug/optimized keywords
  73. # in *_LIBRARIES variable
  74. function(_protobuf_find_libraries name filename)
  75. if(${name}_LIBRARIES)
  76. # Use result recorded by a previous call.
  77. elseif(${name}_LIBRARY)
  78. # Honor cache entry used by CMake 3.5 and lower.
  79. set(${name}_LIBRARIES "${${name}_LIBRARY}" PARENT_SCOPE)
  80. else()
  81. get_target_property(${name}_LIBRARY_RELEASE protobuf::lib${filename}
  82. LOCATION_RELEASE)
  83. get_target_property(${name}_LIBRARY_DEBUG protobuf::lib${filename}
  84. LOCATION_DEBUG)
  85. select_library_configurations(${name})
  86. set(${name}_LIBRARY ${${name}_LIBRARY} PARENT_SCOPE)
  87. set(${name}_LIBRARIES ${${name}_LIBRARIES} PARENT_SCOPE)
  88. endif()
  89. endfunction()
  90. # Internal function: find threads library
  91. function(_protobuf_find_threads)
  92. set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
  93. find_package(Threads)
  94. if(Threads_FOUND)
  95. list(APPEND PROTOBUF_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
  96. set(PROTOBUF_LIBRARIES "${PROTOBUF_LIBRARIES}" PARENT_SCOPE)
  97. endif()
  98. endfunction()
  99. #
  100. # Main.
  101. #
  102. # By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc
  103. # for each directory where a proto file is referenced.
  104. if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
  105. set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
  106. endif()
  107. # The Protobuf library
  108. _protobuf_find_libraries(Protobuf protobuf)
  109. # The Protobuf Lite library
  110. _protobuf_find_libraries(Protobuf_LITE protobuf-lite)
  111. # The Protobuf Protoc Library
  112. _protobuf_find_libraries(Protobuf_PROTOC protoc)
  113. if(UNIX)
  114. _protobuf_find_threads()
  115. endif()
  116. # Set the include directory
  117. get_target_property(Protobuf_INCLUDE_DIRS protobuf::libprotobuf
  118. INTERFACE_INCLUDE_DIRECTORIES)
  119. # Set the protoc Executable
  120. get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
  121. IMPORTED_LOCATION_RELEASE)
  122. if(NOT EXISTS "${Protobuf_PROTOC_EXECUTABLE}")
  123. get_target_property(Protobuf_PROTOC_EXECUTABLE protobuf::protoc
  124. IMPORTED_LOCATION_DEBUG)
  125. endif()
  126. # Version info variable
  127. set(Protobuf_VERSION "@protobuf_VERSION@")
  128. include(FindPackageHandleStandardArgs)
  129. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf
  130. REQUIRED_VARS Protobuf_PROTOC_EXECUTABLE Protobuf_LIBRARIES Protobuf_INCLUDE_DIRS
  131. VERSION_VAR Protobuf_VERSION
  132. )
  133. # Backwards compatibility
  134. # Define upper case versions of output variables
  135. foreach(Camel
  136. Protobuf_VERSION
  137. Protobuf_SRC_ROOT_FOLDER
  138. Protobuf_IMPORT_DIRS
  139. Protobuf_DEBUG
  140. Protobuf_INCLUDE_DIRS
  141. Protobuf_LIBRARIES
  142. Protobuf_PROTOC_LIBRARIES
  143. Protobuf_LITE_LIBRARIES
  144. Protobuf_LIBRARY
  145. Protobuf_PROTOC_LIBRARY
  146. Protobuf_INCLUDE_DIR
  147. Protobuf_PROTOC_EXECUTABLE
  148. Protobuf_LIBRARY_DEBUG
  149. Protobuf_PROTOC_LIBRARY_DEBUG
  150. Protobuf_LITE_LIBRARY
  151. Protobuf_LITE_LIBRARY_DEBUG
  152. )
  153. string(TOUPPER ${Camel} UPPER)
  154. set(${UPPER} ${${Camel}})
  155. endforeach()