Make ncval-annotate and ncval-stubout executable
[nativeclient.git] / common / nacl_util.py
blobb27a6bc011751929670f993396103045d3feb267
1 #!/usr/bin/python
2 # Copyright 2008, Google Inc.
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
8 #
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
14 # distribution.
15 # * Neither the name of Google Inc. nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 import os
32 import sys
34 def GetBase():
35 base = os.getcwd().split(os.path.sep + "googleclient" + os.path.sep).pop(0)
36 return os.path.join(base, "googleclient", "native_client")
38 def GetSelLdr(target):
39 sys_platform = sys.platform.lower()
40 if sys_platform in ("linux", "linux2"):
41 platform = "linux"
42 sel_ldr = "sel_ldr"
43 elif sys_platform in ("win", "win32", "windows", "cygwin"):
44 platform = "win"
45 sel_ldr = "sel_ldr.exe"
46 elif sys_platform in ("darwin", "mac"):
47 platform = "mac"
48 sel_ldr = "sel_ldr"
49 else:
50 print "ERROR: Unsupported platform for nacl demo!"
51 return None
53 sel_ldr = os.path.join(GetBase(), "scons-out",
54 target + "-" + platform, "staging", sel_ldr)
55 if not os.path.exists(sel_ldr):
56 return None
57 return sel_ldr
60 def FindSelLdr(target):
61 if target != None:
62 # target explicitly specified
63 sel_ldr = GetSelLdr(target)
64 else:
65 # target not specified, so look for dbg first, then opt
66 sel_ldr = GetSelLdr("dbg")
67 if sel_ldr == None:
68 sel_ldr = GetSelLdr("opt")
69 if sel_ldr == None:
70 print "ERROR: Cannot locate sel_ldr executable"
71 print " See documentation for SCons build instructions"
72 return sel_ldr
75 def GetExe(target):
76 scons_target = os.path.join(GetBase(), "scons-out", "nacl", "staging", target)
77 if os.path.exists(scons_target):
78 return scons_target
79 print ("ERROR: Cannot find Native Client executable at %s" % scons_target)
80 print (" See documentation for SCons build instructions")
81 return None
84 def SubProcessCall(*command):
85 arglist = command[0]
86 ret_val = os.spawnv(os.P_WAIT, arglist[0], arglist)
87 return ret_val
90 def GetArgs(default_args):
91 if len(sys.argv) > 1:
92 return sys.argv[1:]
93 return default_args
96 def LaunchSelLdr(demo_exe, args, sel_ldr_target = None):
97 sel_ldr = FindSelLdr(sel_ldr_target)
98 if not sel_ldr or not demo_exe:
99 return -1
100 print "Launching sel_ldr at %s" % sel_ldr
101 print "Using executable at %s" % demo_exe
102 print "Args: %s" % repr(args)
103 command = [sel_ldr, "-d", "-f", demo_exe]
104 if args != None:
105 if len(args) > 0:
106 command = command + ["--"] + args
107 ret_val = SubProcessCall(command)
108 return ret_val
111 if __name__ == '__main__':
112 assert len(sys.argv) > 1
113 nexe_name = sys.argv[1]
114 del sys.argv[1]
115 sys.exit(LaunchSelLdr(GetExe(nexe_name), GetArgs(nexe_args)))