Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

FindPETSc: Fixed paths and library names on windows

If PETSc static libs (/MT, static) disagree with existing library flags
(/MD, dynamic), we issue a warning telling the user to choose a
compatible compilation mode.

Windows library paths need not start with "/", requiring more permissive
regex patterns.

Squashed-by: Jed Brown <jed@59A2.org>
  • Loading branch information...
commit be91079d12bc1bca652dc289d217a098cb88ccf6 1 parent 0d33c69
John Fettig authored March 10, 2013 jedbrown committed March 10, 2013
14  CorrectWindowsPaths.cmake
... ...
@@ -0,0 +1,14 @@
  1
+# CorrectWindowsPaths - this module defines one macro
  2
+#
  3
+# CONVERT_CYGWIN_PATH( PATH )
  4
+#  This uses the command cygpath (provided by cygwin) to convert
  5
+#  unix-style paths into paths useable by cmake on windows
  6
+
  7
+macro (CONVERT_CYGWIN_PATH _path)
  8
+  if (WIN32)
  9
+    EXECUTE_PROCESS(COMMAND cygpath.exe -m ${${_path}}
  10
+      OUTPUT_VARIABLE ${_path})
  11
+    string (STRIP ${${_path}} ${_path})
  12
+  endif (WIN32)
  13
+endmacro (CONVERT_CYGWIN_PATH)
  14
+
31  FindPETSc.cmake
@@ -149,6 +149,7 @@ show :
149 149
   petsc_get_variable (PETSC_CCPPFLAGS          petsc_cpp_line)
150 150
   petsc_get_variable (PETSC_INCLUDE            petsc_include)
151 151
   petsc_get_variable (PCC                      petsc_cc)
  152
+  petsc_get_variable (PCC_FLAGS                petsc_cc_flags)
152 153
   petsc_get_variable (MPIEXEC                  petsc_mpiexec)
153 154
   # We are done with the temporary Makefile, calling PETSC_GET_VARIABLE after this point is invalid!
154 155
   file (REMOVE ${petsc_config_makefile})
@@ -157,11 +158,39 @@ show :
157 158
   # Extract include paths and libraries from compile command line
158 159
   resolve_includes (petsc_includes_all "${petsc_cpp_line}")
159 160
 
  161
+  #on windows we need to make sure we're linking against the right
  162
+  #runtime library
  163
+  if (WIN32)
  164
+    if (petsc_cc_flags MATCHES "-MT")
  165
+      set(using_md False)
  166
+      foreach(flag_var
  167
+          CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
  168
+          CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
  169
+          CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
  170
+          CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
  171
+        if(${flag_var} MATCHES "/MD")
  172
+          set(using_md True)
  173
+        endif(${flag_var} MATCHES "/MD")
  174
+      endforeach(flag_var)
  175
+      if(${using_md} MATCHES "True")
  176
+        message(WARNING "PETSc was built with /MT, but /MD is currently set.
  177
+ See http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F")
  178
+      endif(${using_md} MATCHES "True")
  179
+    endif (petsc_cc_flags MATCHES "-MT")
  180
+  endif (WIN32)
  181
+
  182
+  include (CorrectWindowsPaths)
  183
+  convert_cygwin_path(petsc_lib_dir)
160 184
   message (STATUS "petsc_lib_dir ${petsc_lib_dir}")
161 185
 
162 186
   macro (PETSC_FIND_LIBRARY suffix name)
163 187
     set (PETSC_LIBRARY_${suffix} "NOTFOUND" CACHE INTERNAL "Cleared" FORCE) # Clear any stale value, if we got here, we need to find it again
164  
-    find_library (PETSC_LIBRARY_${suffix} NAMES ${name} HINTS ${petsc_lib_dir} NO_DEFAULT_PATH)
  188
+    if (WIN32)
  189
+      set (libname lib${name}) #windows expects "libfoo", linux expects "foo"
  190
+    else (WIN32)
  191
+      set (libname ${name})
  192
+    endif (WIN32)
  193
+    find_library (PETSC_LIBRARY_${suffix} NAMES ${libname} HINTS ${petsc_lib_dir} NO_DEFAULT_PATH)
165 194
     set (PETSC_LIBRARIES_${suffix} "${PETSC_LIBRARY_${suffix}}")
166 195
     mark_as_advanced (PETSC_LIBRARY_${suffix})
167 196
   endmacro (PETSC_FIND_LIBRARY suffix name)
22  ResolveCompilerPaths.cmake
@@ -38,9 +38,10 @@
38 38
 #
39 39
 #  assuming both directories exist.
40 40
 #  Note: as currently implemented, the -I/string will be picked up mistakenly (cry, cry)
  41
+include (CorrectWindowsPaths)
41 42
 
42 43
 macro (RESOLVE_LIBRARIES LIBS LINK_LINE)
43  
-  string (REGEX MATCHALL "((-L|-l|-Wl)([^\" ]+|\"[^\"]+\")|/[^\" ]+(a|so|dll))" _all_tokens "${LINK_LINE}")
  44
+  string (REGEX MATCHALL "((-L|-l|-Wl)([^\" ]+|\"[^\"]+\")|[^\" ]+(a|so|dll|lib))" _all_tokens "${LINK_LINE}")
44 45
   set (_libs_found)
45 46
   set (_directory_list)
46 47
   foreach (token ${_all_tokens})
@@ -48,13 +49,23 @@ macro (RESOLVE_LIBRARIES LIBS LINK_LINE)
48 49
       # If it's a library path, add it to the list
49 50
       string (REGEX REPLACE "^-L" "" token ${token})
50 51
       string (REGEX REPLACE "//" "/" token ${token})
  52
+      convert_cygwin_path(token)
51 53
       list (APPEND _directory_list ${token})
52  
-    elseif (token MATCHES "^(-l([^\" ]+|\"[^\"]+\")|/[^\" ]+(a|so|dll))")
  54
+    elseif (token MATCHES "^(-l([^\" ]+|\"[^\"]+\")|[^\" ]+(a|so|dll|lib))")
53 55
       # It's a library, resolve the path by looking in the list and then (by default) in system directories
54  
-      string (REGEX REPLACE "^-l" "" token ${token})
  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)
55 61
       set (_root)
56  
-      if (token MATCHES "^/")	# We have an absolute path, add root to the search path
57  
-	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})
58 69
       endif (token MATCHES "^/")
59 70
       set (_lib "NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
60 71
       find_library (_lib ${token} HINTS ${_directory_list} ${_root})
@@ -82,6 +93,7 @@ macro (RESOLVE_INCLUDES INCS COMPILE_LINE)
82 93
   foreach (token ${_all_tokens})
83 94
     string (REGEX REPLACE "^-I" "" token ${token})
84 95
     string (REGEX REPLACE "//" "/" token ${token})
  96
+    convert_cygwin_path(token)
85 97
     if (EXISTS ${token})
86 98
       list (APPEND _incs_found ${token})
87 99
     else (EXISTS ${token})

0 notes on commit be91079

Please sign in to comment.
Something went wrong with that request. Please try again.