2 #=============================================================================
3 # Copyright 2009 Kitware, Inc.
5 # Distributed under the OSI-approved BSD License (the "License");
6 # see accompanying file Copyright.txt for details.
8 # This software is distributed WITHOUT ANY WARRANTY; without even the
9 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 # See the License for more information.
11 #=============================================================================
12 # (To distribute this file outside of CMake, substitute the full
13 # License text for the above reference.)
15 # Function parse implicit linker options.
16 # This is used internally by CMake and should not be included by user
19 function(CMAKE_PARSE_IMPLICIT_LINK_INFO text lib_var dir_var log_var obj_regex)
20 set(implicit_libs_tmp "")
21 set(implicit_dirs_tmp)
24 # Parse implicit linker arguments.
25 set(linker "CMAKE_LINKER-NOTFOUND")
27 get_filename_component(linker ${CMAKE_LINKER} NAME)
29 # Construct a regex to match linker lines. It must match both the
30 # whole line and just the command (argv[0]).
31 set(linker_regex "^( *|.*[/\\])(${linker}|ld|collect2)[^/\\]*( |$)")
32 set(linker_exclude_regex "collect2 version ")
33 set(log "${log} link line regex: [${linker_regex}]\n")
34 string(REGEX REPLACE "\r?\n" ";" output_lines "${text}")
35 foreach(line IN LISTS output_lines)
37 if("${line}" MATCHES "${linker_regex}" AND
38 NOT "${line}" MATCHES "${linker_exclude_regex}")
40 separate_arguments(args UNIX_COMMAND "${line}")
42 separate_arguments(args WINDOWS_COMMAND "${line}")
46 if("${cmd}" MATCHES "${linker_regex}")
47 set(log "${log} link line: [${line}]\n")
48 string(REGEX REPLACE ";-([LYz]);" ";-\\1" args "${args}")
49 foreach(arg IN LISTS args)
50 if("${arg}" MATCHES "^-L(.:)?[/\\]")
52 string(REGEX REPLACE "^-L" "" dir "${arg}")
53 list(APPEND implicit_dirs_tmp ${dir})
54 set(log "${log} arg [${arg}] ==> dir [${dir}]\n")
55 elseif("${arg}" MATCHES "^-l[^:]")
57 string(REGEX REPLACE "^-l" "" lib "${arg}")
58 list(APPEND implicit_libs_tmp ${lib})
59 set(log "${log} arg [${arg}] ==> lib [${lib}]\n")
60 elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.a$")
61 # Unix library full path.
62 list(APPEND implicit_libs_tmp ${arg})
63 set(log "${log} arg [${arg}] ==> lib [${arg}]\n")
64 elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.o$"
65 AND obj_regex AND "${arg}" MATCHES "${obj_regex}")
66 # Object file full path.
67 list(APPEND implicit_libs_tmp ${arg})
68 set(log "${log} arg [${arg}] ==> obj [${arg}]\n")
69 elseif("${arg}" MATCHES "^-Y(P,)?[^0-9]")
70 # Sun search path ([^0-9] avoids conflict with Mac -Y<num>).
71 string(REGEX REPLACE "^-Y(P,)?" "" dirs "${arg}")
72 string(REPLACE ":" ";" dirs "${dirs}")
73 list(APPEND implicit_dirs_tmp ${dirs})
74 set(log "${log} arg [${arg}] ==> dirs [${dirs}]\n")
75 elseif("${arg}" MATCHES "^-l:")
77 list(APPEND implicit_libs_tmp ${arg})
78 set(log "${log} arg [${arg}] ==> lib [${arg}]\n")
79 elseif("${arg}" MATCHES "^-z(all|default|weak)extract")
81 list(APPEND implicit_libs_tmp ${arg})
82 set(log "${log} arg [${arg}] ==> opt [${arg}]\n")
84 set(log "${log} arg [${arg}] ==> ignore\n")
88 elseif("${line}" MATCHES "LPATH(=| is:? )")
89 set(log "${log} LPATH line: [${line}]\n")
91 string(REGEX REPLACE ".*LPATH(=| is:? *)" "" paths "${line}")
92 string(REPLACE ":" ";" paths "${paths}")
93 list(APPEND implicit_dirs_tmp ${paths})
94 set(log "${log} dirs [${paths}]\n")
96 set(log "${log} ignore line: [${line}]\n")
100 # Cleanup list of libraries and flags.
101 # We remove items that are not language-specific.
102 set(implicit_libs "")
103 foreach(lib IN LISTS implicit_libs_tmp)
104 if("${lib}" MATCHES "^(crt.*\\.o|gcc.*|System.*)$")
105 set(log "${log} remove lib [${lib}]\n")
107 list(APPEND implicit_libs "${lib}")
111 # Cleanup list of directories.
112 set(implicit_dirs "")
113 foreach(d IN LISTS implicit_dirs_tmp)
114 get_filename_component(dir "${d}" ABSOLUTE)
115 list(APPEND implicit_dirs "${dir}")
116 set(log "${log} collapse dir [${d}] ==> [${dir}]\n")
118 list(REMOVE_DUPLICATES implicit_dirs)
121 set(log "${log} implicit libs: [${implicit_libs}]\n")
122 set(log "${log} implicit dirs: [${implicit_dirs}]\n")
125 set(${lib_var} "${implicit_libs}" PARENT_SCOPE)
126 set(${dir_var} "${implicit_dirs}" PARENT_SCOPE)
127 set(${log_var} "${log}" PARENT_SCOPE)