forked from krivenko/pycommute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
executable file
·151 lines (129 loc) · 5.08 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#
# This file is part of pycommute, Python bindings for the libcommute C++
# quantum operator algebra library.
#
# Copyright (C) 2019-2021 Igor Krivenko <igor.s.krivenko@gmail.com>
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Based on code in https://github.com/pybind/python_example
#
import os
from setuptools import setup
from packaging.specifiers import SpecifierSet
from packaging.version import Version
from pybind11.setup_helpers import Pybind11Extension, build_ext
__version__ = "0.7.1"
comp_libcommute_versions = ">=0.7.1"
ext_modules = [
Pybind11Extension("pycommute/expression", ["pycommute/expression.cpp"]),
Pybind11Extension("pycommute/loperator", ["pycommute/loperator.cpp"])
]
class pycommute_build_ext(build_ext):
"""A custom build extension for adding libcommute-specific options."""
def find_libcommute(self):
import os
import tempfile
import subprocess
import distutils.errors
from os import environ
self.libcommute_includedir = environ.get('LIBCOMMUTE_INCLUDEDIR', None)
inc = [self.libcommute_includedir] if self.libcommute_includedir else []
with tempfile.TemporaryDirectory() as d:
cwd = os.getcwd()
os.chdir(d)
open("libcommute_version.cpp", 'w').write("""
#include <iostream>
#include <libcommute/version.hpp>
int main () { std::cout << LIBCOMMUTE_VERSION; return 0; }
""")
try:
objs = self.compiler.compile(["libcommute_version.cpp"],
include_dirs=inc)
self.compiler.link_executable(objs,
"libcommute_version",
target_lang='c++')
v = subprocess.run(["./libcommute_version"],
stdout=subprocess.PIPE).stdout
v = str(v, 'utf-8', 'ignore')
except distutils.errors.CompileError:
return False
finally:
os.chdir(cwd)
return v
def build_extensions(self):
libcommute_version = self.find_libcommute()
if libcommute_version:
print("Found libcommute version " + libcommute_version)
else:
raise RuntimeError(
"Could not find libcommute headers. "
"Use the LIBCOMMUTE_INCLUDEDIR environment variable "
"to specify location of libcommute include directory."
)
if Version(libcommute_version) not in \
SpecifierSet(comp_libcommute_versions):
raise RuntimeError(
"Incompatible libcommute version %s (required %s)." % (
libcommute_version, comp_libcommute_versions
)
)
for ext in self.extensions:
ext.include_dirs.append(self.libcommute_includedir)
ext.cxx_std = 17
build_ext.build_extensions(self)
cmdclass = {'build_ext': pycommute_build_ext}
# Detect Sphinx/sphinx_rtd_theme/myst_parser and enable documentation build
try:
from sphinx.setup_command import BuildDoc
except ImportError:
BuildDoc = None
if BuildDoc:
class run_build_before_doc(BuildDoc):
def run(self):
self.run_command("build")
super(run_build_before_doc, self).run()
cmdclass['build_sphinx'] = run_build_before_doc
else:
print("Note: "
"Install Sphinx>=2.1.0, sphinx_rtd_theme and myst_parser "
"to build documentation")
def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()
setup(
name="pycommute",
version=__version__,
author="Igor Krivenko",
author_email="igor.s.krivenko@gmail.com",
description="Python bindings for the libcommute C++ library",
long_description=read("README.md"),
long_description_content_type="text/markdown",
url="https://krivenko.github.io/pycommute",
keywords="libcommute dsl computer algebra quantum",
license="MPL-2",
project_urls={
"Bug Tracker": "https://github.com/krivenko/pycommute/issues",
"Documentation": "https://krivenko.github.io/pycommute",
"Source Code": "https://github.com/krivenko/pycommute"
},
classifiers=[
'Development Status :: 4 - Beta',
'License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Topic :: Scientific/Engineering :: Physics'
],
python_requires=">=3.6",
install_requires=["numpy>=1.12.0"],
packages=['pycommute'],
ext_modules=ext_modules,
include_package_data=True,
cmdclass=cmdclass,
zip_safe=False
)