-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathsetup.py
More file actions
206 lines (168 loc) · 5.86 KB
/
setup.py
File metadata and controls
206 lines (168 loc) · 5.86 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
import sys
import os
import platform
import os.path
import shutil
from glob import glob
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
try:
from Cython.Build import cythonize
except ImportError:
print("Please install Cython and try again.")
exit(1)
SFML_HEADERS = os.getenv('SFML_HEADERS')
SFML_LIBRARIES = os.getenv('SFML_LIBRARIES')
SFML_INSTALL_PREFIX = os.getenv('SFML_INSTALL_PREFIX')
REPOSITORY_ROOT = os.path.abspath(os.path.dirname(__file__))
DEFAULT_SFML_PREFIX_NAMES = (
'sfml-3.0.2-install',
'sfml-3-install'
)
def resolve_sfml_path(explicit_path, *relative_parts):
if explicit_path:
return os.path.normpath(explicit_path)
if SFML_INSTALL_PREFIX:
candidate = os.path.join(SFML_INSTALL_PREFIX, *relative_parts)
if os.path.isdir(candidate):
return os.path.normpath(candidate)
for prefix_name in DEFAULT_SFML_PREFIX_NAMES:
candidate = os.path.join(REPOSITORY_ROOT, '.deps', prefix_name, *relative_parts)
if os.path.isdir(candidate):
return os.path.normpath(candidate)
return None
SFML_HEADERS = resolve_sfml_path(SFML_HEADERS, 'include')
SFML_LIBRARIES = resolve_sfml_path(SFML_LIBRARIES, 'lib')
WINDOWS_USE_SHARED_SFML = platform.system() == 'Windows'
cpp_link_args = []
if platform.system() == 'Windows':
cpp_compile_args = ['/std:c++17']
else:
cpp_compile_args = ['-std=c++17']
if platform.system() == 'Darwin':
minimum_macos_target = '10.15'
requested_macos_target = os.environ.get('MACOSX_DEPLOYMENT_TARGET', minimum_macos_target)
def parse_version(version):
parts = []
for part in version.split('.'):
if part.isdigit():
parts.append(int(part))
else:
break
return tuple(parts)
if parse_version(requested_macos_target) < parse_version(minimum_macos_target):
requested_macos_target = minimum_macos_target
os.environ['MACOSX_DEPLOYMENT_TARGET'] = requested_macos_target
macos_deployment_flag = f'-mmacosx-version-min={requested_macos_target}'
cpp_compile_args.append(macos_deployment_flag)
cpp_link_args.append(macos_deployment_flag)
def stage_generated_headers(build_temp):
staged_headers = []
for module in ('system', 'window', 'graphics', 'audio', 'network'):
header_files = glob(os.path.join('src', 'sfml', module, '*.hpp'))
header_files.append(os.path.join('src', 'sfml', module, module + '.h'))
header_files.append(os.path.join('src', 'sfml', module, module + '_api.h'))
destination = os.path.join(build_temp, 'include', 'pysfml', module)
os.makedirs(destination, exist_ok=True)
copied_files = []
for header_file in header_files:
if os.path.isfile(header_file):
destination_file = os.path.join(destination, os.path.basename(header_file))
shutil.copy(header_file, destination_file)
copied_files.append(destination_file)
if copied_files:
staged_headers.append((module, copied_files))
return staged_headers
class CythonBuildExt(build_ext):
def build_extensions(self):
staged_headers = stage_generated_headers(self.build_temp)
self.include_dirs.append(os.path.join(self.build_temp, 'include'))
self.compiler.include_dirs.append(os.path.join(self.build_temp, 'include'))
for module, files_to_install in staged_headers:
install_directory = os.path.join('include', 'pysfml', module)
data_files.append((install_directory, files_to_install))
super().build_extensions()
include_dirs = []
library_dirs = []
include_dirs.append(os.path.join('include', 'Includes'))
if SFML_HEADERS:
include_dirs.append(SFML_HEADERS)
if SFML_LIBRARIES:
library_dirs.append(SFML_LIBRARIES)
def extension(name, files, libs): return Extension(
name='sfml.' + name,
sources=[os.path.join('src', 'sfml', name, filename) for filename in files],
include_dirs=include_dirs,
library_dirs=library_dirs,
language='c++',
libraries=libs,
extra_compile_args=cpp_compile_args,
extra_link_args=cpp_link_args,
define_macros=[('SFML_STATIC', '1')] if platform.system() == 'Windows' and not WINDOWS_USE_SHARED_SFML else []
)
if platform.system() == 'Windows':
system_libs = [
'sfml-system'
]
window_libs = [
'sfml-system',
'sfml-window'
]
graphics_libs = [
'sfml-system',
'sfml-window',
'sfml-graphics'
]
audio_libs = [
'sfml-system',
'sfml-audio'
]
network_libs = [
'ws2_32',
'sfml-system',
'sfml-network'
]
else:
system_libs = ['sfml-system']
window_libs = ['sfml-system', 'sfml-window']
graphics_libs = ['sfml-system', 'sfml-window', 'sfml-graphics']
audio_libs = ['sfml-system', 'sfml-audio']
network_libs = ['sfml-system', 'sfml-network']
system = extension(
'system',
['system.pyx', 'error.cpp', 'hacks.cpp', 'NumericObject.cpp'],
system_libs
)
window = extension(
'window',
['window.pyx', 'DerivableWindow.cpp'],
window_libs
)
graphics = extension(
'graphics',
['graphics.pyx', 'DerivableRenderWindow.cpp', 'DerivableDrawable.cpp', 'NumericObject.cpp'],
graphics_libs)
audio = extension(
'audio',
['audio.pyx', 'DerivableSoundRecorder.cpp', 'DerivableSoundStream.cpp'],
audio_libs
)
network = extension(
'network',
['network.pyx'],
network_libs
)
ext_modules = cythonize(
[system, window, graphics, audio, network],
include_path=[os.path.join('include', 'Includes')],
compiler_directives={
'embedsignature': True,
'language_level': '3',
},
)
data_files = []
setup(
ext_modules=ext_modules,
data_files=data_files,
cmdclass={'build_ext': CythonBuildExt}
)