myx/MyxCMake/lib/QtTargetSetup.cmake

145 lines
6.0 KiB
CMake
Raw Normal View History

2022-09-29 10:34:57 +00:00
include_guard(GLOBAL)
function(myx_qt_target_setup TARGET_NAME)
2022-09-29 10:34:57 +00:00
set(options)
set(oneValueArgs VERSION)
2022-10-02 13:22:43 +00:00
set(multiValueArgs COMPONENTS PRIVATE PUBLIC_MOC PRIVATE_MOC UI QRC LANGS)
2022-10-01 20:34:07 +00:00
2022-10-01 23:39:48 +00:00
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
2022-09-29 10:34:57 +00:00
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()
2022-11-02 12:51:08 +00:00
get_target_property(__target_type ${TARGET_NAME} TYPE)
2022-10-01 20:34:07 +00:00
2022-10-04 03:08:31 +00:00
foreach(iter ${ARG_COMPONENTS})
find_package(Qt${ARG_VERSION} COMPONENTS ${iter} REQUIRED)
2022-11-02 12:51:08 +00:00
if(__target_type STREQUAL "INTERFACE_LIBRARY")
target_include_directories(${TARGET_NAME} INTERFACE ${Qt${ARG_VERSION}${iter}_INCLUDE_DIRS})
2022-10-04 05:08:30 +00:00
else()
target_include_directories(${TARGET_NAME} PRIVATE ${Qt${ARG_VERSION}${iter}_INCLUDE_DIRS})
2022-10-04 05:08:30 +00:00
endif()
2022-11-02 12:51:08 +00:00
if(NOT iter STREQUAL "LinguistTools")
if(__target_type STREQUAL "EXECUTABLE")
target_link_libraries(${TARGET_NAME} PRIVATE "Qt${ARG_VERSION}::${iter}")
2022-11-02 12:51:08 +00:00
endif()
2023-04-13 06:58:45 +00:00
if(__target_type STREQUAL "SHARED_LIBRARY")
target_link_libraries(${TARGET_NAME} PUBLIC "Qt${ARG_VERSION}::${iter}")
2023-04-13 06:58:45 +00:00
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}")
2022-11-02 12:51:08 +00:00
endif()
endif()
2022-10-04 03:08:31 +00:00
endforeach()
2022-09-29 10:34:57 +00:00
2022-10-04 03:08:31 +00:00
foreach(iter ${ARG_PRIVATE})
find_package("Qt${ARG_VERSION}${iter}" COMPONENTS Private REQUIRED)
2022-11-02 12:51:08 +00:00
if(__target_type STREQUAL "INTERFACE_LIBRARY")
target_include_directories(${TARGET_NAME} INTERFACE ${Qt${ARG_VERSION}${iter}_PRIVATE_INCLUDE_DIRS})
2022-10-04 05:08:30 +00:00
else()
target_include_directories(${TARGET_NAME} PRIVATE ${Qt${ARG_VERSION}${iter}_PRIVATE_INCLUDE_DIRS})
2022-10-04 05:08:30 +00:00
endif()
2022-10-04 03:08:31 +00:00
endforeach()
2022-09-29 10:34:57 +00:00
2022-11-02 12:51:08 +00:00
if(__target_type STREQUAL "EXECUTABLE")
target_compile_options(${TARGET_NAME} PRIVATE ${Qt${ARG_VERSION}Core_EXECUTABLE_COMPILE_FLAGS})
2022-10-01 20:34:07 +00:00
endif()
2022-10-02 13:22:43 +00:00
if(ARG_PUBLIC_MOC)
2022-10-15 07:14:08 +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-02 10:12:07 +00:00
endif()
2022-10-02 13:22:43 +00:00
if(ARG_PRIVATE_MOC)
2022-10-15 07:14:08 +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-02 10:12:07 +00:00
endif()
2022-10-02 13:22:43 +00:00
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()
2022-10-02 10:12:07 +00:00
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()
2022-10-02 10:12:07 +00:00
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()
2022-10-15 07:14:08 +00:00
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY TR_FILES ${ARG_UI})
# TODO
target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_BINARY_DIR})
2022-10-01 20:34:07 +00:00
endif()
2022-09-29 10:34:57 +00:00
2022-10-04 09:58:19 +00:00
# Перечень файлов, подлежащих переводу
2022-11-02 12:51:08 +00:00
if(__target_type STREQUAL "INTERFACE_LIBRARY")
2022-10-15 07:14:08 +00:00
get_target_property(tr ${TARGET_NAME} INTERFACE_TR_FILES)
2022-10-08 14:23:13 +00:00
else()
2022-10-15 07:14:08 +00:00
get_target_property(tr ${TARGET_NAME} TR_FILES)
2022-10-08 14:23:13 +00:00
endif()
# Формирование файла ресурсов с переводами
2022-10-04 09:58:19 +00:00
if("LinguistTools" IN_LIST ARG_COMPONENTS AND tr)
# Заглавие файла ресурсов
2022-10-15 07:14:08 +00:00
file(WRITE ${PROJECT_BINARY_DIR}/${TARGET_NAME}_l10n.qrc "<RCC><qresource prefix=\"/qm\">\n")
# Для каждого языка, указанное в параметре LANGS
2022-10-01 20:34:07 +00:00
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()
# Добавление записи для скомпилированного файла переводов в ресурсный файл
2022-10-15 07:14:08 +00:00
file(APPEND ${PROJECT_BINARY_DIR}/${TARGET_NAME}_l10n.qrc
"<file alias=\"${TARGET_NAME}_${iter}\">${TARGET_NAME}_${iter}.qm</file>\n")
# Добавление скомпилированного файла переводов к списку зависимостей для цели
2022-10-15 07:14:08 +00:00
target_sources(${TARGET_NAME} PRIVATE ${qm})
2022-09-29 10:34:57 +00:00
endforeach()
2023-07-30 07:53:43 +00:00
# Окончание файла ресурсов
2022-10-15 07:14:08 +00:00
file(APPEND ${PROJECT_BINARY_DIR}/${TARGET_NAME}_l10n.qrc "</qresource></RCC>\n")
2023-07-30 07:53:43 +00:00
# Компиляция файла ресурсов с переводами
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()
2022-10-15 07:14:08 +00:00
target_sources(${TARGET_NAME} PRIVATE ${qrc_l10n})
2022-09-29 10:34:57 +00:00
endif()
2022-10-04 09:58:19 +00:00
unset(tr)
2022-11-02 12:51:08 +00:00
if(__target_type STREQUAL "INTERFACE_LIBRARY")
2022-10-20 09:48:52 +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-01 20:34:07 +00:00
2022-10-02 13:22:43 +00:00
# Установка публичных заголовочных файлов
2022-10-02 13:33:14 +00:00
if(PROJECT_IS_TOP_LEVEL)
2022-10-08 22:01:31 +00:00
myx_install_relative(${PROJECT_SOURCE_DIR}
2022-10-02 13:33:14 +00:00
FILES ${ARG_PUBLIC_MOC}
DESTINATION ${CMAKE_INSTALL_PREFIX}
2022-10-09 01:23:55 +00:00
COMPONENT DEV
2022-10-02 13:33:14 +00:00
)
endif()
2022-09-29 10:34:57 +00:00
endfunction()