153 lines
4.9 KiB
CMake
153 lines
4.9 KiB
CMake
#[=======================================================================[
|
||
平台匹配检查函数
|
||
参数:
|
||
platform: 平台标识符 (windows|linux|mac|mobile|desktop)
|
||
is_match: 输出变量,表示当前平台是否匹配
|
||
#]=======================================================================]
|
||
function(is_current_platform platform is_match)
|
||
# 设置默认值为TRUE(用于未知平台)
|
||
set(matches FALSE)
|
||
|
||
if(platform STREQUAL "windows")
|
||
if(WIN32)
|
||
set(matches TRUE)
|
||
endif()
|
||
elseif(platform STREQUAL "linux")
|
||
if(UNIX AND NOT APPLE)
|
||
set(matches TRUE)
|
||
endif()
|
||
elseif(platform STREQUAL "mac")
|
||
if(APPLE AND NOT IOS)
|
||
set(matches TRUE)
|
||
endif()
|
||
elseif(platform STREQUAL "ios")
|
||
if(IOS)
|
||
set(matches TRUE)
|
||
endif()
|
||
elseif (platform STREQUAL "android")
|
||
if(ANDROID)
|
||
set(matches TRUE)
|
||
endif()
|
||
elseif(platform STREQUAL "mobile")
|
||
if(ANDROID OR IOS)
|
||
set(matches TRUE)
|
||
endif()
|
||
elseif(platform STREQUAL "desktop")
|
||
if(WIN32 OR (UNIX AND NOT APPLE) OR (APPLE AND NOT IOS))
|
||
set(matches TRUE)
|
||
endif()
|
||
else()
|
||
# 未知平台标识,默认匹配
|
||
set(matches TRUE)
|
||
endif()
|
||
|
||
set(${is_match} ${matches} PARENT_SCOPE)
|
||
endfunction()
|
||
|
||
#[=======================================================================[
|
||
主文件检索函数
|
||
参数:
|
||
path: 要检索的根目录路径
|
||
extension: 文件扩展名列表
|
||
out_files: 输出变量名,将包含筛选后的文件列表
|
||
#]=======================================================================]
|
||
function(retrieve_files_custom path extension out_files)
|
||
# 1. 参数验证
|
||
if(NOT IS_DIRECTORY "${path}")
|
||
message(WARNING "错误:目录 '${path}' 不存在")
|
||
return()
|
||
endif()
|
||
|
||
message(STATUS "正在检索目录: ${path}")
|
||
|
||
# 2. 构建文件匹配模式
|
||
set(file_patterns "")
|
||
foreach(ext IN LISTS extension)
|
||
list(APPEND file_patterns "${path}/*.${ext}")
|
||
endforeach()
|
||
|
||
# 3. 递归查找所有匹配的文件
|
||
file(GLOB_RECURSE found_files
|
||
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
|
||
CONFIGURE_DEPENDS ${file_patterns}
|
||
)
|
||
|
||
# 4. 处理找到的文件
|
||
set(filtered_files "")
|
||
foreach(current_file IN LISTS found_files)
|
||
# 4.1 获取文件所在目录
|
||
get_filename_component(file_dir "${current_file}" DIRECTORY)
|
||
string(REPLACE "/" ";" dir_components "${file_dir}")
|
||
|
||
# 4.2 检查平台兼容性
|
||
set(should_skip_file FALSE)
|
||
set(found_platform_dir FALSE)
|
||
|
||
foreach(dir_name IN LISTS dir_components)
|
||
# 检查是否是平台相关目录
|
||
if(dir_name MATCHES "^(windows|linux|mac|ios|android|mobile|desktop)$")
|
||
set(found_platform_dir TRUE)
|
||
is_current_platform(${dir_name} platform_matches)
|
||
if(NOT platform_matches)
|
||
set(should_skip_file TRUE)
|
||
break()
|
||
endif()
|
||
endif()
|
||
endforeach()
|
||
|
||
# 如果文件需要跳过,继续处理下一个文件
|
||
if(should_skip_file)
|
||
continue()
|
||
endif()
|
||
|
||
# 4.3 添加符合条件的文件
|
||
list(APPEND filtered_files "${current_file}")
|
||
|
||
# 4.4 设置IDE文件分组
|
||
# 计算相对路径作为分组名称
|
||
get_filename_component(root_abs_path "${path}" ABSOLUTE)
|
||
get_filename_component(file_dir_abs_path "${file_dir}" ABSOLUTE)
|
||
file(RELATIVE_PATH group_path "${root_abs_path}" "${file_dir_abs_path}")
|
||
|
||
# 处理根目录的特殊情况
|
||
if(group_path STREQUAL ".")
|
||
set(group_name "")
|
||
else()
|
||
string(REPLACE "/" "\\" group_name "${group_path}")
|
||
endif()
|
||
|
||
# 创建IDE分组
|
||
source_group("${group_name}" FILES "${current_file}")
|
||
endforeach()
|
||
|
||
# 5. 设置输出变量
|
||
set(${out_files} ${filtered_files} PARENT_SCOPE)
|
||
endfunction()
|
||
|
||
#[=======================================================================[
|
||
便捷封装函数
|
||
自动处理常见文件扩展名,针对不同平台添加特定文件类型
|
||
#]=======================================================================]
|
||
function(retrieve_files path out_files)
|
||
# 设置基础文件类型
|
||
set(file_extensions
|
||
"h" # 头文件
|
||
"hpp" # C++头文件
|
||
"ini" # 配置文件
|
||
"cpp" # C++源文件
|
||
"c" # C源文件
|
||
"ixx" # C++20模块文件
|
||
)
|
||
|
||
# 针对Mac平台添加额外文件类型
|
||
if(APPLE)
|
||
list(APPEND file_extensions "mm") # Objective-C++源文件
|
||
endif()
|
||
|
||
# 执行文件检索
|
||
set(temp_files "")
|
||
retrieve_files_custom(${path} "${file_extensions}" temp_files)
|
||
|
||
# 合并结果到输出变量
|
||
set(${out_files} ${${out_files}} ${temp_files} PARENT_SCOPE)
|
||
endfunction() |