Merge topic 'cxx-checks-tolerate-unused-arguments'
[kiteware-cmake.git] / Modules / CMakeDetermineSystem.cmake
blobb330ed9d509fceca4718ee30695a7ad65f669585
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
11 if(CMAKE_HOST_UNIX)
12   find_program(CMAKE_UNAME NAMES uname PATHS /bin /usr/bin /usr/local/bin)
13   if(CMAKE_UNAME)
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
18         ERROR_QUIET)
19       execute_process(COMMAND ${CMAKE_UNAME} -r
20         OUTPUT_VARIABLE _CMAKE_HOST_SYSTEM_MINOR_VERSION
21         OUTPUT_STRIP_TRAILING_WHITESPACE
22         ERROR_QUIET)
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
30         ERROR_QUIET)
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}")
38         if(_ANDROID_API)
39           set(CMAKE_SYSTEM_VERSION "${_ANDROID_API}")
40         endif()
42         unset(_ANDROID_API_LEVEL_H)
43         unset(_ANDROID_API_LEVEL_H_CONTENT)
44         unset(_ANDROID_API_REGEX)
45         unset(_ANDROID_API_LINE)
46         unset(_ANDROID_API)
47       endif()
48     else()
49       execute_process(COMMAND ${CMAKE_UNAME} -r
50         OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_VERSION
51         OUTPUT_STRIP_TRAILING_WHITESPACE
52         ERROR_QUIET)
53     endif()
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
57         RESULT_VARIABLE val
58         OUTPUT_STRIP_TRAILING_WHITESPACE
59         ERROR_QUIET)
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}")
66       else()
67         set(_CMAKE_APPLE_SILICON_PROCESSOR "")
68       endif()
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
75             )
76           if(NOT _sysctl_result EQUAL 0 OR NOT _sysctl_stdout MATCHES "hw.optional.arm64: 1")
77             set(_CMAKE_APPLE_SILICON_PROCESSOR "")
78           endif()
79           unset(_sysctl_result)
80           unset(_sysctl_stderr)
81           unset(_sysctl_stdout)
82         endif()
83       endif()
84       if(_CMAKE_APPLE_SILICON_PROCESSOR)
85         set(CMAKE_HOST_SYSTEM_PROCESSOR "${_CMAKE_APPLE_SILICON_PROCESSOR}")
86       else()
87         execute_process(COMMAND ${CMAKE_UNAME} -m
88           OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
89           RESULT_VARIABLE val
90           OUTPUT_STRIP_TRAILING_WHITESPACE
91           ERROR_QUIET)
92       endif()
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")
97       endif()
98     elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "OpenBSD")
99       execute_process(COMMAND arch -s
100         OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
101         RESULT_VARIABLE val
102         OUTPUT_STRIP_TRAILING_WHITESPACE
103         ERROR_QUIET)
104     else()
105       execute_process(COMMAND ${CMAKE_UNAME} -p
106         OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
107         RESULT_VARIABLE val
108         OUTPUT_STRIP_TRAILING_WHITESPACE
109         ERROR_QUIET)
110       if("${val}" GREATER 0)
111         execute_process(COMMAND ${CMAKE_UNAME} -m
112           OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
113           RESULT_VARIABLE val
114           OUTPUT_STRIP_TRAILING_WHITESPACE
115           ERROR_QUIET)
116       endif()
117     endif()
118     # check the return of the last uname -m or -p
119     if("${val}" GREATER 0)
120         set(CMAKE_HOST_SYSTEM_PROCESSOR "unknown")
121     endif()
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}")
126   endif()
127 else()
128   if(CMAKE_HOST_WIN32)
129     if (DEFINED ENV{PROCESSOR_ARCHITEW6432})
130       set (CMAKE_HOST_SYSTEM_PROCESSOR "$ENV{PROCESSOR_ARCHITEW6432}")
131     else()
132       set (CMAKE_HOST_SYSTEM_PROCESSOR "$ENV{PROCESSOR_ARCHITECTURE}")
133     endif()
134   endif()
135 endif()
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)
147   endif()
149   if(_INCLUDED_TOOLCHAIN_FILE)
150     set(CMAKE_TOOLCHAIN_FILE "${_INCLUDED_TOOLCHAIN_FILE}" CACHE FILEPATH "The CMake toolchain file" FORCE)
151   else()
152     message(FATAL_ERROR "Could not find toolchain file: ${CMAKE_TOOLCHAIN_FILE}")
153   endif()
154 endif()
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)
161   endif()
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)
167 else()
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}")
172   endif()
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."
179       )
180   endif()
181   set(CMAKE_CROSSCOMPILING FALSE)
182 endif()
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})
189 endif()
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})
193 endif()
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
197 if(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"
203       )
204   else()
205     message(CONFIGURE_LOG
206       "The system is: ${CMAKE_SYSTEM_NAME} - ${CMAKE_SYSTEM_VERSION} - ${CMAKE_SYSTEM_PROCESSOR}\n"
207       )
208   endif()
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}\")")
215   endif()
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
220                 @ONLY)
222 endif()