forked from QuTech-Delft/libqasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
145 lines (116 loc) · 4.58 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
import os
import subprocess
from shutil import copyfile
from sys import platform
from typing import Dict
from setuptools import setup
root_dir = os.path.dirname(os.path.realpath(__file__))
src_dir = os.path.join(root_dir, 'src')
build_dir = os.path.join(src_dir, "cbuild")
libqasm_dir = os.path.join(src_dir, "libQasm")
platforms = {
'unix': {
'make_command': 'make',
'cmake_options': '',
'clib_name': '_libQasm.so',
'liblexgram': 'liblexgram.so'
},
'darwin': {
'make_command': 'make',
'cmake_options': '',
'clib_name': '_libQasm.so',
'liblexgram': 'liblexgram.a'
},
'win32': {
'make_command': 'mingw32-make',
'cmake_options': '-G "MinGW Makefiles"',
'clib_name': '_libQasm.pyd',
'liblexgram': 'liblexgram.dll'
}
}
def determine_platform() -> Dict[str, str]:
"""Determine the family of the current platform.
Based on the system libraries, determine whether the platform is of the UNIX family or the win32 family. Other
platforms are currently not supported and will raise an exception.
"""
if platform == "linux" or platform == "linux2":
return platforms['unix']
elif platform == "darwin" or platform == "win32":
return platforms[platform]
else:
raise OSError('Platform not recognised!')
def create_directory(directory: str) -> None:
"""Wrapper function for checking whether a directory already exists, and otherwise create it.
Args:
directory: the path for the directory that needs to be created.
"""
if not os.path.exists(directory):
os.makedirs(directory)
def build_libqasm_library(make_command: str, cmake_options: str) -> None:
"""Call cmake and make to build the c++ libraries.
Args:
make_command: the make command to use, varies between windows and unix.
cmake_options: additional build options to pass to cmake.
"""
os.chdir(build_dir)
execute_process(f'git submodule update --init --recursive')
execute_process(f'cmake {cmake_options} {os.path.join("..", "library")}')
execute_process(f'{make_command} all')
execute_process(f'{make_command} test')
os.chdir(root_dir)
def execute_process(command: str) -> None:
"""Execute shell commands.
Args:
command: the shell command to execute.
"""
proc = subprocess.Popen(command, shell=True)
proc.communicate()
def create_init_file() -> None:
"""Create init file for the libQasm directory
Create a __init__.py file to make the libQasm directory a python package. This __init__ file will be prepopulated
with a relative import of libQasm.
"""
init_file_path = os.path.join(libqasm_dir, '__init__.py')
with open(init_file_path, 'w') as init_fp:
init_fp.write('from .libQasm import libQasm')
def copy_file(src_dir: str, dest_dir: str, file_name: str) -> None:
"""Copy a specified file from the source directory to the destination directory.
Args:
src_dir: source folder from which to copy the specified file.
dest_dir: destination folder to which to copy the specified file.
file_name: the file name of the file to copy.
"""
copyfile(
os.path.join(src_dir, file_name),
os.path.join(dest_dir, file_name)
)
def build_libqasm():
"""Wrapper that calls the differnt components to build libQasm and place the necessary binaries"""
sys_platform = determine_platform()
for directory in [libqasm_dir, build_dir]:
create_directory(directory)
build_libqasm_library(sys_platform['make_command'], sys_platform['cmake_options'])
clibname = sys_platform['clib_name']
create_init_file()
copy_file(build_dir, libqasm_dir, clibname)
copy_file(build_dir, libqasm_dir, "libQasm.py")
copy_file(build_dir, libqasm_dir, sys_platform['liblexgram'])
return os.path.join(libqasm_dir, clibname), os.path.join(libqasm_dir, sys_platform['liblexgram'])
clib, liblexgram = build_libqasm()
setup(name='libQasm',
description='libQasm Python Package',
author='Kelvin Loh',
author_email='[email protected]',
url="https://www.github.com/QE-Lab/libqasm/",
version='0.0.1',
python_requires='>=3.6',
packages=['libQasm'],
package_dir={'': 'src'},
package_data={'libQasm': [clib, liblexgram]},
classifiers=[
'Development Status :: 3 - Alpha',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7'],
license='Other/Proprietary License',
zip_safe=False)