44"""
55from setuptools import setup , Extension
66from distutils .command .build_ext import build_ext
7+ from distutils .command .build_scripts import build_scripts
8+ from distutils .command .install_lib import install_lib
79from distutils .sysconfig import get_config_vars
810from platform import architecture
911from subprocess import Popen , CalledProcessError , PIPE , check_call
12+ from glob import glob
1013import shutil
1114import sys
1215import os
2427 _xbuild = cc .find_exe ("msbuild.exe" )
2528 _defines_sep = ";"
2629 _config = "%sWin" % CONFIG
30+ _npython_exe = "nPython.exe"
2731
2832elif DEVTOOLS == "Mono" :
2933 _xbuild = "xbuild"
3034 _defines_sep = ","
3135 _config = "%sMono" % CONFIG
36+ _npython_exe = "npython"
3237
3338else :
3439 raise NotImplementedError ("DevTools %s not supported (use MsDev or Mono)" % DEVTOOLS )
@@ -42,7 +47,7 @@ def build_extension(self, ext):
4247 Builds the .pyd file using msbuild or xbuild.
4348 """
4449 if ext .name != "clr" :
45- return super ( PythonNET_BuildExt , self ) .build_extension (ext )
50+ return build_ext .build_extension (self , ext )
4651
4752 dest_file = self .get_ext_fullpath (ext .name )
4853 dest_dir = os .path .dirname (dest_file )
@@ -85,17 +90,15 @@ def _build_monoclr(self, ext):
8590 libs = mono_libs .strip () + " " + glib_libs .strip ()
8691
8792 # build the clr python module
88- setup (name = "monoclr" ,
89- ext_modules = [
90- Extension ("clr" ,
93+ clr_ext = Extension ("clr" ,
9194 sources = [
9295 "src/monoclr/pynetinit.c" ,
9396 "src/monoclr/clrmod.c"
9497 ],
9598 extra_compile_args = cflags .split (" " ),
96- extra_link_args = libs .split (" " ),
97- )]
98- )
99+ extra_link_args = libs .split (" " ))
100+
101+ build_ext . build_extension ( self , clr_ext )
99102
100103 # build the clr python executable
101104 sources = [
@@ -120,7 +123,7 @@ def _build_monoclr(self, ext):
120123 libs += " " + py_libs
121124
122125 self .compiler .link_executable (objects ,
123- "npython" ,
126+ _npython_exe ,
124127 output_dir = output_dir ,
125128 libraries = self .get_libraries (ext ),
126129 library_dirs = ext .library_dirs ,
@@ -129,6 +132,41 @@ def _build_monoclr(self, ext):
129132 debug = self .debug )
130133
131134
135+ class PythonNET_InstallLib (install_lib ):
136+
137+ def install (self ):
138+ if not os .path .isdir (self .build_dir ):
139+ self .warn ("'%s' does not exist -- no Python modules to install" %
140+ self .build_dir )
141+ return
142+
143+ if not os .path .exists (self .install_dir ):
144+ self .mkpath (self .install_dir )
145+
146+ # only copy clr.pyd and its dependencies
147+ for pattern in ("clr.*" , "Python.Runtime.*" ):
148+ for srcfile in glob (os .path .join (self .build_dir , pattern )):
149+ destfile = os .path .join (self .install_dir , os .path .basename (srcfile ))
150+ self .copy_file (srcfile , destfile )
151+
152+
153+ class PythonNET_BuildScripts (build_scripts ):
154+
155+ def finalize_options (self ):
156+ build_scripts .finalize_options (self )
157+
158+ # fixup scripts to look in the build_ext output folder
159+ if self .scripts :
160+ build_ext = self .get_finalized_command ("build_ext" )
161+ output_dir = os .path .dirname (build_ext .get_ext_fullpath (_npython_exe ))
162+ scripts = []
163+ for script in self .scripts :
164+ if os .path .exists (os .path .join (output_dir , script )):
165+ script = os .path .join (output_dir , script )
166+ scripts .append (script )
167+ self .scripts = scripts
168+
169+
132170def _check_output (* popenargs , ** kwargs ):
133171 """subprocess.check_output from python 2.7.
134172 Added here to support building for earlier versions
@@ -147,11 +185,15 @@ def _check_output(*popenargs, **kwargs):
147185
148186if __name__ == "__main__" :
149187 setup (name = "pythonnet" ,
150- ext_modules = [
188+ ext_modules = [
151189 Extension ("clr" , sources = [])
152- ],
153- cmdclass = {
154- "build_ext" : PythonNET_BuildExt
155- }
190+ ],
191+ scripts = [_npython_exe ],
192+ zip_safe = False ,
193+ cmdclass = {
194+ "build_ext" : PythonNET_BuildExt ,
195+ "build_scripts" : PythonNET_BuildScripts ,
196+ "install_lib" : PythonNET_InstallLib
197+ }
156198 )
157199
0 commit comments