CMakeLists.txt 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. cmake_minimum_required(VERSION 3.1)
  2. set(QTPROTOBUF_PROJECT_VERSION 0.1.0)
  3. project(QtProtobufProject VERSION ${QTPROTOBUF_PROJECT_VERSION} LANGUAGES CXX)
  4. include(GNUInstallDirs)
  5. find_package(Qt5 COMPONENTS Core Network Qml REQUIRED)
  6. if (Qt5Core_VERSION VERSION_LESS "5.11.0")
  7. # grpc target requires QT version not less than 5.11
  8. # earlier versions do not provide required Http2DirectAttribute: https://doc.qt.io/qt-5/whatsnew511.html
  9. message(FATAL_ERROR "Required Qt version is 5.11+")
  10. endif()
  11. if (Qt5Core_VERSION VERSION_LESS "5.11.4" OR (Qt5Core_VERSION VERSION_GREATER "5.12.0" AND Qt5Core_VERSION VERSION_LESS "5.12.3"))
  12. message(WARNING "Qt version<5.11.4 and (v5.12.0>=version<v5.12.3 has a bug in http2 connection handling. Tests using http2 feature and examples will not work!")
  13. endif()
  14. if(EXISTS "${CMAKE_SOURCE_DIR}/3rdparty/grpc/CMakeLists.txt")
  15. message(STATUS "Found local gRPC sources directory. Perform all-in-one build")
  16. set(gRPC_BUILD_TESTS FALSE)
  17. set(protobuf_BUILD_TESTS FALSE)
  18. add_subdirectory("3rdparty/grpc")
  19. if(TARGET grpc AND TARGET grpc++ AND TARGET grpc_cpp_plugin)
  20. add_library(gRPC::grpc ALIAS grpc)
  21. add_library(gRPC::grpc++ ALIAS grpc++)
  22. add_executable(gRPC::grpc_cpp_plugin ALIAS grpc_cpp_plugin)
  23. set(gRPC_FOUND TRUE)
  24. endif()
  25. else()
  26. include("cmake/ProtobufLookup.cmake")
  27. include("cmake/gRPCLookup.cmake")
  28. endif()
  29. set(QTPROTOBUF_COMMON_NAMESPACE QtProtobufProject)
  30. set(GENERATOR_TARGET qtprotobufgen)
  31. set(QTPROTOBUF_EXECUTABLE_INSTALL ${CMAKE_INSTALL_FULL_BINDIR}/${GENERATOR_TARGET})
  32. set(QTPROTOBUF_EXECUTABLE $<TARGET_FILE:${GENERATOR_TARGET}>)
  33. if(UNIX)
  34. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  35. # using Clang
  36. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wno-pessimizing-move -Wno-mismatched-tags -Wno-unused-private-field")
  37. elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  38. # using GCC
  39. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wno-error=deprecated-declarations")
  40. endif()
  41. elseif(WIN32)
  42. if (${CMAKE_CXX_COMPILER_ID} MATCHES "MSVC")
  43. set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "/WX /W4")
  44. else()
  45. message(WARNING "Only MSVC compilers family are supported for build")
  46. endif()
  47. endif()
  48. if(Qt5_POSITION_INDEPENDENT_CODE)
  49. set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
  50. endif()
  51. set(CMAKE_CXX_STANDARD 14)
  52. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  53. add_subdirectory("src/protobuf")
  54. add_subdirectory("src/grpc")
  55. add_subdirectory("src/generator")
  56. configure_package_config_file(
  57. "${CMAKE_CURRENT_SOURCE_DIR}/ProjectConfig.cmake.in" "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
  58. INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake")
  59. install(FILES "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
  60. export(PACKAGE ${PROJECT_NAME})
  61. # add a target to generate API documentation with Doxygen
  62. find_package(Doxygen)
  63. if(DOXYGEN_FOUND)
  64. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
  65. add_custom_target(doc
  66. ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
  67. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  68. COMMENT "Generating API documentation with Doxygen" VERBATIM
  69. )
  70. endif(DOXYGEN_FOUND)
  71. if(DEFINED $ENV{MAKE_TESTS})
  72. set(MAKE_TESTS $ENV{MAKE_TESTS})
  73. elseif(NOT DEFINED MAKE_TESTS)
  74. set(MAKE_TESTS ON)
  75. endif()
  76. if(DEFINED $ENV{MAKE_EXAMPLES})
  77. set(MAKE_EXAMPLES $ENV{MAKE_EXAMPLES})
  78. elseif(NOT DEFINED MAKE_EXAMPLES)
  79. set(MAKE_EXAMPLES ON)
  80. endif()
  81. if(DEFINED $ENV{MAKE_COVERAGE})
  82. set(MAKE_COVERAGE $ENV{MAKE_COVERAGE})
  83. elseif(NOT DEFINED MAKE_COVERAGE)
  84. set(MAKE_COVERAGE OFF)
  85. endif()
  86. if(MAKE_COVERAGE)
  87. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
  88. endif()
  89. if(NOT gRPC_FOUND)
  90. message(WARNING "gRPC not found: some tests and examples cannot be built.")
  91. message(STATUS "Force disable test")
  92. set(MAKE_TESTS OFF)
  93. message(STATUS "Force disable examples")
  94. set(MAKE_EXAMPLES OFF)
  95. endif()
  96. if(MAKE_TESTS)
  97. find_package(GTest)
  98. if (UNIX AND NOT GTest_FOUND)
  99. include(cmake/DownloadProject.cmake)
  100. download_project(PROJ GTest
  101. GIT_REPOSITORY https://github.com/google/googletest.git
  102. GIT_TAG v1.8.x
  103. )
  104. # uncomment if gmock is going to be needed
  105. set(BUILD_GMOCK OFF CACHE BOOL "Disable gmock build functionality" FORCE)
  106. add_subdirectory(${GTest_SOURCE_DIR} ${GTest_BINARY_DIR})
  107. endif()
  108. enable_testing()
  109. add_subdirectory("tests")
  110. endif()
  111. if(MAKE_EXAMPLES)
  112. add_subdirectory("examples")
  113. endif()