-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
164 lines (140 loc) · 4.97 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
#!/usr/bin/env python3
import os
import subprocess
import sys
from distutils.cmd import Command
from distutils.log import INFO, ERROR
from distutils.spawn import find_executable
from setuptools import setup
from setuptools.extension import Extension
class BuildCoreCommand(Command):
"""Command to build the livesplit-core submodule."""
description = "build livesplit-core submodule"
user_options = [
("cargo-path=", None, "path to cargo")
]
PATH_NOT_FOUND = "cargo not found"
NOT_EXECUTABLE = "cargo found on path is not executable"
CORE_NOT_FOUND = ("livesplit-core not found. Have you initialized its "
"submodule?\n\t(git submodule update --init "
"--recursive)")
SUBMODULE_PATH = os.path.join("extern", "livesplit-core")
MANIFEST_PATH = os.path.join(SUBMODULE_PATH, "Cargo.toml")
RELEASE_PATH = os.path.join(SUBMODULE_PATH, "target", "release")
BINDINGS_PATH = os.path.join(SUBMODULE_PATH, "capi", "bindings")
BUILD_ARGS = [
"build", "--manifest-path", MANIFEST_PATH, "--release", "-p",
"staticlib"
]
RUN_ARGS = [
"run", "--manifest-path", MANIFEST_PATH, "-p", "bindings", "--", "c",
"-o", BINDINGS_PATH
]
def initialize_options(self):
"""Initialize default path."""
self.cargo_path = "cargo"
def finalize_options(self):
"""Confirm cargo executable is available and submodule is loaded."""
path = find_executable(self.cargo_path)
if not path:
raise IOError(self.PATH_NOT_FOUND)
elif not os.access(path, os.X_OK):
raise IOError(self.NOT_EXECUTABLE)
if not os.path.isdir("extern/livesplit-core"):
raise Exception(self.CORE_NOT_FOUND)
def run(self):
"""Run command."""
def cargo_command(args):
command = [self.cargo_path] + args
subprocess.check_call(command)
try:
self.announce("building livesplit-core", level=INFO)
cargo_command(self.BUILD_ARGS)
self.announce("generating c bindings", level=INFO)
cargo_command(self.RUN_ARGS)
except subprocess.CalledProcessError:
self.announce("failed to build livesplit-core!", level=ERROR)
# A representation of livesplit-cy's packages and modules in the following
# format: List[(name: str, sources: List[str])]
PACKAGES = [
("component", []),
("layout", [
"component",
"layout"
]),
("run", [
"run",
"segment"
]),
("time", [
"atomic_date_time",
"time_span",
"time"
]),
("timer", [
"shared_timer",
"timer"
])
]
# A representation of this package's statically linked dependencies in the
# following format: List[(name: str, path: str, include path: str)]
STATIC_LIBS = [
(
"livesplit_core",
BuildCoreCommand.RELEASE_PATH,
BuildCoreCommand.BINDINGS_PATH
)
]
def parse_libs():
"""Parse STATIC_LIBS into kwargs for setuptools Extensions."""
if sys.platform == "win32":
libraries, library_dirs = [], []
include_dirs = ["include"]
for name, ldir, idir in STATIC_LIBS:
libraries.append(name)
library_dirs.append(ldir)
include_dirs.append(idir)
return {
"libraries": libraries,
"library_dirs": library_dirs,
"include_dirs": include_dirs
}
else:
extra_objects = []
include_dirs = ["include"]
for name, ldir, idir in STATIC_LIBS:
extra_objects.append(os.path.join(ldir, "lib{}.a".format(name)))
include_dirs.append(idir)
return {
"extra_objects": extra_objects,
"include_dirs": include_dirs
}
def parse_packages(library_args, cython=False):
"""Parse PACKAGES into setuptools Extensions."""
def parse_one(module):
name, sources = module
fullname = "livesplit_cy." + name
source_dir = "" if cython else "src."
package_path = os.path.join(*(source_dir + fullname).split("."))
ext = ".pyx" if cython else ".c"
return [
Extension(fullname + "." + source,
[os.path.join(package_path, source + ext)],
**library_args)
for source in sources
]
return sum(map(parse_one, filter(None, PACKAGES)), [])
def main():
"""Run cythonized setup if available, else build setup from C sources."""
try:
from Cython.Build import cythonize
libs = parse_libs()
setup(cmdclass={"build_core": BuildCoreCommand},
ext_modules=cythonize(parse_packages(libs, cython=True),
build_dir="src",
include_path=libs["include_dirs"]))
except ImportError:
setup(cmdclass={"build_core": BuildCoreCommand},
ext_modules=parse_packages(parse_libs()))
if __name__ == "__main__":
main()