1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
5 # This module is used by the Makefile generator to determine the following variables:
6 # CMAKE_SYSTEM_NAME - on unix this is uname -s, for windows it is Windows
7 # CMAKE_SYSTEM_VERSION - on unix this is uname -r, for windows it is empty
8 # CMAKE_SYSTEM - ${CMAKE_SYSTEM}-${CMAKE_SYSTEM_VERSION}, for windows: ${CMAKE_SYSTEM}
10 # find out on which system cmake runs
12 find_program(CMAKE_UNAME NAMES uname PATHS /bin /usr/bin /usr/local/bin)
14 if(CMAKE_HOST_SYSTEM_NAME STREQUAL "AIX")
15 execute_process(COMMAND ${CMAKE_UNAME} -v
16 OUTPUT_VARIABLE _CMAKE_HOST_SYSTEM_MAJOR_VERSION
17 OUTPUT_STRIP_TRAILING_WHITESPACE
19 execute_process(COMMAND ${CMAKE_UNAME} -r
20 OUTPUT_VARIABLE _CMAKE_HOST_SYSTEM_MINOR_VERSION
21 OUTPUT_STRIP_TRAILING_WHITESPACE
23 set(CMAKE_HOST_SYSTEM_VERSION "${_CMAKE_HOST_SYSTEM_MAJOR_VERSION}.${_CMAKE_HOST_SYSTEM_MINOR_VERSION}")
24 unset(_CMAKE_HOST_SYSTEM_MAJOR_VERSION)
25 unset(_CMAKE_HOST_SYSTEM_MINOR_VERSION)
26 elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Android")
27 execute_process(COMMAND getprop ro.build.version.sdk
28 OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_VERSION
29 OUTPUT_STRIP_TRAILING_WHITESPACE
32 if(NOT DEFINED CMAKE_SYSTEM_VERSION)
33 set(_ANDROID_API_LEVEL_H $ENV{PREFIX}/include/android/api-level.h)
34 set(_ANDROID_API_REGEX "#define __ANDROID_API__ ([0-9]+)")
35 file(READ ${_ANDROID_API_LEVEL_H} _ANDROID_API_LEVEL_H_CONTENT)
36 string(REGEX MATCH ${_ANDROID_API_REGEX} _ANDROID_API_LINE "${_ANDROID_API_LEVEL_H_CONTENT}")
37 string(REGEX REPLACE ${_ANDROID_API_REGEX} "\\1" _ANDROID_API "${_ANDROID_API_LINE}")
39 set(CMAKE_SYSTEM_VERSION "${_ANDROID_API}")
42 unset(_ANDROID_API_LEVEL_H)
43 unset(_ANDROID_API_LEVEL_H_CONTENT)
44 unset(_ANDROID_API_REGEX)
45 unset(_ANDROID_API_LINE)
49 execute_process(COMMAND ${CMAKE_UNAME} -r
50 OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_VERSION
51 OUTPUT_STRIP_TRAILING_WHITESPACE
54 if(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux|CYGWIN.*|MSYS.*|^GNU$|Android")
55 execute_process(COMMAND ${CMAKE_UNAME} -m
56 OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
58 OUTPUT_STRIP_TRAILING_WHITESPACE
60 elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin")
61 # If we are running on Apple Silicon, honor CMAKE_APPLE_SILICON_PROCESSOR.
62 if(DEFINED CMAKE_APPLE_SILICON_PROCESSOR)
63 set(_CMAKE_APPLE_SILICON_PROCESSOR "${CMAKE_APPLE_SILICON_PROCESSOR}")
64 elseif(DEFINED ENV{CMAKE_APPLE_SILICON_PROCESSOR})
65 set(_CMAKE_APPLE_SILICON_PROCESSOR "$ENV{CMAKE_APPLE_SILICON_PROCESSOR}")
67 set(_CMAKE_APPLE_SILICON_PROCESSOR "")
69 if(_CMAKE_APPLE_SILICON_PROCESSOR)
70 if(";${_CMAKE_APPLE_SILICON_PROCESSOR};" MATCHES "^;(arm64|x86_64);$")
71 execute_process(COMMAND sysctl -q hw.optional.arm64
72 OUTPUT_VARIABLE _sysctl_stdout
73 ERROR_VARIABLE _sysctl_stderr
74 RESULT_VARIABLE _sysctl_result
76 if(NOT _sysctl_result EQUAL 0 OR NOT _sysctl_stdout MATCHES "hw.optional.arm64: 1")
77 set(_CMAKE_APPLE_SILICON_PROCESSOR "")
84 if(_CMAKE_APPLE_SILICON_PROCESSOR)
85 set(CMAKE_HOST_SYSTEM_PROCESSOR "${_CMAKE_APPLE_SILICON_PROCESSOR}")
87 execute_process(COMMAND ${CMAKE_UNAME} -m
88 OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
90 OUTPUT_STRIP_TRAILING_WHITESPACE
93 unset(_CMAKE_APPLE_SILICON_PROCESSOR)
94 if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "Power Macintosh")
95 # OS X ppc 'uname -m' may report 'Power Macintosh' instead of 'powerpc'
96 set(CMAKE_HOST_SYSTEM_PROCESSOR "powerpc")
98 elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "OpenBSD")
99 execute_process(COMMAND arch -s
100 OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
102 OUTPUT_STRIP_TRAILING_WHITESPACE
105 execute_process(COMMAND ${CMAKE_UNAME} -p
106 OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
108 OUTPUT_STRIP_TRAILING_WHITESPACE
110 if("${val}" GREATER 0)
111 execute_process(COMMAND ${CMAKE_UNAME} -m
112 OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
114 OUTPUT_STRIP_TRAILING_WHITESPACE
118 # check the return of the last uname -m or -p
119 if("${val}" GREATER 0)
120 set(CMAKE_HOST_SYSTEM_PROCESSOR "unknown")
122 set(CMAKE_UNAME ${CMAKE_UNAME} CACHE INTERNAL "uname command")
123 # processor may have double quote in the name, and that needs to be removed
124 string(REPLACE "\"" "" CMAKE_HOST_SYSTEM_PROCESSOR "${CMAKE_HOST_SYSTEM_PROCESSOR}")
125 string(REPLACE "/" "_" CMAKE_HOST_SYSTEM_PROCESSOR "${CMAKE_HOST_SYSTEM_PROCESSOR}")
129 if (DEFINED ENV{PROCESSOR_ARCHITEW6432})
130 set (CMAKE_HOST_SYSTEM_PROCESSOR "$ENV{PROCESSOR_ARCHITEW6432}")
132 set (CMAKE_HOST_SYSTEM_PROCESSOR "$ENV{PROCESSOR_ARCHITECTURE}")
137 # if a toolchain file is used, the user wants to cross compile.
138 # in this case read the toolchain file and keep the CMAKE_HOST_SYSTEM_*
139 # variables around so they can be used in CMakeLists.txt.
140 # In all other cases, the host and target platform are the same.
141 if(CMAKE_TOOLCHAIN_FILE)
142 # at first try to load it as path relative to the directory from which cmake has been run
143 include("${CMAKE_BINARY_DIR}/${CMAKE_TOOLCHAIN_FILE}" OPTIONAL RESULT_VARIABLE _INCLUDED_TOOLCHAIN_FILE)
144 if(NOT _INCLUDED_TOOLCHAIN_FILE)
145 # if the file isn't found there, check the default locations
146 include("${CMAKE_TOOLCHAIN_FILE}" OPTIONAL RESULT_VARIABLE _INCLUDED_TOOLCHAIN_FILE)
149 if(_INCLUDED_TOOLCHAIN_FILE)
150 set(CMAKE_TOOLCHAIN_FILE "${_INCLUDED_TOOLCHAIN_FILE}" CACHE FILEPATH "The CMake toolchain file" FORCE)
152 message(FATAL_ERROR "Could not find toolchain file: ${CMAKE_TOOLCHAIN_FILE}")
156 if(CMAKE_SYSTEM_NAME)
157 # CMAKE_SYSTEM_NAME was set by a toolchain file or on the command line.
158 # Assume it set CMAKE_SYSTEM_VERSION and CMAKE_SYSTEM_PROCESSOR too.
159 if(NOT DEFINED CMAKE_CROSSCOMPILING)
160 set(CMAKE_CROSSCOMPILING TRUE)
162 elseif(CMAKE_VS_WINCE_VERSION)
163 set(CMAKE_SYSTEM_NAME "WindowsCE")
164 set(CMAKE_SYSTEM_VERSION "${CMAKE_VS_WINCE_VERSION}")
165 set(CMAKE_SYSTEM_PROCESSOR "${MSVC_C_ARCHITECTURE_ID}")
166 set(CMAKE_CROSSCOMPILING TRUE)
168 # Build for the host platform and architecture by default.
169 set(CMAKE_SYSTEM_NAME "${CMAKE_HOST_SYSTEM_NAME}")
170 if(NOT DEFINED CMAKE_SYSTEM_VERSION)
171 set(CMAKE_SYSTEM_VERSION "${CMAKE_HOST_SYSTEM_VERSION}")
173 set(CMAKE_SYSTEM_PROCESSOR "${CMAKE_HOST_SYSTEM_PROCESSOR}")
174 if(CMAKE_CROSSCOMPILING)
175 message(AUTHOR_WARNING
176 "CMAKE_CROSSCOMPILING has been set by the project, toolchain file, or user. "
177 "CMake is resetting it to false because CMAKE_SYSTEM_NAME was not set. "
178 "To indicate cross compilation, only CMAKE_SYSTEM_NAME needs to be set."
181 set(CMAKE_CROSSCOMPILING FALSE)
184 include(Platform/${CMAKE_SYSTEM_NAME}-Determine OPTIONAL)
186 set(CMAKE_SYSTEM ${CMAKE_SYSTEM_NAME})
187 if(CMAKE_SYSTEM_VERSION)
188 string(APPEND CMAKE_SYSTEM -${CMAKE_SYSTEM_VERSION})
190 set(CMAKE_HOST_SYSTEM ${CMAKE_HOST_SYSTEM_NAME})
191 if(CMAKE_HOST_SYSTEM_VERSION)
192 string(APPEND CMAKE_HOST_SYSTEM -${CMAKE_HOST_SYSTEM_VERSION})
195 # this file is also executed from cpack, then we don't need to generate these files
196 # in this case there is no CMAKE_BINARY_DIR
198 # write entry to the log file
199 if(CMAKE_CROSSCOMPILING)
200 message(CONFIGURE_LOG
201 "The target system is: ${CMAKE_SYSTEM_NAME} - ${CMAKE_SYSTEM_VERSION} - ${CMAKE_SYSTEM_PROCESSOR}\n"
202 "The host system is: ${CMAKE_HOST_SYSTEM_NAME} - ${CMAKE_HOST_SYSTEM_VERSION} - ${CMAKE_HOST_SYSTEM_PROCESSOR}\n"
205 message(CONFIGURE_LOG
206 "The system is: ${CMAKE_SYSTEM_NAME} - ${CMAKE_SYSTEM_VERSION} - ${CMAKE_SYSTEM_PROCESSOR}\n"
210 # if a toolchain file is used, it needs to be included in the configured file,
211 # so settings done there are also available if they don't go in the cache and in try_compile()
212 set(INCLUDE_CMAKE_TOOLCHAIN_FILE_IF_REQUIRED)
213 if(CMAKE_TOOLCHAIN_FILE)
214 set(INCLUDE_CMAKE_TOOLCHAIN_FILE_IF_REQUIRED "include(\"${CMAKE_TOOLCHAIN_FILE}\")")
217 # configure variables set in this file for fast reload, the template file is defined at the top of this file
218 configure_file(${CMAKE_ROOT}/Modules/CMakeSystem.cmake.in
219 ${CMAKE_PLATFORM_INFO_DIR}/CMakeSystem.cmake