| 1 |
cmake_minimum_required(VERSION 3.17.0 FATAL_ERROR) |
| 2 |
|
| 3 |
# defines helper to link against Qt dynamically or statically |
| 4 |
|
| 5 |
# prevent multiple inclusion |
| 6 |
if (DEFINED QT_LINKAGE_DETERMINED) |
| 7 |
return() |
| 8 |
endif () |
| 9 |
set(QT_LINKAGE_DETERMINED ON) |
| 10 |
|
| 11 |
# include validate_visibility from c++utilities' 3rdParty module |
| 12 |
include(3rdParty) |
| 13 |
|
| 14 |
# allow switching the Qt and KDE Frameworks version |
| 15 |
set(QT_PACKAGE_PREFIX |
| 16 |
"Qt5" |
| 17 |
CACHE STRING "specifies the prefix for Qt packages") |
| 18 |
set(KF_PACKAGE_PREFIX |
| 19 |
"KF5" |
| 20 |
CACHE STRING "specifies the prefix for KDE Frameworks packages") |
| 21 |
set(QT_MAJOR_VERSION 5) |
| 22 |
if (QT_PACKAGE_PREFIX MATCHES ".*Qt([0-9]+).*") |
| 23 |
set(QT_MAJOR_VERSION "${CMAKE_MATCH_1}") |
| 24 |
endif () |
| 25 |
set(KF_MAJOR_VERSION 5) |
| 26 |
if (KF_PACKAGE_PREFIX MATCHES ".*KF([0-9]+).*") |
| 27 |
set(KF_MAJOR_VERSION "${CMAKE_MATCH_1}") |
| 28 |
endif () |
| 29 |
|
| 30 |
# determine the minimum Qt version |
| 31 |
if (NOT META_QT_VERSION) |
| 32 |
if (META_QT5_VERSION) |
| 33 |
# use deprecated META_QT5_VERSION variable (for compatibility) |
| 34 |
set(META_QT_VERSION "${META_QT5_VERSION}") |
| 35 |
else () |
| 36 |
# require Qt 5.6 or higher by default |
| 37 |
set(META_QT_VERSION 5.6) |
| 38 |
endif () |
| 39 |
endif () |
| 40 |
|
| 41 |
# avoid "add_custom_target cannot create target "apk" because another target…" errors produced by Qt's Android support module |
| 42 |
# (which can not cope with Qt CMake modules already pulled in by a dependency) |
| 43 |
if (ANDROID AND NOT ${PROJECT_NAME}-MultiAbiBuild) |
| 44 |
set(${PROJECT_NAME}-MultiAbiBuild ON) |
| 45 |
endif () |
| 46 |
|
| 47 |
# define function for using Qt and KDE Frameworks modules and static plugins |
| 48 |
macro (use_qt_module) |
| 49 |
# parse arguments |
| 50 |
set(OPTIONAL_ARGS ONLY_PLUGINS PLUGINS_OPTIONAL PLUGINS_LITERAL) |
| 51 |
set(ONE_VALUE_ARGS PREFIX MODULE VISIBILITY LIBRARIES_VARIABLE) |
| 52 |
set(MULTI_VALUE_ARGS TARGETS PLUGINS) |
| 53 |
cmake_parse_arguments(ARGS "${OPTIONAL_ARGS}" "${ONE_VALUE_ARGS}" "${MULTI_VALUE_ARGS}" ${ARGN}) |
| 54 |
|
| 55 |
# validate values |
| 56 |
if (NOT ARGS_PREFIX) |
| 57 |
message(FATAL_ERROR "use_qt_module called without PREFIX.") |
| 58 |
endif () |
| 59 |
if (NOT ARGS_MODULE) |
| 60 |
message(FATAL_ERROR "use_qt_module called without MODULE.") |
| 61 |
endif () |
| 62 |
if (ARGS_VISIBILITY) |
| 63 |
validate_visibility(${ARGS_VISIBILITY}) |
| 64 |
else () |
| 65 |
set(ARGS_VISIBILITY PRIVATE) |
| 66 |
endif () |
| 67 |
if (NOT ARGS_LIBRARIES_VARIABLE) |
| 68 |
set(ARGS_LIBRARIES_VARIABLE "${ARGS_VISIBILITY}_LIBRARIES") |
| 69 |
endif () |
| 70 |
if (NOT ARGS_TARGETS) |
| 71 |
if (${MODULE}_MODULE_TARGETS) |
| 72 |
set(ARGS_TARGETS "${${MODULE}_MODULE_TARGETS}") |
| 73 |
else () |
| 74 |
set(ARGS_TARGETS "${ARGS_PREFIX}::${ARGS_MODULE}") |
| 75 |
endif () |
| 76 |
endif () |
| 77 |
if (ARGS_ONLY_PLUGINS AND NOT ARGS_PLUGINS) |
| 78 |
message(FATAL_ERROR "ONLY_PLUGINS specified but no plugins.") |
| 79 |
endif () |
| 80 |
|
| 81 |
# find and use module |
| 82 |
if (NOT ARGS_ONLY_PLUGINS) |
| 83 |
find_package("${ARGS_PREFIX}${ARGS_MODULE}" "${META_QT_VERSION}" REQUIRED) |
| 84 |
foreach (TARGET ${ARGS_TARGETS}) |
| 85 |
if (NOT TARGET "${TARGET}") |
| 86 |
message(FATAL_ERROR "The CMake module ${ARGS_PREFIX}${ARGS_MODULE} does not provide the target ${TARGET}.") |
| 87 |
endif () |
| 88 |
if ("${TARGET}" IN_LIST "${ARGS_LIBRARIES_VARIABLE}") |
| 89 |
continue() |
| 90 |
endif () |
| 91 |
set("${ARGS_LIBRARIES_VARIABLE}" "${${ARGS_LIBRARIES_VARIABLE}};${TARGET}") |
| 92 |
set("PKG_CONFIG_${ARGS_PREFIX}_${ARGS_MODULE}" "${ARGS_PREFIX}${ARGS_MODULE}") |
| 93 |
message(STATUS "Linking ${META_TARGET_NAME} against Qt module ${TARGET}.") |
| 94 |
|
| 95 |
# hack for "StaticQt5": re-assign INTERFACE_LINK_LIBRARIES_RELEASE to INTERFACE_LINK_LIBRARIES |
| 96 |
get_target_property("${ARGS_MODULE}_INTERFACE_LINK_LIBRARIES_RELEASE" "${TARGET}" |
| 97 |
INTERFACE_LINK_LIBRARIES_RELEASE) |
| 98 |
if ("${ARGS_MODULE}_INTERFACE_LINK_LIBRARIES_RELEASE") |
| 99 |
set_target_properties("${TARGET}" PROPERTIES INTERFACE_LINK_LIBRARIES |
| 100 |
"${${ARGS_MODULE}_INTERFACE_LINK_LIBRARIES_RELEASE}") |
| 101 |
endif () |
| 102 |
endforeach () |
| 103 |
endif () |
| 104 |
|
| 105 |
# find and use plugins |
| 106 |
foreach (PLUGIN ${ARGS_PLUGINS}) |
| 107 |
if (ARGS_PLUGINS_LITERAL) |
| 108 |
set(PLUGIN_TARGET "${ARGS_PREFIX}::${PLUGIN}") |
| 109 |
else () |
| 110 |
set(PLUGIN_TARGET "${ARGS_PREFIX}::Q${PLUGIN}Plugin") |
| 111 |
endif () |
| 112 |
if (NOT TARGET "${PLUGIN_TARGET}") |
| 113 |
find_package("${ARGS_PREFIX}${ARGS_MODULE}" "${META_QT_VERSION}" REQUIRED) |
| 114 |
endif () |
| 115 |
if (NOT TARGET "${PLUGIN_TARGET}" |
| 116 |
AND PLUGIN MATCHES "Svg.*" |
| 117 |
AND ARGS_MODULE STREQUAL "Svg") |
| 118 |
# look for Svg plugins within the Gui module's directory as well |
| 119 |
find_package("${ARGS_PREFIX}Gui" "${META_QT_VERSION}" REQUIRED) |
| 120 |
set(PLUGIN_CONFIG "${${ARGS_PREFIX}Gui_DIR}/${ARGS_PREFIX}Q${PLUGIN}PluginConfig.cmake") |
| 121 |
if (EXISTS "${PLUGIN_CONFIG}") |
| 122 |
include("${PLUGIN_CONFIG}") |
| 123 |
endif () |
| 124 |
endif () |
| 125 |
if (NOT TARGET "${PLUGIN_TARGET}") |
| 126 |
if (ARGS_PLUGINS_OPTIONAL) |
| 127 |
continue() |
| 128 |
endif () |
| 129 |
message(FATAL_ERROR "The ${ARGS_PREFIX}${ARGS_MODULE} package does not provide the target ${PLUGIN_TARGET}.") |
| 130 |
endif () |
| 131 |
if ("${PLUGIN_TARGET}" IN_LIST "${ARGS_LIBRARIES_VARIABLE}") |
| 132 |
continue() |
| 133 |
endif () |
| 134 |
set("${ARGS_LIBRARIES_VARIABLE}" "${${ARGS_LIBRARIES_VARIABLE}};${PLUGIN_TARGET}") |
| 135 |
message(STATUS "Linking ${META_TARGET_NAME} against Qt plugin ${PLUGIN_TARGET}.") |
| 136 |
endforeach () |
| 137 |
|
| 138 |
# unset variables (can not simply use a function because Qt's variables need to be exported) |
| 139 |
foreach (ARGUMENT ${OPTIONAL_ARGS} ${ONE_VALUE_ARGS} ${MULTI_VALUE_ARGS}) |
| 140 |
unset(ARGS_${ARGUMENT}) |
| 141 |
endforeach () |
| 142 |
endmacro () |
| 143 |
|
| 144 |
# define function to make Qt variable available; queries qmake or uses variables provided by Qt 6 |
| 145 |
function (query_qmake_variable QMAKE_VARIABLE) |
| 146 |
# prevent queries for variables already known |
| 147 |
if (NOT "${${QMAKE_VARIABLE}}" STREQUAL "") |
| 148 |
return() |
| 149 |
endif () |
| 150 |
|
| 151 |
# check configuration variables provided by Qt 6 |
| 152 |
if (QMAKE_VARIABLE MATCHES "QT_(.*)") |
| 153 |
set(VARIABLE_NAME "${CMAKE_MATCH_1}") |
| 154 |
if (QT6_${VARIABLE_NAME}) |
| 155 |
set(VARIABLE_NAME QT6_${VARIABLE_NAME}) |
| 156 |
endif () |
| 157 |
if (NOT "${${VARIABLE_NAME}}" STREQUAL "") |
| 158 |
set("${QMAKE_VARIABLE}" |
| 159 |
"${${VARIABLE_NAME}}" |
| 160 |
PARENT_SCOPE) |
| 161 |
message(STATUS "Qt variable ${QMAKE_VARIABLE} taken from ${VARIABLE_NAME}: ${${VARIABLE_NAME}}") |
| 162 |
return() |
| 163 |
endif () |
| 164 |
endif () |
| 165 |
|
| 166 |
# execute qmake |
| 167 |
get_target_property(QMAKE_BIN "${QT_QMAKE_TARGET}" IMPORTED_LOCATION) |
| 168 |
execute_process( |
| 169 |
COMMAND "${QMAKE_BIN}" -query "${QMAKE_VARIABLE}" |
| 170 |
RESULT_VARIABLE "${QMAKE_VARIABLE}_RESULT" |
| 171 |
OUTPUT_VARIABLE "${QMAKE_VARIABLE}") |
| 172 |
if (NOT "${${QMAKE_VARIABLE}_RESULT}" STREQUAL 0 OR "${${QMAKE_VARIABLE}}" STREQUAL "") |
| 173 |
message( |
| 174 |
FATAL_ERROR |
| 175 |
"Unable to read qmake variable ${QMAKE_VARIABLE} via \"${QMAKE_BIN} -query ${QMAKE_VARIABLE}\"; output was \"${${QMAKE_VARIABLE}}\"." |
| 176 |
) |
| 177 |
endif () |
| 178 |
|
| 179 |
# remove new-line character at the end |
| 180 |
string(REGEX REPLACE "\n$" "" "${QMAKE_VARIABLE}" "${${QMAKE_VARIABLE}}") |
| 181 |
|
| 182 |
# export variable to parent scope |
| 183 |
set("${QMAKE_VARIABLE}" |
| 184 |
"${${QMAKE_VARIABLE}}" |
| 185 |
PARENT_SCOPE) |
| 186 |
message(STATUS "Qt variable ${QMAKE_VARIABLE} queried from qmake: ${${QMAKE_VARIABLE}}") |
| 187 |
endfunction () |
| 188 |
|
| 189 |
# define function to make Qt variable available, resolving relative paths via CMAKE_FIND_ROOT_PATH |
| 190 |
function (query_qmake_variable_path QMAKE_VARIABLE) |
| 191 |
# query the variable itself |
| 192 |
query_qmake_variable("${QMAKE_VARIABLE}") |
| 193 |
set(VARIABLE_VALUE "${${QMAKE_VARIABLE}}") |
| 194 |
if (NOT VARIABLE_VALUE) |
| 195 |
message(WARNING "Unable to resolve Qt variable ${QMAKE_VARIABLE}: it is empty") |
| 196 |
return() |
| 197 |
endif () |
| 198 |
|
| 199 |
# pass the variable as-is if it points to an existing dir/file |
| 200 |
if (EXISTS "${VARIABLE_VALUE}") |
| 201 |
get_filename_component(VARIABLE_VALUE "${VARIABLE_VALUE}" REALPATH) |
| 202 |
message(STATUS "Qt variable ${QMAKE_VARIABLE} resolved to path: ${VARIABLE_VALUE}") |
| 203 |
set("${QMAKE_VARIABLE}" |
| 204 |
"${VARIABLE_VALUE}" |
| 205 |
PARENT_SCOPE) |
| 206 |
return() |
| 207 |
endif () |
| 208 |
|
| 209 |
# assume VARIABLE_VALUE is relative within CMAKE_FIND_ROOT_PATH, e.g. QT_INSTALL_TRANSLATIONS might be set to |
| 210 |
# "share/qt6/translations" |
| 211 |
if ("${CMAKE_VERSION}" VERSION_LESS "3.12.0") |
| 212 |
message(WARNING "Unable to resolve Qt variable ${QMAKE_VARIABLE} to an existing path, try using CMake 3.12 or newer") |
| 213 |
return() # skip if CMake version too old like on Leap 15.1 |
| 214 |
endif () |
| 215 |
foreach (ROOT_PATH ${CMAKE_FIND_ROOT_PATH} "") |
| 216 |
foreach (PREFIX_PATH ${CMAKE_INSTALL_PREFIX} ${CMAKE_PREFIX_PATH} ${CMAKE_SYSTEM_PREFIX_PATH} "") |
| 217 |
string(JOIN "/" FULL_PATH ${ROOT_PATH} ${PREFIX_PATH} ${VARIABLE_VALUE}) |
| 218 |
if (EXISTS "${FULL_PATH}") |
| 219 |
set("${QMAKE_VARIABLE}" |
| 220 |
"${FULL_PATH}" |
| 221 |
PARENT_SCOPE) |
| 222 |
message(STATUS "Qt variable ${QMAKE_VARIABLE} resolved to path: ${FULL_PATH}") |
| 223 |
return() |
| 224 |
else () |
| 225 |
list(APPEND CHECKED_PATHS "${FULL_PATH}") |
| 226 |
endif () |
| 227 |
endforeach () |
| 228 |
endforeach () |
| 229 |
message( |
| 230 |
WARNING "Unable to resolve Qt variable ${QMAKE_VARIABLE} to an existing path, was checking for: ${CHECKED_PATHS}") |
| 231 |
endfunction () |