Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Modules / CMakeParseImplicitLinkInfo.cmake
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 cmake_policy(PUSH)
5 cmake_policy(SET CMP0053 NEW)
6 cmake_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 #
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)
22   set(log "")
23
24   set(keywordArgs)
25   set(oneValueArgs COMPUTE_IMPLICIT_OBJECTS LANGUAGE)
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       if("${cmd}" MATCHES "->")
62         # LCC has '-> ' in-front of the linker
63         list(GET args 1 cmd)
64       endif()
65     else()
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}")
71         list(GET args 0 cmd)
72         if("${cmd}" MATCHES "exec:")
73           # ibm xl sometimes has 'exec: ' in-front of the linker
74           list(GET args 1 cmd)
75         endif()
76       endif()
77     endif()
78     set(is_msvc 0)
79     if(EXTRA_PARSE_LANGUAGE AND
80       ("x${CMAKE_${EXTRA_PARSE_LANGUAGE}_ID}" STREQUAL "xMSVC" OR
81        "x${CMAKE_${EXTRA_PARSE_LANGUAGE}_SIMULATE_ID}" STREQUAL "xMSVC"))
82       set(is_msvc 1)
83     endif()
84     set(search_static 0)
85     if("${cmd}" MATCHES "${linker_regex}")
86       string(APPEND log "  link line: [${line}]\n")
87       string(REGEX REPLACE ";-([LYz]);" ";-\\1" args "${args}")
88       set(skip_value_of "")
89       foreach(arg IN LISTS args)
90         if(skip_value_of)
91           string(APPEND log "    arg [${arg}] ==> skip value of ${skip_value_of}\n")
92           set(skip_value_of "")
93         elseif("${arg}" MATCHES "^-L(.:)?[/\\]")
94           # Unix search path.
95           string(REGEX REPLACE "^-L" "" dir "${arg}")
96           list(APPEND implicit_dirs_tmp ${dir})
97           string(APPEND log "    arg [${arg}] ==> dir [${dir}]\n")
98         elseif("${arg}" MATCHES "^[-/](LIBPATH|libpath):(.+)")
99           # MSVC search path.
100           set(dir "${CMAKE_MATCH_2}")
101           list(APPEND implicit_dirs_tmp ${dir})
102           string(APPEND log "    arg [${arg}] ==> dir [${dir}]\n")
103         elseif(is_msvc AND "${arg}" STREQUAL "-link")
104           string(APPEND log "    arg [${arg}] ==> ignore MSVC cl option\n")
105         elseif(is_msvc AND "${arg}" MATCHES "^(.*\\.[Ll][Ii][Bb])$")
106           set(lib "${CMAKE_MATCH_1}")
107           list(APPEND implicit_libs_tmp ${lib})
108           string(APPEND log "    arg [${arg}] ==> lib [${lib}]\n")
109         elseif("${arg}" STREQUAL "-lto_library")
110           # ld argument "-lto_library <path>"
111           set(skip_value_of "${arg}")
112           string(APPEND log "    arg [${arg}] ==> ignore, skip following value\n")
113         elseif("${arg}" MATCHES "^-l([^:].*)$")
114           # Unix library.
115           set(lib "${CMAKE_MATCH_1}")
116           if(search_static AND lib MATCHES "^(gfortran|stdc\\+\\+)$")
117             # Search for the static library later, once all link dirs are known.
118             set(lib "SEARCH_STATIC:${lib}")
119           endif()
120           list(APPEND implicit_libs_tmp ${lib})
121           string(APPEND log "    arg [${arg}] ==> lib [${lib}]\n")
122         elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.a$")
123           # Unix library full path.
124           list(APPEND implicit_libs_tmp ${arg})
125           string(APPEND log "    arg [${arg}] ==> lib [${arg}]\n")
126         elseif("${arg}" MATCHES "^[-/](DEFAULTLIB|defaultlib):(.+)")
127           # Windows library.
128           set(lib "${CMAKE_MATCH_2}")
129           list(APPEND implicit_libs_tmp ${lib})
130           string(APPEND log "    arg [${arg}] ==> lib [${lib}]\n")
131         elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.o$")
132           if(EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS)
133             list(APPEND implicit_objs_tmp ${arg})
134             string(APPEND log "    arg [${arg}] ==> obj [${arg}]\n")
135           endif()
136           if(obj_regex AND "${arg}" MATCHES "${obj_regex}")
137             # Object file full path.
138             list(APPEND implicit_libs_tmp ${arg})
139           endif()
140         elseif("${arg}" MATCHES "^-Y(P,)?[^0-9]")
141           # Sun search path ([^0-9] avoids conflict with Mac -Y<num>).
142           string(REGEX REPLACE "^-Y(P,)?" "" dirs "${arg}")
143           string(REPLACE ":" ";" dirs "${dirs}")
144           list(APPEND implicit_dirs_tmp ${dirs})
145           string(APPEND log "    arg [${arg}] ==> dirs [${dirs}]\n")
146         elseif("${arg}" STREQUAL "-Bstatic")
147           set(search_static 1)
148           string(APPEND log "    arg [${arg}] ==> search static\n" )
149         elseif("${arg}" STREQUAL "-Bdynamic")
150           set(search_static 0)
151           string(APPEND log "    arg [${arg}] ==> search dynamic\n" )
152         elseif("${arg}" MATCHES "^-l:")
153           # HP named library.
154           list(APPEND implicit_libs_tmp ${arg})
155           string(APPEND log "    arg [${arg}] ==> lib [${arg}]\n")
156         elseif("${arg}" MATCHES "^-z(all|default|weak)extract")
157           # Link editor option.
158           list(APPEND implicit_libs_tmp ${arg})
159           string(APPEND log "    arg [${arg}] ==> opt [${arg}]\n")
160         elseif("${arg}" STREQUAL "cl.exe")
161           string(APPEND log "    arg [${arg}] ==> recognize MSVC cl\n")
162           set(is_msvc 1)
163         else()
164           string(APPEND log "    arg [${arg}] ==> ignore\n")
165         endif()
166       endforeach()
167       break()
168     elseif("${line}" MATCHES "LPATH(=| is:? *)(.*)$")
169       string(APPEND log "  LPATH line: [${line}]\n")
170       # HP search path.
171       string(REPLACE ":" ";" paths "${CMAKE_MATCH_2}")
172       list(APPEND implicit_dirs_tmp ${paths})
173       string(APPEND log "    dirs [${paths}]\n")
174     else()
175       string(APPEND log "  ignore line: [${line}]\n")
176     endif()
177   endforeach()
178
179   # Look for library search paths reported by linker.
180   if("${output_lines}" MATCHES ";Library search paths:((;\t[^;]+)+)")
181     string(REPLACE ";\t" ";" implicit_dirs_match "${CMAKE_MATCH_1}")
182     string(APPEND log "  Library search paths: [${implicit_dirs_match}]\n")
183     list(APPEND implicit_dirs_tmp ${implicit_dirs_match})
184   endif()
185   if("${output_lines}" MATCHES ";Framework search paths:((;\t[^;]+)+)")
186     string(REPLACE ";\t" ";" implicit_fwks_match "${CMAKE_MATCH_1}")
187     string(APPEND log "  Framework search paths: [${implicit_fwks_match}]\n")
188     list(APPEND implicit_fwks_tmp ${implicit_fwks_match})
189   endif()
190
191   # Cleanup list of libraries and flags.
192   # We remove items that are not language-specific.
193   set(implicit_libs "")
194   foreach(lib IN LISTS implicit_libs_tmp)
195     if("x${lib}" MATCHES "^xSEARCH_STATIC:(.*)")
196       set(search_static 1)
197       set(lib "${CMAKE_MATCH_1}")
198     else()
199       set(search_static 0)
200     endif()
201     if("x${lib}" MATCHES "^x(crt.*\\.o|gcc_eh.*|.*libgcc_eh.*|System.*|.*libclang_rt.*|msvcrt.*|libvcruntime.*|libucrt.*|libcmt.*)$")
202       string(APPEND log "  remove lib [${lib}]\n")
203     elseif(search_static)
204       # This library appears after a -Bstatic flag.  Due to ordering
205       # and filtering for mixed-language link lines, we do not preserve
206       # the -Bstatic flag itself.  Instead, use an absolute path.
207       # Search using a temporary variable with a distinct name
208       # so that our test suite does not depend on disk content.
209       find_library("CMAKE_${lang}_IMPLICIT_LINK_LIBRARY_${lib}" NO_CACHE NAMES "lib${lib}.a" NO_DEFAULT_PATH PATHS ${implicit_dirs_tmp})
210       set(_lib_static "${CMAKE_${lang}_IMPLICIT_LINK_LIBRARY_${lib}}")
211       if(_lib_static)
212         string(APPEND log "  search lib [SEARCH_STATIC:${lib}] ==> [${_lib_static}]\n")
213         list(APPEND implicit_libs "${_lib_static}")
214       else()
215         string(APPEND log "  search lib [SEARCH_STATIC:${lib}] ==> [${lib}]\n")
216         list(APPEND implicit_libs "${lib}")
217       endif()
218     elseif(IS_ABSOLUTE "${lib}")
219       get_filename_component(abs "${lib}" ABSOLUTE)
220       if(NOT "x${lib}" STREQUAL "x${abs}")
221         string(APPEND log "  collapse lib [${lib}] ==> [${abs}]\n")
222       endif()
223       list(APPEND implicit_libs "${abs}")
224     else()
225       list(APPEND implicit_libs "${lib}")
226     endif()
227   endforeach()
228
229   if(EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS)
230     set(implicit_objs "")
231     foreach(obj IN LISTS implicit_objs_tmp)
232       if(IS_ABSOLUTE "${obj}")
233         get_filename_component(abs "${obj}" ABSOLUTE)
234         if(NOT "x${obj}" STREQUAL "x${abs}")
235           string(APPEND log "  collapse obj [${obj}] ==> [${abs}]\n")
236         endif()
237         list(APPEND implicit_objs "${abs}")
238       else()
239         list(APPEND implicit_objs "${obj}")
240       endif()
241     endforeach()
242   endif()
243
244   # Cleanup list of library and framework directories.
245   set(desc_dirs "library")
246   set(desc_fwks "framework")
247   foreach(t dirs fwks)
248     set(implicit_${t} "")
249     foreach(d IN LISTS implicit_${t}_tmp)
250       get_filename_component(dir "${d}" ABSOLUTE)
251       string(FIND "${dir}" "${CMAKE_FILES_DIRECTORY}/" pos)
252       if(NOT pos LESS 0)
253         set(msg ", skipping non-system directory")
254       else()
255         set(msg "")
256         list(APPEND implicit_${t} "${dir}")
257       endif()
258       string(APPEND log "  collapse ${desc_${t}} dir [${d}] ==> [${dir}]${msg}\n")
259     endforeach()
260     list(REMOVE_DUPLICATES implicit_${t})
261   endforeach()
262
263   # Log results.
264   string(APPEND log "  implicit libs: [${implicit_libs}]\n")
265   string(APPEND log "  implicit objs: [${implicit_objs}]\n")
266   string(APPEND log "  implicit dirs: [${implicit_dirs}]\n")
267   string(APPEND log "  implicit fwks: [${implicit_fwks}]\n")
268
269   # Return results.
270   set(${lib_var} "${implicit_libs}" PARENT_SCOPE)
271   set(${dir_var} "${implicit_dirs}" PARENT_SCOPE)
272   set(${fwk_var} "${implicit_fwks}" PARENT_SCOPE)
273   set(${log_var} "${log}" PARENT_SCOPE)
274
275   if(EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS)
276     set(${EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS} "${implicit_objs}" PARENT_SCOPE)
277   endif()
278 endfunction()
279
280 cmake_policy(POP)