-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathToolchain.py
381 lines (335 loc) · 12.1 KB
/
Toolchain.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import logging
import os
import time
import platform
from lib.Command import *
from lib.Version import Version
class CheckError(BaseException):
def __init__(self, arg):
self.args = arg
def __str__(self):
return "".join(self.args)
class Toolchain(object):
def __init__(self, triple, src, jobs, version, release_dir, rebuild, short_dir,
extra_config, toolchain_type, host, build_type, fake):
self.host = host
self.fake = fake
self.top_dir = os.getcwd()
if short_dir:
work_dir = self.gen_short_dir(self.top_dir)
else:
work_dir = "build-{}-{}".format(toolchain_type, triple)
self.mkdir_nofake(work_dir)
work_dir = os.path.join(self.top_dir, work_dir)
self.work_dir = work_dir
self.triple = triple
if not os.path.isabs(src):
src = os.path.join(os.getcwd(), src)
self.src = src
if jobs == -1:
self.jobs = ""
else:
self.jobs = jobs
self.version = version
if release_dir is not None:
if not os.path.isabs(release_dir):
release_dir = os.path.join(os.getcwd(), release_dir)
if not os.path.exists(release_dir):
self.mkdir_nofake(release_dir)
else:
release_dir = work_dir
self.release_dir = release_dir
self.rebuild = rebuild
self.short_dir = short_dir
self.extra_config = extra_config
self.test_result_dir = os.path.join(self.top_dir, "result")
self.toolchain_type = toolchain_type
self.build_type = build_type
@property
def stamps_dir(self):
return os.path.join(self.build_dir, "stamps")
@staticmethod
def get_cpu_series(triple):
cpu_series = ""
if "csky" in triple:
cpu_series = "Xuantie-800"
elif "riscv" in triple:
cpu_series = "Xuantie-900"
assert cpu_series
return cpu_series
@property
def cpu_series(self):
return self.get_cpu_series(self.triple)
@staticmethod
def get_platform(triple):
pf = "elf"
if "linux" in triple:
pf = "linux"
return pf
@property
def platform(self):
return self.get_platform(self.triple)
@staticmethod
def get_libc(triple):
libc = "newlib"
if "linux" in triple:
if "musl" in triple:
libc = "musl"
elif "uclibc" in triple:
libc = "uclibc"
else:
libc = "glibc"
elif "csky" in triple and "none" not in triple and "unknown" not in triple:
libc = "minilibc"
return libc
@property
def libc(self):
return self.get_libc(self.triple)
# Give the CFLAGS for project don't use cmake.
# Release usually define to '-O3 -DNDEBUG',
# but project always build for release as default,
# so don't override the default value here.
def build_type_cflags(self):
cflags_tb = {
"Release": "",
"Debug": "-O0 -g3",
"RelWithDebInfo": "-O2 -g -DNDEBUG",
"MinSizeRel": "-Os -DNDEBUG"
}
return cflags_tb[self.build_type]
def tar_name(self, has_date=False):
if self.platform == "linux":
pf = "linux-{}".format(self.kernel_version)
else:
pf = self.platform
libc = self.libc
if libc == "musl":
if "64" in self.triple:
libc += "64"
else:
libc += "32"
name_base = "{}-{}-{}-{}-{}".format(self.cpu_series, self.toolchain_type, pf,
libc, self.host)
if self.version is not None:
name_base += "-{}".format(self.version)
if has_date:
return name_base + time.strftime("-%Y%m%d")
else:
return name_base
@property
def build_dir(self):
if self.short_dir:
ret = os.path.join(self.work_dir, "b")
else:
ret = os.path.join(self.work_dir, "build-" + self.tar_name())
return ret
@property
def version_number(self):
if self.platform == "linux":
pf = "linux-{}".format(self.kernel_version)
else:
pf = self.platform
return "{} {} {} {} Toolchain {} B{}".format(self.cpu_series, pf,
self.libc, self.toolchain_type,
self.version, time.strftime("-%Y%m%d"))
@property
def install_dir(self):
return os.path.join(self.work_dir, self.tar_name())
@staticmethod
def read_kernel_version(version_file):
with open(version_file, 'r') as f:
for line in f.readlines():
if line.startswith("#define LINUX_VERSION_CODE"):
version_number = int(line.split()[-1])
return Version.convert_version_to_str(version_number)
else:
assert False
@property
def kernel_version(self):
version_file = os.path.join(self.src, "linux-headers", "include", "linux", "version.h")
return self.read_kernel_version(version_file)
def tar_release(self):
archive_format = ".zip" if platform.uname()[0] == "Windows" else ".tar.gz"
tar_file_date = "{}{}".format(self.tar_name(has_date=True), archive_format)
tar_file_nodate = "{}{}".format(self.tar_name(has_date=False), archive_format)
tar_file_date = os.path.join(self.release_dir, tar_file_date)
tar_file_nodate = os.path.join(self.release_dir, tar_file_nodate)
self.create_archive(tar_file_date, os.path.basename(self.install_dir),
self.work_dir, self.host == "mingw")
self.copy(tar_file_date, tar_file_nodate)
return tar_file_date
@staticmethod
def stamp_name(name):
return ".stamp_" + name
def add_stamp(self, name):
if not os.path.exists(self.stamps_dir):
self.mkdir_nofake(self.stamps_dir)
self.touch(os.path.join(self.stamps_dir, self.stamp_name(name)))
def has_stamp(self, name):
return os.path.exists(os.path.join(self.stamps_dir, self.stamp_name(name)))
def simple_stamp_build(self, name, config):
if not self.has_stamp(name):
build_dir = os.path.join(self.build_dir, "build-" + name)
self.mkdir(build_dir, clean=True)
self.execute(config, cwd=build_dir)
self.execute("make -j{} && make install -j{}".format(self.jobs, self.jobs), cwd=build_dir)
self.add_stamp(name)
def execute(self, cmd, cwd=None, stdout=None):
cmd_str = cmd
if cwd is not None:
cmd_str = "cd {} && {}".format(cwd, cmd)
if self.fake:
print(cmd_str)
return ""
else:
logging.info(cmd_str)
return execute_cmd(cmd, cwd, stdout)
@staticmethod
def execute_nofake(cmd, cwd=None, stdout=None):
cmd_str = cmd
if cwd is not None:
cmd_str = "cd {} && {}".format(cwd, cmd)
logging.info(cmd_str)
return execute_cmd(cmd, cwd, stdout)
def copy(self, src, dst):
dst = dst.replace(" ", "\\ ")
if type(src) == list:
for f in src:
f = f.replace(" ", "\\ ")
self.execute('cp -a {} {}'.format(f, dst))
else:
src = src.replace(" ", "\\ ")
self.execute('cp -a {} {}'.format(src, dst))
def create(self, file_name, content):
cmd = 'echo {} > {}'.format(content, file_name)
if self.fake:
print(cmd)
return False
else:
logging.info(cmd)
with open(file_name, "w") as fp:
fp.write(content)
return True
def symlink(self, src, dst, cwd=""):
old_cwd = ""
if cwd:
old_cwd = os.getcwd()
self.chdir(cwd)
if platform.uname()[0] == "Windows":
self.copy(src, dst)
else:
cmd = "ln -s {} {}".format(src, dst)
if self.fake:
print(cmd)
else:
logging.info(cmd)
os.symlink(src, dst)
if old_cwd:
self.chdir(old_cwd)
def chdir(self, path):
cmd = "cd {}".format(path)
if self.fake:
print(cmd)
else:
logging.info(cmd)
os.chdir(path)
def rm(self, path):
cmd = "rm -rf {}".format(path)
if self.fake:
print(cmd)
else:
logging.info(cmd)
if os.path.isdir(path):
shutil.rmtree(path)
else:
os.remove(path)
def touch(self, path):
cmd = "touch {}".format(path)
if self.fake:
print(cmd)
else:
logging.info(cmd)
with open(path, "a"):
os.utime(path, None)
@staticmethod
def touch_nofake(path):
cmd = "touch {}".format(path)
logging.info(cmd)
with open(path, "a"):
os.utime(path, None)
def mkdir(self, directory, parent=True, clean=False):
cmd = "mkdir -p {}".format(directory)
if self.fake:
print(cmd)
else:
logging.info("mkdir: {}".format(directory))
extend_mkdir(directory, parent, clean)
@staticmethod
def mkdir_nofake(directory, parent=True, clean=False):
logging.info("mkdir: {}".format(directory))
extend_mkdir(directory, parent, clean)
def download_ftp(self, url, cwd=None):
cmd = "wget -c -t3 -T 30 {}".format(url)
self.execute(cmd, cwd=cwd)
@staticmethod
def download_ftp_nofake(url, cwd=None):
cmd = "wget -c -t3 -T 30 {}".format(url)
Toolchain.execute_nofake(cmd, cwd=cwd)
@staticmethod
def extract_cmd(archive, output_dir=""):
if archive.endswith(".tar.gz"):
if platform.uname()[0] == "Windows":
cmd = "7z x -y {} && 7z x -y {}".format(archive, archive.replace(".tar.gz", ".tar"))
output_dir_option = "-o"
else:
cmd = "tar -xzf {}".format(archive)
output_dir_option = "-C "
elif archive.endswith(".zip"):
if platform.uname()[0] == "Windows":
cmd = "7z x -y {}".format(archive)
output_dir_option = "-o"
else:
cmd = "unzip {}".format(archive)
output_dir_option = "-d "
else:
assert False
if output_dir:
cmd += " {}{}".format(output_dir_option, output_dir)
return cmd
def extract_from_archive(self, archive, output_dir="", cwd=None):
cmd = self.extract_cmd(archive, output_dir)
self.execute(cmd, cwd=cwd)
@staticmethod
def extract_from_archive_nofake(archive, output_dir="", cwd=None):
cmd = Toolchain.extract_cmd(archive, output_dir)
Toolchain.execute_nofake(cmd, cwd=cwd)
def create_archive(self, output_file, input_file, cwd=None, dereference=False):
if output_file.endswith(".tar.gz"):
assert platform.uname()[0] != "Windows"
if dereference:
cmd = "tar --dereference -czf {} {}".format(output_file, input_file)
else:
cmd = "tar -czf {} {}".format(output_file, input_file)
elif output_file.endswith(".zip"):
if platform.uname()[0] == "Windows":
cmd = "7z a -tzip -r {} {}".format(output_file, input_file)
else:
cmd = "zip -q -r {} {}".format(output_file, input_file)
else:
assert False
self.execute(cmd, cwd=cwd)
def append_path(self, path):
cmd = "export PATH={}:$PATH".format(path)
if self.fake:
print(cmd)
else:
os.environ["PATH"] = "{}:{}".format(path, os.environ["PATH"])
logging.info(cmd)
@staticmethod
def gen_short_dir(cwd):
n = 0
while os.path.exists(os.path.join(cwd, str(n))):
n += 1
return str(n)