CheckCompilerFlag.cmake 3.3 KB

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