CMakeParseImplicitLinkInfo.cmake 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 obj_regex)
  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 "^(.:)?[/\\].*\\.o$"
  59. AND obj_regex AND "${arg}" MATCHES "${obj_regex}")
  60. # Object file full path.
  61. list(APPEND implicit_libs_tmp ${arg})
  62. set(log "${log} arg [${arg}] ==> obj [${arg}]\n")
  63. elseif("${arg}" MATCHES "^-Y(P,)?")
  64. # Sun search path.
  65. string(REGEX REPLACE "^-Y(P,)?" "" dirs "${arg}")
  66. string(REPLACE ":" ";" dirs "${dirs}")
  67. list(APPEND implicit_dirs_tmp ${dirs})
  68. set(log "${log} arg [${arg}] ==> dirs [${dirs}]\n")
  69. elseif("${arg}" MATCHES "^-l:")
  70. # HP named library.
  71. list(APPEND implicit_libs_tmp ${arg})
  72. set(log "${log} arg [${arg}] ==> lib [${arg}]\n")
  73. elseif("${arg}" MATCHES "^-z(all|default|weak)extract")
  74. # Link editor option.
  75. list(APPEND implicit_libs_tmp ${arg})
  76. set(log "${log} arg [${arg}] ==> opt [${arg}]\n")
  77. else()
  78. set(log "${log} arg [${arg}] ==> ignore\n")
  79. endif()
  80. endforeach()
  81. break()
  82. elseif("${line}" MATCHES "LPATH(=| is:? )")
  83. set(log "${log} LPATH line: [${line}]\n")
  84. # HP search path.
  85. string(REGEX REPLACE ".*LPATH(=| is:? *)" "" paths "${line}")
  86. string(REPLACE ":" ";" paths "${paths}")
  87. list(APPEND implicit_dirs_tmp ${paths})
  88. set(log "${log} dirs [${paths}]\n")
  89. else()
  90. set(log "${log} ignore line: [${line}]\n")
  91. endif()
  92. endforeach()
  93. # Cleanup list of libraries and flags.
  94. # We remove items that are not language-specific.
  95. set(implicit_libs "")
  96. foreach(lib IN LISTS implicit_libs_tmp)
  97. if("${lib}" MATCHES "^(crt.*\\.o|gcc.*|System.*)$")
  98. set(log "${log} remove lib [${lib}]\n")
  99. else()
  100. list(APPEND implicit_libs "${lib}")
  101. endif()
  102. endforeach()
  103. # Cleanup list of directories.
  104. set(implicit_dirs "")
  105. foreach(d IN LISTS implicit_dirs_tmp)
  106. get_filename_component(dir "${d}" ABSOLUTE)
  107. list(APPEND implicit_dirs "${dir}")
  108. set(log "${log} collapse dir [${d}] ==> [${dir}]\n")
  109. endforeach()
  110. list(REMOVE_DUPLICATES implicit_dirs)
  111. # Log results.
  112. set(log "${log} implicit libs: [${implicit_libs}]\n")
  113. set(log "${log} implicit dirs: [${implicit_dirs}]\n")
  114. # Return results.
  115. set(${lib_var} "${implicit_libs}" PARENT_SCOPE)
  116. set(${dir_var} "${implicit_dirs}" PARENT_SCOPE)
  117. set(${log_var} "${log}" PARENT_SCOPE)
  118. endfunction()