Работа с Qt

This commit is contained in:
Andrei Astafev 2023-08-01 11:21:33 +03:00
parent e2258d1312
commit 1957cbdb5c
10 changed files with 193 additions and 184 deletions

View File

@ -38,6 +38,7 @@ include(CMakeDependentOption)
# Полезные макросы
include(${MYX_CMAKE_LIB_DIR}/macro/CreateSymlink.cmake)
include(${MYX_CMAKE_LIB_DIR}/macro/FindPackages.cmake)
include(${MYX_CMAKE_LIB_DIR}/macro/FindQt.cmake)
include(${MYX_CMAKE_LIB_DIR}/macro/InstallRelative.cmake)
include(${MYX_CMAKE_LIB_DIR}/macro/CheckEnableCxxCompilerFlag.cmake)
include(${MYX_CMAKE_LIB_DIR}/macro/GTest.cmake)
@ -60,7 +61,7 @@ include(${MYX_CMAKE_LIB_DIR}/AddExecutable.cmake)
include(${MYX_CMAKE_LIB_DIR}/AddInterfaceLibrary.cmake)
include(${MYX_CMAKE_LIB_DIR}/AddObjectLibrary.cmake)
include(${MYX_CMAKE_LIB_DIR}/TargetSetup.cmake)
include(${MYX_CMAKE_LIB_DIR}/macro/QtTargetSetup.cmake)
include(${MYX_CMAKE_LIB_DIR}/TargetSetupQt.cmake)
include(${MYX_CMAKE_LIB_DIR}/uncrustify/Uncrustify.cmake)
include(${MYX_CMAKE_LIB_DIR}/doc/Doxygen.cmake)

View File

@ -1,4 +1,4 @@
set(MYX_CMAKE_PACKAGE_VERSION "2.4.7")
set(MYX_CMAKE_PACKAGE_VERSION "2.4.8")
if(MYX_CMAKE_PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()

View File

@ -0,0 +1,145 @@
include_guard(GLOBAL)
function(myx_target_setup_qt TARGET_NAME)
set(options)
set(multiValueArgs COMPONENTS PRIVATE PUBLIC_MOC PRIVATE_MOC UI QRC LANGS)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
set(myx_qt_ver "5")
if(QT_DEFAULT_MAJOR_VERSION)
set(myx_qt_ver "${QT_DEFAULT_MAJOR_VERSION}")
endif()
if(NOT (myx_qt_ver STREQUAL "5" OR myx_qt_ver STREQUAL "6"))
myx_message_fatal_error("Supported Qt versions are 5 and 6")
endif()
get_target_property(__target_type ${TARGET_NAME} TYPE)
foreach(iter ${ARG_COMPONENTS})
if(NOT iter STREQUAL "LinguistTools")
if(__target_type STREQUAL "EXECUTABLE")
target_link_libraries(${TARGET_NAME} PRIVATE "Qt${myx_qt_ver}::${iter}")
endif()
if(__target_type STREQUAL "SHARED_LIBRARY")
target_link_libraries(${TARGET_NAME} PUBLIC "Qt${myx_qt_ver}::${iter}")
endif()
if((${CMAKE_VERSION} VERSION_GREATER "3.8.0") AND (__target_type STREQUAL "OBJECT_LIBRARY"))
target_link_libraries(${TARGET_NAME} PUBLIC "Qt${myx_qt_ver}:${iter}")
endif()
endif()
endforeach()
foreach(iter ${ARG_PRIVATE})
if(__target_type STREQUAL "INTERFACE_LIBRARY")
target_include_directories(${TARGET_NAME} INTERFACE ${Qt${myx_qt_ver}${iter}_PRIVATE_INCLUDE_DIRS})
else()
target_include_directories(${TARGET_NAME} PRIVATE ${Qt${myx_qt_ver}${iter}_PRIVATE_INCLUDE_DIRS})
endif()
endforeach()
if(__target_type STREQUAL "EXECUTABLE")
target_compile_options(${TARGET_NAME} PRIVATE ${Qt${myx_qt_ver}Core_EXECUTABLE_COMPILE_FLAGS})
endif()
if(ARG_PUBLIC_MOC)
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY PUBLIC_HEADER_FILES "${ARG_PUBLIC_MOC}")
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY TR_FILES ${ARG_PUBLIC_MOC})
endif()
if(ARG_PRIVATE_MOC)
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY PRIVATE_HEADER_FILES "${ARG_PRIVATE_MOC}")
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY TR_FILES ${ARG_PRIVATE_MOC})
endif()
if(ARG_PUBLIC_MOC OR ARG_PRIVATE_MOC)
if(myx_qt_ver EQUAL 5)
qt5_wrap_cpp(moc_cpp ${ARG_PUBLIC_MOC} ${ARG_PRIVATE_MOC})
elseif(myx_qt_ver EQUAL 6)
qt6_wrap_cpp(moc_cpp ${ARG_PUBLIC_MOC} ${ARG_PRIVATE_MOC})
endif()
endif()
if(ARG_QRC)
if(myx_qt_ver EQUAL 5)
qt5_add_resources(qrc_cpp ${ARG_QRC})
elseif(myx_qt_ver EQUAL 6)
qt6_add_resources(qrc_cpp ${ARG_QRC})
endif()
endif()
if(ARG_UI)
if(NOT COMMAND qt${myx_ver_qt}qt_wrap_ui)
message(WARNING "MyxCMake: Widgets is required to process UI")
else()
if(myx_qt_ver EQUAL 5)
qt5_wrap_ui(ui_h ${ARG_UI})
elseif(myx_qt_ver EQUAL 6)
qt6_wrap_ui(ui_h ${ARG_UI})
endif()
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY TR_FILES ${ARG_UI})
target_link_libraries(${TARGET_NAME} PRIVATE Qt${myx_qt_ver}::Widgets)
endif()
endif()
# Перечень файлов, подлежащих переводу
if(__target_type STREQUAL "INTERFACE_LIBRARY")
get_target_property(tr ${TARGET_NAME} INTERFACE_TR_FILES)
else()
get_target_property(tr ${TARGET_NAME} TR_FILES)
endif()
# Формирование файла ресурсов с переводами
if(tr AND ARG_LANGS)
if(NOT COMMAND qt${myx_ver_qt}_create_translation)
message(WARNING "MyxCMake: LinguistTools is required to process LANGS")
else()
# Заглавие файла ресурсов
file(WRITE ${PROJECT_BINARY_DIR}/${TARGET_NAME}_l10n.qrc "<RCC><qresource prefix=\"/l10n\">\n")
# Для каждого языка, указанное в параметре LANGS
foreach(iter ${ARG_LANGS})
# Создание или обновление файла переводов в каталоге ${PROJECT_SOURCE_DIR}/l10n
# и его компиляция в каталог ${PROJECT_BINARY_DIR}
if(myx_qt_ver EQUAL 5)
qt5_create_translation(qm ${tr}
"${PROJECT_SOURCE_DIR}/l10n/${TARGET_NAME}_${iter}.ts"
OPTIONS -I ${PROJECT_SOURCE_DIR}/include -I ${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME})
elseif(myx_qt_ver EQUAL 6)
qt6_create_translation(qm ${tr}
"${PROJECT_SOURCE_DIR}/l10n/${TARGET_NAME}_${iter}.ts"
OPTIONS -I ${PROJECT_SOURCE_DIR}/include -I ${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME})
endif()
# Добавление записи для скомпилированного файла переводов в ресурсный файл
file(APPEND ${PROJECT_BINARY_DIR}/${TARGET_NAME}_l10n.qrc
"<file alias=\"${TARGET_NAME}_${iter}\">${TARGET_NAME}_${iter}.qm</file>\n")
# Добавление скомпилированного файла переводов к списку зависимостей для цели
target_sources(${TARGET_NAME} PRIVATE ${qm})
endforeach()
# Окончание файла ресурсов
file(APPEND ${PROJECT_BINARY_DIR}/${TARGET_NAME}_l10n.qrc "</qresource></RCC>\n")
# Компиляция файла ресурсов с переводами
if(myx_qt_ver EQUAL 5)
qt5_add_resources(qrc_l10n ${PROJECT_BINARY_DIR}/${TARGET_NAME}_l10n.qrc)
elseif(myx_qt_ver EQUAL 6)
qt6_add_resources(qrc_l10n ${PROJECT_BINARY_DIR}/${TARGET_NAME}_l10n.qrc)
endif()
target_sources(${TARGET_NAME} PRIVATE ${qrc_l10n})
endif()
endif()
unset(tr)
if(__target_type STREQUAL "INTERFACE_LIBRARY")
target_sources(${TARGET_NAME} INTERFACE ${ARG_PUBLIC_MOC} ${ARG_PRIVATE_MOC} ${moc_cpp} ${ui_h} ${qrc_cpp})
else()
target_sources(${TARGET_NAME} PRIVATE ${ARG_PUBLIC_MOC} ${ARG_PRIVATE_MOC} ${moc_cpp} ${ui_h} ${qrc_cpp})
endif()
# Установка публичных заголовочных файлов
if(PROJECT_IS_TOP_LEVEL)
myx_install_relative(${PROJECT_SOURCE_DIR}
FILES ${ARG_PUBLIC_MOC}
DESTINATION ${CMAKE_INSTALL_PREFIX}
COMPONENT DEV
)
endif()
endfunction()

View File

@ -0,0 +1,39 @@
include_guard(GLOBAL)
macro(myx_find_qt)
set(options)
set(oneValueArgs VERSION)
set(multiValueArgs COMPONENTS PRIVATE)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(ARG_VERSION)
set(myx_qt_ver "${ARG_VERSION}")
else()
set(myx_qt_ver "5")
if(QT_DEFAULT_MAJOR_VERSION)
set(myx_qt_ver "${QT_DEFAULT_MAJOR_VERSION}")
endif()
endif()
if(NOT (myx_qt_ver STREQUAL "5" OR myx_qt_ver STREQUAL "6"))
myx_message_fatal_error("Supported Qt versions are 5 and 6")
endif()
foreach(iter ${ARG_COMPONENTS})
find_package(Qt${myx_qt_ver} COMPONENTS ${iter} REQUIRED)
endforeach()
foreach(iter ${ARG_PRIVATE})
find_package("Qt${myx_qt_ver}${iter}" COMPONENTS Private REQUIRED)
endforeach()
unset(myx_qt_ver)
foreach(__iter IN LISTS oneValueArgs multiValueArgs)
unset(ARG_${__iter})
endforeach()
unset(ARG_UNPARSED_ARGUMENTS)
unset(multiValueArgs)
unset(oneValueArgs)
unset(options)
endmacro()

View File

@ -1,176 +0,0 @@
include_guard(GLOBAL)
macro(myx_qt_target_setup TARGET_NAME)
set(options)
set(oneValueArgs VERSION)
set(multiValueArgs COMPONENTS PRIVATE PUBLIC_MOC PRIVATE_MOC UI QRC LANGS)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT ARG_VERSION)
set(ARG_VERSION "5")
endif()
if(NOT (ARG_VERSION STREQUAL "5" OR ARG_VERSION STREQUAL "6"))
myx_message_fatal_error("Supported Qt versions are 5 and 6")
endif()
foreach(iter ${ARG_COMPONENTS})
find_package(Qt${ARG_VERSION} COMPONENTS ${iter} REQUIRED)
endforeach()
foreach(iter ${ARG_PRIVATE})
find_package("Qt${ARG_VERSION}${iter}" COMPONENTS Private REQUIRED)
endforeach()
myx_qt_target_setup_post(${ARGV})
foreach(__iter IN LISTS oneValueArgs multiValueArgs)
unset(ARG_${__iter})
endforeach()
unset(ARG_UNPARSED_ARGUMENTS)
unset(multiValueArgs)
unset(oneValueArgs)
unset(options)
endmacro()
function(myx_qt_target_setup_post TARGET_NAME)
set(options)
set(oneValueArgs VERSION)
set(multiValueArgs COMPONENTS PRIVATE PUBLIC_MOC PRIVATE_MOC UI QRC LANGS)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT ARG_VERSION)
set(ARG_VERSION "5")
endif()
if(NOT (ARG_VERSION STREQUAL "5" OR ARG_VERSION STREQUAL "6"))
myx_message_fatal_error("Supported Qt versions are 5 and 6")
endif()
get_target_property(__target_type ${TARGET_NAME} TYPE)
foreach(iter ${ARG_COMPONENTS})
if(__target_type STREQUAL "INTERFACE_LIBRARY")
target_include_directories(${TARGET_NAME} INTERFACE ${Qt${ARG_VERSION}${iter}_INCLUDE_DIRS})
else()
target_include_directories(${TARGET_NAME} PRIVATE ${Qt${ARG_VERSION}${iter}_INCLUDE_DIRS})
endif()
if(NOT iter STREQUAL "LinguistTools")
if(__target_type STREQUAL "EXECUTABLE")
target_link_libraries(${TARGET_NAME} PRIVATE "Qt${ARG_VERSION}::${iter}")
endif()
if(__target_type STREQUAL "SHARED_LIBRARY")
target_link_libraries(${TARGET_NAME} PUBLIC "Qt${ARG_VERSION}::${iter}")
endif()
if((${CMAKE_VERSION} VERSION_GREATER "3.8.0") AND (__target_type STREQUAL "OBJECT_LIBRARY"))
target_link_libraries(${TARGET_NAME} PUBLIC "Qt${ARG_VERSION}::${iter}")
endif()
endif()
endforeach()
foreach(iter ${ARG_PRIVATE})
if(__target_type STREQUAL "INTERFACE_LIBRARY")
target_include_directories(${TARGET_NAME} INTERFACE ${Qt${ARG_VERSION}${iter}_PRIVATE_INCLUDE_DIRS})
else()
target_include_directories(${TARGET_NAME} PRIVATE ${Qt${ARG_VERSION}${iter}_PRIVATE_INCLUDE_DIRS})
endif()
endforeach()
if(__target_type STREQUAL "EXECUTABLE")
target_compile_options(${TARGET_NAME} PRIVATE ${Qt${ARG_VERSION}Core_EXECUTABLE_COMPILE_FLAGS})
endif()
if(ARG_PUBLIC_MOC)
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY PUBLIC_HEADER_FILES "${ARG_PUBLIC_MOC}")
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY TR_FILES ${ARG_PUBLIC_MOC})
endif()
if(ARG_PRIVATE_MOC)
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY PRIVATE_HEADER_FILES "${ARG_PRIVATE_MOC}")
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY TR_FILES ${ARG_PRIVATE_MOC})
endif()
if(ARG_PUBLIC_MOC OR ARG_PRIVATE_MOC)
if(ARG_VERSION STREQUAL "5")
qt5_wrap_cpp(moc_cpp ${ARG_PUBLIC_MOC} ${ARG_PRIVATE_MOC})
else()
qt6_wrap_cpp(moc_cpp ${ARG_PUBLIC_MOC} ${ARG_PRIVATE_MOC})
endif()
endif()
if(ARG_QRC)
if(ARG_VERSION STREQUAL "5")
qt5_add_resources(qrc_cpp ${ARG_QRC})
else()
qt6_add_resources(qrc_cpp ${ARG_QRC})
endif()
endif()
if(ARG_UI AND "Widgets" IN_LIST ARG_COMPONENTS)
if(ARG_VERSION STREQUAL "5")
qt5_wrap_ui(ui_h ${ARG_UI})
else()
qt6_wrap_ui(ui_h ${ARG_UI})
endif()
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY TR_FILES ${ARG_UI})
# TODO
target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_BINARY_DIR})
endif()
# Перечень файлов, подлежащих переводу
if(__target_type STREQUAL "INTERFACE_LIBRARY")
get_target_property(tr ${TARGET_NAME} INTERFACE_TR_FILES)
else()
get_target_property(tr ${TARGET_NAME} TR_FILES)
endif()
# Формирование файла ресурсов с переводами
if("LinguistTools" IN_LIST ARG_COMPONENTS AND tr)
# Заглавие файла ресурсов
file(WRITE ${PROJECT_BINARY_DIR}/${TARGET_NAME}_l10n.qrc "<RCC><qresource prefix=\"/l10n\">\n")
# Для каждого языка, указанное в параметре LANGS
foreach(iter ${ARG_LANGS})
# Создание или обновление файла переводов в каталоге ${PROJECT_SOURCE_DIR}/l10n
# и его компиляция в каталог ${PROJECT_BINARY_DIR}
if(ARG_VERSION STREQUAL "5")
qt5_create_translation(qm ${tr}
"${PROJECT_SOURCE_DIR}/l10n/${TARGET_NAME}_${iter}.ts"
OPTIONS -I ${PROJECT_SOURCE_DIR}/include -I ${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME})
else()
qt6_create_translation(qm ${tr}
"${PROJECT_SOURCE_DIR}/l10n/${TARGET_NAME}_${iter}.ts"
OPTIONS -I ${PROJECT_SOURCE_DIR}/include -I ${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME})
endif()
# Добавление записи для скомпилированного файла переводов в ресурсный файл
file(APPEND ${PROJECT_BINARY_DIR}/${TARGET_NAME}_l10n.qrc
"<file alias=\"${TARGET_NAME}_${iter}\">${TARGET_NAME}_${iter}.qm</file>\n")
# Добавление скомпилированного файла переводов к списку зависимостей для цели
target_sources(${TARGET_NAME} PRIVATE ${qm})
endforeach()
# Окончание файла ресурсов
file(APPEND ${PROJECT_BINARY_DIR}/${TARGET_NAME}_l10n.qrc "</qresource></RCC>\n")
# Компиляция файла ресурсов с переводами
if(ARG_VERSION STREQUAL "5")
qt5_add_resources(qrc_l10n ${PROJECT_BINARY_DIR}/${TARGET_NAME}_l10n.qrc)
else()
qt6_add_resources(qrc_l10n ${PROJECT_BINARY_DIR}/${TARGET_NAME}_l10n.qrc)
endif()
target_sources(${TARGET_NAME} PRIVATE ${qrc_l10n})
endif()
unset(tr)
if(__target_type STREQUAL "INTERFACE_LIBRARY")
target_sources(${TARGET_NAME} INTERFACE ${ARG_PUBLIC_MOC} ${ARG_PRIVATE_MOC} ${moc_cpp} ${ui_h} ${qrc_cpp})
else()
target_sources(${TARGET_NAME} PRIVATE ${ARG_PUBLIC_MOC} ${ARG_PRIVATE_MOC} ${moc_cpp} ${ui_h} ${qrc_cpp})
endif()
# Установка публичных заголовочных файлов
if(PROJECT_IS_TOP_LEVEL)
myx_install_relative(${PROJECT_SOURCE_DIR}
FILES ${ARG_PUBLIC_MOC}
DESTINATION ${CMAKE_INSTALL_PREFIX}
COMPONENT DEV
)
endif()
endfunction()

View File

@ -3,7 +3,7 @@
## Установка
В корневом каталоге проекта создать каталог `cmake` и распаковать в него
[архив](../../../releases/download/2.4.7/myx-cmake-local-2.4.7.tar.xz ).
[архив](../../../releases/download/2.4.8/myx-cmake-local-2.4.8.tar.xz ).
## Использование

View File

@ -1 +1 @@
2.4.7
2.4.8

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.6 FATAL_ERROR)
project(myx-cmake VERSION 2.4.7 LANGUAGES)
project(myx-cmake VERSION 2.4.8 LANGUAGES)
include(GNUInstallDirs)
file(WRITE ${CMAKE_SOURCE_DIR}/MyxCMake/MyxCMakeConfigVersion.cmake

2
debian/changelog vendored
View File

@ -1,4 +1,4 @@
myx-cmake (2.4.7) unstable; urgency=medium
myx-cmake (2.4.8) unstable; urgency=medium
* New version.

View File

@ -20,11 +20,11 @@ if(ENV{MYX_CMAKE_DIR})
set(MYX_CMAKE_DIR $ENV{MYX_CMAKE_DIR})
endif()
if(MYX_CMAKE_DIR)
find_package(MyxCMake 2.4.7 REQUIRED CONFIG PATHS ${MYX_CMAKE_DIR} NO_DEFAULT_PATH)
find_package(MyxCMake 2.4.8 REQUIRED CONFIG PATHS ${MYX_CMAKE_DIR} NO_DEFAULT_PATH)
myx_message_notice("== MyxCMake directory: ${MyxCMake_CONFIG} ==")
else()
if(MYX_CMAKE_USE_SYSTEM)
find_package(MyxCMake 2.4.7 REQUIRED)
find_package(MyxCMake 2.4.8 REQUIRED)
myx_message_notice("== MyxCMake directory: ${MyxCMake_CONFIG} ==")
else()
include(${PROJECT_SOURCE_DIR}/cmake/myx/MyxCMakeConfig.cmake)