CMakeParseImplicitLinkInfo.cmake 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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(linker_exclude_regex "collect2 version ")
  29. set(log "${log} link line regex: [${linker_regex}]\n")
  30. string(REGEX REPLACE "\r?\n" ";" output_lines "${text}")
  31. foreach(line IN LISTS output_lines)
  32. set(cmd)
  33. if("${line}" MATCHES "${linker_regex}" AND
  34. NOT "${line}" MATCHES "${linker_exclude_regex}")
  35. if(UNIX)
  36. separate_arguments(args UNIX_COMMAND "${line}")
  37. else()
  38. separate_arguments(args WINDOWS_COMMAND "${line}")
  39. endif()
  40. list(GET args 0 cmd)
  41. endif()
  42. if("${cmd}" MATCHES "${linker_regex}")
  43. set(log "${log} link line: [${line}]\n")
  44. string(REGEX REPLACE ";-([LYz]);" ";-\\1" args "${args}")
  45. foreach(arg IN LISTS args)
  46. if("${arg}" MATCHES "^-L(.:)?[/\\]")
  47. # Unix search path.
  48. string(REGEX REPLACE "^-L" "" dir "${arg}")
  49. list(APPEND implicit_dirs_tmp ${dir})
  50. set(log "${log} arg [${arg}] ==> dir [${dir}]\n")
  51. elseif("${arg}" MATCHES "^-l[^:]")
  52. # Unix library.
  53. string(REGEX REPLACE "^-l" "" lib "${arg}")
  54. list(APPEND implicit_libs_tmp ${lib})
  55. set(log "${log} arg [${arg}] ==> lib [${lib}]\n")
  56. elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.a$")
  57. # Unix library full path.
  58. list(APPEND implicit_libs_tmp ${arg})
  59. set(log "${log} arg [${arg}] ==> lib [${arg}]\n")
  60. elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.o$"
  61. AND obj_regex AND "${arg}" MATCHES "${obj_regex}")
  62. # Object file full path.
  63. list(APPEND implicit_libs_tmp ${arg})
  64. set(log "${log} arg [${arg}] ==> obj [${arg}]\n")
  65. elseif("${arg}" MATCHES "^-Y(P,)?[^0-9]")
  66. # Sun search path ([^0-9] avoids conflict with Mac -Y<num>).
  67. string(REGEX REPLACE "^-Y(P,)?" "" dirs "${arg}")
  68. string(REPLACE ":" ";" dirs "${dirs}")
  69. list(APPEND implicit_dirs_tmp ${dirs})
  70. set(log "${log} arg [${arg}] ==> dirs [${dirs}]\n")
  71. elseif("${arg}" MATCHES "^-l:")
  72. # HP named library.
  73. list(APPEND implicit_libs_tmp ${arg})
  74. set(log "${log} arg [${arg}] ==> lib [${arg}]\n")
  75. elseif("${arg}" MATCHES "^-z(all|default|weak)extract")
  76. # Link editor option.
  77. list(APPEND implicit_libs_tmp ${arg})
  78. set(log "${log} arg [${arg}] ==> opt [${arg}]\n")
  79. else()
  80. set(log "${log} arg [${arg}] ==> ignore\n")
  81. endif()
  82. endforeach()
  83. break()
  84. elseif("${line}" MATCHES "LPATH(=| is:? )")
  85. set(log "${log} LPATH line: [${line}]\n")
  86. # HP search path.
  87. string(REGEX REPLACE ".*LPATH(=| is:? *)" "" paths "${line}")
  88. string(REPLACE ":" ";" paths "${paths}")
  89. list(APPEND implicit_dirs_tmp ${paths})
  90. set(log "${log} dirs [${paths}]\n")
  91. else()
  92. set(log "${log} ignore line: [${line}]\n")
  93. endif()
  94. endforeach()
  95. # Cleanup list of libraries and flags.
  96. # We remove items that are not language-specific.
  97. set(implicit_libs "")
  98. foreach(lib IN LISTS implicit_libs_tmp)
  99. if("${lib}" MATCHES "^(crt.*\\.o|gcc.*|System.*)$")
  100. set(log "${log} remove lib [${lib}]\n")
  101. else()
  102. list(APPEND implicit_libs "${lib}")
  103. endif()
  104. endforeach()
  105. # Cleanup list of directories.
  106. set(implicit_dirs "")
  107. foreach(d IN LISTS implicit_dirs_tmp)
  108. get_filename_component(dir "${d}" ABSOLUTE)
  109. list(APPEND implicit_dirs "${dir}")
  110. set(log "${log} collapse dir [${d}] ==> [${dir}]\n")
  111. endforeach()
  112. list(REMOVE_DUPLICATES implicit_dirs)
  113. # Log results.
  114. set(log "${log} implicit libs: [${implicit_libs}]\n")
  115. set(log "${log} implicit dirs: [${implicit_dirs}]\n")
  116. # Return results.
  117. set(${lib_var} "${implicit_libs}" PARENT_SCOPE)
  118. set(${dir_var} "${implicit_dirs}" PARENT_SCOPE)
  119. set(${log_var} "${log}" PARENT_SCOPE)
  120. endfunction()