-
Notifications
You must be signed in to change notification settings - Fork 7
/
setup.py
171 lines (149 loc) · 6.64 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
import sys
if sys.version_info < (3, 6):
raise Exception('Building jgrapht requires Python 3.6 or higher.')
import os
import codecs
import setuptools
from setuptools import setup
from setuptools import Command
from setuptools import Extension
from setuptools.command.build_ext import build_ext
from distutils.command.build import build
extra_link_args = []
runtime_library_dirs = []
if sys.platform.startswith('win32'):
so_ext = '.dll'
capi_filename = 'jgrapht_capi' + so_ext
if sys.platform.startswith('linux'):
so_ext = '.so'
capi_filename = 'libjgrapht_capi' + so_ext
# Make sure that _backend.so will be able to load jgrapht_capi.so
runtime_library_dirs=['$ORIGIN']
elif sys.platform.startswith('darwin'):
so_ext = '.dylib'
capi_filename = 'libjgrapht_capi' + so_ext
extra_link_args = ['-Wl,-rpath,@loader_path']
class BuildCapiCommand(Command):
"""A custom command to build the jgrapht-capi from source."""
description = 'build jgrapht-capi'
def initialize_options(self):
self.build_lib = None
self.inplace = 0
self.src_dir = None
self.build_dir = None
self.package_name = None
self.filename = None
def finalize_options(self):
self.set_undefined_options('build',
('build_lib', 'build_lib'),
)
self.set_undefined_options('build_ext',
('inplace', 'inplace'),
)
self.src_dir = os.path.join('vendor', 'source', 'jgrapht-capi')
self.build_dir = os.path.join('vendor', 'build', 'jgrapht-capi')
self.package_name = 'jgrapht'
self.filename = capi_filename
def run(self):
"""Compile the jgrapht-capi from the git submodule inside `vendor/source/jgrapht-capi`."""
# Because setuptools develop command reinitializes the build_ext command with
# inplace=1 without also reinitializing its subcommands, we need to update
# the inplace option every time we run
self.inplace = self.get_finalized_command('build_ext').inplace
if not os.path.isfile(os.path.join(self.src_dir, "CMakeLists.txt")):
# No git submodule present with vendored source
print("Cannot find source in " + self.src_dir)
print("Make sure that you recursively checkout the submodule")
print("")
return False
print("Found source at {}".format(self.src_dir))
# Additional CMake parameters should be set as environment variables
# before calling setup.py depending on the platform and toolchain.
self.spawn(['cmake', '-B{}'.format(self.build_dir), '-H{}'.format(self.src_dir)])
self.spawn(['cmake', '--build', self.build_dir])
lib_source_path = os.path.join(self.build_dir, self.filename)
# inplace will is set to 1 when the develop command runs.
# Copy the JgraphT C API directly to the development area
if self.inplace:
lib_target_path = self.package_name
else:
lib_target_path = os.path.join(self.build_lib, self.package_name)
self.mkpath(lib_target_path)
self.copy_file(lib_source_path, os.path.join(lib_target_path, self.filename))
class CustomBuild(build):
# Because by default distutils build_py runs before build_ext and misses the SWIG
# generated .py file(s) we need to reorder the sub_commands to something sensible
sub_commands = [('build_clib', build.has_c_libraries),
('build_ext', build.has_ext_modules),
('build_py', build.has_pure_modules),
('build_scripts', build.has_scripts),
]
class CustomBuildExt(build_ext):
# I wish this was used more by distutils, but setting it anyway
sub_commands = [('build_capi', None),
]
def run(self):
self.run_command('build_capi')
super().run()
_backend_extension = Extension('jgrapht._backend', ['jgrapht/backend.i','jgrapht/backend.c'],
include_dirs=['jgrapht/', 'vendor/build/jgrapht-capi/', 'vendor/build/jgrapht-capi/src/main/native'],
library_dirs=['vendor/build/jgrapht-capi/'],
libraries=['jgrapht_capi'],
runtime_library_dirs=runtime_library_dirs,
extra_link_args=extra_link_args,
)
def read(rel_path):
here = os.path.abspath(os.path.dirname(__file__))
with codecs.open(os.path.join(here, rel_path), 'r') as fp:
return fp.read()
def get_version(rel_path):
for line in read(rel_path).splitlines():
if line.startswith('__version__'):
delim = '"' if '"' in line else "'"
return line.split(delim)[1]
else:
raise RuntimeError("Unable to find version string.")
with open("README.md", "r") as fh:
long_description = fh.read()
setup(
name='jgrapht',
cmdclass={
'build_capi': BuildCapiCommand,
'build_ext': CustomBuildExt,
'build': CustomBuild,
},
ext_modules=[_backend_extension],
version=get_version('jgrapht/__version__.py'),
description='JGraphT graph library',
long_description=long_description,
long_description_content_type='text/markdown',
author='Dimitrios Michail',
author_email='[email protected]',
url='https://github.com/d-michail/python-jgrapht',
license='LGPL-2.1-or-later OR EPL-2.0',
platforms=['any'],
packages=setuptools.find_packages(),
classifiers = [
'Development Status :: 4 - Beta',
'Intended Audience :: Science/Research',
'Intended Audience :: Developers',
'Operating System :: POSIX :: Linux',
'Operating System :: MacOS :: MacOS X',
'Operating System :: Microsoft :: Windows',
'License :: OSI Approved :: Eclipse Public License 2.0 (EPL-2.0)',
'License :: OSI Approved :: GNU Lesser General Public License v2 or later (LGPLv2+)',
'Programming Language :: C',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Topic :: Documentation :: Sphinx',
'Topic :: Scientific/Engineering',
'Topic :: Scientific/Engineering :: Mathematics',
'Topic :: Scientific/Engineering :: Artificial Intelligence',
'Topic :: Software Development :: Libraries'
],
keywords='graphs,networks,data-structures,algorithms',
python_requires='>=3.6'
)