2024-09-26 15:01:18 +08:00
|
|
|
|
message(STATUS "CMAKE_SIZEOF_VOID_P: ${CMAKE_SIZEOF_VOID_P}")
|
|
|
|
|
message(STATUS "CMAKE_LIBRARY_ARCHITECTURE: ${CMAKE_LIBRARY_ARCHITECTURE}")
|
|
|
|
|
|
2023-10-10 16:25:43 +08:00
|
|
|
|
# 设置CMake的最低版本要求
|
2023-10-09 20:38:38 +08:00
|
|
|
|
cmake_minimum_required(VERSION 3.12)
|
|
|
|
|
|
2023-10-10 16:25:43 +08:00
|
|
|
|
# 设置项目名称和版本,以及编程语言
|
2023-10-10 11:54:29 +08:00
|
|
|
|
project(WinDevice VERSION 0.1 LANGUAGES CXX)
|
2023-10-09 20:38:38 +08:00
|
|
|
|
|
2023-10-24 20:25:03 +08:00
|
|
|
|
# 允许 Windows 导出所有符号
|
|
|
|
|
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
|
2024-09-26 15:01:18 +08:00
|
|
|
|
# 设置Windows相关配置
|
|
|
|
|
set(CMAKE_GENERATOR_TOOLSET "v142")
|
2023-10-24 20:25:03 +08:00
|
|
|
|
|
2024-09-26 15:40:34 +08:00
|
|
|
|
if (NOT DEFINED CMAKE_GENERATOR_PLATFORM)
|
|
|
|
|
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
|
|
|
|
|
set(CMAKE_GENERATOR_PLATFORM x64)
|
|
|
|
|
else()
|
|
|
|
|
set(CMAKE_GENERATOR_PLATFORM x86)
|
|
|
|
|
endif()
|
|
|
|
|
endif()
|
|
|
|
|
|
2023-10-10 16:25:43 +08:00
|
|
|
|
# 设置C++标准
|
2023-10-10 11:54:29 +08:00
|
|
|
|
set(CMAKE_CXX_STANDARD 11)
|
|
|
|
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|
|
|
|
|
2023-10-10 16:25:43 +08:00
|
|
|
|
# 添加源代码文件编码为UTF-8的选项,可选择启用或禁用
|
|
|
|
|
option(ENABLE_UTF8_SOURCE "Enable UTF-8 source code encoding" ON)
|
|
|
|
|
|
|
|
|
|
# 如果启用了UTF-8源码编码选项
|
|
|
|
|
if (ENABLE_UTF8_SOURCE)
|
|
|
|
|
# 如果是MSVC编译器
|
|
|
|
|
if (MSVC)
|
|
|
|
|
# 设置MSVC编译器的UTF-8源码编码选项
|
|
|
|
|
add_compile_options("/source-charset:utf-8")
|
|
|
|
|
# 如果是MinGW编译器
|
|
|
|
|
elseif (CMAKE_COMPILER_IS_GNUCXX)
|
|
|
|
|
# 设置MinGW编译器的UTF-8源码编码选项
|
|
|
|
|
add_compile_options("-fexec-charset=UTF-8" "-finput-charset=UTF-8")
|
|
|
|
|
endif()
|
|
|
|
|
endif()
|
2023-10-10 11:54:29 +08:00
|
|
|
|
|
2023-12-19 19:00:44 +08:00
|
|
|
|
# DirectX目录
|
|
|
|
|
set(Windows_Kits_DIR "C:\\Program Files (x86)\\Windows Kits\\10")
|
|
|
|
|
set(Windows_Kits_Version "10.0.19041.0")
|
2024-09-26 15:01:18 +08:00
|
|
|
|
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")
|
2023-12-19 19:00:44 +08:00
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
# 32-bit 架构
|
|
|
|
|
set(CMAKE_LIBRARY_ARCHITECTURE x86)
|
|
|
|
|
set(DirectX_SDK_LIB_DIR "${Windows_Kits_DIR}\\Lib\\${Windows_Kits_Version}\\um\\x86")
|
|
|
|
|
endif()
|
2023-12-22 19:05:40 +08:00
|
|
|
|
# 获取所有 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"
|
|
|
|
|
)
|
2023-12-19 19:00:44 +08:00
|
|
|
|
|
2023-12-22 19:05:40 +08:00
|
|
|
|
# 使用通配符自动列举源文件
|
|
|
|
|
file(GLOB_RECURSE SOURCE_FILES "./src/*")
|
|
|
|
|
set(Third_Include_DIR "include/third_lib/")
|
|
|
|
|
# 添加功能模块的源文件
|
|
|
|
|
add_library(WinDevice SHARED
|
|
|
|
|
${SOURCE_FILES}
|
|
|
|
|
)
|
2023-12-19 19:00:44 +08:00
|
|
|
|
# 设置WinDevice库的包含目录
|
|
|
|
|
target_include_directories(WinDevice
|
|
|
|
|
PUBLIC
|
|
|
|
|
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/src>
|
|
|
|
|
PRIVATE
|
2024-09-26 15:01:18 +08:00
|
|
|
|
${Windows_Kits_UM_DIR}
|
|
|
|
|
${Windows_Kits_SHARED_DIR}
|
2023-12-22 19:05:40 +08:00
|
|
|
|
${Third_Include_DIR}
|
2023-12-19 19:00:44 +08:00
|
|
|
|
)
|
|
|
|
|
# 链接 d3d 的 lib
|
2023-12-22 19:05:40 +08:00
|
|
|
|
target_link_libraries(WinDevice PRIVATE ${DirectX_LIBS})
|
2023-12-19 19:00:44 +08:00
|
|
|
|
|
|
|
|
|
# 创建 output 目录
|
|
|
|
|
set(OUTPUT_DIR ${CMAKE_SOURCE_DIR}/output)
|
|
|
|
|
file(MAKE_DIRECTORY ${OUTPUT_DIR})
|
2024-09-26 15:40:34 +08:00
|
|
|
|
set_target_properties(WinDevice 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"
|
|
|
|
|
)
|
2023-12-19 19:00:44 +08:00
|
|
|
|
|
2023-10-30 16:44:02 +08:00
|
|
|
|
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
|
|
|
|
|
# Debug 模式下的配置
|
|
|
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG")
|
2023-12-19 19:00:44 +08:00
|
|
|
|
set(LIBRARY_OUTPUT_PATH "${OUTPUT_DIR}/debug")
|
2023-12-22 19:05:40 +08:00
|
|
|
|
# 添加测试
|
|
|
|
|
add_executable(WinDeviceTest "src/main.cpp" ${SOURCE_FILES})
|
|
|
|
|
target_include_directories(WinDeviceTest
|
|
|
|
|
PUBLIC $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/src>
|
2024-09-26 15:01:18 +08:00
|
|
|
|
PRIVATE ${Third_Include_DIR} ${Windows_Kits_UM_DIR} ${Windows_Kits_SHARED_DIR})
|
2023-12-22 19:05:40 +08:00
|
|
|
|
target_link_libraries(WinDeviceTest PRIVATE ${DirectX_LIBS})
|
2023-10-30 16:44:02 +08:00
|
|
|
|
else()
|
|
|
|
|
# Release 模式下的配置
|
2023-12-19 19:00:44 +08:00
|
|
|
|
set(LIBRARY_OUTPUT_PATH "${OUTPUT_DIR}/release")
|
2023-10-30 16:44:02 +08:00
|
|
|
|
endif()
|
|
|
|
|
|
2023-10-10 16:25:43 +08:00
|
|
|
|
|
2023-12-19 19:00:44 +08:00
|
|
|
|
# 拷贝头文件到构建目录的 include 文件夹下
|
|
|
|
|
file(GLOB_RECURSE EXPORTED_HEADERS ${CMAKE_SOURCE_DIR}/src/*.h)
|
|
|
|
|
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()
|
2023-10-10 16:25:43 +08:00
|
|
|
|
|
2023-12-19 19:00:44 +08:00
|
|
|
|
# 创建 output/third_lib 目录
|
|
|
|
|
set(OUTPUT_THIRD_LIB_DIR ${OUTPUT_DIR}/third_lib)
|
|
|
|
|
file(MAKE_DIRECTORY ${OUTPUT_THIRD_LIB_DIR})
|
2023-10-24 20:25:03 +08:00
|
|
|
|
|
2023-12-19 19:00:44 +08:00
|
|
|
|
# 拷贝 include/third_lib 目录下的文件到 output/third_lib 目录中
|
|
|
|
|
file(COPY ${CMAKE_SOURCE_DIR}/include/third_lib/ DESTINATION ${OUTPUT_THIRD_LIB_DIR})
|
2023-10-24 20:25:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
target_compile_definitions(WinDevice PRIVATE -DWINDEVICE_EXPORTS)
|