6bdefde833961a6005f6ed959ed532d013ceb79b
[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)
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     set(search_static 0)
80     if("${cmd}" MATCHES "${linker_regex}")
81       string(APPEND log "  link line: [${line}]\n")
82       string(REGEX REPLACE ";-([LYz]);" ";-\\1" args "${args}")
83       set(skip_value_of "")
84       foreach(arg IN LISTS args)
85         if(skip_value_of)
86           string(APPEND log "    arg [${arg}] ==> skip value of ${skip_value_of}\n")
87           set(skip_value_of "")
88         elseif("${arg}" MATCHES "^-L(.:)?[/\\]")
89           # Unix search path.
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):(.+)")
94           # MSVC search path.
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([^:].*)$")
109           # Unix library.
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}")
114           endif()
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):(.+)")
122           # Windows library.
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")
130           endif()
131           if(obj_regex AND "${arg}" MATCHES "${obj_regex}")
132             # Object file full path.
133             list(APPEND implicit_libs_tmp ${arg})
134           endif()
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")
142           set(search_static 1)
143           string(APPEND log "    arg [${arg}] ==> search static\n" )
144         elseif("${arg}" STREQUAL "-Bdynamic")
145           set(search_static 0)
146           string(APPEND log "    arg [${arg}] ==> search dynamic\n" )
147         elseif("${arg}" MATCHES "^-l:")
148           # HP named library.
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")
157           set(is_msvc 1)
158         else()
159           string(APPEND log "    arg [${arg}] ==> ignore\n")
160         endif()
161       endforeach()
162       break()
163     elseif("${line}" MATCHES "LPATH(=| is:? *)(.*)$")
164       string(APPEND log "  LPATH line: [${line}]\n")
165       # HP search path.
166       string(REPLACE ":" ";" paths "${CMAKE_MATCH_2}")
167       list(APPEND implicit_dirs_tmp ${paths})
168       string(APPEND log "    dirs [${paths}]\n")
169     else()
170       string(APPEND log "  ignore line: [${line}]\n")
171     endif()
172   endforeach()
173
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})
179   endif()
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})
184   endif()
185
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:(.*)")
191       set(search_static 1)
192       set(lib "${CMAKE_MATCH_1}")
193     else()
194       set(search_static 0)
195     endif()
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}}")
206       if(_lib_static)
207         string(APPEND log "  search lib [SEARCH_STATIC:${lib}] ==> [${_lib_static}]\n")
208         list(APPEND implicit_libs "${_lib_static}")
209       else()
210         string(APPEND log "  search lib [SEARCH_STATIC:${lib}] ==> [${lib}]\n")
211         list(APPEND implicit_libs "${lib}")
212       endif()
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")
217       endif()
218       list(APPEND implicit_libs "${abs}")
219     else()
220       list(APPEND implicit_libs "${lib}")
221     endif()
222   endforeach()
223
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")
231         endif()
232         list(APPEND implicit_objs "${abs}")
233       else()
234         list(APPEND implicit_objs "${obj}")
235       endif()
236     endforeach()
237   endif()
238
239   # Cleanup list of library and framework directories.
240   set(desc_dirs "library")
241   set(desc_fwks "framework")
242   foreach(t dirs fwks)
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)
247       if(NOT pos LESS 0)
248         set(msg ", skipping non-system directory")
249       else()
250         set(msg "")
251         list(APPEND implicit_${t} "${dir}")
252       endif()
253       string(APPEND log "  collapse ${desc_${t}} dir [${d}] ==> [${dir}]${msg}\n")
254     endforeach()
255     list(REMOVE_DUPLICATES implicit_${t})
256   endforeach()
257
258   # Log results.
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")
263
264   # Return results.
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)
269
270   if(EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS)
271     set(${EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS} "${implicit_objs}" PARENT_SCOPE)
272   endif()
273 endfunction()
274
275 cmake_policy(POP)