-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsetup.py
95 lines (81 loc) · 3.6 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
import os
import re
import sys
import subprocess
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
from packaging.version import Version, parse
# Get version number information
base_path = os.path.dirname(os.path.realpath(__file__))
file_path = os.path.join( base_path, 'MDI_Library', 'mdi_global.h' )
found_major_version = False
found_minor_version = False
found_patch_version = False
with open( file_path ) as mdi_file:
line = mdi_file.readline()
while line and ( not found_major_version or not found_minor_version or not found_patch_version ):
print("line: " + str(line) )
sline = line.split()
if line.startswith( '#define MDI_MAJOR_VERSION_ ' ):
major_version = sline[2]
found_major_version = True
elif line.startswith( '#define MDI_MINOR_VERSION_ ' ):
minor_version = sline[2]
found_minor_version = True
elif line.startswith( '#define MDI_PATCH_VERSION_ ' ):
patch_version = sline[2]
found_patch_version = True
line = mdi_file.readline()
if not found_major_version or not found_minor_version or not found_patch_version:
raise RuntimeError("Unable to identify package version number")
mdi_version = major_version + '.' + minor_version + '.' + patch_version
class CMakeExtension(Extension):
def __init__(self, name, source_dir=''):
Extension.__init__(self, name, sources=[])
self.source_dir = os.path.abspath(source_dir)
class CMakeBuild(build_ext):
def run(self):
# Verify that CMake >= 3.5.0 is available
try:
version = subprocess.check_output(['cmake', '--version'])
except OSError:
raise RuntimeError("CMake is required for the following extensions: " +
", ".join(e.name for e in self.extensions))
version_match = re.search(r'version\s*([\d.]+)', version.decode())
if version_match:
cmake_version = Version(version_match.group(1))
else:
raise ValueError("Unable to find version number")
if cmake_version < parse('3.5.0'):
raise RuntimeError("CMake >= 3.5.0 is required for the following extensions: " +
", ".join(e.name for e in self.extensions))
# Build all CMake extensions
for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext):
# Get build directory
ext_dir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
if not ext_dir.endswith(os.path.sep):
ext_dir += os.path.sep
install_dir = os.path.join( ext_dir, "mdi" )
# Set CMake arguments
cmake_args = ['-DCMAKE_INSTALL_PREFIX=' + install_dir,
'-DCMAKE_INSTALL_INCLUDEDIR=' + ext_dir,
'-DCMAKE_INSTALL_LIBDIR=' + ext_dir,
'-DPYTHON_EXECUTABLE=' + sys.executable,
'-Dlanguage=Python']
# Do the CMake install
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
subprocess.check_call( ['cmake', ext.source_dir] + cmake_args,
cwd=self.build_temp)
subprocess.check_call( ['cmake', '--build', '.'],
cwd=self.build_temp)
subprocess.check_call( ['cmake', '--install', '.', '--prefix', install_dir],
cwd=self.build_temp)
setup(
name='pymdi',
version=mdi_version,
ext_modules=[CMakeExtension('mdi')],
cmdclass=dict(build_ext=CMakeBuild),
)