myx/MyxCMake/lib/TargetSetupQt.cmake

156 lines
6.4 KiB
CMake
Raw Normal View History

2023-08-01 08:21:33 +00:00
include_guard(GLOBAL)
function(myx_target_setup_qt TARGET_NAME)
set(options)
2023-08-01 09:03:08 +00:00
set(oneValueArgs)
2023-08-01 08:21:33 +00:00
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()
2024-03-12 13:27:30 +00:00
get_target_property(target_type ${TARGET_NAME} TYPE)
2023-08-01 08:21:33 +00:00
foreach(iter ${ARG_COMPONENTS})
2024-03-12 13:27:30 +00:00
if(target_type STREQUAL "INTERFACE_LIBRARY")
2024-02-27 14:29:40 +00:00
target_include_directories(${TARGET_NAME} INTERFACE ${Qt${myx_qt_ver}${iter}_INCLUDE_DIRS})
2023-08-01 09:03:08 +00:00
else()
2024-02-27 14:29:40 +00:00
target_include_directories(${TARGET_NAME} PRIVATE ${Qt${myx_qt_ver}${iter}_INCLUDE_DIRS})
if(NOT iter STREQUAL "LinguistTools")
2024-03-12 13:27:30 +00:00
if(target_type STREQUAL "EXECUTABLE")
2024-02-27 14:29:40 +00:00
target_link_libraries(${TARGET_NAME} PRIVATE Qt${myx_qt_ver}::${iter})
endif()
2024-03-12 13:27:30 +00:00
if(target_type STREQUAL "SHARED_LIBRARY")
2024-02-27 14:29:40 +00:00
target_link_libraries(${TARGET_NAME} PUBLIC Qt${myx_qt_ver}::${iter})
endif()
2024-03-12 13:27:30 +00:00
if((${CMAKE_VERSION} VERSION_GREATER "3.8.0") AND (target_type STREQUAL "OBJECT_LIBRARY"))
2024-02-27 14:29:40 +00:00
target_link_libraries(${TARGET_NAME} PUBLIC Qt${myx_qt_ver}::${iter})
else()
target_include_directories(${TARGET_NAME} PUBLIC ${Qt${myx_qt_ver}${iter}_INCLUDE_DIRS})
endif()
2023-08-01 08:21:33 +00:00
endif()
endif()
endforeach()
foreach(iter ${ARG_PRIVATE})
2024-03-12 13:27:30 +00:00
if(target_type STREQUAL "INTERFACE_LIBRARY")
2023-08-01 08:21:33 +00:00
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()
2024-03-12 13:27:30 +00:00
if(target_type STREQUAL "EXECUTABLE")
2023-08-01 08:21:33 +00:00
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_qt_ver}_wrap_ui)
2023-08-01 08:21:33 +00:00
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)
2023-08-01 09:03:08 +00:00
# TODO
target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_BINARY_DIR})
2023-08-01 08:21:33 +00:00
endif()
endif()
# Перечень файлов, подлежащих переводу
2024-03-12 13:27:30 +00:00
if(target_type STREQUAL "INTERFACE_LIBRARY")
2023-08-01 08:21:33 +00:00
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_qt_ver}_create_translation)
2023-08-01 08:21:33 +00:00
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}.qm\">${TARGET_NAME}_${iter}.qm</file>\n")
2023-08-01 08:21:33 +00:00
# Добавление скомпилированного файла переводов к списку зависимостей для цели
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)
2024-03-12 13:27:30 +00:00
if(target_type STREQUAL "INTERFACE_LIBRARY")
2023-08-01 08:21:33 +00:00
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
)
2023-08-01 08:21:33 +00:00
endif()
endfunction()