CMakeLists.txt 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. cmake_minimum_required(VERSION 3.16...3.22)
  2. set(QT_PROTOBUF_VERSION 0.6.0)
  3. set(QT_PROTOBUF_NAMESPACE QtProtobuf)
  4. set(QT_PREFIX Qt)
  5. set(QT_VERSIONED_PREFIX Qt5)
  6. project(${QT_PROTOBUF_NAMESPACE} VERSION ${QT_PROTOBUF_VERSION} LANGUAGES CXX)
  7. # Conan support, optional
  8. if(EXISTS "${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
  9. include("${CMAKE_BINARY_DIR}/conanbuildinfo.cmake")
  10. conan_basic_setup()
  11. endif()
  12. include(GNUInstallDirs)
  13. include(CMakePackageConfigHelpers)
  14. set(QT_PROTOBUF_CMAKE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
  15. set(QT_PROTOBUF_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
  16. set(QT_PROTOBUF_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
  17. list(PREPEND CMAKE_MODULE_PATH ${QT_PROTOBUF_CMAKE_DIR})
  18. find_package(WrapgRPC)
  19. if(NOT CMAKE_CROSSCOMPILING)
  20. find_package(WrapProtobuf COMPONENTS Protoc OPTIONAL_COMPONENTS Generator Protobuf REQUIRED)
  21. else()
  22. if(NOT QT_PROTOBUF_HOST_PATH)
  23. message(FATAL_ERROR "QT_PROTOBUF_HOST_PATH is not specified. You shall specify
  24. QT_PROTOBUF_HOST_PATH to find the host version of QtProtobufGenerator")
  25. endif()
  26. find_package(${QT_PROTOBUF_NAMESPACE} COMPONENTS ProtobufGenerator CONFIG REQUIRED
  27. PATHS ${QT_PROTOBUF_HOST_PATH}
  28. NO_DEFAULT_PATH
  29. )
  30. endif()
  31. find_package(Threads)
  32. find_package(${QT_VERSIONED_PREFIX} COMPONENTS Core Qml CONFIG REQUIRED)
  33. find_package(${QT_VERSIONED_PREFIX} OPTIONAL_COMPONENTS Network Quick Gui CONFIG)
  34. set(CMAKE_AUTOMOC ON)
  35. set(CMAKE_AUTORCC ON)
  36. set(CMAKE_CXX_STANDARD 14)
  37. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  38. set(CMAKE_POSITION_INDEPENDENT_CODE ${${QT_VERSIONED_PREFIX}_POSITION_INDEPENDENT_CODE})
  39. if(UNIX)
  40. if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  41. # using Clang
  42. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wno-pessimizing-move -Wno-mismatched-tags -Wno-unused-private-field -Wno-self-assign-overloaded")
  43. elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  44. # using GCC
  45. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wno-error=deprecated-declarations")
  46. endif()
  47. elseif(WIN32)
  48. if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
  49. #TODO: not all l4 warnings are fixed for MSVC. Enable options bellow
  50. # once fixed
  51. # set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX /W4 /MD")
  52. # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX /W4 /MD")
  53. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /bigobj /wd4005")
  54. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj /wd4005")
  55. else()
  56. message(WARNING "Only MSVC compilers family are supported for build")
  57. endif()
  58. endif()
  59. include(QtProtobufInternalHelpers)
  60. include(QtProtobufCoverage)
  61. include(QtProtobufGen)
  62. qt_protobuf_internal_check_install()
  63. set(PROTOBUF_QUICK_PLUGIN_NAME protobufquickplugin)
  64. set(GRPC_QUICK_PLUGIN_NAME grpcquickplugin)
  65. set(QT_PROTOBUF_MAKE_TESTS ON CACHE BOOL "Enable QtProtobuf tests build")
  66. set(QT_PROTOBUF_STANDALONE_TESTS OFF CACHE BOOL "Enable QtProtobuf tests build")
  67. set(QT_PROTOBUF_MAKE_EXAMPLES ON CACHE BOOL "Enable QtProtobuf examples build")
  68. set(QT_PROTOBUF_MAKE_COVERAGE OFF CACHE BOOL "Enable QtProtobuf build for profiler (gcov)")
  69. set(QT_PROTOBUF_FIELD_ENUM OFF CACHE BOOL "Enable generation of enumeration with fields numbers for well-known and Qt types libraries")
  70. set(QT_PROTOBUF_NATIVE_GRPC_CHANNEL OFF CACHE BOOL "Enable native gRPC channel implementation")
  71. if(NOT QT_PROTOBUF_STANDALONE_TESTS)
  72. if(QT_PROTOBUF_NATIVE_GRPC_CHANNEL)
  73. if(NOT WrapgRPC_FOUND OR NOT Threads_FOUND)
  74. message("QT_PROTOBUF_NATIVE_GRPC_CHANNEL is ON, but required dependencies are not found.")
  75. set(QT_PROTOBUF_NATIVE_GRPC_CHANNEL OFF)
  76. endif()
  77. endif()
  78. if(CMAKE_CROSSCOMPILING)
  79. set(QT_PROTOBUF_MAKE_TESTS OFF)
  80. set(QT_PROTOBUF_MAKE_EXAMPLES OFF)
  81. endif()
  82. set(QT_PROTOBUF_STATIC OFF CACHE INTERNAL "" FORCE)
  83. if(NOT BUILD_SHARED_LIBS)
  84. set(QT_PROTOBUF_STATIC ON CACHE INTERNAL "" FORCE)
  85. endif()
  86. set(extra_type_libraries_options "") #Prevent to set from outside
  87. if(QT_PROTOBUF_FIELD_ENUM)
  88. set(extra_type_libraries_options "FIELDENUM ${extra_type_libraries_options}")
  89. endif()
  90. configure_package_config_file(
  91. "${CMAKE_CURRENT_SOURCE_DIR}/ProjectConfig.cmake.in" "${QT_PROTOBUF_BINARY_DIR}/${QT_PROTOBUF_NAMESPACE}Config.cmake"
  92. INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake")
  93. install(FILES "${QT_PROTOBUF_BINARY_DIR}/${QT_PROTOBUF_NAMESPACE}Config.cmake"
  94. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${QT_PROTOBUF_NAMESPACE}"
  95. COMPONENT dev
  96. )
  97. export(PACKAGE ${QT_PROTOBUF_NAMESPACE})
  98. install(FILES "${QT_PROTOBUF_CMAKE_DIR}/FindWrapProtobuf.cmake"
  99. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${QT_PROTOBUF_NAMESPACE}"
  100. COMPONENT dev
  101. )
  102. if(NOT CMAKE_CROSSCOMPILING)
  103. add_subdirectory("src/generator")
  104. endif()
  105. add_subdirectory("src/protobuf")
  106. if(TARGET ${QT_VERSIONED_PREFIX}::Network)
  107. if(${QT_VERSIONED_PREFIX}Core_VERSION VERSION_LESS "5.12.4")
  108. # grpc target requires QT version not less than 5.12.4
  109. # earlier versions Http2DirectAttribute is broken: https://doc.qt.io/qt-5/whatsnew511.html
  110. # https://bugreports.qt.io/browse/QTBUG-74765
  111. message(WARNING "QtGrpc requires the Qt framework version 5.12.4 or higher")
  112. else()
  113. add_subdirectory("src/grpc")
  114. endif()
  115. endif()
  116. if(TARGET protobuf::libprotobuf AND TARGET protobuf::protoc)
  117. add_subdirectory("src/wellknowntypes")
  118. endif()
  119. if(TARGET ${QT_VERSIONED_PREFIX}::Gui AND TARGET protobuf::protoc)
  120. add_subdirectory("src/qttypes")
  121. endif()
  122. else()
  123. # Disable any installtion rules if build tests only
  124. set(QT_PROTOBUF_INSTALL FALSE CACHE INTERNAL "" FORCE)
  125. set(QT_PROTOBUF_MAKE_TESTS TRUE)
  126. endif()
  127. if(QT_PROTOBUF_MAKE_TESTS)
  128. find_package(WrapGTest)
  129. find_package(${QT_VERSIONED_PREFIX} OPTIONAL_COMPONENTS Test QuickTest CONFIG)
  130. if(WrapGTest_FOUND AND TARGET ${QT_VERSIONED_PREFIX}::Test)
  131. enable_testing()
  132. add_subdirectory("tests")
  133. else()
  134. message(STATUS "googletest not found. Force disable tests.")
  135. endif()
  136. endif()
  137. if(QT_PROTOBUF_MAKE_EXAMPLES AND NOT QT_PROTOBUF_STANDALONE_TESTS)
  138. if(TARGET ${QT_VERSIONED_PREFIX}::Quick)
  139. add_subdirectory("examples")
  140. else()
  141. message(STATUS "${QT_VERSIONED_PREFIX}::Quick is not found. Disable examples")
  142. endif()
  143. endif()
  144. if(QT_PROTOBUF_INSTALL)
  145. include(packaging)
  146. endif()
  147. include(doxygen)
  148. qt_protobuf_internal_install_targets()