CMakeLists.txt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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")
  40. endif()
  41. elseif(WIN32)
  42. set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-Wall" "-Werror")
  43. endif()
  44. if(Qt5_POSITION_INDEPENDENT_CODE)
  45. set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
  46. endif()
  47. set(CMAKE_CXX_STANDARD 14)
  48. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  49. add_subdirectory("src/protobuf")
  50. add_subdirectory("src/grpc")
  51. add_subdirectory("src/generator")
  52. configure_package_config_file(
  53. "${CMAKE_CURRENT_SOURCE_DIR}/ProjectConfig.cmake.in" "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
  54. INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake")
  55. install(FILES "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
  56. export(PACKAGE ${PROJECT_NAME})
  57. # add a target to generate API documentation with Doxygen
  58. find_package(Doxygen)
  59. if(DOXYGEN_FOUND)
  60. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
  61. add_custom_target(doc
  62. ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
  63. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  64. COMMENT "Generating API documentation with Doxygen" VERBATIM
  65. )
  66. endif(DOXYGEN_FOUND)
  67. if(DEFINED $ENV{MAKE_TESTS})
  68. set(MAKE_TESTS $ENV{MAKE_TESTS})
  69. elseif(NOT DEFINED MAKE_TESTS)
  70. set(MAKE_TESTS ON)
  71. endif()
  72. if(DEFINED $ENV{MAKE_EXAMPLES})
  73. set(MAKE_EXAMPLES $ENV{MAKE_EXAMPLES})
  74. elseif(NOT DEFINED MAKE_EXAMPLES)
  75. set(MAKE_EXAMPLES ON)
  76. endif()
  77. if(DEFINED $ENV{MAKE_COVERAGE})
  78. set(MAKE_COVERAGE $ENV{MAKE_COVERAGE})
  79. elseif(NOT DEFINED MAKE_COVERAGE)
  80. set(MAKE_COVERAGE OFF)
  81. endif()
  82. if(MAKE_COVERAGE)
  83. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
  84. endif()
  85. if(NOT gRPC_FOUND)
  86. message(WARNING "gRPC not found: some tests and examples cannot be built.")
  87. message(STATUS "Force disable test")
  88. set(MAKE_TESTS OFF)
  89. message(STATUS "Force disable examples")
  90. set(MAKE_EXAMPLES OFF)
  91. endif()
  92. if(MAKE_TESTS)
  93. find_package(GTest)
  94. if (UNIX AND NOT GTest_FOUND)
  95. include(cmake/DownloadProject.cmake)
  96. download_project(PROJ GTest
  97. GIT_REPOSITORY https://github.com/google/googletest.git
  98. GIT_TAG v1.8.x
  99. )
  100. # uncomment if gmock is going to be needed
  101. set(BUILD_GMOCK OFF CACHE BOOL "Disable gmock build functionality" FORCE)
  102. add_subdirectory(${GTest_SOURCE_DIR} ${GTest_BINARY_DIR})
  103. endif()
  104. enable_testing()
  105. add_subdirectory("tests")
  106. endif()
  107. if(MAKE_EXAMPLES)
  108. add_subdirectory("examples")
  109. endif()