Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / native_client / pnacl / driver / pnacl-strip.py
1 #!/usr/bin/python
2 # Copyright (c) 2012 The Native Client Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 import driver_tools
7 import filetype
8 import shutil
9 import pathtools
10 from driver_env import env
11 from driver_log import Log
12
13 EXTRA_ENV = {
14   'INPUTS'             : '',
15   'OUTPUT'             : '',
16   'MODE'               : 'all',
17
18   'OPT_FLAGS_all'      : '-disable-opt --strip',
19   'OPT_FLAGS_debug'    : '-disable-opt --strip-debug',
20   'STRIP_FLAGS_all'    : '-s',
21   'STRIP_FLAGS_debug'  : '-S',
22
23   'OPT_FLAGS'          : '${OPT_FLAGS_%MODE%}',
24   'STRIP_FLAGS'        : '${STRIP_FLAGS_%MODE%}',
25
26   'RUN_OPT'            : '${LLVM_OPT} ${OPT_FLAGS} ${input} -o ${output}',
27   'RUN_STRIP'          : '${STRIP} ${STRIP_FLAGS} ${input} -o ${output}',
28 }
29
30 StripPatterns = [
31     ( ('-o','(.*)'),     "env.set('OUTPUT', pathtools.normalize($0))"),
32     ( ('-o','(.*)'),     "env.set('OUTPUT', pathtools.normalize($0))"),
33
34     ( '--strip-all',     "env.set('MODE', 'all')"),
35     ( '-s',              "env.set('MODE', 'all')"),
36
37     ( '--strip-debug',   "env.set('MODE', 'debug')"),
38     ( '-S',              "env.set('MODE', 'debug')"),
39     ( '-g',              "env.set('MODE', 'debug')"),
40     ( '-d',              "env.set('MODE', 'debug')"),
41
42     ( '(-p)',            "env.append('STRIP_FLAGS', $0)"),
43     ( '(--info)',        "env.append('STRIP_FLAGS', $0)"),
44
45     ( '(-.*)',           driver_tools.UnrecognizedOption),
46
47     ( '(.*)',            "env.append('INPUTS', pathtools.normalize($0))"),
48 ]
49
50 def main(argv):
51   env.update(EXTRA_ENV)
52   driver_tools.ParseArgs(argv, StripPatterns)
53   inputs = env.get('INPUTS')
54   output = env.getone('OUTPUT')
55   for path in inputs + [output]:
56     driver_tools.CheckPathLength(path)
57
58   if len(inputs) > 1 and output != '':
59     Log.Fatal('Cannot have -o with multiple inputs')
60
61   if '--info' in env.get('STRIP_FLAGS'):
62     code, _, _ = driver_tools.Run('${STRIP} ${STRIP_FLAGS}')
63     return code
64
65   for f in inputs:
66     if output != '':
67       f_output = output
68     else:
69       f_output = f
70     if filetype.IsPNaClBitcode(f):
71       # PNaCl-format bitcode has no symbols, i.e. it is already stripped.
72       if f != f_output:
73         shutil.copyfile(f, f_output)
74     elif filetype.IsLLVMBitcode(f):
75       driver_tools.RunWithEnv('${RUN_OPT}', input=f, output=f_output)
76     elif filetype.IsELF(f) or filetype.IsNativeArchive(f):
77       driver_tools.RunWithEnv('${RUN_STRIP}', input=f, output=f_output)
78     elif filetype.IsBitcodeArchive(f):
79       # The strip tool supports native archives, but it does not support the
80       # LLVM gold plugin so cannot handle bitcode.  There is also no bitcode
81       # tool like opt that support archives.
82       Log.Fatal('%s: strip does not support bitcode archives',
83                 pathtools.touser(f))
84     else:
85       Log.Fatal('%s: File is neither ELF, nor bitcode', pathtools.touser(f))
86   return 0
87
88
89 def get_help(unused_argv):
90   script = env.getone('SCRIPT_NAME')
91   return """Usage: %s <option(s)> in-file(s)
92   Removes symbols and sections from bitcode or native code.
93
94   The options are:
95   -p --preserve-dates              Copy modified/access timestamps to the output
96                                    (only native code for now)
97   -s --strip-all                   Remove all symbol and relocation information
98   -g -S -d --strip-debug           Remove all debugging symbols & sections
99   -h --help                        Display this output
100      --info                        List object formats & architectures supported
101   -o <file>                        Place stripped output into <file>
102
103 %s: supported targets: bitcode, native code (see --info).
104 """ % (script, script)