-
Notifications
You must be signed in to change notification settings - Fork 0
/
linker.py
61 lines (43 loc) · 1.43 KB
/
linker.py
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
import os
import subprocess
from tool import Tool
class Linker(Tool):
""" A tool that links object files together into libraries or binaries. """
def __init__(self, binary):
Tool.__init__(self, 'Link', binary)
print binary
def execute(self, inputs, outputs, paths, flags):
print "Binary: ", self.binhash, "(but what about deps?)"
print "Flags: ", self.hashstore.hash_string(' '.join(flags))
print "Paths: ", [ self.hashstore.hash_string(p) for p in paths ]
if len(outputs) != 1:
raise Exception, 'Linker generates one file at a time, not %d' % (
len(outputs)
)
print 'Dependencies:'
libdeps = self.parse_libs(inputs)
for j in libdeps:
print "%33s: %s" % (j, self.hashstore.hash_file(j))
print
libdirs = [ '-I %s' % p for p in paths ]
argv = [ 'clang' ] + flags + libdirs + [ '-o' ] + outputs + inputs
print "Executing: '%s'" % (' '.join(argv))
return_code = subprocess.check_call(
argv,
stdout = subprocess.devnull,
stderr = subprocess.devnull,
)
def parse_libs(self, files):
assert type(files) == list
try:
# Ask clang to trace library dependencies.
output = subprocess.check_output(
[ 'clang', '-dynamiclib', '-t' ] + files,
stderr = subprocess.devnull,
)
libs = output.split('\n')
return [ l.strip() for l in libs if len(l) > 0 ]
except subprocess.CalledProcessError, e:
print 'Error:', e
pass # No library dependencies
return libs