Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Modules / CMakeCompilerIdDetection.cmake
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 function(_readFile file)
6   include(${file})
7   get_filename_component(name ${file} NAME_WE)
8   string(REGEX REPLACE "-.*" "" CompilerId ${name})
9   set(_compiler_id_version_compute_${CompilerId} ${_compiler_id_version_compute} PARENT_SCOPE)
10   set(_compiler_id_simulate_${CompilerId} ${_compiler_id_simulate} PARENT_SCOPE)
11   set(_compiler_id_pp_test_${CompilerId} ${_compiler_id_pp_test} PARENT_SCOPE)
12 endfunction()
13
14 function(compiler_id_detection outvar lang)
15
16   if (NOT "x${lang}" STREQUAL "xFortran" AND NOT "x${lang}" STREQUAL "xCSharp"
17       AND NOT "x${lang}" STREQUAL "xISPC")
18     file(GLOB lang_files
19       "${CMAKE_ROOT}/Modules/Compiler/*-DetermineCompiler.cmake")
20     set(nonlang CXX)
21     if ("x${lang}" STREQUAL "xCXX")
22       set(nonlang C)
23     endif()
24
25     file(GLOB nonlang_files
26       "${CMAKE_ROOT}/Modules/Compiler/*-${nonlang}-DetermineCompiler.cmake")
27     list(REMOVE_ITEM lang_files ${nonlang_files})
28   endif()
29
30   set(files ${lang_files})
31   if (files)
32     foreach(file ${files})
33       _readFile(${file})
34     endforeach()
35
36     set(options ID_STRING VERSION_STRINGS ID_DEFINE PLATFORM_DEFAULT_COMPILER)
37     set(oneValueArgs PREFIX)
38     cmake_parse_arguments(CID "${options}" "${oneValueArgs}" "${multiValueArgs}"  ${ARGN})
39     if (CID_UNPARSED_ARGUMENTS)
40       message(FATAL_ERROR "Unrecognized arguments: \"${CID_UNPARSED_ARGUMENTS}\"")
41     endif()
42
43     # Order is relevant here. For example, compilers which pretend to be
44     # GCC must appear before the actual GCC.
45     if ("x${lang}" STREQUAL "xCXX")
46       list(APPEND ordered_compilers
47         Comeau
48       )
49     endif()
50     list(APPEND ordered_compilers
51       Intel
52       IntelLLVM
53       PathScale
54       Embarcadero
55       Borland
56       Watcom
57       OpenWatcom
58       SunPro
59       HP
60       Compaq
61       zOS
62       IBMClang
63       XLClang
64       XL
65       VisualAge
66       NVHPC
67       PGI
68       Cray
69       TI
70       FujitsuClang
71       Fujitsu
72       GHS
73       Tasking
74     )
75     if ("x${lang}" STREQUAL "xC")
76       list(APPEND ordered_compilers
77         TinyCC
78         Bruce
79       )
80     endif()
81     list(APPEND ordered_compilers
82       SCO
83       ARMCC
84       AppleClang
85       ARMClang
86     )
87     list(APPEND ordered_compilers
88       Clang
89       LCC
90       GNU
91       MSVC
92       ADSP
93       IAR
94     )
95     if ("x${lang}" STREQUAL "xC")
96       list(APPEND ordered_compilers
97         SDCC
98       )
99     endif()
100
101     if("x${lang}" STREQUAL "xCUDA")
102       set(ordered_compilers NVIDIA Clang)
103     endif()
104
105     if(CID_ID_DEFINE)
106       foreach(Id ${ordered_compilers})
107         string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "# define ${CID_PREFIX}COMPILER_IS_${Id} 0\n")
108       endforeach()
109       # Hard-code definitions for compilers that are no longer supported.
110       string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "# define ${CID_PREFIX}COMPILER_IS_MIPSpro 0\n")
111     endif()
112
113     set(pp_if "#if")
114     if (CID_VERSION_STRINGS)
115       string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "\n/* Version number components: V=Version, R=Revision, P=Patch
116    Version date components:   YYYY=Year, MM=Month,   DD=Day  */\n")
117     endif()
118
119     foreach(Id ${ordered_compilers})
120       if (NOT _compiler_id_pp_test_${Id})
121         message(FATAL_ERROR "No preprocessor test for \"${Id}\"")
122       endif()
123       set(id_content "${pp_if} ${_compiler_id_pp_test_${Id}}\n")
124       if (CID_ID_STRING)
125         set(PREFIX ${CID_PREFIX})
126         string(CONFIGURE "${_compiler_id_simulate_${Id}}" SIMULATE_BLOCK @ONLY)
127         string(APPEND id_content "# define ${CID_PREFIX}COMPILER_ID \"${Id}\"${SIMULATE_BLOCK}")
128       endif()
129       if (CID_ID_DEFINE)
130         string(APPEND id_content "# undef ${CID_PREFIX}COMPILER_IS_${Id}\n")
131         string(APPEND id_content "# define ${CID_PREFIX}COMPILER_IS_${Id} 1\n")
132       endif()
133       if (CID_VERSION_STRINGS)
134         set(PREFIX ${CID_PREFIX})
135         set(MACRO_DEC DEC)
136         set(MACRO_HEX HEX)
137         string(CONFIGURE "${_compiler_id_version_compute_${Id}}" VERSION_BLOCK @ONLY)
138         string(APPEND id_content "${VERSION_BLOCK}\n")
139       endif()
140       string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "\n${id_content}")
141       set(pp_if "#elif")
142     endforeach()
143
144     if (CID_PLATFORM_DEFAULT_COMPILER)
145       set(platform_compiler_detection "
146 /* These compilers are either not known or too old to define an
147   identification macro.  Try to identify the platform and guess that
148   it is the native compiler.  */
149 #elif defined(__hpux) || defined(__hpua)
150 # define ${CID_PREFIX}COMPILER_ID \"HP\"
151
152 #else /* unknown compiler */
153 # define ${CID_PREFIX}COMPILER_ID \"\"")
154     endif()
155
156     string(APPEND CMAKE_${lang}_COMPILER_ID_CONTENT "\n${platform_compiler_detection}\n#endif")
157   endif()
158
159   set(${outvar} ${CMAKE_${lang}_COMPILER_ID_CONTENT} PARENT_SCOPE)
160 endfunction()