|
| 1 | +""" |
| 2 | +Setup script for building clr.pyd and dependencies using mono and into |
| 3 | +an egg or wheel. |
| 4 | +""" |
| 5 | +from setuptools import setup, Extension |
| 6 | +from distutils.command.build_ext import build_ext |
| 7 | +from distutils.sysconfig import get_config_vars |
| 8 | +from platform import architecture |
| 9 | +from subprocess import check_output, check_call |
| 10 | +import shutil |
| 11 | +import sys |
| 12 | +import os |
| 13 | + |
| 14 | +CONFIG = "Release" # Release or Debug |
| 15 | +DEVTOOLS = "Mono" # Mono or MsDev |
| 16 | +VERBOSITY = "minimal" # quiet, minimal, normal, detailed, diagnostic |
| 17 | + |
| 18 | +if DEVTOOLS == "MsDev": |
| 19 | + from distutils import msvc9compiler |
| 20 | + msvc9compiler.VERSION = 11 |
| 21 | + |
| 22 | + cc = msvc9compiler.MSVCCompiler() |
| 23 | + cc.initialize() |
| 24 | + _xbuild = cc.find_exe("msbuild.exe") |
| 25 | + _defines_sep = ";" |
| 26 | + _config = "%sWin" % CONFIG |
| 27 | + |
| 28 | +elif DEVTOOLS == "Mono": |
| 29 | + _xbuild = "xbuild" |
| 30 | + _defines_sep = "," |
| 31 | + _config = "%sMono" % CONFIG |
| 32 | + |
| 33 | +else: |
| 34 | + raise NotImplementedError("DevTools %s not supported (use MsDev or Mono)" % DEVTOOLS) |
| 35 | + |
| 36 | +_platform = "x64" if architecture()[0] == "64bit" else "x86" |
| 37 | + |
| 38 | +class PythonNET_BuildExt(build_ext): |
| 39 | + |
| 40 | + def build_extension(self, ext): |
| 41 | + """ |
| 42 | + Builds the .pyd file using msbuild or xbuild. |
| 43 | + """ |
| 44 | + if ext.name != "clr": |
| 45 | + return super(PythonNET_BuildExt, self).build_extension(ext) |
| 46 | + |
| 47 | + dest_file = self.get_ext_fullpath(ext.name) |
| 48 | + dest_dir = os.path.dirname(dest_file) |
| 49 | + if not os.path.exists(dest_dir): |
| 50 | + os.makedirs(dest_dir) |
| 51 | + |
| 52 | + defines = [ |
| 53 | + "PYTHON%d%s" % (sys.version_info[:2]), |
| 54 | + "UCS2" if sys.maxunicode < 0x10FFFF else "UCS4", |
| 55 | + ] |
| 56 | + |
| 57 | + if CONFIG == "Debug": |
| 58 | + defines.extend(["DEBUG", "TRACE"]) |
| 59 | + |
| 60 | + cmd = [ |
| 61 | + _xbuild, |
| 62 | + "pythonnet.sln", |
| 63 | + "/p:Configuration=%s" % _config, |
| 64 | + "/p:Platform=%s" % _platform, |
| 65 | + "/p:DefineConstants=\"%s\"" % _defines_sep.join(defines), |
| 66 | + "/p:PythonBuildDir=%s" % os.path.abspath(dest_dir), |
| 67 | + "/p:NoNuGet=true", |
| 68 | + "/verbosity:%s" % VERBOSITY, |
| 69 | + ] |
| 70 | + |
| 71 | + self.announce("Building: %s" % " ".join(cmd)) |
| 72 | + check_call(" ".join(cmd) + " /t:Clean", shell=True) |
| 73 | + check_call(" ".join(cmd) + " /t:Build", shell=True) |
| 74 | + |
| 75 | + if DEVTOOLS == "Mono": |
| 76 | + self._build_monoclr(ext) |
| 77 | + |
| 78 | + |
| 79 | + def _build_monoclr(self, ext): |
| 80 | + mono_libs = check_output("pkg-config --libs mono-2", shell=True) |
| 81 | + mono_cflags = check_output("pkg-config --cflags mono-2", shell=True) |
| 82 | + glib_libs = check_output("pkg-config --libs glib-2.0", shell=True) |
| 83 | + glib_cflags = check_output("pkg-config --cflags glib-2.0", shell=True) |
| 84 | + cflags = mono_cflags.strip() + " " + glib_cflags.strip() |
| 85 | + libs = mono_libs.strip() + " " + glib_libs.strip() |
| 86 | + |
| 87 | + # build the clr python module |
| 88 | + setup(name="monoclr", |
| 89 | + ext_modules=[ |
| 90 | + Extension("clr", |
| 91 | + sources=[ |
| 92 | + "src/monoclr/pynetinit.c", |
| 93 | + "src/monoclr/clrmod.c" |
| 94 | + ], |
| 95 | + extra_compile_args=cflags.split(" "), |
| 96 | + extra_link_args=libs.split(" "), |
| 97 | + )] |
| 98 | + ) |
| 99 | + |
| 100 | + # build the clr python executable |
| 101 | + sources = [ |
| 102 | + "src/monoclr/pynetinit.c", |
| 103 | + "src/monoclr/python.c", |
| 104 | + ] |
| 105 | + |
| 106 | + macros = ext.define_macros[:] |
| 107 | + for undef in ext.undef_macros: |
| 108 | + macros.append((undef,)) |
| 109 | + |
| 110 | + objects = self.compiler.compile(sources, |
| 111 | + output_dir=self.build_temp, |
| 112 | + macros=macros, |
| 113 | + include_dirs=ext.include_dirs, |
| 114 | + debug=self.debug, |
| 115 | + extra_postargs=cflags.split(" "), |
| 116 | + depends=ext.depends) |
| 117 | + |
| 118 | + output_dir = os.path.dirname(self.get_ext_fullpath(ext.name)) |
| 119 | + py_libs = get_config_vars("BLDLIBRARY")[0] |
| 120 | + libs += " " + py_libs |
| 121 | + |
| 122 | + self.compiler.link_executable(objects, |
| 123 | + "npython", |
| 124 | + output_dir=output_dir, |
| 125 | + libraries=self.get_libraries(ext), |
| 126 | + library_dirs=ext.library_dirs, |
| 127 | + runtime_library_dirs=ext.runtime_library_dirs, |
| 128 | + extra_postargs=libs.split(" "), |
| 129 | + debug=self.debug) |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + setup(name="pythonnet", |
| 134 | + ext_modules=[ |
| 135 | + Extension("clr", sources=[]) |
| 136 | + ], |
| 137 | + cmdclass = { |
| 138 | + "build_ext" : PythonNET_BuildExt |
| 139 | + } |
| 140 | + ) |
| 141 | + |
0 commit comments