CMakeParseImplicitLinkInfo.cmake 11 KB

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