44
55import tccbox
66
7+ system = platform .system ()
8+
79libtcc_dir = tccbox .tcc_lib_dir ()
810# windows: libtcc.dll
911# linux: libtcc.so
1214 "Windows" : "dll" ,
1315 "Linux" : "so" ,
1416 "Darwin" : "dylib" ,
15- }[platform . system () ]
17+ }[system ]
1618libtcc_path = os .path .join (libtcc_dir , f"libtcc.{ sharedlib_suffix } " )
1719
20+ # On Linux/Mac, tcc has lib/tcc/include/ and lib/tcc/libtcc1.a which must be included in compilation
21+ libtcc_extra_include_path = None
22+ libtcc_extra_lib_path = None
23+ libtcc_extra_lib_name = None
24+ if system in ["Linux" , "Darwin" ]:
25+ libtcc_extra_include_path = os .path .join (libtcc_dir , "tcc" , "include" )
26+ libtcc_extra_lib_path = os .path .join (libtcc_dir , "tcc" )
27+ libtcc_extra_lib_name = "libtcc1.a"
28+
1829# Define types
1930TCCState = ctypes .c_void_p
2031TCCReallocFunc = ctypes .CFUNCTYPE (ctypes .c_void_p , ctypes .c_void_p , ctypes .c_ulong )
@@ -49,6 +60,32 @@ def create_state(self):
4960 if self .libtcc .tcc_set_output_type (state , TCC_OUTPUT_MEMORY ) == - 1 :
5061 raise Exception ("Failed to set output type" )
5162
63+ # Add extra include path and library path
64+ if libtcc_extra_include_path :
65+ if (
66+ self .libtcc .tcc_add_include_path (
67+ state , libtcc_extra_include_path .encode ()
68+ )
69+ == - 1
70+ ):
71+ raise Exception ("Failed to add extra include path" )
72+ if (
73+ self .libtcc .tcc_add_sysinclude_path (
74+ state , libtcc_extra_include_path .encode ()
75+ )
76+ == - 1
77+ ):
78+ raise Exception ("Failed to add extra sysinclude path" )
79+ if libtcc_extra_lib_path :
80+ if (
81+ self .libtcc .tcc_add_library_path (state , libtcc_extra_lib_path .encode ())
82+ == - 1
83+ ):
84+ raise Exception ("Failed to add extra library path" )
85+ if libtcc_extra_lib_name :
86+ if self .libtcc .tcc_add_library (state , libtcc_extra_lib_name .encode ()) == - 1 :
87+ raise Exception ("Failed to add extra library" )
88+
5289 self .states .append (state )
5390
5491 return state
@@ -65,6 +102,14 @@ def _initialize_function_prototypes(self):
65102 libtcc .tcc_add_symbol .restype = ctypes .c_int
66103 libtcc .tcc_get_symbol .argtypes = [TCCState , ctypes .c_char_p ]
67104 libtcc .tcc_get_symbol .restype = ctypes .c_void_p
105+ libtcc .tcc_add_include_path .argtypes = [TCCState , ctypes .c_char_p ]
106+ libtcc .tcc_add_include_path .restype = ctypes .c_int
107+ libtcc .tcc_add_sysinclude_path .argtypes = [TCCState , ctypes .c_char_p ]
108+ libtcc .tcc_add_sysinclude_path .restype = ctypes .c_int
109+ libtcc .tcc_add_library_path .argtypes = [TCCState , ctypes .c_char_p ]
110+ libtcc .tcc_add_library_path .restype = ctypes .c_int
111+ libtcc .tcc_add_library .argtypes = [TCCState , ctypes .c_char_p ]
112+ libtcc .tcc_add_library .restype = ctypes .c_int
68113
69114 def compile_string (self , state , c_code : str ):
70115 if self .libtcc .tcc_compile_string (state , c_code .encode ()) == - 1 :
0 commit comments