-
Notifications
You must be signed in to change notification settings - Fork 274
/
Compilers.cmake
208 lines (165 loc) · 6.26 KB
/
Compilers.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
################################################################################
#
# Compilers
#
################################################################################
#
# sets (cached):
#
# CMAKE_C_COMPILER_IS_<TYPE>
# CMAKE_CXX_COMPILER_IS_<TYPE>
#
# where TYPE is:
# - GNU
# - CLANG
# - INTEL
# - INTEL_ICC
# - INTEL_ICPC
# - PGI
# - XLC
# - HP_ACC
# - MIPS
# - MSVC
#
# include guard
if(__compilers_is_loaded)
return()
endif()
set(__compilers_is_loaded ON)
include(CheckLanguage)
include(CheckCCompilerFlag)
include(CheckCSourceCompiles)
include(CheckCSourceRuns)
include(CheckCXXCompilerFlag)
include(CheckCXXSourceCompiles)
include(CheckCXXSourceRuns)
################################################################################
# macro converting string to list
################################################################################
macro(to_list _VAR _STR)
STRING(REPLACE " " " " ${_VAR} "${_STR}")
STRING(REPLACE " " ";" ${_VAR} "${_STR}")
endmacro(to_list _VAR _STR)
################################################################################
# macro converting string to list
################################################################################
macro(to_string _VAR _STR)
STRING(REPLACE ";" " " ${_VAR} "${_STR}")
endmacro(to_string _VAR _STR)
################################################################################
# Macro to add to string
################################################################################
macro(add _VAR _FLAG)
if(NOT "${_FLAG}" STREQUAL "")
if("${${_VAR}}" STREQUAL "")
set(${_VAR} "${_FLAG}")
else()
set(${_VAR} "${${_VAR}} ${_FLAG}")
endif()
endif()
endmacro()
################################################################################
# macro to remove duplicates from string
################################################################################
macro(set_no_duplicates _VAR)
if(NOT "${ARGN}" STREQUAL "")
set(${_VAR} "${ARGN}")
endif()
# remove the duplicates
if(NOT "${${_VAR}}" STREQUAL "")
# create list of flags
to_list(_VAR_LIST "${${_VAR}}")
list(REMOVE_DUPLICATES _VAR_LIST)
to_string(${_VAR} "${_VAR_LIST}")
endif(NOT "${${_VAR}}" STREQUAL "")
endmacro(set_no_duplicates _VAR)
################################################################################
# check C flag
################################################################################
macro(ADD_C_FLAG_IF_AVAIL FLAG)
if(NOT "${FLAG}" STREQUAL "")
string(REGEX REPLACE "^-" "c_" FLAG_NAME "${FLAG}")
string(REPLACE "-" "_" FLAG_NAME "${FLAG_NAME}")
string(REPLACE " " "_" FLAG_NAME "${FLAG_NAME}")
string(REPLACE "=" "_" FLAG_NAME "${FLAG_NAME}")
check_c_compiler_flag("${FLAG}" ${FLAG_NAME})
if(${FLAG_NAME})
list(APPEND ${PROJECT_NAME}_C_FLAGS "${FLAG}")
endif()
endif()
endmacro()
################################################################################
# check CXX flag
################################################################################
macro(ADD_CXX_FLAG_IF_AVAIL FLAG)
if(NOT "${FLAG}" STREQUAL "")
string(REGEX REPLACE "^-" "cxx_" FLAG_NAME "${FLAG}")
string(REPLACE "-" "_" FLAG_NAME "${FLAG_NAME}")
string(REPLACE " " "_" FLAG_NAME "${FLAG_NAME}")
string(REPLACE "=" "_" FLAG_NAME "${FLAG_NAME}")
check_cxx_compiler_flag("${FLAG}" ${FLAG_NAME})
if(${FLAG_NAME})
list(APPEND ${PROJECT_NAME}_CXX_FLAGS "${FLAG}")
endif()
endif()
endmacro()
################################################################################
# determine compiler types for each language
################################################################################
foreach(LANG C CXX)
macro(SET_COMPILER_VAR VAR _BOOL)
set(CMAKE_${LANG}_COMPILER_IS_${VAR} ${_BOOL} CACHE BOOL
"CMake ${LANG} compiler identification (${VAR})")
mark_as_advanced(CMAKE_${LANG}_COMPILER_IS_${VAR})
endmacro()
if(("${LANG}" STREQUAL "C" AND CMAKE_COMPILER_IS_GNUCC)
OR
("${LANG}" STREQUAL "CXX" AND CMAKE_COMPILER_IS_GNUCXX))
# GNU compiler
SET_COMPILER_VAR( GNU ON)
elseif(CMAKE_${LANG}_COMPILER MATCHES "icc.*")
# Intel icc compiler
SET_COMPILER_VAR( INTEL ON)
SET_COMPILER_VAR( INTEL_ICC ON)
elseif(CMAKE_${LANG}_COMPILER MATCHES "icpc.*")
# Intel icpc compiler
SET_COMPILER_VAR( INTEL ON)
SET_COMPILER_VAR( INTEL_ICPC ON)
elseif(CMAKE_${LANG}_COMPILER_ID MATCHES "Clang" OR
CMAKE_${LANG}_COMPILER_ID MATCHES "AppleClang")
# Clang/LLVM compiler
SET_COMPILER_VAR( CLANG ON)
elseif(CMAKE_${LANG}_COMPILER_ID MATCHES "PGI")
# PGI compiler
SET_COMPILER_VAR( PGI ON)
elseif(CMAKE_${LANG}_COMPILER MATCHES "xlC" AND UNIX)
# IBM xlC compiler
SET_COMPILER_VAR( XLC ON)
elseif(CMAKE_${LANG}_COMPILER MATCHES "aCC" AND UNIX)
# HP aC++ compiler
SET_COMPILER_VAR( HP_ACC ON)
elseif(CMAKE_${LANG}_COMPILER MATCHES "CC" AND
CMAKE_SYSTEM_NAME MATCHES "IRIX" AND UNIX)
# IRIX MIPSpro CC Compiler
SET_COMPILER_VAR( MIPS ON)
elseif(CMAKE_${LANG}_COMPILER_ID MATCHES "Intel")
SET_COMPILER_VAR( INTEL ON)
set(CTYPE ICC)
if("${LANG}" STREQUAL "CXX")
set(CTYPE ICPC)
endif()
SET_COMPILER_VAR( INTEL_${CTYPE} ON)
elseif(CMAKE_${LANG}_COMPILER MATCHES "MSVC")
# Windows Visual Studio compiler
SET_COMPILER_VAR( MSVC ON)
endif()
# set other to no
foreach(TYPE GNU INTEL INTEL_ICC INTEL_ICPC CLANG PGI XLC HP_ACC MIPS MSVC)
if(NOT ${CMAKE_${LANG}_COMPILER_IS_${TYPE}})
SET_COMPILER_VAR(${TYPE} OFF)
endif()
endforeach()
if(APPLE)
set(CMAKE_INCLUDE_SYSTEM_FLAG_${LANG} "-isystem ")
endif(APPLE)
endforeach()