forked from Baekalfen/PyBoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
317 lines (276 loc) · 11.1 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
316
317
import distutils.cmd
import os
import platform
import shutil
import subprocess
import sys
from distutils.command.clean import clean as _clean
from distutils.command.clean import log
from distutils.dir_util import remove_tree
from multiprocessing import cpu_count
from pathlib import Path
from setuptools import Extension, find_packages, setup
from setuptools.command.test import test
# The requirements.txt file will not be included in the PyPi package
REQUIREMENTS = """\
# Change in setup.py
cython; platform_python_implementation == 'CPython'
numpy
pillow
pysdl2
"""
def load_requirements(filename):
if os.path.isfile(filename):
with open(filename, "w") as f:
f.write(REQUIREMENTS)
return [line.split(";")[0].strip() for line in REQUIREMENTS.splitlines()]
requirements = load_requirements("requirements.txt")
CYTHON = platform.python_implementation() != "PyPy"
if CYTHON:
# "Recommended" method of installing Cython: https://github.com/pypa/pip/issues/5761
from setuptools import dist
dist.Distribution().fetch_build_eggs(["cython"])
from Cython.Build import cythonize
import Cython.Compiler.Options
from Cython.Distutils import build_ext
else:
try:
requirements.remove("cython")
except ValueError:
pass
class build_ext(distutils.cmd.Command):
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
pass
ROOT_DIR = "pyboy"
codecov = "--codecov-trace" in sys.argv
if codecov:
sys.argv.pop(sys.argv.index("--codecov-trace"))
directive_defaults = Cython.Compiler.Options.get_directive_defaults()
directive_defaults["linetrace"] = True
directive_defaults["binding"] = True
class PyTest(test):
def finalize_options(self):
super().finalize_options()
self.test_suite = True
self.test_args = []
def run_tests(self):
if not os.environ.get("TEST_NO_EXAMPLES"):
script_path = os.path.dirname(os.path.realpath(__file__))
base = Path(f"{script_path}/examples/")
tetris_script = base / f"gamewrapper_tetris.py"
tetris_rom = Path(f"{script_path}/ROMs/Tetris.gb")
return_code = subprocess.Popen([sys.executable, str(tetris_script), str(tetris_rom), "--quiet"]).wait()
if return_code != 0:
sys.exit(return_code)
mario_script = base / f"gamewrapper_mario.py"
mario_rom = Path(f"{script_path}/ROMs/SuperMarioLand.gb")
return_code = subprocess.Popen([sys.executable, str(mario_script), str(mario_rom), "--quiet"]).wait()
if return_code != 0:
sys.exit(return_code)
import pytest
args = ["tests/", f"-n{cpu_count()}", "-v", "--dist=loadfile"]
# args = ["tests/", "-v", "-x"]
if codecov: # TODO: There's probably a more correct way to read the argv flags
args += ["--cov=./"]
sys.exit(pytest.main(args))
# Add inplace functionality to the clean command
class clean(_clean):
user_options = _clean.user_options + [("inplace", "i", "remove all output from an inplace build")]
boolean_options = _clean.boolean_options + ["inplace"]
def initialize_options(self):
super().initialize_options()
self.inplace = None
def run(self):
super().run()
if self.inplace:
for p in ("PyBoy.egg-info", "build", "dist"):
if os.path.isdir(p):
shutil.rmtree(p)
for root, dirs, files in os.walk(ROOT_DIR):
if "__pycache__" in dirs:
log.info(f"removing: {os.path.join(root, '__pycache__')}")
remove_tree(os.path.join(root, "__pycache__"))
for f in files:
if os.path.splitext(f)[1] in (
".pyo", ".pyc", ".pyd", ".so", ".c", ".h", ".dll", ".lib", ".exp", ".html"
):
print(f"removing: {os.path.join(root, f)}")
os.remove(os.path.join(root, f))
# Locate the directory with SDL2 headers and library
def locate_sdl2_config():
print("Searching for SDL2 installation")
if sys.platform == "win32" and "GCC" in platform.python_compiler():
print("Detected msys, looking for sdl2-config")
msys_sh = None
msys_sdl2_config = None
for path in os.getenv("PATH").split(";"):
if not msys_sh and os.path.isfile(os.path.join(path, "sh.exe")):
msys_sh = f"{path}\\sh.exe"
if not msys_sdl2_config and os.path.isfile(os.path.join(path, "sdl2-config")):
msys_sdl2_config = f"{path}\\sdl2-config"
if msys_sh and msys_sdl2_config:
print(f"Found sdl2-config at {msys_sdl2_config} and shell at {msys_sh}")
msys_sdl2_config = msys_sdl2_config.replace("\\", "/")
msys_mingw_prefix = msys_sdl2_config[:msys_sdl2_config.index("/bin")]
msys_sdl2_config = f"/{msys_sdl2_config[0].lower()}/{msys_sdl2_config[2:]}"
return os.popen(f"{msys_sh} -c '{msys_sdl2_config} --prefix={msys_mingw_prefix}"
f" --cflags --libs'").read().split()
else:
print(f"Could not find shell ({msys_sh}) and sdl2-config ({msys_sdl2_config})")
return None
elif sys.platform == "win32" and "MSC" in platform.python_compiler():
return [] # Defaults to env var PYSDL2_DLL_PATH
elif sys.platform in ["darwin", "linux"]:
print("Didn't detect msys2 environment, trying sdl2-config assuming Unix environment")
return os.popen("sdl2-config --cflags --libs").read().split()
else:
print(f"Unsupported OS type: {sys.platform}")
sys.exit(121)
# Define libs, libdirs, includes and cflags for SDL2
def define_lib_includes_cflags():
sdl2_config = locate_sdl2_config()
sdl2_path = os.getenv("PYSDL2_DLL_PATH")
libs = []
libdirs = []
includes = []
cflags = []
if sdl2_config != []:
for arg in sdl2_config:
if arg.startswith("-l"):
libs += [arg[2:]]
elif arg.startswith("-L"):
libdirs += [arg[2:]]
elif arg.startswith("-I"):
includes += [arg[2:]]
else:
cflags += [arg]
print("SDL2 found using sdl2-config")
elif sdl2_path:
print("No sdl2-config found, using PYSDL2_DLL_PATH variable")
sdl2_lib_arch = "x86" if "x86" in sdl2_path else "x64" if "x64" in sdl2_path else ""
sdl2_path = os.path.abspath(sdl2_path[:sdl2_path.index("lib")])
if not os.path.isdir(sdl2_path):
print(f"Error locating SDL2: {sdl2_path} is not a directory")
sys.exit(122)
else:
print(f"Found SDL2 at {sdl2_path}")
libs += ["SDL2"]
libdirs += [os.path.join(sdl2_path, "lib", sdl2_lib_arch)]
includes += [os.path.join(sdl2_path, "include", p) for p in ("", "SDL2")]
else:
print("SDL2 cannot be found through neither sdl2-config nor PYSDL2_DLL_PATH")
sys.exit(123)
return libs, libdirs, includes, cflags
def prep_pxd_py_files():
ignore_py_files = ["__main__.py", "manager_gen.py", "opcodes_gen.py"]
# Cython doesn't trigger a recompile on .py files, where only the .pxd file has changed. So we fix this here.
# We also yield the py_files that have a .pxd file, as we feed these into the cythonize call.
for root, dirs, files in os.walk(ROOT_DIR):
for f in files:
if os.path.splitext(f)[1] == ".py" and f not in ignore_py_files:
yield os.path.join(root, f)
if os.path.splitext(f)[1] == ".pxd":
py_file = os.path.join(root, os.path.splitext(f)[0]) + ".py"
if os.path.isfile(py_file):
if os.path.getmtime(os.path.join(root, f)) > os.path.getmtime(py_file):
os.utime(py_file)
# Cython seems to cythonize these before cleaning, so we only add them, if we aren't cleaning.
ext_modules = None
if CYTHON and "clean" not in sys.argv:
if sys.platform == "win32":
# Cython currently has a bug in its code that results in symbol collision on Windows
def get_export_symbols(self, ext):
parts = ext.name.split(".")
initfunc_name = "PyInit_" + parts[-2] if parts[-1] == "__init__" else parts[-1] # noqa: F841
# Override function in Cython to fix symbol collision
build_ext.get_export_symbols = get_export_symbols
thread_count = 0 # Disables multiprocessing (windows)
elif platform.python_version().startswith("3.8"):
# Causes infinite recursion
thread_count = 0
else:
thread_count = cpu_count()
# Set up some values for use in setup()
libs, libdirs, includes, cflags = define_lib_includes_cflags()
py_pxd_files = prep_pxd_py_files()
cythonize_files = map(
lambda src: Extension(
src.split(".")[0].replace(os.sep, "."), [src],
include_dirs=includes,
library_dirs=libdirs,
libraries=libs,
extra_compile_args=cflags
), list(py_pxd_files)
)
ext_modules = cythonize(
[*cythonize_files], # This runs even if build_ext isn't invoked...
nthreads=thread_count,
annotate=False,
gdb_debug=False,
language_level=2,
compiler_directives={
"boundscheck": False,
"cdivision": True,
"cdivision_warnings": False,
"infer_types": True,
"initializedcheck": False,
"nonecheck": False,
"overflowcheck": False,
# "profile" : True,
"wraparound": False,
},
)
try:
this_directory = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(this_directory, "README.md"), encoding="utf-8") as f:
long_description = f.read()
except FileNotFoundError:
print("README.md not found")
long_description = ""
setup(
name="pyboy",
version="1.0.1",
packages=find_packages(),
author="Mads Ynddal",
author_email="[email protected]",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/Baekalfen/PyBoy",
classifiers=[
"License :: Free for non-commercial use",
"Operating System :: OS Independent",
"Programming Language :: Cython",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: Implementation :: PyPy",
"Topic :: System :: Emulators",
],
entry_points={
"console_scripts": ["pyboy = pyboy.__main__:main", ],
},
cmdclass={
"build_ext": build_ext,
"clean": clean,
"test": PyTest
},
install_requires=requirements,
tests_require=[
"pytest",
"pytest-xdist",
"pyopengl",
],
extras_require={
"all": [
"pyopengl",
"markdown",
"pdoc3",
],
},
zip_safe=(not CYTHON), # Cython doesn't support it
ext_modules=ext_modules,
python_requires=">=3.6",
package_data={"": ["*.pyx", "*.pxd", "*.c", "*.h", "bootrom.bin"]},
)