1. 引言
在Ungoogled Chromium的编译过程中,我们发现官方提供的build.py脚本在多次运行时会出现一些问题。这些问题主要源于脚本重复执行解压和补丁应用等操作。本文将详细介绍如何优化build.py脚本,使其能够在多次运行时正常工作。
2. 脚本问题分析
2.1 主要问题
- 重复解压导致的文件冲突
- 重复应用补丁导致的错误
- 重复进行域名替换
2.2 问题原因
- 脚本没有检查操作是否已完成
- 每次运行都尝试重新执行所有步骤
- 缺少状态保持机制
3. 脚本修改
3.1 注释不必要的操作
需要注释掉以下部分:
# 注释下载和解压相关代码
"""
# 下载chromium源码包
get_logger().info('Downloading chromium tarball...')
download_info = downloads.DownloadInfo([_ROOT_DIR / 'ungoogled-chromium' / 'downloads.ini'])
downloads.retrieve_downloads(download_info, downloads_cache, True, args.disable_ssl_verification)
# 解压chromium源码包
get_logger().info('Unpacking chromium tarball...')
downloads.unpack_downloads(download_info, downloads_cache, source_tree, None, extractors)
"""
# 注释补丁应用相关代码
"""
# 应用补丁
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)
)
"""
# 注释域名替换相关代码
"""
domain_substitution.apply_substitution(
_ROOT_DIR / 'ungoogled-chromium' / 'domain_regex.list',
domain_substitution_list,
source_tree,
None
)
"""
3.2 保留必要功能
确保保留以下关键功能:
if not args.ci or not (source_tree / 'BUILD.gn').exists():
source_tree.mkdir(parents=True, exist_ok=True)
downloads_cache.mkdir(parents=True, exist_ok=True)
_make_tmp_paths()
4. 优化后的脚本结构
4.1 主要功能保留
- 环境检查和初始化
- 目录创建
- 编译工具链准备
- 构建命令执行
4.2 脚本内容
将build.py
替换成如下的内容:
#!/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(v