CheckPIESupported.cmake 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. Supported languages are ``C``, ``CXX``, and ``Fortran``.
  21. It makes no sense to use this module when :policy:`CMP0083` is set to ``OLD``,
  22. so the command will return an error in this case. See policy :policy:`CMP0083`
  23. for details.
  24. Variables
  25. ^^^^^^^^^
  26. For each language checked, two boolean cache variables are defined.
  27. ``CMAKE_<lang>_LINK_PIE_SUPPORTED``
  28. Set to ``YES`` if ``PIE`` is supported by the linker and ``NO`` otherwise.
  29. ``CMAKE_<lang>_LINK_NO_PIE_SUPPORTED``
  30. Set to ``YES`` if ``NO_PIE`` is supported by the linker and ``NO`` otherwise.
  31. Examples
  32. ^^^^^^^^
  33. .. code-block:: cmake
  34. check_pie_supported()
  35. set_property(TARGET foo PROPERTY POSITION_INDEPENDENT_CODE TRUE)
  36. .. code-block:: cmake
  37. # Retrieve any error message.
  38. check_pie_supported(OUTPUT_VARIABLE output LANGUAGES C)
  39. set_property(TARGET foo PROPERTY POSITION_INDEPENDENT_CODE TRUE)
  40. if(NOT CMAKE_C_LINK_PIE_SUPPORTED)
  41. message(WARNING "PIE is not supported at link time: ${output}.\n"
  42. "PIE link options will not be passed to linker.")
  43. endif()
  44. #]=======================================================================]
  45. include (Internal/CMakeTryCompilerOrLinkerFlag)
  46. function (check_pie_supported)
  47. cmake_policy(GET CMP0083 cmp0083)
  48. if (NOT cmp0083)
  49. message(FATAL_ERROR "check_pie_supported: Policy CMP0083 is not set")
  50. endif()
  51. if(cmp0083 STREQUAL "OLD")
  52. message(FATAL_ERROR "check_pie_supported: Policy CMP0083 set to OLD")
  53. endif()
  54. set(optional)
  55. set(one OUTPUT_VARIABLE)
  56. set(multiple LANGUAGES)
  57. cmake_parse_arguments(CHECK_PIE "${optional}" "${one}" "${multiple}" "${ARGN}")
  58. if(CHECK_PIE_UNPARSED_ARGUMENTS)
  59. message(FATAL_ERROR "check_pie_supported: Unparsed arguments: ${CHECK_PIE_UNPARSED_ARGUMENTS}")
  60. endif()
  61. if (CHECK_PIE_LANGUAGES)
  62. set (unsupported_languages "${CHECK_PIE_LANGUAGES}")
  63. list (REMOVE_ITEM unsupported_languages "C" "CXX" "Fortran")
  64. if(unsupported_languages)
  65. message(FATAL_ERROR "check_pie_supported: language(s) '${unsupported_languages}' not supported")
  66. endif()
  67. else()
  68. # User did not set any languages, use defaults
  69. get_property (enabled_languages GLOBAL PROPERTY ENABLED_LANGUAGES)
  70. if (NOT enabled_languages)
  71. return()
  72. endif()
  73. list (FILTER enabled_languages INCLUDE REGEX "^(C|CXX|Fortran)$")
  74. if (NOT enabled_languages)
  75. return()
  76. endif()
  77. set (CHECK_PIE_LANGUAGES ${enabled_languages})
  78. endif()
  79. set (outputs)
  80. foreach(lang IN LISTS CHECK_PIE_LANGUAGES)
  81. if(_CMAKE_${lang}_PIE_MAY_BE_SUPPORTED_BY_LINKER)
  82. cmake_try_compiler_or_linker_flag(${lang}
  83. "${CMAKE_${lang}_LINK_OPTIONS_PIE}"
  84. CMAKE_${lang}_LINK_PIE_SUPPORTED
  85. OUTPUT_VARIABLE output)
  86. if (NOT CMAKE_${lang}_LINK_PIE_SUPPORTED)
  87. string (APPEND outputs "PIE (${lang}): ${output}\n")
  88. endif()
  89. cmake_try_compiler_or_linker_flag(${lang}
  90. "${CMAKE_${lang}_LINK_OPTIONS_NO_PIE}"
  91. CMAKE_${lang}_LINK_NO_PIE_SUPPORTED
  92. OUTPUT_VARIABLE output)
  93. if (NOT CMAKE_${lang}_LINK_NO_PIE_SUPPORTED)
  94. string (APPEND outputs "NO_PIE (${lang}): ${output}\n")
  95. endif()
  96. else()
  97. # no support at link time. Set cache variables to NO
  98. set(CMAKE_${lang}_LINK_PIE_SUPPORTED NO CACHE INTERNAL "PIE (${lang})")
  99. set(CMAKE_${lang}_LINK_NO_PIE_SUPPORTED NO CACHE INTERNAL "NO_PIE (${lang})")
  100. string (APPEND outputs "PIE and NO_PIE are not supported by linker for ${lang}")
  101. endif()
  102. endforeach()
  103. if (CHECK_PIE_OUTPUT_VARIABLE)
  104. set (${CHECK_PIE_OUTPUT_VARIABLE} "${outputs}" PARENT_SCOPE)
  105. endif()
  106. endfunction()