tResolveCompilerPaths.cmake - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
 (HTM) git clone git://src.adamsgaard.dk/pism
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
       tResolveCompilerPaths.cmake (4183B)
       ---
            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 include (CorrectWindowsPaths)
           42 
           43 macro (RESOLVE_LIBRARIES LIBS LINK_LINE)
           44   string (REGEX MATCHALL "((-L|-l|-Wl)([^\" ]+|\"[^\"]+\")|[^\" ]+\\.(a|so|dll|lib))" _all_tokens "${LINK_LINE}")
           45   set (_libs_found "")
           46   set (_directory_list "")
           47   foreach (token ${_all_tokens})
           48     if (token MATCHES "-L([^\" ]+|\"[^\"]+\")")
           49       # If it's a library path, add it to the list
           50       string (REGEX REPLACE "^-L" "" token ${token})
           51       string (REGEX REPLACE "//" "/" token ${token})
           52       convert_cygwin_path(token)
           53       list (APPEND _directory_list ${token})
           54     elseif (token MATCHES "^(-l([^\" ]+|\"[^\"]+\")|[^\" ]+\\.(a|so|dll|lib))")
           55       # It's a library, resolve the path by looking in the list and then (by default) in system directories
           56       if (WIN32) #windows expects "libfoo", linux expects "foo"
           57         string (REGEX REPLACE "^-l" "lib" token ${token})
           58       else (WIN32)
           59         string (REGEX REPLACE "^-l" "" token ${token})
           60       endif (WIN32)
           61       set (_root "")
           62       if (token MATCHES "^/")        # We have an absolute path
           63         #separate into a path and a library name:
           64         string (REGEX MATCH "[^/]*\\.(a|so|dll|lib)$" libname ${token})
           65         string (REGEX MATCH ".*[^${libname}$]" libpath ${token})
           66         convert_cygwin_path(libpath)
           67         set (_directory_list ${_directory_list} ${libpath})
           68         set (token ${libname})
           69       endif (token MATCHES "^/")
           70       set (_lib "NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
           71       find_library (_lib ${token} HINTS ${_directory_list} ${_root})
           72       if (_lib)
           73         string (REPLACE "//" "/" _lib ${_lib})
           74         list (APPEND _libs_found ${_lib})
           75       else (_lib)
           76         message (STATUS "Unable to find library ${token}")
           77       endif (_lib)
           78     endif (token MATCHES "-L([^\" ]+|\"[^\"]+\")")
           79   endforeach (token)
           80   set (_lib "NOTFOUND" CACHE INTERNAL "Scratch variable" FORCE)
           81   # only the LAST occurence of each library is required since there should be no circular dependencies
           82   if (_libs_found)
           83     list (REVERSE _libs_found)
           84     list (REMOVE_DUPLICATES _libs_found)
           85     list (REVERSE _libs_found)
           86   endif (_libs_found)
           87   set (${LIBS} "${_libs_found}")
           88 endmacro (RESOLVE_LIBRARIES)
           89 
           90 macro (RESOLVE_INCLUDES INCS COMPILE_LINE)
           91   string (REGEX MATCHALL "-I([^\" ]+|\"[^\"]+\")" _all_tokens "${COMPILE_LINE}")
           92   set (_incs_found "")
           93   foreach (token ${_all_tokens})
           94     string (REGEX REPLACE "^-I" "" token ${token})
           95     string (REGEX REPLACE "//" "/" token ${token})
           96     convert_cygwin_path(token)
           97     if (EXISTS ${token})
           98       list (APPEND _incs_found ${token})
           99     else (EXISTS ${token})
          100       message (STATUS "Include directory ${token} does not exist")
          101     endif (EXISTS ${token})
          102   endforeach (token)
          103   list (REMOVE_DUPLICATES _incs_found)
          104   set (${INCS} "${_incs_found}")
          105 endmacro (RESOLVE_INCLUDES)