CheckPIESupported.cmake 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. CheckPIESupported
  5. -----------------
  6. .. versionadded:: 3.14
  7. Check whether the linker supports Position Independent Code (PIE) or No
  8. Position Independent Code (NO_PIE) for executables.
  9. Use this to ensure that the :prop_tgt:`POSITION_INDEPENDENT_CODE` target
  10. property for executables will be honored at link time.
  11. .. command:: check_pie_supported
  12. ::
  13. check_pie_supported([OUTPUT_VARIABLE <output>]
  14. [LANGUAGES <lang>...])
  15. Options are:
  16. ``OUTPUT_VARIABLE <output>``
  17. Set ``<output>`` variable with details about any error.
  18. ``LANGUAGES <lang>...``
  19. Check the linkers used for each of the specified languages.
  20. ``C``, ``CXX``, ``Fortran`` are supported.
  21. .. versionadded:: 3.23
  22. ``OBJC``, ``OBJCXX``, ``CUDA``, and ``HIP`` are supported.
  23. It makes no sense to use this module when :policy:`CMP0083` is set to ``OLD``,
  24. so the command will return an error in this case. See policy :policy:`CMP0083`
  25. for details.
  26. Variables
  27. ^^^^^^^^^
  28. For each language checked, two boolean cache variables are defined.
  29. ``CMAKE_<lang>_LINK_PIE_SUPPORTED``
  30. Set to ``YES`` if ``PIE`` is supported by the linker and ``NO`` otherwise.
  31. ``CMAKE_<lang>_LINK_NO_PIE_SUPPORTED``
  32. Set to ``YES`` if ``NO_PIE`` is supported by the linker and ``NO`` otherwise.
  33. Examples
  34. ^^^^^^^^
  35. .. code-block:: cmake
  36. check_pie_supported()
  37. set_property(TARGET foo PROPERTY POSITION_INDEPENDENT_CODE TRUE)
  38. .. code-block:: cmake
  39. # Retrieve any error message.
  40. check_pie_supported(OUTPUT_VARIABLE output LANGUAGES C)
  41. set_property(TARGET foo PROPERTY POSITION_INDEPENDENT_CODE TRUE)
  42. if(NOT CMAKE_C_LINK_PIE_SUPPORTED)
  43. message(WARNING "PIE is not supported at link time: ${output}.\n"
  44. "PIE link options will not be passed to linker.")
  45. endif()
  46. #]=======================================================================]
  47. include (Internal/CheckLinkerFlag)
  48. function (check_pie_supported)
  49. cmake_policy(GET CMP0083 cmp0083)
  50. if (NOT cmp0083)
  51. message(FATAL_ERROR "check_pie_supported: Policy CMP0083 is not set")
  52. endif()
  53. if(cmp0083 STREQUAL "OLD")
  54. message(FATAL_ERROR "check_pie_supported: Policy CMP0083 set to OLD")
  55. endif()
  56. set(optional)
  57. set(one OUTPUT_VARIABLE)
  58. set(multiple LANGUAGES)
  59. cmake_parse_arguments(CHECK_PIE "${optional}" "${one}" "${multiple}" "${ARGN}")
  60. if(CHECK_PIE_UNPARSED_ARGUMENTS)
  61. message(FATAL_ERROR "check_pie_supported: Unparsed arguments: ${CHECK_PIE_UNPARSED_ARGUMENTS}")
  62. endif()
  63. if (CHECK_PIE_LANGUAGES)
  64. set (unsupported_languages "${CHECK_PIE_LANGUAGES}")
  65. list (REMOVE_ITEM unsupported_languages "C" "CXX" "OBJC" "OBJCXX" "Fortran" "CUDA" "HIP")
  66. if(unsupported_languages)
  67. message(FATAL_ERROR "check_pie_supported: language(s) '${unsupported_languages}' not supported")
  68. endif()
  69. else()
  70. # User did not set any languages, use defaults
  71. get_property (enabled_languages GLOBAL PROPERTY ENABLED_LANGUAGES)
  72. if (NOT enabled_languages)
  73. return()
  74. endif()
  75. list (FILTER enabled_languages INCLUDE REGEX "^(C|CXX|OBJC|OBJCXX|Fortran|CUDA|HIP)$")
  76. if (NOT enabled_languages)
  77. return()
  78. endif()
  79. set (CHECK_PIE_LANGUAGES ${enabled_languages})
  80. endif()
  81. set(CMAKE_REQUIRED_QUIET TRUE)
  82. set (outputs)
  83. foreach(lang IN LISTS CHECK_PIE_LANGUAGES)
  84. if(_CMAKE_${lang}_PIE_MAY_BE_SUPPORTED_BY_LINKER)
  85. if(NOT DEFINED CMAKE_${lang}_LINK_PIE_SUPPORTED)
  86. cmake_check_linker_flag(${lang}
  87. "${CMAKE_${lang}_LINK_OPTIONS_PIE}"
  88. CMAKE_${lang}_LINK_PIE_SUPPORTED
  89. OUTPUT_VARIABLE output)
  90. if (NOT CMAKE_${lang}_LINK_PIE_SUPPORTED)
  91. string (APPEND outputs "PIE (${lang}): ${output}\n")
  92. endif()
  93. endif()
  94. if(NOT DEFINED CMAKE_${lang}_LINK_NO_PIE_SUPPORTED)
  95. cmake_check_linker_flag(${lang}
  96. "${CMAKE_${lang}_LINK_OPTIONS_NO_PIE}"
  97. CMAKE_${lang}_LINK_NO_PIE_SUPPORTED
  98. OUTPUT_VARIABLE output)
  99. if (NOT CMAKE_${lang}_LINK_NO_PIE_SUPPORTED)
  100. string (APPEND outputs "NO_PIE (${lang}): ${output}\n")
  101. endif()
  102. endif()
  103. else()
  104. # no support at link time. Set cache variables to NO
  105. set(CMAKE_${lang}_LINK_PIE_SUPPORTED NO CACHE INTERNAL "PIE (${lang})")
  106. set(CMAKE_${lang}_LINK_NO_PIE_SUPPORTED NO CACHE INTERNAL "NO_PIE (${lang})")
  107. string (APPEND outputs "PIE and NO_PIE are not supported by linker for ${lang}")
  108. endif()
  109. endforeach()
  110. if (CHECK_PIE_OUTPUT_VARIABLE)
  111. set (${CHECK_PIE_OUTPUT_VARIABLE} "${outputs}" PARENT_SCOPE)
  112. endif()
  113. endfunction()