-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·32 lines (25 loc) · 925 Bytes
/
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
#!/usr/bin/env python3
import sys
import subprocess
import pathlib
compiler_path = pathlib.Path(__file__).parent.absolute()/'COMPILERBABY'
if __name__ == '__main__':
if len(sys.argv) != 2:
print('usage: build.py <filename>')
sys.exit(-1)
input_file = sys.argv[1]
if not input_file.endswith('.c'):
print('error: filename must end with \'.c\' extension')
asm_file = input_file.replace('.c', '.s')
compile_args = [compiler_path, input_file]
result = subprocess.Popen(compile_args, stdout=subprocess.PIPE)
output, _ = result.communicate()
err = result.returncode
if err != 0:
print("Compilation failed")
sys.exit(-1)
with open(asm_file, 'w+') as out:
out.write(output.decode('utf-8'))
executable = asm_file.replace('.s', '')
assemble_args = ['gcc', '-no-pie', asm_file, '-o', executable]
subprocess.run(assemble_args)