Files
sunhpc-soft/cmake/Common.cmake
2026-06-14 23:45:57 +08:00

68 lines
1.9 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.
#
# 全局变量、路径、日志、工具函数
#
# 日志宏
macro(DEPLOY_MSG MSG)
message(STATUS "[DEPLOY] ${MSG}")
endmacro()
# 校验 root 权限(系统包/系统目录需要)
function(CHECK_ROOT)
execute_process(
COMMAND id -u
OUTPUT_VARIABLE CURRENT_UID
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT CURRENT_UID STREQUAL "0")
message(FATAL_ERROR "Please run cmake/make with root privileges!")
endif()
endfunction()
# ===================== 编译器自动检测&安装 =====================
function(CheckAndInstallCompiler)
DEPLOY_MSG("Checking GCC/G++ compiler...")
# 查找 gcc g++
find_program(GCC_EXEC gcc)
find_program(GXX_EXEC g++)
if(NOT GCC_EXEC OR NOT GXX_EXEC)
DEPLOY_MSG("GCC/G++ not found, start installing via dnf...")
CHECK_ROOT()
# dnf 安装 gcc gcc-c++
execute_process(
COMMAND dnf install -y gcc gcc-c++
RESULT_VARIABLE COMP_RET
OUTPUT_VARIABLE COMP_OUT
ERROR_VARIABLE COMP_ERR
)
if(NOT COMP_RET EQUAL 0)
message(FATAL_ERROR "Failed to install GCC/G++: ${COMP_ERR}")
endif()
DEPLOY_MSG("GCC/G++ installed successfully")
# 重新刷新程序查找让CMake识别新安装的编译器
find_program(GCC_EXEC gcc REQUIRED)
find_program(GXX_EXEC g++ REQUIRED)
else()
DEPLOY_MSG("Found compiler: ${GCC_EXEC}, ${GXX_EXEC}")
endif()
# 赋值给全局 CMake 编译器变量
set(CMAKE_C_COMPILER ${GCC_EXEC} CACHE FILEPATH "C compiler" FORCE)
set(CMAKE_CXX_COMPILER ${GXX_EXEC} CACHE FILEPATH "CXX compiler" FORCE)
endfunction()
# 统一安装路径拼接
function(SET_INSTALL_PATH NAME VER OUT_PATH)
set(${OUT_PATH} "${INSTALL_ROOT}/${NAME}/${VER}" PARENT_SCOPE)
endfunction()
# 基础编译参数
set(CMAKE_C_FLAGS "-O2" CACHE STRING "C flags")
set(CMAKE_CXX_FLAGS "-O2" CACHE STRING "C++ flags")