Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / native_client / pnacl / driver / pnacl-opt.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 subprocess
7
8 from driver_env import env
9 import driver_tools
10 import filetype
11
12 EXTRA_ENV = {
13   'ARGS'   : '',
14   'INPUT'  : '',
15   'OUTPUT' : '',
16   # Binary output may go to stdout (when -o was not specified)
17   'HAVE_OUTPUT' : '0',
18   # We disable the LLVM simplify-libcalls pass by default, since we
19   # statically link in libc, and pnacl-opt is typically used for post-link
20   # optimizations.  Changing one library call to another can lead
21   # to undefined symbol errors since which definitions from libc are linked
22   # in is already decided.
23   'DISABLE_SIMPLIFY_LIBCALLS' : '1',
24 }
25
26 PATTERNS  = [
27   ( '--enable-simplify-libcalls', "env.set('DISABLE_SIMPLIFY_LIBCALLS', '0')"),
28   (('-o','(.*)'),      "env.set('OUTPUT', pathtools.normalize($0))\n" +
29                        "env.set('HAVE_OUTPUT', '1')"),
30   ( '(-.*)',           "env.append('ARGS', $0)"),
31   ( '(.*)',            "env.set('INPUT', $0)"),
32 ]
33
34 def main(argv):
35   env.update(EXTRA_ENV)
36   driver_tools.ParseArgs(argv, PATTERNS)
37
38   driver_tools.CheckPathLength(env.getone('INPUT'))
39   driver_tools.CheckPathLength(env.getone('OUTPUT'))
40
41   driver_tools.Run(
42       '"${LLVM_OPT}" ${ARGS} ' +
43       '${DISABLE_SIMPLIFY_LIBCALLS ? -disable-simplify-libcalls} ' +
44       '${HAVE_OUTPUT ? -o ${OUTPUT}} ' +
45       '${INPUT}')
46
47   # Opt is the only tool that will modify a file in-place. If this happens we
48   # need to clear the filetype cache so future invocations of the type checking
49   # routines will re-check the file.
50   if env.getone('INPUT') == env.getone('OUTPUT'):
51     filetype.ClearFileTypeCaches()
52
53   # only reached in case of no errors
54   return 0
55
56 def get_help(unused_argv):
57   # Set errexit=False -- Do not exit early to allow testing.
58   # For some reason most the llvm tools return non-zero with --help,
59   # while all of the gnu tools return 0 with --help.
60   # On windows, the exit code is also inconsistent =(
61   code, stdout, stderr = driver_tools.Run('${LLVM_OPT} -help',
62                                           redirect_stdout=subprocess.PIPE,
63                                           redirect_stderr=subprocess.STDOUT,
64                                           errexit=False)
65   return stdout