CMakeParseImplicitLinkInfo.cmake 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. endif()
  26. # Construct a regex to match linker lines. It must match both the
  27. # whole line and just the command (argv[0]).
  28. set(linker_regex "^( *|.*[/\\])(${linker}|ld|collect2)[^/\\]*( |$)")
  29. set(linker_exclude_regex "collect2 version ")
  30. set(log "${log} link line regex: [${linker_regex}]\n")
  31. string(REGEX REPLACE "\r?\n" ";" output_lines "${text}")
  32. foreach(line IN LISTS output_lines)
  33. set(cmd)
  34. if("${line}" MATCHES "${linker_regex}" AND
  35. NOT "${line}" MATCHES "${linker_exclude_regex}")
  36. if(XCODE)
  37. # Xcode unconditionally adds a path under the project build tree and
  38. # on older versions it is not reported with proper quotes. Remove it.
  39. string(REGEX REPLACE "([][+.*()^])" "\\\\\\1" _dir_regex "${CMAKE_BINARY_DIR}")
  40. string(REGEX REPLACE " -[FL]${_dir_regex}/([^ ]| [^-])+( |$)" " " xline "${line}")
  41. if(NOT "x${xline}" STREQUAL "x${line}")
  42. set(log "${log} reduced line: [${line}]\n to: [${xline}]\n")
  43. set(line "${xline}")
  44. endif()
  45. endif()
  46. if(UNIX)
  47. separate_arguments(args UNIX_COMMAND "${line}")
  48. else()
  49. separate_arguments(args WINDOWS_COMMAND "${line}")
  50. endif()
  51. list(GET args 0 cmd)
  52. endif()
  53. if("${cmd}" MATCHES "${linker_regex}")
  54. set(log "${log} link line: [${line}]\n")
  55. string(REGEX REPLACE ";-([LYz]);" ";-\\1" args "${args}")
  56. foreach(arg IN LISTS args)
  57. if("${arg}" MATCHES "^-L(.:)?[/\\]")
  58. # Unix search path.
  59. string(REGEX REPLACE "^-L" "" dir "${arg}")
  60. list(APPEND implicit_dirs_tmp ${dir})
  61. set(log "${log} arg [${arg}] ==> dir [${dir}]\n")
  62. elseif("${arg}" MATCHES "^-l[^:]")
  63. # Unix library.
  64. string(REGEX REPLACE "^-l" "" lib "${arg}")
  65. list(APPEND implicit_libs_tmp ${lib})
  66. set(log "${log} arg [${arg}] ==> lib [${lib}]\n")
  67. elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.a$")
  68. # Unix library full path.
  69. list(APPEND implicit_libs_tmp ${arg})
  70. set(log "${log} arg [${arg}] ==> lib [${arg}]\n")
  71. elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.o$"
  72. AND obj_regex AND "${arg}" MATCHES "${obj_regex}")
  73. # Object file full path.
  74. list(APPEND implicit_libs_tmp ${arg})
  75. set(log "${log} arg [${arg}] ==> obj [${arg}]\n")
  76. elseif("${arg}" MATCHES "^-Y(P,)?[^0-9]")
  77. # Sun search path ([^0-9] avoids conflict with Mac -Y<num>).
  78. string(REGEX REPLACE "^-Y(P,)?" "" dirs "${arg}")
  79. string(REPLACE ":" ";" dirs "${dirs}")
  80. list(APPEND implicit_dirs_tmp ${dirs})
  81. set(log "${log} arg [${arg}] ==> dirs [${dirs}]\n")
  82. elseif("${arg}" MATCHES "^-l:")
  83. # HP named library.
  84. list(APPEND implicit_libs_tmp ${arg})
  85. set(log "${log} arg [${arg}] ==> lib [${arg}]\n")
  86. elseif("${arg}" MATCHES "^-z(all|default|weak)extract")
  87. # Link editor option.
  88. list(APPEND implicit_libs_tmp ${arg})
  89. set(log "${log} arg [${arg}] ==> opt [${arg}]\n")
  90. else()
  91. set(log "${log} arg [${arg}] ==> ignore\n")
  92. endif()
  93. endforeach()
  94. break()
  95. elseif("${line}" MATCHES "LPATH(=| is:? )")
  96. set(log "${log} LPATH line: [${line}]\n")
  97. # HP search path.
  98. string(REGEX REPLACE ".*LPATH(=| is:? *)" "" paths "${line}")
  99. string(REPLACE ":" ";" paths "${paths}")
  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("${lib}" MATCHES "^(crt.*\\.o|gcc.*|System.*)$")
  122. set(log "${log} remove lib [${lib}]\n")
  123. else()
  124. list(APPEND implicit_libs "${lib}")
  125. endif()
  126. endforeach()
  127. # Cleanup list of library and framework directories.
  128. set(desc_dirs "library")
  129. set(desc_fwks "framework")
  130. foreach(t dirs fwks)
  131. set(implicit_${t} "")
  132. foreach(d IN LISTS implicit_${t}_tmp)
  133. get_filename_component(dir "${d}" ABSOLUTE)
  134. string(FIND "${dir}" "${CMAKE_FILES_DIRECTORY}/" pos)
  135. if(NOT pos LESS 0)
  136. set(msg ", skipping non-system directory")
  137. else()
  138. set(msg "")
  139. list(APPEND implicit_${t} "${dir}")
  140. endif()
  141. set(log "${log} collapse ${desc_${t}} dir [${d}] ==> [${dir}]${msg}\n")
  142. endforeach()
  143. list(REMOVE_DUPLICATES implicit_${t})
  144. endforeach()
  145. # Log results.
  146. set(log "${log} implicit libs: [${implicit_libs}]\n")
  147. set(log "${log} implicit dirs: [${implicit_dirs}]\n")
  148. set(log "${log} implicit fwks: [${implicit_fwks}]\n")
  149. # Return results.
  150. set(${lib_var} "${implicit_libs}" PARENT_SCOPE)
  151. set(${dir_var} "${implicit_dirs}" PARENT_SCOPE)
  152. set(${fwk_var} "${implicit_fwks}" PARENT_SCOPE)
  153. set(${log_var} "${log}" PARENT_SCOPE)
  154. endfunction()