Roll src/third_party/WebKit 6cdd902:94953d6 (svn 197985:197991)
[chromium-blink-merge.git] / native_client_sdk / src / tools / sel_ldr.py
blob45dfddcd38b47b07ba42b00c36e9e6ecd0d9a1ff
1 #!/usr/bin/env python
2 # Copyright 2013 The Chromium 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.
6 """Wrapper script for launching application within the sel_ldr.
7 """
9 import argparse
10 import os
11 import subprocess
12 import sys
14 import create_nmf
15 import getos
17 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
18 NACL_SDK_ROOT = os.path.dirname(SCRIPT_DIR)
20 if sys.version_info < (2, 7, 0):
21 sys.stderr.write("python 2.7 or later is required run this script\n")
22 sys.exit(1)
25 class Error(Exception):
26 pass
29 def Log(msg):
30 if Log.verbose:
31 sys.stderr.write(str(msg) + '\n')
32 Log.verbose = False
35 def FindQemu():
36 qemu_locations = [os.path.join(SCRIPT_DIR, 'qemu_arm'),
37 os.path.join(SCRIPT_DIR, 'qemu-arm')]
38 qemu_locations += [os.path.join(path, 'qemu_arm')
39 for path in os.environ["PATH"].split(os.pathsep)]
40 qemu_locations += [os.path.join(path, 'qemu-arm')
41 for path in os.environ["PATH"].split(os.pathsep)]
42 # See if qemu is in any of these locations.
43 qemu_bin = None
44 for loc in qemu_locations:
45 if os.path.isfile(loc) and os.access(loc, os.X_OK):
46 qemu_bin = loc
47 break
48 return qemu_bin
51 def main(argv):
52 epilog = 'Example: sel_ldr.py my_nexe.nexe'
53 parser = argparse.ArgumentParser(description=__doc__, epilog=epilog)
54 parser.add_argument('-v', '--verbose', action='store_true',
55 help='Verbose output')
56 parser.add_argument('-d', '--debug', action='store_true',
57 help='Enable debug stub')
58 parser.add_argument('-e', '--exceptions', action='store_true',
59 help='Enable exception handling interface')
60 parser.add_argument('-p', '--passthrough-environment', action='store_true',
61 help='Pass environment of host through to nexe')
62 parser.add_argument('--debug-libs', action='store_true',
63 help='For dynamic executables, reference debug '
64 'libraries rather then release')
65 parser.add_argument('executable', help='executable (.nexe) to run')
66 parser.add_argument('args', nargs='*', help='argument to pass to exectuable')
67 parser.add_argument('--library-path',
68 help='Pass extra library paths')
70 # To enable bash completion for this command first install optcomplete
71 # and then add this line to your .bashrc:
72 # complete -F _optcomplete sel_ldr.py
73 try:
74 import optcomplete
75 optcomplete.autocomplete(parser)
76 except ImportError:
77 pass
79 options = parser.parse_args(argv)
81 if options.verbose:
82 Log.verbose = True
84 osname = getos.GetPlatform()
85 if not os.path.exists(options.executable):
86 raise Error('executable not found: %s' % options.executable)
87 if not os.path.isfile(options.executable):
88 raise Error('not a file: %s' % options.executable)
90 arch, dynamic = create_nmf.ParseElfHeader(options.executable)
92 if arch == 'arm' and osname != 'linux':
93 raise Error('Cannot run ARM executables under sel_ldr on ' + osname)
95 arch_suffix = arch.replace('-', '_')
97 sel_ldr = os.path.join(SCRIPT_DIR, 'sel_ldr_%s' % arch_suffix)
98 irt = os.path.join(SCRIPT_DIR, 'irt_core_%s.nexe' % arch_suffix)
99 if osname == 'win':
100 sel_ldr += '.exe'
101 Log('ROOT = %s' % NACL_SDK_ROOT)
102 Log('SEL_LDR = %s' % sel_ldr)
103 Log('IRT = %s' % irt)
104 cmd = [sel_ldr]
106 if osname == 'linux':
107 # Run sel_ldr under nacl_helper_bootstrap
108 helper = os.path.join(SCRIPT_DIR, 'nacl_helper_bootstrap_%s' % arch_suffix)
109 Log('HELPER = %s' % helper)
110 cmd.insert(0, helper)
111 cmd.append('--r_debug=0xXXXXXXXXXXXXXXXX')
112 cmd.append('--reserved_at_zero=0xXXXXXXXXXXXXXXXX')
114 cmd += ['-a', '-B', irt]
116 if options.debug:
117 cmd.append('-g')
119 if options.exceptions:
120 cmd.append('-e')
122 if options.passthrough_environment:
123 cmd.append('-p')
125 if not options.verbose:
126 cmd += ['-l', os.devnull]
128 if arch == 'arm':
129 # Use the QEMU arm emulator if available.
130 qemu_bin = FindQemu()
131 if not qemu_bin:
132 raise Error('Cannot run ARM executables under sel_ldr without an emulator'
133 '. Try installing QEMU (http://wiki.qemu.org/).')
135 arm_libpath = os.path.join(NACL_SDK_ROOT, 'tools', 'lib', 'arm_trusted')
136 if not os.path.isdir(arm_libpath):
137 raise Error('Could not find ARM library path: %s' % arm_libpath)
138 qemu = [qemu_bin, '-cpu', 'cortex-a8', '-L', arm_libpath]
139 # '-Q' disables platform qualification, allowing arm binaries to run.
140 cmd = qemu + cmd + ['-Q']
142 if dynamic:
143 if options.debug_libs:
144 sdk_lib_dir = os.path.join(NACL_SDK_ROOT, 'lib',
145 'glibc_%s' % arch_suffix, 'Debug')
146 else:
147 sdk_lib_dir = os.path.join(NACL_SDK_ROOT, 'lib',
148 'glibc_%s' % arch_suffix, 'Release')
149 toolchain = '%s_x86_glibc' % osname
150 toolchain_dir = os.path.join(NACL_SDK_ROOT, 'toolchain', toolchain)
151 lib_dir = os.path.join(toolchain_dir, 'x86_64-nacl')
152 if arch == 'x86-64':
153 lib_dir = os.path.join(lib_dir, 'lib')
154 usr_lib_dir = os.path.join(toolchain_dir, 'x86_64-nacl', 'usr', 'lib')
155 else:
156 lib_dir = os.path.join(lib_dir, 'lib32')
157 usr_lib_dir = os.path.join(toolchain_dir, 'i686-nacl', 'usr', 'lib')
158 ldso = os.path.join(lib_dir, 'runnable-ld.so')
159 cmd.append(ldso)
160 Log('LD.SO = %s' % ldso)
161 libpath = [usr_lib_dir, sdk_lib_dir, lib_dir]
162 if options.library_path:
163 libpath.extend([os.path.abspath(p) for p
164 in options.library_path.split(':')])
165 cmd.append('--library-path')
166 cmd.append(':'.join(libpath))
169 # Append arguments for the executable itself.
170 cmd.append(options.executable)
171 cmd += options.args
173 Log(cmd)
174 return subprocess.call(cmd)
177 if __name__ == '__main__':
178 try:
179 sys.exit(main(sys.argv[1:]))
180 except Error as e:
181 sys.stderr.write(str(e) + '\n')
182 sys.exit(1)