blob: b2f8a92d5007800355ab4d696f248e79eb7ce561 [file] [log] [blame]
# Copyright (C) 2018 The Android Open Source Project
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.
"""Updates libchrome.
This script uprevs the libchrome library with newer Chromium code.
How to use:
Prepare your local Chromium repository with the target revision.
$ cd external/libchrome
$ python3 libchrome_tools/ \
This script does following things;
- Clean existing libchrome code, except some manually created files and tools.
- Copy necessary files from original Chromium repository.
- Apply patches to the copied files, if necessary.
import argparse
import fnmatch
import glob
import os
import re
import shutil
import subprocess
_TOOLS_DIR = os.path.dirname(os.path.realpath(__file__))
_LIBCHROME_ROOT = os.path.dirname(_TOOLS_DIR)
# Files in this list or in the directory listed here will be just copied.
# Paths ends with '/' is interpreted as directory.
# Files which are in the repository, but should not be imported from Chrome
# repository.
# Libchrome specific files.
# No Chromium OWNERS should be imported.
# libchrome_tools and soong are out of the update target.
# No internal directories.
# Those files should be generated. Please see also buildflag_header.patch.
# Blacklist several third party libraries; system libraries should be used.
def _find_target_files(chromium_root):
"""Returns target files to be upreved."""
# Files in the repository should be updated.
output = subprocess.check_output(
['git', 'ls-tree', '-r', '--name-only', '--full-name', 'HEAD'],
# Files in _IMPORT_LIST are copied in the following section, so
# exclude them from candidates, here, so that files deleted in chromium
# repository will be deleted on update.
candidates = [
path for path in output.splitlines()
if not any(path.startswith(import_path) for import_path in _IMPORT_LIST)]
# All files listed in _IMPORT_LIST should be imported, too.
for import_path in _IMPORT_LIST:
import_root = os.path.join(chromium_root, import_path)
# If it is a file, just add to the candidates.
if os.path.isfile(import_root):
# If it is a directory, traverse all files in the directory recursively
# and add all of them to candidates.
for dirpath, dirnames, filenames in os.walk(import_root):
for filename in filenames:
filepath = os.path.join(dirpath, filename)
candidates.append(os.path.relpath(filepath, chromium_root))
# Apply blacklist.
exclude_pattern = re.compile('|'.join(
'(?:%s)' % fnmatch.translate(pattern) for pattern in _IMPORT_BLACKLIST))
return [filepath for filepath in candidates
if not exclude_pattern.match(filepath)]
def _clean_existing_dir(output_root):
"""Removes existing libchrome files.
output_root: Path to the output directory.
os.makedirs(output_root, mode=0o755, exist_ok=True)
for path in os.listdir(output_root):
target_path = os.path.join(output_root, path)
if (not os.path.isdir(target_path) or path in ('.git', 'libchrome_tools', 'soong')):
def _import_files(chromium_root, output_root):
"""Copies files from Chromium repository into libchrome.
chromium_root: Path to the Chromium's repository.
output_root: Path to the output directory.
for filepath in _find_target_files(chromium_root):
source_path = os.path.join(chromium_root, filepath)
target_path = os.path.join(output_root, filepath)
os.makedirs(os.path.dirname(target_path), mode=0o755, exist_ok=True)
shutil.copy2(source_path, target_path)
def _apply_patch_files(patch_root, output_root):
"""Applies patches.
libchrome needs some modification from Chromium repository, e.g. supporting
toolchain which is not used by Chrome, or using system library rather than
the library checked in the Chromium repository.
See each *.patch file in libchrome_tools/patch/ directory for details.
patch_root: Path to the directory containing patch files.
output_root: Path to the output directory.
for patch_file in glob.iglob(os.path.join(patch_root, '*.patch')):
with open(patch_file, 'r') as f:
subprocess.check_call(['patch', '-p1'], stdin=f, cwd=output_root)
def _parse_args():
"""Parses commandline arguments."""
parser = argparse.ArgumentParser()
# TODO(hidehiko): Support to specify the Chromium's revision number.
help='Root directory to the local chromium repository.')
help='Output directory, which is libchrome root directory.')
default=os.path.join(_TOOLS_DIR, 'patch'),
help='Directory containing patch files to be applied.')
return parser.parse_args()
def main():
args = _parse_args()
_import_files(args.chromium_root, args.output_root)
_apply_patch_files(args.patch_dir, args.output_root)
# TODO(hidehiko): Create a git commit with filling templated message.
if __name__ == '__main__':