-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcross.py
261 lines (229 loc) · 7.99 KB
/
cross.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
## @file
## @brief Cross-Compiler toolchain build from source
from metaL import *
## @defgroup cross
## @ingroup os
## @brief Cross-Compiler toolchain build from source
## @{
## hardware
class HW(Object):
def __format__(self, spec):
assert not spec
return f'{self.val}'
class ARCH(HW):
pass
i386 = ARCH('i386')
class TARGET(HW):
def file(self, depth=0):#, parent=None
return f'{self}'
class CPU(HW):
def __init__(self, V):
super().__init__(V)
self['target'] = self.target = TARGET('i486-elf')
i486dx = CPU('i486dx')
i486dx['arch'] = i486dx.arch = i386
class QEMU(HW):
def __init__(self, V='386', cpu=i486dx):
super().__init__(V)
if V == '386':
self['cpu'] = self.cpu = cpu
self['target'] = self.target = cpu.target
else:
raise Error((V))
def __format__(self, spec):
assert not spec
return f'{self._type()}{self.val}'
qemu386 = QEMU()
## packages list
cclibs = {
'gmp': '6.2.0',
'mpfr': '4.1.0',
'mpc': '1.2.0',
}
gnu = {
'binutils': '2.35.1',
'gdb': '9.2',
'gcc': '9.3.0'
}
pkg = {}
pkg.update(cclibs)
pkg.update(gnu)
## [T]arget [C]ompiler
class tcModule(anyModule):
def init_first(self):
super().init_first()
self['hw'] = self.hw = qemu386
self['cpu'] = self.cpu = i486dx
self['arch'] = self.arch = self.cpu.arch
self['target'] = self.target = self.cpu.target
def init_vscode_settings(self):
super().init_vscode_settings()
self.f12.cmd.val = 'make gmp'
for p in pkg:
t = f'"**/tmp/{p}-*/*":true,'
h = f'"**/tmp/{p}.configure.help":true,'
s = f'"**/src/{p}-*/*":true,'
ts = f'{t:<28}{s}'
self.vscode.watcher // ts
self.vscode.exclude // ts
self.vscode.watcher //\
f'"**/{self.target}/**":true,' //\
f'"**/firmware/**":true,'
self.vscode.settings.sync()
def init_dirs(self):
super().init_dirs()
self.fw = Dir('firmware')
self.diroot // self.fw
self.fw.giti = (File('.gitignore') //
'*.iso' // '*.elf' //
'*.bin' // '*.hex' //
'*.kernel' // '*.rootfs')
self.fw // self.fw.giti
self.fw.giti.sync()
self.tmp.giti // '*.configure.help'
self.tmp.giti.sync()
self.target['dir'] = self.target.dir = Dir(self.target)
self.diroot // self.target.dir
self['root'] = self.root = Dir('root')
self.target.dir // self.root
def init_mk(self):
super().init_mk()
#
self.mk.dirs //\
f'{"GZ":<8} = $(HOME)/gz' //\
f'{"PFX":<8} = $(CWD)/$(TARGET)' //\
f'{"BIN":<8} = $(PFX)/bin' //\
f'{"LIB":<8} = $(PFX)/lib' //\
f'{"ROOT":<8} = $(PFx)/root' //\
f'{"FW":<8} = $(CWD)/firmware'
#
self.mk.module //\
f'{"APP":<8} = {self}' //\
f'{"HW":<8} = {self.hw}' //\
f'{"CPU":<8} = {self.cpu}' //\
f'{"ARCH":<8} = {self.arch}' //\
f'{"TARGET":<8} = {self.target}'
# cross/packages
cross = Section('cross')
packages = Section('packages')
self.mk.version // cross
self.mk.mid // packages
for p in pkg:
pack = f'{p.upper()}_VER'
ver = f'{pkg[p]}'
cross // f'{pack:<12} = {ver}'
packages // f'{p.upper():<8} = {p.lower():>8}-$({p.upper()}_VER)'
pv = f'{p}-*/'
self.tmp.giti // pv
self.src.giti // pv
self.tmp.giti.sync()
self.src.giti.sync()
# #
# self.mk.mid // packs
# for p in pkg:
# packs //
# cfg
cfg = Section('cfg')
self.mk.mid // cfg
cfg //\
f'{"CFG":<12} = configure --prefix=$(PFX)' //\
f'{"CFG_CCLIBS":<12} = --disable-shared ' //\
f'{"CFG_GMP":<12} = $(CFG_CCLIBS)' //\
f'{"CFG_MPFR":<12} = $(CFG_CCLIBS)' //\
f'{"CFG_MPC":<12} = $(CFG_CCLIBS) --with-mpfr=$(PFX)' //\
(S(f'{"CFG_BINUTILS":<12} = \\') //
'--target=$(TARGET) \\' //
'--with-sysroot=$(ROOT) --with-native-system-header-dir=/include \\' //
'--enable-lto --disable-multilib \\' //
'--disable-nls'
) //\
f'{"CFG_GDB":<12} = $(CFG_BINUTILS)' //\
(S(f'{"CFG_GCC":<12} = $(CFG_BINUTILS) \\') //
'--enable-languages="c" \\' //
'--with-gmp=$(PFX) --with-mpfr=$(PFX) --with-mpc=$(PFX)'
) //\
f'{"CFG_GCC0":<12} = $(CFG_GCC) --without-headers --with-newlib' //\
''
# cross
cross = Section('cross') // '.PHONY: cross\ncross: cclibs gnu'
ccl = Section('cclibs') // '.PHONY: cclibs\ncclibs: gmp mpfr mpc' // ''
gnus = Section('gnu') // '.PHONY: gnu\ngnu: binutils gdb gcc0' // ''
self.mk.mid // cross // ccl // gnus
def genpack(p0, l, make='$(XMAKE)', install='$(MAKE) install', clean='rm -rf'):
p = re.sub(r'\d$', r'', p0)
assert p in pkg
s = f'$(SRC)/$({p.upper()})'
t = f'$(TMP)/$({p.upper()})'
return (Section(p, 0) //
f'.PHONY: {p0}' //
f'{p0}: {l}' //
(S(f'{l}:') //
f'$(MAKE) {s}/README' //
f'rm -rf {t} ; mkdir -p {t} ; cd {t} ;\\' //
f'{s}/$(CFG) --help > $(TMP)/{p}.configure.help ;\\' //
f'$(XPATH) {s}/$(CFG) $(CFG_{p0.upper()}) ;\\' //
f'{make} &&\\' //
f'{install} &&\\' //
f'touch $@ && {clean} {s} {t}'
))
## cclibs
for p in cclibs:
ccl // genpack(p, f'$(LIB)/lib{p}.a')
# gnu
gnus //\
genpack('binutils', f'$(BIN)/$(TARGET)-ld') //\
genpack('gdb', f'$(BIN)/$(TARGET)-gdb') //\
genpack('gcc0', f'$(BIN)/$(TARGET)-gcc',
make='$(XMAKE) all-gcc && $(MAKE) install-gcc',
install='$(XMAKE) all-target-libgcc && $(MAKE) install-target-libgcc',
clean='sync')
# gz
gz = Section('gz')
self.mk.mid // gz
for p in pkg:
if p in ['mpc']:
ext = 'gz'
else:
ext = 'xz'
gz // f'{p.upper()+"_GZ":<11} = {"$("+p.upper()+")":>11}.tar.{ext}'
#
makegz = S('\n.PHONY: gz\ngz:', '', 0)
gz // makegz
for p in pkg:
makegz // f' $(GZ)/$({p.upper()}_GZ)'
#
urls = {
'binutils': 'https://ftp.gnu.org/gnu/binutils',
'gcc': 'https://mirror.yandex.ru/mirrors/gnu/gcc/gcc-$(GCC_VER)',
'gdb': 'https://mirror.yandex.ru/mirrors/gnu/gdb',
'gmp': 'https://mirror.yandex.ru/mirrors/gnu/gmp',
'mpfr': 'https://mirror.yandex.ru/mirrors/gnu/mpfr',
'mpc': 'https://mirror.yandex.ru/mirrors/gnu/mpc'
}
for p in pkg:
gz // f'\n$(GZ)/$({p.upper()}_GZ):\n\t$(WGET) -O $@ {urls[p]}/$({p.upper()}_GZ)'
#
rules = Section('rules')
self.mk.mid // rules
arch = {'gz': 'zcat', 'bz2': 'bzcat', 'xz': 'xzcat'}
for xz in arch:
rules // f'$(SRC)/%/README: $(GZ)/%.tar.{xz}\n\tcd $(SRC) && {arch[xz]:>5} $< | tar x && touch $@'
#
self.mk.sync()
def init_apt(self):
super().init_apt()
self.apt // 'build-essential bzip2 xz-utils'
self.apt.sync()
def init_giti(self):
super().init_giti()
self.giti.mid // f'/{self.target}/'
self.giti.sync()
MODULE = tcModule()
MODULE['TITLE'] = TITLE = Title('Cross-Compiler toolchain build from source')
MODULE['ABOUT'] = ABOUT = '''
build custom GCC toolchain
'''
README = README(MODULE)
MODULE['dir'] // README
README.sync()
## @}