1 ##Copyright (C) jedbrown, johnfettig.
4 ##Redistribution and use in source and binary forms, with or without modification,
5 ##are permitted provided that the following conditions are met:
7 ##* Redistributions of source code must retain the above copyright notice, this
8 ## list of conditions and the following disclaimer.
10 ##* Redistributions in binary form must reproduce the above copyright notice, this
11 ## list of conditions and the following disclaimer in the documentation and/or
12 ## other materials provided with the distribution.
14 ##THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ##ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 ##WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 ##DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18 ##ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 ##(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 ##LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21 ##ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 ##(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 ##SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 # ResolveCompilerPaths - this module defines two macros
27 # RESOLVE_LIBRARIES (XXX_LIBRARIES LINK_LINE)
28 # This macro is intended to be used by FindXXX.cmake modules.
29 # It parses a compiler link line and resolves all libraries
30 # (-lfoo) using the library path contexts (-L/path) in scope.
31 # The result in XXX_LIBRARIES is the list of fully resolved libs.
34 # RESOLVE_LIBRARIES (FOO_LIBRARIES "-L/A -la -L/B -lb -lc -ld")
38 # FOO_LIBRARIES:STRING="/A/liba.so;/B/libb.so;/A/libc.so;/usr/lib/libd.so"
40 # if the filesystem looks like
44 # /usr/lib: liba.so libb.so libc.so libd.so
46 # and /usr/lib is a system directory.
48 # Note: If RESOLVE_LIBRARIES() resolves a link line differently from
49 # the native linker, there is a bug in this macro (please report it).
51 # RESOLVE_INCLUDES (XXX_INCLUDES INCLUDE_LINE)
52 # This macro is intended to be used by FindXXX.cmake modules.
53 # It parses a compile line and resolves all includes
54 # (-I/path/to/include) to a list of directories. Other flags are ignored.
57 # RESOLVE_INCLUDES (FOO_INCLUDES "-I/A -DBAR='\"irrelevant -I/string here\"' -I/B")
61 # FOO_INCLUDES:STRING="/A;/B"
63 # assuming both directories exist.
64 # Note: as currently implemented, the -I/string will be picked up mistakenly (cry, cry)
65 include (CorrectWindowsPaths)
67 macro (RESOLVE_LIBRARIES LIBS LINK_LINE)
68 string (REGEX MATCHALL "((-L|-l|-Wl)([^\" ]+|\"[^\"]+\")|[^\" ]+\\.(a|so|dll|lib))" _all_tokens "${LINK_LINE}")
71 foreach (token ${_all_tokens})
72 if (token MATCHES "-L([^\" ]+|\"[^\"]+\")")
73 # If it's a library path, add it to the list
74 string (REGEX REPLACE "^-L" "" token ${token})
75 string (REGEX REPLACE "//" "/" token ${token})
76 convert_cygwin_path(token)
77 list (APPEND _directory_list ${token})
78 elseif (token MATCHES "^(-l([^\" ]+|\"[^\"]+\")|[^\" ]+\\.(a|so|dll|lib))")
79 # It's a library, resolve the path by looking in the list and then (by default) in system directories
80 if (WIN32) #windows expects "libfoo", linux expects "foo"
81 string (REGEX REPLACE "^-l" "lib" token ${token})
83 string (REGEX REPLACE "^-l" "" token ${token})
86 if (token MATCHES "^/") # We have an absolute path
87 #separate into a path and a library name:
88 string (REGEX MATCH "[^/]*\\.(a|so|dll|lib)$" libname ${token})
89 string (REGEX MATCH ".*[^${libname}$]" libpath ${token})
90 convert_cygwin_path(libpath)
91 set (_directory_list ${_directory_list} ${libpath})
92 set (token ${libname})
93 endif (token MATCHES "^/")
94 set (_lib "NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
95 find_library (_lib ${token} HINTS ${_directory_list} ${_root})
97 string (REPLACE "//" "/" _lib ${_lib})
98 list (APPEND _libs_found ${_lib})
100 message (STATUS "Unable to find library ${token}")
102 endif (token MATCHES "-L([^\" ]+|\"[^\"]+\")")
104 set (_lib "NOTFOUND" CACHE INTERNAL "Scratch variable" FORCE)
105 # only the LAST occurence of each library is required since there should be no circular dependencies
107 list (REVERSE _libs_found)
108 list (REMOVE_DUPLICATES _libs_found)
109 list (REVERSE _libs_found)
111 set (${LIBS} "${_libs_found}")
112 endmacro (RESOLVE_LIBRARIES)
114 macro (RESOLVE_INCLUDES INCS COMPILE_LINE)
115 string (REGEX MATCHALL "-I([^\" ]+|\"[^\"]+\")" _all_tokens "${COMPILE_LINE}")
117 foreach (token ${_all_tokens})
118 string (REGEX REPLACE "^-I" "" token ${token})
119 string (REGEX REPLACE "//" "/" token ${token})
120 convert_cygwin_path(token)
122 list (APPEND _incs_found ${token})
123 else (EXISTS ${token})
124 message (STATUS "Include directory ${token} does not exist")
125 endif (EXISTS ${token})
127 list (REMOVE_DUPLICATES _incs_found)
128 set (${INCS} "${_incs_found}")
129 endmacro (RESOLVE_INCLUDES)