-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.py
395 lines (306 loc) · 11.7 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
"""
This file is a part of the Dust Programming Language
project and distributed under the MIT license.
Copyright © Kadir Aksoy
https://github.com/kadir014/Dust
Dust builder script
----------------------------
This Python script is used to build (compile) Dust and
distribute cross-platform packages.
This script must be run in the same directory as Dust.
a) Building Dust
----------------------------
Script assumes MinGW binaries are on PATH as a prerequisite.
Just simply run the script with 'python build.py', (or 'py',
'python3' depending on the platform.)
By default, all cores of the machine are used to reduce compile
time. You can use '-j' flag to adjust number of cores used
manually. For example, 'python build.py -j1' uses only one
core, '-j2' uses 2 core, '-j4' uses 4 core and suchlike.
You can use flags '-O' flag to set compiler optimization.
Compilation time increases as the optimization level go higher.
-O0 - Don't optimize (default)
-O1 - Optimize moderately
-O2 - Optimize more
-O3 - Optimize even more
All files created during building are cleaned afterwards by
default, but one can use `--clean` flag to remove them manually.
b) Distributing Dust
----------------------------
Use '--package' flag to distribute Dust package.
Any dependency will be downloaded on the fly.
Optional flags are:
--package-dist - Just archive Dust
--package-clean - Clean remaining files from previous
packaging (This shouldn't be used
normally because all extra files are
removed by default.)
"""
import os
import sys
import platform
import pathlib
import time
import multiprocessing
import subprocess
import datetime
import threading
GH_REPO = "https://github.com/kadir014/Dust"
CPU_COUNT = multiprocessing.cpu_count()
FINAL_BUILD = "dust.exe" if platform.system() == "Windows" else "dust"
DUST_PATH = pathlib.Path(os.getcwd())
SOURCE_FILES = [
DUST_PATH / "src" / "cli.c",
DUST_PATH / "src" / "ustring.c",
DUST_PATH / "src" / "error.c",
DUST_PATH / "src" / "platform.c",
DUST_PATH / "src" / "io.c",
DUST_PATH / "src" / "tokenizer.c",
DUST_PATH / "src" / "parser.c",
DUST_PATH / "src" / "transpiler.c"
]
INCLUDE_FILES = [
DUST_PATH / "include" / "dust" / "tokenizer.h",
DUST_PATH / "include" / "dust" / "ustring.h",
DUST_PATH / "include" / "dust" / "error.h",
DUST_PATH / "include" / "dust" / "ansi.h",
DUST_PATH / "include" / "dust" / "io.h",
DUST_PATH / "include" / "dust" / "parser.h",
DUST_PATH / "include" / "dust" / "platform.h",
DUST_PATH / "include" / "dust" / "transpiler.h"
]
class ValidityError(Exception): pass
class OptionError(Exception): pass
class Color:
"""
Terminal ANSI color codes
It's easier and simpler to define it in the script
instead of using a dependency
"""
reset = "\033[0m"
bold = "\033[01m"
fgblack = "\033[30m"
fgdarkgray = "\033[90m"
fglightgray = "\033[37m"
fgwhite = "\033[97m"
fgred = "\033[31m"
fgorange = "\033[33m"
fgyellow = "\033[93m"
fggreen = "\033[32m"
fgblue = "\033[34m"
fgcyan = "\033[36m"
fgpurple = "\033[35m"
fgmagenta = "\033[95m"
fglightred = "\033[91m"
fglightgreen = "\033[92m"
fglightblue = "\033[94m"
fglightcyan = "\033[96m"
bgblack = "\033[40m"
bgdarkgray = "\033[100m"
bglightgray = "\033[47m"
bgwhite = "\033[107m"
bgred = "\033[41m"
bgorange = "\033[43m"
bgyellow = "\033[103m"
bggreen = "\033[42m"
bgblue = "\033[44m"
bgcyan = "\033[46m"
bgpurple = "\033[45m"
bgmagenta = "\033[105m"
bglightred = "\033[101m"
bglightgreen = "\033[102m"
bglightblue = "\033[104m"
bglightcyan = "\033[106m"
class LoadingBar(threading.Thread):
"""
Loading bar animation on terminal
It's easier and simpler to implement it in the script
instead of using a dependency
"""
def __init__(self):
super().__init__()
self.running = False
self.done = False
self.last_time = time.time()
self.frames = ("[/]", "[-]", "[\\]", "[|]")
self.frame = 0
def stop(self):
self.running = False
def run(self):
self.running = True
self.done = False
sys.stdout.write(" " * 16)
sys.stdout.flush()
sys.stdout.write("\b" * 16)
while self.running:
time.sleep(0.001)
if (time.time() - self.last_time > 0.1):
self.last_time = time.time()
self.frame += 1
if self.frame > len(self.frames)-1:
self.frame = 0
sys.stdout.write(f"Compiling... {self.frames[self.frame]}")
sys.stdout.flush()
sys.stdout.write("\b" * 16)
sys.stdout.write("\b" * 16)
self.done = True
def remove_object_files():
"""
Remove all object (.o) files from the previous compilation
"""
for file in os.listdir(os.getcwd()):
if file.endswith(".o"):
os.remove(file)
class OptionHandler:
"""
Organizes command line arguments and compiler options to simplify building
"""
def __init__(self):
self.cores = CPU_COUNT # -jx
self.optimization = 0 # -Ox
self.clean = False # --clean
self.optimization_map = {
0: "Don't optimize",
1: "Optimize moderately",
2: "Optimize more",
3: "Optimize even more"
}
self.package = False # --package
self.package_dist = False # --package-dist
self.package_clean = False
self.gcc_args = [] # GCC arguments
self.resources = [] # Resource paths
if platform.system() == "Windows":
self.resources.append("dust-res.res")
self.gcc_args.append("-lws2_32")
elif platform.system() == "Linux":
self.gcc_args.append("-lm")
elif platform.system() == "Darwin":
self.gcc_args.append("-framework CoreServices")
def add_option(self, opt: str):
if opt.startswith("-j"):
try:
self.cores = int(opt[2:])
except ValueError:
raise OptionError("integer expected after -j flag (-j0, -j4, etc..)")
elif opt.startswith("-O"):
try:
self.optimization = int(opt[2:])
if not (0 <= self.optimization <= 3):
raise OptionError("invalid optimization level")
except ValueError:
raise OptionError("integer expected after -O flag (-O0, -O2, etc..)")
elif opt.startswith("--clean"):
self.clean = True
elif opt.startswith("--package"):
self.package = True
if opt == "--package-dist":
self.package_dist = True
elif opt == "--package-clean":
self.package_clean = True
else:
raise OptionError(f"unknown option '{opt}'")
def get_gcc_argstr(self):
argstr = f" {' '.join(self.gcc_args)} -O{self.optimization}"
return argstr
class Compiler:
"""
Compiles Dust source code
"""
def __init__(self, option_handler: OptionHandler):
self.option_handler = option_handler
if option_handler.cores == 1:
self.targets = [str(source) for source in SOURCE_FILES]
else:
self.targets = [list() for _ in range(option_handler.cores)]
i = 0
for source in SOURCE_FILES:
self.targets[i].append(str(source))
i += 1
if i > len(self.targets)-1: i = 0
def _compile_1proc(self):
"""
Compiles on single process
"""
if platform.system() == "Windows":
os.system("windres assets/dust.rc -O coff -o dust-res.res")
start_time = time.perf_counter()
os.system(f"gcc -o dust {' '.join(self.targets)} {' '.join(self.option_handler.resources)} -I./include/ {self.option_handler.get_gcc_argstr()}")
end_time = time.perf_counter() - start_time
if os.path.exists("dust-res.res"): os.remove("dust-res.res")
return end_time
def _compile_multiproc(self):
"""
Compiles on multiple processes
"""
remove_object_files()
if platform.system() == "Windows":
os.system("windres assets/dust.rc -O coff dust-res.res")
subprocs = []
start_time = time.perf_counter()
for sources in self.targets:
subprocs.append(subprocess.Popen(("gcc", "-c", *sources, "-I./include/")))
for s in subprocs:
s.communicate()
# Link all object files to finish compiling
os.system(f"gcc -o dust cli.o ustring.o error.o platform.o tokenizer.o parser.o transpiler.o {' '.join(self.option_handler.resources)} {self.option_handler.get_gcc_argstr()}")
end_time = time.perf_counter() - start_time
remove_object_files()
if os.path.exists("dust-res.res"): os.remove("dust-res.res")
return end_time
def compile(self):
"""
Compile Dust
"""
if os.path.exists(FINAL_BUILD): os.remove(FINAL_BUILD)
if self.option_handler.cores == 1:
return self._compile_1proc()
else:
return self._compile_multiproc()
if __name__ == "__main__":
if platform.system() == "Windows":
os.system(" ")
option_handler = OptionHandler()
for arg in sys.argv[1:]:
option_handler.add_option(arg)
# Clean build remainings
if option_handler.clean:
remove_object_files()
print("Succesfully cleaned building remaining files.")
sys.exit(0)
# Clean packaging remainings
if option_handler.package_clean:
print("Succesfully cleaned packaging remaining files.")
sys.exit(0)
# Package Dust
# TODO
if option_handler.package:
pass
# Build Dust
else:
print("Welcome to the Dust builder\n" + \
"\n" + \
"Configured settings\n" + \
f" - Compiler process(es): {Color.fgyellow}{option_handler.cores}{Color.reset}\n" + \
f" - Optimization level : {Color.fgyellow}{option_handler.optimization}{Color.reset} " + \
f"({option_handler.optimization_map[option_handler.optimization]})\n")
if option_handler.cores > CPU_COUNT:
print(f"{Color.fglightred}[WARNING]{Color.reset} given process count ({option_handler.cores})" + \
f" exceeds your machine's core count ({CPU_COUNT})\n")
print("Starting building Dust")
if platform.system() == "Windows":
system = f"Windows {platform.release()} ({platform.win32_ver()[1]}, {platform.win32_ver()[2]})"
else:
system = f"{platform.system()} {platform.release()} ({platform.version()})"
machine = ("32-bit", "64-bit")[platform.machine().endswith("64")]
print(f"{Color.fglightblue}[INFO]{Color.reset} Start time: {datetime.datetime.now().strftime('%H:%M:%S %d.%m.%Y')}")
print(f"{Color.fglightblue}[INFO]{Color.reset} Platform: {system}")
print(f"{Color.fglightblue}[INFO]{Color.reset} Machine: {machine}\n")
loader = LoadingBar()
loader.start()
compiler = Compiler(option_handler)
end_time = compiler.compile()
loader.stop()
while not loader.done: pass
print(f"{Color.fglightgreen}[DONE]{Color.reset} Dust succesfully built in {round(end_time, 1)} secs " + \
f"({int(end_time*1000)} ms)")