CMakeParseImplicitLinkInfo.cmake 6.8 KB

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