CheckLinkerFlag.cmake 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CheckLinkerFlag
  5. ---------------
  6. .. versionadded:: 3.18
  7. Check whether the compiler supports a given link flag.
  8. .. command:: check_linker_flag
  9. .. code-block:: cmake
  10. check_linker_flag(<lang> <flag> <var>)
  11. Check that the link ``<flag>`` is accepted by the ``<lang>`` compiler without
  12. a diagnostic. Stores the result in an internal cache entry named ``<var>``.
  13. This command temporarily sets the ``CMAKE_REQUIRED_LINK_OPTIONS`` variable
  14. and calls the :command:`check_source_compiles` command from the
  15. :module:`CheckSourceCompiles` module. See that module's documentation
  16. for a listing of variables that can otherwise modify the build.
  17. The underlying implementation relies on the :prop_tgt:`LINK_OPTIONS` property
  18. to check the specified flag. The ``LINKER:`` prefix, as described in the
  19. :command:`target_link_options` command, can be used as well.
  20. A positive result from this check indicates only that the compiler did not
  21. issue a diagnostic message when given the link flag. Whether the flag has any
  22. effect or even a specific one is beyond the scope of this module.
  23. .. note::
  24. Since the :command:`try_compile` command forwards flags from variables
  25. like :variable:`CMAKE_<LANG>_FLAGS`, unknown flags in such variables may
  26. cause a false negative for this check.
  27. #]=======================================================================]
  28. include_guard(GLOBAL)
  29. include(CMakeCheckCompilerFlagCommonPatterns)
  30. function(CHECK_LINKER_FLAG _lang _flag _var)
  31. get_property (_supported_languages GLOBAL PROPERTY ENABLED_LANGUAGES)
  32. if (NOT _lang IN_LIST _supported_languages)
  33. message (SEND_ERROR "check_linker_flag: ${_lang}: unknown language.")
  34. return()
  35. endif()
  36. include (CheckSourceCompiles)
  37. set(CMAKE_REQUIRED_LINK_OPTIONS "${_flag}")
  38. # Normalize locale during test compilation.
  39. set(_locale_vars LC_ALL LC_MESSAGES LANG)
  40. foreach(v IN LISTS _locale_vars)
  41. set(_locale_vars_saved_${v} "$ENV{${v}}")
  42. set(ENV{${v}} C)
  43. endforeach()
  44. if (_lang MATCHES "^(C|CXX)$")
  45. set (_source "int main() { return 0; }")
  46. elseif (_lang STREQUAL "Fortran")
  47. set (_source " program test\n stop\n end program")
  48. elseif (_lang MATCHES "CUDA")
  49. set (_source "__host__ int main() { return 0; }")
  50. elseif (_lang MATCHES "^(OBJC|OBJCXX)$")
  51. set (_source "#ifndef __OBJC__\n# error \"Not an Objective-C++ compiler\"\n#endif\nint main(void) { return 0; }")
  52. else()
  53. message (SEND_ERROR "check_linker_flag: ${_lang}: unsupported language.")
  54. return()
  55. endif()
  56. check_compiler_flag_common_patterns(_common_patterns)
  57. check_source_compiles(${_lang} "${_source}" ${_var} ${_common_patterns})
  58. foreach(v IN LISTS _locale_vars)
  59. set(ENV{${v}} ${_locale_vars_saved_${v}})
  60. endforeach()
  61. set(${_var} "${${_var}}" PARENT_SCOPE)
  62. endfunction()