Files
mirai/cmake/compiler_options.cmake

108 lines
4.3 KiB
CMake
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# ================================================================================================
# MIRAI Framework - 编译器选项配置模块
# 描述: 配置编译器特定选项、警告级别和优化设置
# ================================================================================================
# ================================================================================================
# 配置编译器选项
# ================================================================================================
function(configure_compiler_options)
# 使用生成器表达式为多配置生成器如Visual Studio正确配置选项
if(MSVC)
add_compile_options(
/wd5054 # 禁用 C5054: 枚举之间的 & 运算符弃用警告
/wd4324 # 禁用 C4324: 由于对齐说明符,结构被填充警告
)
# Debug配置特定设置
add_compile_options(
$<$<CONFIG:Debug>:/Zi> # 调试信息
$<$<CONFIG:Debug>:/Od> # 禁用优化
$<$<CONFIG:Debug>:/RTC1> # 运行时检查
$<$<CONFIG:Debug>:/W4> # 严格警告
)
add_compile_definitions(
$<$<CONFIG:Debug>:MIRAI_DEBUG_BUILD>
)
# Release配置特定设置
add_compile_options(
$<$<CONFIG:Release>:/O2> # 最大优化
$<$<CONFIG:Release>:/Ob2> # 内联展开
$<$<CONFIG:Release>:/Zi> # 调试信息用于调试Release版本
)
add_compile_definitions(
$<$<CONFIG:Release>:MIRAI_RELEASE_BUILD>
$<$<CONFIG:Release>:NDEBUG>
)
# RelWithDebInfo配置
add_compile_options(
$<$<CONFIG:RelWithDebInfo>:/O2>
$<$<CONFIG:RelWithDebInfo>:/Ob1>
$<$<CONFIG:RelWithDebInfo>:/Zi>
)
add_compile_definitions(
$<$<CONFIG:RelWithDebInfo>:MIRAI_RELEASE_BUILD>
$<$<CONFIG:RelWithDebInfo>:NDEBUG>
)
# MinSizeRel配置
add_compile_options(
$<$<CONFIG:MinSizeRel>:/O1>
$<$<CONFIG:MinSizeRel>:/Ob1>
$<$<CONFIG:MinSizeRel>:/Zi>
)
add_compile_definitions(
$<$<CONFIG:MinSizeRel>:MIRAI_RELEASE_BUILD>
$<$<CONFIG:MinSizeRel>:NDEBUG>
)
add_compile_definitions(
_ENABLE_EXTENDED_ALIGNED_STORAGE
)
message(STATUS "MSVC编译器选项已配置支持多配置生成器")
else()
# 非MSVC编译器的传统配置
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_definitions(MIRAI_DEBUG_BUILD)
add_compile_options(-g -O0 -fno-omit-frame-pointer)
add_compile_options(-Wall -Wextra -Wpedantic)
message(STATUS "Debug模式: 启用调试符号,禁用优化,启用严格警告")
else()
add_compile_definitions(MIRAI_RELEASE_BUILD NDEBUG)
add_compile_options(-O3 -DNDEBUG -march=native)
message(STATUS "Release模式: 启用最高级别优化")
endif()
# 启用更好的诊断信息
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
add_compile_options(-fdiagnostics-color=always)
add_compile_options(-ftemplate-backtrace-limit=0)
message(STATUS "启用彩色诊断和完整模板回溯")
endif()
endif()
if (MSVC)
add_compile_definitions(MIRAI_MSVC=1)
add_compile_definitions(MIRAI_GCC=0)
add_compile_definitions(MIRAI_CLANG=0)
elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
add_compile_definitions(MIRAI_MSVC=0)
add_compile_definitions(MIRAI_GCC=1)
add_compile_definitions(MIRAI_CLANG=0)
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_definitions(MIRAI_MSVC=0)
add_compile_definitions(MIRAI_GCC=0)
add_compile_definitions(MIRAI_CLANG=1)
endif()
endfunction()
# ================================================================================================
# 应用编译器配置
# ================================================================================================
function(apply_compiler_configuration)
configure_compiler_options()
message(STATUS "编译器配置完成")
endfunction()