CMakeLists.txt 5.4 KB

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