1*635a8641SAndroid Build Coastguard Worker#!/usr/bin/python3 2*635a8641SAndroid Build Coastguard Worker 3*635a8641SAndroid Build Coastguard Worker# Copyright (C) 2018 The Android Open Source Project 4*635a8641SAndroid Build Coastguard Worker# 5*635a8641SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*635a8641SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*635a8641SAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*635a8641SAndroid Build Coastguard Worker# 9*635a8641SAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*635a8641SAndroid Build Coastguard Worker# 11*635a8641SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*635a8641SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*635a8641SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*635a8641SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*635a8641SAndroid Build Coastguard Worker# limitations under the License. 16*635a8641SAndroid Build Coastguard Worker 17*635a8641SAndroid Build Coastguard Worker"""Updates libchrome. 18*635a8641SAndroid Build Coastguard Worker 19*635a8641SAndroid Build Coastguard WorkerThis script uprevs the libchrome library with newer Chromium code. 20*635a8641SAndroid Build Coastguard WorkerHow to use: 21*635a8641SAndroid Build Coastguard Worker 22*635a8641SAndroid Build Coastguard WorkerPrepare your local Chromium repository with the target revision. 23*635a8641SAndroid Build Coastguard Worker$ cd external/libchrome 24*635a8641SAndroid Build Coastguard Worker$ python3 libchrome_tools/update_libchrome.py \ 25*635a8641SAndroid Build Coastguard Worker --chromium_root=${PATH_TO_YOUR_LOCAL_CHROMIUM_REPO} 26*635a8641SAndroid Build Coastguard Worker 27*635a8641SAndroid Build Coastguard WorkerThis script does following things; 28*635a8641SAndroid Build Coastguard Worker- Clean existing libchrome code, except some manually created files and tools. 29*635a8641SAndroid Build Coastguard Worker- Copy necessary files from original Chromium repository. 30*635a8641SAndroid Build Coastguard Worker- Apply patches to the copied files, if necessary. 31*635a8641SAndroid Build Coastguard Worker""" 32*635a8641SAndroid Build Coastguard Worker 33*635a8641SAndroid Build Coastguard Worker 34*635a8641SAndroid Build Coastguard Workerimport argparse 35*635a8641SAndroid Build Coastguard Workerimport fnmatch 36*635a8641SAndroid Build Coastguard Workerimport glob 37*635a8641SAndroid Build Coastguard Workerimport os 38*635a8641SAndroid Build Coastguard Workerimport re 39*635a8641SAndroid Build Coastguard Workerimport shutil 40*635a8641SAndroid Build Coastguard Workerimport subprocess 41*635a8641SAndroid Build Coastguard Worker 42*635a8641SAndroid Build Coastguard Worker 43*635a8641SAndroid Build Coastguard Worker_TOOLS_DIR = os.path.dirname(os.path.realpath(__file__)) 44*635a8641SAndroid Build Coastguard Worker_LIBCHROME_ROOT = os.path.dirname(_TOOLS_DIR) 45*635a8641SAndroid Build Coastguard Worker 46*635a8641SAndroid Build Coastguard Worker 47*635a8641SAndroid Build Coastguard Worker# Files in this list or in the directory listed here will be just copied. 48*635a8641SAndroid Build Coastguard Worker# Paths ends with '/' is interpreted as directory. 49*635a8641SAndroid Build Coastguard Worker_IMPORT_LIST = [ 50*635a8641SAndroid Build Coastguard Worker 'mojo/', 51*635a8641SAndroid Build Coastguard Worker 'third_party/ply/', 52*635a8641SAndroid Build Coastguard Worker 'third_party/markupsafe/', 53*635a8641SAndroid Build Coastguard Worker 'third_party/jinja2/', 54*635a8641SAndroid Build Coastguard Worker] 55*635a8641SAndroid Build Coastguard Worker 56*635a8641SAndroid Build Coastguard Worker# Files which are in the repository, but should not be imported from Chrome 57*635a8641SAndroid Build Coastguard Worker# repository. 58*635a8641SAndroid Build Coastguard Worker_IMPORT_BLACKLIST = [ 59*635a8641SAndroid Build Coastguard Worker # Libchrome specific files. 60*635a8641SAndroid Build Coastguard Worker '.gitignore', 61*635a8641SAndroid Build Coastguard Worker 'Android.bp', 62*635a8641SAndroid Build Coastguard Worker 'MODULE_LICENSE_BSD', 63*635a8641SAndroid Build Coastguard Worker 'NOTICE', 64*635a8641SAndroid Build Coastguard Worker 'OWNERS', 65*635a8641SAndroid Build Coastguard Worker 'SConstruct', 66*635a8641SAndroid Build Coastguard Worker 'libmojo.pc.in', 67*635a8641SAndroid Build Coastguard Worker 'testrunner.cc', 68*635a8641SAndroid Build Coastguard Worker '*/DEPS', 69*635a8641SAndroid Build Coastguard Worker 70*635a8641SAndroid Build Coastguard Worker # No Chromium OWNERS should be imported. 71*635a8641SAndroid Build Coastguard Worker '*/OWNERS', 72*635a8641SAndroid Build Coastguard Worker 73*635a8641SAndroid Build Coastguard Worker # libchrome_tools and soong are out of the update target. 74*635a8641SAndroid Build Coastguard Worker 'libchrome_tools/*', 75*635a8641SAndroid Build Coastguard Worker 'soong/*', 76*635a8641SAndroid Build Coastguard Worker 77*635a8641SAndroid Build Coastguard Worker # No internal directories. 78*635a8641SAndroid Build Coastguard Worker 'mojo/internal/*', 79*635a8641SAndroid Build Coastguard Worker 80*635a8641SAndroid Build Coastguard Worker # Those files should be generated. Please see also buildflag_header.patch. 81*635a8641SAndroid Build Coastguard Worker 'base/allocator/buildflags.h', 82*635a8641SAndroid Build Coastguard Worker 'base/android/java/src/org/chromium/base/BuildConfig.java', 83*635a8641SAndroid Build Coastguard Worker 'base/cfi_buildflags.h', 84*635a8641SAndroid Build Coastguard Worker 'base/debug/debugging_buildflags.h', 85*635a8641SAndroid Build Coastguard Worker 'base/memory/protected_memory_buildflags.h', 86*635a8641SAndroid Build Coastguard Worker 'base/synchronization/synchronization_buildflags.h', 87*635a8641SAndroid Build Coastguard Worker 'gen/*', 88*635a8641SAndroid Build Coastguard Worker 'ipc/ipc_buildflags.h', 89*635a8641SAndroid Build Coastguard Worker 90*635a8641SAndroid Build Coastguard Worker # Blacklist several third party libraries; system libraries should be used. 91*635a8641SAndroid Build Coastguard Worker 'base/third_party/libevent/*', 92*635a8641SAndroid Build Coastguard Worker 'base/third_party/symbolize/*', 93*635a8641SAndroid Build Coastguard Worker 94*635a8641SAndroid Build Coastguard Worker 'testing/gmock/*', 95*635a8641SAndroid Build Coastguard Worker 'testing/gtest/*', 96*635a8641SAndroid Build Coastguard Worker 'third_party/ashmem/*', 97*635a8641SAndroid Build Coastguard Worker 'third_party/modp_b64/*', 98*635a8641SAndroid Build Coastguard Worker 'third_party/protobuf/*', 99*635a8641SAndroid Build Coastguard Worker] 100*635a8641SAndroid Build Coastguard Worker 101*635a8641SAndroid Build Coastguard Workerdef _find_target_files(chromium_root): 102*635a8641SAndroid Build Coastguard Worker """Returns target files to be upreved.""" 103*635a8641SAndroid Build Coastguard Worker # Files in the repository should be updated. 104*635a8641SAndroid Build Coastguard Worker output = subprocess.check_output( 105*635a8641SAndroid Build Coastguard Worker ['git', 'ls-tree', '-r', '--name-only', '--full-name', 'HEAD'], 106*635a8641SAndroid Build Coastguard Worker cwd=_LIBCHROME_ROOT).decode('utf-8') 107*635a8641SAndroid Build Coastguard Worker 108*635a8641SAndroid Build Coastguard Worker # Files in _IMPORT_LIST are copied in the following section, so 109*635a8641SAndroid Build Coastguard Worker # exclude them from candidates, here, so that files deleted in chromium 110*635a8641SAndroid Build Coastguard Worker # repository will be deleted on update. 111*635a8641SAndroid Build Coastguard Worker candidates = [ 112*635a8641SAndroid Build Coastguard Worker path for path in output.splitlines() 113*635a8641SAndroid Build Coastguard Worker if not any(path.startswith(import_path) for import_path in _IMPORT_LIST)] 114*635a8641SAndroid Build Coastguard Worker 115*635a8641SAndroid Build Coastguard Worker # All files listed in _IMPORT_LIST should be imported, too. 116*635a8641SAndroid Build Coastguard Worker for import_path in _IMPORT_LIST: 117*635a8641SAndroid Build Coastguard Worker import_root = os.path.join(chromium_root, import_path) 118*635a8641SAndroid Build Coastguard Worker 119*635a8641SAndroid Build Coastguard Worker # If it is a file, just add to the candidates. 120*635a8641SAndroid Build Coastguard Worker if os.path.isfile(import_root): 121*635a8641SAndroid Build Coastguard Worker candidates.append(import_path) 122*635a8641SAndroid Build Coastguard Worker continue 123*635a8641SAndroid Build Coastguard Worker 124*635a8641SAndroid Build Coastguard Worker # If it is a directory, traverse all files in the directory recursively 125*635a8641SAndroid Build Coastguard Worker # and add all of them to candidates. 126*635a8641SAndroid Build Coastguard Worker for dirpath, dirnames, filenames in os.walk(import_root): 127*635a8641SAndroid Build Coastguard Worker for filename in filenames: 128*635a8641SAndroid Build Coastguard Worker filepath = os.path.join(dirpath, filename) 129*635a8641SAndroid Build Coastguard Worker candidates.append(os.path.relpath(filepath, chromium_root)) 130*635a8641SAndroid Build Coastguard Worker 131*635a8641SAndroid Build Coastguard Worker # Apply blacklist. 132*635a8641SAndroid Build Coastguard Worker exclude_pattern = re.compile('|'.join( 133*635a8641SAndroid Build Coastguard Worker '(?:%s)' % fnmatch.translate(pattern) for pattern in _IMPORT_BLACKLIST)) 134*635a8641SAndroid Build Coastguard Worker return [filepath for filepath in candidates 135*635a8641SAndroid Build Coastguard Worker if not exclude_pattern.match(filepath)] 136*635a8641SAndroid Build Coastguard Worker 137*635a8641SAndroid Build Coastguard Worker 138*635a8641SAndroid Build Coastguard Workerdef _clean_existing_dir(output_root): 139*635a8641SAndroid Build Coastguard Worker """Removes existing libchrome files. 140*635a8641SAndroid Build Coastguard Worker 141*635a8641SAndroid Build Coastguard Worker Args: 142*635a8641SAndroid Build Coastguard Worker output_root: Path to the output directory. 143*635a8641SAndroid Build Coastguard Worker """ 144*635a8641SAndroid Build Coastguard Worker os.makedirs(output_root, mode=0o755, exist_ok=True) 145*635a8641SAndroid Build Coastguard Worker for path in os.listdir(output_root): 146*635a8641SAndroid Build Coastguard Worker target_path = os.path.join(output_root, path) 147*635a8641SAndroid Build Coastguard Worker if (not os.path.isdir(target_path) or path in ('.git', 'libchrome_tools', 'soong')): 148*635a8641SAndroid Build Coastguard Worker continue 149*635a8641SAndroid Build Coastguard Worker shutil.rmtree(target_path) 150*635a8641SAndroid Build Coastguard Worker 151*635a8641SAndroid Build Coastguard Worker 152*635a8641SAndroid Build Coastguard Workerdef _import_files(chromium_root, output_root): 153*635a8641SAndroid Build Coastguard Worker """Copies files from Chromium repository into libchrome. 154*635a8641SAndroid Build Coastguard Worker 155*635a8641SAndroid Build Coastguard Worker Args: 156*635a8641SAndroid Build Coastguard Worker chromium_root: Path to the Chromium's repository. 157*635a8641SAndroid Build Coastguard Worker output_root: Path to the output directory. 158*635a8641SAndroid Build Coastguard Worker """ 159*635a8641SAndroid Build Coastguard Worker for filepath in _find_target_files(chromium_root): 160*635a8641SAndroid Build Coastguard Worker source_path = os.path.join(chromium_root, filepath) 161*635a8641SAndroid Build Coastguard Worker target_path = os.path.join(output_root, filepath) 162*635a8641SAndroid Build Coastguard Worker os.makedirs(os.path.dirname(target_path), mode=0o755, exist_ok=True) 163*635a8641SAndroid Build Coastguard Worker shutil.copy2(source_path, target_path) 164*635a8641SAndroid Build Coastguard Worker 165*635a8641SAndroid Build Coastguard Worker 166*635a8641SAndroid Build Coastguard Workerdef _apply_patch_files(patch_root, output_root): 167*635a8641SAndroid Build Coastguard Worker """Applies patches. 168*635a8641SAndroid Build Coastguard Worker 169*635a8641SAndroid Build Coastguard Worker libchrome needs some modification from Chromium repository, e.g. supporting 170*635a8641SAndroid Build Coastguard Worker toolchain which is not used by Chrome, or using system library rather than 171*635a8641SAndroid Build Coastguard Worker the library checked in the Chromium repository. 172*635a8641SAndroid Build Coastguard Worker See each *.patch file in libchrome_tools/patch/ directory for details. 173*635a8641SAndroid Build Coastguard Worker 174*635a8641SAndroid Build Coastguard Worker Args: 175*635a8641SAndroid Build Coastguard Worker patch_root: Path to the directory containing patch files. 176*635a8641SAndroid Build Coastguard Worker output_root: Path to the output directory. 177*635a8641SAndroid Build Coastguard Worker """ 178*635a8641SAndroid Build Coastguard Worker for patch_file in glob.iglob(os.path.join(patch_root, '*.patch')): 179*635a8641SAndroid Build Coastguard Worker with open(patch_file, 'r') as f: 180*635a8641SAndroid Build Coastguard Worker subprocess.check_call(['patch', '-p1'], stdin=f, cwd=output_root) 181*635a8641SAndroid Build Coastguard Worker 182*635a8641SAndroid Build Coastguard Worker 183*635a8641SAndroid Build Coastguard Workerdef _parse_args(): 184*635a8641SAndroid Build Coastguard Worker """Parses commandline arguments.""" 185*635a8641SAndroid Build Coastguard Worker parser = argparse.ArgumentParser() 186*635a8641SAndroid Build Coastguard Worker 187*635a8641SAndroid Build Coastguard Worker # TODO(hidehiko): Support to specify the Chromium's revision number. 188*635a8641SAndroid Build Coastguard Worker parser.add_argument( 189*635a8641SAndroid Build Coastguard Worker '--chromium_root', 190*635a8641SAndroid Build Coastguard Worker help='Root directory to the local chromium repository.') 191*635a8641SAndroid Build Coastguard Worker parser.add_argument( 192*635a8641SAndroid Build Coastguard Worker '--output_root', 193*635a8641SAndroid Build Coastguard Worker default=_LIBCHROME_ROOT, 194*635a8641SAndroid Build Coastguard Worker help='Output directory, which is libchrome root directory.') 195*635a8641SAndroid Build Coastguard Worker parser.add_argument( 196*635a8641SAndroid Build Coastguard Worker '--patch_dir', 197*635a8641SAndroid Build Coastguard Worker default=os.path.join(_TOOLS_DIR, 'patch'), 198*635a8641SAndroid Build Coastguard Worker help='Directory containing patch files to be applied.') 199*635a8641SAndroid Build Coastguard Worker 200*635a8641SAndroid Build Coastguard Worker return parser.parse_args() 201*635a8641SAndroid Build Coastguard Worker 202*635a8641SAndroid Build Coastguard Worker 203*635a8641SAndroid Build Coastguard Workerdef main(): 204*635a8641SAndroid Build Coastguard Worker args = _parse_args() 205*635a8641SAndroid Build Coastguard Worker _clean_existing_dir(args.output_root) 206*635a8641SAndroid Build Coastguard Worker _import_files(args.chromium_root, args.output_root) 207*635a8641SAndroid Build Coastguard Worker _apply_patch_files(args.patch_dir, args.output_root) 208*635a8641SAndroid Build Coastguard Worker # TODO(hidehiko): Create a git commit with filling templated message. 209*635a8641SAndroid Build Coastguard Worker 210*635a8641SAndroid Build Coastguard Worker 211*635a8641SAndroid Build Coastguard Workerif __name__ == '__main__': 212*635a8641SAndroid Build Coastguard Worker main() 213