Browse Source

Add QML realted conditions

- Add conditional compilation of the QML-related code
- Throw fatal error if generating of the QML code is enabled
  but QML is not found

Fixes #239
Alexey Edelev 2 years ago
parent
commit
7225e6e324

+ 5 - 1
cmake/QtProtobufGen.cmake

@@ -32,7 +32,11 @@ function(qtprotobuf_generate)
     set(GENERATION_OPTIONS ${GENERATION_TYPE})
 
     if(qtprotobuf_generate_QML)
-        message(STATUS "Enabled QML generation for ${GENERATED_TARGET_NAME}")
+        if(NOT TARGET Qt5::Qml)
+            message(FATAL_ERROR "Trying to enable QML support for ${GENERATED_TARGET_NAME}, but Qt5::Qml is not a target."
+                " find_package(Qt5 COMPONENTS Qml) is missing?")
+        endif()
+        message(STATUS "Enabling QML generation for ${GENERATED_TARGET_NAME}")
         set(GENERATION_OPTIONS "${GENERATION_OPTIONS}:QML")
     endif()
 

+ 1 - 1
src/grpc/qtgrpcglobal.h

@@ -52,7 +52,7 @@
 #endif
 
 #ifndef Q_GRPC_IMPORT_QUICK_PLUGIN
-    #ifdef QT_PROTOBUF_STATIC
+    #if defined(QT_PROTOBUF_STATIC) && defined(QT_QML_LIB) // TODO: Check how detect this in Qt6
         #include <QtPlugin>
         #include <QQmlExtensionPlugin>
         #define Q_GRPC_IMPORT_QUICK_PLUGIN() \

+ 9 - 2
src/protobuf/qprotobuflazymessagepointer.h

@@ -26,8 +26,10 @@
 #pragma once //QProtobufLazyMessagePointer
 
 #include "qtprotobufglobal.h"
-#include <QQmlEngine>
 #include <QObject>
+#if defined(QT_QML_LIB) // TODO: Check how detect this in Qt6
+#  include <QQmlEngine>
+#endif
 
 #include <memory>
 #include <type_traits>
@@ -96,7 +98,12 @@ public:
 
 private:
     void checkAndRelease() const {
-        if (m_ptr != nullptr && QQmlEngine::objectOwnership(m_ptr.get()) == QQmlEngine::JavaScriptOwnership) {
+#if defined(QT_QML_LIB)
+        bool qmlCheck = QQmlEngine::objectOwnership(m_ptr.get()) == QQmlEngine::JavaScriptOwnership;
+#else
+        bool qmlCheck = true;
+#endif
+        if (m_ptr != nullptr && qmlCheck) {
             m_ptr.release();//In case if object owned by QML it's qml responsibility to clean it up
             QObject::disconnect(m_destroyed);
         }

+ 1 - 1
src/protobuf/qtprotobufglobal.h

@@ -78,7 +78,7 @@
  * \endcode
  */
 #ifndef Q_PROTOBUF_IMPORT_QUICK_PLUGIN
-    #ifdef QT_PROTOBUF_STATIC
+    #if defined(QT_PROTOBUF_STATIC) && defined(QT_QML_LIB) // TODO: Check how detect this in Qt6
         #include <QtPlugin>
         #include <QQmlExtensionPlugin>
         #define Q_PROTOBUF_IMPORT_QUICK_PLUGIN() \

+ 10 - 1
src/qttypes/CMakeLists.txt

@@ -19,10 +19,19 @@ qt_protobuf_internal_add_library(ProtobufQtTypes
 
 
 file(GLOB PROTO_FILE ${CMAKE_CURRENT_SOURCE_DIR}/QtProtobuf/Qt*.proto)
+
+set(qml_enabled "")
+if(TARGET Qt5::Qml)
+    set(qml_enabled QML)
+endif()
+
 qtprotobuf_generate(TARGET ProtobufQtTypes
     OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated
     PROTO_FILES ${PROTO_FILE}
-    QML FOLDER ${extra_type_libraries_options})
+    FOLDER
+    ${qml_enabled}
+    ${extra_type_libraries_options}
+)
 
 target_compile_definitions(ProtobufQtTypes PRIVATE QT_BUILD_PROTOBUF_QT_TYPES_LIB)
 

+ 6 - 1
src/wellknowntypes/CMakeLists.txt

@@ -15,6 +15,11 @@ function(add_wellknowntype type_name)
     set(lookup_dirs "${QT_PROTOBUF_SOURCE_DIR}/3rdparty/grpc/third_party/protobuf/src"
         ${Protobuf_INCLUDE_DIRS}
     )
+
+    set(qml_enabled "")
+    if(TARGET Qt5::Qml)
+        set(qml_enabled QML)
+    endif()
     foreach(dir ${lookup_dirs})
         file(GLOB PROTO_FILE "${dir}/google/protobuf/${type_name}.proto")
         if(NOT "${PROTO_FILE}" STREQUAL "")
@@ -24,7 +29,7 @@ function(add_wellknowntype type_name)
                 OUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated"
                 PROTO_FILES "${PROTO_FILE}"
                 PROTO_INCLUDES "-I\"${dir}\""
-                QML
+                ${qml_enabled}
                 FOLDER
                 ${extra_type_libraries_options}
             )