-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.py
executable file
·141 lines (121 loc) · 4.86 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
#
# This file is part of pycommute, Python bindings for the libcommute C++
# quantum operator algebra library.
#
# Copyright (C) 2019-2024 Igor Krivenko
#
# 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
import sys
from setuptools import setup
from packaging.specifiers import SpecifierSet
from packaging.version import Version
from pybind11.setup_helpers import Pybind11Extension, build_ext
__version__ = "0.8.0"
comp_libcommute_versions = ">=0.8.0"
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
ext.extra_compile_args.append("-O3")
if not sys.platform.startswith("darwin"):
ext.extra_compile_args.append("-march=native")
build_ext.build_extensions(self)
cmdclass = {'build_ext': pycommute_build_ext}
def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()
setup(
name="pycommute",
version=__version__,
author="Igor Krivenko",
author_email="[email protected]",
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',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
'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
)