Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320)
[chromium-blink-merge.git] / build / android / gyp / pack_arm_relocations.py
blob517a22eba9ed429eb56a53badecd70c751886a3d
1 #!/usr/bin/env python
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.
7 """Pack ARM relative relocations in a library (or copy unchanged).
9 If --enable-packing and --configuration-name=='Release', invoke the
10 relocation_packer tool to pack the .rel.dyn or .rela.dyn section in the given
11 library files. This step is inserted after the libraries are stripped.
12 Packing adds a new .android.rel.dyn or .android.rela.dyn section to the file
13 and reduces the size of .rel.dyn or .rela.dyn accordingly.
15 Currently packing only understands ARM32 shared libraries. For all other
16 architectures --enable-packing should be set to zero. In this case the
17 script copies files verbatim, with no attempt to pack relative relocations.
19 Any library listed in --exclude-packing-list is also copied verbatim,
20 irrespective of any --enable-packing setting. Typically this would be
21 'libchromium_android_linker.so'.
22 """
24 import optparse
25 import os
26 import shlex
27 import shutil
28 import sys
29 import tempfile
31 from util import build_utils
33 def PackArmLibraryRelocations(android_pack_relocations,
34 android_objcopy,
35 has_relocations_with_addends,
36 library_path,
37 output_path):
38 # Select an appropriate name for the section we add.
39 if has_relocations_with_addends:
40 new_section = '.android.rela.dyn'
41 else:
42 new_section = '.android.rel.dyn'
44 # Copy and add a 'NULL' packed relocations section for the packing tool.
45 with tempfile.NamedTemporaryFile() as stream:
46 stream.write('NULL')
47 stream.flush()
48 objcopy_command = [android_objcopy,
49 '--add-section', '%s=%s' % (new_section, stream.name),
50 library_path, output_path]
51 build_utils.CheckOutput(objcopy_command)
53 # Pack R_ARM_RELATIVE relocations.
54 pack_command = [android_pack_relocations, output_path]
55 build_utils.CheckOutput(pack_command)
58 def CopyArmLibraryUnchanged(library_path, output_path):
59 shutil.copy(library_path, output_path)
62 def main(args):
63 args = build_utils.ExpandFileArgs(args)
64 parser = optparse.OptionParser()
65 build_utils.AddDepfileOption(parser)
66 parser.add_option('--clear-dir', action='store_true',
67 help='If set, the destination directory will be deleted '
68 'before copying files to it. This is highly recommended to '
69 'ensure that no stale files are left in the directory.')
71 parser.add_option('--configuration-name',
72 default='Release',
73 help='Gyp configuration name (i.e. Debug, Release)')
74 parser.add_option('--enable-packing',
75 choices=['0', '1'],
76 help=('Pack relocations if 1 and configuration name is \'Release\','
77 ' otherwise plain file copy'))
78 parser.add_option('--has-relocations-with-addends',
79 choices=['0', '1'],
80 help=('Pack into \'.android.rela.dyn\' if 1, else \'.android.rel.dyn\''))
81 parser.add_option('--exclude-packing-list',
82 default='',
83 help='Names of any libraries explicitly not packed')
84 parser.add_option('--android-pack-relocations',
85 help='Path to the ARM relocations packer binary')
86 parser.add_option('--android-objcopy',
87 help='Path to the toolchain\'s objcopy binary')
88 parser.add_option('--stripped-libraries-dir',
89 help='Directory for stripped libraries')
90 parser.add_option('--packed-libraries-dir',
91 help='Directory for packed libraries')
92 parser.add_option('--libraries', action='append',
93 help='List of libraries')
94 parser.add_option('--stamp', help='Path to touch on success')
96 options, _ = parser.parse_args(args)
97 enable_packing = (options.enable_packing == '1' and
98 options.configuration_name == 'Release')
99 has_relocations_with_addends = (options.has_relocations_with_addends == '1')
100 exclude_packing_set = set(shlex.split(options.exclude_packing_list))
102 libraries = []
103 for libs_arg in options.libraries:
104 libraries += build_utils.ParseGypList(libs_arg)
106 if options.clear_dir:
107 build_utils.DeleteDirectory(options.packed_libraries_dir)
109 build_utils.MakeDirectory(options.packed_libraries_dir)
111 for library in libraries:
112 library_path = os.path.join(options.stripped_libraries_dir, library)
113 output_path = os.path.join(
114 options.packed_libraries_dir, os.path.basename(library))
116 if enable_packing and library not in exclude_packing_set:
117 PackArmLibraryRelocations(options.android_pack_relocations,
118 options.android_objcopy,
119 has_relocations_with_addends,
120 library_path,
121 output_path)
122 else:
123 CopyArmLibraryUnchanged(library_path, output_path)
125 if options.depfile:
126 build_utils.WriteDepfile(
127 options.depfile,
128 libraries + build_utils.GetPythonDependencies())
130 if options.stamp:
131 build_utils.Touch(options.stamp)
133 return 0
136 if __name__ == '__main__':
137 sys.exit(main(sys.argv[1:]))