forked from google-deepmind/open_spiel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
145 lines (122 loc) · 4.84 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
# Copyright 2019 DeepMind Technologies Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""The setup script for setuptools.
See https://setuptools.readthedocs.io/en/latest/setuptools.html
"""
import os
import subprocess
import sys
import setuptools
from setuptools.command.build_ext import build_ext
class CMakeExtension(setuptools.Extension):
"""An extension with no sources.
We do not want distutils to handle any of the compilation (instead we rely
on CMake), so we always pass an empty list to the constructor.
"""
def __init__(self, name, sourcedir=""):
super().__init__(name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
class BuildExt(build_ext):
"""Our custom build_ext command.
Uses CMake to build extensions instead of a bare compiler (e.g. gcc, clang).
"""
def run(self):
self._check_build_environment()
for ext in self.extensions:
self.build_extension(ext)
def _check_build_environment(self):
"""Check for required build tools: CMake, C++ compiler, and python dev."""
try:
subprocess.check_call(["cmake", "--version"])
except OSError as e:
ext_names = ", ".join(e.name for e in self.extensions)
raise RuntimeError(
"CMake must be installed to build" +
f"the following extensions: {ext_names}") from e
print("Found CMake")
cxx = "clang++"
if os.environ.get("CXX") is not None:
cxx = os.environ.get("CXX")
try:
subprocess.check_call([cxx, "--version"])
except OSError as e:
ext_names = ", ".join(e.name for e in self.extensions)
raise RuntimeError(
"A C++ compiler that supports c++17 must be installed to build the "
+ "following extensions: {}".format(ext_names)
+ ". We recommend: Clang version >= 7.0.0."
) from e
print("Found C++ compiler: {}".format(cxx))
def build_extension(self, ext):
extension_dir = os.path.abspath(
os.path.dirname(self.get_ext_fullpath(ext.name)))
cxx = "clang++"
if os.environ.get("CXX") is not None:
cxx = os.environ.get("CXX")
env = os.environ.copy()
cmake_args = [
f"-DPython3_EXECUTABLE={sys.executable}",
f"-DCMAKE_CXX_COMPILER={cxx}",
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extension_dir}",
]
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)
if os.environ.get("OPEN_SPIEL_BUILD_ALL") is not None:
# Build everything (necessary for nox tests)
subprocess.check_call(["make", f"-j{os.cpu_count()}"],
cwd=self.build_temp,
env=env)
else:
# Build only pyspiel (for pip package)
subprocess.check_call(["make", "pyspiel", f"-j{os.cpu_count()}"],
cwd=self.build_temp,
env=env)
def _get_requirements(requirements_file): # pylint: disable=g-doc-args
"""Returns a list of dependencies for setup() from requirements.txt.
Currently a requirements.txt is being used to specify dependencies. In order
to avoid specifying it in two places, we're going to use that file as the
source of truth.
"""
with open(requirements_file) as f:
return [_parse_line(line) for line in f if line]
def _parse_line(s):
"""Parses a line of a requirements.txt file."""
requirement, *_ = s.split("#")
return requirement.strip()
# Get the requirements from file. During nox tests, this is in the current
# directory, but when installing from pip it is in the parent directory
req_file = ""
if os.path.exists("requirements.txt"):
req_file = "requirements.txt"
else:
req_file = "../requirements.txt"
setuptools.setup(
name="open_spiel",
version="1.5",
license="Apache 2.0",
author="The OpenSpiel authors",
author_email="open_spiel@google.com",
description="A Framework for Reinforcement Learning in Games",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
url="https://github.com/deepmind/open_spiel",
install_requires=_get_requirements(req_file),
python_requires=">=3.9",
ext_modules=[CMakeExtension("pyspiel", sourcedir="open_spiel")],
cmdclass={"build_ext": BuildExt},
zip_safe=False,
packages=setuptools.find_packages(include=["open_spiel", "open_spiel.*"]))