-
Notifications
You must be signed in to change notification settings - Fork 1
/
cudalt.py
executable file
·95 lines (76 loc) · 2.16 KB
/
cudalt.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
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
# libtoolish hack: compile a .cu file like libtool does
# adapted from http://www.clusterchimps.org
import sys
import os
libtool_path = sys.argv[1]
lo_filepath = sys.argv[2]
other_args = sys.argv[3:]
o_filepath = lo_filepath.replace(".lo", ".o")
try:
i = o_filepath.rindex("/")
lo_dir = o_filepath[0:i+1]
o_filename = o_filepath[i+1:]
except ValueError:
lo_dir = ""
o_filename = o_filepath
local_pic_dir = ".libs/"
local_npic_dir = ""
pic_dir = lo_dir + local_pic_dir
npic_dir = lo_dir + local_npic_dir
pic_filepath = pic_dir + o_filename
npic_filepath = npic_dir + o_filename
local_pic_filepath = local_pic_dir + o_filename
local_npic_filepath = local_npic_dir + o_filename
def quote(s):
if " " in s:
return "\"" + s + "\""
else:
return s
# Make lib dir
try:
os.mkdir(pic_dir)
except OSError:
pass
# generate the command to compile the .cu for shared library
args = other_args[:]
args.extend(["-Xcompiler","-fPIC"]) # position indep code
args.append("-o")
args.append(pic_filepath)
command = " ".join(quote(a) for a in args)
print "[cudaalt.py] " + command
# compile the .cu
rv = os.system(command)
if rv != 0:
sys.exit(1)
# generate the command to compile the .cu for static library
args = other_args[:]
args.append("-o")
args.append(npic_filepath)
command = " ".join(quote(a) for a in args)
print "[cudaalt.py] " + command
# compile the .cu
rv = os.system(command)
if rv != 0:
sys.exit(1)
# get libtool version
fd = os.popen(libtool_path + " --version")
libtool_version = fd.readline()
def read_whole_stream(stream):
for _ in stream:
pass
# suppress any broken pipe error by reading the whole stream
read_whole_stream(fd)
fd.close()
# generate the .lo file
f = open(lo_filepath, "w")
f.write("# " + lo_filepath + " - a libtool object file\n")
f.write("# Generated by " + libtool_version + "\n")
f.write("#\n")
f.write("# Please DO NOT delete this file!\n")
f.write("# It is necessary for linking the library.\n\n")
f.write("# Name of the PIC object.\n")
f.write("pic_object='" + local_pic_filepath + "'\n\n")
f.write("# Name of the non-PIC object.\n")
f.write("non_pic_object='" + local_npic_filepath + "'\n")
f.close()
sys.exit(0)