CMakeLists.txt 5.9 KB

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