-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·213 lines (175 loc) · 5.84 KB
/
Copy pathmain.py
File metadata and controls
executable file
·213 lines (175 loc) · 5.84 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
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
#!/usr/bin/python3
import argparse
import logging
import os
from packaging.version import Version
from pathlib import Path
import shutil
import subprocess
from subprocess import PIPE, Popen
import sys
from tempfile import NamedTemporaryFile
from typing import Dict, List
from _python_path import add_extra_path
add_extra_path()
from mingw_lite.args import parse_args
from mingw_lite.path import ProjectPaths
from mingw_lite.prepare_source import prepare_source
from mingw_lite.profile import BranchProfile, resolve_profile
from mingw_lite.util import ensure, overlayfs_ro
# A = x86_64-linux-gnu or x86_64-linux-musl
# B = {i686,x86_64}-w64-mingw32
# XYZ: build = X, host = Y, target = Z
from mingw_lite.AAA import build_AAA_library, build_AAA_tool
from mingw_lite.AAB import build_AAB_compiler, build_AAB_library
from mingw_lite.ABB import build_ABB_test_driver, build_ABB_toolchain, build_ABB_xmake
def check_short_import_version(ver: BranchProfile, download_only: bool):
if not ver.short_import:
return
if Version(ver.binutils) < Version('2.47'):
raise Exception('short import requires binutils >= 2.47')
if download_only:
return
llvm_version = subprocess.run(
['llvm-config', '--version'],
check = True,
stdout = PIPE,
).stdout.decode().strip()
if Version(llvm_version).major < 19:
raise Exception('short import requires LLVM >= 19')
def clean(config: argparse.Namespace, paths: ProjectPaths):
if paths.build_dir.exists():
shutil.rmtree(paths.build_dir)
if not config.no_cross and paths.layer_AAA.prefix.exists():
shutil.rmtree(paths.layer_AAA.prefix)
if not config.no_cross and paths.layer_AAB.prefix.exists():
shutil.rmtree(paths.layer_AAB.prefix)
if paths.layer_ABB.prefix.exists():
shutil.rmtree(paths.layer_ABB.prefix)
def prepare_dirs(paths: ProjectPaths):
paths.assets_dir.mkdir(parents = True, exist_ok = True)
paths.build_dir.mkdir(parents = True, exist_ok = True)
paths.dist_dir.mkdir(parents = True, exist_ok = True)
def _sort_tarball(root: Path, src: Path):
files: Dict[Path, List[str]] = {}
for file in src.glob('**/*'):
if not file.is_dir():
dn = file.relative_to(root).parent
fn = file.name
if dn not in files:
files[dn] = []
files[dn].append(fn)
result = []
for dn in sorted(files.keys()):
result.append(f'{dn}/')
for fn in sorted(files[dn]):
result.append(f'{dn}/{fn}')
return result
def _package(root: Path, files: List[str], dst: Path):
with NamedTemporaryFile(delete = False) as listfile:
listname = listfile.name
for fn in files:
listfile.write(f'{fn}\n'.encode())
tar = Popen([
'bsdtar', '-c',
'-f', '-',
'-C', root,
'-T', listname, '-n',
'--numeric-owner',
], stdout = PIPE)
zstd = Popen([
'zstd', '-f',
'--zstd=strat=5,wlog=27,hlog=25,slog=6,ovlog=9',
'-o', dst,
], stdin = tar.stdout)
tar.stdout.close()
zstd.communicate()
tar.wait()
if tar.returncode != 0 or zstd.returncode != 0:
raise Exception('bsdtar | zstd failed')
os.unlink(listname)
def package_cross(paths: ProjectPaths):
files = [
*_sort_tarball(paths.layer_dir.parent, paths.layer_AAA.prefix),
*_sort_tarball(paths.layer_dir.parent, paths.layer_AAB.prefix),
]
_package(paths.layer_dir.parent, files, paths.cross_pkg)
def package_test_driver(paths: ProjectPaths):
files = [
*_sort_tarball(paths.layer_ABB.test_driver, paths.layer_ABB.test_driver),
]
_package(paths.layer_ABB.test_driver, files, paths.test_driver_pkg)
def package_layers(pkg_dir: Path, layers: List[Path], dst: Path):
files = []
file_to_package_map: Dict[str, str] = {}
for layer in layers:
sorted_part = _sort_tarball(layer, layer)
files.extend(map(
lambda fn: f'{pkg_dir.name}/{fn}',
sorted_part
))
# check file collisions
for fn in sorted_part:
if fn.endswith('/'):
continue
if fn in file_to_package_map:
raise Exception(f'file collision: {fn} in {layer.name} and {file_to_package_map[fn]}')
file_to_package_map[fn] = layer.name
ensure(pkg_dir)
with overlayfs_ro(pkg_dir, layers):
_package(pkg_dir.parent, files, dst)
def package_mingw(paths: ProjectPaths):
layers = [
paths.layer_ABB.binutils,
paths.layer_ABB.crt,
paths.layer_ABB.gcc,
paths.layer_ABB.gcc_lib,
paths.layer_ABB.gdb,
paths.layer_ABB.headers,
paths.layer_ABB.make,
paths.layer_ABB.mcfgthread,
paths.layer_ABB.nowide,
paths.layer_ABB.pkgconf,
paths.layer_ABB.winpthreads,
]
package_layers(paths.pkg_dir, layers, paths.mingw_pkg)
def package_xmake(paths: ProjectPaths):
layers = [
paths.layer_ABB.xmake,
]
package_layers(paths.pkg_dir, layers, paths.xmake_pkg)
def main():
config = parse_args(require_build_compiler = True)
if config.verbose >= 2:
logging.basicConfig(level = logging.DEBUG)
elif config.verbose >= 1:
logging.basicConfig(level = logging.INFO)
else:
logging.basicConfig(level = logging.ERROR)
logging.info("building GCC %s for %s", config.branch, config.profile)
ver = resolve_profile(config)
check_short_import_version(ver, config.download_only)
paths = ProjectPaths(config, ver)
if config.clean:
clean(config, paths)
prepare_dirs(paths)
prepare_source(ver, paths, config.download_only)
if config.download_only:
return
# https://bugs.archlinux.org/task/78702
os.environ['LIBMOUNT_FORCE_MOUNT2'] = 'always'
os.environ['XMAKE_ROOT'] = 'y'
if not config.no_cross:
build_AAA_library(ver, paths, config)
build_AAA_tool(ver, paths, config)
build_AAB_compiler(ver, paths, config)
build_AAB_library(ver, paths, config)
package_cross(paths)
build_ABB_test_driver(ver, paths, config)
package_test_driver(paths)
build_ABB_toolchain(ver, paths, config)
package_mingw(paths)
build_ABB_xmake(ver, paths, config)
package_xmake(paths)
if __name__ == '__main__':
main()