1414import sys
1515import sysconfig
1616from distutils import spawn
17- from distutils .command import build_ext , install_data , install_lib
17+ from distutils .command import install , build , build_ext , install_data , install_lib
1818
1919from setuptools import Extension , setup
2020
@@ -131,6 +131,12 @@ def _get_long_description():
131131 except ImportError :
132132 return '.Net and Mono integration for Python'
133133
134+ def _update_xlat_devtools ():
135+ global DEVTOOLS
136+ if DEVTOOLS == "MsDev" :
137+ DEVTOOLS = "MsDev15"
138+ elif DEVTOOLS == "Mono" :
139+ DEVTOOLS = "dotnet"
134140
135141class BuildExtPythonnet (build_ext .build_ext ):
136142 user_options = build_ext .build_ext .user_options + [
@@ -144,6 +150,9 @@ def finalize_options(self):
144150 build_ext .build_ext .finalize_options (self )
145151
146152 def build_extension (self , ext ):
153+ if self .xplat :
154+ _update_xlat_devtools ()
155+
147156 """Builds the .pyd file using msbuild or xbuild"""
148157 if ext .name != "clr" :
149158 return build_ext .build_ext .build_extension (self , ext )
@@ -174,7 +183,7 @@ def build_extension(self, ext):
174183 if CONFIG == "Debug" :
175184 defines .extend (["DEBUG" , "TRACE" ])
176185
177- if sys .platform != "win32" and DEVTOOLS == "Mono" :
186+ if sys .platform != "win32" and ( DEVTOOLS == "Mono" or DEVTOOLS == "dotnet" ) :
178187 on_darwin = sys .platform == "darwin"
179188 defines .append ("MONO_OSX" if on_darwin else "MONO_LINUX" )
180189
@@ -206,20 +215,34 @@ def build_extension(self, ext):
206215 if DEVTOOLS == "MsDev" :
207216 _xbuild = '"{0}"' .format (self ._find_msbuild_tool ("msbuild.exe" ))
208217 _config = "{0}Win" .format (CONFIG )
209-
218+ _solution_file = 'pythonnet.sln'
219+ _custom_define_constants = False
220+ elif DEVTOOLS == "MsDev15" :
221+ # Improve this with self._find_msbuild_tool_15 to find good >15.3 msbuild, currently only works under VS 15.3 developer environment.
222+ _xbuild = '"{0}"' .format (self ._find_msbuild_tool ("msbuild.exe" ))
223+ _config = "{0}Win" .format (CONFIG )
224+ _solution_file = 'pythonnet.15.sln'
225+ _custom_define_constants = True
210226 elif DEVTOOLS == "Mono" :
211- _xbuild = 'dotnet msbuild' if self . xplat else ' xbuild'
227+ _xbuild = 'xbuild'
212228 _config = "{0}Mono" .format (CONFIG )
229+ _solution_file = 'pythonnet.sln'
230+ _custom_define_constants = False
231+ elif DEVTOOLS == "dotnet" :
232+ _xbuild = 'dotnet msbuild'
233+ _config = "{0}Mono" .format (CONFIG )
234+ _solution_file = 'pythonnet.15.sln'
235+ _custom_define_constants = True
213236 else :
214237 raise NotImplementedError (
215- "DevTool {0} not supported (use MsDev/Mono)" .format (DEVTOOLS ))
238+ "DevTool {0} not supported (use MsDev/MsDev15/ Mono/dotnet )" .format (DEVTOOLS ))
216239
217240 cmd = [
218241 _xbuild ,
219- 'pythonnet.15.sln' if self . xplat else 'pythonnet.sln' ,
242+ _solution_file ,
220243 '/p:Configuration={}' .format (_config ),
221244 '/p:Platform={}' .format (ARCH ),
222- '/p:{}DefineConstants="{}"' .format ('Custom' if self . xplat else '' ,'%3B' .join (defines )),
245+ '/p:{}DefineConstants="{}"' .format ('Custom' if _custom_define_constants else '' ,'%3B' .join (defines )),
223246 '/p:PythonBuildDir="{}"' .format (os .path .abspath (dest_dir )),
224247 '/p:PythonInteropFile="{}"' .format (os .path .basename (interop_file )),
225248 '/verbosity:{}' .format (VERBOSITY ),
@@ -230,16 +253,16 @@ def build_extension(self, ext):
230253 cmd .append ('/p:PythonManifest="{0}"' .format (manifest ))
231254
232255 self .debug_print ("Building: {0}" .format (" " .join (cmd )))
233- use_shell = True if DEVTOOLS == "Mono" else False
256+ use_shell = True if DEVTOOLS == "Mono" or DEVTOOLS == "dotnet" else False
234257
235258 subprocess .check_call (" " .join (cmd + ["/t:Clean" ]), shell = use_shell )
236259 subprocess .check_call (" " .join (cmd + ["/t:Build" ]), shell = use_shell )
237260
238- if DEVTOOLS == "Mono" :
261+ if DEVTOOLS == "Mono" or DEVTOOLS == "dotnet" :
239262 self ._build_monoclr ()
240263
241264 def _get_manifest (self , build_dir ):
242- if DEVTOOLS != "MsDev" :
265+ if DEVTOOLS != "MsDev" and DEVTOOLS != "MsDev15" :
243266 return
244267 mt = self ._find_msbuild_tool ("mt.exe" , use_windows_sdk = True )
245268 manifest = os .path .abspath (os .path .join (build_dir , "app.manifest" ))
@@ -272,33 +295,30 @@ def _build_monoclr(self):
272295
273296 def _install_packages (self ):
274297 """install packages using nuget"""
275- nuget = os .path .join ("tools" , "nuget" , "nuget.exe" )
276- use_shell = False
277- if DEVTOOLS == "Mono" :
278- nuget = "mono {0}" .format (nuget )
279- use_shell = True
298+ use_shell = DEVTOOLS == "Mono" or DEVTOOLS == "dotnet"
280299
281- if self . xplat :
282- if DEVTOOLS == "MsDev " :
300+ if DEVTOOLS == "MsDev15" or DEVTOOLS == "dotnet" :
301+ if DEVTOOLS == "MsDev15 " :
283302 _config = "{0}Win" .format (CONFIG )
284- elif DEVTOOLS == "Mono " :
303+ elif DEVTOOLS == "dotnet " :
285304 _config = "{0}Mono" .format (CONFIG )
286- else :
287- raise NotImplementedError (
288- "DevTool {0} not supported (use MsDev/Mono)" .format (DEVTOOLS ))
289305
290306 cmd = "dotnet msbuild /t:Restore pythonnet.15.sln /p:Configuration={0} /p:Platform={1}" .format (_config , ARCH )
291307 self .debug_print ("Updating packages with xplat: {0}" .format (cmd ))
292308 subprocess .check_call (cmd , shell = use_shell )
293- return ;
309+ else :
310+ nuget = os .path .join ("tools" , "nuget" , "nuget.exe" )
294311
295- cmd = "{0} update -self" .format (nuget )
296- self .debug_print ("Updating NuGet: {0}" .format (cmd ))
297- subprocess .check_call (cmd , shell = use_shell )
312+ if DEVTOOLS == "Mono" :
313+ nuget = "mono {0}" .format (nuget )
298314
299- cmd = "{0} restore pythonnet.sln -o packages" .format (nuget )
300- self .debug_print ("Installing packages: {0}" .format (cmd ))
301- subprocess .check_call (cmd , shell = use_shell )
315+ cmd = "{0} update -self" .format (nuget )
316+ self .debug_print ("Updating NuGet: {0}" .format (cmd ))
317+ subprocess .check_call (cmd , shell = use_shell )
318+
319+ cmd = "{0} restore pythonnet.sln -o packages" .format (nuget )
320+ self .debug_print ("Installing packages: {0}" .format (cmd ))
321+ subprocess .check_call (cmd , shell = use_shell )
302322
303323 def _find_msbuild_tool (self , tool = "msbuild.exe" , use_windows_sdk = False ):
304324 """Return full path to one of the Microsoft build tools"""
@@ -381,6 +401,21 @@ def run(self):
381401
382402 return install_data .install_data .run (self )
383403
404+ class InstallPythonnet (install .install ):
405+ user_options = install .install .user_options + [
406+ ('xplat' , None , None )
407+ ]
408+ def initialize_options (self ):
409+ install .install .initialize_options (self )
410+ self .xplat = None
411+
412+ def finalize_options (self ):
413+ install .install .finalize_options (self )
414+
415+ def run (self ):
416+ if self .xplat :
417+ _update_xlat_devtools ()
418+ return install .install .run (self )
384419
385420###############################################################################
386421setupdir = os .path .dirname (__file__ )
@@ -410,6 +445,7 @@ def run(self):
410445 ]),
411446 ],
412447 cmdclass = {
448+ "install" : InstallPythonnet ,
413449 "build_ext" : BuildExtPythonnet ,
414450 "install_lib" : InstallLibPythonnet ,
415451 "install_data" : InstallDataPythonnet ,
0 commit comments