forked from ClangBuiltLinux/tc-build
-
Notifications
You must be signed in to change notification settings - Fork 4
/
build-binutils.py
executable file
·275 lines (239 loc) · 9.9 KB
/
build-binutils.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python3
# Description: Builds a standalone copy of binutils
import argparse
import multiprocessing
import os
import pathlib
import platform
import shutil
import subprocess
import utils
def host_arch_target():
"""
Converts the host architecture to the first part of a target triple
:return: Target host
"""
host_mapping = {
"armv7l": "arm",
"ppc64": "powerpc64",
"ppc64le": "powerpc64le",
"ppc": "powerpc"
}
machine = platform.machine()
return host_mapping.get(machine, machine)
def target_arch(target):
"""
Returns the architecture from a target triple
:param target: Triple to deduce architecture from
:return: Architecture associated with given triple
"""
return target.split("-")[0]
def host_is_target(target):
"""
Checks if the current target triple the same as the host.
:param target: Triple to match host architecture against
:return: True if host and target are same, False otherwise
"""
return host_arch_target() == target_arch(target)
def parse_parameters(root_folder):
"""
Parses parameters passed to the script into options
:param root_folder: The directory where the script is being invoked from
:return: A 'Namespace' object with all the options parsed from supplied parameters
"""
parser = argparse.ArgumentParser()
parser.add_argument("-B",
"--build-folder",
help="""
By default, the script will create a "build" folder in the same folder as this script,
then a "binutils" folder within that one and build the files there. If you would like
that done somewhere else, pass it to this parameter. This can either be an absolute
or relative path.
""",
type=str,
default=os.path.join(root_folder.as_posix(), "build",
"binutils"))
parser.add_argument("-I",
"--install-folder",
help="""
By default, the script will create an "install" folder in the same folder as this script
and install binutils there. If you'd like to have it installed somewhere else, pass
it to this parameter. This can either be an absolute or relative path.
""",
type=str,
default=os.path.join(root_folder.as_posix(),
"install"))
parser.add_argument("-t",
"--targets",
help="""
The script can build binutils targeting arm-linux-gnueabi, aarch64-linux-gnu,
mipsel-linux-gnu, powerpc-linux-gnu, powerpc64-linux-gnu, powerpc64le-linux-gnu,
riscv64-linux-gnu, s390x-linux-gnu, and x86_64-linux-gnu.
You can either pass the full target or just the first part (arm, aarch64, x86_64, etc)
or all if you want to build all targets (which is the default). It will only add the
target prefix if it is not for the host architecture.
""",
nargs="+")
parser.add_argument("-m",
"--march",
metavar="ARCH",
help="""
Add -march=ARCH and -mtune=ARCH to CFLAGS to optimize the toolchain for the target
host processor.
""",
type=str)
parser.add_argument("-u",
"--update",
help="""
Update the binutils repos before building.
""",
action="store_true")
return parser.parse_args()
def create_targets(targets):
"""
Generate a list of targets that can be passed to the binutils compile function
:param targets: A list of targets to convert to binutils target triples
:return: A list of target triples
"""
targets_dict = {
"arm": "arm-linux-gnueabi",
"aarch64": "aarch64-linux-gnu",
"mips": "mips-linux-gnu",
"mipsel": "mipsel-linux-gnu",
"powerpc64": "powerpc64-linux-gnu",
"powerpc64le": "powerpc64le-linux-gnu",
"powerpc": "powerpc-linux-gnu",
"riscv64": "riscv64-linux-gnu",
"s390x": "s390x-linux-gnu",
"x86_64": "x86_64-linux-gnu"
}
targets_set = set()
for target in targets:
if target == "all":
return list(targets_dict.values())
elif target == "host":
key = host_arch_target()
else:
key = target_arch(target)
targets_set.add(targets_dict[key])
return list(targets_set)
def cleanup(build_folder):
"""
Cleanup the build directory
:param build_folder: Build directory
"""
if build_folder.is_dir():
shutil.rmtree(build_folder.as_posix())
build_folder.mkdir(parents=True, exist_ok=True)
def invoke_configure(build_folder, install_folder, root_folder, target,
host_arch):
"""
Invokes the configure script to generate a Makefile
:param build_folder: Build directory
:param install_folder: Directory to install binutils to
:param root_folder: Working directory
:param target: Target to compile for
:param host_arch: Host architecture to optimize for
"""
configure = [
root_folder.joinpath("binutils", "configure").as_posix(),
'--enable-plugins',
'--enable-threads',
'--enable-deterministic-archives',
'--enable-new-dtags'
'--prefix=%s' % install_folder.as_posix(),
'--enable-gold',
'--disable-compressed-debug-sections',
'--with-system-zlib',
'--disable-werror',
'--enable-ld=default',
]
if host_arch:
configure += [
'CFLAGS=-O3 -march=%s -mtune=%s' % (host_arch, host_arch),
'CXXFLAGS=-O3 -march=%s -mtune=%s' % (host_arch, host_arch)
]
else:
configure += ['CFLAGS=-O3', 'CXXFLAGS=-O3']
configure_arch_flags = {
"arm-linux-gnueabi": [''],
"mips-linux-gnu": [
'--enable-targets=mips64-linux-gnuabi64,mips64-linux-gnuabin32',
'--enable-shared'
],
"mipsel-linux-gnu": [
'--enable-targets=mips64el-linux-gnuabi64,mips64el-linux-gnuabin32',
'--enable-shared'
],
"powerpc-linux-gnu":
['--enable-targets=powerpc64-linux-gnu', '--enable-shared'],
"riscv64-linux-gnu": ['--enable-shared'],
"s390x-linux-gnu":
['--enable-targets=s390-linux-gnu', '--enable-shared'],
"x86_64-linux-gnu":
['--enable-targets=x86_64-linux-gnux32,x86_64-pep', '--enable-shared']
}
configure_arch_flags['aarch64-linux-gnu'] = configure_arch_flags[
'arm-linux-gnueabi'] + ['--enable-targets=aarch64_be-linux-gnu']
configure_arch_flags['powerpc64-linux-gnu'] = configure_arch_flags[
'powerpc-linux-gnu'] + ['--enable-targets=powerpc-linux-gnu']
configure_arch_flags['powerpc64le-linux-gnu'] = configure_arch_flags[
'powerpc-linux-gnu'] + ['--enable-targets=powerpc-linux-gnu']
configure += configure_arch_flags.get(target, [])
# If the current machine is not the target, add the prefix to indicate
# that it is a cross compiler
if not host_is_target(target):
configure += ['--program-prefix=%s-' % target, '--target=%s' % target]
utils.print_header("Building %s binutils" % target)
subprocess.run(configure, check=True, cwd=build_folder.as_posix())
def invoke_make(build_folder, install_folder, target):
"""
Invoke make to compile binutils
:param build_folder: Build directory
:param install_folder: Directory to install binutils to
:param target: Target to compile for
"""
make = ['make', '-s', '-j' + str(multiprocessing.cpu_count()), 'V=0']
if host_is_target(target):
subprocess.run(make + ['configure-host'],
check=True,
cwd=build_folder.as_posix())
subprocess.run(make, check=True, cwd=build_folder.as_posix())
subprocess.run(make + ['prefix=%s' % install_folder.as_posix(), 'install'],
check=True,
cwd=build_folder.as_posix())
with install_folder.joinpath(".gitignore").open("w") as gitignore:
gitignore.write("*")
def build_targets(build, install_folder, root_folder, targets, host_arch):
"""
Builds binutils for all specified targets
:param build: Build directory
:param install_folder: Directory to install binutils to
:param root_folder: Working directory
:param targets: Targets to compile binutils for
:param host_arch: Host architecture to optimize for
:return:
"""
for target in targets:
build_folder = build.joinpath(target)
cleanup(build_folder)
invoke_configure(build_folder, install_folder, root_folder, target,
host_arch)
invoke_make(build_folder, install_folder, target)
def main():
root_folder = pathlib.Path(__file__).resolve().parent
args = parse_parameters(root_folder)
build_folder = pathlib.Path(args.build_folder)
if not build_folder.is_absolute():
build_folder = root_folder.joinpath(build_folder)
install_folder = pathlib.Path(args.install_folder)
if not install_folder.is_absolute():
install_folder = root_folder.joinpath(install_folder)
targets = ["all"]
if args.targets is not None:
targets = args.targets
utils.fetch_binutils(root_folder, args.update)
build_targets(build_folder, install_folder, root_folder,
create_targets(targets), args.march)
if __name__ == '__main__':
main()