summaryrefslogtreecommitdiffstats
path: root/Modules/Compiler/CMakeCommonCompilerMacros.cmake
blob: 409b65a5f58b912fa974940a967abe896cc7d3ce (plain)
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
# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.

# This module is shared by multiple languages and compilers; use include guard
if (__COMPILER_CMAKE_COMMON_COMPILER_MACROS)
  return()
endif ()
set(__COMPILER_CMAKE_COMMON_COMPILER_MACROS 1)


# Check that a compiler's language standard is properly detected
# Parameters:
#   lang   - Language to check
#   stdver1 - Minimum version to set a given default for
#   std1    - Default to use for compiler ver >= stdver1
#   stdverN - Minimum version to set a given default for
#   stdN    - Default to use for compiler ver >= stdverN
#
#   The order of stdverN stdN pairs passed as arguments is expected to be in
#   monotonically increasing version order.
#
# Note:
#   This macro can be called with multiple version / std pairs to convey that
#   newer compiler versions may use a newer standard default.
#
# Example:
#   To specify that compiler version 6.1 and newer defaults to C++11 while
#   4.8 <= ver < 6.1 default to C++98, you would call:
#
# __compiler_check_default_language_standard(CXX 4.8 98 6.1 11)
#
macro(__compiler_check_default_language_standard lang stdver1 std1)
  set(__std_ver_pairs "${stdver1};${std1};${ARGN}")
  string(REGEX REPLACE " *; *" " " __std_ver_pairs "${__std_ver_pairs}")
  string(REGEX MATCHALL "[^ ]+ [^ ]+" __std_ver_pairs "${__std_ver_pairs}")

  # If the compiler version is below the threshold of even having CMake
  # support for language standards, then don't bother.
  if (CMAKE_${lang}_COMPILER_VERSION VERSION_GREATER_EQUAL "${stdver1}")
    if (NOT CMAKE_${lang}_COMPILER_FORCED)
      if (NOT CMAKE_${lang}_STANDARD_COMPUTED_DEFAULT)
        message(FATAL_ERROR "CMAKE_${lang}_STANDARD_COMPUTED_DEFAULT should be set for ${CMAKE_${lang}_COMPILER_ID} (${CMAKE_${lang}_COMPILER}) version ${CMAKE_${lang}_COMPILER_VERSION}")
      endif ()
      set(CMAKE_${lang}_STANDARD_DEFAULT ${CMAKE_${lang}_STANDARD_COMPUTED_DEFAULT})
    else ()
      list(REVERSE __std_ver_pairs)
      foreach (__std_ver_pair IN LISTS __std_ver_pairs)
        string(REGEX MATCH "([^ ]+) (.+)" __std_ver_pair "${__std_ver_pair}")
        set(__stdver ${CMAKE_MATCH_1})
        set(__std ${CMAKE_MATCH_2})
        if (CMAKE_${lang}_COMPILER_VERSION VERSION_GREATER_EQUAL __stdver AND
          NOT DEFINED CMAKE_${lang}_STANDARD_DEFAULT)
          # Compiler id was forced so just guess the default standard level.
          set(CMAKE_${lang}_STANDARD_DEFAULT ${__std})
        endif ()
        unset(__std)
        unset(__stdver)
      endforeach ()
    endif ()
  endif ()
  unset(__std_ver_pairs)
endmacro()

# Define to allow compile features to be automatically determined
macro(cmake_record_c_compile_features)
  set(_result 0)
  if(_result EQUAL 0 AND DEFINED CMAKE_C11_STANDARD_COMPILE_OPTION)
    if(CMAKE_C11_STANDARD__HAS_FULL_SUPPORT)
      _has_compiler_features_c(11)
    else()
      _record_compiler_features_c(11)
    endif()
    unset(CMAKE_C11_STANDARD__HAS_FULL_SUPPORT)
  endif()
  if(_result EQUAL 0 AND DEFINED CMAKE_C99_STANDARD_COMPILE_OPTION)
    if(CMAKE_C99_STANDARD__HAS_FULL_SUPPORT)
      _has_compiler_features_c(99)
    else()
      _record_compiler_features_c(99)
    endif()
    unset(CMAKE_C99_STANDARD__HAS_FULL_SUPPORT)
  endif()
  if(_result EQUAL 0 AND DEFINED CMAKE_C90_STANDARD_COMPILE_OPTION)
    if(CMAKE_C90_STANDARD__HAS_FULL_SUPPORT)
      _has_compiler_features_c(90)
    else()
      _record_compiler_features_c(90)
    endif()
    unset(CMAKE_C90_STANDARD__HAS_FULL_SUPPORT)
  endif()
endmacro()

# Define to allow compile features to be automatically determined
macro(cmake_record_cxx_compile_features)
  set(_result 0)
  if(_result EQUAL 0 AND DEFINED CMAKE_CXX20_STANDARD_COMPILE_OPTION)
    if(CMAKE_CXX20_STANDARD__HAS_FULL_SUPPORT)
      _has_compiler_features_cxx(20)
    else()
      _record_compiler_features_cxx(20)
    endif()
    unset(CMAKE_CXX20_STANDARD__HAS_FULL_SUPPORT)
  endif()
  if(_result EQUAL 0 AND DEFINED CMAKE_CXX17_STANDARD_COMPILE_OPTION)
    if(CMAKE_CXX17_STANDARD__HAS_FULL_SUPPORT)
      _has_compiler_features_cxx(17)
    else()
      _record_compiler_features_cxx(17)
    endif()
    unset(CMAKE_CXX17_STANDARD__HAS_FULL_SUPPORT)
  endif()
  if(_result EQUAL 0 AND DEFINED CMAKE_CXX14_STANDARD_COMPILE_OPTION)
    if(CMAKE_CXX14_STANDARD__HAS_FULL_SUPPORT)
      _has_compiler_features_cxx(14)
    else()
      _record_compiler_features_cxx(14)
    endif()
    unset(CMAKE_CXX14_STANDARD__HAS_FULL_SUPPORT)
  endif()
  if(_result EQUAL 0 AND DEFINED CMAKE_CXX11_STANDARD_COMPILE_OPTION)
    if(CMAKE_CXX11_STANDARD__HAS_FULL_SUPPORT)
      _has_compiler_features_cxx(11)
    else()
      _record_compiler_features_cxx(11)
    endif()
    unset(CMAKE_CXX11_STANDARD__HAS_FULL_SUPPORT)
  endif()
  if(_result EQUAL 0 AND DEFINED CMAKE_CXX98_STANDARD_COMPILE_OPTION)
    if(CMAKE_CXX98_STANDARD__HAS_FULL_SUPPORT)
      _has_compiler_features_cxx(98)
    else()
      _record_compiler_features_cxx(98)
    endif()
    unset(CMAKE_CXX98_STANDARD__HAS_FULL_SUPPORT)
  endif()
endmacro()

macro(cmake_record_cuda_compile_features)
  set(_result 0)
  if(_result EQUAL 0 AND DEFINED CMAKE_CUDA20_STANDARD_COMPILE_OPTION)
    if(CMAKE_CUDA20_STANDARD__HAS_FULL_SUPPORT)
      _has_compiler_features_cuda(20)
    else()
      _record_compiler_features_cuda(20)
    endif()
    unset(CMAKE_CUDA20_STANDARD__HAS_FULL_SUPPORT)
  endif()
  if(_result EQUAL 0 AND DEFINED CMAKE_CUDA17_STANDARD_COMPILE_OPTION)
    if(CMAKE_CUDA17_STANDARD__HAS_FULL_SUPPORT)
      _has_compiler_features_cuda(17)
    else()
      _record_compiler_features_cuda(17)
    endif()
    unset(CMAKE_CUDA17_STANDARD__HAS_FULL_SUPPORT)
  endif()
  if(_result EQUAL 0 AND DEFINED CMAKE_CUDA14_STANDARD_COMPILE_OPTION)
    if(CMAKE_CUDA14_STANDARD__HAS_FULL_SUPPORT)
      _has_compiler_features_cuda(14)
    else()
      _record_compiler_features_cuda(14)
    endif()
    unset(CMAKE_CUDA14_STANDARD__HAS_FULL_SUPPORT)
  endif()
  if(_result EQUAL 0 AND DEFINED CMAKE_CUDA11_STANDARD_COMPILE_OPTION)
    if(CMAKE_CUDA11_STANDARD__HAS_FULL_SUPPORT)
      _has_compiler_features_cuda(11)
    else()
      _record_compiler_features_cuda(11)
    endif()
    unset(CMAKE_CUDA11_STANDARD__HAS_FULL_SUPPORT)
  endif()
  if(_result EQUAL 0 AND DEFINED CMAKE_CUDA03_STANDARD_COMPILE_OPTION)
    if(CMAKE_CUDA03_STANDARD__HAS_FULL_SUPPORT)
      _has_compiler_features_cuda(03)
    else()
      _record_compiler_features_cuda(03)
    endif()
    unset(CMAKE_CUDA03_STANDARD__HAS_FULL_SUPPORT)
  endif()
endmacro()