-
Notifications
You must be signed in to change notification settings - Fork 3
/
setup.py
163 lines (136 loc) · 5.96 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
import hashlib
import os
import platform
import shutil
import subprocess
import sys
from pathlib import Path
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
SETUP_DIR = Path(__file__).parent
sys.path.append(str(SETUP_DIR))
from utils.generate_bindings import IMCPybind, IMCPyi
class CMakeExtension(Extension):
def __init__(self, name, sourcedir='', subdir=''):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
self.subdir = subdir
class CMakeBuild(build_ext):
def run(self):
try:
subprocess.check_output(['cmake', '--version'])
except OSError:
raise RuntimeError(
'CMake must be installed to build the following extensions: '
+ ', '.join(e.name for e in self.extensions)
)
for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext):
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
cmake_args = [
'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
'-DPYTHON_EXECUTABLE=' + sys.executable,
'-DDUNE_PROGRAM_PYTHON=' + sys.executable,
]
cfg = 'Debug' if self.debug else 'Release'
build_args = ['--config', cfg]
if platform.system() == 'Windows':
cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir)]
if sys.maxsize > 2**32:
cmake_args += ['-A', 'x64']
build_args += ['--', '/m']
else:
cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
build_args += ['--', '-j{}'.format(os.cpu_count() if os.cpu_count() is not None else 2)]
env = os.environ.copy()
env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get('CXXFLAGS', ''), self.distribution.get_version())
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
# Check for dune
if not os.path.isdir('dune'):
raise RuntimeError('Dune not found. Repository not cloned with --recursive?.')
# Check for IMC definition
if not (os.path.isfile('imc/IMC.xml') or os.path.isfile('IMC/IMC.xml')):
raise RuntimeError('IMC specification not found. Repository not cloned with --recursive?.')
# Copy IMC to cmake build folder (to generate dune definitions)
imc_dir = 'imc' if os.path.isfile('imc/IMC.xml') else 'IMC'
imc_build = os.path.join(self.build_temp, imc_dir)
if os.path.isdir(imc_build):
shutil.rmtree(imc_build)
shutil.copytree(imc_dir, imc_build)
# Generate imcpy bindings
whitelist = []
if os.path.isfile('whitelist.cfg'):
with open('whitelist.cfg', 'rt') as f:
# Ignore empty lines and lines that starts with hashtag
whitelist = [x.strip().lower() for x in f.readlines() if x.strip() and not x.startswith('#')]
print('Generating IMC bindings using whitelist.cfg.')
# Generate md5 of IMC spec
imc_xml = os.path.join(imc_dir, 'IMC.xml')
with open(imc_xml, 'rb') as f:
b_imc_xml = f.read()
md5 = hashlib.md5()
md5.update(b_imc_xml)
imc_md5 = md5.hexdigest()
# Check for previous md5 and compare
already_generated = False
md5_path = os.path.join('src', 'generated', 'imc.md5')
if os.path.exists(md5_path):
with open(md5_path, 'rt') as f:
imc_md5_current = f.read()
if imc_md5_current == imc_md5:
already_generated = True
else:
# Remove old bindings on MD5 mismatch
shutil.rmtree(os.path.join('src', 'generated'))
# Generate bindings if necessary
if not already_generated:
print('Generating python bindings.')
pb = IMCPybind(imc_xml, whitelist=whitelist)
pb.write_bindings()
print('Generating stub file for typing hints.')
pyi = IMCPyi(os.path.join(imc_dir, 'IMC.xml'), whitelist=whitelist)
pyi.write_pyi()
print('Compiling with cmake.')
subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=self.build_temp)
# Copy pyi file to out dir
shutil.move('_imcpy.pyi', os.path.join(extdir, '_imcpy.pyi'))
# Build was successful, write imc md5
with open(md5_path, 'wt') as f:
f.write(imc_md5)
if __name__ == '__main__':
with open('README.md', 'r', encoding='utf-8') as fh:
long_description = fh.read()
setup(
name='imcpy',
version='1.1.0',
author='Oystein Sture',
author_email='[email protected]',
description='Python bindings for DUNE-IMC',
long_description=long_description,
long_description_content_type='text/markdown',
url='https://github.com/oysstu/imcpy',
project_urls={
'Bug Tracker': 'https://github.com/oysstu/imcpy/issues',
},
license='MIT',
classifiers=[
'Programming Language :: Python :: 3',
'License :: OSI Approved :: MIT License',
'Intended Audience :: Developers',
'Intended Audience :: Other Audience',
'Intended Audience :: Science/Research',
'Topic :: Scientific/Engineering',
],
packages=['imcpy', 'imcpy.actors', 'imcpy.algorithms', 'imcpy.coordinates', 'imcpy.network'],
python_requires='>=3.6',
install_requires=['ifaddr'],
extras_require={'LSFExporter': ['pandas']},
package_data={'': ['_imcpy.pyi'], 'imcpy.coordinates': ['*.pyi'], 'imcpy.algorithms': ['*.pyi']},
include_package_data=True,
ext_modules=[CMakeExtension('_imcpy')],
cmdclass=dict(build_ext=CMakeBuild),
zip_safe=False,
)