CMakeParseImplicitLinkInfo.cmake 12 KB

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