CheckCompilerFlag.cmake 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. elseif(_lang STREQUAL "CXX")
  14. set(_lang_src "int main() { return 0; }")
  15. set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for C\\+\\+")
  16. elseif(_lang STREQUAL "CUDA")
  17. set(_lang_src "__host__ int main() { return 0; }")
  18. set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for C\\+\\+" # Host GNU
  19. FAIL_REGEX "argument unused during compilation: .*") # Clang
  20. elseif(_lang STREQUAL "Fortran")
  21. set(_lang_src " program test\n stop\n end program")
  22. set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for Fortran")
  23. elseif(_lang STREQUAL "OBJC")
  24. set(_lang_src [=[
  25. #ifndef __OBJC__
  26. # error "Not an Objective-C compiler"
  27. #endif
  28. int main(void) { return 0; }]=])
  29. set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for Objective-C" # GNU
  30. FAIL_REGEX "argument unused during compilation: .*") # Clang
  31. elseif(_lang STREQUAL "OBJCXX")
  32. set(_lang_src [=[
  33. #ifndef __OBJC__
  34. # error "Not an Objective-C++ compiler"
  35. #endif
  36. int main(void) { return 0; }]=])
  37. set(_lang_fail_regex FAIL_REGEX "command[ -]line option .* is valid for .* but not for Objective-C\\+\\+" # GNU
  38. FAIL_REGEX "argument unused during compilation: .*") # Clang
  39. elseif(_lang STREQUAL "ISPC")
  40. set(_lang_src "float func(uniform int32, float a) { return a / 2.25; }")
  41. else()
  42. message (SEND_ERROR "check_compiler_flag: ${_lang}: unknown language.")
  43. return()
  44. endif()
  45. get_property (_supported_languages GLOBAL PROPERTY ENABLED_LANGUAGES)
  46. if (NOT _lang IN_LIST _supported_languages)
  47. message (SEND_ERROR "check_compiler_flag: ${_lang}: needs to be enabled before use.")
  48. return()
  49. endif()
  50. set(CMAKE_REQUIRED_DEFINITIONS ${_flag})
  51. # Normalize locale during test compilation.
  52. set(_locale_vars LC_ALL LC_MESSAGES LANG)
  53. foreach(v IN LISTS _locale_vars)
  54. set(_locale_vars_saved_${v} "$ENV{${v}}")
  55. set(ENV{${v}} C)
  56. endforeach()
  57. check_compiler_flag_common_patterns(_common_patterns)
  58. cmake_check_source_compiles(${_lang}
  59. "${_lang_src}"
  60. ${_var}
  61. ${_lang_fail_regex}
  62. ${_common_patterns}
  63. )
  64. foreach(v IN LISTS _locale_vars)
  65. set(ENV{${v}} ${_locale_vars_saved_${v}})
  66. endforeach()
  67. set(${_var} "${${_var}}" PARENT_SCOPE)
  68. endfunction ()
  69. cmake_policy(POP)