1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4
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}
9#
10#  Expected uname -s output:
11#
12# AIX                           AIX
13# BSD/OS                        BSD/OS
14# FreeBSD                       FreeBSD
15# HP-UX                         HP-UX
16# Linux                         Linux
17# GNU/kFreeBSD                  GNU/kFreeBSD
18# NetBSD                        NetBSD
19# OpenBSD                       OpenBSD
20# OFS/1 (Digital Unix)          OSF1
21# SCO OpenServer 5              SCO_SV
22# SCO UnixWare 7                UnixWare
23# SCO UnixWare (pre release 7)  UNIX_SV
24# SCO XENIX                     Xenix
25# Solaris                       SunOS
26# SunOS                         SunOS
27# Tru64                         Tru64
28# Ultrix                        ULTRIX
29# cygwin                        CYGWIN_NT-5.1
30# MSYS                          MSYS_NT-6.1
31# MacOSX                        Darwin
32
33
34# find out on which system cmake runs
35if(CMAKE_HOST_UNIX)
36  find_program(CMAKE_UNAME uname /bin /usr/bin /usr/local/bin )
37  if(CMAKE_UNAME)
38    if(CMAKE_HOST_SYSTEM_NAME STREQUAL "AIX")
39      exec_program(${CMAKE_UNAME} ARGS -v OUTPUT_VARIABLE _CMAKE_HOST_SYSTEM_MAJOR_VERSION)
40      exec_program(${CMAKE_UNAME} ARGS -r OUTPUT_VARIABLE _CMAKE_HOST_SYSTEM_MINOR_VERSION)
41      set(CMAKE_HOST_SYSTEM_VERSION "${_CMAKE_HOST_SYSTEM_MAJOR_VERSION}.${_CMAKE_HOST_SYSTEM_MINOR_VERSION}")
42      unset(_CMAKE_HOST_SYSTEM_MAJOR_VERSION)
43      unset(_CMAKE_HOST_SYSTEM_MINOR_VERSION)
44    else()
45      exec_program(${CMAKE_UNAME} ARGS -r OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_VERSION)
46    endif()
47    if(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux|CYGWIN.*|MSYS.*|^GNU$|Android")
48      exec_program(${CMAKE_UNAME} ARGS -m OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
49        RETURN_VALUE val)
50    elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin")
51      # If we are running on Apple Silicon, honor CMAKE_APPLE_SILICON_PROCESSOR.
52      if(DEFINED CMAKE_APPLE_SILICON_PROCESSOR)
53        set(_CMAKE_APPLE_SILICON_PROCESSOR "${CMAKE_APPLE_SILICON_PROCESSOR}")
54      elseif(DEFINED ENV{CMAKE_APPLE_SILICON_PROCESSOR})
55        set(_CMAKE_APPLE_SILICON_PROCESSOR "$ENV{CMAKE_APPLE_SILICON_PROCESSOR}")
56      else()
57        set(_CMAKE_APPLE_SILICON_PROCESSOR "")
58      endif()
59      if(_CMAKE_APPLE_SILICON_PROCESSOR)
60        if(";${_CMAKE_APPLE_SILICON_PROCESSOR};" MATCHES "^;(arm64|x86_64);$")
61          execute_process(COMMAND sysctl -q hw.optional.arm64
62            OUTPUT_VARIABLE _sysctl_stdout
63            ERROR_VARIABLE _sysctl_stderr
64            RESULT_VARIABLE _sysctl_result
65            )
66          if(NOT _sysctl_result EQUAL 0 OR NOT _sysctl_stdout MATCHES "hw.optional.arm64: 1")
67            set(_CMAKE_APPLE_SILICON_PROCESSOR "")
68          endif()
69          unset(_sysctl_result)
70          unset(_sysctl_stderr)
71          unset(_sysctl_stdout)
72        endif()
73      endif()
74      if(_CMAKE_APPLE_SILICON_PROCESSOR)
75        set(CMAKE_HOST_SYSTEM_PROCESSOR "${_CMAKE_APPLE_SILICON_PROCESSOR}")
76      else()
77        exec_program(${CMAKE_UNAME} ARGS -m OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
78          RETURN_VALUE val)
79      endif()
80      unset(_CMAKE_APPLE_SILICON_PROCESSOR)
81      if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "Power Macintosh")
82        # OS X ppc 'uname -m' may report 'Power Macintosh' instead of 'powerpc'
83        set(CMAKE_HOST_SYSTEM_PROCESSOR "powerpc")
84      endif()
85    elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "OpenBSD")
86      exec_program(arch ARGS -s OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
87        RETURN_VALUE val)
88    else()
89      exec_program(${CMAKE_UNAME} ARGS -p OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
90        RETURN_VALUE val)
91      if("${val}" GREATER 0)
92        exec_program(${CMAKE_UNAME} ARGS -m OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
93          RETURN_VALUE val)
94      endif()
95    endif()
96    # check the return of the last uname -m or -p
97    if("${val}" GREATER 0)
98        set(CMAKE_HOST_SYSTEM_PROCESSOR "unknown")
99    endif()
100    set(CMAKE_UNAME ${CMAKE_UNAME} CACHE INTERNAL "uname command")
101    # processor may have double quote in the name, and that needs to be removed
102    string(REPLACE "\"" "" CMAKE_HOST_SYSTEM_PROCESSOR "${CMAKE_HOST_SYSTEM_PROCESSOR}")
103    string(REPLACE "/" "_" CMAKE_HOST_SYSTEM_PROCESSOR "${CMAKE_HOST_SYSTEM_PROCESSOR}")
104  endif()
105else()
106  if(CMAKE_HOST_WIN32)
107    if (DEFINED ENV{PROCESSOR_ARCHITEW6432})
108      set (CMAKE_HOST_SYSTEM_PROCESSOR "$ENV{PROCESSOR_ARCHITEW6432}")
109    else()
110      set (CMAKE_HOST_SYSTEM_PROCESSOR "$ENV{PROCESSOR_ARCHITECTURE}")
111    endif()
112  endif()
113endif()
114
115# if a toolchain file is used, the user wants to cross compile.
116# in this case read the toolchain file and keep the CMAKE_HOST_SYSTEM_*
117# variables around so they can be used in CMakeLists.txt.
118# In all other cases, the host and target platform are the same.
119if(CMAKE_TOOLCHAIN_FILE)
120  # at first try to load it as path relative to the directory from which cmake has been run
121  include("${CMAKE_BINARY_DIR}/${CMAKE_TOOLCHAIN_FILE}" OPTIONAL RESULT_VARIABLE _INCLUDED_TOOLCHAIN_FILE)
122  if(NOT _INCLUDED_TOOLCHAIN_FILE)
123     # if the file isn't found there, check the default locations
124     include("${CMAKE_TOOLCHAIN_FILE}" OPTIONAL RESULT_VARIABLE _INCLUDED_TOOLCHAIN_FILE)
125  endif()
126
127  if(_INCLUDED_TOOLCHAIN_FILE)
128    set(CMAKE_TOOLCHAIN_FILE "${_INCLUDED_TOOLCHAIN_FILE}" CACHE FILEPATH "The CMake toolchain file" FORCE)
129  else()
130    message(FATAL_ERROR "Could not find toolchain file: ${CMAKE_TOOLCHAIN_FILE}")
131    set(CMAKE_TOOLCHAIN_FILE "NOTFOUND" CACHE FILEPATH "The CMake toolchain file" FORCE)
132  endif()
133endif()
134
135
136# if CMAKE_SYSTEM_NAME is here already set, either it comes from a toolchain file
137# or it was set via -DCMAKE_SYSTEM_NAME=...
138# if that's the case, assume we are crosscompiling
139if(CMAKE_SYSTEM_NAME)
140  if(NOT DEFINED CMAKE_CROSSCOMPILING)
141    set(CMAKE_CROSSCOMPILING TRUE)
142  endif()
143  set(PRESET_CMAKE_SYSTEM_NAME TRUE)
144elseif(CMAKE_VS_WINCE_VERSION)
145  set(CMAKE_SYSTEM_NAME      "WindowsCE")
146  set(CMAKE_SYSTEM_VERSION   "${CMAKE_VS_WINCE_VERSION}")
147  set(CMAKE_SYSTEM_PROCESSOR "${MSVC_C_ARCHITECTURE_ID}")
148  set(CMAKE_CROSSCOMPILING TRUE)
149  set(PRESET_CMAKE_SYSTEM_NAME TRUE)
150else()
151  set(CMAKE_SYSTEM_NAME      "${CMAKE_HOST_SYSTEM_NAME}")
152  if(NOT DEFINED CMAKE_SYSTEM_VERSION)
153    set(CMAKE_SYSTEM_VERSION "${CMAKE_HOST_SYSTEM_VERSION}")
154  endif()
155  set(CMAKE_SYSTEM_PROCESSOR "${CMAKE_HOST_SYSTEM_PROCESSOR}")
156  set(CMAKE_CROSSCOMPILING FALSE)
157  set(PRESET_CMAKE_SYSTEM_NAME FALSE)
158endif()
159
160include(Platform/${CMAKE_SYSTEM_NAME}-Determine OPTIONAL)
161
162set(CMAKE_SYSTEM ${CMAKE_SYSTEM_NAME})
163if(CMAKE_SYSTEM_VERSION)
164  string(APPEND CMAKE_SYSTEM -${CMAKE_SYSTEM_VERSION})
165endif()
166set(CMAKE_HOST_SYSTEM ${CMAKE_HOST_SYSTEM_NAME})
167if(CMAKE_HOST_SYSTEM_VERSION)
168  string(APPEND CMAKE_HOST_SYSTEM -${CMAKE_HOST_SYSTEM_VERSION})
169endif()
170
171# this file is also executed from cpack, then we don't need to generate these files
172# in this case there is no CMAKE_BINARY_DIR
173if(CMAKE_BINARY_DIR)
174  # write entry to the log file
175  if(PRESET_CMAKE_SYSTEM_NAME)
176    file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
177                "The target system is: ${CMAKE_SYSTEM_NAME} - ${CMAKE_SYSTEM_VERSION} - ${CMAKE_SYSTEM_PROCESSOR}\n")
178    file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
179                "The host system is: ${CMAKE_HOST_SYSTEM_NAME} - ${CMAKE_HOST_SYSTEM_VERSION} - ${CMAKE_HOST_SYSTEM_PROCESSOR}\n")
180  else()
181    file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
182                "The system is: ${CMAKE_SYSTEM_NAME} - ${CMAKE_SYSTEM_VERSION} - ${CMAKE_SYSTEM_PROCESSOR}\n")
183  endif()
184
185  # if a toolchain file is used, it needs to be included in the configured file,
186  # so settings done there are also available if they don't go in the cache and in try_compile()
187  set(INCLUDE_CMAKE_TOOLCHAIN_FILE_IF_REQUIRED)
188  if(CMAKE_TOOLCHAIN_FILE)
189    set(INCLUDE_CMAKE_TOOLCHAIN_FILE_IF_REQUIRED "include(\"${CMAKE_TOOLCHAIN_FILE}\")")
190  endif()
191
192  # configure variables set in this file for fast reload, the template file is defined at the top of this file
193  configure_file(${CMAKE_ROOT}/Modules/CMakeSystem.cmake.in
194                ${CMAKE_PLATFORM_INFO_DIR}/CMakeSystem.cmake
195                @ONLY)
196
197endif()
198