只需加一段代碼到項目文件,編譯完成後自動打包。
# Author: Qt君
# QQ交流羣: 732271126
# INFO: 關注微信公衆號: [Qt君] 第一時間獲取最新推送.
# --- [start]注意事項[start] --- #
# 目前僅支持windows平臺 #
# --- [end]注意事項[end] --- #
# --- [start]使用方法[start] --- #
# 複製本腳本代碼到你的項目文件中 #
# 或在項目文件中使用include包含本文件 #
# --- [end]使用方法[end] --- #
# --- [start]輸入參數[start] --- #
# 是否開啓打印信息輸出(不會影響主項目的打印輸出) #
# 需要屏蔽打印就將它註釋即可 #
# 默認不開啓 #
#DEBUG_LOGGER = hello world
# 是否在編譯完成後自動打開目標目錄
# 需要屏蔽該功能就將它註釋即可 #
# 默認開啓 #
DEPLOY_COMPLETE_AUTO_OPEN_EXPLORER = hello world
# 是否開啓實驗性功能 #
# 需要屏蔽實驗性功能只需註釋即可 #
# 默認不開啓 #
#EXPERIMENTAL = hello world # 優化qmake執行速度
# --- [end]輸入參數[end] --- #
# --- [start]函數[start] --- #
# 移除多餘的debug或release配置項(實驗功能)
defineReplace(remove_extra_config_parameter) {
configs = $$1
debug_and_release_params =
keys = debug Debug release Release #debug_and_release
for (iter, configs) {
contains(keys, $$lower($$iter)) {
debug_and_release_params += $$lower($$iter)
}
}
for (iter, debug_and_release_params) {
configs -= $$iter
}
configs += $$last(debug_and_release_params)
return($$configs)
}
# 獲取資源文件中的qml文件
defineReplace(find_qml_file) {
resources = $$1
qml_file_list =
for (resource, resources) {
content_lines = $$cat($$resource)
for (iter, content_lines) {
tmp = $$find(iter, <file>.*</file>)
!isEmpty(tmp) {
qml_file_list += $$replace(iter, (<file>(.*)<\/file>), \2)
}
}
}
return ($$qml_file_list)
}
# 獲取qml文件中使用到的模塊
defineReplace(get_qml_module_list) {
qml_file_list = $$1
default_qml_module_list = QtQuick.Shapes QtQuick.Particles
qml_module_list =
for (file, qml_file_list) {
content_lines = $$cat($$file)
for (iter, content_lines) {
contains(default_qml_module_list, $$iter) {
module_name = $$section(iter, ., 1, 1)
qml_module_list += $$module_name
}
}
}
!isEmpty(DEBUG_LOGGER): message(qml_module_list: $$qml_module_list)
return ($$qml_module_list)
}
# 獲取需要複製qml庫的命令行
defineReplace(get_copy_qml_library_cmd_line) {
qt_dir = $$1
qt_bin_dir = $$2
target_out_dir = $$3
resources = $$4
cmd_line =
qml_file_list = $$find_qml_file($$resources)
!isEmpty(DEBUG_LOGGER): !isEmpty(qml_file_list): message(result: $$qml_file_list)
qml_module_list = $$get_qml_module_list($$qml_file_list)
!isEmpty(DEBUG_LOGGER): !isEmpty(qml_module_list): message($$qml_module_list)
for (qml_module, qml_module_list) {
if (equals(qml_module, Particles)) {
# 源qml的quick的某個模塊庫目錄
source = $${qt_dir}qml/QtQuick/$${qml_module}.2
# 目標qml的quick的某個模塊源庫目錄
dest = $${target_out_dir}QtQuick/$${qml_module}.2
}
else {
# 源qml的quick的某個模塊庫目錄
source = $${qt_dir}qml/QtQuick/$${qml_module}
# 目標qml的quick的某個模塊源庫目錄
dest = $${target_out_dir}QtQuick/$${qml_module}
}
source = $$replace(source, /, \\)
dest = $$replace(dest, /, \\)
mkdir_qml_quick_module_dest_cmd_line = cmd /c mkdir $$dest # 創建模塊目錄在QtQuick
copy_qml_quick_module_file_cmd_line = cmd /c xcopy /s/y $$source $$dest # 複製Qml模塊到指定目錄
# 複製qml模塊(dll)(命令行)
CONFIG(debug, debug|release) {
qml_module_params = $${qt_bin_dir}Qt5Quick$${qml_module}d.dll $${target_out_dir}
}
else {
qml_module_params = $${qt_bin_dir}Qt5Quick$${qml_module}.dll $${target_out_dir}
}
qml_module_params = $$replace(qml_module_params, /, \\)
copy_qml_module_cmd_line = cmd /c copy $$qml_module_params
cmd_line += && $$mkdir_qml_quick_module_dest_cmd_line
cmd_line += & $$copy_qml_quick_module_file_cmd_line
cmd_line += && $$copy_qml_module_cmd_line
}
return ($$cmd_line)
}
# --- [end]函數[end] --- #
# 獲取從QMake執行文件的所在目錄得出Qt的bin路徑
QT_BIN_DIR = $$replace(QMAKE_QMAKE, ^(\S*/)\S+$, \1)
# 獲取Qt開發環境路徑
QT_DIR = $${QT_BIN_DIR}../
# Qt打包工具參數配置集合
DEPLOY_OPTIONS += --force
# 可用的Qt模塊
QT_AVAILABLE_LIBRARY_LIST = \
bluetooth concurrent core declarative designer designercomponents enginio \
gamepad gui qthelp multimedia multimediawidgets multimediaquick network nfc \
opengl positioning printsupport qml qmltooling quick quickparticles quickwidgets \
script scripttools sensors serialport sql svg test webkit webkitwidgets \
websockets widgets winextras xml xmlpatterns webenginecore webengine \
webenginewidgets 3dcore 3drenderer 3dquick 3dquickrenderer 3dinput 3danimation \
3dextras geoservices webchannel texttospeech serialbus webview
# 掃描QT變量用於打包模塊的參數配置
for (LIBRARY_MODULE, QT_AVAILABLE_LIBRARY_LIST) {
if (contains(QT, $$LIBRARY_MODULE)) {
DEPLOY_OPTIONS += --$$LIBRARY_MODULE
}
else {
DEPLOY_OPTIONS += --no-$$LIBRARY_MODULE
}
}
# 針對Qml模塊配置打包參數
if (contains(QT, quick)) {
DEPLOY_OPTIONS -= --no-qml
DEPLOY_OPTIONS += --qml
DEPLOY_OPTIONS -= --no-network
DEPLOY_OPTIONS += --network
DEPLOY_OPTIONS += --qmldir $${QT_DIR}qml/
}
if (!isEmpty(DESTDIR)) {
# 如有設置目標輸出路徑則定向於該路徑
TARGET_OUT_DIR = $$OUT_PWD/$$DESTDIR/
}
else {
# 判斷是debug版本還是release版本
CONFIG(debug, debug|release) {
TARGET_OUT_DIR = $${OUT_PWD}/debug/
DEPLOY_OPTIONS += --debug
}
else {
TARGET_OUT_DIR = $${OUT_PWD}/release/
DEPLOY_OPTIONS += --release
}
}
# 實驗性功能
!isEmpty(EXPERIMENTAL) {
# 該功能(用於優化qmake調試輸出)是否開放還需待定,因爲會導致其他未知的問題。
CONFIG = $$remove_extra_config_parameter($$CONFIG)
}
# 調試輸出
!isEmpty(DEBUG_LOGGER) {
message(TARGET_OUT_DIR: $$TARGET_OUT_DIR) # 生成文件的輸出目錄
message(QMAKE_POST_LINK: $$QMAKE_POST_LINK) # 打印命令
}
win32 {
# 拼接Qt部署程序的文件(windows平臺下爲windeployqt.exe)
WIN_DEPLOY_BIN = $${QT_BIN_DIR}windeployqt.exe
# 編譯完成後執行打包命令
QMAKE_POST_LINK += $$WIN_DEPLOY_BIN $$DEPLOY_OPTIONS $$TARGET_OUT_DIR$${TARGET}.exe
# 掃描Qml依賴庫,並在編譯完成後自動複製qml依賴庫到目標目錄
QMAKE_POST_LINK += $$get_copy_qml_library_cmd_line($$QT_DIR, $$QT_BIN_DIR, $$TARGET_OUT_DIR, $$RESOURCES)
!isEmpty(DEPLOY_COMPLETE_AUTO_OPEN_EXPLORER) {
# 打包完成後自動打開目標路徑
QMAKE_POST_LINK += && start $$TARGET_OUT_DIR
}
}