Skip to content

Commit

Permalink
Refactor Qt configuration
Browse files Browse the repository at this point in the history
Unfortunatley the advice given by the Qt documentation to support older
Qt versions in parallel to Qt6 by using the NAMES parameter of the CMake
find_package() command, appears to make it impossible to control which
Qt version will be selected if multiple are available. In contrast to
what is stated in the documentation, this mechanism will not search for
Qt versions in the order given by the NAMES parameter because CMake
generates a list of candidate configuration file paths that doesn't
reflect this order. In addition, this mechanism appears to make it
impossible to ignore specific Qt versions by using the
CMAKE_DISABLE_FIND_PACKAGE_<PackageName> definition.

It is therefore necessary to build the NAMES list by checking for the
CMAKE_DISABLE_FIND_PACKAGE_<PackageName> definitions. This also allows
for sufficient control over the selected Qt version to comply with
Gentoo's Qt policy.
  • Loading branch information
leonlynch committed Dec 31, 2023
1 parent 4e1eb1e commit e76b23c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
30 changes: 25 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,34 @@ add_subdirectory(test)

# If Qt::Widgets and tr31 are available, build dukpt-ui
option(BUILD_DUKPT_UI "Build dukpt-ui")
find_package(QT 5.12 NAMES Qt6 Qt5 COMPONENTS Widgets)
if(QT_FOUND)
message(STATUS "Found Qt${QT_VERSION_MAJOR} Widgets: ${QT_CONFIG} (found suitable version \"${QT_VERSION}\")")
# See https://doc.qt.io/qt-6/cmake-qt5-and-qt6-compatibility.html#supporting-older-qt-5-versions
# Note that CMAKE_DISABLE_FIND_PACKAGE_<PackageName> only applies to the
# primary package name and not the alternative names and therefore it is
# necessary to build the list of alternative names to ensure that either Qt5 or
# Qt6 can be disabled, otherwise CMAKE_DISABLE_FIND_PACKAGE_<PackageName> will
# be ignored.
if(NOT CMAKE_DISABLE_FIND_PACKAGE_Qt5)
list(APPEND Qt_NAMES Qt5)
endif()
if(NOT CMAKE_DISABLE_FIND_PACKAGE_Qt6)
list(APPEND Qt_NAMES Qt6)
endif()
if(Qt_NAMES)
find_package(QT 5.12 NAMES ${Qt_NAMES} COMPONENTS Widgets)
if(QT_FOUND)
message(STATUS "Found Qt${QT_VERSION_MAJOR} Widgets: ${QT_CONFIG} (found suitable version \"${QT_VERSION}\")")
else()
if(BUILD_DUKPT_UI)
message(FATAL_ERROR "Could NOT find Qt Widgets; required to build dukpt-ui")
else()
message(STATUS "Could NOT find Qt Widgets: skipping dukpt-ui build")
endif()
endif()
else()
if(BUILD_DUKPT_UI)
message(FATAL_ERROR "Could NOT find Qt Widgets; required to build dukpt-ui")
message(FATAL_ERROR "Both Qt5 and Qt6 are disabled: required to build dukpt-ui")
else()
message(STATUS "Could NOT find Qt Widgets: skipping dukpt-ui build")
message(STATUS "Both Qt5 and Qt6 are disabled: skipping dukpt-ui build")
endif()
endif()
find_package(tr31 ${TR31_MIN_VERSION} QUIET)
Expand Down
17 changes: 16 additions & 1 deletion ui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,22 @@ configure_file(
)

# See https://doc.qt.io/qt-6/cmake-qt5-and-qt6-compatibility.html#supporting-older-qt-5-versions
find_package(QT 5.12 NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
# Note that CMAKE_DISABLE_FIND_PACKAGE_<PackageName> only applies to the
# primary package name and not the alternative names and therefore it is
# necessary to build the list of alternative names to ensure that either Qt5 or
# Qt6 can be disabled, otherwise CMAKE_DISABLE_FIND_PACKAGE_<PackageName> will
# be ignored.
if(NOT CMAKE_DISABLE_FIND_PACKAGE_Qt5)
list(APPEND Qt_NAMES Qt5)
endif()
if(NOT CMAKE_DISABLE_FIND_PACKAGE_Qt6)
list(APPEND Qt_NAMES Qt6)
endif()
if(NOT Qt_NAMES)
message(FATAL_ERROR "Either Qt5 or Qt6 are required to build dukpt-ui")
endif()

find_package(QT 5.12 NAMES ${Qt_NAMES} REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets PATHS ${QT_DIR})
message(STATUS "Found Qt${QT_VERSION_MAJOR} Widgets: ${QT_CONFIG} (found suitable version \"${QT_VERSION}\")")
if(QT_VERSION VERSION_LESS 5.15)
Expand Down

0 comments on commit e76b23c

Please sign in to comment.