HeaderpadWorkaround.cmake 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # Do NOT include this module directly into any of your code. It is used by
  4. # the try_compile() implementation to work around a specific issue with
  5. # conflicting flags when building for Apple platforms.
  6. if(NOT APPLE)
  7. return()
  8. endif()
  9. function(__cmake_internal_workaround_headerpad_flag_conflict _LANG)
  10. # Until we can avoid hard-coding -Wl,-headerpad_max_install_names in the
  11. # linker flags, we need to remove it here for cases where we know it will
  12. # conflict with other flags, generate a warning and be ignored.
  13. set(regex "(^| )(-fembed-bitcode(-marker|=(all|bitcode|marker))?|-bundle_bitcode)($| )")
  14. set(remove_headerpad NO)
  15. # Check arbitrary flags that the user or project has set. These compiler
  16. # flags get added to the linker command line.
  17. if("${CMAKE_${_LANG}_FLAGS}" MATCHES "${regex}")
  18. set(remove_headerpad YES)
  19. endif()
  20. if(NOT remove_headerpad)
  21. get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  22. if(is_multi_config)
  23. # Only one of these config-specific variables will be set by try_compile()
  24. # and the rest will be unset, but we can't easily tell which one is set.
  25. # No harm to just add them all here, empty ones won't add flags to check.
  26. foreach(config IN LISTS CMAKE_CONFIGURATION_TYPES)
  27. if("${CMAKE_${_LANG}_FLAGS_${config}}" MATCHES "${regex}")
  28. set(remove_headerpad YES)
  29. break()
  30. endif()
  31. endforeach()
  32. else()
  33. if("${CMAKE_${_LANG}_FLAGS_${CMAKE_BUILD_TYPE}}" MATCHES "${regex}")
  34. set(remove_headerpad YES)
  35. endif()
  36. endif()
  37. endif()
  38. # The try_compile() command passes compiler flags to check in a way that
  39. # results in them being added to add_definitions(). Those don't end up on
  40. # the linker command line, so we don't need to check them here.
  41. if(remove_headerpad)
  42. foreach(flag IN ITEMS
  43. CMAKE_${_LANG}_LINK_FLAGS
  44. CMAKE_SHARED_LIBRARY_CREATE_${_LANG}_FLAGS
  45. CMAKE_SHARED_MODULE_CREATE_${_LANG}_FLAGS)
  46. string(REPLACE "-Wl,-headerpad_max_install_names" "" ${flag} "${${flag}}")
  47. set(${flag} "${${flag}}" PARENT_SCOPE)
  48. endforeach()
  49. endif()
  50. endfunction()
  51. get_property(__enabled_languages GLOBAL PROPERTY ENABLED_LANGUAGES)
  52. foreach(__lang IN LISTS __enabled_languages)
  53. __cmake_internal_workaround_headerpad_flag_conflict(${__lang})
  54. endforeach()
  55. unset(__lang)
  56. unset(__enabled_languages)