forked from Farama-Foundation/Arcade-Learning-Environment
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsetup.py
More file actions
197 lines (173 loc) · 7.21 KB
/
setup.py
File metadata and controls
197 lines (173 loc) · 7.21 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
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
import subprocess
import os
import sys
import shlex
import re
class CMakeExtension(Extension):
def __init__(self, name, sourcedir="", config=[]):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
self.config = config
class CMakeBuild(build_ext):
@staticmethod
def _shared_library_extension():
if sys.platform.startswith("linux"):
return ".so"
if sys.platform.startswith("darwin"):
return ".dylib"
if sys.platform.startswith("win"):
return ".dll"
raise RuntimeError('CMakeBuild: Platform "%s" not recognized' % sys.platform)
@classmethod
def _shared_library_filename(cls, name):
ext_suffix = cls._shared_library_extension()
library_format = "{}{}" if sys.platform.startswith("win") else "lib{}{}"
return library_format.format(name, ext_suffix)
def get_ext_filename(self, fullname):
ext = next((ext for ext in self.extensions if ext.name == fullname), None)
if isinstance(ext, CMakeExtension):
package_path = fullname.split(".")[:-1]
filename = self._shared_library_filename(fullname.split(".")[-1])
return os.path.join(*package_path, filename)
return super().get_ext_filename(fullname)
def build_extensions(self):
try:
subprocess.check_output(["cmake", "--version"])
except OSError:
raise RuntimeError(
"CMake must be installed to build the extensions: %s"
% ", ".join(ext.name for ext in self.extensions)
)
for ext in self.extensions:
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
build_temp = os.path.abspath(self.build_temp)
cfg = "Debug" if self.debug else "Release"
ext_suffix = self._shared_library_extension()
cmake_build_args = []
cmake_config_args = [
"-DCMAKE_BUILD_TYPE={}".format(cfg),
"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={}".format(extdir),
"-DCMAKE_RUNTIME_OUTPUT_DIRECTORY={}".format(extdir),
"-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY={}".format(build_temp),
"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}".format(cfg.upper(), extdir),
"-DCMAKE_RUNTIME_OUTPUT_DIRECTORY_{}={}".format(cfg.upper(), extdir),
"-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_{}={}".format(
cfg.upper(), build_temp
),
"-DPYTHON_MODULE_EXTENSION={}".format(ext_suffix),
] + ext.config
# -DCMAKE_BUILD_TYPE doesn't work on Windows
# we need to specify --config Release at build time
if sys.platform.startswith("win"):
# Specify platform x86 or x86-64
platform = "x64" if sys.maxsize > 2 ** 32 else "Win32"
cmake_config_args += ["-A", platform]
cmake_build_args += ["--config", cfg]
cmake_config_args += shlex.split(os.environ.get("ALE_PY_CMAKE_ARGS", ""))
cmake_build_args += shlex.split(os.environ.get("ALE_PY_BUILD_ARGS", ""))
os.makedirs(build_temp, exist_ok=True)
os.makedirs(extdir, exist_ok=True)
subprocess.check_call(
["cmake", "-S", ext.sourcedir, "-B", build_temp] + cmake_config_args
)
subprocess.check_call(
["cmake", "--build", build_temp] + cmake_build_args
)
def _read(filename):
with open(os.path.join(os.path.dirname(__file__), filename)) as f:
return f.read()
def _is_valid_semver(version):
"""
Checks if `version` conforms to semver rules.
"""
regex = r"^((([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)$"
return re.match(regex, version)
def _parse_version(filename):
"""
Parse VERSION from `CMakeLists.txt`
args:
filename: should point to the projects CMakeLists.txt
returns:
version:
1) Running locally version will be of the form VERSION.dev
2) Running in CI with a version tag will be of the form TAGGED_VERSION
raises:
RuntimeError:
1) Unable to find ALEVERSION in `filename`
AssertionError:
1) Running in CI and tagged version doesn't match parsed version
2) Tagged version or parsed version doesn't conform to semver rules
"""
# Parse version from file
contents = _read(filename)
version_match = re.search(r"ale.*VERSION\s(\d+[^\n]*)", contents, re.M | re.S)
if not version_match:
raise RuntimeError("Unable to find VERSION in {}".format(filename))
version = version_match.group(1)
version_suffix = ""
assert _is_valid_semver(version), "ALEVERSION {} must conform to semver.".format(
version
)
# If the git ref is a tag verify the tag and don't use a suffix
ref = "GITHUB_REF"
tag_regex = r"refs\/tags\/v(.*)$"
if os.environ.get(ref, False) and re.match(tag_regex, os.environ.get(ref)):
version_match = re.search(tag_regex, os.environ.get(ref))
version_tag = version_match.group(1)
assert _is_valid_semver(
version_tag
), "Tag is invalid semver. {} must conform to semver.".format(version_tag)
assert (
version_tag == version
), "Tagged version must match VERSION but got:\n\tVERSION: {}\n\tTAG: {}".format(
version, version_tag
)
version_suffix = ""
return version + version_suffix
setup(
name="multi-agent-ale-py",
version=_parse_version("CMakeLists.txt"),
description="Multi-Agent Arcade Learning Environment Python Interface",
long_description=_read("README.md"),
long_description_content_type="text/markdown",
keywords=["reinforcement-learning", "arcade-learning-environment", "atari"],
url="https://github.com/Farama-Foundation/Multi-Agent-ALE",
author="Farama Foundation",
author_email="jkterry@farama.org",
license="GPL",
ext_modules=[
CMakeExtension(
"multi_agent_ale_py.ale_c",
".",
[
"-DUSE_SDL=OFF",
"-DUSE_RLGLUE=OFF",
"-DBUILD_EXAMPLES=OFF",
"-DBUILD_CPP_LIB=OFF",
"-DBUILD_CLI=OFF",
"-DBUILD_C_LIB=ON",
],
)
],
cmdclass={"build_ext": CMakeBuild},
packages=["multi_agent_ale_py"],
install_requires=[
"numpy"
],
python_requires=">=3.9",
include_package_data=True,
classifiers=[
"Development Status :: 5 - Production/Stable",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
"Intended Audience :: Science/Research",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: 3.14",
],
)