From c72ef9cb47ad94f2e21ecb2aeac19244c248c65b Mon Sep 17 00:00:00 2001 From: DevWiki Date: Thu, 26 Sep 2024 20:06:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DeviceManager/CMakeLists.txt | 53 +++++++++++-------- DeviceManager/build.bat | 33 ++++++++++++ WinDevice/CMakeLists.txt | 94 +++++++++++++++------------------ WinDevice/build.bat | 33 ++++++++++++ WinDeviceDemo/WinDeviceDemo.cpp | 22 -------- WinDeviceDemo/WinDeviceDemo.h | 8 --- 6 files changed, 141 insertions(+), 102 deletions(-) create mode 100644 DeviceManager/build.bat create mode 100644 WinDevice/build.bat delete mode 100644 WinDeviceDemo/WinDeviceDemo.cpp delete mode 100644 WinDeviceDemo/WinDeviceDemo.h diff --git a/DeviceManager/CMakeLists.txt b/DeviceManager/CMakeLists.txt index 035dcb0..1f0d045 100644 --- a/DeviceManager/CMakeLists.txt +++ b/DeviceManager/CMakeLists.txt @@ -49,12 +49,20 @@ endif() target_link_libraries(DeviceManager PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) -set(WinDevice_INCLUDE_DIR "../WinDevice/output/include/") -set(WinDevice_THIRD_INCLUDE_DIR "../WinDevice/output/third_lib/") -if (CMAKE_BUILD_TYPE STREQUAL "Debug") - set(WinDevice_LIB_DIR "../WinDevice/output/debug/") +if(CMAKE_SIZEOF_VOID_P EQUAL 8) + set(ARCH_DIR "x64") else() - set(WinDevice_LIB_DIR "../WinDevice/output/release/") + set(ARCH_DIR "x86") +endif() + +set(WinDevice_OUTPUT ${CMAKE_SOURCE_DIR}/../WinDevice/output) +# 获取架构信息 +set(WinDevice_INCLUDE_DIR "${WinDevice_OUTPUT}/include/") +set(WinDevice_THIRD_INCLUDE_DIR "${WinDevice_OUTPUT}/third_lib/") +if (CMAKE_BUILD_TYPE STREQUAL "Debug") + set(WinDevice_LIB_DIR "${WinDevice_OUTPUT}/${ARCH_DIR}/debug/") +else() + set(WinDevice_LIB_DIR "${WinDevice_OUTPUT}/${ARCH_DIR}/release/") endif() # 使用 find_library 查找动态库文件 find_library(WinDevice_LIB @@ -66,23 +74,24 @@ include_directories(${WinDevice_INCLUDE_DIR} ${WinDevice_THIRD_INCLUDE_DIR}) # 将找到的库链接到你的目标 target_link_libraries(DeviceManager PRIVATE ${WinDevice_LIB}) -# if (CMAKE_BUILD_TYPE STREQUAL "Debug") -# # Debug 模式下,添加 WinDevice 项目作为子项目 -# add_subdirectory(../WinDevice WinDevice) -# # 链接WinDevice项目 -# target_include_directories(DeviceManager PRIVATE -# ../WinDevice/src -# include/third_lib -# ) -# target_link_libraries(DeviceManager PRIVATE WinDevice) -# else() -# # Release 模式下,链接 WinDevice 生成的库文件 -# target_link_libraries(DeviceManager PRIVATE WinDevice) -# # 在 Release 模式下,添加 WinDevice 的头文件路径 -# target_include_directories(DeviceManager PRIVATE -# ../WinDevice/lib -# ) -# endif() + if (CMAKE_BUILD_TYPE STREQUAL "Debug") + # Debug 模式下,添加 WinDevice 项目作为子项目 + add_subdirectory(../WinDevice WinDevice) + # 链接WinDevice项目 + target_include_directories(DeviceManager PRIVATE + ../WinDevice/src + include/third_lib + ) + target_link_libraries(DeviceManager PRIVATE WinDevice) + else() + # Release 模式下,链接 WinDevice 生成的库文件 + target_link_libraries(DeviceManager PRIVATE WinDevice) + # 在 Release 模式下,添加 WinDevice 的头文件路径 + target_include_directories(DeviceManager PRIVATE + ${WinDevice_INCLUDE_DIR} + ${WinDevice_THIRD_INCLUDE_DIR} + ) + endif() set_target_properties(DeviceManager PROPERTIES diff --git a/DeviceManager/build.bat b/DeviceManager/build.bat new file mode 100644 index 0000000..006536e --- /dev/null +++ b/DeviceManager/build.bat @@ -0,0 +1,33 @@ +@echo on +chcp 65001 +setlocal enabledelayedexpansion + +set arch=x64 +set build_type=Debug + +rem 如果参数1不为空,设置架构 +if not "%~1" == "" ( + set arch=%~1 +) + +rem 如果参数2不为空,设置构建类型 +if not "%~2" == "" ( + set build_type=%~2 +) + +rem 删除旧的构建目录 +if exist "build" ( + rmdir "build" /s /q +) + +rem 创建新的构建目录并进入 +mkdir "build" +cd build + +rem 运行 CMake 配置命令 +cmake .. -G "Visual Studio 17 2022" -A %arch% -DCMAKE_BUILD_TYPE=%build_type% + +rem 执行构建 +cmake --build . --config %build_type% + +endlocal \ No newline at end of file diff --git a/WinDevice/CMakeLists.txt b/WinDevice/CMakeLists.txt index 79b4f18..f089d7e 100644 --- a/WinDevice/CMakeLists.txt +++ b/WinDevice/CMakeLists.txt @@ -15,10 +15,10 @@ set(CMAKE_GENERATOR_TOOLSET "v143") if (NOT DEFINED CMAKE_GENERATOR_PLATFORM) if (CMAKE_SIZEOF_VOID_P EQUAL 8) set(CMAKE_GENERATOR_PLATFORM x64) - else() + else () set(CMAKE_GENERATOR_PLATFORM x86) - endif() -endif() + endif () +endif () # 设置C++标准 set(CMAKE_CXX_STANDARD 11) @@ -37,8 +37,8 @@ if (ENABLE_UTF8_SOURCE) elseif (CMAKE_COMPILER_IS_GNUCXX) # 设置MinGW编译器的UTF-8源码编码选项 add_compile_options("-fexec-charset=UTF-8" "-finput-charset=UTF-8") - endif() -endif() + endif () +endif () # DirectX目录 set(Windows_Kits_DIR "C:\\Program Files (x86)\\Windows Kits\\10") @@ -46,64 +46,63 @@ set(Windows_Kits_Version "10.0.19041.0") set(Windows_Kits_UM_DIR "${Windows_Kits_DIR}\\Include\\${Windows_Kits_Version}\\um") set(Windows_Kits_SHARED_DIR "${Windows_Kits_DIR}\\Include\\${Windows_Kits_Version}\\shared") -if(CMAKE_SIZEOF_VOID_P EQUAL 8) +if (CMAKE_SIZEOF_VOID_P EQUAL 8) # 64-bit 架构 set(CMAKE_LIBRARY_ARCHITECTURE x64) set(DirectX_SDK_LIB_DIR "${Windows_Kits_DIR}\\Lib\\${Windows_Kits_Version}\\um\\x64") -else() +else () # 32-bit 架构 set(CMAKE_LIBRARY_ARCHITECTURE x86) set(DirectX_SDK_LIB_DIR "${Windows_Kits_DIR}\\Lib\\${Windows_Kits_Version}\\um\\x86") -endif() +endif () # 获取所有 DirectX 相关的库文件 file(GLOB DirectX_LIBS - "${DirectX_SDK_LIB_DIR}/d3d9.lib" - "${DirectX_SDK_LIB_DIR}/d3d10.lib" - "${DirectX_SDK_LIB_DIR}/d3d11.lib" - "${DirectX_SDK_LIB_DIR}/d3d12.lib" - "${DirectX_SDK_LIB_DIR}/dxgi.lib" - "${DirectX_SDK_LIB_DIR}/ddraw.lib" -) + "${DirectX_SDK_LIB_DIR}/d3d9.lib" + "${DirectX_SDK_LIB_DIR}/d3d10.lib" + "${DirectX_SDK_LIB_DIR}/d3d11.lib" + "${DirectX_SDK_LIB_DIR}/d3d12.lib" + "${DirectX_SDK_LIB_DIR}/dxgi.lib" + "${DirectX_SDK_LIB_DIR}/ddraw.lib" + ) # 使用通配符自动列举源文件 file(GLOB_RECURSE SOURCE_FILES "./src/*") set(Third_Include_DIR "include/third_lib/") # 添加功能模块的源文件 add_library(WinDevice SHARED - ${SOURCE_FILES} -) + ${SOURCE_FILES} + ) # 设置WinDevice库的包含目录 target_include_directories(WinDevice - PUBLIC - $ - PRIVATE - ${Windows_Kits_UM_DIR} - ${Windows_Kits_SHARED_DIR} - ${Third_Include_DIR} -) + PUBLIC + $ + PRIVATE + ${Windows_Kits_UM_DIR} + ${Windows_Kits_SHARED_DIR} + ${Third_Include_DIR} + ) # 链接 d3d 的 lib target_link_libraries(WinDevice PRIVATE ${DirectX_LIBS}) # 创建 output 目录 -set(OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/output) +set(OUTPUT_DIR ${CMAKE_SOURCE_DIR}/output) # 获取架构信息 -if(CMAKE_SIZEOF_VOID_P EQUAL 8) +if (CMAKE_SIZEOF_VOID_P EQUAL 8) set(ARCH_DIR "x64") -else() +else () set(ARCH_DIR "x86") -endif() +endif () # 创建架构特定的输出目录 file(MAKE_DIRECTORY ${OUTPUT_DIR}/${ARCH_DIR}) set_target_properties(WinDevice PROPERTIES - ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIR}/${ARCH_DIR}/debug" - LIBRARY_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIR}/${ARCH_DIR}/debug" - RUNTIME_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIR}/${ARCH_DIR}/debug" + ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIR}/${ARCH_DIR}/debug" + LIBRARY_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIR}/${ARCH_DIR}/debug" + RUNTIME_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIR}/${ARCH_DIR}/debug" - ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIR}/${ARCH_DIR}/release" - LIBRARY_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIR}/${ARCH_DIR}/release" - RUNTIME_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIR}/${ARCH_DIR}/release" - ) + ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIR}/${ARCH_DIR}/release" + LIBRARY_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIR}/${ARCH_DIR}/release" + RUNTIME_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIR}/${ARCH_DIR}/release") if (CMAKE_BUILD_TYPE STREQUAL "Debug") # Debug 模式下的配置 @@ -111,33 +110,28 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug") set(LIBRARY_OUTPUT_PATH "${OUTPUT_DIR}/debug") # 添加测试 add_executable(WinDeviceTest "src/main.cpp" ${SOURCE_FILES}) - target_include_directories(WinDeviceTest - PUBLIC $ - PRIVATE ${Third_Include_DIR} ${Windows_Kits_UM_DIR} ${Windows_Kits_SHARED_DIR}) + target_include_directories(WinDeviceTest + PUBLIC $ + PRIVATE ${Third_Include_DIR} ${Windows_Kits_UM_DIR} ${Windows_Kits_SHARED_DIR}) target_link_libraries(WinDeviceTest PRIVATE ${DirectX_LIBS}) set_target_properties(WinDeviceTest PROPERTIES - ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIR}/debug" - LIBRARY_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIR}/debug" - RUNTIME_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIR}/debug" - - ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIR}/release" - LIBRARY_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIR}/release" - RUNTIME_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIR}/release" - ) -else() + ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIR}/${ARCH_DIR}/debug" + LIBRARY_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIR}/${ARCH_DIR}/debug" + RUNTIME_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIR}/${ARCH_DIR}/debug") +else () # Release 模式下的配置 set(LIBRARY_OUTPUT_PATH "${OUTPUT_DIR}/release") -endif() +endif () # 拷贝头文件到构建目录的 include 文件夹下 file(GLOB_RECURSE EXPORTED_HEADERS ${CMAKE_SOURCE_DIR}/src/*.h) -foreach(HEADER ${EXPORTED_HEADERS}) +foreach (HEADER ${EXPORTED_HEADERS}) get_filename_component(HEADER_DIR ${HEADER} DIRECTORY) get_filename_component(HEADER_NAME ${HEADER} NAME) string(REPLACE "${CMAKE_SOURCE_DIR}/src/" "" HEADER_RELATIVE_PATH ${HEADER_DIR}) file(COPY ${HEADER} DESTINATION ${OUTPUT_DIR}/include/${HEADER_RELATIVE_PATH}) -endforeach() +endforeach () # 创建 output/third_lib 目录 set(OUTPUT_THIRD_LIB_DIR ${OUTPUT_DIR}/third_lib) diff --git a/WinDevice/build.bat b/WinDevice/build.bat new file mode 100644 index 0000000..006536e --- /dev/null +++ b/WinDevice/build.bat @@ -0,0 +1,33 @@ +@echo on +chcp 65001 +setlocal enabledelayedexpansion + +set arch=x64 +set build_type=Debug + +rem 如果参数1不为空,设置架构 +if not "%~1" == "" ( + set arch=%~1 +) + +rem 如果参数2不为空,设置构建类型 +if not "%~2" == "" ( + set build_type=%~2 +) + +rem 删除旧的构建目录 +if exist "build" ( + rmdir "build" /s /q +) + +rem 创建新的构建目录并进入 +mkdir "build" +cd build + +rem 运行 CMake 配置命令 +cmake .. -G "Visual Studio 17 2022" -A %arch% -DCMAKE_BUILD_TYPE=%build_type% + +rem 执行构建 +cmake --build . --config %build_type% + +endlocal \ No newline at end of file diff --git a/WinDeviceDemo/WinDeviceDemo.cpp b/WinDeviceDemo/WinDeviceDemo.cpp deleted file mode 100644 index a69949b..0000000 --- a/WinDeviceDemo/WinDeviceDemo.cpp +++ /dev/null @@ -1,22 +0,0 @@ -// WinDeviceDemo.cpp: 定义应用程序的入口点。 -// - -#include "WinDeviceDemo.h" -#include "Video/ScreenManager.h" - -using namespace std; - -int main() -{ - HRESULT hr = S_OK; - hr = CoInitialize(nullptr); - if (FAILED(hr)) { - return 0; - } - cout << "Hello CMake." << endl; - - ScreenManager screenManager; - screenManager.UpdateDisplayInfo(); - CoUninitialize(); - return 0; -} diff --git a/WinDeviceDemo/WinDeviceDemo.h b/WinDeviceDemo/WinDeviceDemo.h deleted file mode 100644 index 9c11dee..0000000 --- a/WinDeviceDemo/WinDeviceDemo.h +++ /dev/null @@ -1,8 +0,0 @@ -// WinDeviceDemo.h: 标准系统包含文件的包含文件 -// 或项目特定的包含文件。 - -#pragma once - -#include - -// TODO: 在此处引用程序需要的其他标头。