Skip to content

Commit ffbbf17

Browse files
authored
Implement inplace building and develop (#1317)
* Implement inplace building and develop * Drop setuptools_scm
1 parent 6bc85ff commit ffbbf17

File tree

4 files changed

+98
-54
lines changed

4 files changed

+98
-54
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ configured.props
77
*.dll
88
*.exe
99
*.pdb
10+
*.deps.json
1011

1112
### JetBrains ###
1213
.idea/

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[build-system]
2-
requires = ["setuptools>=42", "wheel", "setuptools_scm[toml]>=3.4", "pycparser"]
2+
requires = ["setuptools>=42", "wheel", "pycparser"]
33
build-backend = "setuptools.build_meta"

requirements.txt

-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ codecov
1010
wheel
1111
pycparser
1212
setuptools
13-
setuptools_scm

setup.py

+96-52
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#!/usr/bin/env python
22

33
from setuptools import setup, Command, Extension
4-
from wheel.bdist_wheel import bdist_wheel
54
from setuptools.command.build_ext import build_ext
65
import distutils
76
from distutils.command import build
87
from subprocess import check_output, check_call
98

109
import sys, os
1110

11+
BUILD_MONO = True
12+
BUILD_NETFX = True
13+
1214
PY_MAJOR = sys.version_info[0]
1315
PY_MINOR = sys.version_info[1]
1416

@@ -91,26 +93,40 @@ class build_dotnet(Command):
9193
"""Build command for dotnet-cli based builds"""
9294

9395
description = "Build DLLs with dotnet-cli"
94-
user_options = [("dotnet-config", None, "dotnet build configuration")]
96+
user_options = [
97+
("dotnet-config", None, "dotnet build configuration"),
98+
(
99+
"inplace",
100+
"i",
101+
"ignore build-lib and put compiled extensions into the source "
102+
+ "directory alongside your pure Python modules",
103+
),
104+
]
95105

96106
def initialize_options(self):
97107
self.dotnet_config = None
98108
self.build_lib = None
109+
self.inplace = False
99110

100111
def finalize_options(self):
101112
if self.dotnet_config is None:
102113
self.dotnet_config = "release"
103-
114+
104115
build = self.distribution.get_command_obj("build")
105116
build.ensure_finalized()
106-
self.build_lib = build.build_lib
117+
if self.inplace:
118+
self.build_lib = "."
119+
else:
120+
self.build_lib = build.build_lib
107121

108122
def run(self):
109123
dotnet_modules = self.distribution.dotnet_libs
110124
self.run_command("configure")
111125

112126
for lib in dotnet_modules:
113-
output = os.path.join(os.path.abspath(self.build_lib), lib.args.pop("output"))
127+
output = os.path.join(
128+
os.path.abspath(self.build_lib), lib.args.pop("output")
129+
)
114130
rename = lib.args.pop("rename", {})
115131

116132
opts = sum(
@@ -139,75 +155,105 @@ def run(self):
139155

140156
self.move_file(src=source, dst=dest, level=distutils.log.INFO)
141157
else:
142-
self.warn("Can't find file to rename: {}, current dir: {}".format(source, os.getcwd()))
158+
self.warn(
159+
"Can't find file to rename: {}, current dir: {}".format(
160+
source, os.getcwd()
161+
)
162+
)
163+
143164

144165
# Add build_dotnet to the build tasks:
145166
from distutils.command.build import build as _build
167+
from setuptools.command.develop import develop as _develop
146168
from setuptools import Distribution
169+
import setuptools
170+
147171

148172
class build(_build):
149-
sub_commands = _build.sub_commands + [('build_dotnet', None)]
173+
sub_commands = _build.sub_commands + [("build_dotnet", None)]
150174

175+
176+
class develop(_develop):
177+
def install_for_development(self):
178+
# Build extensions in-place
179+
self.reinitialize_command("build_dotnet", inplace=1)
180+
self.run_command("build_dotnet")
181+
182+
return super().install_for_development()
183+
184+
185+
# Monkey-patch Distribution s.t. it supports the dotnet_libs attribute
151186
Distribution.dotnet_libs = None
152187

188+
cmdclass = {
189+
"build": build,
190+
"build_dotnet": build_dotnet,
191+
"configure": configure,
192+
"develop": develop,
193+
}
194+
153195

154196
with open("README.rst", "r") as f:
155197
long_description = f.read()
156198

157-
158199
dotnet_libs = [
159200
DotnetLib(
160201
"python-runtime",
161202
"src/runtime/Python.Runtime.csproj",
162-
output="pythonnet/runtime"
163-
),
164-
DotnetLib(
165-
"clrmodule-amd64",
166-
"src/clrmodule/",
167-
runtime="win-x64",
168-
output="pythonnet/netfx/amd64",
169-
rename={"clr.dll": "clr.pyd"},
170-
),
171-
DotnetLib(
172-
"clrmodule-x86",
173-
"src/clrmodule/",
174-
runtime="win-x86",
175-
output="pythonnet/netfx/x86",
176-
rename={"clr.dll": "clr.pyd"},
177-
),
203+
output="pythonnet/runtime",
204+
)
178205
]
179206

180-
ext_modules = []
181-
182-
try:
183-
mono_libs = check_output("pkg-config --libs mono-2", shell=True, encoding="utf8")
184-
mono_cflags = check_output(
185-
"pkg-config --cflags mono-2", shell=True, encoding="utf8"
207+
if BUILD_NETFX:
208+
dotnet_libs.extend(
209+
[
210+
DotnetLib(
211+
"clrmodule-amd64",
212+
"src/clrmodule/",
213+
runtime="win-x64",
214+
output="pythonnet/netfx/amd64",
215+
rename={"clr.dll": "clr.pyd"},
216+
),
217+
DotnetLib(
218+
"clrmodule-x86",
219+
"src/clrmodule/",
220+
runtime="win-x86",
221+
output="pythonnet/netfx/x86",
222+
rename={"clr.dll": "clr.pyd"},
223+
),
224+
]
186225
)
187-
cflags = mono_cflags.strip()
188-
libs = mono_libs.strip()
189-
190-
# build the clr python module
191-
clr_ext = Extension(
192-
"clr",
193-
language="c++",
194-
sources=["src/monoclr/clrmod.c"],
195-
extra_compile_args=cflags.split(" "),
196-
extra_link_args=libs.split(" "),
197-
)
198-
ext_modules.append(clr_ext)
199-
except Exception:
200-
print("Failed to find mono libraries via pkg-config, skipping the Mono CLR loader")
201226

227+
ext_modules = []
202228

229+
if BUILD_MONO:
230+
try:
231+
mono_libs = check_output(
232+
"pkg-config --libs mono-2", shell=True, encoding="utf8"
233+
)
234+
mono_cflags = check_output(
235+
"pkg-config --cflags mono-2", shell=True, encoding="utf8"
236+
)
237+
cflags = mono_cflags.strip()
238+
libs = mono_libs.strip()
239+
240+
# build the clr python module
241+
clr_ext = Extension(
242+
"pythonnet.mono.clr",
243+
language="c++",
244+
sources=["src/monoclr/clrmod.c"],
245+
extra_compile_args=cflags.split(" "),
246+
extra_link_args=libs.split(" "),
247+
)
248+
ext_modules.append(clr_ext)
249+
except Exception:
250+
print(
251+
"Failed to find mono libraries via pkg-config, skipping the Mono CLR loader"
252+
)
203253

204-
setup(
205-
cmdclass={
206-
"build": build,
207-
"build_dotnet": build_dotnet,
208-
"configure": configure,
209-
},
210254

255+
setup(
256+
cmdclass=cmdclass,
211257
name="pythonnet",
212258
version="3.0.0.dev1",
213259
description=".Net and Mono integration for Python",
@@ -216,11 +262,9 @@ class build(_build):
216262
author="The Contributors of the Python.NET Project",
217263
author_email="pythonnet@python.org",
218264
packages=["pythonnet"],
219-
setup_requires=["setuptools_scm"],
220265
install_requires=["pycparser"],
221266
long_description=long_description,
222267
# data_files=[("{install_platlib}", ["{build_lib}/pythonnet"])],
223-
224268
py_modules=["clr"],
225269
ext_modules=ext_modules,
226270
dotnet_libs=dotnet_libs,

0 commit comments

Comments
 (0)