44import sys
55import subprocess
66
7+ import os
8+ from .util import tempdir
9+
10+
711def create_shared (output ,
812 objects ,
913 options = None ,
@@ -24,26 +28,85 @@ def create_shared(output,
2428 cc : str, optional
2529 The compile string.
2630 """
31+ if sys .platform == "darwin" or sys .platform .startswith ('linux' ):
32+ _linux_shared (output , objects , options , cc )
33+ elif sys .platform == "win32" :
34+ _windows_shared (output , objects , options )
35+ else :
36+ raise ValueError ("Unsupported platform" )
37+
38+
39+ def _linux_shared (output , objects , options , cc = "g++" ):
2740 cmd = [cc ]
2841 cmd += ["-shared" , "-fPIC" ]
29-
3042 if sys .platform == "darwin" :
3143 cmd += ["-undefined" , "dynamic_lookup" ]
3244 cmd += ["-o" , output ]
33-
3445 if isinstance (objects , str ):
3546 cmd += [objects ]
3647 else :
3748 cmd += objects
38-
3949 if options :
4050 cmd += options
41-
4251 proc = subprocess .Popen (
4352 cmd , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
4453 (out , _ ) = proc .communicate ()
54+ if proc .returncode != 0 :
55+ msg = "Compilation error:\n "
56+ msg += str (out )
57+ raise RuntimeError (msg )
58+
59+
60+ def _windows_shared (output , objects , options ):
61+ cl_cmd = ["cl" ]
62+ cl_cmd += ["-c" ]
63+ if isinstance (objects , str ):
64+ objects = [objects ]
65+ cl_cmd += objects
66+ if options :
67+ cl_cmd += options
68+
69+ temp = tempdir ()
70+ dllmain_path = temp .relpath ("dllmain.cc" )
71+ with open (dllmain_path , "w" ) as dllmain_obj :
72+ dllmain_obj .write ('#include <windows.h>\
73+ BOOL APIENTRY DllMain( HMODULE hModule,\
74+ DWORD ul_reason_for_call,\
75+ LPVOID lpReserved)\
76+ {return TRUE;}' )
77+
78+ cl_cmd += [dllmain_path ]
79+
80+ temp_path = dllmain_path .replace ("dllmain.cc" , "" )
81+ cl_cmd += ["-Fo:" + temp_path ]
82+
83+ proc = subprocess .Popen (
84+ cl_cmd , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
85+ (out , _ ) = proc .communicate ()
86+ if proc .returncode != 0 :
87+ msg = "Compilation error:\n "
88+ msg += str (out )
89+ raise RuntimeError (msg )
90+ link_cmd = ["link" ]
91+ link_cmd += ["-dll" , "-FORCE:MULTIPLE" ]
92+
93+ for obj in objects :
94+ if obj .endswith (".cc" ):
95+ (_ , temp_file_name ) = os .path .split (obj )
96+ (shot_name , _ ) = os .path .splitext (temp_file_name )
97+ link_cmd += [os .path .join (temp_path , shot_name + ".obj" )]
98+ if obj .endswith (".o" ):
99+ link_cmd += [obj ]
100+
101+ link_cmd += ["-EXPORT:__tvm_main__" ]
102+ link_cmd += [temp_path + "dllmain.obj" ]
103+ link_cmd += ["-out:" + output ]
45104
105+ proc = subprocess .Popen (
106+ link_cmd , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
107+ (out , _ ) = proc .communicate ()
46108 if proc .returncode != 0 :
47109 msg = "Compilation error:\n "
48- msg += out
110+ msg += str (out )
111+
49112 raise RuntimeError (msg )
0 commit comments