CMakeParseImplicitLinkInfo.cmake 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 log_var)
  17. set(implicit_libs_tmp "")
  18. set(implicit_dirs_tmp)
  19. set(log "")
  20. # Parse implicit linker arguments.
  21. set(linker "CMAKE_LINKER-NOTFOUND")
  22. if(CMAKE_LINKER)
  23. get_filename_component(linker ${CMAKE_LINKER} NAME)
  24. endif()
  25. # Construct a regex to match linker lines. It must match both the
  26. # whole line and just the command (argv[0]).
  27. set(linker_regex "^( *|.*[/\\])(${linker}|ld|collect2)[^/\\]*( |$)")
  28. set(log "${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}")
  33. if(UNIX)
  34. separate_arguments(args UNIX_COMMAND "${line}")
  35. else()
  36. separate_arguments(args WINDOWS_COMMAND "${line}")
  37. endif()
  38. list(GET args 0 cmd)
  39. endif()
  40. if("${cmd}" MATCHES "${linker_regex}")
  41. set(log "${log} link line: [${line}]\n")
  42. string(REGEX REPLACE ";-([LYz]);" ";-\\1" args "${args}")
  43. foreach(arg IN LISTS args)
  44. if("${arg}" MATCHES "^-L(.:)?[/\\]")
  45. # Unix search path.
  46. string(REGEX REPLACE "^-L" "" dir "${arg}")
  47. list(APPEND implicit_dirs_tmp ${dir})
  48. set(log "${log} arg [${arg}] ==> dir [${dir}]\n")
  49. elseif("${arg}" MATCHES "^-l[^:]")
  50. # Unix library.
  51. string(REGEX REPLACE "^-l" "" lib "${arg}")
  52. list(APPEND implicit_libs_tmp ${lib})
  53. set(log "${log} arg [${arg}] ==> lib [${lib}]\n")
  54. elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.a$")
  55. # Unix library full path.
  56. list(APPEND implicit_libs_tmp ${arg})
  57. set(log "${log} arg [${arg}] ==> lib [${arg}]\n")
  58. elseif("${arg}" MATCHES "^-Y(P,)?")
  59. # Sun search path.
  60. string(REGEX REPLACE "^-Y(P,)?" "" dirs "${arg}")
  61. string(REPLACE ":" ";" dirs "${dirs}")
  62. list(APPEND implicit_dirs_tmp ${dirs})
  63. set(log "${log} arg [${arg}] ==> dirs [${dirs}]\n")
  64. elseif("${arg}" MATCHES "^-l:")
  65. # HP named library.
  66. list(APPEND implicit_libs_tmp ${arg})
  67. set(log "${log} arg [${arg}] ==> lib [${arg}]\n")
  68. elseif("${arg}" MATCHES "^-z(all|default|weak)extract")
  69. # Link editor option.
  70. list(APPEND implicit_libs_tmp ${arg})
  71. set(log "${log} arg [${arg}] ==> opt [${arg}]\n")
  72. else()
  73. set(log "${log} arg [${arg}] ==> ignore\n")
  74. endif()
  75. endforeach()
  76. break()
  77. elseif("${line}" MATCHES "LPATH(=| is:? )")
  78. set(log "${log} LPATH line: [${line}]\n")
  79. # HP search path.
  80. string(REGEX REPLACE ".*LPATH(=| is:? *)" "" paths "${line}")
  81. string(REPLACE ":" ";" paths "${paths}")
  82. list(APPEND implicit_dirs_tmp ${paths})
  83. set(log "${log} dirs [${paths}]\n")
  84. else()
  85. set(log "${log} ignore line: [${line}]\n")
  86. endif()
  87. endforeach()
  88. # Cleanup list of libraries and flags.
  89. # We remove items that are not language-specific.
  90. set(implicit_libs "")
  91. foreach(lib IN LISTS implicit_libs_tmp)
  92. if("${lib}" MATCHES "^(crt.*\\.o|gcc.*|System.*)$")
  93. set(log "${log} remove lib [${lib}]\n")
  94. else()
  95. list(APPEND implicit_libs "${lib}")
  96. endif()
  97. endforeach()
  98. # Cleanup list of directories.
  99. set(implicit_dirs "")
  100. foreach(d IN LISTS implicit_dirs_tmp)
  101. get_filename_component(dir "${d}" ABSOLUTE)
  102. list(APPEND implicit_dirs "${dir}")
  103. set(log "${log} collapse dir [${d}] ==> [${dir}]\n")
  104. endforeach()
  105. list(REMOVE_DUPLICATES implicit_dirs)
  106. # Log results.
  107. set(log "${log} implicit libs: [${implicit_libs}]\n")
  108. set(log "${log} implicit dirs: [${implicit_dirs}]\n")
  109. # Return results.
  110. set(${lib_var} "${implicit_libs}" PARENT_SCOPE)
  111. set(${dir_var} "${implicit_dirs}" PARENT_SCOPE)
  112. set(${log_var} "${log}" PARENT_SCOPE)
  113. endfunction()