CMake Reference Documentation — CMake 3.25.0-rc3 Documentation
Cmake本身提供了很多变量来判断当前的系统环境,如下
cmake-variables(7) — CMake 3.25.0-rc3 Documentation
- message("CMAKE_HOST_SYSTEM_NAME: ${CMAKE_HOST_SYSTEM_NAME} ")
- message("CMAKE_HOST_SYSTEM: ${CMAKE_HOST_SYSTEM} ")
-
- message("CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME} ")
- message("CMAKE_SYSTEM: ${CMAKE_SYSTEM} ")
-
- message("CMAKE_HOST_UNIX: ${CMAKE_HOST_UNIX} ")
- message("CMAKE_HOST_WIN32: ${CMAKE_HOST_WIN32} ")
- # 在 Linux 平台
- CMAKE_HOST_SYSTEM_NAME = "Linux"
- CMAKE_HOST_UNIX = 1
- CMAKE_HOST_WIN32 = 空
- UNIX = 1
- WIN32 = 空
-
- # 在 Windows 平台
- CMAKE_HOST_SYSTEM_NAME = "Windows"
- CMAKE_HOST_UNIX = 空
- CMAKE_HOST_WIN32 = 1
- UNIX = 空
- WIN32 = 1
- if(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux")
-
- message("current platform: Linux ")
-
- elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Windows")
-
- message("current platform: Windows ")
-
- else()
-
- message("current platform: unkonw ")
-
- endif()
- if(CMAKE_HOST_UNIX)
-
- message("current platform: Linux ")
-
- elseif(CMAKE_HOST_WIN32)
-
- message("current platform: Windows ")
-
- else()
-
- message("current platform: unkonw ")
-
- endif()
- if(UNIX)
-
- message("current platform: Linux ")
-
- elseif(WIN32)
-
- message("current platform: Windows ")
-
- else()
-
- message("current platform: unkonw ")
-
- endif()
- if(CMAKE_CL_64)
- set(CURRENT_PLATFORM "x64")
- message(STATUS "Current Platform is ${CURRENT_PLATFORM}")
- else(CMAKE_CL_64)
- set(CURRENT_PLATFORM "x86")
- message(STATUS "Current Platform is ${CURRENT_PLATFORM}")
- endif(CMAKE_CL_64)
-
- if(CMAKE_CROSSCOMPILING)
- message(STATUS "Cross Comliling!!!, ARM")
- set(CURRENT_SYSTEM "Arm")
- else()
- message(STATUS "No Cross Comliling!!!")
-
- #根据不同平台给CURRENT_SYSTEM命名
- if(CMAKE_SYSTEM_NAME MATCHES "Linux")
- set(CURRENT_SYSTEM "Linux")
- elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
- set(CURRENT_SYSTEM "Windows")
- elseif(CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
- set(CURRENT_SYSTEM "FreeBSD")
- endif()
- endif()