Aleš Erjavec avatar Aleš Erjavec committed 4d52aba

Added support for MSVC compiler.

Comments (0)

Files changed (2)

 
 from setuptools import setup, find_packages
 from setuptools.command.build_ext import build_ext as _build_ext
-
+from distutils.cygwinccompiler import CygwinCCompiler
+from distutils.msvccompiler import MSVCCompiler
 
 from ConfigParser import SafeConfigParser
 
 PACKAGES = find_packages()
 
 
+def mno_cygwin_fix(compiler):
+    """
+    Remove the '-mno-cygwin' flag from a mingw32 compiler commands.
+    """
+    for name in ["compiler", "compiler_so", "compiler_cxx",
+                 "linker_exe", "linker_so"]:
+        if "-mno-cygwin" in getattr(compiler, name):
+            getattr(compiler, name).remove("-mno-cygwin")
+
+
+def msvc_force_cpp_fix(compiler):
+    """
+    Force the MSVCcompiler instance to compile *.c sources as c++.
+    """
+    # Copy the ext mappings from class members to instance members
+    compiler._c_extensions = list(compiler._c_extensions)
+    compiler._cpp_extensions = list(compiler._cpp_extensions)
+    # make sure a .c file will be compiled as C++.
+    compiler._c_extensions.remove(".c")
+    compiler._cpp_extensions.append(".c")
+
+
 class build_ext(_build_ext):
     def run(self):
         # Run make sure the c libraries are build
         self.run_command("build_clib")
         _build_ext.run(self)
 
+    def build_extensions(self):
+        self._customize_compiler()
+        _build_ext.build_extensions(self)
+
+    def _customize_compiler(self):
+        if isinstance(self.compiler, CygwinCCompiler) and \
+                self.compiler.compiler_type == "mingw32":
+            mno_cygwin_fix(self.compiler)
+        elif isinstance(self.compiler,  MSVCCompiler):
+            msvc_force_cpp_fix(self.compiler)
+
 
 lib_config = namedtuple(
     "lib_config", ["libraries", "library_dirs"])
         define_macros=[("STANDALONE", 1),
                        ("USE_BLAS", 1),
                        ("MAIN", 1)],
-        libraries=libraries + ["m"],
+        libraries=libraries,
         library_dirs=library_dirs,
         extra_compile_args=["-std=c99"],
+        # export Earth function for ctypes (needed for MSVC)
+        export_symbols=["Earth"]
     )
 
     return config
-#include <stdbool.h>
+#ifndef _MSC_VER
+	#include <stdbool.h>
+#else
+	#define _Bool char
+	#define bool _Bool
+	#define true 1
+	#define false 0
+#endif
 
 #ifndef _MSC_VER
 	#define _finite isfinite
 
 #include "earth/earth.c"
 
+
+#ifndef _MSC_VER
+	#undef _finite
+	#undef _isnan
+#endif
+
 #include "Python.h"
 
 void init_earth(void) {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.