Bläddra i källkod

Split generator and QtProtobuf library

- Split cmake rules and configs for generator and QtProtobuf library
- Manage dependencies more accurate
- Move position independent definition to QtProtobuf config
Alexey Edelev 4 år sedan
förälder
incheckning
ade25f9064

+ 3 - 0
.gitmodules

@@ -7,3 +7,6 @@
 [submodule "3rdparty/microjson"]
 	path = 3rdparty/microjson
 	url = https://github.com/semlanik/microjson.git
+[submodule "src/protobuf/microjson"]
+	path = src/protobuf/3rdparty/microjson
+	url = https://github.com/semlanik/microjson.git

+ 18 - 33
CMakeLists.txt

@@ -17,21 +17,6 @@ set(QT_PROTOBUF_CMAKE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
 set(QT_PROTOBUF_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
 set(QT_PROTOBUF_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
 
-find_package(Qt5 COMPONENTS Core Network Qml REQUIRED)
-
-if(EXISTS "${QT_PROTOBUF_SOURCE_DIR}/3rdparty/microjson/CMakeLists.txt")
-    set(MICROJSON_MAKE_TESTS OFF)
-    add_subdirectory("${QT_PROTOBUF_SOURCE_DIR}/3rdparty/microjson" EXCLUDE_FROM_ALL)
-    message(STATUS "Using microjson from 3rdparty")
-endif()
-
-if(Qt5Core_VERSION VERSION_LESS "5.12.4")
-    # grpc target requires QT version not less than 5.12.4
-    # earlier versions Http2DirectAttribute is broken: https://doc.qt.io/qt-5/whatsnew511.html
-    #                                                  https://bugreports.qt.io/browse/QTBUG-74765
-    message(FATAL_ERROR "Required Qt version is 5.12.4+")
-endif()
-
 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/grpc/CMakeLists.txt")
     message(STATUS "Found local gRPC sources directory. Perform all-in-one build")
     set(gRPC_BUILD_TESTS FALSE)
@@ -74,24 +59,19 @@ elseif(WIN32)
     endif()
 endif()
 
-if(Qt5_POSITION_INDEPENDENT_CODE)
-    set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
-endif()
-set(CMAKE_CXX_STANDARD 14)
-set(CMAKE_CXX_STANDARD_REQUIRED ON)
-
 include(CMakePackageConfigHelpers)
-configure_package_config_file(
-    "${CMAKE_CURRENT_SOURCE_DIR}/ProjectConfig.cmake.in" "${QT_PROTOBUF_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
-    INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake")
-install(FILES "${QT_PROTOBUF_BINARY_DIR}/${PROJECT_NAME}Config.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
-    COMPONENT dev)
-
-export(PACKAGE ${PROJECT_NAME})
+if(NOT CMAKE_CROSSCOMPILING)
+    configure_package_config_file(
+        "${CMAKE_CURRENT_SOURCE_DIR}/ProjectConfig.cmake.in" "${QT_PROTOBUF_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
+        INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake")
+    install(FILES "${QT_PROTOBUF_BINARY_DIR}/${PROJECT_NAME}Config.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
+        COMPONENT dev)
+    export(PACKAGE ${PROJECT_NAME})
+    add_subdirectory("src/generator")
+endif()
 
 add_subdirectory("src/protobuf")
 add_subdirectory("src/grpc")
-add_subdirectory("src/generator")
 if(NOT WIN32)#TODO: There are linking issues with windows build of well-known types...
     add_subdirectory("src/wellknowntypes")
 endif()
@@ -122,12 +102,17 @@ set(QT_PROTOBUF_MAKE_EXAMPLES ON CACHE BOOL "Enable QtProtobuf examples build")
 set(QT_PROTOBUF_MAKE_COVERAGE OFF CACHE BOOL "Enable QtProtobuf build for profiler (gcov)")
 set(BUILD_SHARED_LIBS ON CACHE BOOL "Enable QtProtobuf static library build (disables shared library build)")
 
-if(NOT gRPC_FOUND)
-    message(WARNING "gRPC not found: some tests and examples cannot be built.")
-    message(STATUS "Force disable test")
+if(CMAKE_CROSSCOMPILING)
     set(QT_PROTOBUF_MAKE_TESTS OFF)
-    message(STATUS "Force disable examples")
     set(QT_PROTOBUF_MAKE_EXAMPLES OFF)
+else()
+    if(NOT gRPC_FOUND)
+        message(WARNING "gRPC not found: some tests and examples cannot be built.")
+        message(STATUS "Force disable test")
+        set(QT_PROTOBUF_MAKE_TESTS OFF)
+        message(STATUS "Force disable examples")
+        set(QT_PROTOBUF_MAKE_EXAMPLES OFF)
+    endif()
 endif()
 
 include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/QtProtobufCommon.cmake")

+ 1 - 0
ProjectConfig.cmake.in

@@ -1,3 +1,4 @@
+set(QT_PROTOBUF_PROJECT "@QT_PROTOBUF_PROJECT@")
 foreach(component ${@PROJECT_NAME@_FIND_COMPONENTS})
   # For requested component, execute its "config" script
   #message(STATUS "Including ${CMAKE_CURRENT_LIST_DIR}/${component}Config.cmake...")

+ 1 - 1
cmake/QtProtobufGen.cmake

@@ -137,7 +137,7 @@ function(qtprotobuf_generate)
     set_target_properties(${GENERATED_TARGET_NAME} PROPERTIES PUBLIC_HEADER "${GENERATED_HEADERS_FULL}")
 
     #Add include directories in case if projects are enabled by find_project
-    target_include_directories(${GENERATED_TARGET_NAME} PUBLIC ${OUT_DIR} PRIVATE ${Qt5Core_INCLUDE_DIRS}
+    target_include_directories(${GENERATED_TARGET_NAME} PUBLIC ${OUT_DIR} PRIVATE
         $<TARGET_PROPERTY:${QT_PROTOBUF_PROJECT}::QtProtobuf,INTERFACE_INCLUDE_DIRECTORIES>)
 
     if(TARGET ${QT_PROTOBUF_PROJECT}::QtGrpc)

+ 1 - 1
cmake/QtProtobufTest.cmake

@@ -1,4 +1,4 @@
-find_package(QtProtobufProject COMPONENTS QtProtobuf QtGrpc REQUIRED)
+find_package(${QT_PROTOBUF_PROJECT} COMPONENTS QtProtobuf QtGrpc REQUIRED)
 
 include(${QT_PROTOBUF_CMAKE_DIR}/QtProtobufCommon.cmake)
 

+ 0 - 4
examples/clienttutorial/CMakeLists.txt

@@ -8,10 +8,6 @@ set(CMAKE_AUTOUIC ON)
 set(CMAKE_AUTOMOC ON)
 set(CMAKE_AUTORCC ON)
 
-set(CMAKE_CXX_STANDARD 14)
-set(CMAKE_CXX_STANDARD_REQUIRED ON)
-set(CMAKE_POSITION_INDEPENDENT_CODE ON)
-
 find_package(Qt5 COMPONENTS Core Quick REQUIRED)
 find_package(QtProtobufProject COMPONENTS QtProtobuf QtGrpc REQUIRED)
 

+ 2 - 0
examples/examples_common/CMakeLists.txt

@@ -2,6 +2,8 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
 set(CMAKE_AUTOMOC ON)
 set(CMAKE_AUTORCC ON)
 
+find_package(Qt5 COMPONENTS Core CONFIG REQUIRED)
+
 file(GLOB HEADERS
     universallistmodelbase.h
     universallistmodel.h

+ 17 - 1
src/generator/CMakeLists.txt

@@ -1,5 +1,6 @@
 set(TARGET ${GENERATOR_TARGET})
-set(TARGET_EXPORT ${TARGET}Targets)
+set(TARGET_EXPORT GeneratorTargets)
+set(TARGET_CONFIG GeneratorConfig)
 
 set(TARGET_BINDIR ${CMAKE_INSTALL_BINDIR})
 set(TARGET_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
@@ -55,4 +56,19 @@ install(TARGETS ${TARGET}
 
 install(EXPORT ${TARGET_EXPORT} NAMESPACE ${QT_PROTOBUF_PROJECT}:: FILE ${TARGET_EXPORT}.cmake DESTINATION ${TARGET_CMAKE_DIR} COMPONENT dev)
 
+include(CMakePackageConfigHelpers)
+configure_package_config_file(
+    "${TARGET_CONFIG}.cmake.in" "${QT_PROTOBUF_BINARY_DIR}/${TARGET_CONFIG}.cmake"
+    INSTALL_DESTINATION "${TARGET_CMAKE_DIR}")
+install(FILES "${QT_PROTOBUF_BINARY_DIR}/${TARGET_CONFIG}.cmake" DESTINATION "${TARGET_CMAKE_DIR}" COMPONENT dev)
+
 export(TARGETS ${TARGET} NAMESPACE ${QT_PROTOBUF_PROJECT}:: FILE ${TARGET_EXPORT}.cmake)
+
+configure_file("${QT_PROTOBUF_CMAKE_DIR}/QtProtobufGen.cmake" "${QT_PROTOBUF_BINARY_DIR}/QtProtobufGen.cmake" COPYONLY)
+install(FILES "${QT_PROTOBUF_BINARY_DIR}/QtProtobufGen.cmake" DESTINATION "${TARGET_CMAKE_DIR}" COMPONENT dev)
+
+configure_file("${CMAKE_CURRENT_SOURCE_DIR}/parsemessages.go" "${QT_PROTOBUF_BINARY_DIR}/parsemessages.go" COPYONLY)
+install(FILES "${QT_PROTOBUF_BINARY_DIR}/parsemessages.go" DESTINATION "${TARGET_CMAKE_DIR}" COMPONENT dev)
+
+configure_file("${QT_PROTOBUF_CMAKE_DIR}/ProtobufLookup.cmake" "${QT_PROTOBUF_BINARY_DIR}/ProtobufLookup.cmake" COPYONLY)
+install(FILES "${QT_PROTOBUF_BINARY_DIR}/ProtobufLookup.cmake" DESTINATION "${TARGET_CMAKE_DIR}" COMPONENT dev)

+ 10 - 0
src/generator/GeneratorConfig.cmake.in

@@ -0,0 +1,10 @@
+include("${CMAKE_CURRENT_LIST_DIR}/ProtobufLookup.cmake")
+
+if(NOT TARGET @TARGET@ AND NOT @TARGET@_BINARY_DIR)
+    include("${CMAKE_CURRENT_LIST_DIR}/@TARGET_EXPORT@.cmake")
+endif()
+
+@PACKAGE_INIT@
+set(PROTO_PARSER "${CMAKE_CURRENT_LIST_DIR}/parsemessages.go")
+set(GENERATOR_TARGET "@GENERATOR_TARGET@")
+include("${CMAKE_CURRENT_LIST_DIR}/QtProtobufGen.cmake")

+ 0 - 0
src/protobuf/parsemessages.go → src/generator/parsemessages.go


+ 13 - 0
src/grpc/CMakeLists.txt

@@ -10,6 +10,19 @@ set(TARGET_BINDIR ${CMAKE_INSTALL_BINDIR})
 set(CMAKE_AUTOMOC ON)
 set(CMAKE_AUTORCC ON)
 
+find_package(Qt5 COMPONENTS Core Network Qml CONFIG REQUIRED)
+
+if(Qt5Core_VERSION VERSION_LESS "5.12.4")
+    # grpc target requires QT version not less than 5.12.4
+    # earlier versions Http2DirectAttribute is broken: https://doc.qt.io/qt-5/whatsnew511.html
+    #                                                  https://bugreports.qt.io/browse/QTBUG-74765
+    message(FATAL_ERROR "Required Qt version is 5.12.4+")
+endif()
+
+set(CMAKE_POSITION_INDEPENDENT_CODE ${Qt5_POSITION_INDEPENDENT_CODE})
+set(CMAKE_CXX_STANDARD 14)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
+
 include(${QT_PROTOBUF_CMAKE_DIR}/Coverage.cmake)
 include(${QT_PROTOBUF_CMAKE_DIR}/GenerateQtHeaders.cmake)
 include(${QT_PROTOBUF_CMAKE_DIR}/QtProtobufCommon.cmake)

+ 0 - 0
3rdparty/microjson → src/protobuf/3rdparty/microjson


+ 19 - 9
src/protobuf/CMakeLists.txt

@@ -11,10 +11,28 @@ set(CMAKE_AUTOMOC ON)
 set(CMAKE_AUTORCC ON)
 
 find_package(Qt5 COMPONENTS Core Qml REQUIRED)
+
+if(Qt5Core_VERSION VERSION_LESS "5.12.4")
+    # grpc target requires QT version not less than 5.12.4
+    # earlier versions Http2DirectAttribute is broken: https://doc.qt.io/qt-5/whatsnew511.html
+    #                                                  https://bugreports.qt.io/browse/QTBUG-74765
+    message(FATAL_ERROR "Required Qt version is 5.12.4+")
+endif()
+
+if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/3rdparty/microjson/CMakeLists.txt")
+    set(MICROJSON_MAKE_TESTS OFF)
+    add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/microjson" EXCLUDE_FROM_ALL)
+    message(STATUS "Using microjson from 3rdparty")
+endif()
+
 if(NOT TARGET microjson)
     find_package(microjson CONFIG REQUIRED)
 endif()
 
+set(CMAKE_POSITION_INDEPENDENT_CODE ${Qt5_POSITION_INDEPENDENT_CODE})
+set(CMAKE_CXX_STANDARD 14)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
+
 include(${QT_PROTOBUF_CMAKE_DIR}/Coverage.cmake)
 include(${QT_PROTOBUF_CMAKE_DIR}/GenerateQtHeaders.cmake)
 include(${QT_PROTOBUF_CMAKE_DIR}/QtProtobufCommon.cmake)
@@ -137,16 +155,8 @@ install(FILES "${QT_PROTOBUF_BINARY_DIR}/${TARGET_CONFIG}.cmake" DESTINATION "${
 
 export(TARGETS ${TARGET} NAMESPACE ${QT_PROTOBUF_PROJECT}:: FILE ${TARGET_EXPORT}.cmake)
 
-configure_file("${QT_PROTOBUF_CMAKE_DIR}/QtProtobufGen.cmake" "${QT_PROTOBUF_BINARY_DIR}/QtProtobufGen.cmake" COPYONLY)
-install(FILES "${QT_PROTOBUF_BINARY_DIR}/QtProtobufGen.cmake" DESTINATION "${TARGET_CMAKE_DIR}" COMPONENT dev)
-
-configure_file("${CMAKE_CURRENT_SOURCE_DIR}/parsemessages.go" "${QT_PROTOBUF_BINARY_DIR}/parsemessages.go" COPYONLY)
-install(FILES "${QT_PROTOBUF_BINARY_DIR}/parsemessages.go" DESTINATION "${TARGET_CMAKE_DIR}" COMPONENT dev)
-
-configure_file("${QT_PROTOBUF_CMAKE_DIR}/ProtobufLookup.cmake" "${QT_PROTOBUF_BINARY_DIR}/ProtobufLookup.cmake" COPYONLY)
-install(FILES "${QT_PROTOBUF_BINARY_DIR}/ProtobufLookup.cmake" DESTINATION "${TARGET_CMAKE_DIR}" COMPONENT dev)
-
 set(QT_PROTOBUF_EXECUTABLE_INSTALL ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}/${GENERATOR_TARGET})
+
 configure_file("${CMAKE_CURRENT_SOURCE_DIR}/qt_lib_protobuf.pri.in" "${QT_PROTOBUF_BINARY_DIR}/qt_lib_protobuf.pri" @ONLY)
 install(FILES "${QT_PROTOBUF_BINARY_DIR}/qt_lib_protobuf.pri" DESTINATION "${QT_HOST_DATA}/mkspecs/modules" COMPONENT dev)
 

+ 7 - 11
src/protobuf/QtProtobufConfig.cmake.in

@@ -1,15 +1,16 @@
-include("${CMAKE_CURRENT_LIST_DIR}/ProtobufLookup.cmake")
+include(CMakeFindDependencyMacro)
 
 set(QT_PROTOBUF_STATIC @QT_PROTOBUF_STATIC@)
-
 if(QT_PROTOBUF_STATIC)
-    include(CMakeFindDependencyMacro)
     find_dependency(microjson REQUIRED CONFIG)
 endif()
 
-if(NOT TARGET @GENERATOR_TARGET@ AND NOT @GENERATOR_TARGET@_BINARY_DIR)
-    include("${CMAKE_CURRENT_LIST_DIR}/@GENERATOR_TARGET@Targets.cmake")
-endif()
+find_dependency(Qt5 COMPONENTS Core Qml REQUIRED CONFIG)
+find_dependency(QtProtobufProject COMPONENTS Generator CONFIG REQUIRED)
+
+set(CMAKE_POSITION_INDEPENDENT_CODE @Qt5_POSITION_INDEPENDENT_CODE@)
+set(CMAKE_CXX_STANDARD 14)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
 
 if(NOT TARGET @TARGET@ AND NOT @TARGET@_BINARY_DIR)
     include("${CMAKE_CURRENT_LIST_DIR}/@TARGET_EXPORT@.cmake")
@@ -20,11 +21,6 @@ if(QT_PROTOBUF_STATIC AND NOT TARGET @PROTOBUF_QUICK_PLUGIN_NAME@ AND NOT @PROTO
 endif()
 
 @PACKAGE_INIT@
-set(PROTO_PARSER "${CMAKE_CURRENT_LIST_DIR}/parsemessages.go")
-set(GENERATOR_TARGET "@GENERATOR_TARGET@")
-set(QT_PROTOBUF_PROJECT "@QT_PROTOBUF_PROJECT@")
-
 if(QT_PROTOBUF_STATIC)
     add_definitions(-DQT_PROTOBUF_STATIC)#Old cmake versions compatibility
 endif()
-include("${CMAKE_CURRENT_LIST_DIR}/QtProtobufGen.cmake")

+ 4 - 2
src/qttypes/CMakeLists.txt

@@ -12,12 +12,14 @@ set(CMAKE_AUTORCC ON)
 
 find_package(Qt5 COMPONENTS Core Gui Qml REQUIRED)
 find_package(Protobuf QUIET)
+find_package(QtProtobufProject COMPONENTS Generator CONFIG REQUIRED)
 
-set(PROTO_PARSER ${QT_PROTOBUF_BINARY_DIR}/parsemessages.go)
+set(CMAKE_POSITION_INDEPENDENT_CODE ${Qt5_POSITION_INDEPENDENT_CODE})
+set(CMAKE_CXX_STANDARD 14)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
 
 include(${QT_PROTOBUF_CMAKE_DIR}/Coverage.cmake)
 include(${QT_PROTOBUF_CMAKE_DIR}/GenerateQtHeaders.cmake)
-include(${QT_PROTOBUF_CMAKE_DIR}/QtProtobufGen.cmake)
 
 file(GLOB SOURCES
     qtprotobufqttypes.cpp)

+ 1 - 0
src/qttypes/QtProtobufQtTypesConfig.cmake.in

@@ -1,5 +1,6 @@
 include(CMakeFindDependencyMacro)
 
+find_dependency(Qt5 COMPONENTS Network REQUIRED CONFIG)
 find_dependency(QtProtobufProject COMPONENTS QtProtobuf REQUIRED CONFIG)
 
 if(NOT TARGET @TARGET@ AND NOT @TARGET@_BINARY_DIR)

+ 8 - 3
src/wellknowntypes/CMakeLists.txt

@@ -12,12 +12,14 @@ set(CMAKE_AUTORCC ON)
 
 find_package(Qt5 COMPONENTS Core Qml REQUIRED)
 find_package(Protobuf QUIET)
+find_package(QtProtobufProject COMPONENTS Generator CONFIG REQUIRED)
 
-set(PROTO_PARSER ${QT_PROTOBUF_BINARY_DIR}/parsemessages.go)
+set(CMAKE_POSITION_INDEPENDENT_CODE ${Qt5_POSITION_INDEPENDENT_CODE})
+set(CMAKE_CXX_STANDARD 14)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
 
 include(${QT_PROTOBUF_CMAKE_DIR}/Coverage.cmake)
 include(${QT_PROTOBUF_CMAKE_DIR}/GenerateQtHeaders.cmake)
-include(${QT_PROTOBUF_CMAKE_DIR}/QtProtobufGen.cmake)
 
 file(GLOB SOURCES
     dummy.cpp)
@@ -27,7 +29,7 @@ function(add_wellknowntype TYPENAME)
     list(APPEND LOOKUP_DIRS ${Protobuf_INCLUDE_DIRS})
     foreach(INCLUDE_DIR ${LOOKUP_DIRS})
         file(GLOB PROTO_FILE ${INCLUDE_DIR}/google/protobuf/${TYPENAME}.proto)
-        if (NOT "${PROTO_FILE}" STREQUAL "")
+        if(NOT "${PROTO_FILE}" STREQUAL "")
             message(STATUS "Add well-known type ${PROTO_FILE}")
             qtprotobuf_generate(TARGET ${TARGET} GENERATED_TARGET ${TYPENAME}
                 OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated
@@ -41,6 +43,9 @@ function(add_wellknowntype TYPENAME)
             break()
         endif()
     endforeach()
+    if(NOT TARGET ${TYPENAME})
+        message(FATAL_ERROR "Unable to add well-know type ${TYPENAME}")
+    endif()
 endfunction()
 
 add_library(${TARGET} ${SOURCES})

+ 0 - 7
tests/test_qprotobuf_serializer_plugin/CMakeLists.txt

@@ -8,13 +8,6 @@ find_package(Qt5 COMPONENTS Test REQUIRED)
 file(GLOB SOURCES
     serializationplugintest.cpp)
 
-if(Qt5_POSITION_INDEPENDENT_CODE)
-    set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
-endif()
-
-set(CMAKE_CXX_STANDARD 14)
-set(CMAKE_CXX_STANDARD_REQUIRED ON)
-
 add_executable(${TARGET} ${SOURCES})
 target_link_libraries(${TARGET} PRIVATE gtest_main gtest ${QT_PROTOBUF_PROJECT}::QtProtobuf ${QT_PROTOBUF_PROJECT}::QtGrpc Qt5::Core Qt5::Test Qt5::Network ${CMAKE_THREAD_LIBS_INIT})
 add_target_windeployqt(TARGET ${TARGET}