FluentUI/src/CMakeLists.txt

185 lines
6.8 KiB
CMake
Raw Normal View History

2023-06-08 23:19:38 +08:00
cmake_minimum_required(VERSION 3.20)
2023-09-09 20:09:20 +08:00
if (FLUENTUI_BUILD_STATIC_LIB AND (QT_VERSION VERSION_GREATER_EQUAL "6.2"))
2023-12-13 17:31:08 +08:00
project(fluentui VERSION 1.0)
2024-04-11 14:51:43 +08:00
else ()
2023-12-13 17:31:08 +08:00
project(fluentuiplugin VERSION 1.0)
2024-04-11 14:51:43 +08:00
endif ()
2023-09-09 20:09:20 +08:00
2024-03-09 15:35:48 +08:00
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/.cmake/)
2023-05-05 18:51:40 +08:00
#配置通用编译
2023-12-14 22:06:26 +08:00
set(CMAKE_CXX_STANDARD 17)
2023-04-27 14:30:10 +08:00
set(CMAKE_CXX_STANDARD_REQUIRED ON)
2023-12-14 22:06:26 +08:00
2024-03-11 00:02:11 +08:00
#设置版本号
2024-04-11 20:29:59 +08:00
add_definitions(-DFLUENTUI_VERSION=1,7,5,0)
2024-03-11 00:02:11 +08:00
2023-09-09 20:09:20 +08:00
if (FLUENTUI_BUILD_STATIC_LIB)
add_definitions(-DFLUENTUI_BUILD_STATIC_LIB)
2024-04-11 14:51:43 +08:00
endif ()
2023-09-09 20:09:20 +08:00
2024-03-11 00:02:11 +08:00
#编译参数设置
option(FLUENTUI_BUILD_STATIC_LIB "Build static library." OFF)
2023-12-21 18:12:43 +08:00
#导入Qt相关依赖包
2023-08-24 15:50:37 +08:00
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Quick Qml)
2024-04-11 14:51:43 +08:00
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Quick)
2023-08-22 18:19:40 +08:00
2024-03-11 00:02:11 +08:00
set(QT_SDK_DIR "${Qt${QT_VERSION_MAJOR}_DIR}/../../..")
cmake_path(SET QT_SDK_DIR NORMALIZE ${QT_SDK_DIR})
#设置QML插件输出目录可以通过外部设置如果外部没有设置就默认到<QT_SDK_DIR_PATH>\qml\FluentUI目录下
set(FLUENTUI_QML_PLUGIN_DIRECTORY "" CACHE PATH "Path to FluentUI plugin")
2024-04-11 14:51:43 +08:00
if (NOT FLUENTUI_QML_PLUGIN_DIRECTORY)
2024-03-11 00:02:11 +08:00
set(FLUENTUI_QML_PLUGIN_DIRECTORY ${QT_SDK_DIR}/qml/FluentUI)
2024-04-11 14:51:43 +08:00
endif ()
2024-03-11 00:02:11 +08:00
2024-04-11 14:51:43 +08:00
if (QT_VERSION VERSION_GREATER_EQUAL "6.3")
qt_standard_project_setup()
2024-04-11 14:51:43 +08:00
else ()
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
2024-03-20 12:44:35 +08:00
set(CMAKE_INCLUDE_CURRENT_DIR ON)
2024-04-11 14:51:43 +08:00
endif ()
2024-03-13 17:54:11 +08:00
#国际化
find_program(QT_LUPDATE NAMES lupdate)
find_program(QT_LRELEASE NAMES lrelease)
2024-03-24 10:27:16 +08:00
if (NOT EXISTS ${CMAKE_CURRENT_LIST_DIR}/fluentui_en_US.qm)
execute_process(COMMAND ${QT_LUPDATE} ${CMAKE_CURRENT_LIST_DIR} -ts fluentui_en_US.ts WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
execute_process(COMMAND ${QT_LRELEASE} fluentui_en_US.ts WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
2024-03-13 17:54:11 +08:00
endif ()
2024-03-24 10:27:16 +08:00
if (NOT EXISTS ${CMAKE_CURRENT_LIST_DIR}/fluentui_zh_CN.qm)
execute_process(COMMAND ${QT_LUPDATE} ${CMAKE_CURRENT_LIST_DIR} -ts fluentui_zh_CN.ts WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
execute_process(COMMAND ${QT_LRELEASE} fluentui_zh_CN.ts WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
2024-03-13 17:54:11 +08:00
endif ()
file(GLOB QM_FILE_PATHS ${CMAKE_CURRENT_LIST_DIR}/ *.qm)
file(COPY ${QM_FILE_PATHS} DESTINATION "${CMAKE_CURRENT_SOURCE_DIR}/Qt${QT_VERSION_MAJOR}/imports/FluentUI/i18n")
2023-04-27 17:29:39 +08:00
#遍历所有Cpp文件
2024-04-11 14:51:43 +08:00
file(GLOB_RECURSE CPP_FILES *.cpp *.h *.cxx)
foreach (filepath ${CPP_FILES})
2023-04-27 09:38:57 +08:00
string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" filename ${filepath})
list(APPEND sources_files ${filename})
2024-04-11 14:51:43 +08:00
endforeach (filepath)
2023-04-27 09:38:57 +08:00
2024-04-11 14:51:43 +08:00
if (QT_VERSION VERSION_GREATER_EQUAL "6.2")
2023-08-24 15:50:37 +08:00
#删除fluentuiplugin.cpp与fluentuiplugin.h这些只要Qt5使用Qt6不需要
list(REMOVE_ITEM sources_files fluentuiplugin.h fluentuiplugin.cpp)
2023-09-10 16:58:23 +08:00
if (NOT FLUENTUI_BUILD_STATIC_LIB)
list(REMOVE_ITEM sources_files FluentUI.h FluentUI.cpp)
2024-04-11 14:51:43 +08:00
endif ()
2023-04-27 09:38:57 +08:00
2023-08-24 15:50:37 +08:00
#遍历所有qml文件
2024-03-27 00:36:56 +08:00
file(GLOB_RECURSE QML_PATHS *.qml qmldir)
2024-04-11 14:51:43 +08:00
foreach (filepath ${QML_PATHS})
if (${filepath} MATCHES "Qt${QT_VERSION_MAJOR}/")
2023-08-24 15:50:37 +08:00
string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" filename ${filepath})
2024-04-11 14:51:43 +08:00
if (${filename} MATCHES "qmldir")
2024-03-27 00:36:56 +08:00
list(APPEND resource_files ${filename})
2024-04-11 14:51:43 +08:00
else ()
2024-03-27 00:36:56 +08:00
list(APPEND qml_files ${filename})
2024-04-11 14:51:43 +08:00
endif ()
endif ()
endforeach (filepath)
2023-08-24 15:50:37 +08:00
#遍历所有资源文件
2024-03-13 17:54:11 +08:00
file(GLOB_RECURSE RES_PATHS *.png *.jpg *.svg *.ico *.ttf *.webp *.js *.qm)
2024-04-11 14:51:43 +08:00
foreach (filepath ${RES_PATHS})
if (${filepath} MATCHES "Qt${QT_VERSION_MAJOR}/")
2023-08-24 15:50:37 +08:00
string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" filename ${filepath})
list(APPEND resource_files ${filename})
2024-04-11 14:51:43 +08:00
endif ()
endforeach (filepath)
2023-04-27 09:38:57 +08:00
2023-08-24 15:50:37 +08:00
#修改资源文件导出路径
2024-04-11 14:51:43 +08:00
foreach (filepath IN LISTS qml_files resource_files)
2023-08-24 15:50:37 +08:00
string(REPLACE "Qt${QT_VERSION_MAJOR}/imports/FluentUI/" "" filename ${filepath})
set_source_files_properties(${filepath} PROPERTIES QT_RESOURCE_ALIAS ${filename})
2024-04-11 14:51:43 +08:00
endforeach ()
endif ()
2023-04-27 09:38:57 +08:00
if (FLUENTUI_BUILD_STATIC_LIB)
set(LIB_TYPE "STATIC")
2024-04-11 14:51:43 +08:00
else ()
set(LIB_TYPE "SHARED")
2024-04-11 14:51:43 +08:00
endif ()
2023-07-26 11:02:33 +08:00
if (FLUENTUI_BUILD_STATIC_LIB)
2023-07-26 11:02:33 +08:00
set(PLUGIN_TARGET_NAME "")
2024-04-11 14:51:43 +08:00
else ()
2023-07-26 11:02:33 +08:00
#如果是动态库,则使用插件目标作为其自己的支持目标来定义 QML 模块,在这种情况下,模块必须在运行时动态加载,并且不能由其他目标直接链接到
set(PLUGIN_TARGET_NAME ${PROJECT_NAME})
2024-04-11 14:51:43 +08:00
endif ()
2023-04-01 21:01:46 +08:00
2023-08-10 16:08:27 +08:00
#如果是Windows平台则生成rc文件
set(FLUENTUI_VERSION_RC_PATH "")
2024-04-11 14:51:43 +08:00
if (WIN32)
2024-03-09 18:26:54 +08:00
set(FLUENTUI_VERSION_RC_PATH ${CMAKE_CURRENT_BINARY_DIR}/version_${PROJECT_NAME}.rc)
2023-08-10 16:08:27 +08:00
configure_file(
2024-04-11 14:51:43 +08:00
${CMAKE_CURRENT_SOURCE_DIR}/.cmake/version_dll.rc.in
${FLUENTUI_VERSION_RC_PATH}
2023-08-10 16:08:27 +08:00
)
2024-04-11 14:51:43 +08:00
endif ()
2023-08-10 16:08:27 +08:00
2024-04-11 14:51:43 +08:00
if (QT_VERSION VERSION_GREATER_EQUAL "6.2")
2023-12-21 18:12:43 +08:00
#如果是Qt6.2版本以上则使用qt_add_libraryqt_add_qml_module函数添加资源文件
2024-04-11 14:51:43 +08:00
if (FLUENTUI_BUILD_STATIC_LIB)
2024-03-09 15:35:48 +08:00
set(FLUENTUI_QML_PLUGIN_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/FluentUI)
2024-04-11 14:51:43 +08:00
endif ()
2023-08-24 15:50:37 +08:00
qt_add_library(${PROJECT_NAME} ${LIB_TYPE})
qt_add_qml_module(${PROJECT_NAME}
2024-04-11 14:51:43 +08:00
PLUGIN_TARGET ${PLUGIN_TARGET_NAME}
OUTPUT_DIRECTORY ${FLUENTUI_QML_PLUGIN_DIRECTORY}
VERSION 1.0
URI "FluentUI"
#修改qmltypes文件名称。默认fluentuiplugin.qmltypes使用默认名称有时候import FluentUI 1.0会爆红所以修改成plugins.qmltypes
TYPEINFO "plugins.qmltypes"
SOURCES ${sources_files} ${FLUENTUI_VERSION_RC_PATH}
QML_FILES ${qml_files}
RESOURCES ${resource_files}
RESOURCE_PREFIX "/qt/qml"
2023-08-24 15:50:37 +08:00
)
2024-04-11 14:51:43 +08:00
else ()
2024-04-12 16:50:49 +08:00
qt_add_resources(QRC_RESOURCES Qt5/imports/fluentui.qrc)
2023-12-21 18:12:43 +08:00
#如果是Qt6.2版本以下则使用add_qmlplugin函数添加资源文件这是个自定义的函数详情见.cmake/QmlPlugin.cmake
2023-08-24 15:50:37 +08:00
include(QmlPlugin)
add_qmlplugin(${PROJECT_NAME}
2024-04-11 14:51:43 +08:00
URI "FluentUI"
VERSION 1.0
SOURCES ${sources_files} ${FLUENTUI_VERSION_RC_PATH} ${QRC_RESOURCES}
QMLFILES ${qml_files}
QMLDIR imports/FluentUI
BINARY_DIR ${FLUENTUI_QML_PLUGIN_DIRECTORY}
LIBTYPE ${LIB_TYPE}
2023-08-24 15:50:37 +08:00
)
2024-04-11 14:51:43 +08:00
endif ()
2023-07-26 11:02:33 +08:00
2023-12-13 17:31:08 +08:00
target_compile_definitions(${PROJECT_NAME}
2024-04-11 14:51:43 +08:00
PRIVATE
#导入qrcode配置文件
HAVE_CONFIG_H
2023-12-13 17:31:08 +08:00
)
2023-10-11 23:56:16 +08:00
2023-09-09 10:19:47 +08:00
#去掉mingw生成的动态库libxxx前缀lib不去掉前缀会导致 module "FluentUI" plugin "fluentuiplugin" not found
2024-04-11 14:51:43 +08:00
if (MINGW)
2023-10-11 23:56:16 +08:00
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "")
2024-04-11 14:51:43 +08:00
endif ()
2023-10-11 23:56:16 +08:00
#MSVC Debug 添加后缀d与Qt插件风格保持一致
2024-04-11 14:51:43 +08:00
if (MSVC)
2023-10-11 23:56:16 +08:00
set_target_properties(${PROJECT_NAME} PROPERTIES DEBUG_POSTFIX "d")
2024-04-11 14:51:43 +08:00
endif ()
2023-09-09 10:19:47 +08:00
2023-04-27 17:29:39 +08:00
#链接库
target_link_libraries(${PROJECT_NAME} PUBLIC
2024-04-11 14:51:43 +08:00
Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Quick
Qt${QT_VERSION_MAJOR}::Qml
2023-04-27 09:38:57 +08:00
)
2023-06-19 09:50:11 +08:00
#安装
2023-07-15 00:20:33 +08:00
install(DIRECTORY ${FLUENTUI_QML_PLUGIN_DIRECTORY} DESTINATION ${CMAKE_INSTALL_PREFIX}/imports)