CMake/Modules/CMakeParseImplicitLinkInfo.cmake
Christian Pfeiffer 41aacca7fb Restore exclusion of "gcc_eh" from implicit link libraries
Since commit v3.9.0-rc1~148^2 (Do not assume GCC libs are linked by all
compilers, 2017-05-05) we no longer filter out all `gcc*` implicit link
libraries.  This allows mixing of gcc and non-gcc compilers across
languages.  However, this caused a subtle problem with how GCC makes
exception handling symbols available to linked binaries.

GCC (at least on MinGW) provides two different libraries with exception
handling symbols:

* gcc_s: A shared library with -fvisibility=default, used by -shared-libgcc.
* gcc_eh: A static library with -fvisibility=hidden, used by -static-libgcc.

The C compiler (on MinGW) defaults to -static-libgcc and uses gcc_eh.
The C++ compiler defaults to -shared-libgcc and uses gcc_s when linking
shared libraries and executables so that exceptions can propagate across
shared libraries [1].  When linking a mixed-language binary, the C++
compiler should be used along with its choice of gcc_s.  In this case
gcc_eh should not be added even though the C compiler implies it because
gcc_s supersedes it.

Since the above-mentioned change, CMake is adding gcc_eh to C++ link
lines that also contain C code on MinGW.  This causes both gcc_s and
gcc_eh to be used, which is incorrect.  We can fix this simply by
excluding gcc_eh from the C compiler's implicit link libraries.

[1] https://gcc.gnu.org/onlinedocs/gcc-7.2.0/gcc/Link-Options.html#Link-Options

Fixes: #17436
2017-11-08 08:10:52 -05:00

188 lines
7.9 KiB
CMake

# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
# Function parse implicit linker options.
# This is used internally by CMake and should not be included by user
# code.
function(CMAKE_PARSE_IMPLICIT_LINK_INFO text lib_var dir_var fwk_var log_var obj_regex)
set(implicit_libs_tmp "")
set(implicit_dirs_tmp)
set(implicit_fwks_tmp)
set(log "")
# Parse implicit linker arguments.
set(linker "CMAKE_LINKER-NOTFOUND")
if(CMAKE_LINKER)
get_filename_component(linker ${CMAKE_LINKER} NAME)
string(REGEX REPLACE "([][+.*?()^$])" "\\\\\\1" linker "${linker}")
endif()
set(startfile "CMAKE_LINK_STARTFILE-NOTFOUND")
if(CMAKE_LINK_STARTFILE)
set(startfile "${CMAKE_LINK_STARTFILE}")
endif()
# Construct a regex to match linker lines. It must match both the
# whole line and just the command (argv[0]).
set(linker_regex "^( *|.*[/\\])(${linker}|${startfile}|([^/\\]+-)?ld|collect2)[^/\\]*( |$)")
set(linker_exclude_regex "collect2 version |^[A-Za-z0-9_]+=|/ldfe ")
string(APPEND log " link line regex: [${linker_regex}]\n")
string(REGEX REPLACE "\r?\n" ";" output_lines "${text}")
foreach(line IN LISTS output_lines)
set(cmd)
if("${line}" MATCHES "${linker_regex}" AND
NOT "${line}" MATCHES "${linker_exclude_regex}")
if(XCODE)
# Xcode unconditionally adds a path under the project build tree and
# on older versions it is not reported with proper quotes. Remove it.
string(REGEX REPLACE "([][+.*()^])" "\\\\\\1" _dir_regex "${CMAKE_BINARY_DIR}")
string(REGEX REPLACE " -[FL]${_dir_regex}/([^ ]| [^-])+( |$)" " " xline "${line}")
if(NOT "x${xline}" STREQUAL "x${line}")
string(APPEND log " reduced line: [${line}]\n to: [${xline}]\n")
set(line "${xline}")
endif()
endif()
separate_arguments(args NATIVE_COMMAND "${line}")
list(GET args 0 cmd)
endif()
set(is_msvc 0)
if("${cmd}" MATCHES "${linker_regex}")
string(APPEND log " link line: [${line}]\n")
string(REGEX REPLACE ";-([LYz]);" ";-\\1" args "${args}")
set(skip_value_of "")
foreach(arg IN LISTS args)
if(skip_value_of)
string(APPEND log " arg [${arg}] ==> skip value of ${skip_value_of}\n")
set(skip_value_of "")
elseif("${arg}" MATCHES "^-L(.:)?[/\\]")
# Unix search path.
string(REGEX REPLACE "^-L" "" dir "${arg}")
list(APPEND implicit_dirs_tmp ${dir})
string(APPEND log " arg [${arg}] ==> dir [${dir}]\n")
elseif("${arg}" MATCHES "^[-/](LIBPATH|libpath):(.+)")
# MSVC search path.
set(dir "${CMAKE_MATCH_2}")
list(APPEND implicit_dirs_tmp ${dir})
string(APPEND log " arg [${arg}] ==> dir [${dir}]\n")
elseif(is_msvc AND "${arg}" STREQUAL "-link")
string(APPEND log " arg [${arg}] ==> ignore MSVC cl option\n")
elseif(is_msvc AND "${arg}" MATCHES "^(.*\\.[Ll][Ii][Bb])$")
set(lib "${CMAKE_MATCH_1}")
list(APPEND implicit_libs_tmp ${lib})
string(APPEND log " arg [${arg}] ==> lib [${lib}]\n")
elseif("${arg}" STREQUAL "-lto_library")
# ld argument "-lto_library <path>"
set(skip_value_of "${arg}")
string(APPEND log " arg [${arg}] ==> ignore, skip following value\n")
elseif("${arg}" MATCHES "^-l([^:].*)$")
# Unix library.
set(lib "${CMAKE_MATCH_1}")
list(APPEND implicit_libs_tmp ${lib})
string(APPEND log " arg [${arg}] ==> lib [${lib}]\n")
elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.a$")
# Unix library full path.
list(APPEND implicit_libs_tmp ${arg})
string(APPEND log " arg [${arg}] ==> lib [${arg}]\n")
elseif("${arg}" MATCHES "^[-/](DEFAULTLIB|defaultlib):(.+)")
# Windows library.
set(lib "${CMAKE_MATCH_2}")
list(APPEND implicit_libs_tmp ${lib})
string(APPEND log " arg [${arg}] ==> lib [${lib}]\n")
elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.o$"
AND obj_regex AND "${arg}" MATCHES "${obj_regex}")
# Object file full path.
list(APPEND implicit_libs_tmp ${arg})
string(APPEND log " arg [${arg}] ==> obj [${arg}]\n")
elseif("${arg}" MATCHES "^-Y(P,)?[^0-9]")
# Sun search path ([^0-9] avoids conflict with Mac -Y<num>).
string(REGEX REPLACE "^-Y(P,)?" "" dirs "${arg}")
string(REPLACE ":" ";" dirs "${dirs}")
list(APPEND implicit_dirs_tmp ${dirs})
string(APPEND log " arg [${arg}] ==> dirs [${dirs}]\n")
elseif("${arg}" MATCHES "^-l:")
# HP named library.
list(APPEND implicit_libs_tmp ${arg})
string(APPEND log " arg [${arg}] ==> lib [${arg}]\n")
elseif("${arg}" MATCHES "^-z(all|default|weak)extract")
# Link editor option.
list(APPEND implicit_libs_tmp ${arg})
string(APPEND log " arg [${arg}] ==> opt [${arg}]\n")
elseif("${arg}" STREQUAL "cl.exe")
string(APPEND log " arg [${arg}] ==> recognize MSVC cl\n")
set(is_msvc 1)
else()
string(APPEND log " arg [${arg}] ==> ignore\n")
endif()
endforeach()
break()
elseif("${line}" MATCHES "LPATH(=| is:? *)(.*)$")
string(APPEND log " LPATH line: [${line}]\n")
# HP search path.
string(REPLACE ":" ";" paths "${CMAKE_MATCH_2}")
list(APPEND implicit_dirs_tmp ${paths})
string(APPEND log " dirs [${paths}]\n")
else()
string(APPEND log " ignore line: [${line}]\n")
endif()
endforeach()
# Look for library search paths reported by linker.
if("${output_lines}" MATCHES ";Library search paths:((;\t[^;]+)+)")
string(REPLACE ";\t" ";" implicit_dirs_match "${CMAKE_MATCH_1}")
string(APPEND log " Library search paths: [${implicit_dirs_match}]\n")
list(APPEND implicit_dirs_tmp ${implicit_dirs_match})
endif()
if("${output_lines}" MATCHES ";Framework search paths:((;\t[^;]+)+)")
string(REPLACE ";\t" ";" implicit_fwks_match "${CMAKE_MATCH_1}")
string(APPEND log " Framework search paths: [${implicit_fwks_match}]\n")
list(APPEND implicit_fwks_tmp ${implicit_fwks_match})
endif()
# Cleanup list of libraries and flags.
# We remove items that are not language-specific.
set(implicit_libs "")
foreach(lib IN LISTS implicit_libs_tmp)
if("x${lib}" MATCHES "^x(crt.*\\.o|gcc_eh.*|System.*|.*libclang_rt.*|msvcrt.*|libvcruntime.*|libucrt.*|libcmt.*)$")
string(APPEND log " remove lib [${lib}]\n")
elseif(IS_ABSOLUTE "${lib}")
get_filename_component(abs "${lib}" ABSOLUTE)
if(NOT "x${lib}" STREQUAL "x${abs}")
string(APPEND log " collapse lib [${lib}] ==> [${abs}]\n")
endif()
list(APPEND implicit_libs "${abs}")
else()
list(APPEND implicit_libs "${lib}")
endif()
endforeach()
# Cleanup list of library and framework directories.
set(desc_dirs "library")
set(desc_fwks "framework")
foreach(t dirs fwks)
set(implicit_${t} "")
foreach(d IN LISTS implicit_${t}_tmp)
get_filename_component(dir "${d}" ABSOLUTE)
string(FIND "${dir}" "${CMAKE_FILES_DIRECTORY}/" pos)
if(NOT pos LESS 0)
set(msg ", skipping non-system directory")
else()
set(msg "")
list(APPEND implicit_${t} "${dir}")
endif()
string(APPEND log " collapse ${desc_${t}} dir [${d}] ==> [${dir}]${msg}\n")
endforeach()
list(REMOVE_DUPLICATES implicit_${t})
endforeach()
# Log results.
string(APPEND log " implicit libs: [${implicit_libs}]\n")
string(APPEND log " implicit dirs: [${implicit_dirs}]\n")
string(APPEND log " implicit fwks: [${implicit_fwks}]\n")
# Return results.
set(${lib_var} "${implicit_libs}" PARENT_SCOPE)
set(${dir_var} "${implicit_dirs}" PARENT_SCOPE)
set(${fwk_var} "${implicit_fwks}" PARENT_SCOPE)
set(${log_var} "${log}" PARENT_SCOPE)
endfunction()