CMakeParseImplicitLinkInfo.cmake 11 KB

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