-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsetup.py
More file actions
159 lines (130 loc) · 5.5 KB
/
setup.py
File metadata and controls
159 lines (130 loc) · 5.5 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
# -*- coding: utf-8 -*-
# The following GitHub repos were used for this setup.py file
# Used the example in https://github.com/pybind/cmake_example/blob/master/setup.py
# License: https://github.com/pybind/cmake_example/blob/master/LICENSE
# Used the modification in the "class CMakeBuild(build_ext):" from https://github.com/edmBernard/pybind11_opencv_numpy/blob/master/setup.py
# License: https://github.com/edmBernard/pybind11_opencv_numpy/blob/master/LICENSE
# Used also the adaptation from
# https://github.com/safijari/apriltags2_ethz/blob/master/setup.py
# License: https://github.com/safijari/apriltags2_ethz/blob/master/LICENSE
# Update 05/2022
# To fully automate the build process mostly the code from the following GitHub repo was used
# https://github.com/safijari/apriltags2_ethz/blob/master/setup.py
# License: https://github.com/safijari/apriltags2_ethz/blob/master/LICENSE
import sys
import os
import platform
import subprocess
from packaging.version import Version
from setuptools import setup, Extension, find_packages
from setuptools.command.build_ext import build_ext
import numpy as np
def get_system_architecture():
"""
Determine the VCPKG_TARGET_TRIPLET and CMAKE_OSX_ARCHITECTURES based on host OS.
"""
system = platform.system()
machine = platform.machine()
if system == "Darwin":
if machine == "arm64":
return "arm64-osx", "arm64"
else:
return "x64-osx", "x86_64"
elif system == "Linux":
return "x64-linux", "x86_64"
elif system == "Windows":
return "x64-windows-static-md", "x86_64"
else:
raise RuntimeError(f"Unsupported platform: {system}")
def get_python_site_packages():
"""
Get the site-packages directory for the current Python environment.
"""
python_version = "{}.{}".format(
sys.version_info.major, sys.version_info.minor)
return os.path.join(sys.prefix, 'lib', 'python' + python_version, 'site-packages')
class CMakeExtension(Extension):
def __init__(self, name, sourcedir=''):
super().__init__(name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
class CMakeBuild(build_ext):
def run(self):
try:
out = subprocess.check_output(['cmake', '--version'])
print(out.decode())
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)))
# extdir = get_python_site_packages() # Site packages directory for installation
# Ensure the path includes 'pypupilext'
# extdir = os.path.join(extdir, 'pypupilext')
# Create the directory if it does not exist
# os.makedirs(extdir, exist_ok=True)
extdir = os.path.abspath(os.path.dirname(
self.get_ext_fullpath(ext.name)))
cmake_args = [
'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={}'.format(extdir),
'-DPYTHON_EXECUTABLE={}'.format(sys.executable),
'-DVERSION_INFO={}'.format(self.distribution.get_version()),
'-DCMAKE_BUILD_TYPE=Release',
'-DCMAKE_TOOLCHAIN_FILE=3rdparty/vcpkg/scripts/buildsystems/vcpkg.cmake',
f'-DVCPKG_TARGET_TRIPLET={vcpkg_triplet}',
'-DPython_NumPy_INCLUDE_DIR={}'.format(np.get_include()),
'-DTBB_TEST=OFF',
'-DTBBMALLOC_BUILD=OFF',
'-DTBBMALLOC_PROXY_BUILD=OFF',
'-DBUILD_SHARED_LIBS=OFF'
]
os.makedirs(extdir, exist_ok=True)
if osx_arch:
cmake_args.append('-DCMAKE_OSX_ARCHITECTURES={}'.format(osx_arch))
build_args = ['--config', 'Release']
if platform.system() == "Windows":
cmake_args += [
'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE={}'.format(extdir)]
build_args += ['--', '/m']
else:
cmake_args += ['-DCMAKE_BUILD_TYPE=Release']
build_args += ['--', '-j1']
env = os.environ.copy()
env['VCPKG_TARGET_TRIPLET'] = vcpkg_triplet
if osx_arch:
env['CMAKE_OSX_ARCHITECTURES'] = osx_arch
else:
env['CMAKE_SYSTEM_PROCESSOR'] = osx_arch
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
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)
vcpkg_triplet, osx_arch = get_system_architecture()
setup(
name='PyPupilEXT',
version='0.0.1',
author='Moritz Lode, Babak Zandi',
author_email='',
description='Pupil detection library for Python including algorithms and camera calibration routines.',
long_description='',
ext_modules=[CMakeExtension('pypupilext._pypupil', sourcedir='')],
cmdclass={'build_ext': CMakeBuild},
zip_safe=False,
packages=find_packages(),
setup_requires=['wheel', 'cmake'],
install_requires=[
'numpy',
'pandas',
'matplotlib',
'opencv-python'
],
python_requires='>=3.9, <3.11',
include_package_data=True,
package_data={'': ['README.md'],
'pypupilext': ['*.so', '*.dylib'],
'': ['3rdparty/PupilEXT_Third_Party_Licenses/*']}
)