-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
152 lines (138 loc) · 4.37 KB
/
Copy pathsetup.py
File metadata and controls
152 lines (138 loc) · 4.37 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
#!/usr/bin/env python3
"""
Setup script for libfreenect Python wrapper
Combines modern setuptools with backward compatibility
"""
import os
import sys
import re
import numpy as np
from setuptools import setup, Extension
def get_cython_version():
"""
Returns:
Version as a pair of ints (major, minor)
Raises:
ImportError: Can't load cython or find version
"""
try:
import Cython.Compiler.Main
try:
# New way
version = Cython.__version__
except AttributeError:
# Old way
version = Cython.Compiler.Main.version
match = re.search(r'^([0-9]+)\.([0-9]+)', version)
if match:
return [int(g) for g in match.groups()]
else:
raise ImportError("Could not parse Cython version")
except ImportError:
raise ImportError("Cython not available")
# Determine source file and build approach
try:
cython_version = get_cython_version()
print(f"Found Cython version: {'.'.join(map(str, cython_version))}")
# Handle Cython version compatibility (like your CMake does)
if cython_version[0] == 0:
# Use old Cython-compatible file
source_file = "freenect.cython0.pyx"
print("Using freenect.cython0.pyx for Cython 0.x")
else:
# Use new Cython-compatible file
source_file = "freenect.pyx"
print("Using freenect.pyx for Cython 1.x+")
from Cython.Build import cythonize
use_cython = True
except ImportError:
print("Cython not found, looking for pre-compiled C files...")
# Fall back to C files if available
if os.path.exists("freenect.c"):
source_file = "freenect.c"
use_cython = False
print("Using pre-compiled freenect.c")
else:
raise ImportError("Neither Cython nor pre-compiled C files found!")
# Get numpy include directory
numpy_include = np.get_include()
# Base include directories (combining your original paths with new ones)
base_includes = [
numpy_include,
"../../include/", # Your original relative path
"../c_sync/", # Your original relative path
]
# Platform-specific library and include paths
if sys.platform == "darwin": # macOS
include_dirs = base_includes + [
"/usr/local/include",
"/usr/local/include/libfreenect",
"/usr/local/include/libusb-1.0",
"/opt/homebrew/include",
"/opt/homebrew/include/libfreenect",
"/opt/homebrew/include/libusb-1.0",
]
library_dirs = [
"/usr/local/lib",
"/usr/local/lib64",
"/opt/homebrew/lib",
]
libraries = ["usb-1.0", "freenect", "freenect_sync"]
elif sys.platform.startswith("linux"): # Linux
include_dirs = base_includes + [
"/usr/include",
"/usr/include/libfreenect",
"/usr/include/libusb-1.0/",
"/usr/local/include",
"/usr/local/include/libfreenect",
"/usr/local/include/libusb-1.0",
]
library_dirs = [
"/usr/lib",
"/usr/local/lib",
"/usr/local/lib64",
"/usr/lib/x86_64-linux-gnu",
]
libraries = ["usb-1.0", "freenect", "freenect_sync"]
elif sys.platform == "win32": # Windows
include_dirs = base_includes + [
r"C:\Program Files\libfreenect\include",
r"C:\libfreenect\include",
]
library_dirs = [
r"C:\Program Files\libfreenect\lib",
r"C:\libfreenect\lib",
]
libraries = ["freenect", "freenect_sync"] # Windows might not need libusb-1.0 explicitly
else:
# Default fallback
include_dirs = base_includes
library_dirs = ["/usr/local/lib", "/usr/local/lib64", "/usr/lib/"]
libraries = ["usb-1.0", "freenect", "freenect_sync"]
# Define the extension module
extensions = [
Extension(
name="freenect",
sources=[source_file],
include_dirs=include_dirs,
library_dirs=library_dirs,
libraries=libraries,
runtime_library_dirs=['/usr/local/lib', '/usr/local/lib64', '/usr/lib/'],
extra_compile_args=['-fPIC', '-O3'],
language="c",
)
]
# Cythonize if using Cython
if use_cython:
ext_modules = cythonize(
extensions,
compiler_directives={
"language_level": "3",
"boundscheck": False,
"wraparound": False,
}
)
else:
ext_modules = extensions
if __name__ == "__main__":
setup(ext_modules=ext_modules)