CMakeLists.txt 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. include("cmake/ProtobufLookup.cmake")
  15. include("cmake/gRPCLookup.cmake")
  16. set(QTPROTOBUF_COMMON_NAMESPACE QtProtobufProject)
  17. set(GENERATOR_TARGET qtprotobufgen)
  18. set(QTPROTOBUF_EXECUTABLE_INSTALL ${CMAKE_INSTALL_FULL_BINDIR}/${GENERATOR_TARGET})
  19. set(QTPROTOBUF_EXECUTABLE $<TARGET_FILE:${GENERATOR_TARGET}>)
  20. if(UNIX)
  21. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  22. # using Clang
  23. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wno-pessimizing-move -Wno-mismatched-tags -Wno-unused-private-field")
  24. elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  25. # using GCC
  26. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror")
  27. endif()
  28. elseif(WIN32)
  29. set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-Wall" "-Werror")
  30. endif()
  31. if(Qt5_POSITION_INDEPENDENT_CODE)
  32. set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
  33. endif()
  34. set(CMAKE_CXX_STANDARD 14)
  35. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  36. add_subdirectory("src/protobuf")
  37. add_subdirectory("src/grpc")
  38. add_subdirectory("src/generator")
  39. configure_package_config_file(
  40. "${CMAKE_CURRENT_SOURCE_DIR}/ProjectConfig.cmake.in" "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
  41. INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake")
  42. install(FILES "${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
  43. export(PACKAGE ${PROJECT_NAME})
  44. # add a target to generate API documentation with Doxygen
  45. find_package(Doxygen)
  46. if(DOXYGEN_FOUND)
  47. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
  48. add_custom_target(doc
  49. ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
  50. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  51. COMMENT "Generating API documentation with Doxygen" VERBATIM
  52. )
  53. endif(DOXYGEN_FOUND)
  54. if(DEFINED $ENV{MAKE_TESTS})
  55. set(MAKE_TESTS $ENV{MAKE_TESTS})
  56. elseif(NOT DEFINED MAKE_TESTS)
  57. set(MAKE_TESTS ON)
  58. endif()
  59. if(DEFINED $ENV{MAKE_EXAMPLES})
  60. set(MAKE_EXAMPLES $ENV{MAKE_EXAMPLES})
  61. elseif(NOT DEFINED MAKE_EXAMPLES)
  62. set(MAKE_EXAMPLES ON)
  63. endif()
  64. if(DEFINED $ENV{MAKE_COVERAGE})
  65. set(MAKE_COVERAGE $ENV{MAKE_COVERAGE})
  66. elseif(NOT DEFINED MAKE_COVERAGE)
  67. set(MAKE_COVERAGE OFF)
  68. endif()
  69. if(MAKE_COVERAGE)
  70. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
  71. endif()
  72. if(NOT gRPC_FOUND)
  73. message(WARNING "gRPC not found: some tests and examples cannot be built.")
  74. message(STATUS "Force disable test")
  75. set(MAKE_TESTS OFF)
  76. message(STATUS "Force disable examples")
  77. set(MAKE_EXAMPLES OFF)
  78. endif()
  79. if(MAKE_TESTS)
  80. find_package(GTest)
  81. if (UNIX AND NOT GTest_FOUND)
  82. include(cmake/DownloadProject.cmake)
  83. download_project(PROJ GTest
  84. GIT_REPOSITORY https://github.com/google/googletest.git
  85. GIT_TAG v1.8.x
  86. )
  87. # uncomment if gmock is going to be needed
  88. set(BUILD_GMOCK OFF CACHE BOOL "Disable gmock build functionality" FORCE)
  89. add_subdirectory(${GTest_SOURCE_DIR} ${GTest_BINARY_DIR})
  90. endif()
  91. if(WIN32)
  92. #Needs to set path to protobuf libraries
  93. set(PROTOBUF_INSTALATION_PATH "")
  94. set(Protobuf_PROTOC_EXECUTABLE ${PROTOBUF_INSTALATION_PATH}/bin/protoc.exe)
  95. #Set path to GTest build libraries
  96. set(GTEST_BOTH_LIBRARIES "")
  97. #Set path to GTest include directory
  98. include_directories(${GTEST_INCLUDE_PATHS} "/")
  99. link_directories(${GTEST_BOTH_LIBRARIES})
  100. endif()
  101. enable_testing()
  102. add_subdirectory("tests")
  103. endif()
  104. if(MAKE_EXAMPLES)
  105. add_subdirectory("examples")
  106. endif()