1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
5 cmake_policy(SET CMP0053 NEW)
6 cmake_policy(SET CMP0054 NEW)
8 # Function to parse implicit linker options.
10 # This is used internally by CMake and should not be included by user
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.
17 function(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)
25 set(oneValueArgs COMPUTE_IMPLICIT_OBJECTS)
27 cmake_parse_arguments(EXTRA_PARSE "${keywordArgs}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
29 # Parse implicit linker arguments.
30 set(linker "CMAKE_LINKER-NOTFOUND")
32 get_filename_component(linker ${CMAKE_LINKER} NAME)
33 string(REGEX REPLACE "([][+.*?()^$])" "\\\\\\1" linker "${linker}")
35 set(startfile "CMAKE_LINK_STARTFILE-NOTFOUND")
36 if(CMAKE_LINK_STARTFILE)
37 set(startfile "${CMAKE_LINK_STARTFILE}")
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)
47 if("${line}" MATCHES "${linker_regex}" AND
48 NOT "${line}" MATCHES "${linker_exclude_regex}")
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")
59 separate_arguments(args NATIVE_COMMAND "${line}")
61 if("${cmd}" MATCHES "->")
62 # LCC has '-> ' in-front of the linker
66 #check to see if the link line is comma-separated instead of space separated
67 string(REGEX REPLACE "," " " line "${line}")
68 if("${line}" MATCHES "${linker_regex}" AND
69 NOT "${line}" MATCHES "${linker_exclude_regex}")
70 separate_arguments(args NATIVE_COMMAND "${line}")
72 if("${cmd}" MATCHES "exec:")
73 # ibm xl sometimes has 'exec: ' in-front of the linker
80 if("${cmd}" MATCHES "${linker_regex}")
81 string(APPEND log " link line: [${line}]\n")
82 string(REGEX REPLACE ";-([LYz]);" ";-\\1" args "${args}")
84 foreach(arg IN LISTS args)
86 string(APPEND log " arg [${arg}] ==> skip value of ${skip_value_of}\n")
88 elseif("${arg}" MATCHES "^-L(.:)?[/\\]")
90 string(REGEX REPLACE "^-L" "" dir "${arg}")
91 list(APPEND implicit_dirs_tmp ${dir})
92 string(APPEND log " arg [${arg}] ==> dir [${dir}]\n")
93 elseif("${arg}" MATCHES "^[-/](LIBPATH|libpath):(.+)")
95 set(dir "${CMAKE_MATCH_2}")
96 list(APPEND implicit_dirs_tmp ${dir})
97 string(APPEND log " arg [${arg}] ==> dir [${dir}]\n")
98 elseif(is_msvc AND "${arg}" STREQUAL "-link")
99 string(APPEND log " arg [${arg}] ==> ignore MSVC cl option\n")
100 elseif(is_msvc AND "${arg}" MATCHES "^(.*\\.[Ll][Ii][Bb])$")
101 set(lib "${CMAKE_MATCH_1}")
102 list(APPEND implicit_libs_tmp ${lib})
103 string(APPEND log " arg [${arg}] ==> lib [${lib}]\n")
104 elseif("${arg}" STREQUAL "-lto_library")
105 # ld argument "-lto_library <path>"
106 set(skip_value_of "${arg}")
107 string(APPEND log " arg [${arg}] ==> ignore, skip following value\n")
108 elseif("${arg}" MATCHES "^-l([^:].*)$")
110 set(lib "${CMAKE_MATCH_1}")
111 if(search_static AND lib MATCHES "^(gfortran|stdc\\+\\+)$")
112 # Search for the static library later, once all link dirs are known.
113 set(lib "SEARCH_STATIC:${lib}")
115 list(APPEND implicit_libs_tmp ${lib})
116 string(APPEND log " arg [${arg}] ==> lib [${lib}]\n")
117 elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.a$")
118 # Unix library full path.
119 list(APPEND implicit_libs_tmp ${arg})
120 string(APPEND log " arg [${arg}] ==> lib [${arg}]\n")
121 elseif("${arg}" MATCHES "^[-/](DEFAULTLIB|defaultlib):(.+)")
123 set(lib "${CMAKE_MATCH_2}")
124 list(APPEND implicit_libs_tmp ${lib})
125 string(APPEND log " arg [${arg}] ==> lib [${lib}]\n")
126 elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.o$")
127 if(EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS)
128 list(APPEND implicit_objs_tmp ${arg})
129 string(APPEND log " arg [${arg}] ==> obj [${arg}]\n")
131 if(obj_regex AND "${arg}" MATCHES "${obj_regex}")
132 # Object file full path.
133 list(APPEND implicit_libs_tmp ${arg})
135 elseif("${arg}" MATCHES "^-Y(P,)?[^0-9]")
136 # Sun search path ([^0-9] avoids conflict with Mac -Y<num>).
137 string(REGEX REPLACE "^-Y(P,)?" "" dirs "${arg}")
138 string(REPLACE ":" ";" dirs "${dirs}")
139 list(APPEND implicit_dirs_tmp ${dirs})
140 string(APPEND log " arg [${arg}] ==> dirs [${dirs}]\n")
141 elseif("${arg}" STREQUAL "-Bstatic")
143 string(APPEND log " arg [${arg}] ==> search static\n" )
144 elseif("${arg}" STREQUAL "-Bdynamic")
146 string(APPEND log " arg [${arg}] ==> search dynamic\n" )
147 elseif("${arg}" MATCHES "^-l:")
149 list(APPEND implicit_libs_tmp ${arg})
150 string(APPEND log " arg [${arg}] ==> lib [${arg}]\n")
151 elseif("${arg}" MATCHES "^-z(all|default|weak)extract")
152 # Link editor option.
153 list(APPEND implicit_libs_tmp ${arg})
154 string(APPEND log " arg [${arg}] ==> opt [${arg}]\n")
155 elseif("${arg}" STREQUAL "cl.exe")
156 string(APPEND log " arg [${arg}] ==> recognize MSVC cl\n")
159 string(APPEND log " arg [${arg}] ==> ignore\n")
163 elseif("${line}" MATCHES "LPATH(=| is:? *)(.*)$")
164 string(APPEND log " LPATH line: [${line}]\n")
166 string(REPLACE ":" ";" paths "${CMAKE_MATCH_2}")
167 list(APPEND implicit_dirs_tmp ${paths})
168 string(APPEND log " dirs [${paths}]\n")
170 string(APPEND log " ignore line: [${line}]\n")
174 # Look for library search paths reported by linker.
175 if("${output_lines}" MATCHES ";Library search paths:((;\t[^;]+)+)")
176 string(REPLACE ";\t" ";" implicit_dirs_match "${CMAKE_MATCH_1}")
177 string(APPEND log " Library search paths: [${implicit_dirs_match}]\n")
178 list(APPEND implicit_dirs_tmp ${implicit_dirs_match})
180 if("${output_lines}" MATCHES ";Framework search paths:((;\t[^;]+)+)")
181 string(REPLACE ";\t" ";" implicit_fwks_match "${CMAKE_MATCH_1}")
182 string(APPEND log " Framework search paths: [${implicit_fwks_match}]\n")
183 list(APPEND implicit_fwks_tmp ${implicit_fwks_match})
186 # Cleanup list of libraries and flags.
187 # We remove items that are not language-specific.
188 set(implicit_libs "")
189 foreach(lib IN LISTS implicit_libs_tmp)
190 if("x${lib}" MATCHES "^xSEARCH_STATIC:(.*)")
192 set(lib "${CMAKE_MATCH_1}")
196 if("x${lib}" MATCHES "^x(crt.*\\.o|gcc_eh.*|.*libgcc_eh.*|System.*|.*libclang_rt.*|msvcrt.*|libvcruntime.*|libucrt.*|libcmt.*)$")
197 string(APPEND log " remove lib [${lib}]\n")
198 elseif(search_static)
199 # This library appears after a -Bstatic flag. Due to ordering
200 # and filtering for mixed-language link lines, we do not preserve
201 # the -Bstatic flag itself. Instead, use an absolute path.
202 # Search using a temporary variable with a distinct name
203 # so that our test suite does not depend on disk content.
204 find_library("CMAKE_${lang}_IMPLICIT_LINK_LIBRARY_${lib}" NO_CACHE NAMES "lib${lib}.a" NO_DEFAULT_PATH PATHS ${implicit_dirs_tmp})
205 set(_lib_static "${CMAKE_${lang}_IMPLICIT_LINK_LIBRARY_${lib}}")
207 string(APPEND log " search lib [SEARCH_STATIC:${lib}] ==> [${_lib_static}]\n")
208 list(APPEND implicit_libs "${_lib_static}")
210 string(APPEND log " search lib [SEARCH_STATIC:${lib}] ==> [${lib}]\n")
211 list(APPEND implicit_libs "${lib}")
213 elseif(IS_ABSOLUTE "${lib}")
214 get_filename_component(abs "${lib}" ABSOLUTE)
215 if(NOT "x${lib}" STREQUAL "x${abs}")
216 string(APPEND log " collapse lib [${lib}] ==> [${abs}]\n")
218 list(APPEND implicit_libs "${abs}")
220 list(APPEND implicit_libs "${lib}")
224 if(EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS)
225 set(implicit_objs "")
226 foreach(obj IN LISTS implicit_objs_tmp)
227 if(IS_ABSOLUTE "${obj}")
228 get_filename_component(abs "${obj}" ABSOLUTE)
229 if(NOT "x${obj}" STREQUAL "x${abs}")
230 string(APPEND log " collapse obj [${obj}] ==> [${abs}]\n")
232 list(APPEND implicit_objs "${abs}")
234 list(APPEND implicit_objs "${obj}")
239 # Cleanup list of library and framework directories.
240 set(desc_dirs "library")
241 set(desc_fwks "framework")
243 set(implicit_${t} "")
244 foreach(d IN LISTS implicit_${t}_tmp)
245 get_filename_component(dir "${d}" ABSOLUTE)
246 string(FIND "${dir}" "${CMAKE_FILES_DIRECTORY}/" pos)
248 set(msg ", skipping non-system directory")
251 list(APPEND implicit_${t} "${dir}")
253 string(APPEND log " collapse ${desc_${t}} dir [${d}] ==> [${dir}]${msg}\n")
255 list(REMOVE_DUPLICATES implicit_${t})
259 string(APPEND log " implicit libs: [${implicit_libs}]\n")
260 string(APPEND log " implicit objs: [${implicit_objs}]\n")
261 string(APPEND log " implicit dirs: [${implicit_dirs}]\n")
262 string(APPEND log " implicit fwks: [${implicit_fwks}]\n")
265 set(${lib_var} "${implicit_libs}" PARENT_SCOPE)
266 set(${dir_var} "${implicit_dirs}" PARENT_SCOPE)
267 set(${fwk_var} "${implicit_fwks}" PARENT_SCOPE)
268 set(${log_var} "${log}" PARENT_SCOPE)
270 if(EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS)
271 set(${EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS} "${implicit_objs}" PARENT_SCOPE)