CMakeParseImplicitLinkInfo.cmake 8.5 KB

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