1# Distributed under the OSI-approved BSD 3-Clause License. See accompanying 2# file Copyright.txt or https://cmake.org/licensing for details. 3 4cmake_policy(PUSH) 5cmake_policy(SET CMP0053 NEW) 6cmake_policy(SET CMP0054 NEW) 7 8# Function to parse implicit linker options. 9# 10# This is used internally by CMake and should not be included by user 11# code. 12# 13# Note: this function is leaked/exposed by FindOpenMP and therefore needs 14# to have a stable API so projects that copied `FindOpenMP` for backwards 15# compatibility don't break. 16# 17function(CMAKE_PARSE_IMPLICIT_LINK_INFO text lib_var dir_var fwk_var log_var obj_regex) 18 set(implicit_libs_tmp "") 19 set(implicit_objs_tmp "") 20 set(implicit_dirs_tmp) 21 set(implicit_fwks_tmp) 22 set(log "") 23 24 set(keywordArgs) 25 set(oneValueArgs COMPUTE_IMPLICIT_OBJECTS) 26 set(multiValueArgs ) 27 cmake_parse_arguments(EXTRA_PARSE "${keywordArgs}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) 28 29 # Parse implicit linker arguments. 30 set(linker "CMAKE_LINKER-NOTFOUND") 31 if(CMAKE_LINKER) 32 get_filename_component(linker ${CMAKE_LINKER} NAME) 33 string(REGEX REPLACE "([][+.*?()^$])" "\\\\\\1" linker "${linker}") 34 endif() 35 set(startfile "CMAKE_LINK_STARTFILE-NOTFOUND") 36 if(CMAKE_LINK_STARTFILE) 37 set(startfile "${CMAKE_LINK_STARTFILE}") 38 endif() 39 # Construct a regex to match linker lines. It must match both the 40 # whole line and just the command (argv[0]). 41 set(linker_regex "^( *|.*[/\\])(${linker}|${startfile}|([^/\\]+-)?ld|collect2)[^/\\]*( |$)") 42 set(linker_exclude_regex "collect2 version |^[A-Za-z0-9_]+=|/ldfe ") 43 string(APPEND log " link line regex: [${linker_regex}]\n") 44 string(REGEX REPLACE "\r?\n" ";" output_lines "${text}") 45 foreach(line IN LISTS output_lines) 46 set(cmd) 47 if("${line}" MATCHES "${linker_regex}" AND 48 NOT "${line}" MATCHES "${linker_exclude_regex}") 49 if(XCODE) 50 # Xcode unconditionally adds a path under the project build tree and 51 # on older versions it is not reported with proper quotes. Remove it. 52 string(REGEX REPLACE "([][+.*()^])" "\\\\\\1" _dir_regex "${CMAKE_BINARY_DIR}") 53 string(REGEX REPLACE " -[FL]${_dir_regex}/([^ ]| [^-])+( |$)" " " xline "${line}") 54 if(NOT "x${xline}" STREQUAL "x${line}") 55 string(APPEND log " reduced line: [${line}]\n to: [${xline}]\n") 56 set(line "${xline}") 57 endif() 58 endif() 59 separate_arguments(args NATIVE_COMMAND "${line}") 60 list(GET args 0 cmd) 61 else() 62 #check to see if the link line is comma-separated instead of space separated 63 string(REGEX REPLACE "," " " line "${line}") 64 if("${line}" MATCHES "${linker_regex}" AND 65 NOT "${line}" MATCHES "${linker_exclude_regex}") 66 separate_arguments(args NATIVE_COMMAND "${line}") 67 list(GET args 0 cmd) 68 if("${cmd}" MATCHES "exec:") 69 # ibm xl sometimes has 'exec: ' in-front of the linker 70 list(GET args 1 cmd) 71 endif() 72 endif() 73 endif() 74 set(is_msvc 0) 75 set(search_static 0) 76 if("${cmd}" MATCHES "${linker_regex}") 77 string(APPEND log " link line: [${line}]\n") 78 string(REGEX REPLACE ";-([LYz]);" ";-\\1" args "${args}") 79 set(skip_value_of "") 80 foreach(arg IN LISTS args) 81 if(skip_value_of) 82 string(APPEND log " arg [${arg}] ==> skip value of ${skip_value_of}\n") 83 set(skip_value_of "") 84 elseif("${arg}" MATCHES "^-L(.:)?[/\\]") 85 # Unix search path. 86 string(REGEX REPLACE "^-L" "" dir "${arg}") 87 list(APPEND implicit_dirs_tmp ${dir}) 88 string(APPEND log " arg [${arg}] ==> dir [${dir}]\n") 89 elseif("${arg}" MATCHES "^[-/](LIBPATH|libpath):(.+)") 90 # MSVC search path. 91 set(dir "${CMAKE_MATCH_2}") 92 list(APPEND implicit_dirs_tmp ${dir}) 93 string(APPEND log " arg [${arg}] ==> dir [${dir}]\n") 94 elseif(is_msvc AND "${arg}" STREQUAL "-link") 95 string(APPEND log " arg [${arg}] ==> ignore MSVC cl option\n") 96 elseif(is_msvc AND "${arg}" MATCHES "^(.*\\.[Ll][Ii][Bb])$") 97 set(lib "${CMAKE_MATCH_1}") 98 list(APPEND implicit_libs_tmp ${lib}) 99 string(APPEND log " arg [${arg}] ==> lib [${lib}]\n") 100 elseif("${arg}" STREQUAL "-lto_library") 101 # ld argument "-lto_library <path>" 102 set(skip_value_of "${arg}") 103 string(APPEND log " arg [${arg}] ==> ignore, skip following value\n") 104 elseif("${arg}" MATCHES "^-l([^:].*)$") 105 # Unix library. 106 set(lib "${CMAKE_MATCH_1}") 107 if(search_static AND lib MATCHES "^(gfortran|stdc\\+\\+)$") 108 # Search for the static library later, once all link dirs are known. 109 set(lib "SEARCH_STATIC:${lib}") 110 endif() 111 list(APPEND implicit_libs_tmp ${lib}) 112 string(APPEND log " arg [${arg}] ==> lib [${lib}]\n") 113 elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.a$") 114 # Unix library full path. 115 list(APPEND implicit_libs_tmp ${arg}) 116 string(APPEND log " arg [${arg}] ==> lib [${arg}]\n") 117 elseif("${arg}" MATCHES "^[-/](DEFAULTLIB|defaultlib):(.+)") 118 # Windows library. 119 set(lib "${CMAKE_MATCH_2}") 120 list(APPEND implicit_libs_tmp ${lib}) 121 string(APPEND log " arg [${arg}] ==> lib [${lib}]\n") 122 elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.o$") 123 if(EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS) 124 list(APPEND implicit_objs_tmp ${arg}) 125 string(APPEND log " arg [${arg}] ==> obj [${arg}]\n") 126 endif() 127 if(obj_regex AND "${arg}" MATCHES "${obj_regex}") 128 # Object file full path. 129 list(APPEND implicit_libs_tmp ${arg}) 130 endif() 131 elseif("${arg}" MATCHES "^-Y(P,)?[^0-9]") 132 # Sun search path ([^0-9] avoids conflict with Mac -Y<num>). 133 string(REGEX REPLACE "^-Y(P,)?" "" dirs "${arg}") 134 string(REPLACE ":" ";" dirs "${dirs}") 135 list(APPEND implicit_dirs_tmp ${dirs}) 136 string(APPEND log " arg [${arg}] ==> dirs [${dirs}]\n") 137 elseif("${arg}" STREQUAL "-Bstatic") 138 set(search_static 1) 139 string(APPEND log " arg [${arg}] ==> search static\n" ) 140 elseif("${arg}" STREQUAL "-Bdynamic") 141 set(search_static 0) 142 string(APPEND log " arg [${arg}] ==> search dynamic\n" ) 143 elseif("${arg}" MATCHES "^-l:") 144 # HP named library. 145 list(APPEND implicit_libs_tmp ${arg}) 146 string(APPEND log " arg [${arg}] ==> lib [${arg}]\n") 147 elseif("${arg}" MATCHES "^-z(all|default|weak)extract") 148 # Link editor option. 149 list(APPEND implicit_libs_tmp ${arg}) 150 string(APPEND log " arg [${arg}] ==> opt [${arg}]\n") 151 elseif("${arg}" STREQUAL "cl.exe") 152 string(APPEND log " arg [${arg}] ==> recognize MSVC cl\n") 153 set(is_msvc 1) 154 else() 155 string(APPEND log " arg [${arg}] ==> ignore\n") 156 endif() 157 endforeach() 158 break() 159 elseif("${line}" MATCHES "LPATH(=| is:? *)(.*)$") 160 string(APPEND log " LPATH line: [${line}]\n") 161 # HP search path. 162 string(REPLACE ":" ";" paths "${CMAKE_MATCH_2}") 163 list(APPEND implicit_dirs_tmp ${paths}) 164 string(APPEND log " dirs [${paths}]\n") 165 else() 166 string(APPEND log " ignore line: [${line}]\n") 167 endif() 168 endforeach() 169 170 # Look for library search paths reported by linker. 171 if("${output_lines}" MATCHES ";Library search paths:((;\t[^;]+)+)") 172 string(REPLACE ";\t" ";" implicit_dirs_match "${CMAKE_MATCH_1}") 173 string(APPEND log " Library search paths: [${implicit_dirs_match}]\n") 174 list(APPEND implicit_dirs_tmp ${implicit_dirs_match}) 175 endif() 176 if("${output_lines}" MATCHES ";Framework search paths:((;\t[^;]+)+)") 177 string(REPLACE ";\t" ";" implicit_fwks_match "${CMAKE_MATCH_1}") 178 string(APPEND log " Framework search paths: [${implicit_fwks_match}]\n") 179 list(APPEND implicit_fwks_tmp ${implicit_fwks_match}) 180 endif() 181 182 # Cleanup list of libraries and flags. 183 # We remove items that are not language-specific. 184 set(implicit_libs "") 185 foreach(lib IN LISTS implicit_libs_tmp) 186 if("x${lib}" MATCHES "^xSEARCH_STATIC:(.*)") 187 set(search_static 1) 188 set(lib "${CMAKE_MATCH_1}") 189 else() 190 set(search_static 0) 191 endif() 192 if("x${lib}" MATCHES "^x(crt.*\\.o|gcc_eh.*|.*libgcc_eh.*|System.*|.*libclang_rt.*|msvcrt.*|libvcruntime.*|libucrt.*|libcmt.*)$") 193 string(APPEND log " remove lib [${lib}]\n") 194 elseif(search_static) 195 # This library appears after a -Bstatic flag. Due to ordering 196 # and filtering for mixed-language link lines, we do not preserve 197 # the -Bstatic flag itself. Instead, use an absolute path. 198 # Search using a temporary variable with a distinct name 199 # so that our test suite does not depend on disk content. 200 find_library("CMAKE_${lang}_IMPLICIT_LINK_LIBRARY_${lib}" NO_CACHE NAMES "lib${lib}.a" NO_DEFAULT_PATH PATHS ${implicit_dirs_tmp}) 201 set(_lib_static "${CMAKE_${lang}_IMPLICIT_LINK_LIBRARY_${lib}}") 202 if(_lib_static) 203 string(APPEND log " search lib [SEARCH_STATIC:${lib}] ==> [${_lib_static}]\n") 204 list(APPEND implicit_libs "${_lib_static}") 205 else() 206 string(APPEND log " search lib [SEARCH_STATIC:${lib}] ==> [${lib}]\n") 207 list(APPEND implicit_libs "${lib}") 208 endif() 209 elseif(IS_ABSOLUTE "${lib}") 210 get_filename_component(abs "${lib}" ABSOLUTE) 211 if(NOT "x${lib}" STREQUAL "x${abs}") 212 string(APPEND log " collapse lib [${lib}] ==> [${abs}]\n") 213 endif() 214 list(APPEND implicit_libs "${abs}") 215 else() 216 list(APPEND implicit_libs "${lib}") 217 endif() 218 endforeach() 219 220 if(EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS) 221 set(implicit_objs "") 222 foreach(obj IN LISTS implicit_objs_tmp) 223 if(IS_ABSOLUTE "${obj}") 224 get_filename_component(abs "${obj}" ABSOLUTE) 225 if(NOT "x${obj}" STREQUAL "x${abs}") 226 string(APPEND log " collapse obj [${obj}] ==> [${abs}]\n") 227 endif() 228 list(APPEND implicit_objs "${abs}") 229 else() 230 list(APPEND implicit_objs "${obj}") 231 endif() 232 endforeach() 233 endif() 234 235 # Cleanup list of library and framework directories. 236 set(desc_dirs "library") 237 set(desc_fwks "framework") 238 foreach(t dirs fwks) 239 set(implicit_${t} "") 240 foreach(d IN LISTS implicit_${t}_tmp) 241 get_filename_component(dir "${d}" ABSOLUTE) 242 string(FIND "${dir}" "${CMAKE_FILES_DIRECTORY}/" pos) 243 if(NOT pos LESS 0) 244 set(msg ", skipping non-system directory") 245 else() 246 set(msg "") 247 list(APPEND implicit_${t} "${dir}") 248 endif() 249 string(APPEND log " collapse ${desc_${t}} dir [${d}] ==> [${dir}]${msg}\n") 250 endforeach() 251 list(REMOVE_DUPLICATES implicit_${t}) 252 endforeach() 253 254 # Log results. 255 string(APPEND log " implicit libs: [${implicit_libs}]\n") 256 string(APPEND log " implicit objs: [${implicit_objs}]\n") 257 string(APPEND log " implicit dirs: [${implicit_dirs}]\n") 258 string(APPEND log " implicit fwks: [${implicit_fwks}]\n") 259 260 # Return results. 261 set(${lib_var} "${implicit_libs}" PARENT_SCOPE) 262 set(${dir_var} "${implicit_dirs}" PARENT_SCOPE) 263 set(${fwk_var} "${implicit_fwks}" PARENT_SCOPE) 264 set(${log_var} "${log}" PARENT_SCOPE) 265 266 if(EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS) 267 set(${EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS} "${implicit_objs}" PARENT_SCOPE) 268 endif() 269endfunction() 270 271cmake_policy(POP) 272