CMakeLists.txt 6.5 KB

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