-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautobuild
executable file
·251 lines (200 loc) · 7.7 KB
/
autobuild
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
#!/usr/bin/env python3
import os
import sys
import subprocess
import argparse
import shutil
import time
import threading
from pathlib import Path
DEFAULT_VERBOSE = False
DEFAULT_BUILD_DIR = "build"
DEFAULT_BUILD_TYPE = "Debug"
DEFAULT_TEST_DIR = "test"
DEFAULT_TEST_TYPE = "lua"
LUAJIT_TEST_TYPE = "luajit"
DEFAULT_LIST_SOURCES_DIR = "list-sources"
DEFAULT_LIST_BINS_DIR = "list-bins"
DEFAULT_ASSEMBLER_DIR = "assembler"
DEFAULT_ASSEMBLER = "asm6502.lua"
# Always make sure current working directory is set to root of project
working_directory = os.path.dirname(os.path.realpath(__file__))
os.chdir(working_directory)
# Parse argument flags
argument_parser = argparse.ArgumentParser(
description="Build script for automating the boring parts of CMake"
)
# Add desirable arguments
argument_parser.add_argument(
"-c",
"--clean",
default=False,
action="store_true",
help="Clean build directory. Default is {}, but can be specified with option -d".format(
DEFAULT_BUILD_DIR
),
)
argument_parser.add_argument(
"-b",
"--build",
choices=["release", "Release", "debug", "Debug"],
required=False,
type=str,
default=DEFAULT_BUILD_TYPE,
help="Type of build",
)
argument_parser.add_argument(
"-t",
"--test",
choices=[DEFAULT_TEST_TYPE, LUAJIT_TEST_TYPE],
required=False,
type=str,
help="Type of test",
)
argument_parser.add_argument(
"-d", "--dir", required=False, type=str, help="Name of directory",
)
argument_parser.add_argument(
"-v",
"--verbose",
required=False,
default=False,
action="store_true",
help="Enable verbose output",
)
def print_logo():
project_logo = """
_____ _____ _____
/\ \ /\ \ /\ \
/::\____\ /::\ \ /::\ \
/::::| | /::::\ \ /::::\ \
/:::::| | /::::::\ \ /::::::\ \
/::::::| | /:::/\:::\ \ /:::/\:::\ \
/:::/|::| | /:::/__\:::\ \ /:::/__\:::\ \
/:::/ |::| | /::::\ \:::\ \ \:::\ \:::\ \
/:::/ |::| | _____ /::::::\ \:::\ \ ___\:::\ \:::\ \
/:::/ |::| |/\ \ /:::/\:::\ \:::\ \ /\ \:::\ \:::\ \
/:: / |::| /::\____\/:::/__\:::\ \:::\____\/::\ \:::\ \:::\____
\::/ /|::| /:::/ /\:::\ \:::\ \::/ /\:::\ \:::\ \::/ /
\/____/ |::| /:::/ / \:::\ \:::\ \/____/ \:::\ \:::\ \/____/
|::|/:::/ / \:::\ \:::\ \ \:::\ \:::\ \
|::::::/ / \:::\ \:::\____\ \:::\ \:::\____\
|:::::/ / \:::\ \::/ / \:::\ /:::/ /
|::::/ / \:::\ \/____/ \:::\/:::/ /
/:::/ / \:::\ \ \::::::/ /
/:::/ / \:::\____\ \::::/ /
\::/ / \::/ / \::/ /
\/____/ \/____/ \/____/
_ _ _ _
| | | | | | | |
___| | ___ __ _ __| |_ ___| | ___ __ _ __| |_
/ __| |/ / '__| '__| __| / __| |/ / '__| '__| __|
\__ \ <| | | | | |_ \__ \ <| | | | | |_
|___/_|\_\_| |_| \__| |___/_|\_\_| |_| \__|
"""
print(project_logo)
def files(path):
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
yield file
def create_build_dir(dir=DEFAULT_BUILD_DIR):
# TODO: Switch to subprocess, https://stackoverflow.com/questions/89228/calling-an-external-command-from-python
os.system("cmake -E make_directory {}".format(dir))
def clean_build_dir(dir=DEFAULT_BUILD_DIR):
BUILD_DIR_ABS = os.path.join(working_directory, dir)
if os.path.exists(BUILD_DIR_ABS):
shutil.rmtree(BUILD_DIR_ABS)
def generate_build_system(
type=DEFAULT_BUILD_TYPE, dir=DEFAULT_BUILD_DIR, verbose=DEFAULT_VERBOSE
):
# TODO: Switch to subprocess, https://stackoverflow.com/questions/89228/calling-an-external-command-from-python
build_system_status = os.system(
"""
cd {build_dir} &&
cmake -G Ninja -DCMAKE_BUILD_TYPE={build_type} -DVERBOSE={verbose_output} ../
""".format(
build_dir=dir, build_type=type, verbose_output=verbose
)
)
if build_system_status != 0:
sys.exit(build_system_status)
def build(type=DEFAULT_BUILD_TYPE, dir=DEFAULT_BUILD_DIR, verbose=DEFAULT_VERBOSE):
create_build_dir(dir)
generate_build_system(type, dir, verbose)
cmake_flags = ["-S", ".", "-B", dir]
if verbose:
cmake_flags.append(" --verbose")
try:
subprocess.run(["cmake", *cmake_flags], shell=True)
subprocess.run(["ninja", "-j4"], cwd=f"./{dir}", shell=True)
except:
sys.exit(-1)
LUA_TYPE = DEFAULT_TEST_TYPE
class assembleThread(threading.Thread):
def __init__(self, filepath, bin_file_path, outdir, assembler_path):
threading.Thread.__init__(self)
self.filepath = filepath
self.bin_file_path = bin_file_path
self.outdir = outdir
self.assembler_path = assembler_path
def run(self):
subprocess.run([LUA_TYPE, self.assembler_path, self.filepath])
shutil.move(self.bin_file_path, self.outdir)
# Invoke assembler on all source code files in list_files_directory
def assemble(test_directory):
# Invoke assembler with the target directory being the directory with list files in test_directory
assembler_path = DEFAULT_ASSEMBLER_DIR + os.sep + DEFAULT_ASSEMBLER
list_sources_path = os.path.join(test_directory, DEFAULT_LIST_SOURCES_DIR)
list_bins_path = os.path.join(test_directory, DEFAULT_LIST_BINS_DIR)
list_source_files = [
file for file in files(list_sources_path) if file.endswith(".lst")
]
# Create bin folder if not exist
if not os.path.exists(list_bins_path):
os.makedirs(list_bins_path)
threads = list()
for x, lst in enumerate(list_source_files):
source_file_path = os.path.join(list_sources_path, Path(lst))
filename, _ext = os.path.splitext(
Path(lst)
) # Splits it into 'path/filename + .lst'
bin_file_path = os.path.join(
list_sources_path, filename + ".bin"
) # Adds rootname + .bin
bin_out_path = os.path.join(
list_bins_path, os.path.basename(bin_file_path))
t = assembleThread(
source_file_path, bin_file_path, bin_out_path, assembler_path,
)
threads.append(t)
start_time = time.time()
for index, t in enumerate(threads):
t.start()
for index, t in enumerate(threads):
t.join()
print(
"--- Assembled %d files in %s seconds ---"
% (len(threads), time.time() - start_time)
)
os.chdir(test_directory)
args = argument_parser.parse_args()
BUILD_TYPE = args.build
CUSTOM_DIR = args.dir
CLEAN = args.clean
VERBOSE = args.verbose
TEST = args.test
build_dir = CUSTOM_DIR if CUSTOM_DIR is not None else DEFAULT_BUILD_DIR
if CLEAN:
clean_status = clean_build_dir(build_dir)
if clean_status != 0:
sys.exit(clean_status)
if TEST:
target_dir = CUSTOM_DIR if CUSTOM_DIR is not None else DEFAULT_TEST_DIR
if TEST == DEFAULT_TEST_TYPE or LUAJIT_TEST_TYPE: # lua / luajit
LUA_TYPE = TEST
test_dir = os.path.join(working_directory, target_dir)
assemble(test_dir)
sys.exit(0)
print_logo()
# release -> Release, debug -> Debug
build(type=BUILD_TYPE.capitalize(), dir=build_dir, verbose=VERBOSE)