-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuilder
executable file
·331 lines (255 loc) · 10.8 KB
/
builder
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/bin/python
from abc import ABC, abstractmethod
import os
import subprocess
import argparse
import json
import shutil
import glob
import signal
# Modify this if you need
# defaults will be enough for most people
BUILD_DIRECTORY = "build"
EXECUTABLE_OUTPUT = "output"
# builder.py
# Copyright 2020 Firstbober
#
# Permission to use, copy, modify,
# and/or distribute this software for any purpose with or without fee is hereby granted,
# provided that the above copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
__version__ = "1.6.0"
class BuildBackend(ABC):
TriggerFile = ""
def build(self, opts):
pass
def run(self, opts, args):
pass
def valgrind(self, opts, args):
pass
def test(self, opts):
pass
def clean(self):
pass
class Meson(BuildBackend):
TriggerFile = "meson.build"
def build(self, opts):
build_type = "plain"
if len(opts) > 0:
if opts[0] == "build_release":
build_type = "release"
elif opts[0] == "build_debug":
build_type = "debug"
elif opts[0] == "build_plain":
build_type = "plain"
if opts[0] != "build_force" and len(opts) > 1:
if opts[1] == "build_force":
self.clean()
elif opts[0] == "build_force":
self.clean()
meson_out = os.popen("meson introspect " +
BUILD_DIRECTORY + " --buildoptions").read()
if meson_out.startswith("Cu"):
os.system("meson setup " + BUILD_DIRECTORY + " . --buildtype " +
build_type + " --prefix " + os.getcwd() + "/" + EXECUTABLE_OUTPUT + "/" + build_type)
if meson_out.startswith("["):
meson_build_options = json.loads(meson_out)
for option in meson_build_options:
if option["name"] == "buildtype":
if option["value"] != build_type:
os.system("meson configure " + BUILD_DIRECTORY + " --buildtype " +
build_type + " --prefix " + os.getcwd() + "/" + EXECUTABLE_OUTPUT + "/" + build_type)
CURRENT_PROC = subprocess.Popen(["meson", "compile", "-C", BUILD_DIRECTORY])
out, err = CURRENT_PROC.communicate()
if CURRENT_PROC.returncode != 0:
return False
os.system("meson install --quiet -C " + BUILD_DIRECTORY)
return True
def run(self, opts, args):
meson_out = os.popen("meson introspect " +
BUILD_DIRECTORY + " --targets").read()
requested_binary_type = ""
no_call = False
if len(opts) > 0:
if opts[0] == "build_release":
requested_binary_type = "release"
elif opts[0] == "build_debug":
requested_binary_type = "debug"
elif opts[0] == "build_plain":
requested_binary_type = "plain"
for opt in opts:
if opt == "__no_call":
no_call = True
if meson_out.startswith("["):
meson_targets = json.loads(meson_out)
for target in meson_targets:
if target["type"] == "executable":
if requested_binary_type == "":
exec_path = [target["install_filename"][0]]
if len(args) > 0:
exec_path += args
if no_call:
return [exec_path, '/'.join(target["install_filename"][0].split("/")[:-1])]
else:
os.chdir('/'.join(target["install_filename"][0].split("/")[:-1]))
os.system(" ".join(exec_path) + ">&2")
else:
exec_path = [os.getcwd() + "/" + EXECUTABLE_OUTPUT + "/" +
requested_binary_type + "/bin/" +
target["install_filename"][0].split("/")[-1]]
if len(args) > 0:
exec_path += args
if no_call:
return [exec_path, os.getcwd() + "/" + EXECUTABLE_OUTPUT + "/" +
requested_binary_type + "/bin/"]
else:
os.chdir(os.getcwd() + "/" + EXECUTABLE_OUTPUT + "/" + requested_binary_type + "/bin/")
os.system(" ".join(exec_path) + ">&2")
else:
print("Project is not compiled yet!")
pass
def valgrind(self, opts, args):
opts.append("__no_call")
exec_info = self.run(opts, args)
os.chdir(exec_info[1])
os.system("valgrind ./" + exec_info[0][0].split("/")[-1] + ">&2")
pass
def test(self, opts):
os.system("meson test -C " + BUILD_DIRECTORY)
pass
def clean(self):
if os.path.exists(BUILD_DIRECTORY) and os.path.isdir(BUILD_DIRECTORY):
shutil.rmtree(BUILD_DIRECTORY)
print("Removed build directory")
_build_backend_registry = [
Meson()
]
def find_build_backend():
for entry in os.scandir("."):
for build_backend in _build_backend_registry:
if entry.name == build_backend.TriggerFile:
return build_backend
return False
def format_files(directory, extensions):
files = []
for extension in extensions:
for name in glob.glob(directory + "/**/*." + extension, recursive=True):
files.append(name)
os.system("clang-format -i " + ' '.join(files))
pass
def keyboard_interrupt_handler(sig, frame):
exit(0)
RETURN_CODES = [
"0",
"1 (SIGHUP) [Hangup detected on controlling terminal or death of controlling process]",
"2 (SIGINT) [Interrupt from keyboard]",
"3 (SIGQUIT)[Quit from keyboard]",
"4 (SIGILL) [Illegal Instruction]",
"5 (SIGTRAP)[]",
"6 (SIGABRT)[Abort signal from abort(3)]",
"7 (SIGBUS) []",
"8 (SIGFPE) [Floating point exception]",
"9 (SIGKILL)[Kill signal]",
"10 (SIGUSR1) []",
"11 (SIGEGV) [Invalid memory reference]",
"12 (SIGUSR2) []",
"13 (SIGPIPE) [Broken pipe: write to pipe with no readers]",
"14 (SIGALRM) [Timer signal from alarm(2)]",
"15 (SIGTERM) [Termination signal]",
]
if __name__ == "__main__":
signal.signal(signal.SIGINT, keyboard_interrupt_handler)
build_backend = find_build_backend()
if build_backend == False:
print("Cannot find supported build backend!")
parser = argparse.ArgumentParser(
description="builder, version " + __version__)
parser.add_argument(
"-b", "--build", help="build entire source code", action="store_true")
parser.add_argument(
"-r", "--run", help="run compiled executable", action="store_true")
parser.add_argument(
"-t", "--test", help="execute project tests", action="store_true")
parser.add_argument(
"-vg", "--valgrind", help="run valgrind on project executable", action="store_true")
parser.add_argument(
"-c", "--clean", help="clean project build files and generated documentation", action="store_true")
parser.add_argument(
"-f", "--format", help="format all project source code using 'clang-format'", action="store_true")
parser.add_argument(
"-br", "--buildrun", help="build and run", action="store_true")
parser.add_argument(
"-bv", "--buildvalgrind", help="build project and run valgrind on generated executable", action="store_true")
parser.add_argument(
"--release", help="release switch for building", action="store_true")
parser.add_argument(
"--debug", help="debug switch for building", action="store_true")
parser.add_argument(
"--plain", help="plain switch for building", action="store_true")
parser.add_argument(
"--force", help="force rebuilding", action="store_true")
parser.add_argument(
"--args", help="specify args for executable to be passed", nargs=argparse.REMAINDER)
parser.add_argument(
"--directory", help="directory to format by clang-format")
parser.add_argument(
"--extensions", help="list of extensions to be formatted")
parser.add_argument('-v', "--version",
action='version', version=__version__)
parsed_opts = parser.parse_args()
if (parsed_opts.build == False
and parsed_opts.buildrun == False
and parsed_opts.buildvalgrind == False
and parsed_opts.clean == False
and parsed_opts.format == False
and parsed_opts.run == False
and parsed_opts.test == False
and parsed_opts.valgrind == False):
print("Use --help to see available actions")
exit(0)
build_system_opts = []
addional_args = []
if parsed_opts.release == True:
build_system_opts.append("build_release")
elif parsed_opts.debug == True:
build_system_opts.append("build_debug")
elif parsed_opts.plain == True:
build_system_opts.append("build_plain")
if parsed_opts.force == True:
build_system_opts.append("build_force")
if parsed_opts.args != None:
addional_args = parsed_opts.args
if parsed_opts.build == True:
build_backend.build(build_system_opts)
elif parsed_opts.run == True:
build_backend.run(build_system_opts, addional_args)
elif parsed_opts.test == True:
build_backend.test(build_system_opts)
elif parsed_opts.valgrind == True:
build_backend.valgrind(build_system_opts, addional_args)
elif parsed_opts.clean == True:
build_backend.clean()
elif parsed_opts.format == True:
directory = "."
extensions = []
if parsed_opts.directory:
directory = parsed_opts.directory
if parsed_opts.extensions:
extensions = parsed_opts.extensions.split(",")
else:
extensions = ["cc", "hh", "cpp", "hpp", "c", "h"]
format_files(directory, extensions)
elif parsed_opts.buildrun == True:
if build_backend.build(build_system_opts):
build_backend.run(build_system_opts, addional_args)
elif parsed_opts.buildvalgrind == True:
if build_backend.build(build_system_opts):
build_backend.valgrind(build_system_opts, addional_args)