-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·244 lines (184 loc) · 6.72 KB
/
build.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python3
# --------------------------------------------------------------------
# bake.py
#
# Author: Lain Musgrove (lain.proliant@gmail.com)
# Date: Saturday February 22, 2020
#
# Distributed under terms of the MIT license.
# --------------------------------------------------------------------
import shlex
from pathlib import Path
from xeno.build import Recipe, ValueRecipe, build, default, provide, sh, target, factory
from xeno.shell import check
# -------------------------------------------------------------------
PYPI_USERNAME = "lainproliant"
PYPI_KEY_NAME = "pypi"
# -------------------------------------------------------------------
INCLUDES = [
"-I./include",
"-I./moonlight/include",
"-I./moonlight/deps",
"-I./pybind11/include",
]
LDFLAGS = ("-rdynamic", "-g", "-ldl")
RELEASE_CFLAGS = (*INCLUDES, "--std=c++2a")
TEST_CFLAGS = (
*RELEASE_CFLAGS,
"-DMOONLIGHT_AUTOMATA_DEBUG",
"-DMOONLIGHT_DEBUG",
"-DMOONLIGHT_ENABLE_STACKTRACE",
"-DMOONLIGHT_AUTOMATA_DEBUG",
"-DMOONLIGHT_STACKTRACE_IN_DESCRIPTION",
)
RELEASE_ENV = dict(CC="clang++", CFLAGS=RELEASE_CFLAGS, LDFLAGS=LDFLAGS)
TEST_ENV = dict(CC="clang++", CFLAGS=TEST_CFLAGS, LDFLAGS=LDFLAGS)
# -------------------------------------------------------------------
def compile_app(src, headers, env):
return sh(
"{CC} {CFLAGS} {src} {LDFLAGS} -o {output}",
env=env,
src=src,
output=Path(src).with_suffix(""),
includes=headers,
)
# -------------------------------------------------------------------
@factory
def compile_test(src, headers, env):
return compile_app(src, headers, TEST_ENV)
# -------------------------------------------------------------------
@factory
def compile_demo(src, headers, env):
return compile_app(src, headers, RELEASE_ENV)
# -------------------------------------------------------------------
@factory
def run_test(app):
return sh("{test}", test=app, cwd="test", interactive=True)
# -------------------------------------------------------------------
@factory
def link_pybind11_module(pybind11_module_objects):
return sh(
"{CC} -O3 -shared -Wall -std=c++2a -fPIC {input} -o {output}",
env=RELEASE_ENV,
input=pybind11_module_objects,
output=Path("jotdown%s" % check("python3-config --extension-suffix")),
)
# -------------------------------------------------------------------
class FindLatestTarball(ValueRecipe):
def __init__(self, path):
super().__init__(input=[path])
self.path = path.result
async def compute(self):
tarballs = [*self.path.glob("*.tar.gz")]
return max(tarballs, key=lambda x: x.stat().st_mtime)
# -------------------------------------------------------------------
class GetPassword(ValueRecipe):
def __init__(self, name: str):
super().__init__()
self._name = name
async def compute(self):
return check(f"pass {self._name}")
# -------------------------------------------------------------------
@factory
def compile_pybind11_module_object(src, headers, tests):
return sh(
"{CC} -O3 -shared -Wall -std=c++2a -fPIC {flags} {src} -o {output}",
env=RELEASE_ENV,
src=src,
tests=tests,
output=Path(src).with_suffix(".o"),
flags=INCLUDES + shlex.split(check("python-config --includes")),
requires=headers,
)
# -------------------------------------------------------------------
@provide
def submodules():
return sh("git submodule update --init --recursive")
# -------------------------------------------------------------------
@provide
def headers():
return Path.cwd().glob("include/jotdown/*.h")
# -------------------------------------------------------------------
@provide
async def demo_sources():
return Path.cwd().glob("demo/*.cpp")
# -------------------------------------------------------------------
@provide
def test_sources():
return Path.cwd().glob("test/*.cpp")
# -------------------------------------------------------------------
@target
def demos(demo_sources, headers, submodules):
return Recipe(
[compile_demo(src, headers, RELEASE_ENV) for src in demo_sources],
setup=submodules,
)
# -------------------------------------------------------------------
@target
def tests(test_sources, headers, submodules):
return Recipe(
[compile_test(src, headers, TEST_ENV) for src in test_sources], setup=submodules
)
# -------------------------------------------------------------------
@target
def run_tests(tests):
return [run_test(app) for app in tests]
# -------------------------------------------------------------------
@target
def pybind11_tests(submodules):
return Recipe(
[
sh("mkdir -p {output}", output="pybind11-test-build"),
sh("cmake ../pybind11", cwd=Path("pybind11-test-build")),
sh("make check -j 4", cwd=Path("pybind11-test-build"), interactive=True),
],
synchronous=True,
setup=submodules,
)
# -------------------------------------------------------------------
@provide
def pymodule_sources(submodules):
return Path.cwd().glob("src/*.cpp")
# -------------------------------------------------------------------
@target
def pymodule_objects(pymodule_sources, headers, run_tests):
return [
compile_pybind11_module_object(src, headers, run_tests)
for src in pymodule_sources
]
# -------------------------------------------------------------------
@target
def pymodule_dev(pymodule_objects):
return link_pybind11_module(pymodule_objects)
# -------------------------------------------------------------------
@target
def pymodule_sdist(submodules):
return sh("python3 setup.py sdist", output="dist")
# -------------------------------------------------------------------
@provide
def latest_tarball(pymodule_sdist):
return FindLatestTarball(pymodule_sdist)
# -------------------------------------------------------------------
@provide
def pypi_password():
return GetPassword(PYPI_KEY_NAME)
# -------------------------------------------------------------------
@target
def upload_to_pypi(latest_tarball, pypi_password):
return sh(
"twine upload -u {PYPI_USERNAME} -p {password} {input}",
PYPI_USERNAME=PYPI_USERNAME,
password=pypi_password,
input=latest_tarball,
redacted=["password"],
)
# -------------------------------------------------------------------
@default
def all(demos, pymodule_dev):
return [demos, pymodule_dev]
# -------------------------------------------------------------------
@target
def cc_json():
return sh("intercept-build ./build.py compile\* -R; ./build.py -c compile\*")
# -------------------------------------------------------------------
build()