Skip to content

Commit 5b5e3d1

Browse files
committed
Install bindings with setup.py
Include instructions in setup.py to compile the bindings with cmake. Specifically only for the plasma_source library, since we don't need the source_sampling library in Python. Update CMakeLists to avoid forcing the library output directory as this conflicts with the way setuptools etc. look for package artefacts. This means that the resulting source_sampling library is available in the build directory, unless the CMAKE_LIBRARY_OUTPUT_DIRECTORY is passed into the cmake configuration step. Introduce an __init__.py so that the modules can easily be loaded and found by setuptools. Cleanup .gitignore and add dist and egg paths.
1 parent 7be706c commit 5b5e3d1

4 files changed

Lines changed: 73 additions & 9 deletions

File tree

.gitignore

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
*.app
3333

3434
# Build path
35-
build
35+
build/
36+
dist/
37+
*.egg-info/
3638

3739
# Python
38-
__pycache__
39-
*.pyc
40+
__pycache__/
41+
*.py[cod]

CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ set(CMAKE_VERBOSE_MAKEFILE OFF)
55

66
set(SRC_DIR parametric_plasma_source)
77

8-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${SRC_DIR})
9-
108
if(NOT CMAKE_BUILD_TYPE)
119
set(CMAKE_BUILD_TYPE Release)
1210
endif()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .plasma import Plasma
2+
from .plasma_source import PlasmaSource

setup.py

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,69 @@
1-
import setuptools
1+
import os
2+
import subprocess
3+
import sys
4+
5+
from setuptools import setup, find_packages, Extension
6+
from setuptools.command.build_ext import build_ext
7+
8+
9+
class CMakeExtention(Extension):
10+
def __init__(self, name, sourcedir=""):
11+
Extension.__init__(self, name, sources=[])
12+
self.sourcedir = os.path.abspath(sourcedir)
13+
14+
15+
class CMakeBuild(build_ext):
16+
def run(self):
17+
try:
18+
subprocess.check_output(["cmake", "--version"])
19+
except OSError:
20+
raise RuntimeError("CMake must be installed to build the "
21+
"following extentions: "
22+
", ".join(e.name for e in self.extensions))
23+
24+
for ext in self.extensions:
25+
self.build_extension(ext)
26+
27+
def build_extension(self, ext):
28+
extdir = os.path.abspath(
29+
os.path.dirname(self.get_ext_fullpath(ext.name))
30+
)
31+
if not extdir.endswith(os.path.sep):
32+
extdir += os.path.sep
33+
34+
cmake_args = ["-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=" + extdir,
35+
"-DPYTHON_EXECUTABLE=" + sys.executable]
36+
37+
cfg = "Debug" if self.debug else "Release"
38+
build_args = ["--config", cfg]
39+
40+
cmake_args += ["-DCMAKE_BUILD_TYPE=" + cfg]
41+
build_args += ["--", "-j2"]
42+
build_args += ["plasma_source"]
43+
44+
env = os.environ.copy()
45+
env["CXXFLAGS"] = "{} -DVERSION_INFO=\\\"{}\\\"".format(
46+
env.get("CXXFLAGS", ""),
47+
self.distribution.get_version()
48+
)
49+
50+
if not os.path.exists(self.build_temp):
51+
os.makedirs(self.build_temp)
52+
subprocess.check_call(
53+
["cmake", ext.sourcedir] + cmake_args,
54+
cwd=self.build_temp,
55+
env=env
56+
)
57+
subprocess.check_call(
58+
["cmake", "--build", "."] + build_args,
59+
cwd=self.build_temp
60+
)
61+
262

363
with open("README.md", "r") as fh:
464
long_description = fh.read()
565

6-
setuptools.setup(
66+
setup(
767
name="parametric_plasma_source",
868
version="0.0.6",
969
author="Jonathan Shimwell",
@@ -12,10 +72,12 @@
1272
long_description=long_description,
1373
long_description_content_type="text/markdown",
1474
url="https://github.com/shimwell/parametric_plasma_source",
15-
packages=setuptools.find_packages(),
75+
packages=find_packages(),
76+
ext_modules=[CMakeExtention("parametric_plasma_source/plasma_source")],
77+
cmdclass=dict(build_ext=CMakeBuild),
1678
classifiers=[
1779
"Programming Language :: Python :: 3",
1880
"License :: OSI Approved :: MIT License",
1981
"Operating System :: OS Independent",
2082
],
21-
)
83+
)

0 commit comments

Comments
 (0)