Skip to content

Implement inplace building and develop #1317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ configured.props
*.dll
*.exe
*.pdb
*.deps.json

### JetBrains ###
.idea/
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[build-system]
requires = ["setuptools>=42", "wheel", "setuptools_scm[toml]>=3.4", "pycparser"]
requires = ["setuptools>=42", "wheel", "pycparser"]
build-backend = "setuptools.build_meta"
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ codecov
wheel
pycparser
setuptools
setuptools_scm
148 changes: 96 additions & 52 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#!/usr/bin/env python

from setuptools import setup, Command, Extension
from wheel.bdist_wheel import bdist_wheel
from setuptools.command.build_ext import build_ext
import distutils
from distutils.command import build
from subprocess import check_output, check_call

import sys, os

BUILD_MONO = True
BUILD_NETFX = True

PY_MAJOR = sys.version_info[0]
PY_MINOR = sys.version_info[1]

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

description = "Build DLLs with dotnet-cli"
user_options = [("dotnet-config", None, "dotnet build configuration")]
user_options = [
("dotnet-config", None, "dotnet build configuration"),
(
"inplace",
"i",
"ignore build-lib and put compiled extensions into the source "
+ "directory alongside your pure Python modules",
),
]

def initialize_options(self):
self.dotnet_config = None
self.build_lib = None
self.inplace = False

def finalize_options(self):
if self.dotnet_config is None:
self.dotnet_config = "release"

build = self.distribution.get_command_obj("build")
build.ensure_finalized()
self.build_lib = build.build_lib
if self.inplace:
self.build_lib = "."
else:
self.build_lib = build.build_lib

def run(self):
dotnet_modules = self.distribution.dotnet_libs
self.run_command("configure")

for lib in dotnet_modules:
output = os.path.join(os.path.abspath(self.build_lib), lib.args.pop("output"))
output = os.path.join(
os.path.abspath(self.build_lib), lib.args.pop("output")
)
rename = lib.args.pop("rename", {})

opts = sum(
Expand Down Expand Up @@ -139,75 +155,105 @@ def run(self):

self.move_file(src=source, dst=dest, level=distutils.log.INFO)
else:
self.warn("Can't find file to rename: {}, current dir: {}".format(source, os.getcwd()))
self.warn(
"Can't find file to rename: {}, current dir: {}".format(
source, os.getcwd()
)
)


# Add build_dotnet to the build tasks:
from distutils.command.build import build as _build
from setuptools.command.develop import develop as _develop
from setuptools import Distribution
import setuptools


class build(_build):
sub_commands = _build.sub_commands + [('build_dotnet', None)]
sub_commands = _build.sub_commands + [("build_dotnet", None)]


class develop(_develop):
def install_for_development(self):
# Build extensions in-place
self.reinitialize_command("build_dotnet", inplace=1)
self.run_command("build_dotnet")

return super().install_for_development()


# Monkey-patch Distribution s.t. it supports the dotnet_libs attribute
Distribution.dotnet_libs = None

cmdclass = {
"build": build,
"build_dotnet": build_dotnet,
"configure": configure,
"develop": develop,
}


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


dotnet_libs = [
DotnetLib(
"python-runtime",
"src/runtime/Python.Runtime.csproj",
output="pythonnet/runtime"
),
DotnetLib(
"clrmodule-amd64",
"src/clrmodule/",
runtime="win-x64",
output="pythonnet/netfx/amd64",
rename={"clr.dll": "clr.pyd"},
),
DotnetLib(
"clrmodule-x86",
"src/clrmodule/",
runtime="win-x86",
output="pythonnet/netfx/x86",
rename={"clr.dll": "clr.pyd"},
),
output="pythonnet/runtime",
)
]

ext_modules = []

try:
mono_libs = check_output("pkg-config --libs mono-2", shell=True, encoding="utf8")
mono_cflags = check_output(
"pkg-config --cflags mono-2", shell=True, encoding="utf8"
if BUILD_NETFX:
dotnet_libs.extend(
[
DotnetLib(
"clrmodule-amd64",
"src/clrmodule/",
runtime="win-x64",
output="pythonnet/netfx/amd64",
rename={"clr.dll": "clr.pyd"},
),
DotnetLib(
"clrmodule-x86",
"src/clrmodule/",
runtime="win-x86",
output="pythonnet/netfx/x86",
rename={"clr.dll": "clr.pyd"},
),
]
)
cflags = mono_cflags.strip()
libs = mono_libs.strip()

# build the clr python module
clr_ext = Extension(
"clr",
language="c++",
sources=["src/monoclr/clrmod.c"],
extra_compile_args=cflags.split(" "),
extra_link_args=libs.split(" "),
)
ext_modules.append(clr_ext)
except Exception:
print("Failed to find mono libraries via pkg-config, skipping the Mono CLR loader")

ext_modules = []

if BUILD_MONO:
try:
mono_libs = check_output(
"pkg-config --libs mono-2", shell=True, encoding="utf8"
)
mono_cflags = check_output(
"pkg-config --cflags mono-2", shell=True, encoding="utf8"
)
cflags = mono_cflags.strip()
libs = mono_libs.strip()

# build the clr python module
clr_ext = Extension(
"pythonnet.mono.clr",
language="c++",
sources=["src/monoclr/clrmod.c"],
extra_compile_args=cflags.split(" "),
extra_link_args=libs.split(" "),
)
ext_modules.append(clr_ext)
except Exception:
print(
"Failed to find mono libraries via pkg-config, skipping the Mono CLR loader"
)

setup(
cmdclass={
"build": build,
"build_dotnet": build_dotnet,
"configure": configure,
},

setup(
cmdclass=cmdclass,
name="pythonnet",
version="3.0.0.dev1",
description=".Net and Mono integration for Python",
Expand All @@ -216,11 +262,9 @@ class build(_build):
author="The Contributors of the Python.NET Project",
author_email="pythonnet@python.org",
packages=["pythonnet"],
setup_requires=["setuptools_scm"],
install_requires=["pycparser"],
long_description=long_description,
# data_files=[("{install_platlib}", ["{build_lib}/pythonnet"])],

py_modules=["clr"],
ext_modules=ext_modules,
dotnet_libs=dotnet_libs,
Expand Down