-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
170 lines (135 loc) · 4.15 KB
/
utils.py
File metadata and controls
170 lines (135 loc) · 4.15 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import json
import os
import sys
from termcolor import colored
from multiprocessing import Lock
from hashlib import sha1
from copy import deepcopy
settings = {
"callpass_library_path": "./libcallpass.so",
"object_dir": "objects",
"debug": True,
"original_cxx_executable": "/usr/bin/c++",
"original_cc_executable": "/usr/bin/cc",
"targeted_cxx_executable": "/usr/bin/clang++",
"targeted_cc_executable": "/usr/bin/clang",
"llvm_link_executable": "/usr/bin/llvm-link",
"preserve_process": "sha1.json",
"toposort_verbose_logging_dir": ""
}
iolock = Lock()
def loadSettings(fp):
custSettings = json.load(fp)
for i in custSettings.keys():
settings[i] = custSettings[i]
def GET(name):
if name in settings:
return settings[name]
return None
class Console():
@staticmethod
def info(*st):
with iolock:
print(colored("[INFO]", "blue"), *st)
@staticmethod
def warn(*st):
with iolock:
print(colored("[WARN]", "yellow"), *st)
@staticmethod
def error(*st):
with iolock:
print(colored("[ERRR]", "red"), *st)
@staticmethod
def log(*st):
Console.info(*st)
@staticmethod
def success(*st):
with iolock:
print(colored("[SUCC]", "green"), *st)
@staticmethod
def debug(*st):
if GET("debug"):
with iolock:
print("[DEBG]", *st)
def checkDir(subdir, name):
if not os.access(subdir, os.W_OK):
Console.warn(name + " directory unreadable. Trying to create.")
try:
os.mkdir(subdir)
if not os.access(subdir, os.W_OK):
raise IOError
except:
Console.error(name + " directory unwritable")
sys.exit(1)
if len(os.listdir(subdir)) != 0:
Console.warn(name + " directory is not empty!")
def hasNoIndirectDependcies(deps):
for i in deps:
if len(i) > 2 and i[-2:] == ".o":
continue
else:
return False
return True
def findName(path):
return path.split("/")[-1]
def findNames(path):
return list(map(findName, path))
def pathToSha1(path, table):
names = []
for i in path:
if i[-2:] == ".a" or \
i[-2:] == ".o" or \
i[-3:] == ".so":
hashName = sha1sum(i)
table[hashName] = i
names.append(hashName)
return names
def getllvmLinkCmd(destPath, deps):
return GET("llvm_link_executable") + " " + " ".join(deps) + " -S -o " + destPath
def sha1sum(text):
return sha1(text.encode()).hexdigest()
def deduplicate(items):
ret = []
for i in items:
if i not in ret:
ret.append(i)
return ret
def hasNoDependency(fullpath):
return fullpath[-2:] == ".o"
def topoSort(targets, excludes, table):
dup = deepcopy(targets)
seq = []
for idx, key in enumerate(dup):
dup[key] = [r for r in dup[key] if r not in excludes]
keyLen = len(dup.keys())
rnd = 0
logDir = GET("toposort_verbose_logging_dir")
logging = True if logDir != "" and logDir != None else False
if logging:
Console.warn(
"Toposort verbose logging is on. Logs will be saved to {}/. Be cautious since it will be a lot of data.".format(
logDir))
while len(seq) < keyLen:
if logging:
json.dump(unravel(dup, table), open(
logDir + "/" + str(rnd) + ".rest", "w"))
json.dump(list(map(lambda x: table[x], seq)), open(
logDir + "/" + str(rnd) + ".curr", "w"))
removePending = []
for i in dup.copy():
if len(dup[i]) == 0:
seq.append(i)
removePending.append(i)
del dup[i]
if len(removePending) == 0:
Console.debug("Current queue:", seq)
raise ValueError
for idx, key in enumerate(dup):
dup[key] = [r for r in dup[key] if r not in removePending]
rnd += 1
return seq
def unravel(obj, table):
newobj = {}
for i in obj.keys():
newobj[table[i]] = list(map(lambda x: table[x], obj[i]))
return newobj