添加项目文件。
This commit is contained in:
+746
@@ -0,0 +1,746 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
# ===========================================================================
|
||||
# CavityLIBS —— 腔体 LIBS 系统
|
||||
#
|
||||
# 本工程不再写死 Qt / Qwt / ARIS 的安装路径(原来固定为 D:/QT、D:/qwt、
|
||||
# D:/aris,换机器后必然配置失败):
|
||||
#
|
||||
# * Qt —— 由构建套件(CMakePresets 里的 CMAKE_PREFIX_PATH)或命令行
|
||||
# -DCMAKE_PREFIX_PATH=<Qt 目录> 提供;
|
||||
# * Qwt —— 依次在 third_party、环境变量 QWT_ROOT、常见安装位置中查找,
|
||||
# **按编译器挑选库形式**:
|
||||
# MinGW -> lib/libqwt.a (GCC 编译的 qwt)
|
||||
# MSVC -> lib/qwt.lib (VS 编译的 qwt,见 VS2026 说明)
|
||||
# Qwt 是 C++ 库,必须与主程序用同一个编译器编译,混用无法链接;
|
||||
# * ARIS —— 同样自动查找(MinGW 用 libaris.dll.a,MSVC 用 libaris.lib);
|
||||
# 找不到与目标匹配的 SDK 时回退到 third_party/aris-stub 兼容桩;
|
||||
# * libusb 头文件用于 ARIS 头文件包含,运行库在部署阶段复制。
|
||||
#
|
||||
# 支持的套件:
|
||||
# * Visual Studio 2026 / MSVC x64(推荐,见 README-VS2026-构建运行.md)
|
||||
# * Qt Creator + MinGW(原方案,需要 G: 盘那套 Qt mingw 和 qwt)
|
||||
# ===========================================================================
|
||||
|
||||
project(CavityLIBS LANGUAGES CXX)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 基本编译设置
|
||||
# ---------------------------------------------------------------------------
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
# 自动处理 Qt 的 MOC / UIC / RCC
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_AUTOUIC ON)
|
||||
set(CMAKE_AUTORCC ON)
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
# 可执行文件统一输出到 <build>/bin,DLL 和运行数据也部署到这里
|
||||
set(CAVITY_BIN_DIR "${CMAKE_BINARY_DIR}/bin")
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CAVITY_BIN_DIR}")
|
||||
foreach(_cfg DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${_cfg} "${CAVITY_BIN_DIR}")
|
||||
endforeach()
|
||||
|
||||
# 目标位数 / 位数标签 / 工具链标签
|
||||
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||
set(CAVITY_BITS 64)
|
||||
set(CAVITY_TAG "x64")
|
||||
else()
|
||||
set(CAVITY_BITS 32)
|
||||
set(CAVITY_TAG "x86")
|
||||
endif()
|
||||
|
||||
if(MSVC)
|
||||
set(CAVITY_MSVC ON)
|
||||
set(CAVITY_TOOLCHAIN "MSVC")
|
||||
else()
|
||||
set(CAVITY_MSVC OFF)
|
||||
set(CAVITY_TOOLCHAIN "GCC")
|
||||
endif()
|
||||
|
||||
message(STATUS "CavityLIBS: 目标为 ${CAVITY_BITS} 位程序(${CAVITY_TAG}),工具链 ${CAVITY_TOOLCHAIN}")
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# MSVC 专用编译设置
|
||||
#
|
||||
# 1) /utf-8
|
||||
# 源码是 UTF-8(多数没有 BOM),中文字面量必须按 UTF-8 解析;否则 MSVC 会
|
||||
# 按 CP936 读,中文字符的第二字节 0x5C 会被当作行尾续行符,报
|
||||
# “字符串字面量中的换行符(C2001)” 之类的错误。
|
||||
# 2) /FI 兼容层
|
||||
# Qt 5.15.2 的 qvector.h / qlist.h 用到 stdext::make_checked_array_iterator,
|
||||
# 这个辅助函数在新版 MSVC(VS2022 后期 / VS2026)的 STL 里已被删除,
|
||||
# 不补回则任何包含 Qt 容器的源文件都无法编译。见 compat/qt5_msvc_compat.h。
|
||||
# 3) 关闭依赖 CRT 安全警告、补上 M_PI 等数学常量。
|
||||
# ---------------------------------------------------------------------------
|
||||
function(cavity_apply_toolchain_settings target)
|
||||
if(CAVITY_MSVC)
|
||||
target_compile_options(${target} PRIVATE
|
||||
/utf-8
|
||||
"/FI${CMAKE_CURRENT_SOURCE_DIR}/compat/qt5_msvc_compat.h"
|
||||
/Zc:__cplusplus
|
||||
/Zc:preprocessor
|
||||
/wd4819 # 厂商头文件里的 GBK 注释
|
||||
/wd4828
|
||||
/wd4996)
|
||||
target_compile_definitions(${target} PRIVATE
|
||||
_CRT_SECURE_NO_WARNINGS
|
||||
_USE_MATH_DEFINES
|
||||
NOMINMAX
|
||||
UNICODE
|
||||
_UNICODE
|
||||
WIN32_LEAN_AND_MEAN)
|
||||
if(CMAKE_GENERATOR MATCHES "Visual Studio")
|
||||
target_compile_options(${target} PRIVATE /MP)
|
||||
endif()
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 工具函数:读取 PE 文件的 Machine 字段,返回 x86 / x64 / 空
|
||||
# 用于在配置阶段发现“32 位编译器配 64 位库”这类问题,给出明确提示
|
||||
# ---------------------------------------------------------------------------
|
||||
function(cavity_pe_machine dll_path out_var)
|
||||
set(_machine "")
|
||||
if(EXISTS "${dll_path}")
|
||||
file(READ "${dll_path}" _lfanew OFFSET 60 LIMIT 4 HEX)
|
||||
string(LENGTH "${_lfanew}" _len)
|
||||
if(_len EQUAL 8)
|
||||
string(SUBSTRING "${_lfanew}" 0 2 _b0)
|
||||
string(SUBSTRING "${_lfanew}" 2 2 _b1)
|
||||
string(SUBSTRING "${_lfanew}" 4 2 _b2)
|
||||
string(SUBSTRING "${_lfanew}" 6 2 _b3)
|
||||
math(EXPR _offset "0x${_b3}${_b2}${_b1}${_b0}")
|
||||
math(EXPR _machine_offset "${_offset} + 4")
|
||||
file(READ "${dll_path}" _machine_hex OFFSET ${_machine_offset} LIMIT 2 HEX)
|
||||
string(SUBSTRING "${_machine_hex}" 0 2 _m0)
|
||||
string(SUBSTRING "${_machine_hex}" 2 2 _m1)
|
||||
math(EXPR _value "0x${_m1}${_m0}")
|
||||
if(_value EQUAL 34404) # 0x8664
|
||||
set(_machine "x64")
|
||||
elseif(_value EQUAL 332) # 0x014c
|
||||
set(_machine "x86")
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
set(${out_var} "${_machine}" PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Qt
|
||||
# ---------------------------------------------------------------------------
|
||||
find_package(Qt5 5.15 REQUIRED COMPONENTS Core Widgets Gui SerialPort Sql)
|
||||
|
||||
# 校验 Qt 的位数与当前编译器是否一致
|
||||
set(_qt_core_dll "")
|
||||
foreach(_prop IMPORTED_LOCATION IMPORTED_LOCATION_RELEASE IMPORTED_LOCATION_DEBUG)
|
||||
get_target_property(_prop_value Qt5::Core ${_prop})
|
||||
if(_prop_value AND EXISTS "${_prop_value}")
|
||||
set(_qt_core_dll "${_prop_value}")
|
||||
break()
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
if(_qt_core_dll)
|
||||
cavity_pe_machine("${_qt_core_dll}" _qt_machine)
|
||||
if(_qt_machine AND NOT _qt_machine STREQUAL "${CAVITY_TAG}")
|
||||
message(FATAL_ERROR
|
||||
"Qt 与编译器位数不一致:\n"
|
||||
" 当前编译器:${CAVITY_BITS} 位(${CAVITY_TOOLCHAIN} ${CMAKE_CXX_COMPILER})\n"
|
||||
" Qt5Core :${_qt_core_dll} [${_qt_machine}]\n"
|
||||
"请在 CMakePresets 里选择与 Qt 位数一致的套件,"
|
||||
"或在命令行传入 -DCMAKE_PREFIX_PATH=<对应位数的 Qt 目录>。")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Qt 运行库目录(部署阶段复制 DLL / 插件用)
|
||||
get_filename_component(_qt_bin_dir "${Qt5Core_DIR}/../../../bin" ABSOLUTE)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Qwt
|
||||
# Qwt 是 C++ 库:MSVC 只能用 MSVC 编译的 qwt,MinGW 只能用 GCC 编译的 qwt。
|
||||
# 库文件名因此按工具链区分。
|
||||
# ---------------------------------------------------------------------------
|
||||
set(QWT_ROOT "" CACHE PATH "Qwt 根目录(包含 include/ 或 src/ 以及 lib/)")
|
||||
|
||||
if(CAVITY_MSVC)
|
||||
set(_qwt_lib_names_release qwt.lib)
|
||||
set(_qwt_lib_names_debug qwtd.lib)
|
||||
set(_qwt_candidates
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/qwt-6.2.0-msvc"
|
||||
"$ENV{QWT_ROOT}"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/msvc-deps/qwt-6.2.0"
|
||||
"D:/qwt/qwt-6.2.0-msvc"
|
||||
"C:/Qwt-6.2.0")
|
||||
else()
|
||||
set(_qwt_lib_names_release libqwt.a)
|
||||
set(_qwt_lib_names_debug libqwtd.a)
|
||||
set(_qwt_candidates
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/qwt-6.3.0-mingw${CAVITY_BITS}"
|
||||
"$ENV{QWT_ROOT}"
|
||||
"G:/qt/Qt/mylib/qwt-6.3.0"
|
||||
"D:/qwt/qwt-6.3.0"
|
||||
"C:/Qwt-6.3.0"
|
||||
"E:/perfessional/QT/install/qwt-6.3.0")
|
||||
endif()
|
||||
|
||||
if(NOT QWT_ROOT)
|
||||
foreach(_cand IN LISTS _qwt_candidates)
|
||||
if(NOT _cand)
|
||||
continue()
|
||||
endif()
|
||||
|
||||
# 头文件目录:优先 <root>/include,其次 <root>/src(qwt 源码树)
|
||||
set(_qwt_headers "")
|
||||
if(EXISTS "${_cand}/include/qwt_plot.h")
|
||||
set(_qwt_headers "${_cand}/include")
|
||||
elseif(EXISTS "${_cand}/src/qwt_plot.h")
|
||||
set(_qwt_headers "${_cand}/src")
|
||||
endif()
|
||||
|
||||
# 库文件:必须是对应工具链的形式
|
||||
set(_qwt_lib_found "")
|
||||
foreach(_libname IN LISTS _qwt_lib_names_release)
|
||||
if(EXISTS "${_cand}/lib/${_libname}")
|
||||
set(_qwt_lib_found "${_cand}/lib/${_libname}")
|
||||
break()
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
if(NOT _qwt_headers OR NOT _qwt_lib_found)
|
||||
continue()
|
||||
endif()
|
||||
|
||||
# 位数校验:优先看 qwt.dll / qwtd.dll 的 PE 头
|
||||
set(_arch_ok TRUE)
|
||||
foreach(_qwt_dll qwt.dll qwtd.dll)
|
||||
if(EXISTS "${_cand}/lib/${_qwt_dll}")
|
||||
cavity_pe_machine("${_cand}/lib/${_qwt_dll}" _qwt_machine)
|
||||
if(_qwt_machine AND NOT _qwt_machine STREQUAL "${CAVITY_TAG}")
|
||||
set(_arch_ok FALSE)
|
||||
endif()
|
||||
break()
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
if(_arch_ok)
|
||||
set(QWT_ROOT "${_cand}")
|
||||
break()
|
||||
endif()
|
||||
endforeach()
|
||||
endif()
|
||||
|
||||
if(NOT QWT_ROOT)
|
||||
if(CAVITY_MSVC)
|
||||
message(FATAL_ERROR
|
||||
"找不到 MSVC 版 Qwt(需要 lib/qwt.lib 与 qwt_*.h 头文件)。\n"
|
||||
"Qwt 是 C++ 库,必须用与本工程相同的编译器编译:\n"
|
||||
" 1) 用 msvc-deps/build-qwt2.bat 现场编译(源码 msvc-deps/qwt-6.2.0);\n"
|
||||
" 2) 把生成的 lib/qwt.lib、lib/qwt.dll、lib/qwtd.lib、lib/qwtd.dll\n"
|
||||
" 和 src/*.h 放到 third_party/qwt-6.2.0-msvc 下;\n"
|
||||
" 3) 或用 -DQWT_ROOT=<Qwt 根目录> 指定。")
|
||||
else()
|
||||
message(FATAL_ERROR
|
||||
"找不到 ${CAVITY_BITS} 位 Qwt(需要 lib/libqwt.a 与 qwt_*.h 头文件)。\n"
|
||||
"可以用 -DQWT_ROOT=<Qwt 根目录> 指定,或设置环境变量 QWT_ROOT。")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(EXISTS "${QWT_ROOT}/include/qwt_plot.h")
|
||||
set(QWT_INCLUDE_DIR "${QWT_ROOT}/include")
|
||||
else()
|
||||
set(QWT_INCLUDE_DIR "${QWT_ROOT}/src")
|
||||
endif()
|
||||
set(QWT_LIB_DIR "${QWT_ROOT}/lib")
|
||||
message(STATUS "Qwt: ${QWT_ROOT}")
|
||||
|
||||
# Release / Debug 两套库:qwt 在 Debug 下生成 qwtd,MinGW 下是 libqwtd.a。
|
||||
# 只有在 Qt 自身也提供 Debug 运行库时才用 Debug 版 qwt(否则 MSVC 的
|
||||
# debug/release CRT 混用会在运行期崩溃)。
|
||||
set(_qwt_lib_release "")
|
||||
set(_qwt_lib_debug "")
|
||||
foreach(_libname IN LISTS _qwt_lib_names_release)
|
||||
if(EXISTS "${QWT_LIB_DIR}/${_libname}")
|
||||
set(_qwt_lib_release "${QWT_LIB_DIR}/${_libname}")
|
||||
break()
|
||||
endif()
|
||||
endforeach()
|
||||
foreach(_libname IN LISTS _qwt_lib_names_debug)
|
||||
if(EXISTS "${QWT_LIB_DIR}/${_libname}")
|
||||
set(_qwt_lib_debug "${QWT_LIB_DIR}/${_libname}")
|
||||
break()
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
set(_qwt_dll_release "${QWT_LIB_DIR}/qwt.dll")
|
||||
set(_qwt_dll_debug "${QWT_LIB_DIR}/qwtd.dll")
|
||||
|
||||
if(NOT EXISTS "${_qt_bin_dir}/Qt5Cored.dll" OR NOT _qwt_lib_debug)
|
||||
# 没有 Debug 版 Qt / qwt:Debug 配置也统一用 release 版(带符号即可)
|
||||
set(_qwt_lib_debug "${_qwt_lib_release}")
|
||||
set(_qwt_dll_debug "${_qwt_dll_release}")
|
||||
endif()
|
||||
|
||||
set(QWT_LIB "$<IF:$<CONFIG:Debug>,${_qwt_lib_debug},${_qwt_lib_release}>")
|
||||
set(QWT_RUNTIME_DLL "$<IF:$<CONFIG:Debug>,${_qwt_dll_debug},${_qwt_dll_release}>")
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# ARIS 光谱仪 SDK
|
||||
# 注意:厂商只提供 MinGW 编译的 libaris.dll(导出的是 GCC 名字修饰的 C++
|
||||
# 符号,且要求继承它的回调类)。MSVC 无法链接这种库,所以 MSVC 目标下
|
||||
# 只能使用项目内置的兼容桩(界面/算法可用,不驱动硬件)。
|
||||
# ---------------------------------------------------------------------------
|
||||
set(ARIS_ROOT "" CACHE PATH "ARIS SDK 根目录(包含 include/ 与 lib/ 导入库)")
|
||||
set(ARIS_INCLUDE_DIR "" CACHE PATH "ARIS 头文件目录(通常无需手动指定)")
|
||||
option(CAVITY_ALLOW_ARIS_STUB
|
||||
"找不到与目标位数/工具链匹配的 ARIS SDK 时,使用内置兼容桩(仅界面/算法,无光谱仪硬件)"
|
||||
ON)
|
||||
|
||||
if(CAVITY_MSVC)
|
||||
set(_aris_lib_names libaris.lib aris.lib)
|
||||
else()
|
||||
set(_aris_lib_names libaris.dll.a)
|
||||
endif()
|
||||
|
||||
if(NOT ARIS_ROOT)
|
||||
set(_aris_candidates
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/aris-${CAVITY_TOOLCHAIN}"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/aris-mingw${CAVITY_BITS}"
|
||||
"$ENV{ARIS_ROOT}"
|
||||
"G:/qt/Qt/mylib/aris_0411"
|
||||
"D:/aris/aris")
|
||||
|
||||
foreach(_cand IN LISTS _aris_candidates)
|
||||
if(NOT _cand)
|
||||
continue()
|
||||
endif()
|
||||
if(NOT EXISTS "${_cand}/include/ziolinkprotocol.h")
|
||||
continue()
|
||||
endif()
|
||||
|
||||
set(_aris_lib_found "")
|
||||
foreach(_libname IN LISTS _aris_lib_names)
|
||||
if(EXISTS "${_cand}/lib/${_libname}")
|
||||
set(_aris_lib_found "${_cand}/lib/${_libname}")
|
||||
break()
|
||||
endif()
|
||||
endforeach()
|
||||
if(NOT _aris_lib_found)
|
||||
continue()
|
||||
endif()
|
||||
|
||||
if(EXISTS "${_cand}/bin/libaris.dll")
|
||||
cavity_pe_machine("${_cand}/bin/libaris.dll" _aris_machine)
|
||||
if(_aris_machine AND NOT _aris_machine STREQUAL "${CAVITY_TAG}")
|
||||
continue()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
set(ARIS_ROOT "${_cand}")
|
||||
break()
|
||||
endforeach()
|
||||
endif()
|
||||
|
||||
# 头文件目录(兼容桩也需要,头文件本身与位数无关)
|
||||
if(NOT ARIS_INCLUDE_DIR)
|
||||
set(_aris_include_candidates
|
||||
"${ARIS_ROOT}/include"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/aris/include"
|
||||
"G:/qt/Qt/mylib/aris_0411/include"
|
||||
"D:/aris/aris/include")
|
||||
|
||||
foreach(_cand IN LISTS _aris_include_candidates)
|
||||
if(_cand AND EXISTS "${_cand}/ziolinkprotocol.h")
|
||||
set(ARIS_INCLUDE_DIR "${_cand}")
|
||||
break()
|
||||
endif()
|
||||
endforeach()
|
||||
endif()
|
||||
|
||||
if(NOT ARIS_INCLUDE_DIR)
|
||||
message(FATAL_ERROR
|
||||
"找不到 ARIS 头文件(ziolinkprotocol.h / spectrometerdevice.h)。\n"
|
||||
"请用 -DARIS_INCLUDE_DIR=<目录> 或 -DARIS_ROOT=<SDK 根目录> 指定。")
|
||||
endif()
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# ARIS 桥接 DLL(MSVC 专用)
|
||||
# MSVC 无法直接链接厂商的 GCC 版 libaris.dll,改用 third_party/aris-bridge
|
||||
# 里由 MinGW 编译的 aris_bridge.dll(纯 C 接口,运行时 LoadLibrary)。
|
||||
# ---------------------------------------------------------------------------
|
||||
option(CAVITY_USE_ARIS_BRIDGE "MSVC 构建通过 aris_bridge.dll 使用真实 ARIS SDK" ON)
|
||||
set(CAVITY_ARIS_BRIDGE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party/aris-bridge"
|
||||
CACHE PATH "ARIS 桥接目录(bin/aris_bridge.dll、vendor/libaris.dll、runtime/*.dll)")
|
||||
|
||||
set(CAVITY_ARIS_BRIDGE OFF)
|
||||
if(CAVITY_MSVC AND CAVITY_USE_ARIS_BRIDGE AND EXISTS "${CAVITY_ARIS_BRIDGE_DIR}/bin/aris_bridge.dll")
|
||||
set(CAVITY_ARIS_BRIDGE ON)
|
||||
endif()
|
||||
|
||||
set(CAVITY_USING_ARIS_STUB OFF)
|
||||
if(CAVITY_ARIS_BRIDGE)
|
||||
# 桥接模式:不需要 SDK 导入库,也不用兼容桩
|
||||
message(STATUS "ARIS SDK: 通过桥接 DLL 使用真实 SDK(${CAVITY_ARIS_BRIDGE_DIR}/bin/aris_bridge.dll)")
|
||||
elseif(ARIS_ROOT)
|
||||
set(ARIS_LIB "")
|
||||
foreach(_libname IN LISTS _aris_lib_names)
|
||||
if(EXISTS "${ARIS_ROOT}/lib/${_libname}")
|
||||
set(ARIS_LIB "${ARIS_ROOT}/lib/${_libname}")
|
||||
break()
|
||||
endif()
|
||||
endforeach()
|
||||
set(ARIS_RUNTIME_DLL "${ARIS_ROOT}/bin/libaris.dll")
|
||||
if(NOT EXISTS "${ARIS_RUNTIME_DLL}")
|
||||
set(ARIS_RUNTIME_DLL "")
|
||||
endif()
|
||||
message(STATUS "ARIS SDK: ${ARIS_ROOT}")
|
||||
elseif(CAVITY_ALLOW_ARIS_STUB)
|
||||
set(CAVITY_USING_ARIS_STUB ON)
|
||||
if(CAVITY_MSVC)
|
||||
message(WARNING
|
||||
"未找到 MSVC 版 ARIS SDK,将使用内置兼容桩(aris_stub.dll)。\n"
|
||||
" 厂商 SDK 只有 MinGW 编译的 libaris.dll(GCC 名字修饰的 C++ 符号),\n"
|
||||
" MSVC 无法链接;因此 MSVC 目标下无法驱动光谱仪硬件。\n"
|
||||
" 界面、寻峰/元素分析算法、串口、数据库功能均正常。\n"
|
||||
" 确实需要硬件时,请用 MinGW 工具链构建(见 README-构建运行.md)。")
|
||||
else()
|
||||
message(WARNING
|
||||
"未找到 ${CAVITY_BITS} 位 ARIS SDK(libaris.dll.a),将使用内置兼容桩。\n"
|
||||
" 程序可以正常编译、启动和调试界面,但不会检测到光谱仪硬件。\n"
|
||||
" 若拿到 ${CAVITY_BITS} 位 ARIS SDK,请用 -DARIS_ROOT=<SDK 根目录> 指定,\n"
|
||||
" 或把 SDK 放到 third_party/aris-mingw${CAVITY_BITS}/。")
|
||||
endif()
|
||||
else()
|
||||
message(FATAL_ERROR
|
||||
"未找到匹配的 ARIS SDK 导入库(${_aris_lib_names})。\n"
|
||||
"请用 -DARIS_ROOT=<SDK 根目录> 指定,或允许使用兼容桩:-DCAVITY_ALLOW_ARIS_STUB=ON")
|
||||
endif()
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# libusb(ARIS 头文件依赖 libusb-1.0/libusb.h;运行库在部署阶段复制)
|
||||
# ---------------------------------------------------------------------------
|
||||
set(LIBUSB_INCLUDE_DIR "" CACHE PATH "libusb 头文件目录(其下应有 libusb-1.0/libusb.h)")
|
||||
if(NOT LIBUSB_INCLUDE_DIR)
|
||||
set(_libusb_candidates
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/libusb-1.0/include"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/libusb/include"
|
||||
"G:/qt/Qt/mylib/libusb_x64-windows/include"
|
||||
"D:/aris/libusb_x64-windows/include")
|
||||
foreach(_cand IN LISTS _libusb_candidates)
|
||||
if(_cand AND EXISTS "${_cand}/libusb-1.0/libusb.h")
|
||||
set(LIBUSB_INCLUDE_DIR "${_cand}")
|
||||
break()
|
||||
endif()
|
||||
endforeach()
|
||||
endif()
|
||||
|
||||
if(NOT LIBUSB_INCLUDE_DIR)
|
||||
message(FATAL_ERROR
|
||||
"找不到 libusb 头文件(libusb-1.0/libusb.h),ARIS 头文件需要它。\n"
|
||||
"请用 -DLIBUSB_INCLUDE_DIR=<目录> 指定。")
|
||||
endif()
|
||||
|
||||
# libusb 运行库:只在链接真实 ARIS SDK 时需要
|
||||
set(LIBUSB_RUNTIME_DLL "")
|
||||
if(NOT CAVITY_USING_ARIS_STUB)
|
||||
set(_libusb_dll_candidates
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/Libs/libusb-1.0.dll"
|
||||
"G:/qt/Qt/mylib/libusb_x64-windows/bin/libusb-1.0.dll")
|
||||
foreach(_cand IN LISTS _libusb_dll_candidates)
|
||||
if(_cand AND EXISTS "${_cand}")
|
||||
cavity_pe_machine("${_cand}" _libusb_machine)
|
||||
if(NOT _libusb_machine OR _libusb_machine STREQUAL "${CAVITY_TAG}")
|
||||
set(LIBUSB_RUNTIME_DLL "${_cand}")
|
||||
break()
|
||||
endif()
|
||||
endif()
|
||||
endforeach()
|
||||
endif()
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 程序与依赖
|
||||
# ---------------------------------------------------------------------------
|
||||
add_executable(CavityLIBS
|
||||
main.cpp
|
||||
mainwindow.cpp
|
||||
mainwindow.h
|
||||
mainwindow.ui
|
||||
probe_tube.cpp
|
||||
probe_tube.h
|
||||
computer.cpp
|
||||
computer.h
|
||||
Spectrometer/devicemanager.cpp
|
||||
Spectrometer/devicemanager.h
|
||||
Spectrometer/algorithmanalysis.cpp
|
||||
Spectrometer/algorithmanalysis.h
|
||||
Spectrometer/elementanalysis.cpp
|
||||
Spectrometer/elementanalysis.h
|
||||
Spectrometer/specpeakservice.cpp
|
||||
Spectrometer/specpeakservice.h
|
||||
Spectrometer/spectrumalgorithm.cpp
|
||||
Spectrometer/spectrumalgorithm.h
|
||||
Spectrometer/spectrometer.cpp
|
||||
Spectrometer/spectrometer.h
|
||||
Spectrometer/arisbridge.h
|
||||
Spectrometer/arisbridge.cpp
|
||||
Public/util.cpp
|
||||
Public/util.h
|
||||
Public/crc16util.cpp
|
||||
Public/crc16util.h
|
||||
Public/type.h
|
||||
Public/avaspec.h
|
||||
Public/sqliteutil.cpp
|
||||
Public/sqliteutil.h
|
||||
PeakSearching/peakfinder.cpp
|
||||
PeakSearching/peakfinder.h
|
||||
Public/clickable_line_edit.h
|
||||
)
|
||||
|
||||
target_include_directories(CavityLIBS PRIVATE
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}"
|
||||
"${QWT_INCLUDE_DIR}"
|
||||
"${ARIS_INCLUDE_DIR}"
|
||||
"${LIBUSB_INCLUDE_DIR}")
|
||||
|
||||
# GUI 程序:不要系统自动附带的控制台窗口。
|
||||
# main.cpp 会在需要时自己创建一个“运行日志”控制台(独立窗口,实时显示指令/应答),
|
||||
# 而从 Qt Creator / Visual Studio 或命令行重定向运行时则沿用已有的管道/控制台。
|
||||
set_target_properties(CavityLIBS PROPERTIES WIN32_EXECUTABLE TRUE)
|
||||
|
||||
# Qwt 以 DLL 方式构建,使用方需要 QWT_DLL
|
||||
target_compile_definitions(CavityLIBS PRIVATE QWT_DLL)
|
||||
|
||||
# MSVC:/utf-8 + Qt 5.15 兼容层(MinGW 下不做任何事)
|
||||
cavity_apply_toolchain_settings(CavityLIBS)
|
||||
|
||||
# 桥接模式:定义 CAVITY_USE_ARIS_BRIDGE,并加入纯 C 头文件目录
|
||||
if(CAVITY_ARIS_BRIDGE)
|
||||
target_compile_definitions(CavityLIBS PRIVATE CAVITY_USE_ARIS_BRIDGE)
|
||||
target_include_directories(CavityLIBS PRIVATE "${CAVITY_ARIS_BRIDGE_DIR}/src")
|
||||
endif()
|
||||
|
||||
target_link_libraries(CavityLIBS PRIVATE
|
||||
Qt5::Core
|
||||
Qt5::Widgets
|
||||
Qt5::Gui
|
||||
Qt5::SerialPort
|
||||
Qt5::Sql
|
||||
"${QWT_LIB}")
|
||||
|
||||
if(CAVITY_USING_ARIS_STUB)
|
||||
# 用真实的 ARIS 头文件编译一个空实现的替身 DLL,接口与正式 SDK 一致
|
||||
add_library(aris_stub SHARED third_party/aris-stub/aris_stub.cpp)
|
||||
target_include_directories(aris_stub PRIVATE
|
||||
"${ARIS_INCLUDE_DIR}"
|
||||
"${LIBUSB_INCLUDE_DIR}")
|
||||
target_compile_definitions(aris_stub PRIVATE ARIS_LIBRARY)
|
||||
cavity_apply_toolchain_settings(aris_stub)
|
||||
set_target_properties(aris_stub PROPERTIES
|
||||
PREFIX ""
|
||||
OUTPUT_NAME "aris_stub")
|
||||
target_link_libraries(CavityLIBS PRIVATE aris_stub)
|
||||
elseif(ARIS_LIB)
|
||||
target_link_libraries(CavityLIBS PRIVATE "${ARIS_LIB}")
|
||||
endif()
|
||||
|
||||
if(CAVITY_MSVC)
|
||||
# devicemanager.cpp 直接包含 winsock2.h(GCC 会自动带上,MSVC 需要显式链接)
|
||||
target_link_libraries(CavityLIBS PRIVATE ws2_32)
|
||||
endif()
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 运行环境部署:把 DLL、Qt 插件和运行数据复制到 <build>/bin
|
||||
# ---------------------------------------------------------------------------
|
||||
option(CAVITY_DEPLOY_RUNTIME "构建后自动部署运行库与数据文件" ON)
|
||||
if(CAVITY_DEPLOY_RUNTIME)
|
||||
# ---- 第三方库 -------------------------------------------------------
|
||||
set(_deploy_dlls "${QWT_RUNTIME_DLL}")
|
||||
if(CAVITY_ARIS_BRIDGE)
|
||||
# 桥接 DLL + 厂商 libaris.dll + libusb + MinGW 运行库(GCC 8.3)
|
||||
list(APPEND _deploy_dlls "${CAVITY_ARIS_BRIDGE_DIR}/bin/aris_bridge.dll")
|
||||
if(EXISTS "${CAVITY_ARIS_BRIDGE_DIR}/vendor/libaris.dll")
|
||||
list(APPEND _deploy_dlls "${CAVITY_ARIS_BRIDGE_DIR}/vendor/libaris.dll")
|
||||
endif()
|
||||
if(EXISTS "${CAVITY_ARIS_BRIDGE_DIR}/vendor/libusb-1.0.dll")
|
||||
list(APPEND _deploy_dlls "${CAVITY_ARIS_BRIDGE_DIR}/vendor/libusb-1.0.dll")
|
||||
endif()
|
||||
file(GLOB _aris_bridge_runtime "${CAVITY_ARIS_BRIDGE_DIR}/runtime/*.dll")
|
||||
list(APPEND _deploy_dlls ${_aris_bridge_runtime})
|
||||
elseif(CAVITY_USING_ARIS_STUB)
|
||||
list(APPEND _deploy_dlls "$<TARGET_FILE:aris_stub>")
|
||||
else()
|
||||
if(ARIS_RUNTIME_DLL)
|
||||
list(APPEND _deploy_dlls "${ARIS_RUNTIME_DLL}")
|
||||
endif()
|
||||
if(LIBUSB_RUNTIME_DLL)
|
||||
list(APPEND _deploy_dlls "${LIBUSB_RUNTIME_DLL}")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# ---- Qt 运行库 -------------------------------------------------------
|
||||
# Debug 配置要用带 d 后缀的 DLL(MSVC 下 Debug/Release CRT 不能混用),
|
||||
# 与 Qwt 的 qwt.dll / qwtd.dll 选择保持一致。
|
||||
foreach(_name Qt5Core Qt5Gui Qt5Widgets Qt5SerialPort Qt5Sql Qt5OpenGL Qt5Svg)
|
||||
set(_dll_release "${_qt_bin_dir}/${_name}.dll")
|
||||
set(_dll_debug "${_qt_bin_dir}/${_name}d.dll")
|
||||
if(EXISTS "${_dll_release}" AND EXISTS "${_dll_debug}")
|
||||
list(APPEND _deploy_dlls "$<IF:$<CONFIG:Debug>,${_dll_debug},${_dll_release}>")
|
||||
elseif(EXISTS "${_dll_release}")
|
||||
list(APPEND _deploy_dlls "${_dll_release}")
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
# ANGLE / D3D 运行库(没有 OpenGL 驱动时的回退方案),存在才复制
|
||||
foreach(_name libEGL libGLESv2 d3dcompiler_47)
|
||||
if(EXISTS "${_qt_bin_dir}/${_name}.dll")
|
||||
list(APPEND _deploy_dlls "${_qt_bin_dir}/${_name}.dll")
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
# ---- MSVC 运行库(vcruntime140 / msvcp140)--------------------------
|
||||
# 目标机没装 VC++ 运行库时,缺这几个 DLL 会直接 0xC0000135 启动失败
|
||||
if(CAVITY_MSVC)
|
||||
# 可能的 Redist 根:环境变量(VS 开发者命令行里才有)或从 cl.exe 路径推算
|
||||
set(_vc_redist_roots "")
|
||||
if(DEFINED ENV{VCToolsRedistDir})
|
||||
file(TO_CMAKE_PATH "$ENV{VCToolsRedistDir}" _vc_redist_root)
|
||||
list(APPEND _vc_redist_roots "${_vc_redist_root}")
|
||||
endif()
|
||||
|
||||
# <VS>/VC/Tools/MSVC/<ver>/bin/Hostx64/x64/cl.exe
|
||||
# -> 上溯到 <VS>/VC/Tools,再上一级是 <VS>/VC
|
||||
get_filename_component(_msvc_dir "${CMAKE_CXX_COMPILER}" DIRECTORY)
|
||||
foreach(_up RANGE 1 4)
|
||||
get_filename_component(_msvc_dir "${_msvc_dir}" DIRECTORY)
|
||||
endforeach()
|
||||
get_filename_component(_vc_dir "${_msvc_dir}" DIRECTORY)
|
||||
list(APPEND _vc_redist_roots "${_vc_dir}/Redist/MSVC")
|
||||
|
||||
# 在 Redist 根下找 x64/Microsoft.VC*.CRT(里面才是 vcruntime140*.dll)
|
||||
set(_vc_redist_dir "")
|
||||
foreach(_root IN LISTS _vc_redist_roots)
|
||||
if(NOT _root)
|
||||
continue()
|
||||
endif()
|
||||
if(EXISTS "${_root}/vcruntime140.dll")
|
||||
set(_vc_redist_dir "${_root}")
|
||||
break()
|
||||
endif()
|
||||
file(GLOB _vc_redist_candidates "${_root}/x64/Microsoft.VC*.CRT")
|
||||
if(NOT _vc_redist_candidates)
|
||||
file(GLOB _vc_redist_candidates "${_root}/*/x64/Microsoft.VC*.CRT")
|
||||
endif()
|
||||
if(_vc_redist_candidates)
|
||||
list(SORT _vc_redist_candidates ORDER DESCENDING)
|
||||
list(GET _vc_redist_candidates 0 _vc_redist_dir)
|
||||
break()
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
if(_vc_redist_dir)
|
||||
foreach(_name vcruntime140 vcruntime140_1 msvcp140)
|
||||
if(EXISTS "${_vc_redist_dir}/${_name}.dll")
|
||||
list(APPEND _deploy_dlls "${_vc_redist_dir}/${_name}.dll")
|
||||
endif()
|
||||
endforeach()
|
||||
message(STATUS "MSVC 运行库: ${_vc_redist_dir}")
|
||||
else()
|
||||
message(STATUS "MSVC 运行库: 未找到 Redist 目录(目标机需自行安装 VC++ 运行库)")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
foreach(_dll IN LISTS _deploy_dlls)
|
||||
add_custom_command(TARGET CavityLIBS POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
"${_dll}" "$<TARGET_FILE_DIR:CavityLIBS>"
|
||||
COMMENT "部署运行库: ${_dll}")
|
||||
endforeach()
|
||||
|
||||
# ---- Qt 插件 ---------------------------------------------------------
|
||||
get_filename_component(_qt_plugins_dir "${_qt_bin_dir}/../plugins" ABSOLUTE)
|
||||
foreach(_plugin platforms/qwindows
|
||||
sqldrivers/qsqlite
|
||||
styles/qwindowsvistastyle
|
||||
imageformats/qico
|
||||
imageformats/qsvg)
|
||||
get_filename_component(_plugin_dir "${_plugin}" DIRECTORY)
|
||||
get_filename_component(_plugin_name "${_plugin}" NAME)
|
||||
set(_plugin_release "${_qt_plugins_dir}/${_plugin}.dll")
|
||||
set(_plugin_debug "${_qt_plugins_dir}/${_plugin}d.dll")
|
||||
if(EXISTS "${_plugin_release}" AND EXISTS "${_plugin_debug}")
|
||||
set(_plugin_file "$<IF:$<CONFIG:Debug>,${_plugin_debug},${_plugin_release}>")
|
||||
elseif(EXISTS "${_plugin_release}")
|
||||
set(_plugin_file "${_plugin_release}")
|
||||
else()
|
||||
continue()
|
||||
endif()
|
||||
add_custom_command(TARGET CavityLIBS POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E make_directory
|
||||
"$<TARGET_FILE_DIR:CavityLIBS>/${_plugin_dir}"
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
"${_plugin_file}"
|
||||
"$<TARGET_FILE_DIR:CavityLIBS>/${_plugin_dir}/${_plugin_name}$<IF:$<CONFIG:Debug>,d,>.dll"
|
||||
COMMENT "部署 Qt 插件: ${_plugin}")
|
||||
endforeach()
|
||||
|
||||
# ---- MinGW 运行库(只有 GCC 构建才需要)-----------------------------
|
||||
if(NOT CAVITY_MSVC)
|
||||
get_filename_component(_mingw_bin "${CMAKE_CXX_COMPILER}" DIRECTORY)
|
||||
foreach(_name libgcc_s_seh-1 libgcc_s_dw2-1 libstdc++-6 libwinpthread-1)
|
||||
if(EXISTS "${_mingw_bin}/${_name}.dll")
|
||||
list(APPEND _deploy_dlls "${_mingw_bin}/${_name}.dll")
|
||||
add_custom_command(TARGET CavityLIBS POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
"${_mingw_bin}/${_name}.dll"
|
||||
"$<TARGET_FILE_DIR:CavityLIBS>"
|
||||
COMMENT "部署 MinGW 运行库: ${_name}.dll")
|
||||
endif()
|
||||
endforeach()
|
||||
endif()
|
||||
|
||||
# ---- 运行数据 -------------------------------------------------------
|
||||
# 程序按工作目录/程序目录读取这些文件(代码里还有 "<exe>/../Data" 的写法),
|
||||
# 因此 bin 下放一份,<build> 根目录也放一份。
|
||||
foreach(_res Data SQL wavelength_calibrated_files)
|
||||
add_custom_command(TARGET CavityLIBS POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/${_res}"
|
||||
"$<TARGET_FILE_DIR:CavityLIBS>/${_res}"
|
||||
COMMENT "部署数据目录: ${_res}")
|
||||
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${_res}")
|
||||
add_custom_command(TARGET CavityLIBS POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/${_res}"
|
||||
"${CMAKE_BINARY_DIR}/${_res}"
|
||||
COMMENT "部署 ../${_res}")
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
add_custom_command(TARGET CavityLIBS POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/app_icon.ico"
|
||||
"$<TARGET_FILE_DIR:CavityLIBS>"
|
||||
COMMENT "部署图标 app_icon.ico")
|
||||
|
||||
# 打包到目标机时用的启动脚本(放在 exe 同目录,双击即可)
|
||||
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/scripts/Run-CavityLIBS.bat")
|
||||
add_custom_command(TARGET CavityLIBS POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/scripts/Run-CavityLIBS.bat"
|
||||
"$<TARGET_FILE_DIR:CavityLIBS>"
|
||||
COMMENT "部署启动脚本 Run-CavityLIBS.bat")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Visual Studio 里直接按 F5 调试:把工作目录设到 bin(Data / SQL 都在那),
|
||||
# 并把 Qt、qwt 所在目录加进 PATH,避免“找不到 DLL”。
|
||||
# ---------------------------------------------------------------------------
|
||||
if(CAVITY_MSVC)
|
||||
set_target_properties(CavityLIBS PROPERTIES
|
||||
VS_DEBUGGER_WORKING_DIRECTORY "$<TARGET_FILE_DIR:CavityLIBS>"
|
||||
VS_DEBUGGER_ENVIRONMENT "PATH=${_qt_bin_dir};${QWT_LIB_DIR};%PATH%")
|
||||
endif()
|
||||
Reference in New Issue
Block a user