CMakeLists.txt 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. set(QTPROTOBUF_CMAKE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
  6. set(QTPROTOBUF_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
  7. set(QTPROTOBUF_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
  8. find_package(Qt5 COMPONENTS Core Network Qml REQUIRED)
  9. if(Qt5Core_VERSION VERSION_LESS "5.12.3")
  10. # grpc target requires QT version not less than 5.12.3
  11. # earlier versions Http2DirectAttribute is broken: https://doc.qt.io/qt-5/whatsnew511.html
  12. # https://bugreports.qt.io/browse/QTBUG-74765
  13. message(FATAL_ERROR "Required Qt version is 5.12.3+")
  14. endif()
  15. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/grpc/CMakeLists.txt")
  16. message(STATUS "-- Found local gRPC sources directory. Perform all-in-one build")
  17. set(gRPC_BUILD_TESTS FALSE)
  18. set(protobuf_BUILD_TESTS FALSE)
  19. add_subdirectory("3rdparty/grpc")
  20. if(TARGET grpc AND TARGET grpc++ AND TARGET grpc_cpp_plugin)
  21. add_library(gRPC::grpc ALIAS grpc)
  22. add_library(gRPC::grpc++ ALIAS grpc++)
  23. add_executable(gRPC::grpc_cpp_plugin ALIAS grpc_cpp_plugin)
  24. set(gRPC_FOUND TRUE)
  25. endif()
  26. else()
  27. include("${QTPROTOBUF_CMAKE_DIR}/ProtobufLookup.cmake")
  28. include("${QTPROTOBUF_CMAKE_DIR}/gRPCLookup.cmake")
  29. endif()
  30. set(gtest_force_shared_crt OFF)
  31. set(QTPROTOBUF_COMMON_NAMESPACE QtProtobufProject)
  32. set(GENERATOR_TARGET qtprotobufgen)
  33. set(QTPROTOBUF_EXECUTABLE_INSTALL ${CMAKE_INSTALL_FULL_BINDIR}/${GENERATOR_TARGET})
  34. set(QTPROTOBUF_EXECUTABLE $<TARGET_FILE:${GENERATOR_TARGET}> CACHE STRING "QTPROTOBUF_EXECUTABLE is set ${QTPROTOBUF_EXECUTABLE}" FORCE)
  35. if(UNIX)
  36. if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
  37. # using Clang
  38. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wno-pessimizing-move -Wno-mismatched-tags -Wno-unused-private-field -Wno-self-assign-overloaded")
  39. elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  40. # using GCC
  41. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wno-error=deprecated-declarations")
  42. endif()
  43. elseif(WIN32)
  44. if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
  45. #TODO: not all l4 warnings are fixed for MSVC. Enable options bellow
  46. #once fixed
  47. # set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX /W4 /MD")
  48. # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX /W4 /MD")
  49. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /bigobj")
  50. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
  51. else()
  52. message(WARNING "Only MSVC compilers family are supported for build")
  53. endif()
  54. endif()
  55. if(Qt5_POSITION_INDEPENDENT_CODE)
  56. set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
  57. endif()
  58. set(CMAKE_CXX_STANDARD 14)
  59. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  60. add_subdirectory("src/protobuf")
  61. add_subdirectory("src/grpc")
  62. add_subdirectory("src/generator")
  63. if(NOT WIN32)#TODO: There are linking issues with windows build of well-known types...
  64. add_subdirectory("src/wellknowntypes")
  65. endif()
  66. configure_package_config_file(
  67. "${CMAKE_CURRENT_SOURCE_DIR}/ProjectConfig.cmake.in" "${QTPROTOBUF_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
  68. INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake")
  69. install(FILES "${QTPROTOBUF_BINARY_DIR}/${PROJECT_NAME}Config.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
  70. export(PACKAGE ${PROJECT_NAME})
  71. # add a target to generate API documentation with Doxygen
  72. find_package(Doxygen)
  73. if(NOT DOXYGEN_FOUND)
  74. find_program(DOXYGEN_EXECUTABLE doxygen)
  75. endif()
  76. if(DEFINED DOXYGEN_EXECUTABLE)
  77. message(STATUS "-- Using doxygen to generate documenation: ${DOXYGEN_EXECUTABLE}")
  78. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doxygen/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
  79. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doxygen/customdoxygen.css ${CMAKE_CURRENT_BINARY_DIR}/customdoxygen.css COPYONLY)
  80. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doxygen/header.html ${CMAKE_CURRENT_BINARY_DIR}/header.html COPYONLY)
  81. add_custom_target(doc
  82. ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
  83. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  84. COMMENT "Generating API documentation with Doxygen" VERBATIM
  85. )
  86. endif()
  87. if(DEFINED $ENV{QTPROTOBUF_MAKE_TESTS})
  88. set(QTPROTOBUF_MAKE_TESTS $ENV{QTPROTOBUF_MAKE_TESTS})
  89. elseif(NOT DEFINED QTPROTOBUF_MAKE_TESTS)
  90. set(QTPROTOBUF_MAKE_TESTS ON)
  91. endif()
  92. if(DEFINED $ENV{QTPROTOBUF_MAKE_EXAMPLES})
  93. set(QTPROTOBUF_MAKE_EXAMPLES $ENV{QTPROTOBUF_MAKE_EXAMPLES})
  94. elseif(NOT DEFINED QTPROTOBUF_MAKE_EXAMPLES)
  95. set(QTPROTOBUF_MAKE_EXAMPLES ON)
  96. endif()
  97. if(NOT gRPC_FOUND)
  98. message(WARNING "gRPC not found: some tests and examples cannot be built.")
  99. message(STATUS "-- Force disable test")
  100. set(QTPROTOBUF_MAKE_TESTS OFF)
  101. message(STATUS "-- Force disable examples")
  102. set(QTPROTOBUF_MAKE_EXAMPLES OFF)
  103. endif()
  104. include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/QtProtobufCommon.cmake")
  105. if(QTPROTOBUF_MAKE_TESTS)
  106. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/googletest/CMakeLists.txt")
  107. set(BUILD_GMOCK OFF CACHE BOOL "Disable gmock build functionality" FORCE)
  108. set(gtest_force_shared_crt ON CACHE BOOL "Enable shared crt" FORCE)
  109. add_subdirectory("3rdparty/googletest")
  110. set(GTest_FOUND TRUE)
  111. else()
  112. find_package(GTest)
  113. endif()
  114. if(GTest_FOUND)
  115. enable_testing()
  116. add_subdirectory("tests")
  117. else()
  118. message(STATUS "-- Force disable test")
  119. endif()
  120. endif()
  121. if(QTPROTOBUF_MAKE_EXAMPLES)
  122. add_subdirectory("examples")
  123. endif()