-
Notifications
You must be signed in to change notification settings - Fork 16
/
setup.py
executable file
·186 lines (157 loc) · 7.24 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python3
import os
import platform
import re
import shutil
from distutils.dir_util import copy_tree
from setuptools import setup, Extension
from distutils.command.clean import clean as _clean
from setuptools.command.build_ext import build_ext as _build_ext
from distutils.command.build import build as _build
from setuptools.command.install import install as _install
def get_version(verbose=False):
"""Extract version information from source code"""
inc_dir = os.path.join(root_dir, 'include', 'libqasm') # C++ include directory
matcher = re.compile('static const char\* version\{ "(.*)" }')
version = None
with open(os.path.join(inc_dir, 'versioning.hpp'), 'r') as f:
for ln in f:
m = matcher.match(ln)
if m:
version = m.group(1)
break
if verbose:
print("get_version: %s" % version)
return version
root_dir = os.getcwd() # root of the repository
src_dir = root_dir + os.sep + 'src' # C++ source directory
pysrc_dir = root_dir + os.sep + 'python' # Python source files
target_dir = root_dir + os.sep + 'pybuild' # python-specific build directory
build_dir = target_dir + os.sep + 'build' # directory for setuptools to dump various files into
cbuild_dir = target_dir + os.sep + 'cbuild' # cmake build directory
prefix_dir = target_dir + os.sep + 'prefix' # cmake install prefix
srcmod_dir = pysrc_dir + os.sep + 'module' # libqasm Python module directory, source files only
module_dir = target_dir + os.sep + 'module' # libqasm Python module directory, including generated file(s)
# Copy the handwritten Python sources into the module directory that we're telling setuptools is our source directory,
# because setuptools insists on spamming output files into that directory.
# This is ugly, especially because it has to run before setup() is invoked,
# but seems to be more-or-less unavoidable to get editable installations to work.
if not os.path.exists(target_dir):
os.makedirs(target_dir)
copy_tree(srcmod_dir, module_dir)
def read(file_name):
with open(os.path.join(os.path.dirname(__file__), file_name)) as f:
return f.read()
class clean(_clean):
def run(self):
_clean.run(self)
if os.path.exists(target_dir):
shutil.rmtree(target_dir)
class build_ext(_build_ext):
def run(self):
from plumbum import local, FG
# If we were previously built in a different directory,
# nuke the cbuild dir to prevent inane CMake errors.
# This happens when the user does 'pip install .' after building locally
if os.path.exists(cbuild_dir + os.sep + 'CMakeCache.txt'):
with open(cbuild_dir + os.sep + 'CMakeCache.txt', 'r') as f:
for line in f.read().split('\n'):
line = line.split('#')[0].strip()
if not line:
continue
if line.startswith('cqasm_BINARY_DIR:STATIC'):
config_dir = line.split('=', maxsplit=1)[1]
if os.path.realpath(config_dir) != os.path.realpath(cbuild_dir):
print('removing pybuild/cbuild to avoid CMakeCache error')
shutil.rmtree(cbuild_dir)
break
# Figure out how setuptools wants to name the extension file and where it wants to place it
cqasm_target = os.path.abspath(self.get_ext_fullpath('cqasm._cqasm'))
target = os.path.abspath(self.get_ext_fullpath('libqasm._libqasm'))
# Build the Python extension and install it where setuptools expects it
if not os.path.exists(cbuild_dir):
os.makedirs(cbuild_dir)
# Configure and build using Conan
with local.cwd(root_dir):
build_type = os.environ.get('CMAKE_BUILD_TYPE', 'Release')
build_tests = os.environ.get('LIBQASM_BUILD_TESTS', 'False')
cmd = local['conan']['profile']['detect']['--force']
cmd & FG
cmd = (local['conan']['create']['.']
['--version'][get_version()]
['-s:h']['compiler.cppstd=20']
['-s:b']['compiler.cppstd=20']
['-s:h']['libqasm/*:build_type=' + build_type]
['-s:b']['libqasm/*:build_type=' + build_type]
['-o']['libqasm/*:build_python=True']
# The Python library needs the compatibility headers
['-o']['libqasm/*:cqasm_python_dir=' + re.escape(os.path.dirname(cqasm_target))]
['-o']['libqasm/*:python_dir=' + re.escape(os.path.dirname(target))]
['-o']['libqasm/*:python_ext=' + re.escape(os.path.basename(target))]
# (Ab)use static libs for the intermediate libraries
# to avoid dealing with R(UN)PATH nonsense on Linux/OSX as much as possible
['-o']['libqasm/*:shared=False']
['-b']['missing']
['-tf']['']
)
if build_tests == 'True':
cmd = cmd['-c']['tools.build:skip_test=False']
if platform.system() == 'Darwin':
cmd = cmd['-c']['tools.build:defines=["_LIBCPP_DISABLE_AVAILABILITY"]']
cmd & FG
class build(_build):
def initialize_options(self):
_build.initialize_options(self)
self.build_base = os.path.relpath(build_dir)
def run(self):
# Make sure the extension is built before the Python module is "built",
# otherwise SWIG's generated module isn't included.
# See https://stackoverflow.com/questions/12491328
self.run_command('build_ext')
_build.run(self)
class install(_install):
def run(self):
# See https://stackoverflow.com/questions/12491328
self.run_command('build_ext')
_install.run(self)
setup(
name='libqasm',
version=get_version(),
description='libqasm Python Package',
long_description=read('README.md'),
long_description_content_type='text/markdown',
author='QuTech, TU Delft',
url='https://github.com/QuTech-Delft/libqasm',
classifiers=[
'License :: OSI Approved :: Apache Software License',
'Operating System :: POSIX :: Linux',
'Operating System :: MacOS',
'Operating System :: Microsoft :: Windows',
'Programming Language :: Python :: 3 :: Only',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
'Topic :: Scientific/Engineering'
],
packages=['libqasm', 'cqasm', 'cqasm.v3x'],
package_dir={'': 'pybuild/module'},
# NOTE: the library build process is completely overridden to let CMake handle it.
# setuptools implementation is horribly broken.
# This is here just to have the rest of setuptools understand that this is a Python module with an extension in it.
ext_modules=[Extension('libqasm._libqasm', [])],
cmdclass={
'build_ext': build_ext,
'build': build,
'install': install,
'clean': clean,
},
setup_requires=[
'conan',
'plumbum',
'delocate; platform_system == "Darwin"',
],
install_requires=['numpy'],
tests_require=['pytest'],
zip_safe=False
)