CMakeParseImplicitLinkInfo.cmake 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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(keywordArgs)
  17. set(oneValueArgs LANGUAGE COMPUTE_IMPLICIT_OBJECTS)
  18. set(multiValueArgs )
  19. cmake_parse_arguments(EXTRA_PARSE "${keywordArgs}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  20. cmake_parse_implicit_link_info2("${text}" "${log_var}" "${obj_regex}"
  21. COMPUTE_IMPLICIT_LIBS "${lib_var}" COMPUTE_IMPLICIT_DIRS "${dir_var}"
  22. COMPUTE_IMPLICIT_FWKS "${fwk_var}" ${ARGN})
  23. set(${lib_var} "${${lib_var}}" PARENT_SCOPE)
  24. set(${dir_var} "${${dir_var}}" PARENT_SCOPE)
  25. set(${fwk_var} "${${fwk_var}}" PARENT_SCOPE)
  26. set(${log_var} "${${log_var}}" PARENT_SCOPE)
  27. if(EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS)
  28. set(${EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS} "${${EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS}}" PARENT_SCOPE)
  29. endif()
  30. endfunction()
  31. function(cmake_parse_implicit_link_info2 text log_var obj_regex)
  32. set(implicit_libs_tmp "")
  33. set(implicit_objs_tmp "")
  34. set(implicit_dirs_tmp)
  35. set(implicit_fwks_tmp)
  36. set(log "")
  37. set(keywordArgs)
  38. set(oneValueArgs LANGUAGE
  39. COMPUTE_IMPLICIT_LIBS COMPUTE_IMPLICIT_DIRS COMPUTE_IMPLICIT_FWKS
  40. COMPUTE_IMPLICIT_OBJECTS COMPUTE_LINKER)
  41. set(multiValueArgs )
  42. cmake_parse_arguments(EXTRA_PARSE "${keywordArgs}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  43. set(is_msvc 0)
  44. if(EXTRA_PARSE_LANGUAGE AND
  45. ("x${CMAKE_${EXTRA_PARSE_LANGUAGE}_COMPILER_ID}" STREQUAL "xMSVC" OR
  46. "x${CMAKE_${EXTRA_PARSE_LANGUAGE}_SIMULATE_ID}" STREQUAL "xMSVC"))
  47. set(is_msvc 1)
  48. endif()
  49. # Parse implicit linker arguments.
  50. set(linker "ld[0-9]*(\\.[a-z]+)?")
  51. if(is_msvc)
  52. string(APPEND linker "|link\\.exe|lld-link(\\.exe)?")
  53. endif()
  54. if(CMAKE_LINKER)
  55. get_filename_component(default_linker ${CMAKE_LINKER} NAME)
  56. if (NOT default_linker MATCHES "(${linker})")
  57. string(REGEX REPLACE "([][+.*?()^$])" "\\\\\\1" default_linker "${default_linker}")
  58. list(PREPEND linker "${default_linker}|")
  59. endif()
  60. endif()
  61. set(startfile "CMAKE_LINK_STARTFILE-NOTFOUND")
  62. if(CMAKE_LINK_STARTFILE)
  63. set(startfile "${CMAKE_LINK_STARTFILE}")
  64. endif()
  65. # Construct a regex to match linker lines. It must match both the
  66. # whole line and just the command (argv[0]).
  67. set(linker_regex "^( *|.*[/\\])(${linker}|${startfile}|([^/\\]+-)?ld|collect2)[^/\\]*( |$)")
  68. set(linker_exclude_regex "collect2 version |^[A-Za-z0-9_]+=|/ldfe ")
  69. set(linker_tool_regex "^[ \t]*(->|exec:|\")?[ \t]*(.*[/\\](${linker}))(\"|,| |$)")
  70. set(linker_tool_exclude_regex "cuda-fake-ld|-fuse-ld=|--with-ld=")
  71. set(linker_tool "NOTFOUND")
  72. set(link_line_parsed 0)
  73. string(APPEND log " link line regex: [${linker_regex}]\n")
  74. if(EXTRA_PARSE_COMPUTE_LINKER)
  75. string(APPEND log " linker tool regex: [${linker_tool_regex}]\n")
  76. endif()
  77. string(REGEX REPLACE "\r?\n" ";" output_lines "${text}")
  78. foreach(line IN LISTS output_lines)
  79. if(EXTRA_PARSE_COMPUTE_LINKER AND
  80. NOT linker_tool AND NOT "${line}" MATCHES "${linker_tool_exclude_regex}"
  81. AND "${line}" MATCHES "${linker_tool_regex}")
  82. set(linker_tool "${CMAKE_MATCH_2}")
  83. if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
  84. # pick-up last path
  85. string(REGEX REPLACE "^.*([A-Za-z]:[/\\][^:]+)$" "\\1" linker_tool "${linker_tool}")
  86. cmake_path(SET linker_tool "${linker_tool}")
  87. endif()
  88. string(APPEND log " linker tool for '${EXTRA_PARSE_LANGUAGE}': ${linker_tool}\n")
  89. endif()
  90. if(NOT (EXTRA_PARSE_COMPUTE_IMPLICIT_LIBS OR EXTRA_PARSE_COMPUTE_IMPLICIT_DIRS
  91. OR EXTRA_PARSE_COMPUTE_IMPLICIT_FWKS OR EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS))
  92. if(linker_tool)
  93. break()
  94. else()
  95. continue()
  96. endif()
  97. endif()
  98. set(cmd)
  99. if("${line}" MATCHES "${linker_regex}" AND
  100. NOT "${line}" MATCHES "${linker_exclude_regex}")
  101. if(XCODE)
  102. # Xcode unconditionally adds a path under the project build tree and
  103. # on older versions it is not reported with proper quotes. Remove it.
  104. string(REGEX REPLACE "([][+.*()^])" "\\\\\\1" _dir_regex "${CMAKE_BINARY_DIR}")
  105. string(REGEX REPLACE " -[FL]${_dir_regex}/([^ ]| [^-])+( |$)" " " xline "${line}")
  106. if(NOT "x${xline}" STREQUAL "x${line}")
  107. string(APPEND log " reduced line: [${line}]\n to: [${xline}]\n")
  108. set(line "${xline}")
  109. endif()
  110. endif()
  111. separate_arguments(args NATIVE_COMMAND "${line}")
  112. list(GET args 0 cmd)
  113. if("${cmd}" MATCHES "->")
  114. # LCC has '-> ' in-front of the linker
  115. list(GET args 1 cmd)
  116. endif()
  117. else()
  118. #check to see if the link line is comma-separated instead of space separated
  119. string(REGEX REPLACE "," " " line "${line}")
  120. if("${line}" MATCHES "${linker_regex}" AND
  121. NOT "${line}" MATCHES "${linker_exclude_regex}")
  122. separate_arguments(args NATIVE_COMMAND "${line}")
  123. list(GET args 0 cmd)
  124. if("${cmd}" MATCHES "exec:")
  125. # ibm xl sometimes has 'exec: ' in-front of the linker
  126. list(GET args 1 cmd)
  127. endif()
  128. endif()
  129. endif()
  130. set(search_static 0)
  131. if(NOT link_line_parsed AND "${cmd}" MATCHES "${linker_regex}")
  132. set(link_line_parsed 1)
  133. string(APPEND log " link line: [${line}]\n")
  134. string(REGEX REPLACE ";-([LYz]);" ";-\\1" args "${args}")
  135. set(skip_value_of "")
  136. foreach(arg IN LISTS args)
  137. if(skip_value_of)
  138. string(APPEND log " arg [${arg}] ==> skip value of ${skip_value_of}\n")
  139. set(skip_value_of "")
  140. elseif("${arg}" MATCHES "^-L(.:)?[/\\]")
  141. if(EXTRA_PARSE_COMPUTE_IMPLICIT_DIRS)
  142. # Unix search path.
  143. string(REGEX REPLACE "^-L" "" dir "${arg}")
  144. list(APPEND implicit_dirs_tmp ${dir})
  145. string(APPEND log " arg [${arg}] ==> dir [${dir}]\n")
  146. endif()
  147. elseif("${arg}" MATCHES "^[-/](LIBPATH|libpath):(.+)")
  148. if(EXTRA_PARSE_COMPUTE_IMPLICIT_DIRS)
  149. # MSVC search path.
  150. set(dir "${CMAKE_MATCH_2}")
  151. list(APPEND implicit_dirs_tmp ${dir})
  152. string(APPEND log " arg [${arg}] ==> dir [${dir}]\n")
  153. endif()
  154. elseif(is_msvc AND "${arg}" STREQUAL "-link")
  155. string(APPEND log " arg [${arg}] ==> ignore MSVC cl option\n")
  156. elseif(is_msvc AND "${arg}" MATCHES "^[-/][Ii][Mm][Pp][Ll][Ii][Bb]:")
  157. string(APPEND log " arg [${arg}] ==> ignore MSVC link option\n")
  158. elseif(is_msvc AND "${arg}" MATCHES "^[-/][Ww][Hh][Oo][Ll][Ee][Aa][Rr][Cc][Hh][Ii][Vv][Ee]:Fortran_main")
  159. string(APPEND log " arg [${arg}] ==> ignore LLVMFlang program entry point\n")
  160. elseif(is_msvc AND "${arg}" MATCHES "^(.*\\.[Ll][Ii][Bb])$")
  161. if(EXTRA_PARSE_COMPUTE_IMPLICIT_LIBS)
  162. set(lib "${CMAKE_MATCH_1}")
  163. list(APPEND implicit_libs_tmp ${lib})
  164. string(APPEND log " arg [${arg}] ==> lib [${lib}]\n")
  165. endif()
  166. elseif("${arg}" STREQUAL "-lto_library")
  167. # ld argument "-lto_library <path>"
  168. set(skip_value_of "${arg}")
  169. string(APPEND log " arg [${arg}] ==> ignore, skip following value\n")
  170. elseif("${arg}" MATCHES "^-l([^:].*)$")
  171. if(EXTRA_PARSE_COMPUTE_IMPLICIT_LIBS)
  172. # Unix library.
  173. set(lib "${CMAKE_MATCH_1}")
  174. if(search_static AND lib MATCHES "^(gfortran|stdc\\+\\+)$")
  175. # Search for the static library later, once all link dirs are known.
  176. set(lib "SEARCH_STATIC:${lib}")
  177. endif()
  178. list(APPEND implicit_libs_tmp ${lib})
  179. string(APPEND log " arg [${arg}] ==> lib [${lib}]\n")
  180. endif()
  181. elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.a$")
  182. if(EXTRA_PARSE_COMPUTE_IMPLICIT_LIBS)
  183. # Unix library full path.
  184. list(APPEND implicit_libs_tmp ${arg})
  185. string(APPEND log " arg [${arg}] ==> lib [${arg}]\n")
  186. endif()
  187. elseif("${arg}" MATCHES "^[-/](DEFAULTLIB|defaultlib):(.+)")
  188. if(EXTRA_PARSE_COMPUTE_IMPLICIT_LIBS)
  189. # Windows library.
  190. set(lib "${CMAKE_MATCH_2}")
  191. list(APPEND implicit_libs_tmp ${lib})
  192. string(APPEND log " arg [${arg}] ==> lib [${lib}]\n")
  193. endif()
  194. elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.o$")
  195. if(EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS)
  196. list(APPEND implicit_objs_tmp ${arg})
  197. string(APPEND log " arg [${arg}] ==> obj [${arg}]\n")
  198. endif()
  199. if(EXTRA_PARSE_COMPUTE_IMPLICIT_LIBS)
  200. if(obj_regex AND "${arg}" MATCHES "${obj_regex}")
  201. # Object file full path.
  202. list(APPEND implicit_libs_tmp ${arg})
  203. endif()
  204. endif()
  205. elseif("${arg}" MATCHES "^-Y(P,)?[^0-9]")
  206. if(EXTRA_PARSE_COMPUTE_IMPLICIT_DIRS)
  207. # Sun search path ([^0-9] avoids conflict with Mac -Y<num>).
  208. string(REGEX REPLACE "^-Y(P,)?" "" dirs "${arg}")
  209. string(REPLACE ":" ";" dirs "${dirs}")
  210. list(APPEND implicit_dirs_tmp ${dirs})
  211. string(APPEND log " arg [${arg}] ==> dirs [${dirs}]\n")
  212. endif()
  213. elseif("${arg}" STREQUAL "-Bstatic")
  214. set(search_static 1)
  215. string(APPEND log " arg [${arg}] ==> search static\n" )
  216. elseif("${arg}" STREQUAL "-Bdynamic")
  217. set(search_static 0)
  218. string(APPEND log " arg [${arg}] ==> search dynamic\n" )
  219. elseif("${arg}" MATCHES "^-l:")
  220. if(EXTRA_PARSE_COMPUTE_IMPLICIT_LIBS)
  221. # HP named library.
  222. list(APPEND implicit_libs_tmp ${arg})
  223. string(APPEND log " arg [${arg}] ==> lib [${arg}]\n")
  224. endif()
  225. elseif("${arg}" MATCHES "^-z(all|default|weak)extract")
  226. if(EXTRA_PARSE_COMPUTE_IMPLICIT_LIBS)
  227. # Link editor option.
  228. list(APPEND implicit_libs_tmp ${arg})
  229. string(APPEND log " arg [${arg}] ==> opt [${arg}]\n")
  230. endif()
  231. elseif("${arg}" STREQUAL "cl.exe")
  232. string(APPEND log " arg [${arg}] ==> recognize MSVC cl\n")
  233. set(is_msvc 1)
  234. else()
  235. string(APPEND log " arg [${arg}] ==> ignore\n")
  236. endif()
  237. endforeach()
  238. elseif("${line}" MATCHES "LPATH(=| is:? *)(.*)$")
  239. if(EXTRA_PARSE_COMPUTE_IMPLICIT_DIRS)
  240. string(APPEND log " LPATH line: [${line}]\n")
  241. # HP search path.
  242. string(REPLACE ":" ";" paths "${CMAKE_MATCH_2}")
  243. list(APPEND implicit_dirs_tmp ${paths})
  244. string(APPEND log " dirs [${paths}]\n")
  245. endif()
  246. else()
  247. string(APPEND log " ignore line: [${line}]\n")
  248. endif()
  249. if((NOT EXTRA_PARSE_COMPUTE_LINKER OR linker_tool) AND link_line_parsed)
  250. break()
  251. endif()
  252. endforeach()
  253. # Look for library search paths reported by linker.
  254. if(EXTRA_PARSE_COMPUTE_IMPLICIT_DIRS AND "${output_lines}" MATCHES ";Library search paths:((;\t[^;]+)+)")
  255. string(REPLACE ";\t" ";" implicit_dirs_match "${CMAKE_MATCH_1}")
  256. string(APPEND log " Library search paths: [${implicit_dirs_match}]\n")
  257. list(APPEND implicit_dirs_tmp ${implicit_dirs_match})
  258. endif()
  259. if(EXTRA_PARSE_COMPUTE_IMPLICIT_FWKS AND "${output_lines}" MATCHES ";Framework search paths:((;\t[^;]+)+)")
  260. string(REPLACE ";\t" ";" implicit_fwks_match "${CMAKE_MATCH_1}")
  261. string(APPEND log " Framework search paths: [${implicit_fwks_match}]\n")
  262. list(APPEND implicit_fwks_tmp ${implicit_fwks_match})
  263. endif()
  264. # Cleanup list of libraries and flags.
  265. # We remove items that are not language-specific.
  266. set(implicit_libs "")
  267. foreach(lib IN LISTS implicit_libs_tmp)
  268. if("x${lib}" MATCHES "^xSEARCH_STATIC:(.*)")
  269. set(search_static 1)
  270. set(lib "${CMAKE_MATCH_1}")
  271. else()
  272. set(search_static 0)
  273. endif()
  274. if("x${lib}" MATCHES "^x(crt.*\\.o|gcc_eh.*|.*libgcc_eh.*|System.*|.*libclang_rt.*|msvcrt.*|libvcruntime.*|libucrt.*|libcmt.*)$")
  275. string(APPEND log " remove lib [${lib}]\n")
  276. elseif(search_static)
  277. # This library appears after a -Bstatic flag. Due to ordering
  278. # and filtering for mixed-language link lines, we do not preserve
  279. # the -Bstatic flag itself. Instead, use an absolute path.
  280. # Search using a temporary variable with a distinct name
  281. # so that our test suite does not depend on disk content.
  282. find_library("CMAKE_${lang}_IMPLICIT_LINK_LIBRARY_${lib}" NO_CACHE NAMES "lib${lib}.a" NO_DEFAULT_PATH PATHS ${implicit_dirs_tmp})
  283. set(_lib_static "${CMAKE_${lang}_IMPLICIT_LINK_LIBRARY_${lib}}")
  284. if(_lib_static)
  285. string(APPEND log " search lib [SEARCH_STATIC:${lib}] ==> [${_lib_static}]\n")
  286. list(APPEND implicit_libs "${_lib_static}")
  287. else()
  288. string(APPEND log " search lib [SEARCH_STATIC:${lib}] ==> [${lib}]\n")
  289. list(APPEND implicit_libs "${lib}")
  290. endif()
  291. elseif(IS_ABSOLUTE "${lib}")
  292. get_filename_component(abs "${lib}" ABSOLUTE)
  293. if(NOT "x${lib}" STREQUAL "x${abs}")
  294. string(APPEND log " collapse lib [${lib}] ==> [${abs}]\n")
  295. endif()
  296. list(APPEND implicit_libs "${abs}")
  297. else()
  298. list(APPEND implicit_libs "${lib}")
  299. endif()
  300. endforeach()
  301. if(EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS)
  302. set(implicit_objs "")
  303. foreach(obj IN LISTS implicit_objs_tmp)
  304. if(IS_ABSOLUTE "${obj}")
  305. get_filename_component(abs "${obj}" ABSOLUTE)
  306. if(NOT "x${obj}" STREQUAL "x${abs}")
  307. string(APPEND log " collapse obj [${obj}] ==> [${abs}]\n")
  308. endif()
  309. list(APPEND implicit_objs "${abs}")
  310. else()
  311. list(APPEND implicit_objs "${obj}")
  312. endif()
  313. endforeach()
  314. endif()
  315. # Cleanup list of library and framework directories.
  316. set(desc_dirs "library")
  317. set(desc_fwks "framework")
  318. foreach(t dirs fwks)
  319. set(implicit_${t} "")
  320. foreach(d IN LISTS implicit_${t}_tmp)
  321. get_filename_component(dir "${d}" ABSOLUTE)
  322. string(FIND "${dir}" "${CMAKE_FILES_DIRECTORY}/" pos)
  323. if(NOT pos LESS 0)
  324. set(msg ", skipping non-system directory")
  325. else()
  326. set(msg "")
  327. list(APPEND implicit_${t} "${dir}")
  328. endif()
  329. string(APPEND log " collapse ${desc_${t}} dir [${d}] ==> [${dir}]${msg}\n")
  330. endforeach()
  331. list(REMOVE_DUPLICATES implicit_${t})
  332. endforeach()
  333. # Log results.
  334. string(APPEND log " implicit libs: [${implicit_libs}]\n")
  335. string(APPEND log " implicit objs: [${implicit_objs}]\n")
  336. string(APPEND log " implicit dirs: [${implicit_dirs}]\n")
  337. string(APPEND log " implicit fwks: [${implicit_fwks}]\n")
  338. # Return results.
  339. if(EXTRA_PARSE_COMPUTE_LINKER)
  340. set(${EXTRA_PARSE_COMPUTE_LINKER} "${linker_tool}" PARENT_SCOPE)
  341. endif()
  342. set(${log_var} "${log}" PARENT_SCOPE)
  343. if(EXTRA_PARSE_COMPUTE_IMPLICIT_LIBS)
  344. set(${EXTRA_PARSE_COMPUTE_IMPLICIT_LIBS} "${implicit_libs}" PARENT_SCOPE)
  345. endif()
  346. if(EXTRA_PARSE_COMPUTE_IMPLICIT_DIRS)
  347. set(${EXTRA_PARSE_COMPUTE_IMPLICIT_DIRS} "${implicit_dirs}" PARENT_SCOPE)
  348. endif()
  349. if(EXTRA_PARSE_COMPUTE_IMPLICIT_FWKS)
  350. set(${EXTRA_PARSE_COMPUTE_IMPLICIT_FWKS} "${implicit_fwks}" PARENT_SCOPE)
  351. endif()
  352. if(EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS)
  353. set(${EXTRA_PARSE_COMPUTE_IMPLICIT_OBJECTS} "${implicit_objs}" PARENT_SCOPE)
  354. endif()
  355. endfunction()
  356. cmake_policy(POP)