Commits

Aleš Erjavec  committed 4d52aba

Added support for MSVC compiler.

  • Participants
  • Parent commits bef07df

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

File src/_earth.c

-#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) {