CMakeLists.txt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_CXX_STANDARD 14)
  36. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  37. set(CMAKE_POSITION_INDEPENDENT_CODE ${${QT_VERSIONED_PREFIX}_POSITION_INDEPENDENT_CODE})
  38. if(UNIX)
  39. if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  40. # using Clang
  41. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wno-pessimizing-move -Wno-mismatched-tags -Wno-unused-private-field -Wno-self-assign-overloaded")
  42. elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  43. # using GCC
  44. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wno-error=deprecated-declarations")
  45. endif()
  46. elseif(WIN32)
  47. if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
  48. #TODO: not all l4 warnings are fixed for MSVC. Enable options bellow
  49. # once fixed
  50. # set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX /W4 /MD")
  51. # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX /W4 /MD")
  52. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /bigobj /wd4005")
  53. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj /wd4005")
  54. else()
  55. message(WARNING "Only MSVC compilers family are supported for build")
  56. endif()
  57. endif()
  58. include(QtProtobufInternalHelpers)
  59. include(QtProtobufCoverage)
  60. include(QtProtobufGen)
  61. qt_protobuf_internal_check_install()
  62. set(PROTOBUF_QUICK_PLUGIN_NAME protobufquickplugin)
  63. set(GRPC_QUICK_PLUGIN_NAME grpcquickplugin)
  64. set(QT_PROTOBUF_MAKE_TESTS ON CACHE BOOL "Enable QtProtobuf tests build")
  65. set(QT_PROTOBUF_STANDALONE_TESTS OFF CACHE BOOL "Enable QtProtobuf tests build")
  66. set(QT_PROTOBUF_MAKE_EXAMPLES ON CACHE BOOL "Enable QtProtobuf examples build")
  67. set(QT_PROTOBUF_MAKE_COVERAGE OFF CACHE BOOL "Enable QtProtobuf build for profiler (gcov)")
  68. set(QT_PROTOBUF_FIELD_ENUM OFF CACHE BOOL "Enable generation of enumeration with fields numbers for well-known and Qt types libraries")
  69. set(QT_PROTOBUF_NATIVE_GRPC_CHANNEL OFF CACHE BOOL "Enable native gRPC channel implementation")
  70. if(NOT QT_PROTOBUF_STANDALONE_TESTS)
  71. if(QT_PROTOBUF_NATIVE_GRPC_CHANNEL)
  72. if(NOT WrapgRPC_FOUND OR NOT Threads_FOUND)
  73. message("QT_PROTOBUF_NATIVE_GRPC_CHANNEL is ON, but required dependencies are not found.")
  74. set(QT_PROTOBUF_NATIVE_GRPC_CHANNEL OFF)
  75. endif()
  76. endif()
  77. if(CMAKE_CROSSCOMPILING)
  78. set(QT_PROTOBUF_MAKE_TESTS OFF)
  79. set(QT_PROTOBUF_MAKE_EXAMPLES OFF)
  80. endif()
  81. set(QT_PROTOBUF_STATIC OFF CACHE INTERNAL "" FORCE)
  82. if(NOT BUILD_SHARED_LIBS)
  83. set(QT_PROTOBUF_STATIC ON CACHE INTERNAL "" FORCE)
  84. endif()
  85. set(extra_type_libraries_options "") #Prevent to set from outside
  86. if(QT_PROTOBUF_FIELD_ENUM)
  87. set(extra_type_libraries_options "FIELDENUM ${extra_type_libraries_options}")
  88. endif()
  89. configure_package_config_file(
  90. "${CMAKE_CURRENT_SOURCE_DIR}/ProjectConfig.cmake.in" "${QT_PROTOBUF_BINARY_DIR}/${QT_PROTOBUF_NAMESPACE}Config.cmake"
  91. INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake")
  92. install(FILES "${QT_PROTOBUF_BINARY_DIR}/${QT_PROTOBUF_NAMESPACE}Config.cmake"
  93. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${QT_PROTOBUF_NAMESPACE}"
  94. COMPONENT dev
  95. )
  96. export(PACKAGE ${QT_PROTOBUF_NAMESPACE})
  97. install(FILES "${QT_PROTOBUF_CMAKE_DIR}/FindWrapProtobuf.cmake"
  98. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${QT_PROTOBUF_NAMESPACE}"
  99. COMPONENT dev
  100. )
  101. if(NOT CMAKE_CROSSCOMPILING)
  102. add_subdirectory("src/generator")
  103. endif()
  104. add_subdirectory("src/protobuf")
  105. if(TARGET ${QT_VERSIONED_PREFIX}::Network)
  106. if(${QT_VERSIONED_PREFIX}Core_VERSION VERSION_LESS "5.12.4")
  107. # grpc target requires QT version not less than 5.12.4
  108. # earlier versions Http2DirectAttribute is broken: https://doc.qt.io/qt-5/whatsnew511.html
  109. # https://bugreports.qt.io/browse/QTBUG-74765
  110. message(WARNING "QtGrpc requires the Qt framework version 5.12.4 or higher")
  111. else()
  112. add_subdirectory("src/grpc")
  113. endif()
  114. endif()
  115. if(TARGET protobuf::libprotobuf AND TARGET protobuf::protoc)
  116. add_subdirectory("src/wellknowntypes")
  117. endif()
  118. if(TARGET ${QT_VERSIONED_PREFIX}::Gui AND TARGET protobuf::protoc)
  119. add_subdirectory("src/qttypes")
  120. endif()
  121. else()
  122. # Disable any installtion rules if build tests only
  123. set(QT_PROTOBUF_INSTALL FALSE CACHE INTERNAL "" FORCE)
  124. set(QT_PROTOBUF_MAKE_TESTS TRUE)
  125. endif()
  126. if(QT_PROTOBUF_MAKE_TESTS)
  127. find_package(WrapGTest)
  128. find_package(${QT_VERSIONED_PREFIX} OPTIONAL_COMPONENTS Test QuickTest CONFIG)
  129. if(WrapGTest_FOUND AND TARGET ${QT_VERSIONED_PREFIX}::Test)
  130. enable_testing()
  131. add_subdirectory("tests")
  132. else()
  133. message(STATUS "googletest not found. Force disable tests.")
  134. endif()
  135. endif()
  136. if(QT_PROTOBUF_MAKE_EXAMPLES AND NOT QT_PROTOBUF_STANDALONE_TESTS)
  137. if(TARGET ${QT_VERSIONED_PREFIX}::Quick)
  138. add_subdirectory("examples")
  139. else()
  140. message(STATUS "${QT_VERSIONED_PREFIX}::Quick is not found. Disable examples")
  141. endif()
  142. endif()
  143. if(QT_PROTOBUF_INSTALL)
  144. include(packaging)
  145. endif()
  146. include(doxygen)
  147. qt_protobuf_internal_install_targets()