GNU.cmake 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. # This module is shared by multiple linkers; use include blocker.
  4. include_guard()
  5. block(SCOPE_FOR POLICIES)
  6. cmake_policy(SET CMP0140 NEW)
  7. function(__linker_gnu lang)
  8. # define flags for linker depfile generation
  9. set(CMAKE_${lang}_LINKER_DEPFILE_FLAGS "LINKER:--dependency-file=<DEP_FILE>")
  10. set(CMAKE_${lang}_LINKER_DEPFILE_FORMAT gcc)
  11. if(NOT CMAKE_EXECUTABLE_FORMAT STREQUAL "ELF")
  12. # Only ELF binary format supports this capability
  13. set(CMAKE_${lang}_LINKER_DEPFILE_SUPPORTED FALSE)
  14. endif()
  15. if(NOT DEFINED CMAKE_${lang}_LINKER_DEPFILE_SUPPORTED)
  16. ## Ensure ninja tool is recent enough...
  17. if(CMAKE_GENERATOR MATCHES "^Ninja")
  18. # Ninja 1.10 or upper is required
  19. execute_process(COMMAND "${CMAKE_MAKE_PROGRAM}" --version
  20. OUTPUT_VARIABLE _ninja_version
  21. ERROR_VARIABLE _ninja_version)
  22. if (_ninja_version MATCHES "[0-9]+(\\.[0-9]+)*")
  23. set (_ninja_version "${CMAKE_MATCH_0}")
  24. endif()
  25. if (_ninja_version VERSION_LESS "1.10")
  26. set(CMAKE_${lang}_LINKER_DEPFILE_SUPPORTED FALSE)
  27. endif()
  28. endif()
  29. if (NOT DEFINED CMAKE_${lang}_LINKER_DEPFILE_SUPPORTED)
  30. ## check if this feature is supported by the linker
  31. if (CMAKE_${lang}_COMPILER_LINKER AND CMAKE_${lang}_COMPILER_LINKER_ID MATCHES "GNU|LLD|MOLD")
  32. execute_process(COMMAND "${CMAKE_${lang}_COMPILER_LINKER}" --help
  33. OUTPUT_VARIABLE _linker_capabilities
  34. ERROR_VARIABLE _linker_capabilities)
  35. if(_linker_capabilities MATCHES "--dependency-file")
  36. set(CMAKE_${lang}_LINKER_DEPFILE_SUPPORTED TRUE)
  37. else()
  38. set(CMAKE_${lang}_LINKER_DEPFILE_SUPPORTED FALSE)
  39. endif()
  40. else()
  41. set(CMAKE_${lang}_LINKER_DEPFILE_SUPPORTED FALSE)
  42. endif()
  43. endif()
  44. endif()
  45. if (CMAKE_${lang}_LINKER_DEPFILE_SUPPORTED)
  46. set(CMAKE_${lang}_LINK_DEPENDS_USE_LINKER TRUE)
  47. else()
  48. set(CMAKE_${lang}_LINK_DEPENDS_USE_LINKER FALSE)
  49. endif()
  50. # Due to GNU binutils ld bug when LTO is enabled (see GNU bug
  51. # `30568 <https://sourceware.org/bugzilla/show_bug.cgi?id=30568>`_),
  52. # deactivate this feature if the version is less than 2.41.
  53. if (CMAKE_${lang}_COMPILER_LINKER_ID
  54. AND CMAKE_${lang}_COMPILER_LINKER_ID STREQUAL "GNU"
  55. AND CMAKE_${lang}_COMPILER_LINKER_VERSION VERSION_LESS "2.41")
  56. set(CMAKE_${lang}_LINK_DEPENDS_USE_LINKER FALSE)
  57. endif()
  58. # Linker warning as error
  59. set(CMAKE_${lang}_LINK_OPTIONS_WARNING_AS_ERROR "LINKER:--fatal-warnings")
  60. return(PROPAGATE CMAKE_${lang}_LINKER_DEPFILE_FLAGS
  61. CMAKE_${lang}_LINKER_DEPFILE_FORMAT
  62. CMAKE_${lang}_LINKER_DEPFILE_SUPPORTED
  63. CMAKE_${lang}_LINK_DEPENDS_USE_LINKER
  64. CMAKE_${lang}_LINK_OPTIONS_WARNING_AS_ERROR)
  65. endfunction()
  66. endblock()