-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
82 lines (68 loc) · 2.7 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
# Copyright 2020 Zhou Zikang. All rights reserved.
#
# 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.
import os
import subprocess
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):
try:
subprocess.check_call(["cmake", "--version"])
except OSError:
raise RuntimeError(
"CMake must be installed to build the following extensions: " +
", ".join(e.name for e in self.extensions))
for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext):
extension_dir = os.path.abspath(
os.path.dirname(self.get_ext_fullpath(ext.name)))
cmake_args = [
"-DPython_TARGET_VERSION=3.7",
"-DCMAKE_CXX_COMPILER=clang++",
"-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 = os.environ.copy()
subprocess.check_call(["make", "-j" + str(os.cpu_count())],
cwd=self.build_temp,
env=env)
setuptools.setup(
name="pynim",
version="0.0.1",
license="Apache 2.0",
author="ZHOU Zikang",
author_email="[email protected]",
description="A Framework for Reinforcement Learning in Game of Nim",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
url="https://github.com/ZikangZhou/NimRL",
ext_modules=[CMakeExtension("pynim", sourcedir="nim_rl")],
cmdclass=dict(build_ext=BuildExt),
zip_safe=False,
)