-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsetup.py
More file actions
211 lines (174 loc) · 7.56 KB
/
setup.py
File metadata and controls
211 lines (174 loc) · 7.56 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# ----------------------------------------------------------------------------
# PyOgmaNeo
# Copyright(c) 2016-2020 Ogma Intelligent Systems Corp. All rights reserved.
#
# This copy of OgmaNeo is licensed to you under the terms described
# in the PYOGMANEO_LICENSE.md file included in this distribution.
# ----------------------------------------------------------------------------
# -*- coding: utf-8 -*-
from setuptools import setup, Extension
from distutils.command.build import build
from distutils.command.build_ext import build_ext
from distutils.command.install import install
from distutils import sysconfig
import shutil
import shlex
import subprocess
import os, sys
import os.path
from os import chdir, getcwd
from os.path import abspath, dirname, split
# Check if we're running 64-bit Python
is64bit = sys.maxsize > 2**32
# Check if this is a debug build of Python.
if hasattr(sys, 'gettotalrefcount'):
build_type = 'Debug'
else:
build_type = 'Release'
# Subclass the distutils install command
class install_subclass(install):
description = "Building the PyOgmaNeo C++ library, generating SWiG bindings, and installing PyOgmaNeo"
def run(self):
# Run build_ext first, so that it can generate
# the OgmaNeo library and SwiG the .i file
self.run_command("build_ext")
# Return through the usual super().run
return install.run(self)
# Subclass the disutils build command, to reorder sub_commands
# (build_ext *before* build_py)
class build_subclass(build):
sub_commands = [('build_ext', build.has_ext_modules),
('build_py', build.has_pure_modules),
('build_clib', build.has_c_libraries),
('build_scripts', build.has_scripts),
]
# Subclass the distutils build_ext command
class build_ext_subclass(build_ext):
description = "Building the C-extension for PyOgmaNeo with CMake"
user_options = [('extra-cmake-args=', None, 'extra arguments for CMake')]
def initialize_options(self):
build_ext.initialize_options(self)
self.extra_cmake_args = ''
def get_ext_path(self, name):
build_py = self.get_finalized_command('build_py')
package_dir = build_py.get_package_dir('pyogmaneo')
suffix = sysconfig.get_config_var('EXT_SUFFIX')
if suffix is None:
suffix = sysconfig.get_config_var('SO')
suffix = "." + suffix.rsplit(".", 1)[-1]
filename = "../" + name + suffix
return os.path.abspath(os.path.join(package_dir, filename))
def get_ext_name(self, name):
suffix = sysconfig.get_config_var('EXT_SUFFIX')
if suffix is None:
suffix = sysconfig.get_config_var('SO')
suffix = "." + suffix.rsplit(".", 1)[-1]
return name + suffix
def get_py_path(self, name):
build_py = self.get_finalized_command('build_py')
package_dir = build_py.get_package_dir('pyogmaneo')
filename = "../" + name + ".py"
return os.path.abspath(os.path.join(package_dir, filename))
def get_py_name(self, name):
return name + ".py"
def build_extensions(self):
global build_type
# The directory containing this setup.py
source = dirname(abspath(__file__))
# The staging directory for the library being built
build_temp = os.path.join(os.getcwd(), self.build_temp)
build_lib = os.path.join(os.getcwd(), self.build_lib)
# Change to the build directory
saved_cwd = getcwd()
if not os.path.isdir(build_temp):
self.mkpath(build_temp)
chdir(build_temp)
extra_cmake_args = shlex.split(self.extra_cmake_args)
cmake_command = ['cmake'] + extra_cmake_args
if "-G" not in self.extra_cmake_args:
cmake_generator = 'Unix Makefiles'
if sys.platform == 'darwin':
cmake_generator = 'Xcode'
elif sys.platform == 'win32':
if sys.version_info.major < 3 or (sys.version_info.major == 3 and sys.version_info.minor <= 2):
cmake_generator = 'MinGW Makefiles'
else:
if sys.version_info.major == 3 and (sys.version_info.minor == 3 or sys.version_info.minor == 4):
cmake_generator = 'Visual Studio 10 2010'
else:
cmake_generator = 'Visual Studio 14 2015'
if is64bit:
cmake_generator += ' Win64'
cmake_command += ['-G', cmake_generator]
cmake_command += ['-DPYTHON_VERSION='+str(sys.version_info.major)]
cmake_command.append(source)
subprocess.call(cmake_command)
if sys.platform == 'win32' or sys.platform == 'darwin':
self.spawn(['cmake', '--build', '.', '--target', 'install', '--config', build_type])
else:
self.spawn(['cmake', '--build', '.', '--config', build_type])
if not self.inplace:
# Move the library and neo.py bindings interface
# to the place expected by the Python build
self._found_names = []
built_ext = self.get_ext_name("_pyogmaneo")
if os.path.exists(built_ext):
ext_path = os.path.join(build_lib, built_ext)
if os.path.exists(ext_path):
os.remove(ext_path)
self.mkpath(os.path.dirname(ext_path))
print('Moving library', built_ext,
'to build path', ext_path)
shutil.copy(built_ext, ext_path)
shutil.copy(ext_path, saved_cwd)
self._found_names.append("_pyogmaneo")
built_py = self.get_py_name("pyogmaneo")
py_path = os.path.join(build_lib, built_py)
print('Moving Py file', built_py,
'to build path', py_path)
shutil.copy(built_py, py_path)
shutil.copy(py_path, saved_cwd)
else:
raise RuntimeError('C-extension failed to build:',
os.path.abspath(built_ext))
chdir(saved_cwd)
def get_names(self):
return self._found_names
def get_outputs(self):
# Just the C extensions
return [self.get_ext_path(name) for name in self.get_names()]
# The list of cpp files here is optional, the CMakeLists.txt
# script overrides this list of cpp source files.
extension_mod = Extension(
name="_pyogmaneo",
sources=["pyogmaneo.i"]
)
setup(
name="pyogmaneo",
version="1.0.0",
description="Python bindings for the OgmaNeo2 library",
long_description='https://github.com/ogmacorp/PyOgmaNeo2',
author='Ogma Intelligent Systems Corp',
author_email='info@ogmacorp.com',
url='https://ogmacorp.com/',
license='Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License',
classifiers=[
"Development Status :: 5 - Production/Stable",
"Environment :: Console",
"Intended Audience :: Science/Research",
"License :: Other/Proprietary License",
"Operating System :: MacOS :: MacOS X",
"Operating System :: Microsoft :: Windows",
"Operating System :: POSIX :: Linux",
"Programming Language :: Python",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
"Topic :: Software Development :: Libraries :: Python Modules"
],
py_modules=["pyogmaneo"],
ext_modules=[extension_mod],
cmdclass={
'build': build_subclass,
'build_ext': build_ext_subclass,
'install': install_subclass
},
)