diff options
author | Craig Scott <craig.scott@crascit.com> | 2022-04-09 11:44:50 (GMT) |
---|---|---|
committer | Craig Scott <craig.scott@crascit.com> | 2022-04-09 11:51:09 (GMT) |
commit | a10fc754a6a12c681f3acc7efa84798f6c2ea799 (patch) | |
tree | d0b61816e25a08037bdcfd65a585a7e89a331a44 | |
parent | c6ee02fc8db2b2881d6f314a37f193c8726b55ba (diff) | |
download | CMake-a10fc754a6a12c681f3acc7efa84798f6c2ea799.zip CMake-a10fc754a6a12c681f3acc7efa84798f6c2ea799.tar.gz CMake-a10fc754a6a12c681f3acc7efa84798f6c2ea799.tar.bz2 |
CheckSourceCompiles: Avoid linker warning with -fembed-bitcode
When the Apple linker sees -headerpad_max_install_names and
bitcode is enabled with a flag like -fembed-bitcode, it issues a warning
and ignores the -headerpad_max_install_names flag. This causes
unrelated compiler and linker flag checks to fail for valid flags.
In f745e0497e (CheckCompilerFlags: Catch linker warning about ignored
flags, 2022-01-03), we started detecting linker warnings, which caused
a regression for projects that were setting -fembed-bitcode in their
CMAKE_CXX_FLAGS or similar. Prevent that regression by removing
the -headerpad_max_install_names linker flag when we know it will
warn and be ignored anyway.
Fixes: #23390
Issue: #23408
-rw-r--r-- | Modules/Internal/HeaderpadWorkaround.cmake | 69 | ||||
-rw-r--r-- | Source/cmCoreTryCompile.cxx | 6 | ||||
-rw-r--r-- | Tests/RunCMake/CheckCompilerFlag/HeaderpadWorkaround.cmake | 18 | ||||
-rw-r--r-- | Tests/RunCMake/CheckCompilerFlag/RunCMakeTest.cmake | 4 |
4 files changed, 97 insertions, 0 deletions
diff --git a/Modules/Internal/HeaderpadWorkaround.cmake b/Modules/Internal/HeaderpadWorkaround.cmake new file mode 100644 index 0000000..9a7f9f5 --- /dev/null +++ b/Modules/Internal/HeaderpadWorkaround.cmake @@ -0,0 +1,69 @@ +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying +# file Copyright.txt or https://cmake.org/licensing for details. + + +# Do NOT include this module directly into any of your code. It is used by +# the try_compile() implementation to work around a specific issue with +# conflicting flags when building for Apple platforms. +if(NOT APPLE) + return() +endif() + +cmake_policy(PUSH) +cmake_policy(SET CMP0054 NEW) # if() quoted variables not dereferenced + +function(__cmake_internal_workaround_headerpad_flag_conflict _LANG) + + # Until we can avoid hard-coding -Wl,-headerpad_max_install_names in the + # linker flags, we need to remove it here for cases where we know it will + # conflict with other flags, generate a warning and be ignored. + set(regex "(^| )(-fembed-bitcode(-marker|=(all|bitcode|marker))?|-bundle_bitcode)($| )") + set(remove_headerpad NO) + + # Check arbitrary flags that the user or project has set. These compiler + # flags get added to the linker command line. + if("${CMAKE_${_LANG}_FLAGS}" MATCHES "${regex}") + set(remove_headerpad YES) + endif() + if(NOT remove_headerpad) + get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) + if(is_multi_config) + # Only one of these config-specific variables will be set by try_compile() + # and the rest will be unset, but we can't easily tell which one is set. + # No harm to just add them all here, empty ones won't add flags to check. + foreach(config IN LISTS CMAKE_CONFIGURATION_TYPES) + if("${CMAKE_${_LANG}_FLAGS_${config}}" MATCHES "${regex}") + set(remove_headerpad YES) + break() + endif() + endforeach() + else() + if("${CMAKE_${_LANG}_FLAGS_${CMAKE_BUILD_TYPE}}" MATCHES "${regex}") + set(remove_headerpad YES) + endif() + endif() + endif() + + # The try_compile() command passes compiler flags to check in a way that + # results in them being added to add_definitions(). Those don't end up on + # the linker command line, so we don't need to check them here. + + if(remove_headerpad) + foreach(flag IN ITEMS + CMAKE_${_LANG}_LINK_FLAGS + CMAKE_SHARED_LIBRARY_CREATE_${_LANG}_FLAGS + CMAKE_SHARED_MODULE_CREATE_${_LANG}_FLAGS) + string(REPLACE "-Wl,-headerpad_max_install_names" "" ${flag} "${${flag}}") + set(${flag} "${${flag}}" PARENT_SCOPE) + endforeach() + endif() +endfunction() + +get_property(__enabled_languages GLOBAL PROPERTY ENABLED_LANGUAGES) +foreach(__lang IN LISTS __enabled_languages) + __cmake_internal_workaround_headerpad_flag_conflict(${__lang}) +endforeach() +unset(__lang) +unset(__enabled_languages) + +cmake_policy(POP) diff --git a/Source/cmCoreTryCompile.cxx b/Source/cmCoreTryCompile.cxx index 84fa897..324ce87 100644 --- a/Source/cmCoreTryCompile.cxx +++ b/Source/cmCoreTryCompile.cxx @@ -831,6 +831,12 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv, ? "NEW" : "OLD"); + // Workaround for -Wl,-headerpad_max_install_names issue until we can avoid + // adding that flag in the platform and compiler language files + fprintf(fout, + "include(\"${CMAKE_ROOT}/Modules/Internal/" + "HeaderpadWorkaround.cmake\")\n"); + if (targetType == cmStateEnums::EXECUTABLE) { /* Put the executable at a known location (for COPY_FILE). */ fprintf(fout, "set(CMAKE_RUNTIME_OUTPUT_DIRECTORY \"%s\")\n", diff --git a/Tests/RunCMake/CheckCompilerFlag/HeaderpadWorkaround.cmake b/Tests/RunCMake/CheckCompilerFlag/HeaderpadWorkaround.cmake new file mode 100644 index 0000000..128f9db --- /dev/null +++ b/Tests/RunCMake/CheckCompilerFlag/HeaderpadWorkaround.cmake @@ -0,0 +1,18 @@ +enable_language(C) + +include(CheckCompilerFlag) + +# Confirm we can check the conflicting flag directly. This should pass with +# or without the workaround. +check_compiler_flag(C "-fembed-bitcode" result1) +if(NOT result1) + message(FATAL_ERROR "False negative when -fembed-bitcode tested directly") +endif() + +# Check conflicting flag set by user or project won't cause a false negative +# when testing a valid flag. This only passes with the workaround. +set(CMAKE_C_FLAGS -fembed-bitcode) +check_compiler_flag(C "-O" result2) +if(NOT result2) + message(FATAL_ERROR "False negative when -fembed-bitcode set in CMAKE_C_FLAGS") +endif() diff --git a/Tests/RunCMake/CheckCompilerFlag/RunCMakeTest.cmake b/Tests/RunCMake/CheckCompilerFlag/RunCMakeTest.cmake index 7ef1860..df2b667 100644 --- a/Tests/RunCMake/CheckCompilerFlag/RunCMakeTest.cmake +++ b/Tests/RunCMake/CheckCompilerFlag/RunCMakeTest.cmake @@ -26,3 +26,7 @@ endif() if(CMake_TEST_HIP) run_cmake(CheckHIPCompilerFlag) endif() + +if(APPLE) + run_cmake_with_options(HeaderpadWorkaround --debug-trycompile) +endif() |