myx-cmake-example-features/cmake/myx/lib/Qt5TargetSetup.cmake

110 lines
4.7 KiB
CMake
Raw Permalink Normal View History

2022-10-06 12:22:57 +00:00
include_guard(GLOBAL)
2022-10-20 09:53:06 +00:00
function(myx_qt5_target_setup TARGET_NAME)
2022-10-06 12:22:57 +00:00
set(options)
set(oneValueArgs)
set(multiValueArgs COMPONENTS PRIVATE PUBLIC_MOC PRIVATE_MOC UI QRC LANGS)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
2023-02-17 11:36:44 +00:00
get_target_property(__target_type ${TARGET_NAME} TYPE)
2022-10-06 12:22:57 +00:00
foreach(iter ${ARG_COMPONENTS})
2023-02-17 11:36:44 +00:00
if(__target_type STREQUAL "INTERFACE_LIBRARY")
2022-10-20 09:53:06 +00:00
target_include_directories(${TARGET_NAME} INTERFACE ${Qt5${iter}_INCLUDE_DIRS})
2022-10-06 12:22:57 +00:00
else()
2022-10-20 09:53:06 +00:00
target_include_directories(${TARGET_NAME} PRIVATE ${Qt5${iter}_INCLUDE_DIRS})
2022-10-06 12:22:57 +00:00
endif()
2023-02-17 11:36:44 +00:00
if(NOT iter STREQUAL "LinguistTools")
if(__target_type STREQUAL "EXECUTABLE")
target_link_libraries(${TARGET_NAME} PRIVATE "Qt5::${iter}")
endif()
if((__target_type STREQUAL "SHARED_LIBRARY") OR (__target_type STREQUAL "OBJECT_LIBRARY"))
target_link_libraries(${TARGET_NAME} PUBLIC "Qt5::${iter}")
endif()
endif()
2022-10-06 12:22:57 +00:00
endforeach()
foreach(iter ${ARG_PRIVATE})
2023-02-17 11:36:44 +00:00
if(__target_type STREQUAL "INTERFACE_LIBRARY")
2022-10-20 09:53:06 +00:00
target_include_directories(${TARGET_NAME} INTERFACE ${Qt5${iter}_PRIVATE_INCLUDE_DIRS})
2022-10-06 12:22:57 +00:00
else()
2022-10-20 09:53:06 +00:00
target_include_directories(${TARGET_NAME} PRIVATE ${Qt5${iter}_PRIVATE_INCLUDE_DIRS})
2022-10-06 12:22:57 +00:00
endif()
endforeach()
2023-02-17 11:36:44 +00:00
if(__target_type STREQUAL "EXECUTABLE")
2022-10-20 09:53:06 +00:00
target_compile_options(${TARGET_NAME} PRIVATE ${Qt5Core_EXECUTABLE_COMPILE_FLAGS})
2022-10-06 12:22:57 +00:00
endif()
if(ARG_PUBLIC_MOC)
2022-10-20 09:53:06 +00:00
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})
2022-10-06 12:22:57 +00:00
endif()
if(ARG_PRIVATE_MOC)
2022-10-20 09:53:06 +00:00
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})
2022-10-06 12:22:57 +00:00
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})
2022-10-20 09:53:06 +00:00
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY TR_FILES ${ARG_UI})
2022-10-06 12:22:57 +00:00
# TODO
target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_BINARY_DIR})
endif()
# Перечень файлов, подлежащих переводу
2023-02-17 11:36:44 +00:00
if(__target_type STREQUAL "INTERFACE_LIBRARY")
2022-10-20 09:53:06 +00:00
get_target_property(tr ${TARGET_NAME} INTERFACE_TR_FILES)
else()
get_target_property(tr ${TARGET_NAME} TR_FILES)
endif()
2022-10-06 12:22:57 +00:00
# Формирование файла ресурсов с переводами
if("LinguistTools" IN_LIST ARG_COMPONENTS AND tr)
# Заглавие файла ресурсов
2022-10-20 09:53:06 +00:00
file(WRITE ${PROJECT_BINARY_DIR}/${TARGET_NAME}_l10n.qrc "<RCC><qresource prefix=\"/qm\">\n")
2022-10-06 12:22:57 +00:00
# Для каждого языка, указанное в параметре LANGS
foreach(iter ${ARG_LANGS})
# Создание или обновление файла переводов в каталоге ${PROJECT_SOURCE_DIR}/l10n
# и его компиляция в каталог ${PROJECT_BINARY_DIR}
2022-10-20 09:53:06 +00:00
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})
2022-10-06 12:22:57 +00:00
# Добавление записи для скомпилированного файла переводов в ресурсный файл
2022-10-20 09:53:06 +00:00
file(APPEND ${PROJECT_BINARY_DIR}/${TARGET_NAME}_l10n.qrc
"<file alias=\"${TARGET_NAME}_${iter}\">${TARGET_NAME}_${iter}.qm</file>\n")
2022-10-06 12:22:57 +00:00
# Добавление скомпилированного файла переводов к списку зависимостей для цели
2022-10-20 09:53:06 +00:00
target_sources(${TARGET_NAME} PRIVATE ${qm})
2022-10-06 12:22:57 +00:00
endforeach()
# Окончение файла ресурсов
2022-10-20 09:53:06 +00:00
file(APPEND ${PROJECT_BINARY_DIR}/${TARGET_NAME}_l10n.qrc "</qresource></RCC>\n")
2022-10-06 12:22:57 +00:00
# Комплияция файла ресурсов с переводами
2022-10-20 09:53:06 +00:00
qt5_add_resources(qrc_l10n ${PROJECT_BINARY_DIR}/${TARGET_NAME}_l10n.qrc)
target_sources(${TARGET_NAME} PRIVATE ${qrc_l10n})
2022-10-06 12:22:57 +00:00
endif()
unset(tr)
2023-02-17 11:36:44 +00:00
if(__target_type STREQUAL "INTERFACE_LIBRARY")
2022-10-20 10:33:03 +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()
2022-10-06 12:22:57 +00:00
# Установка публичных заголовочных файлов
if(PROJECT_IS_TOP_LEVEL)
2022-10-20 09:53:06 +00:00
myx_install_relative(${PROJECT_SOURCE_DIR}
2022-10-06 12:22:57 +00:00
FILES ${ARG_PUBLIC_MOC}
DESTINATION ${CMAKE_INSTALL_PREFIX}
2022-10-20 09:53:06 +00:00
COMPONENT DEV
2022-10-06 12:22:57 +00:00
)
endif()
endfunction()