44from wheel .bdist_wheel import bdist_wheel
55from setuptools .command .build_ext import build_ext
66import distutils
7+ from distutils .command import build
78from subprocess import check_output , check_call
89
910import sys , os
@@ -26,6 +27,8 @@ def _get_interop_filename():
2627 return os .path .join ("src" , "runtime" , interop_filename )
2728
2829
30+ # Write configuration to configured.props. This will go away once all of these
31+ # can be decided at runtime.
2932def _write_configure_props ():
3033 defines = [
3134 "PYTHON{0}{1}" .format (PY_MAJOR , PY_MINOR ),
@@ -60,7 +63,7 @@ def _write_configure_props():
6063 ET .ElementTree (proj ).write (CONFIGURED_PROPS )
6164
6265
63- class Configure (Command ):
66+ class configure (Command ):
6467 """Configure command"""
6568
6669 description = "Configure the pythonnet build"
@@ -77,36 +80,34 @@ def run(self):
7780 _write_configure_props ()
7881
7982
80- class DotnetLib ( Extension ) :
83+ class DotnetLib :
8184 def __init__ (self , name , path , ** kwargs ):
85+ self .name = name
8286 self .path = path
8387 self .args = kwargs
84- super ().__init__ (name , sources = [])
8588
8689
87- class BuildDotnet ( build_ext ):
90+ class build_dotnet ( Command ):
8891 """Build command for dotnet-cli based builds"""
8992
9093 description = "Build DLLs with dotnet-cli"
9194 user_options = [("dotnet-config" , None , "dotnet build configuration" )]
9295
9396 def initialize_options (self ):
94- self .dotnet_config = "release"
95- super (). initialize_options ()
97+ self .dotnet_config = None
98+ self . build_lib = None
9699
97100 def finalize_options (self ):
98- super ().finalize_options ()
99-
100- def get_source_files (self ):
101- return super ().get_source_files ()
101+ if self .dotnet_config is None :
102+ self .dotnet_config = "release"
103+
104+ build = self .distribution .get_command_obj ("build" )
105+ build .ensure_finalized ()
106+ self .build_lib = build .build_lib
102107
103108 def run (self ):
104- orig_modules = self .distribution .ext_modules
105- dotnet_modules = [lib for lib in orig_modules if isinstance (lib , DotnetLib )]
106- other_modules = [lib for lib in orig_modules if not isinstance (lib , DotnetLib )]
107-
108- if dotnet_modules :
109- self .run_command ("configure" )
109+ dotnet_modules = self .distribution .dotnet_libs
110+ self .run_command ("configure" )
110111
111112 for lib in dotnet_modules :
112113 output = os .path .join (os .path .abspath (self .build_lib ), lib .args .pop ("output" ))
@@ -140,18 +141,21 @@ def run(self):
140141 else :
141142 self .warn ("Can't find file to rename: {}, current dir: {}" .format (source , os .getcwd ()))
142143
143- if other_modules :
144- self .distribution .ext_modules = other_modules
145- super ().run ()
146- self .distribution .ext_modules = orig_modules
147- # If no modules need to be compiled, skip
144+ # Add build_dotnet to the build tasks:
145+ from distutils .command .build import build as _build
146+ from setuptools import Distribution
147+
148+ class build (_build ):
149+ sub_commands = _build .sub_commands + [('build_dotnet' , None )]
150+
151+ Distribution .dotnet_libs = None
148152
149153
150154with open ("README.rst" , "r" ) as f :
151155 long_description = f .read ()
152156
153157
154- ext_modules = [
158+ dotnet_libs = [
155159 DotnetLib (
156160 "python-runtime" ,
157161 "src/runtime/Python.Runtime.csproj" ,
@@ -173,6 +177,8 @@ def run(self):
173177 ),
174178]
175179
180+ ext_modules = []
181+
176182try :
177183 mono_libs = check_output ("pkg-config --libs mono-2" , shell = True , encoding = "utf8" )
178184 mono_cflags = check_output (
@@ -194,7 +200,14 @@ def run(self):
194200 print ("Failed to find mono libraries via pkg-config, skipping the Mono CLR loader" )
195201
196202
203+
197204setup (
205+ cmdclass = {
206+ "build" : build ,
207+ "build_dotnet" : build_dotnet ,
208+ "configure" : configure ,
209+ },
210+
198211 name = "pythonnet" ,
199212 version = "3.0.0.dev1" ,
200213 description = ".Net and Mono integration for Python" ,
@@ -207,12 +220,10 @@ def run(self):
207220 install_requires = ["pycparser" ],
208221 long_description = long_description ,
209222 # data_files=[("{install_platlib}", ["{build_lib}/pythonnet"])],
210- cmdclass = {
211- "build_ext" : BuildDotnet ,
212- "configure" : Configure ,
213- },
223+
214224 py_modules = ["clr" ],
215225 ext_modules = ext_modules ,
226+ dotnet_libs = dotnet_libs ,
216227 classifiers = [
217228 "Development Status :: 5 - Production/Stable" ,
218229 "Intended Audience :: Developers" ,
0 commit comments