blob: a39b63bc51835d0b66a9632e1222f1760d0b0f06 [file] [log] [blame]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07001#!/usr/bin/python
2#
Raghu Gandham1fa0d842012-01-27 17:51:42 -08003# this tool is used to generate the syscall assembler templates
4# to be placed into arch-{arm,x86,mips}/syscalls, as well as the content
5# of arch-{arm,x86,mips}/linux/_syscalls.h
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07006#
7
The Android Open Source Project4e468ed2008-12-17 18:03:48 -08008import sys, os.path, glob, re, commands, filecmp, shutil
Raghu Gandham1fa0d842012-01-27 17:51:42 -08009import getpass
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070010
11from bionic_utils import *
12
13# set this to 1 if you want to generate thumb stubs
14gen_thumb_stubs = 0
15
16# set this to 1 if you want to generate ARM EABI stubs
17gen_eabi_stubs = 1
18
19# get the root Bionic directory, simply this script's dirname
20#
21bionic_root = find_bionic_root()
22if not bionic_root:
23 print "could not find the Bionic root directory. aborting"
24 sys.exit(1)
25
26if bionic_root[-1] != '/':
27 bionic_root += "/"
28
29print "bionic_root is %s" % bionic_root
30
31# temp directory where we store all intermediate files
32bionic_temp = "/tmp/bionic_gensyscalls/"
33
34# all architectures, update as you see fit
Raghu Gandham1fa0d842012-01-27 17:51:42 -080035all_archs = [ "arm", "x86", "mips" ]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070036
37def make_dir( path ):
Raghu Gandham1fa0d842012-01-27 17:51:42 -080038 path = os.path.abspath(path)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070039 if not os.path.exists(path):
40 parent = os.path.dirname(path)
41 if parent:
42 make_dir(parent)
43 os.mkdir(path)
44
45def create_file( relpath ):
46 dir = os.path.dirname( bionic_temp + relpath )
47 make_dir(dir)
48 return open( bionic_temp + relpath, "w" )
49
50# x86 assembler templates for each syscall stub
51#
52
53x86_header = """/* autogenerated by gensyscalls.py */
Elliott Hughes7582a9c2013-02-06 17:08:15 -080054#include <machine/asm.h>
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070055#include <sys/linux-syscalls.h>
56
Elliott Hughes7582a9c2013-02-06 17:08:15 -080057ENTRY(%(fname)s)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070058"""
59
60x86_registers = [ "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp" ]
61
62x86_call = """ movl $%(idname)s, %%eax
63 int $0x80
64 cmpl $-129, %%eax
65 jb 1f
66 negl %%eax
67 pushl %%eax
68 call __set_errno
69 addl $4, %%esp
70 orl $-1, %%eax
711:
72"""
73
74x86_return = """ ret
Elliott Hughes7582a9c2013-02-06 17:08:15 -080075END(%(fname)s)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070076"""
77
78# ARM assembler templates for each syscall stub
79#
80arm_header = """/* autogenerated by gensyscalls.py */
Kenny Rootf540c032011-02-17 10:31:30 -080081#include <machine/asm.h>
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070082#include <sys/linux-syscalls.h>
83
Kenny Rootf540c032011-02-17 10:31:30 -080084ENTRY(%(fname)s)
85"""
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070086
Kenny Rootf540c032011-02-17 10:31:30 -080087arm_footer = """\
88END(%(fname)s)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070089"""
90
91arm_call_default = arm_header + """\
92 swi #%(idname)s
93 movs r0, r0
94 bxpl lr
95 b __set_syscall_errno
Kenny Rootf540c032011-02-17 10:31:30 -080096""" + arm_footer
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070097
98arm_call_long = arm_header + """\
99 .save {r4, r5, lr}
100 stmfd sp!, {r4, r5, lr}
101 ldr r4, [sp, #12]
102 ldr r5, [sp, #16]
103 swi # %(idname)s
104 ldmfd sp!, {r4, r5, lr}
105 movs r0, r0
106 bxpl lr
107 b __set_syscall_errno
Kenny Rootf540c032011-02-17 10:31:30 -0800108""" + arm_footer
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700109
110arm_eabi_call_default = arm_header + """\
Matthieu Castetfaa0fdb2013-01-16 14:02:50 +0100111 mov ip, r7
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700112 ldr r7, =%(idname)s
113 swi #0
Matthieu Castetfaa0fdb2013-01-16 14:02:50 +0100114 mov r7, ip
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700115 movs r0, r0
116 bxpl lr
117 b __set_syscall_errno
Kenny Rootf540c032011-02-17 10:31:30 -0800118""" + arm_footer
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700119
120arm_eabi_call_long = arm_header + """\
121 mov ip, sp
122 .save {r4, r5, r6, r7}
123 stmfd sp!, {r4, r5, r6, r7}
124 ldmfd ip, {r4, r5, r6}
125 ldr r7, =%(idname)s
126 swi #0
127 ldmfd sp!, {r4, r5, r6, r7}
128 movs r0, r0
129 bxpl lr
130 b __set_syscall_errno
Kenny Rootf540c032011-02-17 10:31:30 -0800131""" + arm_footer
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700132
133# ARM thumb assembler templates for each syscall stub
134#
135thumb_header = """/* autogenerated by gensyscalls.py */
136 .text
137 .type %(fname)s, #function
138 .globl %(fname)s
139 .align 4
140 .thumb_func
141 .fnstart
142
143#define __thumb__
144#include <sys/linux-syscalls.h>
145
146
147%(fname)s:
148"""
149
150thumb_call_default = thumb_header + """\
151 .save {r7,lr}
152 push {r7,lr}
153 ldr r7, =%(idname)s
154 swi #0
155 tst r0, r0
156 bmi 1f
157 pop {r7,pc}
1581:
159 neg r0, r0
160 ldr r1, =__set_errno
161 blx r1
162 pop {r7,pc}
163 .fnend
164"""
165
166thumb_call_long = thumb_header + """\
167 .save {r4,r5,r7,lr}
168 push {r4,r5,r7,lr}
169 ldr r4, [sp,#16]
170 ldr r5, [sp,#20]
171 ldr r7, =%(idname)s
172 swi #0
173 tst r0, r0
174 bmi 1f
175 pop {r4,r5,r7,pc}
1761:
177 neg r0, r0
178 ldr r1, =__set_errno
179 blx r1
180 pop {r4,r5,r7,pc}
181 .fnend
182"""
183
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800184# mips assembler templates for each syscall stub
185#
186mips_call = """/* autogenerated by gensyscalls.py */
187#include <sys/linux-syscalls.h>
188 .text
189 .globl %(fname)s
190 .align 4
191 .ent %(fname)s
192
193%(fname)s:
194 .set noreorder
195 .cpload $t9
196 li $v0, %(idname)s
197 syscall
198 bnez $a3, 1f
199 move $a0, $v0
200 j $ra
201 nop
2021:
203 la $t9,__set_errno
204 j $t9
205 nop
206 .set reorder
207 .end %(fname)s
208"""
209
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100210def param_uses_64bits(param):
211 """Returns True iff a syscall parameter description corresponds
212 to a 64-bit type."""
213 param = param.strip()
214 # First, check that the param type begins with one of the known
215 # 64-bit types.
216 if not ( \
217 param.startswith("int64_t") or param.startswith("uint64_t") or \
218 param.startswith("loff_t") or param.startswith("off64_t") or \
219 param.startswith("long long") or param.startswith("unsigned long long") or
220 param.startswith("signed long long") ):
221 return False
222
223 # Second, check that there is no pointer type here
224 if param.find("*") >= 0:
225 return False
226
227 # Ok
228 return True
229
230def count_arm_param_registers(params):
231 """This function is used to count the number of register used
232 to pass parameters when invoking a thumb or ARM system call.
233 This is because the ARM EABI mandates that 64-bit quantities
234 must be passed in an even+odd register pair. So, for example,
235 something like:
236
237 foo(int fd, off64_t pos)
238
239 would actually need 4 registers:
240 r0 -> int
241 r1 -> unused
242 r2-r3 -> pos
243 """
244 count = 0
245 for param in params:
246 if param_uses_64bits(param):
247 if (count & 1) != 0:
248 count += 1
249 count += 2
250 else:
251 count += 1
252 return count
253
254def count_generic_param_registers(params):
255 count = 0
256 for param in params:
257 if param_uses_64bits(param):
258 count += 2
259 else:
260 count += 1
261 return count
262
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700263class State:
264 def __init__(self):
265 self.old_stubs = []
266 self.new_stubs = []
267 self.other_files = []
268 self.syscalls = []
269
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800270 def x86_genstub(self, fname, numparams, idname):
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700271 t = { "fname" : fname,
272 "idname" : idname }
273
274 result = x86_header % t
275 stack_bias = 4
276 for r in range(numparams):
277 result += " pushl " + x86_registers[r] + "\n"
278 stack_bias += 4
279
280 for r in range(numparams):
281 result += " mov %d(%%esp), %s" % (stack_bias+r*4, x86_registers[r]) + "\n"
282
283 result += x86_call % t
284
285 for r in range(numparams):
286 result += " popl " + x86_registers[numparams-r-1] + "\n"
287
Elliott Hughes7582a9c2013-02-06 17:08:15 -0800288 result += x86_return % t
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700289 return result
290
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800291 def x86_genstub_cid(self, fname, numparams, idname, cid):
292 # We'll ignore numparams here because in reality, if there is a
293 # dispatch call (like a socketcall syscall) there are actually
294 # only 2 arguments to the syscall and 2 regs we have to save:
295 # %ebx <--- Argument 1 - The call id of the needed vectored
296 # syscall (socket, bind, recv, etc)
297 # %ecx <--- Argument 2 - Pointer to the rest of the arguments
298 # from the original function called (socket())
299 t = { "fname" : fname,
300 "idname" : idname }
301
302 result = x86_header % t
303 stack_bias = 4
304
305 # save the regs we need
306 result += " pushl %ebx" + "\n"
307 stack_bias += 4
308 result += " pushl %ecx" + "\n"
309 stack_bias += 4
310
311 # set the call id (%ebx)
312 result += " mov $%d, %%ebx" % (cid) + "\n"
313
314 # set the pointer to the rest of the args into %ecx
315 result += " mov %esp, %ecx" + "\n"
316 result += " addl $%d, %%ecx" % (stack_bias) + "\n"
317
318 # now do the syscall code itself
319 result += x86_call % t
320
321 # now restore the saved regs
322 result += " popl %ecx" + "\n"
323 result += " popl %ebx" + "\n"
324
325 # epilog
Elliott Hughes7582a9c2013-02-06 17:08:15 -0800326 result += x86_return % t
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800327 return result
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700328
329 def arm_genstub(self,fname, flags, idname):
330 t = { "fname" : fname,
331 "idname" : idname }
332 if flags:
333 numargs = int(flags)
334 if numargs > 4:
335 return arm_call_long % t
336 return arm_call_default % t
337
338
339 def arm_eabi_genstub(self,fname, flags, idname):
340 t = { "fname" : fname,
341 "idname" : idname }
342 if flags:
343 numargs = int(flags)
344 if numargs > 4:
345 return arm_eabi_call_long % t
346 return arm_eabi_call_default % t
347
348
349 def thumb_genstub(self,fname, flags, idname):
350 t = { "fname" : fname,
351 "idname" : idname }
352 if flags:
353 numargs = int(flags)
354 if numargs > 4:
355 return thumb_call_long % t
356 return thumb_call_default % t
357
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800358 def mips_genstub(self,fname, idname):
359 t = { "fname" : fname,
360 "idname" : idname }
361 return mips_call % t
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700362
363 def process_file(self,input):
364 parser = SysCallsTxtParser()
365 parser.parse_file(input)
366 self.syscalls = parser.syscalls
367 parser = None
368
369 for t in self.syscalls:
370 syscall_func = t["func"]
371 syscall_params = t["params"]
372 syscall_name = t["name"]
373
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800374 if t["common"] >= 0 or t["armid"] >= 0:
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100375 num_regs = count_arm_param_registers(syscall_params)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700376 if gen_thumb_stubs:
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100377 t["asm-thumb"] = self.thumb_genstub(syscall_func,num_regs,"__NR_"+syscall_name)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700378 else:
379 if gen_eabi_stubs:
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100380 t["asm-arm"] = self.arm_eabi_genstub(syscall_func,num_regs,"__NR_"+syscall_name)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700381 else:
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100382 t["asm-arm"] = self.arm_genstub(syscall_func,num_regs,"__NR_"+syscall_name)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700383
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800384 if t["common"] >= 0 or t["x86id"] >= 0:
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100385 num_regs = count_generic_param_registers(syscall_params)
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800386 if t["cid"] >= 0:
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100387 t["asm-x86"] = self.x86_genstub_cid(syscall_func, num_regs, "__NR_"+syscall_name, t["cid"])
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800388 else:
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100389 t["asm-x86"] = self.x86_genstub(syscall_func, num_regs, "__NR_"+syscall_name)
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800390 elif t["cid"] >= 0:
391 E("cid for dispatch syscalls is only supported for x86 in "
392 "'%s'" % syscall_name)
393 return
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800394 if t["common"] >= 0 or t["mipsid"] >= 0:
395 t["asm-mips"] = self.mips_genstub(syscall_func,"__NR_"+syscall_name)
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800396
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700397
398 def gen_NR_syscall(self,fp,name,id):
399 fp.write( "#define __NR_%-25s (__NR_SYSCALL_BASE + %d)\n" % (name,id) )
400
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800401 # now dump the content of linux-syscalls.h
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700402 def gen_linux_syscalls_h(self):
403 path = "include/sys/linux-syscalls.h"
404 D( "generating "+path )
405 fp = create_file( path )
406 fp.write( "/* auto-generated by gensyscalls.py, do not touch */\n" )
Elliott Hughes19285232012-05-09 16:34:11 -0700407 fp.write( "#ifndef _BIONIC_LINUX_SYSCALLS_H_\n" )
408 fp.write( "#define _BIONIC_LINUX_SYSCALLS_H_\n\n" )
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800409 fp.write( "#if !defined __ASM_ARM_UNISTD_H && !defined __ASM_I386_UNISTD_H && !defined __ASM_MIPS_UNISTD_H\n" )
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700410 fp.write( "#if defined __arm__ && !defined __ARM_EABI__ && !defined __thumb__\n" )
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800411 fp.write( " # define __NR_SYSCALL_BASE 0x900000\n" )
412 fp.write( "#elif defined(__mips__)\n" )
413 fp.write( " # define __NR_SYSCALL_BASE 4000\n" )
414 fp.write( "#else\n" )
415 fp.write( " # define __NR_SYSCALL_BASE 0\n" )
416 fp.write( "#endif\n\n" )
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700417
418 # first, all common syscalls
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800419 for sc in sorted(self.syscalls,key=lambda x:x["common"]):
420 sc_id = sc["common"]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700421 sc_name = sc["name"]
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800422 if sc_id >= 0:
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700423 self.gen_NR_syscall( fp, sc_name, sc_id )
424
425 # now, all arm-specific syscalls
426 fp.write( "\n#ifdef __arm__\n" );
427 for sc in self.syscalls:
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800428 sc_id = sc["armid"]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700429 sc_name = sc["name"]
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800430 if sc_id >= 0:
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700431 self.gen_NR_syscall( fp, sc_name, sc_id )
432 fp.write( "#endif\n" );
433
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800434 gen_syscalls = {}
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700435 # finally, all i386-specific syscalls
436 fp.write( "\n#ifdef __i386__\n" );
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800437 for sc in sorted(self.syscalls,key=lambda x:x["x86id"]):
438 sc_id = sc["x86id"]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700439 sc_name = sc["name"]
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800440 if sc_id >= 0 and sc_name not in gen_syscalls:
441 self.gen_NR_syscall( fp, sc_name, sc_id )
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800442 gen_syscalls[sc_name] = True
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700443 fp.write( "#endif\n" );
444
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800445 # all mips-specific syscalls
446 fp.write( "\n#ifdef __mips__\n" );
447 for sc in sorted(self.syscalls,key=lambda x:x["mipsid"]):
448 sc_id = sc["mipsid"]
449 if sc_id >= 0:
450 self.gen_NR_syscall( fp, sc["name"], sc_id )
451 fp.write( "#endif\n" );
452
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700453 fp.write( "\n#endif\n" )
454 fp.write( "\n#endif /* _BIONIC_LINUX_SYSCALLS_H_ */\n" );
455 fp.close()
456 self.other_files.append( path )
457
458
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700459 # now dump the contents of syscalls.mk
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800460 def gen_arch_syscalls_mk(self, arch):
461 path = "arch-%s/syscalls.mk" % arch
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700462 D( "generating "+path )
463 fp = create_file( path )
464 fp.write( "# auto-generated by gensyscalls.py, do not touch\n" )
465 fp.write( "syscall_src := \n" )
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800466 arch_test = {
467 "arm": lambda x: x.has_key("asm-arm") or x.has_key("asm-thumb"),
Shin-ichiro KAWASAKIce0595d2009-09-01 19:03:06 +0900468 "x86": lambda x: x.has_key("asm-x86"),
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800469 "mips": lambda x: x.has_key("asm-mips")
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800470 }
471
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700472 for sc in self.syscalls:
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800473 if arch_test[arch](sc):
474 fp.write("syscall_src += arch-%s/syscalls/%s.S\n" %
475 (arch, sc["func"]))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700476 fp.close()
477 self.other_files.append( path )
478
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800479
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700480 # now generate each syscall stub
481 def gen_syscall_stubs(self):
482 for sc in self.syscalls:
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800483 if sc.has_key("asm-arm") and 'arm' in all_archs:
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700484 fname = "arch-arm/syscalls/%s.S" % sc["func"]
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200485 D2( ">>> generating "+fname )
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700486 fp = create_file( fname )
487 fp.write(sc["asm-arm"])
488 fp.close()
489 self.new_stubs.append( fname )
490
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800491 if sc.has_key("asm-thumb") and 'arm' in all_archs:
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700492 fname = "arch-arm/syscalls/%s.S" % sc["func"]
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200493 D2( ">>> generating "+fname )
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700494 fp = create_file( fname )
495 fp.write(sc["asm-thumb"])
496 fp.close()
497 self.new_stubs.append( fname )
498
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800499 if sc.has_key("asm-x86") and 'x86' in all_archs:
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700500 fname = "arch-x86/syscalls/%s.S" % sc["func"]
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200501 D2( ">>> generating "+fname )
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700502 fp = create_file( fname )
503 fp.write(sc["asm-x86"])
504 fp.close()
505 self.new_stubs.append( fname )
506
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800507 if sc.has_key("asm-mips") and 'mips' in all_archs:
508 fname = "arch-mips/syscalls/%s.S" % sc["func"]
509 D2( ">>> generating "+fname )
510 fp = create_file( fname )
511 fp.write(sc["asm-mips"])
512 fp.close()
513 self.new_stubs.append( fname )
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700514
515 def regenerate(self):
516 D( "scanning for existing architecture-specific stub files" )
517
518 bionic_root_len = len(bionic_root)
519
520 for arch in all_archs:
521 arch_path = bionic_root + "arch-" + arch
522 D( "scanning " + arch_path )
523 files = glob.glob( arch_path + "/syscalls/*.S" )
524 for f in files:
525 self.old_stubs.append( f[bionic_root_len:] )
526
527 D( "found %d stub files" % len(self.old_stubs) )
528
529 if not os.path.exists( bionic_temp ):
530 D( "creating %s" % bionic_temp )
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800531 make_dir( bionic_temp )
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700532
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700533 D( "re-generating stubs and support files" )
534
535 self.gen_linux_syscalls_h()
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800536 for arch in all_archs:
537 self.gen_arch_syscalls_mk(arch)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700538 self.gen_syscall_stubs()
539
540 D( "comparing files" )
541 adds = []
542 edits = []
543
544 for stub in self.new_stubs + self.other_files:
545 if not os.path.exists( bionic_root + stub ):
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200546 # new file, git add it
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700547 D( "new file: " + stub)
548 adds.append( bionic_root + stub )
549 shutil.copyfile( bionic_temp + stub, bionic_root + stub )
550
551 elif not filecmp.cmp( bionic_temp + stub, bionic_root + stub ):
552 D( "changed file: " + stub)
553 edits.append( stub )
554
555 deletes = []
556 for stub in self.old_stubs:
557 if not stub in self.new_stubs:
558 D( "deleted file: " + stub)
559 deletes.append( bionic_root + stub )
560
561
562 if adds:
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200563 commands.getoutput("git add " + " ".join(adds))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700564 if deletes:
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200565 commands.getoutput("git rm " + " ".join(deletes))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700566 if edits:
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700567 for file in edits:
568 shutil.copyfile( bionic_temp + file, bionic_root + file )
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200569 commands.getoutput("git add " +
570 " ".join((bionic_root + file) for file in edits))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700571
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200572 commands.getoutput("git add %s%s" % (bionic_root,"SYSCALLS.TXT"))
573
574 if (not adds) and (not deletes) and (not edits):
575 D("no changes detected!")
576 else:
577 D("ready to go!!")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700578
579D_setlevel(1)
580
581state = State()
582state.process_file(bionic_root+"SYSCALLS.TXT")
583state.regenerate()