1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4include_guard(GLOBAL)
5include(Internal/CheckSourceCompiles)
6include(CMakeCheckCompilerFlagCommonPatterns)
7
8cmake_policy(PUSH)
9cmake_policy(SET CMP0054 NEW) # if() quoted variables not dereferenced
10cmake_policy(SET CMP0057 NEW) # if() supports IN_LIST
11
12function(CMAKE_CHECK_COMPILER_FLAG _lang _flag _var)
13
14  if(_lang STREQUAL "C")
15    set(_lang_src "int main(void) { return 0; }")
16    set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for C"
17                         FAIL_REGEX "-Werror=.* argument .* is not valid for C")
18  elseif(_lang STREQUAL "CXX")
19    set(_lang_src "int main() { return 0; }")
20    set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for C\\+\\+"
21                         FAIL_REGEX "-Werror=.* argument .* is not valid for C\\+\\+")
22  elseif(_lang STREQUAL "CUDA")
23    set(_lang_src "__host__ int main() { return 0; }")
24    set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for C\\+\\+" # Host GNU
25                         FAIL_REGEX "argument unused during compilation: .*") # Clang
26  elseif(_lang STREQUAL "Fortran")
27    set(_lang_src "       program test\n       stop\n       end program")
28    set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for Fortran")
29  elseif(_lang STREQUAL "HIP")
30    set(_lang_src "__host__ int main() { return 0; }")
31    set(_lang_fail_regex FAIL_REGEX "argument unused during compilation: .*") # Clang
32  elseif(_lang STREQUAL "OBJC")
33    set(_lang_src [=[
34#ifndef __OBJC__
35#  error "Not an Objective-C compiler"
36#endif
37int main(void) { return 0; }]=])
38    set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for Objective-C" # GNU
39                         FAIL_REGEX "argument unused during compilation: .*") # Clang
40  elseif(_lang STREQUAL "OBJCXX")
41    set(_lang_src [=[
42#ifndef __OBJC__
43#  error "Not an Objective-C++ compiler"
44#endif
45int main(void) { return 0; }]=])
46    set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for Objective-C\\+\\+" # GNU
47                         FAIL_REGEX "argument unused during compilation: .*") # Clang
48  elseif(_lang STREQUAL "ISPC")
49    set(_lang_src "float func(uniform int32, float a) { return a / 2.25; }")
50  else()
51    message (SEND_ERROR "check_compiler_flag: ${_lang}: unknown language.")
52    return()
53  endif()
54
55  get_property (_supported_languages GLOBAL PROPERTY ENABLED_LANGUAGES)
56  if (NOT _lang IN_LIST _supported_languages)
57    message (SEND_ERROR "check_compiler_flag: ${_lang}: needs to be enabled before use.")
58    return()
59  endif()
60
61  set(CMAKE_REQUIRED_DEFINITIONS ${_flag})
62
63  # Normalize locale during test compilation.
64  set(_locale_vars LC_ALL LC_MESSAGES LANG)
65  foreach(v IN LISTS _locale_vars)
66    set(_locale_vars_saved_${v} "$ENV{${v}}")
67    set(ENV{${v}} C)
68  endforeach()
69
70  check_compiler_flag_common_patterns(_common_patterns)
71  cmake_check_source_compiles(${_lang}
72    "${_lang_src}"
73    ${_var}
74    ${_lang_fail_regex}
75    ${_common_patterns}
76    )
77
78  foreach(v IN LISTS _locale_vars)
79    set(ENV{${v}} ${_locale_vars_saved_${v}})
80  endforeach()
81endfunction ()
82
83cmake_policy(POP)
84