cmake/Modules/CMakeParseImplicitLinkInfo.cmake

188 lines
7.9 KiB
CMake
Raw Normal View History

2016-10-30 18:24:19 +01:00
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
2009-10-04 10:30:41 +03:00
# Function parse implicit linker options.
# This is used internally by CMake and should not be included by user
# code.
2013-03-16 19:13:01 +02:00
function(CMAKE_PARSE_IMPLICIT_LINK_INFO text lib_var dir_var fwk_var log_var obj_regex)
2009-11-14 01:56:15 +02:00
set(implicit_libs_tmp "")
2009-10-04 10:30:41 +03:00
set(implicit_dirs_tmp)
2013-03-16 19:13:01 +02:00
set(implicit_fwks_tmp)
2009-10-11 10:55:36 +03:00
set(log "")
2009-10-04 10:30:41 +03:00
# Parse implicit linker arguments.
set(linker "CMAKE_LINKER-NOTFOUND")
if(CMAKE_LINKER)
get_filename_component(linker ${CMAKE_LINKER} NAME)
2013-11-03 12:27:13 +02:00
string(REGEX REPLACE "([][+.*?()^$])" "\\\\\\1" linker "${linker}")
2009-10-04 10:30:41 +03:00
endif()
2017-07-20 19:35:53 +02:00
set(startfile "CMAKE_LINK_STARTFILE-NOTFOUND")
if(CMAKE_LINK_STARTFILE)
set(startfile "${CMAKE_LINK_STARTFILE}")
endif()
2009-10-11 10:55:36 +03:00
# Construct a regex to match linker lines. It must match both the
# whole line and just the command (argv[0]).
2017-07-20 19:35:53 +02:00
set(linker_regex "^( *|.*[/\\])(${linker}|${startfile}|([^/\\]+-)?ld|collect2)[^/\\]*( |$)")
2016-07-09 11:21:54 +02:00
set(linker_exclude_regex "collect2 version |^[A-Za-z0-9_]+=|/ldfe ")
2016-10-30 18:24:19 +01:00
string(APPEND log " link line regex: [${linker_regex}]\n")
2009-10-04 10:30:41 +03:00
string(REGEX REPLACE "\r?\n" ";" output_lines "${text}")
foreach(line IN LISTS output_lines)
set(cmd)
2011-06-19 15:41:06 +03:00
if("${line}" MATCHES "${linker_regex}" AND
NOT "${line}" MATCHES "${linker_exclude_regex}")
2013-03-16 19:13:01 +02:00
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}")
2016-10-30 18:24:19 +01:00
string(APPEND log " reduced line: [${line}]\n to: [${xline}]\n")
2013-03-16 19:13:01 +02:00
set(line "${xline}")
endif()
endif()
2017-07-20 19:35:53 +02:00
separate_arguments(args NATIVE_COMMAND "${line}")
2009-10-04 10:30:41 +03:00
list(GET args 0 cmd)
endif()
2017-04-14 19:02:05 +02:00
set(is_msvc 0)
2009-10-04 10:30:41 +03:00
if("${cmd}" MATCHES "${linker_regex}")
2016-10-30 18:24:19 +01:00
string(APPEND log " link line: [${line}]\n")
2009-10-04 10:30:41 +03:00
string(REGEX REPLACE ";-([LYz]);" ";-\\1" args "${args}")
2017-04-14 19:02:05 +02:00
set(skip_value_of "")
2009-10-04 10:30:41 +03:00
foreach(arg IN LISTS args)
2017-04-14 19:02:05 +02:00
if(skip_value_of)
string(APPEND log " arg [${arg}] ==> skip value of ${skip_value_of}\n")
set(skip_value_of "")
elseif("${arg}" MATCHES "^-L(.:)?[/\\]")
2009-10-04 10:30:41 +03:00
# Unix search path.
string(REGEX REPLACE "^-L" "" dir "${arg}")
list(APPEND implicit_dirs_tmp ${dir})
2016-10-30 18:24:19 +01:00
string(APPEND log " arg [${arg}] ==> dir [${dir}]\n")
2017-07-20 19:35:53 +02:00
elseif("${arg}" MATCHES "^[-/](LIBPATH|libpath):(.+)")
2017-04-14 19:02:05 +02:00
# MSVC search path.
2017-07-20 19:35:53 +02:00
set(dir "${CMAKE_MATCH_2}")
2017-04-14 19:02:05 +02:00
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")
2015-04-27 22:25:09 +02:00
elseif("${arg}" MATCHES "^-l([^:].*)$")
2009-10-04 10:30:41 +03:00
# Unix library.
2015-04-27 22:25:09 +02:00
set(lib "${CMAKE_MATCH_1}")
2009-11-14 01:56:15 +02:00
list(APPEND implicit_libs_tmp ${lib})
2016-10-30 18:24:19 +01:00
string(APPEND log " arg [${arg}] ==> lib [${lib}]\n")
2009-10-04 10:30:41 +03:00
elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.a$")
# Unix library full path.
2009-11-14 01:56:15 +02:00
list(APPEND implicit_libs_tmp ${arg})
2016-10-30 18:24:19 +01:00
string(APPEND log " arg [${arg}] ==> lib [${arg}]\n")
2017-07-20 19:35:53 +02:00
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")
2011-01-16 11:35:12 +01:00
elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.o$"
AND obj_regex AND "${arg}" MATCHES "${obj_regex}")
# Object file full path.
list(APPEND implicit_libs_tmp ${arg})
2016-10-30 18:24:19 +01:00
string(APPEND log " arg [${arg}] ==> obj [${arg}]\n")
2011-06-19 15:41:06 +03:00
elseif("${arg}" MATCHES "^-Y(P,)?[^0-9]")
# Sun search path ([^0-9] avoids conflict with Mac -Y<num>).
2009-10-04 10:30:41 +03:00
string(REGEX REPLACE "^-Y(P,)?" "" dirs "${arg}")
string(REPLACE ":" ";" dirs "${dirs}")
list(APPEND implicit_dirs_tmp ${dirs})
2016-10-30 18:24:19 +01:00
string(APPEND log " arg [${arg}] ==> dirs [${dirs}]\n")
2009-10-04 10:30:41 +03:00
elseif("${arg}" MATCHES "^-l:")
# HP named library.
2009-11-14 01:56:15 +02:00
list(APPEND implicit_libs_tmp ${arg})
2016-10-30 18:24:19 +01:00
string(APPEND log " arg [${arg}] ==> lib [${arg}]\n")
2009-10-04 10:30:41 +03:00
elseif("${arg}" MATCHES "^-z(all|default|weak)extract")
# Link editor option.
2009-11-14 01:56:15 +02:00
list(APPEND implicit_libs_tmp ${arg})
2016-10-30 18:24:19 +01:00
string(APPEND log " arg [${arg}] ==> opt [${arg}]\n")
2017-04-14 19:02:05 +02:00
elseif("${arg}" STREQUAL "cl.exe")
string(APPEND log " arg [${arg}] ==> recognize MSVC cl\n")
set(is_msvc 1)
2009-10-11 10:55:36 +03:00
else()
2016-10-30 18:24:19 +01:00
string(APPEND log " arg [${arg}] ==> ignore\n")
2009-10-04 10:30:41 +03:00
endif()
endforeach()
break()
2015-04-27 22:25:09 +02:00
elseif("${line}" MATCHES "LPATH(=| is:? *)(.*)$")
2016-10-30 18:24:19 +01:00
string(APPEND log " LPATH line: [${line}]\n")
2009-10-04 10:30:41 +03:00
# HP search path.
2015-04-27 22:25:09 +02:00
string(REPLACE ":" ";" paths "${CMAKE_MATCH_2}")
2009-10-04 10:30:41 +03:00
list(APPEND implicit_dirs_tmp ${paths})
2016-10-30 18:24:19 +01:00
string(APPEND log " dirs [${paths}]\n")
2009-10-11 10:55:36 +03:00
else()
2016-10-30 18:24:19 +01:00
string(APPEND log " ignore line: [${line}]\n")
2009-10-04 10:30:41 +03:00
endif()
endforeach()
2013-03-16 19:13:01 +02:00
# 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}")
2016-10-30 18:24:19 +01:00
string(APPEND log " Library search paths: [${implicit_dirs_match}]\n")
2013-03-16 19:13:01 +02:00
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}")
2016-10-30 18:24:19 +01:00
string(APPEND log " Framework search paths: [${implicit_fwks_match}]\n")
2013-03-16 19:13:01 +02:00
list(APPEND implicit_fwks_tmp ${implicit_fwks_match})
endif()
2009-11-14 01:56:15 +02:00
# Cleanup list of libraries and flags.
# We remove items that are not language-specific.
set(implicit_libs "")
foreach(lib IN LISTS implicit_libs_tmp)
2017-07-20 19:35:53 +02:00
if("x${lib}" MATCHES "^x(crt.*\\.o|System.*|.*libclang_rt.*|msvcrt.*|libvcruntime.*|libucrt.*|libcmt.*)$")
2016-10-30 18:24:19 +01:00
string(APPEND log " remove lib [${lib}]\n")
2013-03-16 19:13:01 +02:00
elseif(IS_ABSOLUTE "${lib}")
get_filename_component(abs "${lib}" ABSOLUTE)
if(NOT "x${lib}" STREQUAL "x${abs}")
2016-10-30 18:24:19 +01:00
string(APPEND log " collapse lib [${lib}] ==> [${abs}]\n")
2013-03-16 19:13:01 +02:00
endif()
list(APPEND implicit_libs "${abs}")
2009-11-14 01:56:15 +02:00
else()
list(APPEND implicit_libs "${lib}")
endif()
endforeach()
2013-03-16 19:13:01 +02:00
# 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()
2016-10-30 18:24:19 +01:00
string(APPEND log " collapse ${desc_${t}} dir [${d}] ==> [${dir}]${msg}\n")
2013-03-16 19:13:01 +02:00
endforeach()
list(REMOVE_DUPLICATES implicit_${t})
2009-10-04 10:30:41 +03:00
endforeach()
2009-10-11 10:55:36 +03:00
# Log results.
2016-10-30 18:24:19 +01:00
string(APPEND log " implicit libs: [${implicit_libs}]\n")
string(APPEND log " implicit dirs: [${implicit_dirs}]\n")
string(APPEND log " implicit fwks: [${implicit_fwks}]\n")
2009-10-11 10:55:36 +03:00
2009-10-04 10:30:41 +03:00
# Return results.
set(${lib_var} "${implicit_libs}" PARENT_SCOPE)
set(${dir_var} "${implicit_dirs}" PARENT_SCOPE)
2013-03-16 19:13:01 +02:00
set(${fwk_var} "${implicit_fwks}" PARENT_SCOPE)
2009-10-11 10:55:36 +03:00
set(${log_var} "${log}" PARENT_SCOPE)
2009-10-04 10:30:41 +03:00
endfunction()