myx/MyxCMake/lib/Qt5TargetSetup.cmake
2022-10-05 11:35:08 +03:00

95 lines
3.9 KiB
CMake
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

include_guard(GLOBAL)
function(myx_qt5_target_setup NAME)
set(options)
set(oneValueArgs)
set(multiValueArgs COMPONENTS PRIVATE PUBLIC_MOC PRIVATE_MOC UI QRC LANGS)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
get_target_property(target_type ${NAME} TYPE)
foreach(iter ${ARG_COMPONENTS})
if(target_type STREQUAL "INTERFACE_LIBRARY")
target_include_directories(${NAME} INTERFACE ${Qt5${iter}_INCLUDE_DIRS})
else()
target_include_directories(${NAME} PRIVATE ${Qt5${iter}_INCLUDE_DIRS})
endif()
if(target_type STREQUAL "EXECUTABLE" AND NOT iter STREQUAL "LinguistTools")
target_link_libraries(${NAME} PRIVATE "Qt5::${iter}")
endif()
endforeach()
foreach(iter ${ARG_PRIVATE})
if(target_type STREQUAL "INTERFACE_LIBRARY")
target_include_directories(${NAME} INTERFACE ${Qt5${iter}_PRIVATE_INCLUDE_DIRS})
else()
target_include_directories(${NAME} PRIVATE ${Qt5${iter}_PRIVATE_INCLUDE_DIRS})
endif()
endforeach()
if(target_type STREQUAL "EXECUTABLE")
target_compile_options(${NAME} PRIVATE ${Qt5Core_EXECUTABLE_COMPILE_FLAGS})
endif()
if(ARG_PUBLIC_MOC)
set_property(TARGET ${NAME} APPEND PROPERTY PUBLIC_HEADER_FILES "${ARG_PUBLIC_MOC}")
set_property(TARGET ${NAME} APPEND PROPERTY TR_FILES ${ARG_PUBLIC_MOC})
endif()
if(ARG_PRIVATE_MOC)
set_property(TARGET ${NAME} APPEND PROPERTY PRIVATE_HEADER_FILES "${ARG_PRIVATE_MOC}")
set_property(TARGET ${NAME} APPEND PROPERTY TR_FILES ${ARG_PRIVATE_MOC})
endif()
if(ARG_PUBLIC_MOC OR ARG_PRIVATE_MOC)
qt5_wrap_cpp(moc_cpp ${ARG_PUBLIC_MOC} ${ARG_PRIVATE_MOC})
endif()
if(ARG_QRC)
qt5_add_resources(qrc_cpp ${ARG_QRC})
endif()
if(ARG_UI AND "Widgets" IN_LIST ARG_COMPONENTS)
qt5_wrap_ui(ui_h ${ARG_UI})
set_property(TARGET ${NAME} APPEND PROPERTY TR_FILES ${ARG_UI})
# TODO
target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_BINARY_DIR})
endif()
# Перечень файлов, подлежащих переводу
get_target_property(tr ${NAME} TR_FILES)
# Формирование файла ресурсов с переводами
if("LinguistTools" IN_LIST ARG_COMPONENTS AND tr)
# Заглавие файла ресурсов
file(WRITE ${PROJECT_BINARY_DIR}/${NAME}_l10n.qrc "<RCC><qresource prefix=\"/qm\">\n")
# Для каждого языка, указанное в параметре LANGS
foreach(iter ${ARG_LANGS})
# Создание или обновление файла переводов в каталоге ${PROJECT_SOURCE_DIR}/l10n
# и его компиляция в каталог ${PROJECT_BINARY_DIR}
qt5_create_translation(qm ${tr} "${PROJECT_SOURCE_DIR}/l10n/${NAME}_${iter}.ts")
# Добавление записи для скомпилированного файла переводов в ресурсный файл
file(APPEND ${PROJECT_BINARY_DIR}/${NAME}_l10n.qrc
"<file alias=\"${NAME}_${iter}\">${NAME}_${iter}.qm</file>\n")
# Добавление скомпилированного файла переводов к списку зависимостей для цели
target_sources(${NAME} PRIVATE ${qm})
endforeach()
# Окончение файла ресурсов
file(APPEND ${PROJECT_BINARY_DIR}/${NAME}_l10n.qrc "</qresource></RCC>\n")
# Комплияция файла ресурсов с переводами
qt5_add_resources(qrc_l10n ${PROJECT_BINARY_DIR}/${NAME}_l10n.qrc)
target_sources(${NAME} PRIVATE ${qrc_l10n})
endif()
unset(tr)
target_sources(${NAME} PRIVATE ${ARG_PUBLIC_MOC} ${ARG_PRIVATE_MOC} ${moc_cpp} ${ui_h} ${qrc_cpp})
# Установка публичных заголовочных файлов
if(PROJECT_IS_TOP_LEVEL)
install_relative(${PROJECT_SOURCE_DIR}
FILES ${ARG_PUBLIC_MOC}
DESTINATION ${CMAKE_INSTALL_PREFIX}
COMPONENT dev
)
endif()
endfunction()