Skip to content

Commit

Permalink
Allow either Qt5 or Qt6
Browse files Browse the repository at this point in the history
These changes allow the code to be compiled with either Qt6
(preferentially) or Qt5 if the Qt6 is not found.  This is paired with a
change to libasteroid to do the same.
See AsteroidOS/libasteroid#18

Signed-off-by: Ed Beroset <[email protected]>
  • Loading branch information
beroset authored and FlorentRevest committed Oct 18, 2022
1 parent 6a97751 commit 9e1d347
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
11 changes: 6 additions & 5 deletions asteroidsyncservice/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
cmake_minimum_required(VERSION 3.15)
find_package(QT NAMES Qt5 COMPONENTS DBus Qml REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS DBus Qml REQUIRED)
find_package(Qt6 COMPONENTS DBus Qml)
if (NOT Qt6_FOUND)
find_package(Qt5 5.15 COMPONENTS DBus Qml REQUIRED)
endif()
add_library(asteroidsyncserviceplugin SHARED syncservice_plugin.cpp watch.cpp watches.cpp ${PLATFORM_SOURCE_DIR}/servicecontrol.cpp)
set_target_properties(asteroidsyncserviceplugin PROPERTIES AUTOMOC ON)
target_include_directories(asteroidsyncserviceplugin PRIVATE ${PROJECT_BINARY_DIR} ${PLATFORM_SOURCE_DIR})
target_compile_features(asteroidsyncserviceplugin PUBLIC cxx_std_17)
target_link_libraries(asteroidsyncserviceplugin PRIVATE Qt${QT_VERSION_MAJOR}::DBus Qt${QT_VERSION_MAJOR}::Qml)
target_link_libraries(asteroidsyncserviceplugin PRIVATE Qt::DBus Qt::Qml)

# Installation paths
get_target_property(REAL_QMAKE_EXECUTABLE ${Qt5Core_QMAKE_EXECUTABLE}
IMPORTED_LOCATION)
get_target_property(REAL_QMAKE_EXECUTABLE Qt::qmake IMPORTED_LOCATION)

if (NOT QT_INSTALL_QML)
execute_process(COMMAND "${REAL_QMAKE_EXECUTABLE}" -query QT_INSTALL_QML
Expand Down
19 changes: 14 additions & 5 deletions asteroidsyncserviced/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
cmake_minimum_required(VERSION 3.15)
find_package(Qt5 COMPONENTS Core Bluetooth DBus Qml REQUIRED)
find_package(Qt6 COMPONENTS Core Bluetooth DBus Qml)
if (NOT Qt6_FOUND)
find_package(Qt5 COMPONENTS Core Bluetooth DBus Qml REQUIRED)
endif()

add_subdirectory(libasteroid)
if(NOT DEFINED Q_DECLARE_PRIVATE_SUPPORTS_UNIQUE_PTR)
include (CheckCXXSourceCompiles)
find_package(Qt5 COMPONENTS Core REQUIRED)
set(CMAKE_REQUIRED_INCLUDES ${Qt5Core_INCLUDE_DIRS})
set(CMAKE_REQUIRED_LIBRARIES Qt5::Core)
find_package(Qt6 COMPONENTS Core)
if (Qt6_FOUND)
set(CMAKE_REQUIRED_INCLUDES ${Qt6Core_INCLUDE_DIRS})
set(CMAKE_REQUIRED_LIBRARIES Qt6::Core)
else()
find_package(Qt5 COMPONENTS Core REQUIRED)
set(CMAKE_REQUIRED_INCLUDES ${Qt5Core_INCLUDE_DIRS})
set(CMAKE_REQUIRED_LIBRARIES Qt5::Core)
endif()
set(Q_DECLARE_PRIVATE_SUPPORTS_UNIQUE_PTR 0)
check_cxx_source_compiles("
#include <QObject>
Expand Down Expand Up @@ -43,7 +52,7 @@ set_target_properties(asteroidsyncserviced PROPERTIES AUTOMOC ON)
target_compile_features(asteroidsyncserviced PUBLIC cxx_std_17)
add_compile_definitions(Q_DECLARE_PRIVATE_SUPPORTS_UNIQUE_PTR=${Q_DECLARE_PRIVATE_SUPPORTS_UNIQUE_PTR})
target_include_directories(asteroidsyncserviced PRIVATE ${PROJECT_BINARY_DIR} ${DAEMON_PLATFORM_SOURCE_DIR} libasteroid/src bluez)
target_link_libraries(asteroidsyncserviced PRIVATE Qt5::Bluetooth Qt5::Core Qt5::DBus Qt5::Qml asteroid)
target_link_libraries(asteroidsyncserviced PRIVATE Qt::Bluetooth Qt::Core Qt::DBus Qt::Qml asteroid)
install(TARGETS asteroidsyncserviced)
if (DESKTOP_PLATFORM)
configure_file(
Expand Down
8 changes: 7 additions & 1 deletion asteroidsyncserviced/dbusinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <QJsonObject>

#include <algorithm>
#include <utility>

/*!
* \brief Convert JSON weather string to QList<WeatherDay>
Expand All @@ -45,7 +46,12 @@
*/
static QList<WeatherDay> parseWeatherJson(const QString &weatherJson)
{
constexpr int maxWeatherDays{5};
/* This looks complex, but it's really just a way to compensate for the fact
* that with Qt5, size() returned an int, and with Qt6, it returns a qsizetype.
* This automatically determines the type so the comparison within std::min()
* below does not trigger a compiler warning.
*/
static constexpr decltype(std::declval<QJsonArray>().size()) maxWeatherDays{5};
QList<WeatherDay> weatherDays;
QJsonParseError parseError;
auto json = QJsonDocument::fromJson(weatherJson.toUtf8(), &parseError);
Expand Down

0 comments on commit 9e1d347

Please sign in to comment.