-
Notifications
You must be signed in to change notification settings - Fork 0
/
,compile
executable file
·81 lines (69 loc) · 2.8 KB
/
,compile
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
#!/usr/bin/env python3
"""Compiles and runs a c file, along with additional features"""
import argparse
import subprocess
import sys
from pathlib import Path
from textwrap import dedent
parser = argparse.ArgumentParser()
parser.add_argument('files', nargs='+', help='The C file/files to compile')
parser.add_argument('-s', '--strict', action='store_true', help='Compiles with strict compiler\
flags')
parser.add_argument('-r', '--run', action='store_true', help='Runs the program')
parser.add_argument('-v', '--val', action='store_true', help='Runs the program inside a valgrind\
environment')
parser.add_argument('--opts', nargs='+', type=str, help='Commandline options to add to\
the running program')
parser.add_argument('--dir', help='Runs the program on each file in\
the provided directory')
parser.add_argument('--out', type=str, help='The name of the output file after compilation')
parser.add_argument('-l', '--libs', nargs=1, help='C libraries to link in while compiling')
args = parser.parse_args()
valgrind_flags = 'valgrind --leak-check=full --track-origins=yes --show-leak-kinds=all'
gcc_strict_flags = '-Wall -pedantic -Werror -Wextra -std=gnu89'
# store args
files = ' '.join(args.files)
run = args.run if args.run else None
strict = gcc_strict_flags if args.strict else ''
val = valgrind_flags if args.val else ''
outfile = f'{args.out}' if args.out else "a.out"
cmd_args = ' '.join(args.opts) if args.opts else ''
dir_given = args.dir if args.dir else None
libs = [
f'-l{lib}'
for lib in [
letters for letters in args.libs[0]
]
]
links = ' '.join(libs) if args.libs else ''
def main():
compilation = f"""gcc {strict} {files} {links} -o {outfile}"""
running = f"""{val} ./{outfile} {cmd_args}"""
subprocess.run(compilation, shell=True)
if run or val:
if dir_given:
run_with_files(dir_given)
subprocess.run(running, shell=True)
def run_with_files(dir):
dir = Path(dir)
if ((not Path.exists(dir)) or (not Path.is_dir(dir))):
sys.exit(f"`{dir}` is not a directory, or does not exist")
files = [x for x in Path.iterdir(dir) if x.is_file()]
for file in files:
message = f"Running `{outfile}` on `{file}`"
bordered_message = draw_message_border(message)
print(bordered_message)
subprocess.run(f"""{val} ./{outfile} {file}""", shell=True)
sys.exit()
def draw_message_border(message):
h_pad = 2 # total horizontal padding
message_len = len(message)
h_len = message_len + h_pad
bordered_message = dedent(f"""
╭{'─' * (h_len)}╮
│{' ' * (h_pad // 2)}{message}{' ' * (h_pad // 2)}│
╰{'─' * (h_len)}╯
""").strip()
return bordered_message
if __name__ == '__main__':
main()