Imported Upstream version 2.8.10.2
[platform/upstream/cmake.git] / Modules / CMakeDetermineSystem.cmake
1
2 #=============================================================================
3 # Copyright 2002-2009 Kitware, Inc.
4 #
5 # Distributed under the OSI-approved BSD License (the "License");
6 # see accompanying file Copyright.txt for details.
7 #
8 # This software is distributed WITHOUT ANY WARRANTY; without even the
9 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 # See the License for more information.
11 #=============================================================================
12 # (To distribute this file outside of CMake, substitute the full
13 #  License text for the above reference.)
14
15 # This module is used by the Makefile generator to determin the following variables:
16 # CMAKE_SYSTEM_NAME - on unix this is uname -s, for windows it is Windows
17 # CMAKE_SYSTEM_VERSION - on unix this is uname -r, for windows it is empty
18 # CMAKE_SYSTEM - ${CMAKE_SYSTEM}-${CMAKE_SYSTEM_VERSION}, for windows: ${CMAKE_SYSTEM}
19 #
20 #  Expected uname -s output:
21 #
22 # AIX                           AIX
23 # BSD/OS                        BSD/OS
24 # FreeBSD                       FreeBSD
25 # HP-UX                         HP-UX
26 # IRIX                          IRIX
27 # Linux                         Linux
28 # GNU/kFreeBSD                  GNU/kFreeBSD
29 # NetBSD                        NetBSD
30 # OpenBSD                       OpenBSD
31 # OFS/1 (Digital Unix)          OSF1
32 # SCO OpenServer 5              SCO_SV
33 # SCO UnixWare 7                UnixWare
34 # SCO UnixWare (pre release 7)  UNIX_SV
35 # SCO XENIX                     Xenix
36 # Solaris                       SunOS
37 # SunOS                         SunOS
38 # Tru64                         Tru64
39 # Ultrix                        ULTRIX
40 # cygwin                        CYGWIN_NT-5.1
41 # MacOSX                        Darwin
42
43
44 # find out on which system cmake runs
45 if(CMAKE_HOST_UNIX)
46   find_program(CMAKE_UNAME uname /bin /usr/bin /usr/local/bin )
47   if(CMAKE_UNAME)
48     exec_program(uname ARGS -s OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_NAME)
49     exec_program(uname ARGS -r OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_VERSION)
50     if(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux|CYGWIN.*")
51       exec_program(uname ARGS -m OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
52         RETURN_VALUE val)
53     elseif(CMAKE_HOST_SYSTEM_NAME MATCHES "OpenBSD")
54       exec_program(arch ARGS -s OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
55         RETURN_VALUE val)
56     else()
57       exec_program(uname ARGS -p OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
58         RETURN_VALUE val)
59       if("${val}" GREATER 0)
60         exec_program(uname ARGS -m OUTPUT_VARIABLE CMAKE_HOST_SYSTEM_PROCESSOR
61           RETURN_VALUE val)
62       endif()
63     endif()
64     # check the return of the last uname -m or -p
65     if("${val}" GREATER 0)
66         set(CMAKE_HOST_SYSTEM_PROCESSOR "unknown")
67     endif()
68     set(CMAKE_UNAME ${CMAKE_UNAME} CACHE INTERNAL "uname command")
69     # processor may have double quote in the name, and that needs to be removed
70     string(REGEX REPLACE "\"" "" CMAKE_HOST_SYSTEM_PROCESSOR "${CMAKE_HOST_SYSTEM_PROCESSOR}")
71     string(REGEX REPLACE "/" "_" CMAKE_HOST_SYSTEM_PROCESSOR "${CMAKE_HOST_SYSTEM_PROCESSOR}")
72   endif()
73 else()
74   if(CMAKE_HOST_WIN32)
75     set (CMAKE_HOST_SYSTEM_NAME "Windows")
76     set (CMAKE_HOST_SYSTEM_PROCESSOR "$ENV{PROCESSOR_ARCHITECTURE}")
77   endif()
78 endif()
79
80 # if a toolchain file is used, the user wants to cross compile.
81 # in this case read the toolchain file and keep the CMAKE_HOST_SYSTEM_*
82 # variables around so they can be used in CMakeLists.txt.
83 # In all other cases, the host and target platform are the same.
84 if(CMAKE_TOOLCHAIN_FILE)
85   # at first try to load it as path relative to the directory from which cmake has been run
86   include("${CMAKE_BINARY_DIR}/${CMAKE_TOOLCHAIN_FILE}" OPTIONAL RESULT_VARIABLE _INCLUDED_TOOLCHAIN_FILE)
87   if(NOT _INCLUDED_TOOLCHAIN_FILE)
88      # if the file isn't found there, check the default locations
89      include("${CMAKE_TOOLCHAIN_FILE}" OPTIONAL RESULT_VARIABLE _INCLUDED_TOOLCHAIN_FILE)
90   endif()
91
92   if(_INCLUDED_TOOLCHAIN_FILE)
93     set(CMAKE_TOOLCHAIN_FILE "${_INCLUDED_TOOLCHAIN_FILE}" CACHE FILEPATH "The CMake toolchain file" FORCE)
94   else()
95     message(FATAL_ERROR "Could not find toolchain file: ${CMAKE_TOOLCHAIN_FILE}")
96     set(CMAKE_TOOLCHAIN_FILE "NOTFOUND" CACHE FILEPATH "The CMake toolchain file" FORCE)
97   endif()
98 endif()
99
100
101 # if CMAKE_SYSTEM_NAME is here already set, either it comes from a toolchain file
102 # or it was set via -DCMAKE_SYSTEM_NAME=...
103 # if that's the case, assume we are crosscompiling
104 if(CMAKE_SYSTEM_NAME)
105   if(NOT DEFINED CMAKE_CROSSCOMPILING)
106     set(CMAKE_CROSSCOMPILING TRUE)
107   endif()
108   set(PRESET_CMAKE_SYSTEM_NAME TRUE)
109 else()
110   set(CMAKE_SYSTEM_NAME      "${CMAKE_HOST_SYSTEM_NAME}")
111   set(CMAKE_SYSTEM_VERSION   "${CMAKE_HOST_SYSTEM_VERSION}")
112   set(CMAKE_SYSTEM_PROCESSOR "${CMAKE_HOST_SYSTEM_PROCESSOR}")
113   set(CMAKE_CROSSCOMPILING FALSE)
114   set(PRESET_CMAKE_SYSTEM_NAME FALSE)
115 endif()
116
117
118 macro(ADJUST_CMAKE_SYSTEM_VARIABLES _PREFIX)
119   if(NOT ${_PREFIX}_NAME)
120     set(${_PREFIX}_NAME "UnknownOS")
121   endif()
122
123   # fix for BSD/OS , remove the /
124   if(${_PREFIX}_NAME MATCHES BSD.OS)
125     set(${_PREFIX}_NAME BSDOS)
126   endif()
127
128   # fix for GNU/kFreeBSD, remove the GNU/
129   if(${_PREFIX}_NAME MATCHES kFreeBSD)
130     set(${_PREFIX}_NAME kFreeBSD)
131   endif()
132
133   # fix for CYGWIN which has windows version in it
134   if(${_PREFIX}_NAME MATCHES CYGWIN)
135     set(${_PREFIX}_NAME CYGWIN)
136   endif()
137
138   # set CMAKE_SYSTEM to the CMAKE_SYSTEM_NAME
139   set(${_PREFIX}  ${${_PREFIX}_NAME})
140   # if there is a CMAKE_SYSTEM_VERSION then add a -${CMAKE_SYSTEM_VERSION}
141   if(${_PREFIX}_VERSION)
142     set(${_PREFIX} ${${_PREFIX}}-${${_PREFIX}_VERSION})
143   endif()
144
145 endmacro()
146
147 ADJUST_CMAKE_SYSTEM_VARIABLES(CMAKE_SYSTEM)
148 ADJUST_CMAKE_SYSTEM_VARIABLES(CMAKE_HOST_SYSTEM)
149
150 # this file is also executed from cpack, then we don't need to generate these files
151 # in this case there is no CMAKE_BINARY_DIR
152 if(CMAKE_BINARY_DIR)
153   # write entry to the log file
154   if(PRESET_CMAKE_SYSTEM_NAME)
155     file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
156                 "The target system is: ${CMAKE_SYSTEM_NAME} - ${CMAKE_SYSTEM_VERSION} - ${CMAKE_SYSTEM_PROCESSOR}\n")
157     file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
158                 "The host system is: ${CMAKE_HOST_SYSTEM_NAME} - ${CMAKE_HOST_SYSTEM_VERSION} - ${CMAKE_HOST_SYSTEM_PROCESSOR}\n")
159   else()
160     file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
161                 "The system is: ${CMAKE_SYSTEM_NAME} - ${CMAKE_SYSTEM_VERSION} - ${CMAKE_SYSTEM_PROCESSOR}\n")
162   endif()
163
164   # if a toolchain file is used, it needs to be included in the configured file,
165   # so settings done there are also available if they don't go in the cache and in try_compile()
166   set(INCLUDE_CMAKE_TOOLCHAIN_FILE_IF_REQUIRED)
167   if(DEFINED CMAKE_TOOLCHAIN_FILE)
168     set(INCLUDE_CMAKE_TOOLCHAIN_FILE_IF_REQUIRED "include(\"${CMAKE_TOOLCHAIN_FILE}\")")
169   endif()
170
171   # configure variables set in this file for fast reload, the template file is defined at the top of this file
172   configure_file(${CMAKE_ROOT}/Modules/CMakeSystem.cmake.in
173                 ${CMAKE_PLATFORM_INFO_DIR}/CMakeSystem.cmake
174                 IMMEDIATE @ONLY)
175
176 endif()