-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.py
More file actions
executable file
·123 lines (94 loc) · 2.91 KB
/
build.py
File metadata and controls
executable file
·123 lines (94 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python3
import os
#os.system('pwd')
#os.system('rm *')
import subprocess
import code
import binascii
def chain_functions(fs):
for f,ouf,errf in fs:
d = subprocess.call(f,stdout=ouf,stderr=errf)
#print(d,f)
if d != 0:
#exit()
return False
return True
devnull = open('/dev/null','w')
with open('correspond_table.txt','r') as fp:
corresp_table = fp.read()
def convert(path):
global devnull
global corresp_table
if path in corresp_table:
return
bfn = subprocess.check_output(['sha1sum',path]).decode().split(' ')[0]
#bfn = binascii.hexlify(path.encode()).decode()
fn = bfn
with open('../correspond_table.txt','a') as fp:
fp.write('%s | %s\n' % (path,bfn))
print(path)
d = subprocess.call('clang -E %s > /dev/null 2> /dev/null' % path,shell=True)
if d == 0:
d = subprocess.call('clang -E %s | grep -v "#" | grep -v "^[[:space:]]*$" > %s.pp.c 2> /dev/null' % (path,fn),shell=True)
if d == 0 and int(list(filter(lambda x: x!= '',subprocess.check_output(['wc','%s.pp.c' % fn]).decode().split(' ')))[0]) > 5000:
# too big.
d = 1
if d == 0:
d = subprocess.call('clang -Xclang -dump-tokens -fsyntax-only %s.pp.c > /dev/null 2> %s.tokenized' % (fn,fn),shell=True)
with open('%s.tokenized' % fn,"r") as fp:
s = fp.read()
if '__asm__' in s: #避けたい...
d = -1
ts = ""
for cs in s.split('\n'):
#print(cs)
cs = "'".join(cs.split("'")[1:-1])
ts += cs + '\n'
with open('%s.tokenized.c' % fn,"w") as fp:
fp.write(ts)
#print('tokenized',path)
tcfn = '%s.tokenized.c' % fn
sfn = '%s.s' % fn
ofn = '%s.o' % fn
with open('%s.parsed' % fn,"w") as parsedfp:
with open('%s.objd' % fn,"w") as objdfp:
isok = chain_functions([
(['gcc','-O0','-S','-g','-c',tcfn,'-o',sfn] ,devnull,devnull),
(['as',sfn,'-o',ofn] ,devnull,devnull),
(['clang','-Xclang','-ast-dump','-fsyntax-only',tcfn],parsedfp,devnull),
(['objdump','-d','-M','intel','-w',ofn] ,objdfp,devnull),
])
parsedfp.close()
objdfp.close()
if isok:
print('convert',path,'to',fn)
else:
#pass
os.system('rm %s.*' % fn)
def enumerate_src(path):
global table
print('search at',path)
mems = '%s | SEARCHED\n' % path
if mems in corresp_table:
return
for fn in os.listdir(path):
rfn = os.path.join(path,fn)
if rfn[-2:]=='.c' and (not os.path.isdir(rfn)):
convert(rfn)
if os.path.isdir(rfn):
enumerate_src(rfn)
with open('../correspond_table.txt','a') as fp:
fp.write(mems)
"""
#for dirn in ['xv6-public/','git/']:
for dirn in ['nginx/src/%s/' % s for s in ['core','event','http','mail','stream']]:
os.system('yes | rm -r src/')
os.system('cp -r ../../corpus_folders/%s src' % dirn)
fns = subprocess.check_output(['ls','./src/']).decode().split('\\n')
#code.interact(local={'fns':fns})
fns = filter(lambda x: x[-2:]=='.c',fns)
tfns = []
"""
import os
os.chdir('./build')
enumerate_src('../../corpus_folders/')