v0.14.0
ResolveCompilerPaths.cmake
Go to the documentation of this file.
1 # ResolveCompilerPaths - this module defines two macros
2 #
3 # RESOLVE_LIBRARIES (XXX_LIBRARIES LINK_LINE)
4 # This macro is intended to be used by FindXXX.cmake modules.
5 # It parses a compiler link line and resolves all libraries
6 # (-lfoo) using the library path contexts (-L/path) in scope.
7 # The result in XXX_LIBRARIES is the list of fully resolved libs.
8 # Example:
9 #
10 # RESOLVE_LIBRARIES (FOO_LIBRARIES "-L/A -la -L/B -lb -lc -ld")
11 #
12 # will be resolved to
13 #
14 # FOO_LIBRARIES:STRING="/A/liba.so;/B/libb.so;/A/libc.so;/usr/lib/libd.so"
15 #
16 # if the filesystem looks like
17 #
18 # /A: liba.so libc.so
19 # /B: liba.so libb.so
20 # /usr/lib: liba.so libb.so libc.so libd.so
21 #
22 # and /usr/lib is a system directory.
23 #
24 # Note: If RESOLVE_LIBRARIES() resolves a link line differently from
25 # the native linker, there is a bug in this macro (please report it).
26 #
27 # RESOLVE_INCLUDES (XXX_INCLUDES INCLUDE_LINE)
28 # This macro is intended to be used by FindXXX.cmake modules.
29 # It parses a compile line and resolves all includes
30 # (-I/path/to/include) to a list of directories. Other flags are ignored.
31 # Example:
32 #
33 # RESOLVE_INCLUDES (FOO_INCLUDES "-I/A -DBAR='\"irrelevant -I/string here\"' -I/B")
34 #
35 # will be resolved to
36 #
37 # FOO_INCLUDES:STRING="/A;/B"
38 #
39 # assuming both directories exist.
40 # Note: as currently implemented, the -I/string will be picked up mistakenly (cry, cry)
41 
42 macro (RESOLVE_LIBRARIES_DIR LIBS_DIR LINK_LINE)
43  string (REGEX MATCHALL "-L([^\" ]+|\"[^\"]+\")" _all_tokens "${LINK_LINE}")
44  set (_libs_found)
45  set (_directory_list)
46  foreach (token ${_all_tokens})
47  string (REGEX REPLACE "-L" "" token ${token})
48  string (REGEX REPLACE "//" "/" token ${token})
49  string (REGEX REPLACE "\n" "" token ${token})
50  list (APPEND _directory_list ${token})
51  endforeach (token)
52  set (${LIBS_DIR} ${_directory_list})
53 endmacro (RESOLVE_LIBRARIES_DIR)
54 
55 macro (RESOLVE_LIBRARIES LIBS LINK_LINE)
56  #message(${LIBS})
57  #message(${LINK_LINE})
58  string (REGEX MATCHALL "(((-L|-l|-Wl)([^\" ]+|\"[^\"]+\")|/[^\" ]+(a|so|dll|dylib))|(-framework Accelerate))" _all_tokens "${LINK_LINE}")
59  #message(${_all_tokens})
60  set (_libs_found)
61  set (_directory_list)
62  foreach (token ${_all_tokens})
63  if (token MATCHES "-L([^\" ]+|\"[^\"]+\")")
64  # If it's a library path, add it to the list
65  string (REGEX REPLACE "^-L" "" token ${token})
66  string (REGEX REPLACE "//" "/" token ${token})
67  string (REGEX REPLACE "\n" "" token ${token})
68  list (APPEND _directory_list ${token})
69  endif (token MATCHES "-L([^\" ]+|\"[^\"]+\")")
70  endforeach (token)
71  foreach (token ${_all_tokens})
72  if (token MATCHES "^(-l([^\" ]+|\"[^\"]+\")|/[^\" ]+(a|so|dll|dylib))")
73  string (REGEX REPLACE "\n" "" token ${token})
74  # It's a library, resolve the path by looking in the list and then (by default) in system directories
75  string (REGEX REPLACE "^-l" "" token ${token})
76  set (_root)
77  if (token MATCHES "^/") # We have an absolute path, add root to the search path
78  set (_root "/")
79  endif (token MATCHES "^/")
80  set (_lib "NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
81  find_library (_lib ${token} HINTS ${_directory_list} ${_root})
82  if (_lib)
83  string (REPLACE "//" "/" _lib ${_lib})
84  list (APPEND _libs_found ${_lib})
85  else (_lib)
86  #message (STATUS "Unable to find library ${token}")
87  endif (_lib)
88  endif (token MATCHES "^(-l([^\" ]+|\"[^\"]+\")|/[^\" ]+(a|so|dll|dylib))")
89  endforeach (token)
90  foreach (token ${_all_tokens})
91  if (token MATCHES "-framework Accelerate")
92  list(APPEND _libs_found ${token})
93  endif (token MATCHES "-framework Accelerate")
94  endforeach (token)
95  if (_libs_found)
96  list (REVERSE _libs_found)
97  list (REMOVE_DUPLICATES _libs_found)
98  list (REVERSE _libs_found)
99  endif (_libs_found)
100  set (${LIBS} "${_libs_found}")
101 endmacro (RESOLVE_LIBRARIES)
102 
103 macro (RESOLVE_INCLUDES INCS COMPILE_LINE)
104  string (REGEX MATCHALL "-I([^\" ]+|\"[^\"]+\")" _all_tokens "${COMPILE_LINE}")
105  set (_incs_found)
106  foreach (token ${_all_tokens})
107  string (REGEX REPLACE "^-I" "" token ${token})
108  string (REGEX REPLACE "//" "/" token ${token})
109  string (REGEX REPLACE "\n" "" token ${token})
110  if (EXISTS ${token})
111  list (APPEND _incs_found ${token})
112  else (EXISTS ${token})
113  #message (STATUS "Include directory ${token} does not exist")
114  endif (EXISTS ${token})
115  endforeach (token)
116  if (_libs_found)
117  list (REMOVE_DUPLICATES _incs_found)
118  endif (_libs_found)
119  set (${INCS} "${_incs_found}")
120 endmacro (RESOLVE_INCLUDES)
121 
122 macro (RESOLVE_DEFINITIONS DEF COMPILE_LINE)
123  string (REGEX MATCHALL "-D[0-9A-Z_]+(=[a-zA-z0-9]+)?" _all_tokens "${COMPILE_LINE}")
124  # message(STATUS ${_all_tokens})
125  set (_incs_found)
126  foreach (token ${_all_tokens})
127  string (REGEX REPLACE "^-D" "-DMOAB_" token ${token})
128  list (APPEND _incs_found ${token})
129  endforeach (token)
130  if (_libs_found)
131  list (REMOVE_DUPLICATES _incs_found)
132  endif (_libs_found)
133  set (${DEF} "${_incs_found}")
134 endmacro (RESOLVE_DEFINITIONS)