CMake I 检测处理器体系结构

目录

一、检测处理器体系结构

1.CMake命令

(1)CMAKE_SIZEOF_VOID_P

(2)CMAKE_HOST_SYSTEM_PROCESSOR

(3)CMAKE_SYSTEM_PROCESSOR

2.应用

(1)CMakeLists.txt

(2)cpp文件

(3)配置及构建

 二、检测处理器指令集

1.CMake命令

(1)cmake_host_system_information

(2)configure_file

(3)target_sources

(4)target_include_directories

(5)PROJECT_SOURCE_DIR

(6)PROJECT_BINARY_DIR 

2.应用

(1)CMakeLists.txt

(2)cpp文件

(3)config.h.in

(4)配置及构建


一、检测处理器体系结构

1.CMake命令

(1)CMAKE_SIZEOF_VOID_P

        CMAKE_SIZEOF_VOID_P 表示 void* 的大小(例如为 4 或者 8),可以使用其来判断当前构建为 32 位还是 64 位。使用 CMAKE_SIZEOF_VOID_P 是检查当前CPU是否具有32位或64位架构的唯一“真正”可移植的方法。

if(CMAKE_SIZEOF_VOID_P EQUAL 8)
	message(STATUS "Target is 64 bits")
else()
	message(STATUS "Target is 32 bits")
endif()

(2)CMAKE_HOST_SYSTEM_PROCESSOR

        CMake定义了 CMAKE_HOST_SYSTEM_PROCESSOR 变量,以包含当前运行的处理器的名称:

  • x86:32/64 位系统编译,在32位系统上运行
  • x86_amd64:32 系统上编译,在64位系统上运行 
  • amd64:64 系统上编译,在64位系统上运行
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_amd64")
	message(STATUS "x86_64 architecture detected")
elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "AMD64")
	message(STATUS "x64 architecture detected")
endif()

(3)CMAKE_SYSTEM_PROCESSOR

CMAKE_HOST_SYSTEM_PROCESSOR包含当前运行的CPU在CMake的名称;

CMAKE_SYSTEM_PROCESSOR包含当前正在为其构建的CPU的名称。

2.应用

(1)CMakeLists.txt

cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

project(SetArch LANGUAGES CXX)

add_executable(helloworldexe helloworld.cpp)

if(CMAKE_SIZEOF_VOID_P EQUAL 8)
	target_compile_definitions(helloworldexe PUBLIC "IS_64_BIT_ARCH")
	message(STATUS "Target is 64 bits")
else()
	target_compile_definitions(helloworldexe PUBLIC "IS_32_BIT_ARCH")
	message(STATUS "Target is 32 bits")
endif()

target_compile_definitions(helloworldexe PUBLIC "ARCHITECTURE=${CMAKE_HOST_SYSTEM_PROCESSOR}")
message(STATUS "CMAKE_HOST_SYSTEM_PROCESSOR=${CMAKE_HOST_SYSTEM_PROCESSOR}")
message(STATUS "CMAKE_SYSTEM_PROCESSOR=${CMAKE_SYSTEM_PROCESSOR}")
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "i386")
	message(STATUS "i386 architecture detected")
elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "i686")
	message(STATUS "i686 architecture detected")
elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
	message(STATUS "x86_64 architecture detected")
elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "AMD64")
	message(STATUS "x64 architecture detected")
else()
	message(STATUS "host processor architecture is unknown")
endif()

(2)cpp文件

#include <cstdlib>
#include <iostream>
#include <string>

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)

std::string SayHello()
{
	std::string arch_info(TOSTRING(ARCHITECTURE));
	arch_info+=std::string(" architecture.");
#ifdef IS_32_BIT_ARCH
	return arch_info+=std::string("Compiled on a 32 bit host processor.");
#elif IS_64_BIT_ARCH
	return arch_info+=std::string("Compiled on a 64 bit host processor.");
#else 
	return arch_info+=std::string("Neither 32 nor 64 bit.");
#endif
}

int main()
{
	std::cout<<SayHello()<<std::endl;
	return EXIT_SUCCESS;
}

(3)配置及构建

 二、检测处理器指令集

1.CMake命令

(1)cmake_host_system_information

cmake_host_system_information(RESULT <variable> QUERY <key> ...)

        查询运行cmake的主机系统的系统信息,可以提供一个或多个 key 来选择要查询的信息,查询的值列表存储在 variable 中。key的值参考文章

(2)configure_file

configure_file(<input> <output>
               [NO_SOURCE_PERMISSIONS | USE_SOURCE_PERMISSIONS |
                FILE_PERMISSIONS <permissions>...]
               [COPYONLY] [ESCAPE_QUOTES] [@ONLY]
               [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ])

        将文件复制到另一个位置并修改其内容。修改时要遵循一定的规则:将input文件复制到output文件,并将输入文件内容中的变量,替换引用为@VAR@或${VAR}的变量值。每个变量引用将替换为该变量的当前值,如果未定义该变量,则为空字符串。

        通常情况下,输入文件Input以.h.in为后缀,输出文件output以.h为后缀

        @ONLY:限制替换, 仅仅替换 @VAR@ 变量, 不替换 ${VAR} 变量。

参考文章:

1.configure_file — CMake 3.21.4 Documentation
2.cmake:configure_file的作用_OceanStar的博客-CSDN博客_cmake configure_file

//CMakeLists.txt文件
cmake_host_system_information(RESULT _HOSTNAME QUERY HOSTNAME)
configure_file(config.h.in config.h @ONLY)

//config.h.in文件
#define HOSTNAME @_HOSTNAME@

//生成的config.h
#define HOSTNAME LAPTOP-2L54P572

(3)target_sources

target_sources(<target>
  <INTERFACE|PUBLIC|PRIVATE> [items1...]
  [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])

        指定在生成目标或其依赖项时要使用的源文件。命名必须由 add_executable()或add_library( ) 或add_custom_target( )命令创建。

add_executable(helloworldexe "")
target_sources(helloworldexe PRIVATE helloworld.cpp)

(4)target_include_directories

target_include_directories(<target> [SYSTEM] [AFTER|BEFORE]
  <INTERFACE|PUBLIC|PRIVATE> [items1...]
  [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])

        指定编译给定目标时要使用的包含目录。命名必须由add_executable( )或add_library( )等命令创建。

add_executable(helloworldexe "")
target_sources(helloworldexe PRIVATE helloworld.cpp)
target_include_directories(helloworldexe PRIVATE ${PROJECT_BINARY_DIR})

(5)PROJECT_SOURCE_DIR

        PROJECT_SOURCE_DIR为包含PROJECT()的最近一个CMakeLists.txt文件所在的文件夹。

(6)PROJECT_BINARY_DIR 

        运行cmake命令的目录。建议定义为${PROJECT_SOURCE_DIR}/build下。

2.应用

(1)CMakeLists.txt

cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project (getHostInfo LANGUAGES CXX)

#定义目标可执行文件及其源文件,并包括目录:
add_executable(helloworldexe "")
target_sources(helloworldexe PRIVATE helloworld.cpp)
target_include_directories(helloworldexe PRIVATE ${PROJECT_BINARY_DIR})
#循环查询主机系统的信息,获取一些关键字
foreach(key IN ITEMS 
	NUMBER_OF_LOGICAL_CORES
	NUMBER_OF_PHYSICAL_CORES
	HOSTNAME
	TOTAL_VIRTUAL_MEMORY
	AVAILABLE_VIRTUAL_MEMORY
	TOTAL_PHYSICAL_MEMORY
	AVAILABLE_PHYSICAL_MEMORY
	IS_64BIT
	HAS_FPU
	HAS_MMX
	HAS_MMX_PLUS
	HAS_SSE
	HAS_SSE2
	HAS_SSE_FP
	HAS_SSE_MMX
	HAS_AMD_3DNOW
	HAS_AMD_3DNOW_PLUS
	HAS_IA64
	OS_NAME
	OS_RELEASE
	OS_VERSION
	OS_PLATFORM)
	cmake_host_system_information(RESULT _${key} QUERY ${key})
endforeach()
#配置 config.h
configure_file(config.h.in config.h @ONLY)

(2)cpp文件

#include <iostream>
#include <cstdlib>
#include "config.h"

int main()
{
	std::cout << "Number of logical cores: " << NUMBER_OF_LOGICAL_CORES << std::endl;
	std::cout << "Number of physical cores: " << NUMBER_OF_PHYSICAL_CORES << std::endl;

	std::cout << "Total virtual memory in megabytes: " << TOTAL_VIRTUAL_MEMORY << std::endl;
	std::cout << "Available virtual memory in megabytes: " << AVAILABLE_VIRTUAL_MEMORY << std::endl;
	std::cout << "Total physical memory in megabytes: " << TOTAL_PHYSICAL_MEMORY << std::endl;
	std::cout << "Available physical memory in megabytes: " << AVAILABLE_PHYSICAL_MEMORY << std::endl;

	std::cout << "Processor is 64Bit: " << IS_64BIT << std::endl;
	std::cout << "Processor has floating point unit: " << HAS_FPU << std::endl;
	std::cout << "Processor supports MMX instructions: " << HAS_MMX << std::endl;
	std::cout << "Processor supports Ext. MMX instructions: " << HAS_MMX_PLUS << std::endl;
	std::cout << "Processor supports SSE instructions: " << HAS_SSE << std::endl;
	std::cout << "Processor supports SSE2 instructions: " << HAS_SSE2 << std::endl;
	std::cout << "Processor supports SSE FP instructions: " << HAS_SSE_FP << std::endl;
	std::cout << "Processor supports SSE MMX instructions: " << HAS_SSE_MMX << std::endl;
	std::cout << "Processor supports 3DNow instructions: " << HAS_AMD_3DNOW << std::endl;
	std::cout << "Processor supports 3DNow+ instructions: " << HAS_AMD_3DNOW_PLUS << std::endl;
	std::cout << "IA64 processor emulating x86 : " << HAS_IA64 << std::endl;
	
	std::cout << "OS name: " << OS_NAME << std::endl;
	std::cout << "OS sub-type: " << OS_RELEASE << std::endl;
	std::cout << "OS build ID: " << OS_VERSION << std::endl;
	std::cout << "OS platform: " << OS_PLATFORM << std::endl;
	
	return EXIT_SUCCESS;
}

(3)config.h.in

#pragma once

#define NUMBER_OF_LOGICAL_CORES   	@_NUMBER_OF_LOGICAL_CORES@
#define NUMBER_OF_PHYSICAL_CORES  	@_NUMBER_OF_PHYSICAL_CORES@
#define HOSTNAME 					@_HOSTNAME@
#define TOTAL_VIRTUAL_MEMORY      	@_TOTAL_VIRTUAL_MEMORY@
#define AVAILABLE_VIRTUAL_MEMORY  	@_AVAILABLE_VIRTUAL_MEMORY@
#define TOTAL_PHYSICAL_MEMORY     	@_TOTAL_PHYSICAL_MEMORY@
#define AVAILABLE_PHYSICAL_MEMORY 	@_AVAILABLE_PHYSICAL_MEMORY@
#define IS_64BIT                  	@_IS_64BIT@
#define HAS_FPU                   	@_HAS_FPU@
#define HAS_MMX                   	@_HAS_MMX@
#define HAS_MMX_PLUS              	@_HAS_MMX_PLUS@
#define HAS_SSE                   	@_HAS_SSE@
#define HAS_SSE2                  	@_HAS_SSE2@
#define HAS_SSE_FP              	@_HAS_SSE_FP@
#define HAS_SSE_MMX             	@_HAS_SSE_MMX@
#define HAS_AMD_3DNOW           	@_HAS_AMD_3DNOW@
#define HAS_AMD_3DNOW_PLUS        	@_HAS_AMD_3DNOW_PLUS@
#define HAS_IA64                  	@_HAS_IA64@
#define OS_NAME                  	"@_OS_NAME@"
#define OS_RELEASE               	"@_OS_RELEASE@"
#define OS_VERSION               	"@_OS_VERSION@"
#define OS_PLATFORM              	"@_OS_PLATFORM@"

(4)配置及构建

        生成的config.h文件如下:

#pragma once

#define NUMBER_OF_LOGICAL_CORES   	12
#define NUMBER_OF_PHYSICAL_CORES  	6
#define HOSTNAME 					LAPTOP-2L54P572
#define TOTAL_VIRTUAL_MEMORY      	47011
#define AVAILABLE_VIRTUAL_MEMORY  	34455
#define TOTAL_PHYSICAL_MEMORY     	16291
#define AVAILABLE_PHYSICAL_MEMORY 	8494
#define IS_64BIT                  	1
#define HAS_FPU                   	1
#define HAS_MMX                   	1
#define HAS_MMX_PLUS              	0
#define HAS_SSE                   	1
#define HAS_SSE2                  	1
#define HAS_SSE_FP              	0
#define HAS_SSE_MMX             	0
#define HAS_AMD_3DNOW           	0
#define HAS_AMD_3DNOW_PLUS        	0
#define HAS_IA64                  	0
#define OS_NAME                  	"Windows"
#define OS_RELEASE               	" Personal"
#define OS_VERSION               	" (Build 19043)"
#define OS_PLATFORM              	"AMD64"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烫青菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值