CMake中的cmake_host_system_information命令用于查询各种主机系统信息,其格式如下:
cmake_host_system_information(RESULT <variable> QUERY <key> ...) # 1
cmake_host_system_information(RESULT <variable>
QUERY WINDOWS_REGISTRY <key> [VALUE_NAMES|SUBKEYS|VALUE <name>]
[VIEW (64|32|64_32|32_64|HOST|TARGET|BOTH)]
[SEPARATOR <separator>]
[ERROR_VARIABLE <result>]) # 2
1. 查询运行cmake的主机系统的系统信息。可以提供一个或多个<key>来选择要查询的信息。查询值列表存储在<variable>中。
<key>可以是以下值:
(1).NUMBER_OF_LOGICAL_CORES: 逻辑内核数
(2).NUMBER_OF_PHYSICAL_CORES: 物理内核数
(3).HOSTNAME: 主机名
(4).FQDN: 完全限定域名
(5).TOTAL_VIRTUAL_MEMORY: 总虚拟内存数(MB)
(6).AVAILABLE_VIRTUAL_MEMORY: 可用虚拟内存数(MB)
(7).TOTAL_PHYSICAL_MEMORY: 总物理内存数(MB)
(8).AVAILABLE_PHYSICAL_MEMORY: 可用物理内存数(MB)
(9).IS_64BIT: 如果处理器为64位,则为1
(10).HAS_FPU: 如果处理器具有浮点单元(floating point unit),则为1
(11).HAS_MMX: 如果处理器支持MMX指令,则为1
(12).HAS_MMX_PLUS: 如果处理器支持扩展MMX指令,则为1
(13).HAS_SSE: 如果处理器支持SSE指令,则为1
(14).HAS_SSE2: 如果处理器支持SSE2指令,则为1
(15).HAS_SSE_FP: 如果处理器支持SSE FP指令,则为1
(16).HAS_SSE_MMX: 如果处理器支持SSE MMX指令,则为1
(17).HAS_AMD_3DNOW: 如果处理器支持AMD 3DNow指令,则为1
(18).HAS_AMD_3DNOW_PLUS: 如果处理器支持AMD 3DNow+指令,则为1
(19).HAS_IA64: 如IA64处理器模拟x86,则为1
(20).HAS_SERIAL_NUMBER: 如果处理器有序列号,则为1
(21).PROCESSOR_SERIAL_NUMBER: 处理器序列号
(22).PROCESSOR_NAME: 处理器名称
(23).PROCESSOR_DESCRIPTION: 完整处理器描述
(24).OS_NAME: CMake运行的操作系统名称,可参考CMAKE_HOST_SYSTEM_NAME
(25).OS_RELEASE: 操作系统子类型(OS sub-type),如Windows Professional
(26).OS_VERSION: 操作系统内建版本ID
(27).OS_PLATFORM: 操作系统平台
(28).DISTRIB_INFO: 读取/etc/os-release文件并将读取的变量列表赋值给<variable>
(29).DISTRIB_<name>: 如果<name>变量存在于/etc/os-release文件中则获取
如果未找到/etc/os-release文件,该命令尝试通过回退脚本收集操作系统标识(the command tries to gather OS identification via fallback scripts).回退脚本可以使用各种特定于发行版的文件来收集操作系统标识数据,并将其映射到os-release变量中(the fallback script can use various distribution-specific files to collect OS identification data and map it into os-release variables)。
cmake_host_system_information(RESULT info QUERY NUMBER_OF_LOGICAL_CORES NUMBER_OF_PHYSICAL_CORES HOSTNAME)
message(STATUS "${info}")
cmake_host_system_information(RESULT info QUERY TOTAL_VIRTUAL_MEMORY AVAILABLE_VIRTUAL_MEMORY TOTAL_PHYSICAL_MEMORY AVAILABLE_PHYSICAL_MEMORY)
message(STATUS "${info}")
cmake_host_system_information(RESULT info QUERY IS_64BIT HAS_FPU HAS_SSE2 HAS_AMD_3DNOW)
message(STATUS "${info}")
cmake_host_system_information(RESULT info QUERY HAS_SERIAL_NUMBER PROCESSOR_SERIAL_NUMBER PROCESSOR_NAME PROCESSOR_DESCRIPTION)
message(STATUS "${info}")
cmake_host_system_information(RESULT info QUERY OS_NAME OS_RELEASE OS_VERSION OS_PLATFORM)
message(STATUS "${info}")
cmake_host_system_information(RESULT PRETTY_NAME QUERY DISTRIB_PRETTY_NAME)
message(STATUS "${PRETTY_NAME}")
cmake_host_system_information(RESULT DISTRO QUERY DISTRIB_INFO)
foreach(VAR IN LISTS DISTRO)
message(STATUS "${VAR}=`${${VAR}}`")
endforeach()
2. 查询windows注册表:对本地计算机注册表子项(registry subkey)执行查询操作.
注意:查询Windows以外的任何其它平台的注册表将始终返回一个空字符串,并在使用子选项ERROR_VARIABLE指定的变量中设置错误消息。
执行测试代码需要多个文件:
build.sh内容如下
#! /bin/bash
# supported input parameters(cmake commands)
params=(function macro cmake_parse_arguments \
find_library find_path find_file find_program find_package \
cmake_policy cmake_minimum_required project include \
string list set foreach message option if while return \
math file configure_file \
include_directories add_executable add_library link_libraries target_link_libraries install \
target_sources add_custom_command add_custom_target \
add_subdirectory aux_source_directory \
set_property set_target_properties define_property \
add_definitions target_compile_definitions target_compile_features \
add_compile_options target_include_directories link_directories \
add_link_options target_precompile_headers cmake_host_system_information)
usage()
{
echo "Error: $0 needs to have an input parameter"
echo "supported input parameters:"
for param in ${params[@]}; do
echo " $0 ${param}"
done
exit -1
}
if [ $# != 1 ]; then
usage
fi
flag=0
for param in ${params[@]}; do
if [ $1 == ${param} ]; then
flag=1
break
fi
done
if [ ${flag} == 0 ]; then
echo "Error: parameter \"$1\" is not supported"
usage
exit -1
fi
if [[ ! -d "build" ]]; then
mkdir build
cd build
else
cd build
fi
echo "==== test $1 ===="
# test_set.cmake: cmake -DTEST_CMAKE_FEATURE=$1 --log-level=verbose ..
# test_option.cmake: cmake -DTEST_CMAKE_FEATURE=$1 -DBUILD_PYTORCH=ON ..
cmake -DTEST_CMAKE_FEATURE=$1 ..
# It can be executed directly on the terminal, no need to execute build.sh, for example: cmake -P test_set.cmake
make
# make install # only used in cmake files with install command
主CMakeLists.txt内容如下:
cmake_minimum_required(VERSION 3.22)
project(cmake_feature_usage)
message("#### current cmake version: ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.${CMAKE_PATCH_VERSION}")
include(test_${TEST_CMAKE_FEATURE}.cmake)
message("==== test finish ====")
test_cmake_host_system_information.cmake内容为上面的所有测试代码段
可能的执行结果如下图所示:
GitHub:https://github.com/fengbingchun/Linux_Code_Test