-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMini-Risc-V-gcc.py
More file actions
executable file
·114 lines (86 loc) · 2.51 KB
/
Mini-Risc-V-gcc.py
File metadata and controls
executable file
·114 lines (86 loc) · 2.51 KB
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
#!/usr/bin/python3
import gccAssembler as asm
import coe_assembler as coe
import gccPrep as prep
import linker
# import sys
import argparse
import re
import os
def makePath(filename):
path = ""
splt = filename.split('/')
f = splt[-1]
if (len(splt) > 1):
for i in range(len(splt) - 1):
path += splt[i] + '/'
return path, f
parser = argparse.ArgumentParser()
parser.add_argument('files', nargs='+', help='File Names (Takes .c and .s')
parser.add_argument('-c', '--coe', action='store_true', help='generate .coe file instead of .hex file')
parser.add_argument('-s', '--save_temps', action='store_true', help='save intermediate .s and .o files')
parser.add_argument('-o', '--output', type=str, default='a.hex')
args = parser.parse_args()
# if (len(sys.argv) == 1):
# print("USAGE: ./Mini-Risc-V-gcc <primary c file> <other optional c files> <optional assembly files>")
# exit()
# path, f = makePath(args.files[0])
# print(path)
# print(f)
paths = list()
files = list()
for f in args.files:
ptmp, ftmp = makePath(f)
paths.append(ptmp)
files.append(ftmp)
# primaryfile = args.files[0]
# primaryfilebase = re.split('\.', primaryfile)
# primaryfile = files[0]
# primaryfilebase = re.split('\.', primaryfile)
# primarypath = path[0]
# cmd = 'riscv32-unknown-elf-gcc -Ilib/ -S '
if (args.output == 'a.hex'):
outpath = paths[0]
outfile = files[0].split('.')[0]
if (args.coe):
outfile += '.coe'
else:
outfile += '.hex'
else:
outpath, outfile = makePath(args.output)
cmd1 = 'riscv32-unknown-elf-gcc -S '
cmd2 = 'riscv32-unknown-elf-as -o '
sfiles = list()
# ofiles = list()
# objname = args.output.split('.')[0] + '.o'
objname = outpath + outfile.split('.')[0] + '.o'
# print(len(sys.argv))
for i in range(len(files)):
f = files[i]
p = paths[i]
fsplit = f.split('.')
sfiles.append(outpath + fsplit[0] + '.s')
if (fsplit[1] == 'c'):
os.system(cmd1 + p + f + ' -o ' + outpath + fsplit[0] + '.s')
# for i in range(0, len(args.files)):
# # print(sys.argv[i])
# fsplit = re.split('\.', args.files[i])
# sf = fsplit[0] + '.s'
# # of = fsplit[0] + '.o'
# sfiles.append(sf)
# # ofiles.append(of)
# if (fsplit[1] == 'c'):
# # print('adding ' + sys.argv[i])
# cmd1 = cmd1 + args.files[i] + ' '
# # cmd = cmd + filenames
# os.system(cmd1)
cmd2 = cmd2 + objname + ' '
for i in range(len(sfiles)):
cmd2 = cmd2 + ' ' + sfiles[i]
os.system(cmd2)
# print(os.system('ls'))
linker.ld(objname, args.coe, outfile, outpath)
if not args.save_temps:
os.system('rm ' + objname)
for f in sfiles:
os.system('rm ' + f)