Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / build / android / gyp / pack_arm_relocations.py
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 ARM relative relocations in a library (or copy unchanged).
8
9 If --enable-packing and --configuration-name=='Release', invoke the
10 relocation_packer tool to pack the .rel.dyn section in the given library
11 files.  This step is inserted after the libraries are stripped.  Packing
12 adds a new .android.rel.dyn section to the file and reduces the size of
13 .rel.dyn accordingly.
14
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.
18
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 """
23
24 import json
25 import optparse
26 import os
27 import shlex
28 import shutil
29 import sys
30 import tempfile
31
32 from util import build_utils
33
34 def PackArmLibraryRelocations(android_pack_relocations,
35                               android_objcopy,
36                               library_path,
37                               output_path):
38   if not build_utils.IsTimeStale(output_path, [library_path]):
39     return
40
41   # Copy and add a 'NULL' .android.rel.dyn section for the packing tool.
42   with tempfile.NamedTemporaryFile() as stream:
43     stream.write('NULL')
44     stream.flush()
45     objcopy_command = [android_objcopy,
46                        '--add-section', '.android.rel.dyn=%s' % stream.name,
47                        library_path, output_path]
48     build_utils.CheckOutput(objcopy_command)
49
50   # Pack R_ARM_RELATIVE relocations.
51   pack_command = [android_pack_relocations, output_path]
52   build_utils.CheckOutput(pack_command)
53
54
55 def CopyArmLibraryUnchanged(library_path, output_path):
56   if not build_utils.IsTimeStale(output_path, [library_path]):
57     return
58
59   shutil.copy(library_path, output_path)
60
61
62 def main():
63   parser = optparse.OptionParser()
64
65   parser.add_option('--configuration-name',
66       default='Release',
67       help='Gyp configuration name (i.e. Debug, Release)')
68   parser.add_option('--enable-packing',
69       choices=['0', '1'],
70       help=('Pack relocations if 1 and configuration name is \'Release\','
71             ' otherwise plain file copy'))
72   parser.add_option('--exclude-packing-list',
73       default='',
74       help='Names of any libraries explicitly not packed')
75   parser.add_option('--android-pack-relocations',
76       help='Path to the ARM relocations packer binary')
77   parser.add_option('--android-objcopy',
78       help='Path to the toolchain\'s objcopy binary')
79   parser.add_option('--stripped-libraries-dir',
80       help='Directory for stripped libraries')
81   parser.add_option('--packed-libraries-dir',
82       help='Directory for packed libraries')
83   parser.add_option('--libraries-file',
84       help='Path to json file containing list of libraries')
85   parser.add_option('--stamp', help='Path to touch on success')
86
87   options, _ = parser.parse_args()
88   enable_packing = (options.enable_packing == '1' and
89                     options.configuration_name == 'Release')
90   exclude_packing_set = set(shlex.split(options.exclude_packing_list))
91
92   with open(options.libraries_file, 'r') as libfile:
93     libraries = json.load(libfile)
94
95   build_utils.MakeDirectory(options.packed_libraries_dir)
96
97   for library in libraries:
98     library_path = os.path.join(options.stripped_libraries_dir, library)
99     output_path = os.path.join(options.packed_libraries_dir, library)
100
101     if enable_packing and library not in exclude_packing_set:
102       PackArmLibraryRelocations(options.android_pack_relocations,
103                                 options.android_objcopy,
104                                 library_path,
105                                 output_path)
106     else:
107       CopyArmLibraryUnchanged(library_path, output_path)
108
109   if options.stamp:
110     build_utils.Touch(options.stamp)
111
112   return 0
113
114
115 if __name__ == '__main__':
116   sys.exit(main())