1#!/usr/bin/env python 2# 3# Copyright 2014 The Chromium Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7"""Pack relocations in a library (or copy unchanged). 8 9If --enable-packing and --configuration-name=='Release', invoke the 10relocation_packer tool to pack the .rel.dyn or .rela.dyn section in the given 11library files. This step is inserted after the libraries are stripped. 12 13If --enable-packing is zero, the script copies files verbatim, with no 14attempt to pack relocations. 15 16Any library listed in --exclude-packing-list is also copied verbatim, 17irrespective of any --enable-packing setting. Typically this would be 18'libchromium_android_linker.so'. 19""" 20 21import optparse 22import os 23import shutil 24import sys 25import tempfile 26 27from util import build_utils 28 29def PackLibraryRelocations(android_pack_relocations, library_path, output_path): 30 shutil.copy(library_path, output_path) 31 pack_command = [android_pack_relocations, output_path] 32 build_utils.CheckOutput(pack_command) 33 34 35def CopyLibraryUnchanged(library_path, output_path): 36 shutil.copy(library_path, output_path) 37 38 39def main(args): 40 args = build_utils.ExpandFileArgs(args) 41 parser = optparse.OptionParser() 42 build_utils.AddDepfileOption(parser) 43 parser.add_option('--clear-dir', action='store_true', 44 help='If set, the destination directory will be deleted ' 45 'before copying files to it. This is highly recommended to ' 46 'ensure that no stale files are left in the directory.') 47 48 parser.add_option('--configuration-name', 49 default='Release', 50 help='Gyp configuration name (i.e. Debug, Release)') 51 parser.add_option('--enable-packing', 52 choices=['0', '1'], 53 help=('Pack relocations if 1 and configuration name is \'Release\',' 54 ' otherwise plain file copy')) 55 parser.add_option('--exclude-packing-list', 56 default='', 57 help='Names of any libraries explicitly not packed') 58 parser.add_option('--android-pack-relocations', 59 help='Path to the relocations packer binary') 60 parser.add_option('--stripped-libraries-dir', 61 help='Directory for stripped libraries') 62 parser.add_option('--packed-libraries-dir', 63 help='Directory for packed libraries') 64 parser.add_option('--libraries', action='append', 65 help='List of libraries') 66 parser.add_option('--stamp', help='Path to touch on success') 67 parser.add_option('--filelistjson', 68 help='Output path of filelist.json to write') 69 70 options, _ = parser.parse_args(args) 71 enable_packing = (options.enable_packing == '1' and 72 options.configuration_name == 'Release') 73 exclude_packing_set = set(build_utils.ParseGypList( 74 options.exclude_packing_list)) 75 76 libraries = [] 77 for libs_arg in options.libraries: 78 libraries += build_utils.ParseGypList(libs_arg) 79 80 if options.clear_dir: 81 build_utils.DeleteDirectory(options.packed_libraries_dir) 82 83 build_utils.MakeDirectory(options.packed_libraries_dir) 84 85 output_paths = [] 86 for library in libraries: 87 library_path = os.path.join(options.stripped_libraries_dir, library) 88 output_path = os.path.join( 89 options.packed_libraries_dir, os.path.basename(library)) 90 output_paths.append(output_path) 91 92 if enable_packing and library not in exclude_packing_set: 93 PackLibraryRelocations(options.android_pack_relocations, 94 library_path, 95 output_path) 96 else: 97 CopyLibraryUnchanged(library_path, output_path) 98 99 if options.filelistjson: 100 build_utils.WriteJson({ 'files': output_paths }, options.filelistjson) 101 102 if options.depfile: 103 build_utils.WriteDepfile( 104 options.depfile, 105 libraries + build_utils.GetPythonDependencies()) 106 107 if options.stamp: 108 build_utils.Touch(options.stamp) 109 110 return 0 111 112 113if __name__ == '__main__': 114 sys.exit(main(sys.argv[1:])) 115