Salome HOME
Mesh should be set befor giving input fields
[tools/solverlab.git] / cmake_files / ResolveCompilerPaths.cmake
1 ##Copyright (C) jedbrown, johnfettig.
2 ##All rights reserved.
3 ##
4 ##Redistribution and use in source and binary forms, with or without modification,
5 ##are permitted provided that the following conditions are met:
6 ##
7 ##* Redistributions of source code must retain the above copyright notice, this
8 ##  list of conditions and the following disclaimer.
9 ##
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.
13 ##
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.
24
25 # ResolveCompilerPaths - this module defines two macros
26 #
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.
32 #  Example:
33 #
34 #    RESOLVE_LIBRARIES (FOO_LIBRARIES "-L/A -la -L/B -lb -lc -ld")
35 #
36 #  will be resolved to
37 #
38 #    FOO_LIBRARIES:STRING="/A/liba.so;/B/libb.so;/A/libc.so;/usr/lib/libd.so"
39 #
40 #  if the filesystem looks like
41 #
42 #    /A:       liba.so         libc.so
43 #    /B:       liba.so libb.so
44 #    /usr/lib: liba.so libb.so libc.so libd.so
45 #
46 #  and /usr/lib is a system directory.
47 #
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).
50 #
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.
55 #  Example:
56 #
57 #    RESOLVE_INCLUDES (FOO_INCLUDES "-I/A -DBAR='\"irrelevant -I/string here\"' -I/B")
58 #
59 #  will be resolved to
60 #
61 #    FOO_INCLUDES:STRING="/A;/B"
62 #
63 #  assuming both directories exist.
64 #  Note: as currently implemented, the -I/string will be picked up mistakenly (cry, cry)
65 include (CorrectWindowsPaths)
66
67 macro (RESOLVE_LIBRARIES LIBS LINK_LINE)
68   string (REGEX MATCHALL "((-L|-l|-Wl)([^\" ]+|\"[^\"]+\")|[^\" ]+\\.(a|so|dll|lib))" _all_tokens "${LINK_LINE}")
69   set (_libs_found)
70   set (_directory_list)
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})
82       else (WIN32)
83         string (REGEX REPLACE "^-l" "" token ${token})
84       endif (WIN32)
85       set (_root)
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})
96       if (_lib)
97         string (REPLACE "//" "/" _lib ${_lib})
98         list (APPEND _libs_found ${_lib})
99       else (_lib)
100         message (STATUS "Unable to find library ${token}")
101       endif (_lib)
102     endif (token MATCHES "-L([^\" ]+|\"[^\"]+\")")
103   endforeach (token)
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
106   if (_libs_found)
107     list (REVERSE _libs_found)
108     list (REMOVE_DUPLICATES _libs_found)
109     list (REVERSE _libs_found)
110   endif (_libs_found)
111   set (${LIBS} "${_libs_found}")
112 endmacro (RESOLVE_LIBRARIES)
113
114 macro (RESOLVE_INCLUDES INCS COMPILE_LINE)
115   string (REGEX MATCHALL "-I([^\" ]+|\"[^\"]+\")" _all_tokens "${COMPILE_LINE}")
116   set (_incs_found)
117   foreach (token ${_all_tokens})
118     string (REGEX REPLACE "^-I" "" token ${token})
119     string (REGEX REPLACE "//" "/" token ${token})
120     convert_cygwin_path(token)
121     if (EXISTS ${token})
122       list (APPEND _incs_found ${token})
123     else (EXISTS ${token})
124       message (STATUS "Include directory ${token} does not exist")
125     endif (EXISTS ${token})
126   endforeach (token)
127   list (REMOVE_DUPLICATES _incs_found)
128   set (${INCS} "${_incs_found}")
129 endmacro (RESOLVE_INCLUDES)