Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / native_client / pnacl / driver / pnacl-dis.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 sys
7 import driver_tools
8 from driver_env import env
9 from driver_log import Log, DriverOpen, DriverClose
10 import filetype
11
12 EXTRA_ENV = {
13   'INPUTS'          : '',
14   'OUTPUT'          : '',
15   'FLAGS'           : '',
16 }
17
18 DISPatterns = [
19   ( '--file-type',            "env.set('FILE_TYPE', '1')"),
20   ( ('-o','(.*)'),            "env.set('OUTPUT', pathtools.normalize($0))"),
21   ( '(-.*)',                  "env.append('FLAGS', $0)"),
22   ( '(.*)',                   "env.append('INPUTS', pathtools.normalize($0))"),
23 ]
24
25 def main(argv):
26   env.update(EXTRA_ENV)
27   driver_tools.ParseArgs(argv, DISPatterns)
28
29   inputs = env.get('INPUTS')
30   output = env.getone('OUTPUT')
31
32   for path in inputs + [output]:
33     driver_tools.CheckPathLength(path)
34
35   if len(inputs) == 0:
36     Log.Fatal("No input files given")
37
38   if len(inputs) > 1 and output != '':
39     Log.Fatal("Cannot have -o with multiple inputs")
40
41   for infile in inputs:
42     env.push()
43     env.set('input', infile)
44     env.set('output', output)
45
46     # When we output to stdout, set redirect_stdout and set log_stdout
47     # to False to bypass the driver's line-by-line handling of stdout
48     # which is extremely slow when you have a lot of output
49
50     if (filetype.IsLLVMBitcode(infile) or
51         filetype.IsPNaClBitcode(infile)):
52       bitcodetype = 'PNaCl' if filetype.IsPNaClBitcode(infile) else 'LLVM'
53       format = bitcodetype.lower()
54
55       if env.has('FILE_TYPE'):
56         sys.stdout.write('%s: %s bitcode\n' % (infile, bitcodetype))
57         continue
58       env.append('FLAGS', '-bitcode-format=' + format)
59       if output == '':
60         # LLVM by default outputs to a file if -o is missing
61         # Let's instead output to stdout
62         env.set('output', '-')
63         env.append('FLAGS', '-f')
64       driver_tools.Run('${LLVM_DIS} ${FLAGS} ${input} -o ${output}')
65     elif filetype.IsELF(infile):
66       if env.has('FILE_TYPE'):
67         sys.stdout.write('%s: ELF\n' % infile)
68         continue
69       flags = env.get('FLAGS')
70       if len(flags) == 0:
71         env.append('FLAGS', '-d')
72       if output == '':
73         # objdump to stdout
74         driver_tools.Run('"${OBJDUMP}" ${FLAGS} ${input}')
75       else:
76         # objdump always outputs to stdout, and doesn't recognize -o
77         # Let's add this feature to be consistent.
78         fp = DriverOpen(output, 'w')
79         driver_tools.Run('${OBJDUMP} ${FLAGS} ${input}', redirect_stdout=fp)
80         DriverClose(fp)
81     else:
82       Log.Fatal('Unknown file type')
83     env.pop()
84   # only reached in case of no errors
85   return 0
86
87 def get_help(unused_argv):
88   return """Usage: pnacl-dis [options] <input binary file> -o <output.txt>
89
90 Disassembler for PNaCl.  Converts either bitcode to text or
91 native code to assembly.  For native code, this just a wrapper around objdump
92 so this accepts the usual objdump flags.
93
94 OPTIONS:
95   -o <file>        Output to file
96   -help | -h       Output this help.
97   --file-type      Detect and print the file type for each of the input files.
98 """