-
Notifications
You must be signed in to change notification settings - Fork 37
/
setup.py
123 lines (105 loc) · 4.15 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
import os
import platform
import subprocess
from subprocess import check_output
import setuptools
from setuptools import Extension
from setuptools.command.build_ext import build_ext
###
# Build process:
# cmake .
# make -j threads
# move libmagent -> build/
###
def get_version():
"""Gets the magent2 version."""
path = "magent2/__init__.py"
with open(path) as file:
lines = file.readlines()
for line in lines:
if line.startswith("__version__"):
return line.strip().split()[-1].strip().strip('"')
raise RuntimeError("bad version data in __init__.py")
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):
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)
)
cfg = "Debug" if self.debug else "Release"
for ext in self.extensions:
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
cmake_config_args = [
f"-DCMAKE_BUILD_TYPE={cfg}",
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}",
f"-DCMAKE_RUNTIME_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}",
"-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_{}={}".format(
cfg.upper(), self.build_temp
),
]
make_location = os.path.abspath(self.build_temp)
subprocess.check_call(
["cmake", ext.sourcedir] + cmake_config_args, cwd=make_location
)
print(ext.name)
if platform.system() == "Windows":
str(subprocess.check_output(["cd"], shell=True).decode()).strip()
# subprocess.check_call(["dir"], shell=True)
else:
subprocess.check_call(["pwd"])
print(extdir)
subprocess.check_call(["ls"])
# lib_ext = ""
# lib_name = ""
if platform.system() == "Darwin":
# lib_ext = ".dylib"
# lib_name = "libmagent"
thread_num = check_output(["sysctl", "-n", "hw.ncpu"], encoding="utf-8")
subprocess.check_call(
["make", "-C", make_location, "-j", str(thread_num).rstrip()],
cwd=extdir,
)
elif platform.system() == "Linux":
# lib_ext = ".so"
# lib_name = "libmagent"
thread_num = check_output(["nproc"], encoding="utf-8")
subprocess.check_call(
["make", "-C", make_location, "-j", str(thread_num).rstrip()],
cwd=extdir,
)
elif platform.system() == "Windows":
# lib_ext = ".dll"
# lib_name = "magent"
thread_num = 1
# cmake --build . --target ALL_BUILD --config Release
subprocess.check_call(
["cmake", "--build", ".", "--target", "ALL_BUILD", "--config", cfg],
cwd=make_location,
shell=True,
)
# build_res_dir = extdir + "/magent/build/"
# if not os.path.exists(build_res_dir):
# os.makedirs(build_res_dir)
# lib_name = extdir + "/libmagent" + lib_ext
# subprocess.check_call(
# ["mv", lib_name, build_res_dir]
# )
setuptools.setup(
name="magent2",
version=get_version(),
packages=setuptools.find_packages(),
ext_modules=[CMakeExtension("magent2.libmagent", ".", [])],
cmdclass={"build_ext": CMakeBuild},
package_data={"magent2": ["environments/*", "config/*", "scenarios/*"]},
include_package_data=True,
)