-
Notifications
You must be signed in to change notification settings - Fork 3
/
convert.py
54 lines (41 loc) · 1.83 KB
/
convert.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
import os
def convert_file(in_file_name, out_file_name):
with open(in_file_name, 'rb') as f:
with open(out_file_name, 'w') as f2:
byte = f.read(1)
while byte:
f2.write('0x{:02x}\n'.format(ord(byte)))
byte = f.read(1)
print(' Converted', in_file_name, 'to', out_file_name)
def convert_folder(in_folder, out_folder):
if not os.path.exists(out_folder):
os.makedirs(out_folder)
for file_name in os.listdir(in_folder):
if os.path.isfile(os.path.join(in_folder, file_name)):
convert_file(os.path.join(in_folder, file_name), os.path.join(out_folder, file_name))
def convert_file_inline(in_file_name, out_file_name):
mydata = []
with open(in_file_name, 'rb') as f:
byte = f.read(1)
while byte:
mydata.append('0x{:02x},'.format(ord(byte)))
byte = f.read(1)
with open(out_file_name, 'w') as f2:
f2.write("// This file is auto-generated by convert.py. Do not modify.\n");
f2.write("pub const BYTECODE: [u8; " + str(len(mydata)) + "]= [");
for bte in mydata:
f2.write(bte)
f2.write('];\n')
print(' Converted', in_file_name, 'to', out_file_name)
def main():
print("Converting ELF files...")
# convert RISCV CPU compliance checks
convert_folder(os.path.join('riscv_compliance_checks', 'in'), os.path.join('riscv_compliance_checks', 'out'))
# convert rust tests. This one is inline because we want to measure gas usage without being polluted by the file loading
convert_file_inline(
os.path.join('rust_tests','target','riscv32i-unknown-none-elf','release','rust_tests'),
os.path.join('tests','rust_tests_bytecode.cairo')
)
print("ELF conversion done.")
if __name__ == '__main__':
main()