-
Notifications
You must be signed in to change notification settings - Fork 118
/
build.py
323 lines (282 loc) · 12.8 KB
/
build.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2019 The ungoogled-chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
ungoogled-chromium build script for Microsoft Windows
"""
import sys
import time
if sys.version_info.major != 3 or sys.version_info.minor < 8 or sys.version_info.minor > 10:
raise RuntimeError('Python 3.8 to 3.10 is required for this script. You have: {}.{}'.format(
sys.version_info.major, sys.version_info.minor))
import argparse
import os
import re
import shutil
import subprocess
import ctypes
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent / 'ungoogled-chromium' / 'utils'))
import downloads
import domain_substitution
import prune_binaries
import patches
from _common import ENCODING, USE_REGISTRY, ExtractorEnum, get_logger
sys.path.pop(0)
_ROOT_DIR = Path(__file__).resolve().parent
_PATCH_BIN_RELPATH = Path('third_party/git/usr/bin/patch.exe')
def _get_vcvars_path(name='64'):
"""
Returns the path to the corresponding vcvars*.bat path
As of VS 2017, name can be one of: 32, 64, all, amd64_x86, x86_amd64
"""
vswhere_exe = '%ProgramFiles(x86)%\\Microsoft Visual Studio\\Installer\\vswhere.exe'
result = subprocess.run(
'"{}" -prerelease -latest -property installationPath'.format(vswhere_exe),
shell=True,
check=True,
stdout=subprocess.PIPE,
universal_newlines=True)
vcvars_path = Path(result.stdout.strip(), 'VC/Auxiliary/Build/vcvars{}.bat'.format(name))
if not vcvars_path.exists():
raise RuntimeError(
'Could not find vcvars batch script in expected location: {}'.format(vcvars_path))
return vcvars_path
def _run_build_process(*args, **kwargs):
"""
Runs the subprocess with the correct environment variables for building
"""
# Add call to set VC variables
cmd_input = ['call "%s" >nul' % _get_vcvars_path()]
cmd_input.append('set DEPOT_TOOLS_WIN_TOOLCHAIN=0')
cmd_input.append(' '.join(map('"{}"'.format, args)))
cmd_input.append('exit\n')
subprocess.run(('cmd.exe', '/k'),
input='\n'.join(cmd_input),
check=True,
encoding=ENCODING,
**kwargs)
def _run_build_process_timeout(*args, timeout):
"""
Runs the subprocess with the correct environment variables for building
"""
# Add call to set VC variables
cmd_input = ['call "%s" >nul' % _get_vcvars_path()]
cmd_input.append('set DEPOT_TOOLS_WIN_TOOLCHAIN=0')
cmd_input.append(' '.join(map('"{}"'.format, args)))
cmd_input.append('exit\n')
with subprocess.Popen(('cmd.exe', '/k'), encoding=ENCODING, stdin=subprocess.PIPE, creationflags=subprocess.CREATE_NEW_PROCESS_GROUP) as proc:
proc.stdin.write('\n'.join(cmd_input))
proc.stdin.close()
try:
proc.wait(timeout)
if proc.returncode != 0:
raise RuntimeError('Build failed!')
except subprocess.TimeoutExpired:
print('Sending keyboard interrupt')
for _ in range(3):
ctypes.windll.kernel32.GenerateConsoleCtrlEvent(1, proc.pid)
time.sleep(1)
try:
proc.wait(10)
except:
proc.kill()
raise KeyboardInterrupt
def _make_tmp_paths():
"""Creates TMP and TEMP variable dirs so ninja won't fail"""
tmp_path = Path(os.environ['TMP'])
if not tmp_path.exists():
tmp_path.mkdir()
tmp_path = Path(os.environ['TEMP'])
if not tmp_path.exists():
tmp_path.mkdir()
def main():
"""CLI Entrypoint"""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'--disable-ssl-verification',
action='store_true',
help='Disables SSL verification for downloading')
parser.add_argument(
'--7z-path',
dest='sevenz_path',
default=USE_REGISTRY,
help=('Command or path to 7-Zip\'s "7z" binary. If "_use_registry" is '
'specified, determine the path from the registry. Default: %(default)s'))
parser.add_argument(
'--winrar-path',
dest='winrar_path',
default=USE_REGISTRY,
help=('Command or path to WinRAR\'s "winrar.exe" binary. If "_use_registry" is '
'specified, determine the path from the registry. Default: %(default)s'))
parser.add_argument(
'--ci',
action='store_true'
)
parser.add_argument(
'--x86',
action='store_true'
)
parser.add_argument(
'--tarball',
action='store_true'
)
args = parser.parse_args()
# Set common variables
source_tree = _ROOT_DIR / 'build' / 'src'
downloads_cache = _ROOT_DIR / 'build' / 'download_cache'
if not args.ci or not (source_tree / 'BUILD.gn').exists():
# Setup environment
source_tree.mkdir(parents=True, exist_ok=True)
downloads_cache.mkdir(parents=True, exist_ok=True)
_make_tmp_paths()
# Extractors
extractors = {
ExtractorEnum.SEVENZIP: args.sevenz_path,
ExtractorEnum.WINRAR: args.winrar_path,
}
# Prepare source folder
if args.tarball:
# Download chromium tarball
get_logger().info('Downloading chromium tarball...')
download_info = downloads.DownloadInfo([_ROOT_DIR / 'ungoogled-chromium' / 'downloads.ini'])
downloads.retrieve_downloads(download_info, downloads_cache, None, True, args.disable_ssl_verification)
try:
downloads.check_downloads(download_info, downloads_cache, None)
except downloads.HashMismatchError as exc:
get_logger().error('File checksum does not match: %s', exc)
exit(1)
# Unpack chromium tarball
get_logger().info('Unpacking chromium tarball...')
downloads.unpack_downloads(download_info, downloads_cache, None, source_tree, False, None, extractors)
else:
# Clone sources
subprocess.run([sys.executable, str(Path('ungoogled-chromium', 'utils', 'clone.py')), '-o', 'build\\src', '-p', 'win32' if args.x86 else 'win64'], check=True)
# Setup GN
get_logger().info('Setting up GN...')
gnpath = source_tree / 'uc_staging' / 'gn_win'
gnpath.mkdir(parents=True, exist_ok=True)
subprocess.run(['git', 'clone', 'https://gn.googlesource.com/gn', str(gnpath)], check=True)
subprocess.run(['git', 'reset', '--hard', '20806f79c6b4ba295274e3a589d85db41a02fdaa'], cwd=gnpath, check=True)
subprocess.run(['git', 'clean', '-ffdx'], cwd=gnpath, check=True)
subprocess.run([sys.executable, str(gnpath / 'build' / 'gen.py')], check=True)
for item in gnpath.iterdir():
if not item.is_dir():
shutil.copy(item, source_tree / 'tools' / 'gn')
elif item.name != '.git' and item.name != 'out':
shutil.copytree(item, source_tree / 'tools' / 'gn' / item.name, dirs_exist_ok=True)
last_commit_position = source_tree / 'tools' / 'gn' / 'bootstrap' / 'last_commit_position.h'
if last_commit_position.exists():
last_commit_position.unlink()
shutil.move(str(gnpath / 'out' / 'last_commit_position.h'), str(last_commit_position))
# Retrieve windows downloads
get_logger().info('Downloading required files...')
download_info_win = downloads.DownloadInfo([_ROOT_DIR / 'downloads.ini'])
downloads.retrieve_downloads(download_info_win, downloads_cache, None, True, args.disable_ssl_verification)
try:
downloads.check_downloads(download_info_win, downloads_cache, None)
except downloads.HashMismatchError as exc:
get_logger().error('File checksum does not match: %s', exc)
exit(1)
# Prune binaries
pruning_list = (_ROOT_DIR / 'ungoogled-chromium' / 'pruning.list') if args.tarball else (_ROOT_DIR / 'pruning.list')
unremovable_files = prune_binaries.prune_files(
source_tree,
pruning_list.read_text(encoding=ENCODING).splitlines()
)
if unremovable_files:
get_logger().error('Files could not be pruned: %s', unremovable_files)
parser.exit(1)
# Unpack downloads
get_logger().info('Unpacking downloads...')
downloads.unpack_downloads(download_info_win, downloads_cache, None, source_tree, False, None, extractors)
# Apply patches
# First, ungoogled-chromium-patches
patches.apply_patches(
patches.generate_patches_from_series(_ROOT_DIR / 'ungoogled-chromium' / 'patches', resolve=True),
source_tree,
patch_bin_path=(source_tree / _PATCH_BIN_RELPATH)
)
# Then Windows-specific patches
patches.apply_patches(
patches.generate_patches_from_series(_ROOT_DIR / 'patches', resolve=True),
source_tree,
patch_bin_path=(source_tree / _PATCH_BIN_RELPATH)
)
# Substitute domains
domain_substitution_list = (_ROOT_DIR / 'ungoogled-chromium' / 'domain_substitution.list') if args.tarball else (_ROOT_DIR / 'domain_substitution.list')
domain_substitution.apply_substitution(
_ROOT_DIR / 'ungoogled-chromium' / 'domain_regex.list',
domain_substitution_list,
source_tree,
None
)
# Check if rust-toolchain folder has been populated
HOST_CPU_IS_64BIT = sys.maxsize > 2**32
RUST_DIR_DST = source_tree / 'third_party' / 'rust-toolchain'
RUST_DIR_SRC64 = source_tree / 'third_party' / 'rust-toolchain-x64'
RUST_DIR_SRC86 = source_tree / 'third_party' / 'rust-toolchain-x86'
RUST_FLAG_FILE = RUST_DIR_DST / 'INSTALLED_VERSION'
if not args.ci or not RUST_FLAG_FILE.exists():
# Directories to copy from source to target folder
DIRS_TO_COPY = ['bin', 'lib']
# Loop over all source folders
for rust_dir_src in [RUST_DIR_SRC64, RUST_DIR_SRC86]:
# Loop over all dirs to copy
for dir_to_copy in DIRS_TO_COPY:
# Copy bin folder for host architecture
if (dir_to_copy == 'bin') and (HOST_CPU_IS_64BIT != (rust_dir_src == RUST_DIR_SRC64)):
continue
# Create target dir
target_dir = RUST_DIR_DST / dir_to_copy
if not os.path.isdir(target_dir):
os.makedirs(target_dir)
# Loop over all subfolders of the rust source dir
for cp_src in rust_dir_src.glob(f'*/{dir_to_copy}/*'):
cp_dst = target_dir / cp_src.name
if cp_src.is_dir():
shutil.copytree(cp_src, cp_dst, dirs_exist_ok=True)
else:
shutil.copy2(cp_src, cp_dst)
# Generate version file
with open(RUST_FLAG_FILE, 'w') as f:
f.write('rustc 1.83.0-nightly (4ac7bcbaa 2024-09-04)')
f.write('\n')
if not args.ci or not (source_tree / 'out/Default').exists():
# Output args.gn
(source_tree / 'out/Default').mkdir(parents=True)
gn_flags = (_ROOT_DIR / 'ungoogled-chromium' / 'flags.gn').read_text(encoding=ENCODING)
gn_flags += '\n'
windows_flags = (_ROOT_DIR / 'flags.windows.gn').read_text(encoding=ENCODING)
if args.x86:
windows_flags = windows_flags.replace('x64', 'x86')
gn_flags += windows_flags
(source_tree / 'out/Default/args.gn').write_text(gn_flags, encoding=ENCODING)
# Enter source tree to run build commands
os.chdir(source_tree)
if not args.ci or not os.path.exists('out\\Default\\gn.exe'):
# Run GN bootstrap
_run_build_process(
sys.executable, 'tools\\gn\\bootstrap\\bootstrap.py', '-o', 'out\\Default\\gn.exe',
'--skip-generate-buildfiles')
# Run gn gen
_run_build_process('out\\Default\\gn.exe', 'gen', 'out\\Default', '--fail-on-unused-args')
if not args.ci or not os.path.exists('third_party\\rust-toolchain\\bin\\bindgen.exe'):
# Build bindgen
_run_build_process(
sys.executable,
'tools\\rust\\build_bindgen.py')
# Run ninja
if args.ci:
_run_build_process_timeout('third_party\\ninja\\ninja.exe', '-C', 'out\\Default', 'chrome',
'chromedriver', 'mini_installer', timeout=3.5*60*60)
# package
os.chdir(_ROOT_DIR)
subprocess.run([sys.executable, 'package.py'])
else:
_run_build_process('third_party\\ninja\\ninja.exe', '-C', 'out\\Default', 'chrome',
'chromedriver', 'mini_installer')
if __name__ == '__main__':
main()