Autofill: Add WalletIntegrationAvailable() to components.
[chromium-blink-merge.git] / native_client_sdk / src / tools / sel_ldr.py
blob5d1a6220c65acc8c46848eb7ea054bd03d1b8e06
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('--debug-libs', action='store_true',
59 help='For dynamic executables, reference debug '
60 'libraries rather then release')
61 parser.add_argument('executable', help='executable (.nexe) to run')
62 parser.add_argument('args', nargs='*', help='argument to pass to exectuable')
64 # To enable bash completion for this command first install optcomplete
65 # and then add this line to your .bashrc:
66 # complete -F _optcomplete sel_ldr.py
67 try:
68 import optcomplete
69 optcomplete.autocomplete(parser)
70 except ImportError:
71 pass
73 options = parser.parse_args(argv)
75 if options.verbose:
76 Log.verbose = True
78 osname = getos.GetPlatform()
79 if not os.path.exists(options.executable):
80 raise Error('executable not found: %s' % options.executable)
81 if not os.path.isfile(options.executable):
82 raise Error('not a file: %s' % options.executable)
84 arch, dynamic = create_nmf.ParseElfHeader(options.executable)
86 if arch == 'arm' and osname != 'linux':
87 raise Error('Cannot run ARM executables under sel_ldr on ' + osname)
89 arch_suffix = arch.replace('-', '_')
91 sel_ldr = os.path.join(SCRIPT_DIR, 'sel_ldr_%s' % arch_suffix)
92 irt = os.path.join(SCRIPT_DIR, 'irt_core_%s.nexe' % arch_suffix)
93 if osname == 'win':
94 sel_ldr += '.exe'
95 Log('ROOT = %s' % NACL_SDK_ROOT)
96 Log('SEL_LDR = %s' % sel_ldr)
97 Log('IRT = %s' % irt)
98 cmd = [sel_ldr]
100 if osname == 'linux':
101 # Run sel_ldr under nacl_helper_bootstrap
102 helper = os.path.join(SCRIPT_DIR, 'nacl_helper_bootstrap_%s' % arch_suffix)
103 Log('HELPER = %s' % helper)
104 cmd.insert(0, helper)
105 cmd.append('--r_debug=0xXXXXXXXXXXXXXXXX')
106 cmd.append('--reserved_at_zero=0xXXXXXXXXXXXXXXXX')
108 cmd += ['-a', '-B', irt]
110 if options.debug:
111 cmd.append('-g')
113 if not options.verbose:
114 cmd += ['-l', os.devnull]
116 if arch == 'arm':
117 # Use the QEMU arm emulator if available.
118 qemu_bin = FindQemu()
119 if not qemu_bin:
120 raise Error('Cannot run ARM executables under sel_ldr without an emulator'
121 '. Try installing QEMU (http://wiki.qemu.org/).')
123 arm_libpath = os.path.join(NACL_SDK_ROOT, 'tools', 'lib', 'arm_trusted')
124 if not os.path.isdir(arm_libpath):
125 raise Error('Could not find ARM library path: %s' % arm_libpath)
126 qemu = [qemu_bin, '-cpu', 'cortex-a8', '-L', arm_libpath]
127 # '-Q' disables platform qualification, allowing arm binaries to run.
128 cmd = qemu + cmd + ['-Q']
130 if dynamic:
131 if options.debug_libs:
132 libpath = os.path.join(NACL_SDK_ROOT, 'lib',
133 'glibc_%s' % arch_suffix, 'Debug')
134 else:
135 libpath = os.path.join(NACL_SDK_ROOT, 'lib',
136 'glibc_%s' % arch_suffix, 'Release')
137 toolchain = '%s_x86_glibc' % osname
138 sdk_lib_dir = os.path.join(NACL_SDK_ROOT, 'toolchain',
139 toolchain, 'x86_64-nacl')
140 if arch == 'x86-64':
141 sdk_lib_dir = os.path.join(sdk_lib_dir, 'lib')
142 else:
143 sdk_lib_dir = os.path.join(sdk_lib_dir, 'lib32')
144 ldso = os.path.join(sdk_lib_dir, 'runnable-ld.so')
145 cmd.append(ldso)
146 Log('LD.SO = %s' % ldso)
147 libpath += ':' + sdk_lib_dir
148 cmd.append('--library-path')
149 cmd.append(libpath)
152 # Append arguments for the executable itself.
153 cmd.append(options.executable)
154 cmd += options.args
156 Log(cmd)
157 return subprocess.call(cmd)
160 if __name__ == '__main__':
161 try:
162 sys.exit(main(sys.argv[1:]))
163 except Error as e:
164 sys.stderr.write(str(e) + '\n')
165 sys.exit(1)