-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconfigure.py
More file actions
executable file
·138 lines (110 loc) · 5.2 KB
/
configure.py
File metadata and controls
executable file
·138 lines (110 loc) · 5.2 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python3
import os
def linker_script(offset):
out = """OUTPUT_ARCH(arm)
MEMORY {{
rom : ORIGIN = 0x{:08X}, LENGTH = 32M
ewram : ORIGIN = 0x02000000, LENGTH = 4M - 4k
}}
SECTIONS {{
.text : {{
FILL (0xABCD)
__text_start = . ;
*(.init)
*(.text)
*(.ctors)
*(.dtors)
*(.rodata)
*(.fini)
*(COMMON)
__text_end = . ;
__bss_start__ = . ;
*(.bss)
__bss_end__ = . ;
_end = __bss_end__ ;
__end__ = __bss_end__ ;
}} >rom = 0xff
}}
""".format(offset)
return out
def target(path):
base, ext = os.path.splitext(path.strip())
# TODO: Ensure target name is unique
target_name = base
if ext in ['.s', '.asm']:
target_name += '.o'
output = '{}: {}\n\t'.format(target_name, path.strip())
output += '$(AS) $(AFLAGS) -c {} -o {}'.format(path.strip(), target_name)
elif ext in ['.c']:
target_name += '.o'
output = '{}: {}\n\t'.format(target_name, path.strip())
output += '$(CC) $(CFLAGS) -c {} -o {}'.format(path, target_name)
elif ext in ['.cc', '.cpp', '.cxx']:
target_name += '.o'
output = '{}: {}\n\t'.format(target_name, path.strip())
output += '$(CXX) $(CXXFLAGS) -c {}'.format(path)
elif ext in ['.csv']:
# String
target_name += '.h'
output = '{}: {}\n\t'.format(target_name, path.strip())
output += 'python3 poke2c.py {}'.format(path)
else:
raise ValueError(path, ext)
output += '\n'
return target_name, output
def makefile(toolchain, rom_path, offset=0x800000):
with open('targets') as file:
lines = [line.strip() for line in file]
targets = dict(target(line) for line in lines)
order = [target(line)[0] for line in lines]
with open('linker.lsc', 'w') as file:
file.write(linker_script(0x8000000 + offset))
with open(rom_path, 'rb') as file:
file.seek(0xAC)
gamecode = file.read(4).decode()
file.seek(0xBC)
version = int(file.read(1)[0])
target_strings = ' '.join(item for item in order if os.path.splitext(item)[1] == '.o')
target_strings_all = ' '.join(order)
output = """
all: {targets_all}
\t$(LD) $(LDFLAGS) -o a.o -T {linker_script} -T {symbols} {targets}
\t$(OBJCOPY) -O binary a.o a.bin
""".format(targets=target_strings, targets_all=target_strings_all, linker_script='linker.lsc', symbols=gamecode + '.sym')
defines = """CC={toolchain[cc]}
CXX={toolchain[cxx]}
AS={toolchain[as]}
LD={toolchain[ld]}
OBJCOPY={toolchain[objcopy]}
OBJDUMP={toolchain[objdump]}
XXD=xxd
IMG=todo
COMPRESS=toolchain/gbalzss
PADBIN=toolchain/padbin
OPTS=-fauto-inc-dec -fcompare-elim -fcprop-registers -fdce -fdefer-pop -fdse -fguess-branch-probability -fif-conversion2 -fif-conversion -fipa-pure-const -fipa-profile -fipa-reference -fmerge-constants -fsplit-wide-types -ftree-bit-ccp -ftree-builtin-call-dce -ftree-ccp -ftree-ch -ftree-copyrename -ftree-dce -ftree-dominator-opts -ftree-dse -ftree-forwprop -ftree-fre -ftree-phiprop -ftree-sra -ftree-pta -ftree-ter -funit-at-a-time -fomit-frame-pointer -fthread-jumps -falign-functions -falign-jumps -falign-loops -falign-labels -fcaller-saves -fcrossjumping -fcse-follow-jumps -fcse-skip-blocks -fdelete-null-pointer-checks -fdevirtualize -fexpensive-optimizations -fgcse -fgcse-lm -finline-small-functions -findirect-inlining -fipa-sra -foptimize-sibling-calls -fpartial-inlining -fpeephole2 -fregmove -freorder-blocks -freorder-functions -frerun-cse-after-loop -fsched-interblock -fsched-spec -fschedule-insns -fschedule-insns2 -fstrict-aliasing -fstrict-overflow -ftree-switch-conversion -ftree-tail-merge -ftree-pre -ftree-vrp -finline-functions -funswitch-loops -fpredictive-commoning -fgcse-after-reload -ftree-slp-vectorize -fvect-cost-model -fipa-cp-clone -ffast-math -fforward-propagate -finline-functions-called-once -fmerge-all-constants -fmodulo-sched -fmodulo-sched-allow-regmoves -fgcse-sm -fgcse-las -funsafe-loop-optimizations -fconserve-stack
DEFINES=-D{gamecode} -DSOFTWARE_VERSION={version}
CFLAGS=-mthumb -mthumb-interwork -mcpu=arm7tdmi $(OPTS) -mlong-calls -march=armv4t -Wall -O3 $(DEFINES)
CXXFLAGS=-mthumb -mthumb-interwork -mcpu=arm7tdmi $(OPTS) -mlong-calls -march=armv4t -Wall -O3 $(DEFINES)
ASFLAGS=-mthumb
LDFLAGS=-z muldefs
""".format(toolchain=toolchain, gamecode=gamecode, version=version)
body = '\n'.join(targets.values())
clean = """
clean:
\trm -rf {targets} a.o a.bin
""".format(targets=target_strings_all)
insert = """
insert:
\tdd conv=notrunc of={rom} if={bin} bs=1 seek={offset}
""".format(offset=offset, rom=rom_path, bin='a.bin')
with open('makefile', 'w') as file:
print(defines + output + body + clean + insert, file=file)
toolchain = {
'cc': 'arm-linux-gnueabi-gcc-4.7',
'cxx': 'arm-linux-gnueabi-gcc-4.7',
'as': 'arm-linux-gnueabi-as',
'ld': 'arm-linux-gnueabi-ld',
'objcopy': 'arm-linux-gnueabi-objcopy',
'objdump': 'arm-linux-gnueabi-objcopy'
}
makefile(toolchain, 'test.gba')