protobuf-module.cmake.in 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. if(PROTOBUF_SRC_ROOT_FOLDER)
  2. message(AUTHOR_WARNING "Variable PROTOBUF_SRC_ROOT_FOLDER defined, but not"
  3. " used in CONFIG mode")
  4. endif()
  5. function(PROTOBUF_GENERATE_CPP SRCS HDRS)
  6. if(NOT ARGN)
  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. # Create an include path for each file specified
  12. foreach(FIL ${ARGN})
  13. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  14. get_filename_component(ABS_PATH ${ABS_FIL} PATH)
  15. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  16. if(${_contains_already} EQUAL -1)
  17. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  18. endif()
  19. endforeach()
  20. else()
  21. set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
  22. endif()
  23. # Add well-known type protos include path
  24. list(APPEND _protobuf_include_path
  25. -I "${_PROTOBUF_IMPORT_PREFIX}/@CMAKE_INSTALL_INCLUDEDIR@")
  26. if(DEFINED PROTOBUF_IMPORT_DIRS)
  27. foreach(DIR ${PROTOBUF_IMPORT_DIRS})
  28. get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
  29. list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
  30. if(${_contains_already} EQUAL -1)
  31. list(APPEND _protobuf_include_path -I ${ABS_PATH})
  32. endif()
  33. endforeach()
  34. endif()
  35. set(${SRCS})
  36. set(${HDRS})
  37. foreach(FIL ${ARGN})
  38. get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
  39. get_filename_component(FIL_WE ${FIL} NAME_WE)
  40. list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc")
  41. list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h")
  42. add_custom_command(
  43. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc"
  44. "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h"
  45. COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
  46. ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL}
  47. DEPENDS ${ABS_FIL} ${PROTOBUF_PROTOC_EXECUTABLE}
  48. COMMENT "Running C++ protocol buffer compiler on ${FIL}"
  49. VERBATIM)
  50. endforeach()
  51. set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE)
  52. set(${SRCS} ${${SRCS}} PARENT_SCOPE)
  53. set(${HDRS} ${${HDRS}} PARENT_SCOPE)
  54. endfunction()
  55. # Internal function: search for normal library as well as a debug one
  56. # if the debug one is specified also include debug/optimized keywords
  57. # in *_LIBRARIES variable
  58. function(_protobuf_find_libraries name filename)
  59. get_target_property(${name}_LIBRARY lib${filename}
  60. IMPORTED_LOCATION_RELEASE)
  61. set(${name}_LIBRARY "${${name}_LIBRARY}" PARENT_SCOPE)
  62. get_target_property(${name}_LIBRARY_DEBUG lib${filename}
  63. IMPORTED_LOCATION_DEBUG)
  64. set(${name}_LIBRARY_DEBUG "${${name}_LIBRARY_DEBUG}" PARENT_SCOPE)
  65. if(NOT ${name}_LIBRARY_DEBUG)
  66. # There is no debug library
  67. set(${name}_LIBRARY_DEBUG ${${name}_LIBRARY} PARENT_SCOPE)
  68. set(${name}_LIBRARIES ${${name}_LIBRARY} PARENT_SCOPE)
  69. else()
  70. # There IS a debug library
  71. set(${name}_LIBRARIES
  72. optimized ${${name}_LIBRARY}
  73. debug ${${name}_LIBRARY_DEBUG}
  74. PARENT_SCOPE
  75. )
  76. endif()
  77. endfunction()
  78. # Internal function: find threads library
  79. function(_protobuf_find_threads)
  80. set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
  81. find_package(Threads)
  82. if(Threads_FOUND)
  83. list(APPEND PROTOBUF_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
  84. set(PROTOBUF_LIBRARIES "${PROTOBUF_LIBRARIES}" PARENT_SCOPE)
  85. endif()
  86. endfunction()
  87. #
  88. # Main.
  89. #
  90. # By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc
  91. # for each directory where a proto file is referenced.
  92. if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH)
  93. set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE)
  94. endif()
  95. # The Protobuf library
  96. _protobuf_find_libraries(PROTOBUF protobuf)
  97. # The Protobuf Lite library
  98. _protobuf_find_libraries(PROTOBUF_LITE protobuf-lite)
  99. # The Protobuf Protoc Library
  100. _protobuf_find_libraries(PROTOBUF_PROTOC protoc)
  101. if(UNIX)
  102. _protobuf_find_threads()
  103. endif()
  104. # Set the include directory
  105. set(PROTOBUF_INCLUDE_DIR "${_PROTOBUF_IMPORT_PREFIX}/@CMAKE_INSTALL_INCLUDEDIR@")
  106. # Set the protoc Executable
  107. get_target_property(PROTOBUF_PROTOC_EXECUTABLE protoc
  108. IMPORTED_LOCATION_RELEASE)
  109. if(NOT PROTOBUF_PROTOC_EXECUTABLE)
  110. get_target_property(PROTOBUF_PROTOC_EXECUTABLE protoc
  111. IMPORTED_LOCATION_DEBUG)
  112. endif()
  113. include(FindPackageHandleStandardArgs)
  114. FIND_PACKAGE_HANDLE_STANDARD_ARGS(PROTOBUF DEFAULT_MSG
  115. PROTOBUF_LIBRARY PROTOBUF_INCLUDE_DIR)
  116. if(PROTOBUF_FOUND)
  117. set(PROTOBUF_INCLUDE_DIRS ${PROTOBUF_INCLUDE_DIR})
  118. endif()