1 # common python utility routines for the Bionic tool scripts
3 import sys, os, commands, string
5 # support Bionic architectures, add new ones as appropriate
7 bionic_archs = [ "arm", "x86" ]
9 # basic debugging trace support
10 # call D_setlevel to set the verbosity level
11 # and D(), D2(), D3(), D4() to add traces
35 def D_setlevel(level):
40 def find_dir_of(path):
41 '''return the directory name of 'path', or "." if there is none'''
42 # remove trailing slash
43 if len(path) > 1 and path[-1] == '/':
46 # find parent directory name
47 d = os.path.dirname(path)
56 def find_file_from_upwards(from_path,target_file):
57 """find a file in the current directory or its parents. if 'from_path' is None,
58 seach from the current program's directory"""
61 path = find_dir_of(sys.argv[0])
62 D("this script seems to be located in: %s" % path)
68 file = path + "/" + target_file
71 if os.path.isfile(file):
72 D("found %s in %s" % (target_file, path))
78 path = os.path.dirname(path)
83 file = path + target_file
86 if os.path.isfile(file):
87 D("found %s in %s" % (target_file, path))
93 def find_bionic_root():
94 '''find the root of the Bionic source tree. we check for the SYSCALLS.TXT file
95 from the location of the current program's directory.'''
97 # note that we can't use find_file_from_upwards() since we can't use os.path.abspath
98 # that's because in some cases the p4 client is in a symlinked directory, and this
99 # function will return the real path instead, which later creates problems when
100 # p4 commands are issued
102 file = find_file_from_upwards(None, "SYSCALLS.TXT")
104 return os.path.dirname(file)
108 def find_kernel_headers():
109 """try to find the directory containing the kernel headers for this machine"""
110 status, version = commands.getstatusoutput( "uname -r" ) # get Linux kernel version
112 D("could not execute 'uname -r' command properly")
115 # get rid of the "-xenU" suffix that is found in Xen virtual machines
116 if len(version) > 5 and version[-5:] == "-xenU":
117 version = version[:-5]
119 path = "/usr/src/linux-headers-" + version
120 D("probing %s for kernel headers" % (path+"/include"))
121 ret = os.path.isdir( path )
123 D("found kernel headers in: %s" % (path + "/include"))
128 # parser for the SYSCALLS.TXT file
130 class SysCallsTxtParser:
136 print "%d: %s" % (self.lineno, msg)
138 def parse_line(self, line):
139 """ parse a syscall spec line.
141 line processing, format is
142 return type func_name[:syscall_name[:call_id]] ( [paramlist] ) (syscall_number[,syscall_number_x86])|stub
144 pos_lparen = line.find('(')
147 E("missing left parenthesis in '%s'" % line)
150 pos_rparen = line.rfind(')')
151 if pos_rparen < 0 or pos_rparen <= pos_lparen:
152 E("missing or misplaced right parenthesis in '%s'" % line)
155 return_type = line[:pos_lparen].strip().split()
156 if len(return_type) < 2:
157 E("missing return type in '%s'" % line)
160 syscall_func = return_type[-1]
161 return_type = string.join(return_type[:-1],' ')
164 pos_colon = syscall_func.find(':')
166 syscall_name = syscall_func
168 if pos_colon == 0 or pos_colon+1 >= len(syscall_func):
169 E("misplaced colon in '%s'" % line)
172 # now find if there is a call_id for a dispatch-type syscall
173 # after the optional 2nd colon
174 pos_colon2 = syscall_func.find(':', pos_colon + 1)
176 syscall_name = syscall_func[pos_colon+1:]
177 syscall_func = syscall_func[:pos_colon]
179 if pos_colon2+1 >= len(syscall_func):
180 E("misplaced colon2 in '%s'" % line)
182 syscall_name = syscall_func[(pos_colon+1):pos_colon2]
183 call_id = int(syscall_func[pos_colon2+1:])
184 syscall_func = syscall_func[:pos_colon]
186 if pos_rparen > pos_lparen+1:
187 syscall_params = line[pos_lparen+1:pos_rparen].split(',')
188 params = string.join(syscall_params,',')
193 number = line[pos_rparen+1:].strip()
201 number = number[1:].strip()
202 numbers = string.split(number,',')
203 syscall_id = int(numbers[0])
204 syscall_id2 = syscall_id
205 syscall_id3 = syscall_id
207 syscall_id2 = int(numbers[1])
208 syscall_id3 = syscall_id2
210 syscall_id3 = int(numbers[2])
212 E("invalid syscall number in '%s'" % line)
215 print str(syscall_id) + ':' + str(syscall_id2) + ':' + str(syscall_id3)
217 t = { "id" : syscall_id,
221 "name" : syscall_name,
222 "func" : syscall_func,
223 "params" : syscall_params,
224 "decl" : "%-15s %s (%s);" % (return_type, syscall_func, params) }
226 self.syscalls.append(t)
228 def parse_file(self, file_path):
229 D2("parse_file: %s" % file_path)
231 for line in fp.xreadlines():
234 if not line: continue
235 if line[0] == '#': continue
236 self.parse_line(line)
242 def __init__(self,out=sys.stdout):
248 def writeln(self,msg):
258 D2("write '%s'" % msg)
260 def writeln(self,msg):
261 self.line += msg + '\n'
262 D2("write '%s\\n'"% msg)
268 def create_file_path(path):
271 parent = os.path.dirname(path)
279 #print "dir %s" % dir
280 if os.path.isdir(dir):
284 def walk_source_files(paths,callback,args,excludes=[]):
285 """recursively walk a list of paths and files, only keeping the source files in directories"""
287 if not os.path.isdir(path):
290 for root, dirs, files in os.walk(path):
291 #print "w-- %s (ex: %s)" % (repr((root,dirs)), repr(excludes))
297 r, ext = os.path.splitext(f)
298 if ext in [ ".h", ".c", ".cpp", ".S" ]:
299 callback( "%s/%s" % (root,f), args )
301 def cleanup_dir(path):
302 """create a directory if needed, and ensure that it is totally empty
303 by removing any existing content in it"""
304 if not os.path.exists(path):
307 for root, dirs, files in os.walk(path, topdown=False):
308 if root.endswith("kernel_headers/"):
309 # skip 'kernel_headers'
312 os.remove(os.path.join(root, name))
314 os.rmdir(os.path.join(root, name))