summaryrefslogtreecommitdiffstats
path: root/Tests/RunCMake/CompileFeatures/RunCMakeTest.cmake
blob: 731a303c509837399a056916a9fc0555b693daa4 (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
181
182
183
184
185
186
187
188
cmake_policy(SET CMP0057 NEW)
include(RunCMake)

run_cmake(NotAFeature)
run_cmake(NotAFeatureGenex)
run_cmake(NotAFeatureTransitive)
run_cmake(NotAFeature_OriginDebug)
run_cmake(NotAFeature_OriginDebugGenex)
run_cmake(NotAFeature_OriginDebugTransitive)
run_cmake(NotAFeature_OriginDebugCommand)

run_cmake(compiler_introspection)
include("${RunCMake_BINARY_DIR}/compiler_introspection-build/info.cmake")

if (NOT C_FEATURES)
  run_cmake(NoSupportedCFeatures)
  run_cmake(NoSupportedCFeaturesGenex)
endif()

if (NOT CXX_FEATURES)
  run_cmake(NoSupportedCxxFeatures)
  run_cmake(NoSupportedCxxFeaturesGenex)
elseif (cxx_std_98 IN_LIST CXX_FEATURES AND cxx_std_11 IN_LIST CXX_FEATURES)
  if(CXX_STANDARD_DEFAULT EQUAL 98)
    run_cmake(LinkImplementationFeatureCycle)
  endif()
  run_cmake(LinkImplementationFeatureCycleSolved)

  if (cxx_final IN_LIST CXX_FEATURES)
    set(RunCMake_TEST_OPTIONS "-DHAVE_FINAL=1")
  endif()
  run_cmake(NonValidTarget1)
  run_cmake(NonValidTarget2)
  unset(RunCMake_TEST_OPTIONS)
endif()

configure_file("${RunCMake_SOURCE_DIR}/CMakeLists.txt" "${RunCMake_BINARY_DIR}/CMakeLists.txt" COPYONLY)
file(READ "${RunCMake_SOURCE_DIR}/CMP0128Common.cmake" cmp0128_common)

function(test_build)
  set(test ${name}-${lang})

  file(READ "${RunCMake_SOURCE_DIR}/${name}.cmake" cmake)
  file(CONFIGURE OUTPUT "${RunCMake_BINARY_DIR}/${test}.cmake" CONTENT "${cmake}${cmp0128_common}" @ONLY)
  if(EXISTS "${RunCMake_SOURCE_DIR}/${name}-build-check.cmake")
    configure_file("${RunCMake_SOURCE_DIR}/${name}-build-check.cmake" "${RunCMake_BINARY_DIR}/${test}-build-check.cmake" @ONLY)
  endif()
  if(EXISTS "${RunCMake_SOURCE_DIR}/${name}-stderr.txt")
    configure_file("${RunCMake_SOURCE_DIR}/${name}-stderr.txt" "${RunCMake_BINARY_DIR}/${test}-stderr.txt" @ONLY)
  endif()

  set(RunCMake_SOURCE_DIR "${RunCMake_BINARY_DIR}")
  set(RunCMake_TEST_BINARY_DIR "${RunCMake_BINARY_DIR}/${test}-build")
  run_cmake(${test})
  set(RunCMake_TEST_NO_CLEAN 1)
  run_cmake_command(${test}-build ${CMAKE_COMMAND} --build . ${ARGN})
endfunction()

# Mangle flags such as they're in verbose build output.
macro(mangle_flags variable)
  set(result "${${variable}}")

  if(RunCMake_GENERATOR MATCHES "Visual Studio" AND MSVC_TOOLSET_VERSION GREATER_EQUAL 141)
    string(REPLACE "-" "/" result "${result}")
  elseif(RunCMake_GENERATOR STREQUAL "Xcode" AND XCODE_VERSION VERSION_LESS 15.3 AND CMAKE_XCODE_BUILD_SYSTEM GREATER_EQUAL 12)
    string(REPLACE "=" [[\\=]] result "${result}")
  endif()

  string(REPLACE ";" " " result "${result}")
  list(APPEND flags "${result}")
endmacro()

function(test_cmp0128_old_same_standard)
  set(flag "${${lang}${${lang}_STANDARD_DEFAULT}_EXT_FLAG}")

  if(NOT flag)
    return()
  endif()

  mangle_flags(flag)

  set(name CMP0128OldSameStandard)
  test_build(--verbose)
endfunction()

function(test_cmp0128_new_extensions_opposite)
  if(extensions_opposite)
    set(flag_ext "_EXT")
  endif()

  set(flag "${${lang}${${lang}_STANDARD_DEFAULT}${flag_ext}_FLAG}")

  if(NOT flag)
    return()
  endif()

  mangle_flags(flag)

  # Make sure we enable/disable extensions when:
  # 1. LANG_STANDARD is unset.
  set(name CMP0128NewExtensionsStandardUnset)
  set(RunCMake_TEST_OPTIONS -DCMAKE_POLICY_DEFAULT_CMP0128=NEW)
  test_build(--verbose)

  # 2. LANG_STANDARD matches CMAKE_LANG_STANDARD_DEFAULT.
  set(name CMP0128NewExtensionsStandardDefault)
  test_build(--verbose)
endfunction()

function(test_cmp0128_new_no_unnecessary_flag)
  set(standard_flag "${${lang}${${lang}_STANDARD_DEFAULT}_FLAG}")
  set(extension_flag "${${lang}${${lang}_STANDARD_DEFAULT}_EXT_FLAG}")

  if(NOT standard_flag AND NOT extension_flag)
    return()
  endif()

  mangle_flags(standard_flag)
  mangle_flags(extension_flag)

  set(name CMP0128NewNoUnnecessaryFlag)
  set(RunCMake_TEST_OPTIONS -DCMAKE_POLICY_DEFAULT_CMP0128=NEW)
  test_build(--verbose)
endfunction()

function(test_cmp0128_warn_match)
  set(name CMP0128WarnMatch)
  test_build()
endfunction()

function(test_cmp0128_warn_unset)
  # For compilers that had CMAKE_<LANG>_EXTENSION_COMPILE_OPTION (only IAR)
  # there is no behavioral change and thus no warning.
  if(NOT "${${lang}_EXT_FLAG}" STREQUAL "")
    return()
  endif()

  if(extensions_opposite)
    set(opposite "enabled")
  else()
    set(opposite "disabled")
  endif()

  set(name CMP0128WarnUnset)
  test_build()
endfunction()

function(test_lang lang ext)
  if(CMake_NO_${lang}_STANDARD)
    return()
  endif()

  set(extensions_default "${${lang}_EXTENSIONS_DEFAULT}")
  set(standard_default "${${lang}_STANDARD_DEFAULT}")

  if(extensions_default)
    set(extensions_opposite OFF)
  else()
    set(extensions_opposite ON)
  endif()

  test_cmp0128_new_extensions_opposite()
  test_cmp0128_new_no_unnecessary_flag()
  test_cmp0128_old_same_standard()
  test_cmp0128_warn_match()
  test_cmp0128_warn_unset()
endfunction()

if(C_STANDARD_DEFAULT)
  test_lang(C c)
endif()

if(CXX_STANDARD_DEFAULT)
  run_cmake(NotAStandard)

  foreach(standard 98 11)
    if (CXX${standard}_FLAG STREQUAL NOTFOUND)
      run_cmake(RequireCXX${standard})
      run_cmake(RequireCXX${standard}Variable)
    endif()
    if (CXX${standard}_EXT_FLAG STREQUAL NOTFOUND)
      run_cmake(RequireCXX${standard}Ext)
      run_cmake(RequireCXX${standard}ExtVariable)
    endif()
  endforeach()

  test_lang(CXX cpp)
endif()