CMakeParseImplicitLinkInfo.cmake 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # Function parse implicit linker options.
  4. # This is used internally by CMake and should not be included by user
  5. # code.
  6. function(CMAKE_PARSE_IMPLICIT_LINK_INFO text lib_var dir_var fwk_var log_var obj_regex)
  7. set(implicit_libs_tmp "")
  8. set(implicit_dirs_tmp)
  9. set(implicit_fwks_tmp)
  10. set(log "")
  11. # Parse implicit linker arguments.
  12. set(linker "CMAKE_LINKER-NOTFOUND")
  13. if(CMAKE_LINKER)
  14. get_filename_component(linker ${CMAKE_LINKER} NAME)
  15. string(REGEX REPLACE "([][+.*?()^$])" "\\\\\\1" linker "${linker}")
  16. endif()
  17. set(startfile "CMAKE_LINK_STARTFILE-NOTFOUND")
  18. if(CMAKE_LINK_STARTFILE)
  19. set(startfile "${CMAKE_LINK_STARTFILE}")
  20. endif()
  21. # Construct a regex to match linker lines. It must match both the
  22. # whole line and just the command (argv[0]).
  23. set(linker_regex "^( *|.*[/\\])(${linker}|${startfile}|([^/\\]+-)?ld|collect2)[^/\\]*( |$)")
  24. set(linker_exclude_regex "collect2 version |^[A-Za-z0-9_]+=|/ldfe ")
  25. string(APPEND log " link line regex: [${linker_regex}]\n")
  26. string(REGEX REPLACE "\r?\n" ";" output_lines "${text}")
  27. foreach(line IN LISTS output_lines)
  28. set(cmd)
  29. if("${line}" MATCHES "${linker_regex}" AND
  30. NOT "${line}" MATCHES "${linker_exclude_regex}")
  31. if(XCODE)
  32. # Xcode unconditionally adds a path under the project build tree and
  33. # on older versions it is not reported with proper quotes. Remove it.
  34. string(REGEX REPLACE "([][+.*()^])" "\\\\\\1" _dir_regex "${CMAKE_BINARY_DIR}")
  35. string(REGEX REPLACE " -[FL]${_dir_regex}/([^ ]| [^-])+( |$)" " " xline "${line}")
  36. if(NOT "x${xline}" STREQUAL "x${line}")
  37. string(APPEND log " reduced line: [${line}]\n to: [${xline}]\n")
  38. set(line "${xline}")
  39. endif()
  40. endif()
  41. separate_arguments(args NATIVE_COMMAND "${line}")
  42. list(GET args 0 cmd)
  43. endif()
  44. set(is_msvc 0)
  45. if("${cmd}" MATCHES "${linker_regex}")
  46. string(APPEND log " link line: [${line}]\n")
  47. string(REGEX REPLACE ";-([LYz]);" ";-\\1" args "${args}")
  48. set(skip_value_of "")
  49. foreach(arg IN LISTS args)
  50. if(skip_value_of)
  51. string(APPEND log " arg [${arg}] ==> skip value of ${skip_value_of}\n")
  52. set(skip_value_of "")
  53. elseif("${arg}" MATCHES "^-L(.:)?[/\\]")
  54. # Unix search path.
  55. string(REGEX REPLACE "^-L" "" dir "${arg}")
  56. list(APPEND implicit_dirs_tmp ${dir})
  57. string(APPEND log " arg [${arg}] ==> dir [${dir}]\n")
  58. elseif("${arg}" MATCHES "^[-/](LIBPATH|libpath):(.+)")
  59. # MSVC search path.
  60. set(dir "${CMAKE_MATCH_2}")
  61. list(APPEND implicit_dirs_tmp ${dir})
  62. string(APPEND log " arg [${arg}] ==> dir [${dir}]\n")
  63. elseif(is_msvc AND "${arg}" STREQUAL "-link")
  64. string(APPEND log " arg [${arg}] ==> ignore MSVC cl option\n")
  65. elseif(is_msvc AND "${arg}" MATCHES "^(.*\\.[Ll][Ii][Bb])$")
  66. set(lib "${CMAKE_MATCH_1}")
  67. list(APPEND implicit_libs_tmp ${lib})
  68. string(APPEND log " arg [${arg}] ==> lib [${lib}]\n")
  69. elseif("${arg}" STREQUAL "-lto_library")
  70. # ld argument "-lto_library <path>"
  71. set(skip_value_of "${arg}")
  72. string(APPEND log " arg [${arg}] ==> ignore, skip following value\n")
  73. elseif("${arg}" MATCHES "^-l([^:].*)$")
  74. # Unix library.
  75. set(lib "${CMAKE_MATCH_1}")
  76. list(APPEND implicit_libs_tmp ${lib})
  77. string(APPEND log " arg [${arg}] ==> lib [${lib}]\n")
  78. elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.a$")
  79. # Unix library full path.
  80. list(APPEND implicit_libs_tmp ${arg})
  81. string(APPEND log " arg [${arg}] ==> lib [${arg}]\n")
  82. elseif("${arg}" MATCHES "^[-/](DEFAULTLIB|defaultlib):(.+)")
  83. # Windows library.
  84. set(lib "${CMAKE_MATCH_2}")
  85. list(APPEND implicit_libs_tmp ${lib})
  86. string(APPEND log " arg [${arg}] ==> lib [${lib}]\n")
  87. elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.o$"
  88. AND obj_regex AND "${arg}" MATCHES "${obj_regex}")
  89. # Object file full path.
  90. list(APPEND implicit_libs_tmp ${arg})
  91. string(APPEND log " arg [${arg}] ==> obj [${arg}]\n")
  92. elseif("${arg}" MATCHES "^-Y(P,)?[^0-9]")
  93. # Sun search path ([^0-9] avoids conflict with Mac -Y<num>).
  94. string(REGEX REPLACE "^-Y(P,)?" "" dirs "${arg}")
  95. string(REPLACE ":" ";" dirs "${dirs}")
  96. list(APPEND implicit_dirs_tmp ${dirs})
  97. string(APPEND log " arg [${arg}] ==> dirs [${dirs}]\n")
  98. elseif("${arg}" MATCHES "^-l:")
  99. # HP named library.
  100. list(APPEND implicit_libs_tmp ${arg})
  101. string(APPEND log " arg [${arg}] ==> lib [${arg}]\n")
  102. elseif("${arg}" MATCHES "^-z(all|default|weak)extract")
  103. # Link editor option.
  104. list(APPEND implicit_libs_tmp ${arg})
  105. string(APPEND log " arg [${arg}] ==> opt [${arg}]\n")
  106. elseif("${arg}" STREQUAL "cl.exe")
  107. string(APPEND log " arg [${arg}] ==> recognize MSVC cl\n")
  108. set(is_msvc 1)
  109. else()
  110. string(APPEND log " arg [${arg}] ==> ignore\n")
  111. endif()
  112. endforeach()
  113. break()
  114. elseif("${line}" MATCHES "LPATH(=| is:? *)(.*)$")
  115. string(APPEND log " LPATH line: [${line}]\n")
  116. # HP search path.
  117. string(REPLACE ":" ";" paths "${CMAKE_MATCH_2}")
  118. list(APPEND implicit_dirs_tmp ${paths})
  119. string(APPEND log " dirs [${paths}]\n")
  120. else()
  121. string(APPEND log " ignore line: [${line}]\n")
  122. endif()
  123. endforeach()
  124. # Look for library search paths reported by linker.
  125. if("${output_lines}" MATCHES ";Library search paths:((;\t[^;]+)+)")
  126. string(REPLACE ";\t" ";" implicit_dirs_match "${CMAKE_MATCH_1}")
  127. string(APPEND log " Library search paths: [${implicit_dirs_match}]\n")
  128. list(APPEND implicit_dirs_tmp ${implicit_dirs_match})
  129. endif()
  130. if("${output_lines}" MATCHES ";Framework search paths:((;\t[^;]+)+)")
  131. string(REPLACE ";\t" ";" implicit_fwks_match "${CMAKE_MATCH_1}")
  132. string(APPEND log " Framework search paths: [${implicit_fwks_match}]\n")
  133. list(APPEND implicit_fwks_tmp ${implicit_fwks_match})
  134. endif()
  135. # Cleanup list of libraries and flags.
  136. # We remove items that are not language-specific.
  137. set(implicit_libs "")
  138. foreach(lib IN LISTS implicit_libs_tmp)
  139. if("x${lib}" MATCHES "^x(crt.*\\.o|gcc_eh.*|System.*|.*libclang_rt.*|msvcrt.*|libvcruntime.*|libucrt.*|libcmt.*)$")
  140. string(APPEND log " remove lib [${lib}]\n")
  141. elseif(IS_ABSOLUTE "${lib}")
  142. get_filename_component(abs "${lib}" ABSOLUTE)
  143. if(NOT "x${lib}" STREQUAL "x${abs}")
  144. string(APPEND log " collapse lib [${lib}] ==> [${abs}]\n")
  145. endif()
  146. list(APPEND implicit_libs "${abs}")
  147. else()
  148. list(APPEND implicit_libs "${lib}")
  149. endif()
  150. endforeach()
  151. # Cleanup list of library and framework directories.
  152. set(desc_dirs "library")
  153. set(desc_fwks "framework")
  154. foreach(t dirs fwks)
  155. set(implicit_${t} "")
  156. foreach(d IN LISTS implicit_${t}_tmp)
  157. get_filename_component(dir "${d}" ABSOLUTE)
  158. string(FIND "${dir}" "${CMAKE_FILES_DIRECTORY}/" pos)
  159. if(NOT pos LESS 0)
  160. set(msg ", skipping non-system directory")
  161. else()
  162. set(msg "")
  163. list(APPEND implicit_${t} "${dir}")
  164. endif()
  165. string(APPEND log " collapse ${desc_${t}} dir [${d}] ==> [${dir}]${msg}\n")
  166. endforeach()
  167. list(REMOVE_DUPLICATES implicit_${t})
  168. endforeach()
  169. # Log results.
  170. string(APPEND log " implicit libs: [${implicit_libs}]\n")
  171. string(APPEND log " implicit dirs: [${implicit_dirs}]\n")
  172. string(APPEND log " implicit fwks: [${implicit_fwks}]\n")
  173. # Return results.
  174. set(${lib_var} "${implicit_libs}" PARENT_SCOPE)
  175. set(${dir_var} "${implicit_dirs}" PARENT_SCOPE)
  176. set(${fwk_var} "${implicit_fwks}" PARENT_SCOPE)
  177. set(${log_var} "${log}" PARENT_SCOPE)
  178. endfunction()