-
Notifications
You must be signed in to change notification settings - Fork 6
/
setup.py
315 lines (263 loc) · 10.5 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# setup.py
#
# Copyright 2021 Matthew D. Cutone <mcutone(at)opensciencetools.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
"""Installation script for PsychXR.
Installation is straightforward as long as your build environment is properly
configured. Significant portions of this library are written in Cython which is
converted to C++ code. Therefore, you must have a C++ compiler and SDKs
installed prior to building PsychXR from source. See
https://github.com/mdcutone/psychxr/blob/master/README.md for instructions on
how to build from source.
"""
import os
import platform
from pathlib import Path, PureWindowsPath
from setuptools import setup
from setuptools.extension import Extension
from Cython.Build import cythonize, build_ext
import numpy
# Version string for the project. Make sure this is updated to reflect the
# project branch version.
PSYCHXR_VERSION_STRING = '0.2.5rc1'
# environment variable values
ENV_TRUE = '1'
ENV_FALSE = '0'
# Cython and C++ include directories
CYTHON_INCLUDE_DIRS = [numpy.get_include(), '.']
CPP_INCLUDE_DIRS = CYTHON_INCLUDE_DIRS + ['include/', 'psychxr/tools/']
# library directories and library names
LIBRARY_DIRS = LIB_DIRS = ['lib/']
LIBRARIES = [] # required
# Extension modules to add to the package. This grows as extensions are built.
EXT_MODULES = []
# additional package data
PACKAGES = ['psychxr']
PACKAGE_DATA = ['*.pxi', '*.pxd', '*.pyx', '*.cpp', '*.h', '*.c']
DATA_FILES = ['*.pyd', '*.pxi', '*.dll', '*.lib']
# platform and build information
THIS_PLATFORM = platform.system()
BUILD_LIBOVR = os.environ.get('PSYCHXR_BUILD_LIBOVR', ENV_TRUE) == ENV_TRUE
BUILD_OPENHMD = os.environ.get('PSYCHXR_BUILD_OPENHMD', ENV_TRUE) == ENV_TRUE
# ------------------------------------------------------------------------------
# Setup build environment
#
# print out the build configuration
if BUILD_LIBOVR:
print("Configured to build `LibOVR` extension modules "
"(`PSYCHXR_BUILD_LIBOVR=1`).")
if BUILD_OPENHMD:
print("Configured to build `OpenHMD` extension modules "
"(`PSYCHXR_BUILD_OPENHMD=1`).")
# setup build environments on supported platforms
if THIS_PLATFORM == 'Windows':
os.environ["MSSdk"] = ENV_TRUE # ensure correct compiler is used
os.environ["DISTUTILS_USE_SDK"] = ENV_TRUE
LIBRARIES.extend(['opengl32', 'User32']) # required Windows libraries
LIBRARY_DIRS.extend(
[os.path.join('psychxr/drivers/openhmd/lib', 'win', 'x64')])
else:
if BUILD_LIBOVR: # windows only
print("WARNING: Cannot build `LibOVR`, platform is not supported.")
BUILD_LIBOVR = False
# ------------------------------------------------------------------------------
# Helper functions for the installer
#
def fix_path(path):
"""Fix a path and convert to an absolute location.
Parameters
----------
path : str or Path
Path to fix.
Returns
-------
str
Absolute path.
"""
if THIS_PLATFORM == 'Windows':
path = PureWindowsPath(path)
return str(Path(path).absolute())
def build_extension(name, **kwargs):
"""Build an extension.
Parameters
----------
name : str
FQN of the extension module.
Returns
-------
Extension
Object referencing the build extension module.
"""
# build the module and add it to the package directory
pyx_file_path = [r"/".join(name.split('.')) + ".pyx"]
pxd_include_path = kwargs.get('include_path', []) + CYTHON_INCLUDE_DIRS
cpp_file_path = [r"/".join(name.split('.')) + ".cpp"]
cpp_include_dirs = kwargs.get('include_dirs', []) + CPP_INCLUDE_DIRS
cpp_libraries = kwargs.get('libraries', []) + LIBRARIES
cpp_library_dirs = kwargs.get('library_dirs', []) + LIBRARY_DIRS
cythonize(
pyx_file_path,
include_path=pxd_include_path,
compiler_directives={
'embedsignature': True,
'language_level': 3})
ext = Extension(
name,
cpp_file_path,
include_dirs=cpp_include_dirs,
libraries=cpp_libraries,
library_dirs=cpp_library_dirs,
language="c++",
extra_compile_args=[''])
return ext
# ------------------------------------------------------------------------------
# Build common libraries
#
print("Building `psychxr.tools.vrmath` extension module ...")
vrmath_package_data = {
'psychxr.tools': PACKAGE_DATA}
vrmath_data_files = {
'psychxr/tools': DATA_FILES}
vrmath_build_params = {
'libraries': LIBRARIES + [],
'library_dirs': LIBRARY_DIRS,
'include_dirs': [fix_path('include/linmath')],
'packages': ['psychxr.tools'],
'package_data': vrmath_package_data,
'data_files': vrmath_data_files}
# compile the `tools` extension
EXT_MODULES.extend(
[build_extension(
"psychxr.tools.vrmath",
**vrmath_build_params)])
PACKAGES.extend(vrmath_build_params['packages'])
# ------------------------------------------------------------------------------
# Build driver extension libraries (e.g., libovr, openhmd, etc.)
#
if BUILD_LIBOVR:
print("Building `psychxr.drivers.libovr` extension module ...")
# build parameters for LibOVR passed to the compiler and linker
libovr_package_data = {
'psychxr.drivers.libovr': PACKAGE_DATA}
libovr_data_files = {'psychxr/drivers/libovr': DATA_FILES}
libovr_build_params = {
'libraries': ['LibOVR'],
'library_dirs': [],
'include_dirs': [],
'packages': ['psychxr.drivers.libovr'],
'package_data': libovr_package_data,
'data_files': libovr_data_files}
# get the path to the SDK, uses the `cohorts` folder if not defined
env_libovr_sdk_path = os.environ.get(
'PSYCHXR_LIBOVR_SDK_PATH',
r'cohorts\LibOVR')
# convert to a path object, make absolute
libovr_sdk_path = Path(PureWindowsPath(env_libovr_sdk_path)).absolute()
# check if the path is a directory
if not libovr_sdk_path.is_dir():
raise NotADirectoryError(
"ERROR: Cannot find the Oculus PC SDK at the specified location "
"'{}'. Make sure `PSYCHXR_LIBOVR_SDK_PATH` is set.".format(
str(libovr_sdk_path)))
# tell the user where the setup is looking for the SDK
print(r"Using `PSYCHXR_LIBOVR_SDK_PATH={}`".format(str(libovr_sdk_path)))
# base paths within the SDK
libovr_base_path = libovr_sdk_path.joinpath('LibOVR')
libovr_include_path = libovr_base_path.joinpath('Include')
libovr_lib_path = libovr_base_path.joinpath('Lib')
# add paths
libovr_build_params['include_dirs'] = [
fix_path(libovr_include_path),
fix_path(libovr_include_path.joinpath('Extras'))]
libovr_build_params['library_dirs'] = [
fix_path(libovr_lib_path.joinpath(
'Windows', 'x64', 'Release', 'VS2017'))]
# compile the `libovr` extension
EXT_MODULES.extend(
[build_extension(
"psychxr.drivers.libovr._libovr",
**libovr_build_params)])
PACKAGES.extend(libovr_build_params['packages'])
# build the OpenHMD driver
if BUILD_OPENHMD:
print("building `psychxr.drivers.openhmd` extension module ...")
ohmd_package_data = {
'psychxr.drivers.openhmd': PACKAGE_DATA}
ohmd_data_files = {
'psychxr/drivers/openhmd': DATA_FILES}
ohmd_build_params = {
'libraries': LIBRARIES + ['hidapi', 'openhmd'],
'library_dirs': LIBRARY_DIRS,
'include_dirs': [ # header files for needed libraries
fix_path('include/linmath'),
fix_path('include/openhmd')],
'packages': ['psychxr.drivers.openhmd'],
'package_data': ohmd_package_data,
'data_files': ohmd_data_files}
# compile the `openhmd` extension
EXT_MODULES.extend(
[build_extension(
"psychxr.drivers.openhmd._openhmd",
**ohmd_build_params)])
PACKAGES.extend(ohmd_build_params['packages'])
# ------------------------------------------------------------------------------
# Packaging and setup
#
setup_pars = {
"name": "psychxr",
"author": "Matthew D. Cutone, Laurie M. Wilcox",
"author_email": "[email protected]",
"maintainer": "Matthew D. Cutone",
"maintainer_email": "[email protected]",
"packages": PACKAGES,
"url": "http://psychxr.org",
"include_package_data": True,
"version": PSYCHXR_VERSION_STRING,
"license": "MIT",
"description":
"Python extension library for interacting with eXtended Reality "
"displays (HMDs), intended for research in neuroscience and "
"psychology.",
"long_description": "",
"classifiers": [
'Development Status :: 5 - Production/Stable',
'Operating System :: Microsoft :: Windows :: Windows 10',
'Operating System :: Microsoft :: Windows :: Windows 8.1',
'Operating System :: Microsoft :: Windows :: Windows 7',
'Programming Language :: Cython',
'Programming Language :: Python :: 3 :: Only',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'License :: OSI Approved :: MIT License',
'Topic :: Scientific/Engineering :: Human Machine Interfaces',
'Intended Audience :: Science/Research'],
"ext_modules": EXT_MODULES,
"install_requires": ["Cython>=0.29.3", "numpy>=1.13.3"],
"requires": ["numpy>=1.13.3"],
"cmdclass": {"build_ext": build_ext},
"zip_safe": False
}
setup(**setup_pars)